-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
201 lines (169 loc) · 5.04 KB
/
CMakeLists.txt
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
cmake_minimum_required(VERSION 3.15)
set(TARGET_PLATFORM "macos" CACHE STRING "Target platform for the Objective-C bridge")
set(METADATA_SIZE 0 CACHE STRING "Size of embedded metadata in bytes")
project(NativeScript)
set(NAME NativeScript)
set(VERSION 0.1.0)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fobjective-c")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-shorten-64-to-32")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_STANDARD 20)
set(BUILD_FRAMEWORK TRUE)
find_program(XCODEBUILD_EXECUTABLE xcodebuild)
if(TARGET_PLATFORM STREQUAL "ios")
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0")
set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0")
set(LIBFFI_BUILD "iphoneos-arm64")
set(TARGET_PLATFORM_IOS TRUE)
set(SDK_NAME "iphoneos")
set(CMAKE_OSX_ARCHITECTURES "arm64")
elseif(TARGET_PLATFORM STREQUAL "ios-sim")
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0")
set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0")
set(LIBFFI_BUILD "iphonesimulator-universal")
set(TARGET_PLATFORM_IOS TRUE)
set(TARGET_PLATFORM_SIM TRUE)
set(SDK_NAME "iphonesimulator")
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
elseif(TARGET_PLATFORM STREQUAL "macos")
set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "13.0")
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0")
set(LIBFFI_BUILD "macosx-universal")
set(TARGET_PLATFORM_MACOS TRUE)
unset(BUILD_FRAMEWORK)
set(SDK_NAME "macosx")
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
else()
message(FATAL_ERROR "Unknown target platform: ${TARGET_PLATFORM}")
return()
endif()
if(METADATA_SIZE EQUAL 0)
unset(METADATA_SIZE)
else()
message(STATUS "EMBED_METADATA_SIZE = ${METADATA_SIZE}"
" (metadata will be embedded in the library)")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEMBED_METADATA_SIZE=${METADATA_SIZE}")
endif()
message(STATUS "TARGET_PLATFORM = ${TARGET_PLATFORM}")
execute_process(
COMMAND ${XCODEBUILD_EXECUTABLE} -version -sdk ${SDK_NAME} Path
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT_INT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(CMAKE_OSX_SYSROOT_INT "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "")
set(CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "")
message(STATUS "SDK = ${CMAKE_OSX_SYSROOT}")
# add_subdirectory("segappend")
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/metadata/include
${CMAKE_CURRENT_SOURCE_DIR}/libffi/prebuilt/${LIBFFI_BUILD}/include
/Library/Developer/CommandLineTools/usr/include
)
set(LIB_SOURCE_FILES
src/AutoreleasePool.mm
src/Protocol.mm
src/ObjCBridge.mm
src/Block.mm
src/Class.mm
src/Closure.mm
src/ClassMember.mm
src/Cif.mm
src/TypeConv.mm
src/Util.mm
src/Struct.mm
src/ObjectRef.mm
src/JSObject.mm
src/Enum.mm
src/Variable.mm
src/Object.mm
src/CFunction.mm
src/Interop.mm
src/InlineFunctions.mm
src/ClassBuilder.mm
)
add_library(
${NAME}
SHARED
${LIB_SOURCE_FILES}
)
target_sources(
${NAME}
PRIVATE
"include/NativeScript.h"
)
set_target_properties(${NAME} PROPERTIES
VERSION ${VERSION}
SOVERSION ${VERSION}
FRAMEWORK_VERSION ${VERSION}
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${VERSION}
MACOSX_FRAMEWORK_BUNDLE_VERSION ${VERSION}
MACOSX_FRAMEWORK_IDENTIFIER org.nativescript.nativescript-${TARGET_PLATFORM}
PUBLIC_HEADER "include/NativeScript.h"
)
if (BUILD_FRAMEWORK)
message(STATUS "FRAMEWORK = TRUE")
set_target_properties(${NAME} PROPERTIES
FRAMEWORK TRUE
)
endif()
if(TARGET_PLATFORM_MACOS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTARGET_PLATFORM_MACOS")
add_custom_command(TARGET ${NAME} POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :LSMinimumSystemVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:${NAME}>/Resources/Info.plist
)
set(METADATA_FILE "metadata.macos.nsmd")
elseif(TARGET_PLATFORM_IOS)
set(METADATA_FILE "metadata.ios.nsmd")
endif()
if(NOT METADATA_SIZE EQUAL 0)
message(STATUS "Added script to embed metadata in the library")
add_custom_command(TARGET ${NAME} POST_BUILD
COMMAND codesign --remove-signature $<TARGET_FILE:${NAME}>
)
add_custom_command(TARGET ${NAME} POST_BUILD
COMMAND deno run -A ${CMAKE_CURRENT_SOURCE_DIR}/scripts/write_metadata.ts $<TARGET_FILE:${NAME}> ${METADATA_FILE}
)
add_custom_command(TARGET ${NAME} POST_BUILD
COMMAND codesign -fs - $<TARGET_FILE:${NAME}>
)
endif()
message(STATUS "LIBFFI_BUILD = ${LIBFFI_BUILD}")
target_link_directories(
${NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/libffi/prebuilt/${LIBFFI_BUILD}
)
target_link_libraries(
${NAME}
PRIVATE
objc
ffi
"-framework Foundation"
)
if(TARGET_PLATFORM_MACOS)
target_link_libraries(
${NAME}
PRIVATE
"-framework AppKit"
)
elseif(TARGET_PLATFORM_IOS)
target_link_libraries(
${NAME}
PRIVATE
"-framework UIKit"
)
endif()
target_link_options(
${NAME}
PRIVATE
"-Wl"
"-undefined"
"dynamic_lookup"
)