Skip to content

Commit 20d0e79

Browse files
committed
Move Xcode build phase script logic to a separate Python script.
1 parent e360ff6 commit 20d0e79

File tree

7 files changed

+2254
-2227
lines changed

7 files changed

+2254
-2227
lines changed

src/scripts/build_individual_libldk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def run(config: ScriptConfig):
4747
lipo_binary_directory = config.LIPO_BINARY_OUTPUT_DIRECTORY
4848
lipo_binary_output_path = os.path.join(lipo_binary_directory, 'libldk.a')
4949

50-
print(individual_architecture_binary_directory)
51-
print(lipo_binary_directory)
50+
print('Building lipo binary into', lipo_binary_output_path)
51+
5252
os.makedirs(lipo_binary_directory, exist_ok=True)
5353

5454
child_environment = dict(os.environ)
@@ -92,7 +92,7 @@ def run(config: ScriptConfig):
9292

9393
# stop the complaints about directories not being empty
9494
if os.path.isdir(cargo_target_directory):
95-
shutil.rmtree(cargo_target_directory)
95+
shutil.rmtree(cargo_target_directory, ignore_errors=True)
9696
subprocess.check_call([CARGO_PATH, 'clean'], cwd=config.LDK_C_BINDINGS_DIRECTORY)
9797

9898
# cargo build -Z build-std=panic_abort,std --features "std" --target "${RUST_ARCH}-apple-${RUST_TARGET_OS}" $RUST_CONFIGURATION_FLAG

src/scripts/prepare_headers.py renamed to src/scripts/copy_c_files.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def run(config: ScriptConfig):
1010
os.path.join(config.LDK_C_BINDINGS_BASE, 'ldk-net'),
1111
]
1212

13-
header_destination_directory = os.path.realpath(
14-
os.path.join(os.path.dirname(__file__), '../../xcode/LDKFramework/headers')
15-
)
13+
header_destination_directory = config.C_FILE_OUTPUT_DIRECTORY
1614
os.makedirs(header_destination_directory, exist_ok=True)
1715

16+
print('Copying headers to', header_destination_directory)
17+
1818
for current_directory in header_directories:
1919
for current_file in os.listdir(current_directory):
2020

@@ -26,9 +26,10 @@ def run(config: ScriptConfig):
2626
if not os.path.isfile(current_path):
2727
continue
2828

29+
print('Copying', current_path)
2930
subprocess.check_call(['cp', current_path, header_destination_directory])
3031

3132

3233
if __name__ == '__main__':
33-
script_config = ScriptConfig.parse(allow_ldk_argument=True)
34+
script_config = ScriptConfig.parse(allow_ldk_argument=True, parse_as_xcode_invocation=True)
3435
run(script_config)

src/scripts/generate_xcframework.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def run(config: ScriptConfig):
5959

6060
# create clean derived data directory
6161
if os.path.exists(derived_data_directory):
62-
shutil.rmtree(derived_data_directory)
62+
shutil.rmtree(derived_data_directory, ignore_errors=True)
6363
os.makedirs(derived_data_directory, exist_ok=False)
6464

6565
child_environment['LDK_C_BINDINGS_BINARY_DIRECTORY'] = lipo_binary_directory
@@ -86,7 +86,7 @@ def run(config: ScriptConfig):
8686
)
8787

8888
# clean up the derived data
89-
shutil.rmtree(derived_data_directory)
89+
shutil.rmtree(derived_data_directory, ignore_errors=True)
9090

9191
# XCFRAMEWORK_INPUT_FLAGS="${XCFRAMEWORK_INPUT_FLAGS}-framework ${CURRENT_ARCHIVE_PATH}.xcarchive/Products/Library/Frameworks/LDKFramework.framework "
9292
framework_input_flags += [
@@ -96,7 +96,7 @@ def run(config: ScriptConfig):
9696

9797
# xcodebuild -create-xcframework ${XCFRAMEWORK_INPUT_FLAGS} -output ${XCFRAMEWORK_OUTPUT_PATH}"
9898
if os.path.exists(xcframework_output_path):
99-
shutil.rmtree(xcframework_output_path)
99+
shutil.rmtree(xcframework_output_path, ignore_errors=True)
100100

101101
subprocess.check_call(
102102
['xcodebuild', '-create-xcframework', *framework_input_flags, '-output', xcframework_output_path]

src/scripts/prepare_xcode_project.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import sys
3+
4+
from script_config import ScriptConfig
5+
import copy_c_files
6+
import build_individual_libldk
7+
8+
9+
10+
def run(config: ScriptConfig):
11+
print('Preparing Xcode project…')
12+
project_directory = os.getenv('PROJECT_DIR')
13+
14+
bindings_binary_directory = os.getenv('LDK_C_BINDINGS_BINARY_DIRECTORY')
15+
forced_rebuild_override_directory = os.getenv('LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY')
16+
17+
print('PROJECT_DIR:', project_directory)
18+
print('LDK_C_BINDINGS_BINARY_DIRECTORY:', bindings_binary_directory)
19+
print('LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY:', forced_rebuild_override_directory)
20+
21+
# step 1: copy C files, *.h and *.c
22+
copy_c_files.run(config)
23+
24+
if bindings_binary_directory and forced_rebuild_override_directory:
25+
print('LDK_C_BINDINGS_BINARY_DIRECTORY and LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY may not both be set at the same time!')
26+
sys.exit(1)
27+
28+
if not bindings_binary_directory and not forced_rebuild_override_directory:
29+
print('One of LDK_C_BINDINGS_BINARY_DIRECTORY and LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY must be set!')
30+
sys.exit(1)
31+
32+
if not forced_rebuild_override_directory:
33+
print('LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY not set: binary rebuild not necessary.')
34+
return
35+
36+
# if and only if and if only LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY is set, we build the individual libldk
37+
config.LIPO_BINARY_OUTPUT_DIRECTORY = forced_rebuild_override_directory
38+
# TODO: detect if there's already a libldk.a in that directory and then not do the build
39+
build_individual_libldk.run(config)
40+
41+
42+
if __name__ == '__main__':
43+
script_config = ScriptConfig.parse(
44+
allow_ldk_argument=False,
45+
parse_configuration=True,
46+
parse_lipo_output_directory=True,
47+
parse_as_xcode_invocation=True
48+
)
49+
run(script_config)

src/scripts/script_config.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ def __init__(self):
3232
self.RUST_CONFIGURATION: str = ''
3333
self.RUST_CONFIGURATION_FLAG: str = ''
3434
self.LIPO_BINARY_OUTPUT_DIRECTORY: str = ''
35+
self.C_FILE_OUTPUT_DIRECTORY: str = ''
3536

3637
@classmethod
37-
def parse(cls, allow_ldk_argument=True, parse_configuration=False, parse_lipo_output_directory=False):
38+
def parse(cls, allow_ldk_argument=True, parse_configuration=False, parse_lipo_output_directory=False, parse_as_xcode_invocation=False):
3839

3940
ldk_directory_string = os.getenv('LDK_C_BINDINGS_BASE')
4041
if allow_ldk_argument and len(sys.argv) > 1:
@@ -76,4 +77,23 @@ def parse(cls, allow_ldk_argument=True, parse_configuration=False, parse_lipo_ou
7677
if parse_lipo_output_directory:
7778
config.LIPO_BINARY_OUTPUT_DIRECTORY = os.getenv('LDK_C_BINDINGS_BINARY_DIRECTORY')
7879

80+
if parse_as_xcode_invocation:
81+
# this script is being called from xcode
82+
# that means we might get a PROJECT_DIR and a PLATFORM, ARCHS, etc.
83+
84+
# parse C file destination directory
85+
config.C_FILE_OUTPUT_DIRECTORY = os.path.join(os.path.dirname(__file__), '../../xcode/LDKFramework/headers')
86+
project_directory = os.getenv('PROJECT_DIR')
87+
if project_directory:
88+
config.C_FILE_OUTPUT_DIRECTORY = os.path.join(os.path.realpath(project_directory), 'headers')
89+
90+
# parse build config aspect
91+
platform = os.getenv('PLATFORM_NAME')
92+
llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX')
93+
architectures = os.getenv('ARCHS').split(' ')
94+
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
95+
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
96+
97+
pass
98+
7999
return config

0 commit comments

Comments
 (0)