|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +from script_config import ScriptConfig, BuildConfig |
| 7 | + |
| 8 | +RUSTUP_PATH = os.getenv('HOME') + '/.cargo/bin/rustup' |
| 9 | +CARGO_PATH = os.getenv('HOME') + '/.cargo/bin/cargo' |
| 10 | + |
| 11 | + |
| 12 | +def run(config: ScriptConfig): |
| 13 | + if len(config.LIBLDK_BUILD_CONFIGURATIONS) != 1: |
| 14 | + print('Individual libldk build must have exactly 1 build configuration.') |
| 15 | + sys.exit(1) |
| 16 | + |
| 17 | + ldkBuildConfig = config.LIBLDK_BUILD_CONFIGURATIONS[0] |
| 18 | + platform = ldkBuildConfig.platform |
| 19 | + human_readable_platform = ldkBuildConfig.human_readable_platform |
| 20 | + llvm_target_triple_suffix = ldkBuildConfig.llvm_target_triple_suffix |
| 21 | + architectures = ldkBuildConfig.architectures |
| 22 | + |
| 23 | + build_products_directory = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../bindings/bin')) |
| 24 | + |
| 25 | + rust_target_os = 'ios' |
| 26 | + if platform == 'macosx' and llvm_target_triple_suffix == '-macabi': |
| 27 | + rust_target_os = 'ios-macabi' |
| 28 | + elif platform == 'macosx': |
| 29 | + rust_target_os = 'darwin' |
| 30 | + |
| 31 | + print(f'\n\nPlatform name: {platform} ({human_readable_platform})') |
| 32 | + print('Configuration:', config.RUST_CONFIGURATION) |
| 33 | + print('LLVM Target Triple Suffix:', llvm_target_triple_suffix) |
| 34 | + |
| 35 | + individual_architecture_binary_directory = os.path.join( |
| 36 | + build_products_directory, |
| 37 | + config.RUST_CONFIGURATION, |
| 38 | + human_readable_platform, |
| 39 | + 'architectures' |
| 40 | + ) |
| 41 | + lipo_binary_directory = os.path.join( |
| 42 | + build_products_directory, |
| 43 | + config.RUST_CONFIGURATION, |
| 44 | + human_readable_platform |
| 45 | + ) |
| 46 | + if config.LIPO_BINARY_OUTPUT_DIRECTORY: |
| 47 | + lipo_binary_directory = config.LIPO_BINARY_OUTPUT_DIRECTORY |
| 48 | + lipo_binary_output_path = os.path.join(lipo_binary_directory, 'libldk.a') |
| 49 | + |
| 50 | + print(individual_architecture_binary_directory) |
| 51 | + print(lipo_binary_directory) |
| 52 | + os.makedirs(lipo_binary_directory, exist_ok=True) |
| 53 | + |
| 54 | + child_environment = dict(os.environ) |
| 55 | + child_environment['RUSTFLAGS'] = '--cfg=c_bindings' |
| 56 | + child_environment['PATH'] = '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' |
| 57 | + |
| 58 | + subprocess.check_call([RUSTUP_PATH, 'override', 'set', 'nightly'], cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 59 | + |
| 60 | + lipo_executables_input: [str] = [] |
| 61 | + |
| 62 | + for current_architecture in architectures: |
| 63 | + current_architecture_binary_directory = os.path.join( |
| 64 | + individual_architecture_binary_directory, |
| 65 | + current_architecture |
| 66 | + ) |
| 67 | + print(f'\nCurrent architecture:', current_architecture) |
| 68 | + |
| 69 | + rust_architecture = current_architecture |
| 70 | + if rust_architecture == 'arm64': |
| 71 | + rust_architecture = 'aarch64' |
| 72 | + if platform == 'iphonesimulator': |
| 73 | + rust_target_os = 'ios-sim' |
| 74 | + elif platform == 'iphonesimulator': |
| 75 | + rust_target_os = 'ios' |
| 76 | + |
| 77 | + print('Rust architecture:', rust_architecture) |
| 78 | + print('Rust target OS:', rust_target_os) |
| 79 | + |
| 80 | + cargo_target_directory = os.path.join(config.LDK_C_BINDINGS_DIRECTORY, 'target') |
| 81 | + cargo_raw_binary_origin = os.path.join( |
| 82 | + cargo_target_directory, |
| 83 | + f'{rust_architecture}-apple-{rust_target_os}', |
| 84 | + config.RUST_CONFIGURATION, |
| 85 | + 'libldk.a' |
| 86 | + ) |
| 87 | + print('Raw binary origin:', cargo_raw_binary_origin) |
| 88 | + print('Raw binary target:', current_architecture_binary_directory) |
| 89 | + |
| 90 | + # create the directory if it doesn't exist |
| 91 | + os.makedirs(current_architecture_binary_directory, exist_ok=True) |
| 92 | + |
| 93 | + # stop the complaints about directories not being empty |
| 94 | + if os.path.isdir(cargo_target_directory): |
| 95 | + shutil.rmtree(cargo_target_directory) |
| 96 | + subprocess.check_call([CARGO_PATH, 'clean'], cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 97 | + |
| 98 | + # cargo build -Z build-std=panic_abort,std --features "std" --target "${RUST_ARCH}-apple-${RUST_TARGET_OS}" $RUST_CONFIGURATION_FLAG |
| 99 | + build_arguments = [CARGO_PATH, 'build', '-Z', 'build-std=panic_abort,std', '--features', 'std', '--target', f'{rust_architecture}-apple-{rust_target_os}'] |
| 100 | + if config.RUST_CONFIGURATION_FLAG: |
| 101 | + build_arguments.append(config.RUST_CONFIGURATION_FLAG) |
| 102 | + subprocess.check_call( |
| 103 | + build_arguments, |
| 104 | + env=child_environment, |
| 105 | + cwd=config.LDK_C_BINDINGS_DIRECTORY |
| 106 | + ) |
| 107 | + |
| 108 | + # copy the generated binary to a monitored directory |
| 109 | + subprocess.check_call(['cp', cargo_raw_binary_origin, current_architecture_binary_directory]) |
| 110 | + lipo_executables_input.append(os.path.join(current_architecture_binary_directory, 'libldk.a')) |
| 111 | + |
| 112 | + subprocess.check_call([RUSTUP_PATH, 'override', 'unset'], cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 113 | + |
| 114 | + # xcrun --sdk $PLATFORM_NAME lipo -create "${EXECUTABLES[@]}" -output "${LIPO_BINARY_DIR}/${TARGET_NAME}.a" |
| 115 | + subprocess.check_call( |
| 116 | + ['xcrun', '--sdk', platform, 'lipo', '-create', *lipo_executables_input, '-output', lipo_binary_output_path] |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + script_config = ScriptConfig.parse( |
| 122 | + allow_ldk_argument=False, |
| 123 | + parse_configuration=True, |
| 124 | + parse_lipo_output_directory=True |
| 125 | + ) |
| 126 | + |
| 127 | + platform = os.getenv('PLATFORM_NAME') |
| 128 | + llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX') |
| 129 | + architectures = os.getenv('ARCHS').split(' ') |
| 130 | + |
| 131 | + ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures) |
| 132 | + script_config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig] |
| 133 | + |
| 134 | + run(script_config) |
0 commit comments