-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDjinni.cmake
239 lines (210 loc) · 10.1 KB
/
Djinni.cmake
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# MIT License
#
# Djinni CMake
# https://github.com/jothepro/djinni-cmake
#
# Copyright (c) 2021 - 2022 jothepro
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
cmake_minimum_required(VERSION 3.18)
function(add_djinni_library LIBRARY_TARGET)
cmake_parse_arguments(DJINNI
# options
"NO_JNI_MAIN;STATIC;SHARED;NO_OBJC_PREFIX;NO_DSYM"
# one-value keywords
"IDL;NAMESPACE;DIRECTORY;JAR_OUTPUT_DIR"
# multi-value keywords
"LANGUAGES;SOURCES;"
# args
${ARGN}
)
set(MESSAGE_PREFIX "[djinni]")
if(NOT DEFINED DJINNI_DIRECTORY)
set(DJINNI_DIRECTORY djinni-generated)
endif()
if(NOT DEFINED DJINNI_NAMESPACE)
set(DJINNI_NAMESPACE Djinni)
endif()
if(NOT DEFINED DJINNI_JAR_OUTPUT_DIR)
set(DJINNI_JAR_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
# derive namespaces, prefixes, paths from NAMESPACE
string(REPLACE "::" "/" DJINNI_NAMESPACE_PATH ${DJINNI_NAMESPACE})
set(DJINNI_JNI_NAMESPACE "${DJINNI_NAMESPACE}::jni")
set(DJINNI_OBJCPP_NAMESPACE "objcpp::${DJINNI_NAMESPACE}")
set(DJINNI_CPPCLI_NAMESPACE "${DJINNI_NAMESPACE}::cppcli")
string(REPLACE "::" "." DJINNI_JAVA_PACKAGE ${DJINNI_NAMESPACE})
string(TOLOWER ${DJINNI_JAVA_PACKAGE} DJINNI_JAVA_PACKAGE)
string(TOLOWER ${DJINNI_JAVA_PACKAGE} DJINNI_JAVA_PATH)
string(REPLACE "." "/" DJINNI_JAVA_PATH ${DJINNI_JAVA_PATH})
string(REGEX REPLACE "[a-z:\\-_]" "" DJINNI_OBJC_PREFIX ${DJINNI_NAMESPACE})
# prepare input variables
set(DJINNI_CPP_DIR ${DJINNI_DIRECTORY}/cpp)
set(DJINNI_CPP_SRC_DIR ${DJINNI_CPP_DIR}/src)
set(DJINNI_CPP_INCLUDE_PREFIX ${DJINNI_NAMESPACE_PATH}/)
set(DJINNI_CPP_INCLUDE_DIR ${DJINNI_CPP_DIR}/include/)
set(DJINNI_CPP_HEADER_OUT ${DJINNI_CPP_INCLUDE_DIR}/${DJINNI_CPP_INCLUDE_PREFIX})
set(DJINNI_JNI_OUT ${DJINNI_DIRECTORY}/jni/src)
set(DJINNI_JNI_INCLUDE_PREFIX ${DJINNI_NAMESPACE_PATH}/jni/)
set(DJINNI_JNI_INCLUDE_DIR ${DJINNI_DIRECTORY}/jni/include/)
set(DJINNI_JNI_HEADER_OUT ${DJINNI_JNI_INCLUDE_DIR}/${DJINNI_JNI_INCLUDE_PREFIX})
set(DJINNI_JNI_INCLUDE_CPP_PREFIX ${DJINNI_NAMESPACE_PATH}/)
set(DJINNI_OBJC_OUT ${DJINNI_DIRECTORY}/objc/src)
set(DJINNI_OBJC_INCLUDE_PREFIX ${LIBRARY_TARGET}/)
set(DJINNI_OBJCPP_INCLUDE_CPP_PREFIX ${DJINNI_NAMESPACE_PATH}/)
set(DJINNI_OBJC_INCLUDE_DIR ${DJINNI_DIRECTORY}/objc/include/)
set(DJINNI_OBJC_HEADER_OUT ${DJINNI_OBJC_INCLUDE_DIR}${LIBRARY_TARGET}/)
set(DJINNI_OBJCPP_INCLUDE_DIR ${DJINNI_DIRECTORY}/objcpp/include/)
set(DJINNI_OBJCPP_OUT ${DJINNI_DIRECTORY}/objcpp/src/)
set(DJINNI_OBJCPP_HEADER_OUT ${DJINNI_OBJCPP_INCLUDE_DIR}${LIBRARY_TARGET}/)
set(DJINNI_OBJCPP_INCLUDE_PREFIX ${LIBRARY_TARGET}/)
set(DJINNI_CPPCLI_OUT ${DJINNI_DIRECTORY}/cppcli/src/)
set(DJINNI_JAVA_OUT ${DJINNI_DIRECTORY}/java/${DJINNI_JAVA_PATH})
set(DJINNI_OBJC_SWIFT_BRIDGING_HEADER ${LIBRARY_TARGET})
set(DJINNI_JAVA_LIBRARY_TARGET ${LIBRARY_TARGET}-java)
# trigger re-generation if IDL file changes
set_directory_properties(PROPERTIES CMAKE_CONFIGURE_DEPENDS ${DJINNI_IDL})
# find Djinni executable.
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
find_program(DJINNI_EXECUTABLE djinni.bat REQUIRED)
else()
find_program(DJINNI_EXECUTABLE djinni REQUIRED)
endif()
# output djinni version
execute_process(COMMAND ${DJINNI_EXECUTABLE} --version
OUTPUT_VARIABLE DJINNI_VERSION_OUTPUT)
message(STATUS "${MESSAGE_PREFIX} ${DJINNI_VERSION_OUTPUT}")
# generate Java sources and add prepare parameters for JNI generation.
if("JAVA" IN_LIST DJINNI_LANGUAGES)
if(DJINNI_NO_JNI_MAIN)
set(DJINNI_GENERATE_MAIN false)
else()
set(DJINNI_GENERATE_MAIN true)
endif()
set(ADDITIONAL_DJINNI_PARAMETERS
--java-out ${DJINNI_JAVA_OUT}
--jni-out ${DJINNI_JNI_OUT}
--jni-header-out ${DJINNI_JNI_HEADER_OUT}
--jni-generate-main ${DJINNI_GENERATE_MAIN})
endif()
if("OBJC" IN_LIST DJINNI_LANGUAGES)
set(ADDITIONAL_DJINNI_PARAMETERS
--objc-out ${DJINNI_OBJC_OUT}
--objc-header-out ${DJINNI_OBJC_HEADER_OUT}
--objcpp-out ${DJINNI_OBJCPP_OUT}
--objcpp-header-out ${DJINNI_OBJCPP_HEADER_OUT}
--objc-swift-bridging-header ${DJINNI_OBJC_SWIFT_BRIDGING_HEADER})
endif()
if("CPPCLI" IN_LIST DJINNI_LANGUAGES)
set(ADDITIONAL_DJINNI_PARAMETERS
--cppcli-out ${DJINNI_CPPCLI_OUT}
)
endif()
if("CPP" IN_LIST DJINNI_LANGUAGES)
set(ADDITIONAL_DJINNI_PARAMETERS
--cpp-out ${DJINNI_CPP_SRC_DIR}
--cpp-header-out ${DJINNI_CPP_HEADER_OUT}
)
endif()
set(DJINNI_GENERATED_FILES_OUTFILE ${CMAKE_CURRENT_BINARY_DIR}/djinni-generated-files.txt)
message(STATUS "${MESSAGE_PREFIX} Generating Gluecode for ${DJINNI_LANGUAGES}")
if(NOT DJINNI_NO_OBJC_PREFIX)
list(APPEND ADDITIONAL_DJINNI_PARAMETERS --objc-type-prefix ${DJINNI_OBJC_PREFIX})
endif()
# generate c++ interface
execute_process(COMMAND ${DJINNI_EXECUTABLE}
--idl ${DJINNI_IDL}
${DJINNI_IDL_INCLUDE_PARAMETERS}
--cppcli-namespace ${DJINNI_CPPCLI_NAMESPACE}
--cppcli-include-cpp-prefix ${DJINNI_CPP_INCLUDE_PREFIX}
--cpp-namespace ${DJINNI_NAMESPACE}
--java-package ${DJINNI_JAVA_PACKAGE}
--jni-namespace ${DJINNI_JNI_NAMESPACE}
--jni-include-prefix ${DJINNI_JNI_INCLUDE_PREFIX}
--jni-include-cpp-prefix ${DJINNI_JNI_INCLUDE_CPP_PREFIX}
--cpp-include-prefix ${DJINNI_CPP_INCLUDE_PREFIX}
--objc-include-prefix ${DJINNI_OBJC_INCLUDE_PREFIX}
--objcpp-include-cpp-prefix ${DJINNI_OBJCPP_INCLUDE_CPP_PREFIX}
--objcpp-include-objc-prefix ${DJINNI_OBJC_INCLUDE_PREFIX}
--objcpp-include-prefix ${DJINNI_OBJCPP_INCLUDE_PREFIX}
--objcpp-namespace ${DJINNI_OBJCPP_NAMESPACE}
--list-out-files ${DJINNI_GENERATED_FILES_OUTFILE}
${ADDITIONAL_DJINNI_PARAMETERS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
file(STRINGS ${DJINNI_GENERATED_FILES_OUTFILE} DJINNI_GENERATED_FILES
ENCODING UTF-8)
# filter out java files from generated files
set(DJINNI_GENERATED_JAVA_FILES_REGEX "${DJINNI_JAVA_OUT}.*")
set(DJINNI_GENERATED_JAVA_FILES ${DJINNI_GENERATED_FILES})
list(FILTER DJINNI_GENERATED_JAVA_FILES INCLUDE REGEX ${DJINNI_GENERATED_JAVA_FILES_REGEX})
set(DJINNI_GENERATED_CPP_FILES ${DJINNI_GENERATED_FILES})
list(FILTER DJINNI_GENERATED_CPP_FILES EXCLUDE REGEX ${DJINNI_GENERATED_JAVA_FILES_REGEX})
if(DJINNI_STATIC)
set(DJINNI_LIBRARY_TYPE STATIC)
elseif(DJINNI_SHARED)
set(DJINNI_LIBRARY_TYPE SHARED)
endif()
add_library(${LIBRARY_TARGET} ${DJINNI_LIBRARY_TYPE} ${DJINNI_GENERATED_CPP_FILES} ${DJINNI_SOURCES} ${DJINNI_GENERATED_OTHER_FILES})
target_include_directories(${LIBRARY_TARGET} PUBLIC ${DJINNI_CPP_INCLUDE_DIR})
if("JAVA" IN_LIST DJINNI_LANGUAGES)
find_package(Java 1.8 REQUIRED)
include(UseJava)
add_jar(${DJINNI_JAVA_LIBRARY_TARGET}
SOURCES ${DJINNI_GENERATED_JAVA_FILES}
OUTPUT_DIR ${DJINNI_JAR_OUTPUT_DIR}
OUTPUT_NAME ${LIBRARY_TARGET})
add_dependencies(${LIBRARY_TARGET} ${DJINNI_JAVA_LIBRARY_TARGET})
target_include_directories(${LIBRARY_TARGET} PUBLIC ${DJINNI_JNI_INCLUDE_DIR})
endif()
if("OBJC" IN_LIST DJINNI_LANGUAGES)
target_include_directories(${LIBRARY_TARGET} PUBLIC ${DJINNI_OBJC_INCLUDE_DIR} ${DJINNI_OBJCPP_INCLUDE_DIR})
set(DJINNI_OBJC_SWIFT_BRIDGING_HEADER_PATH "${DJINNI_OBJC_HEADER_OUT}${DJINNI_OBJC_SWIFT_BRIDGING_HEADER}.h")
target_sources(${LIBRARY_TARGET} PUBLIC ${DJINNI_OBJC_SWIFT_BRIDGING_HEADER_PATH})
# determine framework public headers
set(DJINNI_GENERATED_PUBLIC_HEADER_FILES ${DJINNI_GENERATED_CPP_FILES})
list(APPEND DJINNI_GENERATED_PUBLIC_HEADER_FILES ${DJINNI_OBJC_SWIFT_BRIDGING_HEADER_PATH})
list(FILTER DJINNI_GENERATED_PUBLIC_HEADER_FILES INCLUDE REGEX "^${DJINNI_OBJC_HEADER_OUT}.*$")
set_target_properties(${LIBRARY_TARGET} PROPERTIES
FRAMEWORK TRUE
MACOSX_FRAMEWORK_IDENTIFIER ${DJINNI_JAVA_PACKAGE}
PUBLIC_HEADER "${DJINNI_GENERATED_PUBLIC_HEADER_FILES}"
XCODE_ATTRIBUTE_DEFINES_MODULE YES
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
)
if(NOT DJINNI_NO_DSYM)
set_target_properties(${LIBRARY_TARGET} PROPERTIES
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT dwarf-with-dsym
)
endif()
endif()
if("CPP" IN_LIST DJINNI_LANGUAGES)
install(
DIRECTORY
${DJINNI_CPP_INCLUDE_DIR}
DESTINATION include
)
install(TARGETS ${LIBRARY_TARGET} EXPORT ${LIBRARY_TARGET}Targets
LIBRARY DESTINATION lib
FRAMEWORK DESTINATION lib
INCLUDES DESTINATION include
PUBLIC_HEADER DESTINATION include
)
endif()
endfunction()