Skip to content

Commit 5eeae5f

Browse files
committed
Create central build config parser, and allow bulk libldk builder to call individual libldk builder directly as an alternative to via shell invocation.
1 parent b77cd95 commit 5eeae5f

File tree

4 files changed

+150
-134
lines changed

4 files changed

+150
-134
lines changed

src/scripts/build_bulk_libldks.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,48 @@
22
import subprocess
33
import sys
44

5-
from build_config import BuildConfig, LibldkBuildConfiguration
5+
import build_individual_libldk
6+
from script_config import BuildConfig, LibldkBuildConfiguration
67

8+
CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL = False
79

8-
def parseConfig() -> BuildConfig:
9-
ldk_directory_string = sys.argv[1] if len(sys.argv) > 1 else 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)
1310

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)
11+
def parse_config() -> BuildConfig:
12+
config = BuildConfig.parse(allow_ldk_argument=True, parse_configuration=(not CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL))
1913

20-
if not os.path.isdir(c_bindings_directory):
21-
print('lightning-c-bindings is not a directory')
22-
sys.exit(1)
23-
24-
individual_configurations: [LibldkBuildConfiguration] = [
25-
LibldkBuildConfiguration('iphoneos', '', ['arm64']),
14+
individual_configurations: [LibldkBuildConfiguration] = [LibldkBuildConfiguration('iphoneos', '', ['arm64']),
2615
LibldkBuildConfiguration('iphonesimulator', '-simulator', ['arm64', 'x86_64']),
2716
LibldkBuildConfiguration('macosx', '', ['arm64', 'x86_64']),
28-
LibldkBuildConfiguration('macosx', '-macabi', ['arm64', 'x86_64']),
29-
]
17+
LibldkBuildConfiguration('macosx', '-macabi', ['arm64', 'x86_64']), ]
3018

31-
config = BuildConfig()
32-
config.LDK_C_BINDINGS_BASE = ldk_directory
33-
config.LDK_C_BINDINGS_DIRECTORY = c_bindings_directory
3419
config.LIBLDK_BUILD_CONFIGURATIONS = individual_configurations
3520

3621
return config
3722

3823

39-
config = parseConfig()
40-
child_environment = dict(os.environ)
41-
child_environment['LDK_C_BINDINGS_BASE'] = config.LDK_C_BINDINGS_BASE
42-
child_environment['LDK_C_BINDINGS_DIRECTORY'] = config.LDK_C_BINDINGS_DIRECTORY
43-
individual_libldk_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'build_individual_libldk.py')
24+
def run(config: BuildConfig):
25+
child_environment = dict(os.environ)
26+
child_environment['LDK_C_BINDINGS_BASE'] = config.LDK_C_BINDINGS_BASE
27+
child_environment['LDK_C_BINDINGS_DIRECTORY'] = config.LDK_C_BINDINGS_DIRECTORY
28+
individual_libldk_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'build_individual_libldk.py')
29+
30+
libldk_build_configurations = config.LIBLDK_BUILD_CONFIGURATIONS
31+
for current_build_config in libldk_build_configurations:
32+
current_platform: str = current_build_config.platform
33+
current_llvm_target_triple_suffix: str = current_build_config.llvm_target_triple_suffix
34+
current_architectures: [str] = current_build_config.architectures
4435

45-
for current_build_config in config.LIBLDK_BUILD_CONFIGURATIONS:
46-
current_platform: str = current_build_config.platform
47-
current_llvm_target_triple_suffix: str = current_build_config.llvm_target_triple_suffix
48-
current_architectures: [str] = current_build_config.architectures
36+
if CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL:
37+
child_environment['PLATFORM'] = current_platform
38+
child_environment['LLVM_TARGET_TRIPLE_SUFFIX'] = current_llvm_target_triple_suffix
39+
child_environment['ARCHS'] = ' '.join(current_architectures)
4940

50-
child_environment['PLATFORM'] = current_platform
51-
child_environment['LLVM_TARGET_TRIPLE_SUFFIX'] = current_llvm_target_triple_suffix
52-
child_environment['ARCHS'] = ' '.join(current_architectures)
41+
subprocess.check_call([sys.executable, individual_libldk_file], env=child_environment)
42+
else:
43+
current_config_clone = config
44+
current_config_clone.LIBLDK_BUILD_CONFIGURATIONS = [current_build_config]
45+
build_individual_libldk.run(config=current_config_clone)
5346

54-
process = subprocess.check_call([sys.executable, individual_libldk_file], env=child_environment)
5547

48+
if __name__ == '__main__':
49+
run(config=parse_config())

src/scripts/build_config.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/scripts/build_individual_libldk.py

Lines changed: 66 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,98 @@
22
import subprocess
33
import sys
44

5-
from build_config import BuildConfig, LibldkBuildConfiguration
5+
from script_config import BuildConfig, LibldkBuildConfiguration
66

77

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
1311

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)
1912

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.')
2216
sys.exit(1)
2317

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
2722

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'))
2924

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'
3432

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)
4236

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')
4440

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)
4844

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'
5247

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)
6049

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] = []
6451

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)
6855

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'
7263

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)
7566

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)
7770

78-
lipo_executables_input: [str] = []
71+
# create the directory if it doesn't exist
72+
os.makedirs(current_architecture_binary_directory, exist_ok=True)
7973

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)
8375

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)
9178

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'))
9482

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)
9884

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])
10187

102-
subprocess.check_call(['cargo', 'clean'], cwd=config.LDK_C_BINDINGS_DIRECTORY)
10388

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()
10691

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(' ')
11695

96+
ldkBuildConfig = LibldkBuildConfiguration(platform, llvm_target_triple_suffix, architectures)
97+
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
11798

99+
run(config=config)

src/scripts/script_config.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import sys
3+
4+
5+
class LibldkBuildConfiguration:
6+
def __init__(self, platform: str, llvm_target_triple_suffix: str, architectures: [str]):
7+
self.platform = platform
8+
self.llvm_target_triple_suffix = llvm_target_triple_suffix
9+
self.architectures = architectures
10+
11+
12+
class BuildConfig:
13+
def __init__(self):
14+
self.LDK_C_BINDINGS_BASE: str = ''
15+
self.LDK_C_BINDINGS_DIRECTORY: str = ''
16+
self.LIBLDK_BUILD_CONFIGURATIONS: [LibldkBuildConfiguration] = []
17+
self.CONFIGURATION: str = ''
18+
self.RUST_CONFIGURATION: str = ''
19+
self.RUST_CONFIGURATION_FLAG: str = ''
20+
21+
@classmethod
22+
def parse(cls, allow_ldk_argument=True, parse_configuration=False):
23+
24+
ldk_directory_string = os.getenv('LDK_C_BINDINGS_BASE')
25+
if allow_ldk_argument and len(sys.argv) > 1:
26+
ldk_directory_string = sys.argv[1]
27+
28+
if not ldk_directory_string:
29+
print('Missing LDK C-bindings base directory. Either call the value or set the environment variable.')
30+
sys.exit(1)
31+
32+
ldk_directory = os.path.realpath(ldk_directory_string)
33+
c_bindings_directory = os.path.join(ldk_directory, 'lightning-c-bindings')
34+
if not os.path.exists(c_bindings_directory):
35+
print('LDK C-bindings directory does not contain lightning-c-bindings')
36+
sys.exit(1)
37+
38+
if not os.path.isdir(c_bindings_directory):
39+
print('lightning-c-bindings is not a directory')
40+
sys.exit(1)
41+
42+
config = BuildConfig()
43+
config.LDK_C_BINDINGS_BASE = ldk_directory
44+
config.LDK_C_BINDINGS_DIRECTORY = c_bindings_directory
45+
46+
if parse_configuration:
47+
config.CONFIGURATION = os.getenv('CONFIGURATION')
48+
if config.CONFIGURATION == 'Debug':
49+
config.RUST_CONFIGURATION = 'debug'
50+
config.RUST_CONFIGURATION_FLAG = ''
51+
else:
52+
config.RUST_CONFIGURATION = 'release'
53+
config.RUST_CONFIGURATION_FLAG = '--release'
54+
55+
return config

0 commit comments

Comments
 (0)