|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | +from build_config import BuildConfig, LibldkBuildConfiguration |
| 6 | + |
| 7 | + |
| 8 | +def parseConfig() -> BuildConfig: |
| 9 | + ldk_directory_string = os.getenv('LDK_C_BINDINGS_BASE') |
| 10 | + if not ldk_directory_string: |
| 11 | + print('Missing LDK C-bindings base directory. Either call the value or set the environment variable.') |
| 12 | + sys.exit(1) |
| 13 | + |
| 14 | + ldk_directory = os.path.realpath(ldk_directory_string) |
| 15 | + c_bindings_directory = os.path.join(ldk_directory, 'lightning-c-bindings') |
| 16 | + if not os.path.exists(c_bindings_directory): |
| 17 | + print('LDK C-bindings directory does not contain lightning-c-bindings') |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + if not os.path.isdir(c_bindings_directory): |
| 21 | + print('lightning-c-bindings is not a directory') |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + platform = os.getenv('PLATFORM') |
| 25 | + llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX') |
| 26 | + architectures = os.getenv('ARCHS').split(' ') |
| 27 | + |
| 28 | + ldkBuildConfig = LibldkBuildConfiguration(platform, llvm_target_triple_suffix, architectures) |
| 29 | + |
| 30 | + config = BuildConfig() |
| 31 | + config.LDK_C_BINDINGS_BASE = ldk_directory |
| 32 | + config.LDK_C_BINDINGS_DIRECTORY = c_bindings_directory |
| 33 | + config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig] |
| 34 | + |
| 35 | + config.CONFIGURATION = os.getenv('CONFIGURATION') |
| 36 | + if config.CONFIGURATION == 'Debug': |
| 37 | + config.RUST_CONFIGURATION = 'debug' |
| 38 | + config.RUST_CONFIGURATION_FLAG = '' |
| 39 | + else: |
| 40 | + config.RUST_CONFIGURATION = 'release' |
| 41 | + config.RUST_CONFIGURATION_FLAG = '--release' |
| 42 | + |
| 43 | + return config |
| 44 | + |
| 45 | +config = parseConfig() |
| 46 | +ldkBuildConfig: LibldkBuildConfiguration = config.LIBLDK_BUILD_CONFIGURATIONS[0] |
| 47 | +build_products_directory = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../bindings/bin')) |
| 48 | + |
| 49 | +platform = ldkBuildConfig.platform |
| 50 | +llvm_target_triple_suffix = ldkBuildConfig.llvm_target_triple_suffix |
| 51 | +architectures = ldkBuildConfig.architectures |
| 52 | + |
| 53 | +human_readable_platform = platform |
| 54 | +rust_target_os = 'ios' |
| 55 | +if platform == 'macosx' and llvm_target_triple_suffix == '-macabi': |
| 56 | + human_readable_platform = 'catalyst' |
| 57 | + rust_target_os = 'ios-macabi' |
| 58 | +elif platform == 'macosx': |
| 59 | + rust_target_os = 'darwin' |
| 60 | + |
| 61 | +print(f'\n\nPlatform name: {platform} ({human_readable_platform})') |
| 62 | +print('Configuration:', config.RUST_CONFIGURATION) |
| 63 | +print('LLVM Target Triple Suffix:', llvm_target_triple_suffix) |
| 64 | + |
| 65 | +individual_architecture_binary_directory = os.path.join(build_products_directory, config.RUST_CONFIGURATION, human_readable_platform, 'raw') |
| 66 | +lipo_binary_directory = os.path.join(build_products_directory, config.RUST_CONFIGURATION, human_readable_platform, 'lipo') |
| 67 | +lipo_binary_output_path = os.path.join(lipo_binary_directory, 'libldk.a') |
| 68 | + |
| 69 | +print(individual_architecture_binary_directory) |
| 70 | +print(lipo_binary_directory) |
| 71 | +os.makedirs(lipo_binary_directory, exist_ok=True) |
| 72 | + |
| 73 | +child_environment = dict(os.environ) |
| 74 | +child_environment['RUSTFLAGS'] = '--cfg=c_bindings' |
| 75 | + |
| 76 | +subprocess.check_call(['rustup', 'override', 'set', 'nightly'], cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 77 | + |
| 78 | +lipo_executables_input: [str] = [] |
| 79 | + |
| 80 | +for current_architecture in architectures: |
| 81 | + current_architecture_binary_directory = os.path.join(individual_architecture_binary_directory, current_architecture) |
| 82 | + print(f'\nCurrent architecture:', current_architecture) |
| 83 | + |
| 84 | + rust_architecture = current_architecture |
| 85 | + if rust_architecture == 'arm64': |
| 86 | + rust_architecture = 'aarch64' |
| 87 | + if platform == 'iphonesimulator': |
| 88 | + rust_target_os = 'ios-sim' |
| 89 | + elif platform == 'iphonesimulator': |
| 90 | + rust_target_os = 'ios' |
| 91 | + |
| 92 | + print('Rust architecture:', rust_architecture) |
| 93 | + print('Rust target OS:', rust_target_os) |
| 94 | + |
| 95 | + cargo_raw_binary_origin = os.path.join(config.LDK_C_BINDINGS_DIRECTORY, 'target', f'{rust_architecture}-apple-{rust_target_os}', config.RUST_CONFIGURATION, 'libldk.a') |
| 96 | + print('Raw binary origin:', cargo_raw_binary_origin) |
| 97 | + print('Raw binary target:', current_architecture_binary_directory) |
| 98 | + |
| 99 | + # create the directory if it doesn't exist |
| 100 | + os.makedirs(current_architecture_binary_directory, exist_ok=True) |
| 101 | + |
| 102 | + subprocess.check_call(['cargo', 'clean'], cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 103 | + |
| 104 | + # cargo build -Z build-std=panic_abort,std --features "std" --target "${RUST_ARCH}-apple-${RUST_TARGET_OS}" $RUST_CONFIGURATION_FLAG |
| 105 | + subprocess.check_call(['cargo', 'build', '-Z', 'build-std=panic_abort,std', '--features', 'std', '--target', f'{rust_architecture}-apple-{rust_target_os}', config.RUST_CONFIGURATION_FLAG], env=child_environment, cwd=config.LDK_C_BINDINGS_DIRECTORY) |
| 106 | + |
| 107 | + # copy the generated binary to a monitored directory |
| 108 | + subprocess.check_call(['cp', cargo_raw_binary_origin, current_architecture_binary_directory]) |
| 109 | + lipo_executables_input.append(os.path.join(current_architecture_binary_directory, 'libldk.a')) |
| 110 | + |
| 111 | + |
| 112 | +subprocess.check_call(['rustup', '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(['xcrun', '--sdk', platform, 'lipo', '-create', *lipo_executables_input, '-output', lipo_binary_output_path]) |
| 116 | + |
| 117 | + |
0 commit comments