Skip to content

Commit e6d089b

Browse files
committed
Create script to mutate Package.swift checksum.
1 parent 98b7ace commit e6d089b

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

.github/workflows/swift.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ jobs:
2929
LDK_SWIFT_GENERATOR_OUTPUT_DIRECTORY_PATH: ci/LDKSwift/Sources/LDKSwift/bindings
3030
- name: Copy new headers into bindings
3131
run: |
32-
cd ci
33-
mkdir -p ./LDKSwift/Sources/LDKHeaders/include
34-
cp ldk-c-bindings/lightning-c-bindings/include/*.h ./LDKSwift/Sources/LDKHeaders/include/
35-
cp ldk-c-bindings/ldk-net/ldk_net.h ./LDKSwift/Sources/LDKHeaders/include/
36-
cp ldk-c-bindings/ldk-net/ldk_net.c ./LDKSwift/Sources/LDKHeaders/
32+
python3 ./src/scripts/copy_c_files.py
33+
env:
34+
LDK_C_BINDINGS_BASE: /home/runner/work/ldk-swift/ldk-swift/ci/ldk-c-bindings
35+
H_FILE_OUTPUT_DIRECTORY: /home/runner/work/ldk-swift/ldk-swift/ci/LDKSwift/Sources/LDKHeaders/include
36+
C_FILE_OUTPUT_DIRECTORY: /home/runner/work/ldk-swift/ldk-swift/ci/LDKSwift/Sources/LDKHeaders
3737
- name: Check that the latest auto-generated Swift files are in the repo
3838
run: |
3939
python3 ./ # Generate bindings into local directory

src/scripts/copy_c_files.py

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

13-
header_destination_directory = config.C_FILE_OUTPUT_DIRECTORY
13+
c_file_destination_directory = config.C_FILE_OUTPUT_DIRECTORY
14+
header_destination_directory = config.H_FILE_OUTPUT_DIRECTORY
15+
os.makedirs(c_file_destination_directory, exist_ok=True)
1416
os.makedirs(header_destination_directory, exist_ok=True)
1517

18+
if c_file_destination_directory != header_destination_directory:
19+
print('Copying C files to', c_file_destination_directory)
1620
print('Copying headers to', header_destination_directory)
1721

1822
for current_directory in header_directories:
1923
for current_file in os.listdir(current_directory):
2024

21-
is_relevant_file = current_file.endswith('.h') or current_file.endswith('.c')
25+
is_header_file = current_file.endswith('.h')
26+
is_c_file = current_file.endswith('.c')
27+
28+
is_relevant_file = is_header_file or is_c_file
2229
if not is_relevant_file:
2330
continue
2431

2532
current_path = os.path.join(current_directory, current_file)
2633
if not os.path.isfile(current_path):
2734
continue
2835

36+
current_destination_directory = header_destination_directory
37+
if is_c_file:
38+
current_destination_directory = c_file_destination_directory
39+
2940
print('Copying', current_path)
30-
subprocess.check_call(['cp', current_path, header_destination_directory])
41+
subprocess.check_call(['cp', current_path, current_destination_directory])
3142

3243

3344
if __name__ == '__main__':

src/scripts/script_config.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self):
3333
self.RUST_CONFIGURATION_FLAG: str = ''
3434
self.LIPO_BINARY_OUTPUT_DIRECTORY: str = ''
3535
self.C_FILE_OUTPUT_DIRECTORY: str = ''
36+
self.H_FILE_OUTPUT_DIRECTORY: str = ''
3637
self.PRESERVE_XCARCHIVES: bool = False
3738

3839
@classmethod
@@ -108,10 +109,19 @@ def parse(cls, allow_ldk_argument=True, parse_configuration=False, parse_lipo_ou
108109
# parse build config aspect
109110
platform = os.getenv('PLATFORM_NAME')
110111
llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX')
111-
architectures = os.getenv('ARCHS').split(' ')
112-
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
113-
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
114-
115-
pass
112+
architecture_input_string = os.getenv('ARCHS')
113+
if platform and architecture_input_string:
114+
architectures = architecture_input_string.split(' ')
115+
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
116+
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
117+
118+
output_directory_override = os.getenv('C_FILE_OUTPUT_DIRECTORY')
119+
if output_directory_override:
120+
config.C_FILE_OUTPUT_DIRECTORY = output_directory_override
121+
122+
config.H_FILE_OUTPUT_DIRECTORY = config.C_FILE_OUTPUT_DIRECTORY
123+
header_output_directory_override = os.getenv('H_FILE_OUTPUT_DIRECTORY')
124+
if header_output_directory_override:
125+
config.H_FILE_OUTPUT_DIRECTORY = header_output_directory_override
116126

117127
return config
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
import os
3+
import re
4+
import sys
5+
6+
7+
def run(new_checksum: str):
8+
print("setting checksum (JSON-serialization):")
9+
print(json.dumps(new_checksum))
10+
11+
if not new_checksum.isalnum():
12+
print('Checksum must be alphanumeric.', file=sys.stderr)
13+
sys.exit(1)
14+
15+
if not new_checksum.islower():
16+
print('Checksum must be lowercase.', file=sys.stderr)
17+
sys.exit(1)
18+
19+
try:
20+
int(new_checksum, 16)
21+
except:
22+
print('Checksum must be hexadecimal.', file=sys.stderr)
23+
sys.exit(1)
24+
25+
26+
package_file_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../Package.swift'))
27+
print(package_file_path)
28+
regex = re.compile("(let[\s]+checksum[\s]*=[\s]*)(.*)")
29+
30+
with open(package_file_path, 'r') as package_file_handle:
31+
package_file = package_file_handle.read()
32+
previous_checksum = regex.search(package_file).group(2)
33+
# new_package_file = regex.sub(f'\g<1>"{new_checksum}"', package_file)
34+
new_package_file = package_file.replace(previous_checksum, f'"{new_checksum}"')
35+
with open(package_file_path, "w") as f:
36+
f.write(new_package_file)
37+
38+
39+
40+
if __name__ == '__main__':
41+
new_checksum = sys.argv[1]
42+
run(new_checksum)

0 commit comments

Comments
 (0)