Skip to content

Commit 359c70d

Browse files
committed
fix LDKWitnessVersion instantiation for arguments
1 parent c38a732 commit 359c70d

File tree

7 files changed

+136
-14
lines changed

7 files changed

+136
-14
lines changed

ci/LDKSwift/Package.swift

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@
44
import PackageDescription
55
import Foundation
66

7+
var cBindingsBase = "/Users/arik/Developer/ldk-c-bindings"
8+
if let bindingsBase = getenv("LDK_C_BINDINGS_BASE") {
9+
cBindingsBase = String(utf8String: bindingsBase)!
10+
}
11+
12+
let linkerSettings: [PackageDescription.LinkerSetting] = [
13+
.linkedLibrary(cBindingsBase + "/lightning-c-bindings/target/debug/libldk.a")
14+
]
15+
16+
#if os(Linux)
17+
linkerSettings.append(.linkedLibrary(String(utf8String: getenv("LLVM_CLANG_ASAN_PATH")!)!))
18+
#endif
19+
720
let package = Package(
821
name: "LDKSwift",
922
products: [
1023
.library(
1124
name: "LDKSwift",
12-
targets: ["LDKSwift"]),
25+
targets: ["LDKBindings", "LDKBatteries"]),
1326
// .library(name: "LDKSwift", type: .dynamic, targets: ["LDKSwift"])
1427
],
1528
dependencies: [
@@ -21,7 +34,7 @@ let package = Package(
2134
.target(name: "LDKHeaders"),
2235
// .systemLibrary(name: "LDKHeaders"),
2336
.target(
24-
name: "LDKSwift",
37+
name: "LDKBindings",
2538
dependencies: [
2639
"LDKHeaders"
2740
],
@@ -44,14 +57,40 @@ let package = Package(
4457
// ],
4558
cxxSettings: nil,
4659
swiftSettings: nil,
47-
linkerSettings: [
48-
.linkedLibrary(String(utf8String: getenv("LDK_C_BINDINGS_BASE")!)! + "/lightning-c-bindings/target/debug/libldk.a"),
49-
.linkedLibrary(String(utf8String: getenv("LLVM_CLANG_ASAN_PATH")!)!),
50-
]),
60+
linkerSettings: linkerSettings
61+
),
62+
.target(
63+
name: "LDKBatteries",
64+
dependencies: [
65+
"LDKHeaders",
66+
"LDKBindings"
67+
],
68+
path: nil,
69+
exclude: [],
70+
sources: nil,
71+
// sources: [
72+
// "ldk_net.h",
73+
// "ldk_net.c",
74+
// "ldk_rust_types.h",
75+
// "ldk_ver.h",
76+
// "lightning.h",
77+
// "SanitySample.swift"
78+
// ],
79+
resources: nil,
80+
publicHeadersPath: "include",
81+
cSettings: nil,
82+
// cSettings: [
83+
// .headerSearchPath("include"),
84+
// ],
85+
cxxSettings: nil,
86+
swiftSettings: nil,
87+
linkerSettings: linkerSettings
88+
),
5189
.testTarget(
5290
name: "LDKSwiftTests",
5391
dependencies: [
54-
"LDKSwift",
92+
"LDKBindings",
93+
"LDKBatteries",
5594
"LDKHeaders",
5695
.product(name: "Crypto", package: "swift-crypto")
5796
],

src/conversion_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self, key: str, size: int = -1, length_key = None):
4141
}
4242

4343
unary_tuples: [str] = ["LDKu5", "LDKWitnessVersion"]
44+
unary_tuple_type_details: Dict[str, object] = {}
4445

4546
class ConversionHelper:
4647
trait_structs = set()

src/lightning_header_parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ def populate_type_details(self):
397397

398398
self.byte_arrays.add(struct_name)
399399
self.type_details[struct_name].fields.append(byte_array_info)
400+
if self.type_details[struct_name].is_unary_tuple:
401+
src.conversion_helper.unary_tuple_type_details[struct_name] = self.type_details[struct_name]
402+
400403
else:
401404
# there is no block-scoped object currently being parsed
402405
fn_ptr = fn_ptr_regex.match(current_line)

src/sdk_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def generate_sdk():
125125

126126
def cleanup_bindings():
127127
directory_path = os.path.dirname(os.path.realpath(__file__))
128-
header_directory = os.path.realpath(f'{directory_path}/../bindings/LDK')
128+
header_directory = os.path.realpath(f'{directory_path}/../ci/LDKSwift/Sources/LDKBindings')
129129
print("Cleaning up bindings/LDK directory:", header_directory)
130130
for current_directory, current_subdirectories, current_files in os.walk(header_directory):
131131
for current_file in current_files:

src/swift_type_mapper.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import src.conversion_helper
12
from src.binding_types import TypeInfo
23
from src.type_parsing_regeces import TypeParsingRegeces
34
from src.conversion_helper import ConversionHelper
45
from src.conversion_helper import array_accessor_types
6+
from src.conversion_helper import unary_tuples
57

68
var_is_arr_regex = TypeParsingRegeces.IS_VARIABLE_AN_ARRAY_REGEX
79
var_ty_regex = TypeParsingRegeces.VARIABLE_TYPE_REGEX
@@ -99,12 +101,18 @@ def map_types_to_swift(fn_arg, ret_arr_len, java_c_types_none_allowed, tuple_typ
99101
c_ty = "int8_t"
100102
fn_arg = fn_arg[7:].strip()
101103
is_primitive = True
102-
elif fn_arg.startswith("LDKu5"):
103-
mapped_type = language_constants.c_type_map['uint8_t']
104+
elif any(fn_arg.startswith(current_unary_tuple) for current_unary_tuple in unary_tuples):
105+
current_unary_tuple = None
106+
for current_unary_tuple in unary_tuples:
107+
if fn_arg.startswith(current_unary_tuple):
108+
break
109+
auxiliary_type_details = src.conversion_helper.unary_tuple_type_details[current_unary_tuple]
110+
primary_field = auxiliary_type_details.fields[0]
111+
c_ty = primary_field.c_ty
112+
mapped_type = language_constants.c_type_map['u'+c_ty]
104113
java_ty = mapped_type
105-
c_ty = "int8_t"
106-
fn_arg = fn_arg[6:].strip()
107-
rust_obj = 'LDKu5'
114+
fn_arg = fn_arg[len(current_unary_tuple)+1:].strip()
115+
rust_obj = current_unary_tuple
108116
is_unary_tuple = True
109117
arr_len = 1
110118
is_primitive = True

templates/BindingsTemplate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ public class Bindings {
418418
return LDKStr(chars: nativeType, len: UInt(string.count), chars_is_owned: chars_is_owned)
419419
}
420420

421+
/*
421422
public class func createInvoiceFromChannelManager(channelManager: ChannelManager, keysManager: KeysInterface, network: LDKCurrency, amountMsat: UInt64?, description: String) -> Result_InvoiceSignOrCreationErrorZ {
422423
let nativeKeysManager = keysManager.cOpaqueStruct!
423424
let amount = Option_u64Z(value: amountMsat)
@@ -429,7 +430,6 @@ public class Bindings {
429430
}
430431
}
431432

432-
/*
433433
public class func getRoute(our_node_id: [UInt8], network: NetworkGraph, payee: [UInt8], payee_features: InvoiceFeatures, first_hops: [LDKChannelDetails], last_hops: [LDKRouteHint], final_value_msat: UInt64, final_cltv: UInt32, logger: Logger) -> Result_RouteLightningErrorZ {
434434
return withUnsafePointer(to: network.cOpaqueStruct!) { (networkPointer: UnsafePointer<LDKNetworkGraph>) in
435435
var mutableHops = Bindings.new_LDKCVec_ChannelDetailsZWrapper(array: first_hops).cOpaqueStruct!
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
set -e # stop execution upon the first error
2+
3+
LDK_DIRECTORY=$1 # directory to compile the C bindings in
4+
C_BINDINGS_SOURCE_DIRECTORY="${LDK_DIRECTORY}/lightning-c-bindings"
5+
6+
DIRECT_BINDINGS_PROJECT_DIRECTORY="`pwd`/DirectBindingsApp" # directory to copy the shared library and headers into
7+
FRAMEWORK_PROJECT_DIRECTORY_IOS="`pwd`/LDKFramework" # directory to copy the shared library and headers into
8+
FRAMEWORK_PROJECT_DIRECTORY_MAC="`pwd`/LDKFramework_Mac" # directory to copy the shared library and headers into
9+
10+
usage() {
11+
echo "USAGE: path/to/ldk-c-bindings"
12+
exit 1
13+
}
14+
15+
[ "${LDK_DIRECTORY}" = "" ] && echo "Usage: ./compile_dependency_binaries.sh /path/to/ldk-c-bindings" && exit 1;
16+
[ ! -d "${LDK_DIRECTORY}" ] && echo "Provided directory does not exist" && exit 1;
17+
18+
19+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/include/"*.h $FRAMEWORK_PROJECT_DIRECTORY_IOS
20+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/include/"*.h $FRAMEWORK_PROJECT_DIRECTORY_MAC
21+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/include/"*.h $DIRECT_BINDINGS_PROJECT_DIRECTORY
22+
23+
cp "${LDK_DIRECTORY}/ldk-net/ldk_net."{c,h} $FRAMEWORK_PROJECT_DIRECTORY_IOS
24+
cp "${LDK_DIRECTORY}/ldk-net/ldk_net."{c,h} $FRAMEWORK_PROJECT_DIRECTORY_MAC
25+
cp "${LDK_DIRECTORY}/ldk-net/ldk_net."{c,h} $DIRECT_BINDINGS_PROJECT_DIRECTORY
26+
27+
python3 ../ci/fix_header_includes.py $FRAMEWORK_PROJECT_DIRECTORY_IOS
28+
python3 ../ci/fix_header_includes.py $FRAMEWORK_PROJECT_DIRECTORY_MAC
29+
python3 ../ci/fix_header_includes.py $DIRECT_BINDINGS_PROJECT_DIRECTORY
30+
31+
32+
# build for Catalyst
33+
pushd $C_BINDINGS_SOURCE_DIRECTORY
34+
#export RUSTFLAGS=""
35+
#export RUSTFLAGS="-Z sanitizer=address"
36+
#export RUSTFLAGS="--cfg=c_bindings --cfg=feature=\"std\" --cfg=feature=\"bitcoin/std\" --cfg=feature=\"lightning/std\" --cfg=feature=\"lightning-invoice/std\""
37+
export RUSTFLAGS="--cfg=c_bindings"
38+
#export RUSTFLAGS="--cfg=c_bindings --cfg=feature=\"default\" --cfg=feature=\"std\""
39+
#export RUSTFLAGS="--cfg=c_bindings --cfg=feature=\"default\""
40+
#export RUSTFLAGS="--cfg=c_bindings -C lto=off -C embed-bitcode=no"
41+
#export RUSTFLAGS="--cfg=c_bindings -C lto=off -Z embed-bitcode"
42+
43+
# sanity check
44+
#cargo rustc -- --print cfg
45+
#cargo build --target x86_64-apple-ios-macabi --release
46+
#exit
47+
48+
# Mac ABI binary (disable temporarily) TODO: reenable
49+
# it might be necessary to run "sudo xcode-select --switch /Library/Developer/CommandLineTools" here
50+
# sudo xcode-select --switch /Library/Developer/CommandLineTools
51+
rustup override set nightly
52+
cargo clean
53+
cargo build -Z build-std=panic_abort,std --features "std" --target x86_64-apple-ios-macabi --release
54+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/target/x86_64-apple-ios-macabi/release/libldk.a" $FRAMEWORK_PROJECT_DIRECTORY_MAC
55+
56+
# iOS & Simulator binaries
57+
# it might be necessary to run "sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer"
58+
# sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
59+
rustup override unset
60+
cargo clean
61+
cargo lipo --features "std" --release
62+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/target/universal/release/libldk.a" $FRAMEWORK_PROJECT_DIRECTORY_IOS
63+
cargo lipo --features "std"
64+
cp "${C_BINDINGS_SOURCE_DIRECTORY}/target/universal/debug/libldk.a" $DIRECT_BINDINGS_PROJECT_DIRECTORY
65+
66+
67+
68+
69+
70+
71+

0 commit comments

Comments
 (0)