@@ -528,7 +528,6 @@ extension VercelOutput {
528528 print ( " ------------------------------------------------------------------------- " )
529529 print ( " Building product: \" \( product. name) \" " )
530530 print ( " ------------------------------------------------------------------------- " )
531- print ( " " )
532531
533532 if isDeploy, Utils . isAmazonLinux == false {
534533 return try await buildDockerProduct ( product)
@@ -566,27 +565,80 @@ extension VercelOutput {
566565 ? " swiftlang/swift:nightly- \( context. package . toolsVersion. major) . \( context. package . toolsVersion. minor) -amazonlinux2 "
567566 : " swift: \( context. package . toolsVersion. major) . \( context. package . toolsVersion. minor) -amazonlinux2 "
568567
568+ let cleanCommand = arguments. contains ( " --clean " )
569+ ? " rm -rf .build && rm -rf ~/.swift/pm && "
570+ : " "
571+
572+ let buildCommand = " swift build -c release -Xswiftc -Osize -Xlinker -S --product \( product. name) --static-swift-stdlib "
573+ let buildOutputPathCommand = " \( cleanCommand) \( buildCommand) --show-bin-path "
574+
575+ let workspacePathPrefix = arguments. contains ( " --parent " )
576+ ? context. package . directory. removingLastComponent ( )
577+ : context. package . directory
578+
579+ let lastPathComponent = arguments. contains ( " --parent " )
580+ ? context. package . directory. lastComponent
581+ : " "
582+
583+ let dockerWorkspacePath = " /workspace/ \( lastPathComponent) "
584+
585+ // get the build output path
586+ let dockerBuildOutputPath = try Shell . execute (
587+ executable: dockerToolPath,
588+ arguments: [
589+ " run " ,
590+ " --platform " , " linux/ \( architecture. rawValue) " ,
591+ " --rm " ,
592+ " -v " , " \( workspacePathPrefix) :/workspace " ,
593+ " -w " , dockerWorkspacePath,
594+ baseImage,
595+ " bash " , " -cl " , " pwd && \( buildOutputPathCommand) "
596+ ]
597+ )
598+
599+ guard let buildPathOutput = dockerBuildOutputPath. split ( separator: " \n " ) . last else {
600+ throw BuildError . failedParsingDockerOutput ( dockerBuildOutputPath)
601+ }
602+
603+ let productPath = Path ( buildPathOutput. replacingOccurrences ( of: dockerWorkspacePath, with: context. package . directory. string) )
604+
569605 // build the product
570606 try Shell . execute (
571607 executable: dockerToolPath,
572608 arguments: [
573609 " run " ,
574610 " --platform " , " linux/ \( architecture. rawValue) " ,
575611 " --rm " ,
576- " -v " , " \( context . package . directory . string ) :/workspace " ,
577- " -w " , " /workspace " ,
612+ " -v " , " \( workspacePathPrefix ) :/workspace " ,
613+ " -w " , dockerWorkspacePath ,
578614 baseImage,
579615 " bash " , " -cl " , " swift build -c release -Xswiftc -Osize --static-swift-stdlib " ,
580616 ]
581617 )
582618
583619 // ensure the final binary built correctly
584- let productPath = swiftBuildReleaseDirectory. appending ( product. name)
585- guard fs. fileExists ( atPath: productPath . string) else {
620+ let productPathFinal = swiftBuildReleaseDirectory. appending ( product. name)
621+ guard fs. fileExists ( atPath: productPathFinal . string) else {
586622 Diagnostics . error ( " expected ' \( product. name) ' binary at \" \( productPath. string) \" " )
587623 throw BuildError . productExecutableNotFound ( product. name)
588624 }
589- return productPath
625+
626+ // strip the binary
627+ let stripCommand = " ls -la .build/release/ \( product. name) && strip .build/release/ \( product. name) && ls -la .build/release/ \( product. name) "
628+ try Shell . execute (
629+ executable: dockerToolPath,
630+ arguments: [
631+ " run " ,
632+ " --platform " , " linux/ \( architecture. rawValue) " ,
633+ " --rm " ,
634+ " -v " , " \( workspacePathPrefix) :/workspace " ,
635+ " -w " , dockerWorkspacePath,
636+ baseImage,
637+ " bash " , " -cl " , stripCommand
638+ ]
639+ )
640+
641+ return productPathFinal
590642 }
591643}
592644
0 commit comments