-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
157 lines (129 loc) · 6.72 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# This makefile is used to build the Nand7400 framework for iOS, macOS and Mac Catalyst. To use it, run `make package` in
# the root of the repository.
# Rust-specific configuration
PACKAGE_NAME=nand7400-ffi
LIB_NAME=libnand7400_ffi.a
CARGO_FLAGS= --package ${PACKAGE_NAME} --lib --release
# General binding configuration
UNIFFI_CMD=cargo run -p nand7400-ffi --features=cli --bin uniffi --
UNIFFI_UDL_FILE=src/ffi.udl
# General build configuration
BUILD_FOLDER=target
ARTIFACTS_FOLDER=target/uniffi-artifacts
BINDINGS_FOLDER=nand7400-ffi-bindings
# Testing/fuzzing stuff
FUZZ_TARGET=nand7400-fuzz
# Additional flags for AFL besides the input, output and target binary
AFL_FLAGS=-d -D
# Swift-specific stuff
FRAMEWORK_NAME=Nand7400FFI
XCFRAMEWORK_FOLDER=target/${FRAMEWORK_NAME}.xcframework
all: package
# Install all the necessary build targets to build for Mac, iOS and Mac Catalyst.
setup-build:
@echo "▸ Installing toolchains..."
# iOS Simulator (Intel)
@rustup target add x86_64-apple-ios
# iOS Simulator (M1)
@rustup target add aarch64-apple-ios-sim
# iOS Device
@rustup target add aarch64-apple-ios
# macOS ARM/M1
@rustup target add aarch64-apple-darwin
# macOS Intel/x86
@rustup target add x86_64-apple-darwin
clean:
@echo ▸ Cleaning build...
@rm -rf ${ARTIFACTS_FOLDER}
@rm -rf ${XCFRAMEWORK_FOLDER}
@rm -rf ${BINDINGS_FOLDER}
@mkdir -p ${ARTIFACTS_FOLDER}
@mkdir -p ${BINDINGS_FOLDER}
bind: setup-build clean
@echo "▸ Building bindings..."
cargo build ${CARGO_FLAGS}
@echo "▸ Generating Swift scaffolding code..."
# New command necessary to generate scaffolding code, otherwise the bindings won't compile when using in Xcode.
${UNIFFI_CMD} scaffolding ${PACKAGE_NAME}/${UNIFFI_UDL_FILE} --no-format
${UNIFFI_CMD} generate ${PACKAGE_NAME}/${UNIFFI_UDL_FILE} --language swift --out-dir ${BINDINGS_FOLDER}/swift
# cargo build --package ${PACKAGE_NAME} --lib --locked --release
# ${UNIFFI_CMD} generate --library ${BUILD_FOLDER}/release/${LIB_NAME} --language swift --out-dir ${BINDINGS_FOLDER}/swift
build-swift: bind
@echo "▸ Building for x86_64-apple-ios..."
@CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios" \
cargo build --target x86_64-apple-ios ${CARGO_FLAGS}
@echo "▸ Building for aarch64-apple-ios-sim..."
@CFLAGS_aarch64_apple_ios="-target aarch64-apple-ios-sim" \
cargo build --target aarch64-apple-ios-sim ${CARGO_FLAGS}
@echo "▸ Building for aarch64-apple-ios..."
@CFLAGS_aarch64_apple_ios="-target aarch64-apple-ios" \
cargo build --target aarch64-apple-ios ${CARGO_FLAGS}
@echo "▸ Building for aarch64-apple-darwin..."
@CFLAGS_aarch64_apple_darwin="-target aarch64-apple-darwin" \
cargo build --target aarch64-apple-darwin ${CARGO_FLAGS}
@echo "▸ Building for x86_64-apple-darwin..."
@CFLAGS_x86_64_apple_darwin="-target x86_64-apple-darwin" \
cargo build --target x86_64-apple-darwin ${CARGO_FLAGS}
@echo "▸ Building for x86_64-apple-ios-macabi..."
@CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios-macabi" \
cargo +nightly build -Z build-std --target x86_64-apple-ios-macabi ${CARGO_FLAGS}
@echo "▸ Building for aarch64-apple-ios-macabi..."
@CFLAGS_aarch64_apple_ios="-target aarch64-apple-ios-macabi" \
cargo +nightly build -Z build-std --target aarch64-apple-ios-macabi ${CARGO_FLAGS}
@echo "▸ Consolidating the headers and modulemaps for XCFramework generation..."
@mkdir -p ${ARTIFACTS_FOLDER}/includes
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.h ${ARTIFACTS_FOLDER}/includes
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${ARTIFACTS_FOLDER}/includes/module.modulemap
@echo "▸ Merging x86 and arm iOS simulator static libraries into a fat static binary..."
@mkdir -p ${ARTIFACTS_FOLDER}/ios-simulator/release
@lipo -create \
./${BUILD_FOLDER}/x86_64-apple-ios/release/${LIB_NAME} \
./${BUILD_FOLDER}/aarch64-apple-ios-sim/release/${LIB_NAME} \
-output ${ARTIFACTS_FOLDER}/ios-simulator/release/${LIB_NAME}
@echo "▸ Merging x86 and arm macOS static libraries into a fat static binary..."
@mkdir -p ${ARTIFACTS_FOLDER}/apple-darwin/release
@lipo -create \
./${BUILD_FOLDER}/x86_64-apple-darwin/release/${LIB_NAME} \
./${BUILD_FOLDER}/aarch64-apple-darwin/release/${LIB_NAME} \
-output ${ARTIFACTS_FOLDER}/apple-darwin/release/${LIB_NAME}
@echo "▸ Merging x86 and arm macOS Catalyst static libraries into a fat static binary..."
@mkdir -p ${ARTIFACTS_FOLDER}/mac-catalyst/release
@lipo -create \
./${BUILD_FOLDER}/x86_64-apple-ios-macabi/release/${LIB_NAME} \
./${BUILD_FOLDER}/aarch64-apple-ios-macabi/release/${LIB_NAME} \
-output ${ARTIFACTS_FOLDER}/mac-catalyst/release/${LIB_NAME}
package-swift: build-swift
@echo "▸ Creating XCFramework..."
# what docs there are:
# xcodebuild -create-xcframework -help
# https://developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-bundle
@BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
xcodebuild -create-xcframework \
-library ./${BUILD_FOLDER}/aarch64-apple-ios/release/${LIB_NAME} \
-headers ./${ARTIFACTS_FOLDER}/includes \
-library ./${ARTIFACTS_FOLDER}/ios-simulator/release/${LIB_NAME} \
-headers ./${ARTIFACTS_FOLDER}/includes \
-library ./${ARTIFACTS_FOLDER}/apple-darwin/release/${LIB_NAME} \
-headers ./${ARTIFACTS_FOLDER}/includes \
-library ./${ARTIFACTS_FOLDER}/mac-catalyst/release/${LIB_NAME} \
-headers ./${ARTIFACTS_FOLDER}/includes \
-output ./${XCFRAMEWORK_FOLDER}
# Move modulemaps to the right place, so that the XCFramework can be used directly in Swift Package Manager
@mkdir -p ${XCFRAMEWORK_FOLDER}/ios-arm64/Modules
@mkdir -p ${XCFRAMEWORK_FOLDER}/ios-x86_64-simulator/Modules
@mkdir -p ${XCFRAMEWORK_FOLDER}/ios-arm64_x86_64-simulator/Modules
@mkdir -p ${XCFRAMEWORK_FOLDER}/ios-arm64_x86_64-maccatalyst/Modules
@mkdir -p ${XCFRAMEWORK_FOLDER}/macos-arm64_x86_64/Modules
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${XCFRAMEWORK_FOLDER}/ios-arm64/Modules/module.modulemap
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${XCFRAMEWORK_FOLDER}/ios-x86_64-simulator/Modules/module.modulemap
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${XCFRAMEWORK_FOLDER}/ios-arm64_x86_64-simulator/Modules/module.modulemap
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${XCFRAMEWORK_FOLDER}/ios-arm64_x86_64-maccatalyst/Modules/module.modulemap
@cp ${BINDINGS_FOLDER}/swift/${FRAMEWORK_NAME}.modulemap ${XCFRAMEWORK_FOLDER}/macos-arm64_x86_64/Modules/module.modulemap
@echo "▸ Compressing XCFramework..."
@ditto -c -k --sequesterRsrc --keepParent ${XCFRAMEWORK_FOLDER} ${XCFRAMEWORK_FOLDER}.zip
@echo "▸ Computing checksum..."
@swift package compute-checksum ${XCFRAMEWORK_FOLDER}.zip > ${XCFRAMEWORK_FOLDER}.zip.sha256
@echo "▸ Finished Swift bindings!"
# Convenience target to build and package everything
package: package-swift
@echo "▸ Done!"