diff --git a/swiftpm/Utilities/bootstrap b/swiftpm/Utilities/bootstrap index f7439427..5f284c48 100755 --- a/swiftpm/Utilities/bootstrap +++ b/swiftpm/Utilities/bootstrap @@ -167,7 +167,7 @@ def parse_build_args(args): args.clang_path = get_clang_path(args) args.cmake_path = get_cmake_path(args) args.ninja_path = get_ninja_path(args) - if args.cross_compile_hosts: # Use XCBuild target directory when building for multiple arches. + if args.cross_compile_hosts and not args.cross_compiling: # Use XCBuild target directory when building for multiple arches. args.target_dir = os.path.join(args.build_dir, "apple/Products") else: args.target_dir = os.path.join(args.build_dir, get_build_target(args)) @@ -232,7 +232,7 @@ def get_ninja_path(args): def get_build_target(args): """Returns the target-triple of the current machine.""" try: - target_info_json = subprocess.check_output([args.swiftc_path, '-print-target-info'], stderr=subprocess.PIPE, universal_newlines=True).strip() + target_info_json = subprocess.check_output([args.swiftc_path, '-print-target-info', '-target', '@SWIFT_ARCH@-unknown-linux-android'], stderr=subprocess.PIPE, universal_newlines=True).strip() args.target_info = json.loads(target_info_json) return args.target_info["target"]["unversionedTriple"] except Exception as e: @@ -255,13 +255,15 @@ def clean(args): def build(args): """Builds SwiftPM using a two-step process: first using CMake, then with itself.""" + args.cross_compiling = 'ANDROID_DATA' not in os.environ parse_build_args(args) # Build llbuild if its build path is not passed in. if not args.llbuild_build_dir: build_llbuild(args) - build_tsc(args) + if not args.cross_compiling: + build_tsc(args) build_swiftpm_with_cmake(args) build_swiftpm_with_swiftpm(args) @@ -366,7 +368,13 @@ def build_with_cmake(args, cmake_args, source_path, build_dir): """Runs CMake if needed, then builds with Ninja.""" cache_path = os.path.join(build_dir, "CMakeCache.txt") if args.reconfigure or not os.path.isfile(cache_path) or not args.swiftc_path in open(cache_path).read(): - swift_flags = "" + if args.cross_compiling: + # The termux prefix flag is needed because the Swift flags pass the + # standalone toolchain as the sdk, ie the sysroot. + swift_flags = os.getenv("TERMUX_SWIFTPM_FLAGS") + " -resource-dir @TERMUX_PREFIX@/lib/swift -Xcc -I@TERMUX_PREFIX@/include" + else: + swift_flags = "" + if args.sysroot: swift_flags = "-sdk %s" % args.sysroot @@ -390,6 +398,10 @@ def build_with_cmake(args, cmake_args, source_path, build_dir): if args.verbose: ninja_cmd.append("-v") + if args.cross_compiling: + ninja_cmd.append("PD4") + ninja_cmd.append("PD4_2") + call(ninja_cmd, cwd=build_dir, verbose=args.verbose) def build_llbuild(args): @@ -432,9 +444,11 @@ def build_swiftpm_with_cmake(args): cmake_flags = [ get_llbuild_cmake_arg(args), - "-DTSC_DIR=" + os.path.join(args.tsc_build_dir, "cmake/modules"), ] + if not args.cross_compiling: + cmake_flags.append("-DTSC_DIR=" + os.path.join(args.tsc_build_dir, "cmake/modules")) + if platform.system() == 'Darwin': cmake_flags.append("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target(args), g_macos_deployment_target)) cmake_flags.append("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target) @@ -451,12 +465,15 @@ def build_swiftpm_with_swiftpm(args): """Builds SwiftPM using the version of SwiftPM built with CMake.""" note("Building SwiftPM (with swift-build)") - swiftpm_args = [ - "SWIFT_EXEC=" + args.swiftc_path, - "SWIFTPM_PD_LIBS=" + os.path.join(args.bootstrap_dir, "pm"), - os.path.join(args.bootstrap_dir, "bin/swift-build"), - "--disable-sandbox", - ] + swiftpm_args = [ "SWIFT_EXEC=" + args.swiftc_path ] + + if args.cross_compiling: + swiftpm_args.append(os.path.join(os.path.split(args.swiftc_path)[0], "swift-build")) + else: + swiftpm_args.append("SWIFTPM_PD_LIBS=" + os.path.join(args.bootstrap_dir, "pm")) + swiftpm_args.append(os.path.join(args.bootstrap_dir, "bin/swift-build")) + + swiftpm_args.append("--disable-sandbox") build_target = get_build_target(args) cross_compile_hosts = args.cross_compile_hosts @@ -510,15 +529,16 @@ def get_swiftpm_env_cmd(args): env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1") env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target) - libs_joined = ":".join([ - os.path.join(args.bootstrap_dir, "lib"), - os.path.join(args.tsc_build_dir, "lib"), - os.path.join(args.llbuild_build_dir, "lib"), - ]) + if not args.cross_compiling: + libs_joined = ":".join([ + os.path.join(args.bootstrap_dir, "lib"), + os.path.join(args.tsc_build_dir, "lib"), + os.path.join(args.llbuild_build_dir, "lib"), + ]) if platform.system() == 'Darwin': env_cmd.append("DYLD_LIBRARY_PATH=%s" % libs_joined) - else: + elif not args.cross_compiling: env_cmd.append("LD_LIBRARY_PATH=%s" % libs_joined) return env_cmd @@ -600,13 +618,20 @@ def get_swiftpm_flags(args): ) # Don't use GNU strerror_r on Android. - if 'ANDROID_DATA' in os.environ: - build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"]) + build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"]) + build_flags.extend(["-Xswiftc", "-no-toolchain-stdlib-rpath"]) + build_flags.extend(["-Xlinker", "-landroid-spawn"]) build_target = get_build_target(args) cross_compile_hosts = args.cross_compile_hosts if build_target == 'x86_64-apple-macosx' and "macosx-arm64" in cross_compile_hosts: build_flags += ["--arch", "x86_64", "--arch", "arm64"] + elif args.cross_compiling and "android-@SWIFT_ARCH@" in cross_compile_hosts: + build_flags.extend([ + "--destination", "@TERMUX_PKG_BUILDDIR@/swiftpm-android-flags.json", + "-Xlinker", "-rpath", "-Xlinker", "@TERMUX_PREFIX@/lib", + "-Xcc", "-I@TERMUX_PREFIX@/include", + ]) elif cross_compile_hosts: error("cannot cross-compile for %s" % cross_compile_hosts)