Skip to content

Commit 69db7ab

Browse files
authored
Merge pull request #57 from lightningdevkit/2022-07-python-build-scripts
Create Python scripts for generating libldk binaries.
2 parents 673b550 + d8e4846 commit 69db7ab

File tree

11 files changed

+547
-81
lines changed

11 files changed

+547
-81
lines changed
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
name: Build For Testing
22
inputs:
3-
scheme:
4-
description: Scheme
5-
required: true
6-
destination:
7-
description: Destination
8-
required: true
9-
project:
10-
description: Project directory, relative to the xcode folder
11-
required: true
3+
scheme:
4+
description: Scheme
5+
required: true
6+
destination:
7+
description: Destination
8+
required: true
9+
project:
10+
description: Project directory, relative to the xcode folder
11+
required: true
1212
description: Build for testing.
1313
runs:
14-
using: composite
15-
steps:
16-
- shell: bash
17-
working-directory: .
18-
run: |
19-
cd xcode
14+
using: composite
15+
steps:
16+
- shell: bash
17+
working-directory: .
18+
run: |
19+
cd xcode
2020
21-
xcodebuild clean build-for-testing \
22-
-allowProvisioningUpdates \
23-
-allowProvisioningDeviceRegistration \
24-
-project '${{ inputs.project }}' \
25-
-scheme '${{ inputs.scheme }}' \
26-
-destination '${{ inputs.destination }}' \
27-
-resultBundlePath BuildResults.xcresult \
28-
CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
21+
LDK_C_BINDINGS_BINARY_DIRECTORY="" \
22+
xcodebuild clean build-for-testing \
23+
-allowProvisioningUpdates \
24+
-allowProvisioningDeviceRegistration \
25+
-project '${{ inputs.project }}' \
26+
-scheme '${{ inputs.scheme }}' \
27+
-destination '${{ inputs.destination }}' \
28+
-resultBundlePath BuildResults.xcresult \
29+
CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO \
30+
LDK_C_BINDINGS_BASE="${LDK_C_BINDINGS_BASE}" \
31+
LDK_C_BINDINGS_BINARY_DIRECTORY="" \
32+
LDK_C_BINDINGS_BINARY_FORCED_REBUILD_OUTPUT_DIRECTORY="${LDK_C_BINDINGS_BINARY_DIRECTORY}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Generate XCFramework
2+
description: Build xcarchives and combine them into an xcframework using Python scripts..
3+
runs:
4+
using: composite
5+
steps:
6+
- name: Build binaries
7+
shell: bash
8+
run:
9+
python3 ./src/scripts/build_bulk_libldks.py
10+
- name: Generate xcframework
11+
shell: bash
12+
run:
13+
python3 ./src/scripts/generate_xcframework.py

.github/workflows/build-frameworks.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build Frameworks
1+
name: Xcode Builds
22
permissions:
33
contents: read
44
statuses: read
@@ -15,7 +15,7 @@ on:
1515

1616
jobs:
1717
build:
18-
name: Build Framework (${{ matrix.configuration['platform'] }})
18+
name: Build XCArchive for ${{ matrix.configuration['platform'] }}
1919
runs-on: macos-12
2020
strategy:
2121
fail-fast: false
@@ -24,19 +24,19 @@ jobs:
2424
- scheme: LDKFramework
2525
destination: generic/platform=iOS
2626
platform: iOS
27-
project: LDK/LDKFramework.xcodeproj
27+
project: LDKFramework/LDKFramework.xcodeproj
2828
- scheme: LDKFramework
2929
destination: platform=iOS Simulator,OS=15.2,name=iPhone 13 Pro
3030
platform: iOS Simulator
31-
project: LDK/LDKFramework.xcodeproj
32-
- scheme: LDKFramework_Mac
31+
project: LDKFramework/LDKFramework.xcodeproj
32+
- scheme: LDKFramework
3333
destination: generic/platform=OS X
3434
platform: OS X
35-
project: LDK/LDKFramework.xcodeproj
35+
project: LDKFramework/LDKFramework.xcodeproj
3636
- scheme: LDKFramework
3737
destination: platform=macOS,variant=Mac Catalyst,arch=x86_64
3838
platform: Mac Catalyst
39-
project: LDK/LDKFramework.xcodeproj
39+
project: LDKFramework/LDKFramework.xcodeproj
4040
env:
4141
DEVELOPER_DIR: /Applications/Xcode_13.2.1.app/Contents/Developer
4242
LDK_C_BINDINGS_BASE: /Users/runner/work/ldk-swift/ldk-swift/bindings/artifacts/ldk-c-bindings
@@ -57,3 +57,20 @@ jobs:
5757
project: ${{ matrix.configuration['project'] }}
5858
scheme: ${{ matrix.configuration['scheme'] }}
5959
destination: ${{ matrix.configuration['destination'] }}
60+
generate:
61+
name: Generate XCFramework
62+
runs-on: macos-12
63+
env:
64+
DEVELOPER_DIR: /Applications/Xcode_13.2.1.app/Contents/Developer
65+
LDK_C_BINDINGS_BASE: /Users/runner/work/ldk-swift/ldk-swift/bindings/artifacts/ldk-c-bindings
66+
steps:
67+
- name: Configure Xcode
68+
uses: maxim-lobanov/setup-xcode@v1
69+
with:
70+
xcode-version: latest
71+
- name: Checkout
72+
uses: actions/checkout@v2
73+
- name: Install Dependencies
74+
uses: ./.github/actions/install-dependencies
75+
- name: Generate XCFramework
76+
uses: ./.github/actions/generate-xcframework

.github/workflows/swift.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Check Bindings
1+
name: Test Bindings
22

33
on:
44
push:
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
check_bindings:
11-
name: Build Bindings
11+
name: Swift Package Manager Tests
1212
runs-on: ubuntu-20.04
1313
env:
1414
TOOLCHAIN: stable
@@ -54,12 +54,10 @@ jobs:
5454
cp ldk-c-bindings/lightning-c-bindings/include/*.h ./LDKSwift/Sources/LDKHeaders/include/
5555
cp ldk-c-bindings/ldk-net/ldk_net.h ./LDKSwift/Sources/LDKHeaders/include/
5656
cp ldk-c-bindings/ldk-net/ldk_net.c ./LDKSwift/Sources/LDKHeaders/
57-
- name: Set up Python
58-
uses: actions/setup-python@v2
5957
- name: Generate (copy for debugging) Swift bindings and copy batteries
6058
run: |
6159
pwd
62-
python ./ # Generate bindings into ci subdirectory
60+
python3 ./ # Generate bindings into ci subdirectory
6361
6462
# TODO: switch back to generation method after debugging
6563
# mkdir -p ci/LDKSwift/Sources/LDKSwift
@@ -69,7 +67,7 @@ jobs:
6967
LDK_SWIFT_GENERATOR_OUTPUT_DIRECTORY_PATH: ci/LDKSwift/Sources/LDKSwift/bindings
7068
- name: Check that the latest auto-generated Swift files are in the repo
7169
run: |
72-
python ./ # Generate bindings into local directory
70+
python3 ./ # Generate bindings into local directory
7371
git diff --exit-code ci/LDKSwift/Sources/LDKSwift ':(exclude)ci/LDKSwift/Sources/LDKSwift/bindings/Bindings.swift'
7472
env:
7573
LDK_SWIFT_GENERATOR_INPUT_HEADER_PATH: ci/ldk-c-bindings/lightning-c-bindings/include/lightning.h

src/scripts/__init__.py

Whitespace-only changes.

src/scripts/build_bulk_libldks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
import build_individual_libldk
6+
from script_config import ScriptConfig, BuildConfig
7+
8+
CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL = False
9+
10+
11+
def parse_config() -> ScriptConfig:
12+
config = ScriptConfig.parse(
13+
allow_ldk_argument=True,
14+
parse_configuration=(not CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL)
15+
)
16+
17+
individual_configurations: [BuildConfig] = [
18+
BuildConfig('iphoneos', '', ['arm64']),
19+
BuildConfig('iphonesimulator', '-simulator', ['arm64', 'x86_64']),
20+
BuildConfig('macosx', '', ['arm64', 'x86_64']),
21+
BuildConfig('macosx', '-macabi', ['arm64', 'x86_64'])
22+
]
23+
config.LIBLDK_BUILD_CONFIGURATIONS = individual_configurations
24+
25+
return config
26+
27+
28+
def run(config: ScriptConfig):
29+
child_environment = dict(os.environ)
30+
child_environment['LDK_C_BINDINGS_BASE'] = config.LDK_C_BINDINGS_BASE
31+
child_environment['LDK_C_BINDINGS_DIRECTORY'] = config.LDK_C_BINDINGS_DIRECTORY
32+
individual_libldk_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'build_individual_libldk.py')
33+
34+
libldk_build_configurations = config.LIBLDK_BUILD_CONFIGURATIONS
35+
for current_build_config in libldk_build_configurations:
36+
current_platform: str = current_build_config.platform
37+
current_llvm_target_triple_suffix: str = current_build_config.llvm_target_triple_suffix
38+
current_architectures: [str] = current_build_config.architectures
39+
40+
if CALL_INDIVIDUAL_BUILD_METHOD_VIA_SHELL:
41+
child_environment['PLATFORM'] = current_platform
42+
child_environment['LLVM_TARGET_TRIPLE_SUFFIX'] = current_llvm_target_triple_suffix
43+
child_environment['ARCHS'] = ' '.join(current_architectures)
44+
45+
subprocess.check_call([sys.executable, individual_libldk_file], env=child_environment)
46+
else:
47+
current_config_clone = config
48+
current_config_clone.LIBLDK_BUILD_CONFIGURATIONS = [current_build_config]
49+
build_individual_libldk.run(config=current_config_clone)
50+
51+
52+
if __name__ == '__main__':
53+
run(config=parse_config())
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import sys
5+
6+
from script_config import ScriptConfig, BuildConfig
7+
8+
RUSTUP_PATH = os.getenv('HOME') + '/.cargo/bin/rustup'
9+
CARGO_PATH = os.getenv('HOME') + '/.cargo/bin/cargo'
10+
11+
12+
def run(config: ScriptConfig):
13+
if len(config.LIBLDK_BUILD_CONFIGURATIONS) != 1:
14+
print('Individual libldk build must have exactly 1 build configuration.')
15+
sys.exit(1)
16+
17+
ldkBuildConfig = config.LIBLDK_BUILD_CONFIGURATIONS[0]
18+
platform = ldkBuildConfig.platform
19+
human_readable_platform = ldkBuildConfig.human_readable_platform
20+
llvm_target_triple_suffix = ldkBuildConfig.llvm_target_triple_suffix
21+
architectures = ldkBuildConfig.architectures
22+
23+
build_products_directory = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../bindings/bin'))
24+
25+
rust_target_os = 'ios'
26+
if platform == 'macosx' and llvm_target_triple_suffix == '-macabi':
27+
rust_target_os = 'ios-macabi'
28+
elif platform == 'macosx':
29+
rust_target_os = 'darwin'
30+
31+
print(f'\n\nPlatform name: {platform} ({human_readable_platform})')
32+
print('Configuration:', config.RUST_CONFIGURATION)
33+
print('LLVM Target Triple Suffix:', llvm_target_triple_suffix)
34+
35+
individual_architecture_binary_directory = os.path.join(
36+
build_products_directory,
37+
config.RUST_CONFIGURATION,
38+
human_readable_platform,
39+
'architectures'
40+
)
41+
lipo_binary_directory = os.path.join(
42+
build_products_directory,
43+
config.RUST_CONFIGURATION,
44+
human_readable_platform
45+
)
46+
if config.LIPO_BINARY_OUTPUT_DIRECTORY:
47+
lipo_binary_directory = config.LIPO_BINARY_OUTPUT_DIRECTORY
48+
lipo_binary_output_path = os.path.join(lipo_binary_directory, 'libldk.a')
49+
50+
print(individual_architecture_binary_directory)
51+
print(lipo_binary_directory)
52+
os.makedirs(lipo_binary_directory, exist_ok=True)
53+
54+
child_environment = dict(os.environ)
55+
child_environment['RUSTFLAGS'] = '--cfg=c_bindings'
56+
child_environment['PATH'] = '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
57+
58+
subprocess.check_call([RUSTUP_PATH, 'override', 'set', 'nightly'], cwd=config.LDK_C_BINDINGS_DIRECTORY)
59+
60+
lipo_executables_input: [str] = []
61+
62+
for current_architecture in architectures:
63+
current_architecture_binary_directory = os.path.join(
64+
individual_architecture_binary_directory,
65+
current_architecture
66+
)
67+
print(f'\nCurrent architecture:', current_architecture)
68+
69+
rust_architecture = current_architecture
70+
if rust_architecture == 'arm64':
71+
rust_architecture = 'aarch64'
72+
if platform == 'iphonesimulator':
73+
rust_target_os = 'ios-sim'
74+
elif platform == 'iphonesimulator':
75+
rust_target_os = 'ios'
76+
77+
print('Rust architecture:', rust_architecture)
78+
print('Rust target OS:', rust_target_os)
79+
80+
cargo_target_directory = os.path.join(config.LDK_C_BINDINGS_DIRECTORY, 'target')
81+
cargo_raw_binary_origin = os.path.join(
82+
cargo_target_directory,
83+
f'{rust_architecture}-apple-{rust_target_os}',
84+
config.RUST_CONFIGURATION,
85+
'libldk.a'
86+
)
87+
print('Raw binary origin:', cargo_raw_binary_origin)
88+
print('Raw binary target:', current_architecture_binary_directory)
89+
90+
# create the directory if it doesn't exist
91+
os.makedirs(current_architecture_binary_directory, exist_ok=True)
92+
93+
# stop the complaints about directories not being empty
94+
if os.path.isdir(cargo_target_directory):
95+
shutil.rmtree(cargo_target_directory)
96+
subprocess.check_call([CARGO_PATH, 'clean'], cwd=config.LDK_C_BINDINGS_DIRECTORY)
97+
98+
# cargo build -Z build-std=panic_abort,std --features "std" --target "${RUST_ARCH}-apple-${RUST_TARGET_OS}" $RUST_CONFIGURATION_FLAG
99+
build_arguments = [CARGO_PATH, 'build', '-Z', 'build-std=panic_abort,std', '--features', 'std', '--target', f'{rust_architecture}-apple-{rust_target_os}']
100+
if config.RUST_CONFIGURATION_FLAG:
101+
build_arguments.append(config.RUST_CONFIGURATION_FLAG)
102+
subprocess.check_call(
103+
build_arguments,
104+
env=child_environment,
105+
cwd=config.LDK_C_BINDINGS_DIRECTORY
106+
)
107+
108+
# copy the generated binary to a monitored directory
109+
subprocess.check_call(['cp', cargo_raw_binary_origin, current_architecture_binary_directory])
110+
lipo_executables_input.append(os.path.join(current_architecture_binary_directory, 'libldk.a'))
111+
112+
subprocess.check_call([RUSTUP_PATH, '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(
116+
['xcrun', '--sdk', platform, 'lipo', '-create', *lipo_executables_input, '-output', lipo_binary_output_path]
117+
)
118+
119+
120+
if __name__ == '__main__':
121+
script_config = ScriptConfig.parse(
122+
allow_ldk_argument=False,
123+
parse_configuration=True,
124+
parse_lipo_output_directory=True
125+
)
126+
127+
platform = os.getenv('PLATFORM_NAME')
128+
llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX')
129+
architectures = os.getenv('ARCHS').split(' ')
130+
131+
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
132+
script_config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
133+
134+
run(script_config)

0 commit comments

Comments
 (0)