Skip to content

Commit b77cd95

Browse files
committed
Create Python scripts for generating libldk binaries.
1 parent 673b550 commit b77cd95

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

src/scripts/__init__.py

Whitespace-only changes.

src/scripts/build_bulk_libldks.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 = 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)
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+
individual_configurations: [LibldkBuildConfiguration] = [
25+
LibldkBuildConfiguration('iphoneos', '', ['arm64']),
26+
LibldkBuildConfiguration('iphonesimulator', '-simulator', ['arm64', 'x86_64']),
27+
LibldkBuildConfiguration('macosx', '', ['arm64', 'x86_64']),
28+
LibldkBuildConfiguration('macosx', '-macabi', ['arm64', 'x86_64']),
29+
]
30+
31+
config = BuildConfig()
32+
config.LDK_C_BINDINGS_BASE = ldk_directory
33+
config.LDK_C_BINDINGS_DIRECTORY = c_bindings_directory
34+
config.LIBLDK_BUILD_CONFIGURATIONS = individual_configurations
35+
36+
return config
37+
38+
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')
44+
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
49+
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)
53+
54+
process = subprocess.check_call([sys.executable, individual_libldk_file], env=child_environment)
55+

src/scripts/build_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class LibldkBuildConfiguration:
2+
def __init__(self, platform: str, llvm_target_triple_suffix: str, architectures: [str]):
3+
self.platform = platform
4+
self.llvm_target_triple_suffix = llvm_target_triple_suffix
5+
self.architectures = architectures
6+
7+
8+
class BuildConfig:
9+
def __init__(self):
10+
self.LDK_C_BINDINGS_BASE: str = ''
11+
self.LDK_C_BINDINGS_DIRECTORY: str = ''
12+
self.LIBLDK_BUILD_CONFIGURATIONS: [LibldkBuildConfiguration] = []
13+
self.CONFIGURATION: str = ''
14+
self.RUST_CONFIGURATION: str = ''
15+
self.RUST_CONFIGURATION_FLAG: str = ''
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)