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