-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdotnet.cmake
407 lines (361 loc) · 13.6 KB
/
dotnet.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Will need swig
set(CMAKE_SWIG_FLAGS)
find_package(SWIG REQUIRED)
include(UseSWIG)
#if(${SWIG_VERSION} VERSION_GREATER_EQUAL 4)
# list(APPEND CMAKE_SWIG_FLAGS "-doxygen")
#endif()
if(UNIX AND NOT APPLE)
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
endif()
# Find dotnet cli
find_program(DOTNET_EXECUTABLE NAMES dotnet)
if(NOT DOTNET_EXECUTABLE)
message(FATAL_ERROR "Check for dotnet Program: not found")
else()
message(STATUS "Found dotnet Program: ${DOTNET_EXECUTABLE}")
endif()
# Needed by dotnet/CMakeLists.txt
set(DOTNET_PACKAGE ${COMPANY_NAME}.${PROJECT_NAME})
set(DOTNET_PACKAGES_DIR "${PROJECT_BINARY_DIR}/dotnet/packages")
# Runtime IDentifier
# see: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(DOTNET_PLATFORM arm64)
else()
set(DOTNET_PLATFORM x64)
endif()
if(APPLE)
set(DOTNET_RID osx-${DOTNET_PLATFORM})
elseif(UNIX)
set(DOTNET_RID linux-${DOTNET_PLATFORM})
elseif(WIN32)
set(DOTNET_RID win-${DOTNET_PLATFORM})
else()
message(FATAL_ERROR "Unsupported system !")
endif()
message(STATUS ".Net RID: ${DOTNET_RID}")
set(DOTNET_NATIVE_PROJECT ${DOTNET_PACKAGE}.runtime.${DOTNET_RID})
message(STATUS ".Net runtime project: ${DOTNET_NATIVE_PROJECT}")
set(DOTNET_NATIVE_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_NATIVE_PROJECT})
message(STATUS ".Net runtime project build path: ${DOTNET_NATIVE_PROJECT_DIR}")
# Targeted Framework Moniker
# see: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
# see: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
if(USE_DOTNET_STD_20)
list(APPEND TFM "netstandard2.0")
endif()
if(USE_DOTNET_STD_21)
list(APPEND TFM "netstandard2.1")
endif()
if(USE_DOTNET_46)
list(APPEND TFM "net46")
endif()
if(USE_DOTNET_461)
list(APPEND TFM "net461")
endif()
if(USE_DOTNET_462)
list(APPEND TFM "net462")
endif()
if(USE_DOTNET_47)
list(APPEND TFM "net47")
endif()
if(USE_DOTNET_48)
list(APPEND TFM "net48")
endif()
if(USE_DOTNET_CORE_31)
list(APPEND TFM "netcoreapp3.1")
endif()
if(USE_DOTNET_6)
list(APPEND TFM "net6.0")
endif()
if(USE_DOTNET_7)
list(APPEND TFM "net7.0")
endif()
if(USE_DOTNET_8)
list(APPEND TFM "net8.0")
endif()
list(LENGTH TFM TFM_LENGTH)
if(TFM_LENGTH EQUAL "0")
message(FATAL_ERROR "No .Net SDK selected !")
endif()
string(JOIN ";" DOTNET_TFM ${TFM})
message(STATUS ".Net TFM: ${DOTNET_TFM}")
if(TFM_LENGTH GREATER "1")
string(CONCAT DOTNET_TFM "<TargetFrameworks>" "${DOTNET_TFM}" "</TargetFrameworks>")
else()
string(CONCAT DOTNET_TFM "<TargetFramework>" "${DOTNET_TFM}" "</TargetFramework>")
endif()
set(DOTNET_PROJECT ${DOTNET_PACKAGE})
message(STATUS ".Net project: ${DOTNET_PROJECT}")
set(DOTNET_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_PROJECT})
message(STATUS ".Net project build path: ${DOTNET_PROJECT_DIR}")
# Create the native library
string(TOLOWER "${COMPANY_NAME}_${PROJECT_NAME}_native" DOTNET_NATIVE_LIBRARY)
message(STATUS ".Net runtime library: ${DOTNET_NATIVE_LIBRARY}")
add_library(${DOTNET_NATIVE_LIBRARY} SHARED "")
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
PREFIX ""
POSITION_INDEPENDENT_CODE ON)
# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
INSTALL_RPATH "@loader_path")
# Xcode fails to build if library doesn't contains at least one source file.
if(XCODE)
file(GENERATE
OUTPUT ${PROJECT_BINARY_DIR}/${DOTNET_NATIVE_LIBRARY}/version.cpp
CONTENT "namespace {char* version = \"${PROJECT_VERSION}\";}")
target_sources(${DOTNET_NATIVE_LIBRARY} PRIVATE ${PROJECT_BINARY_DIR}/${DOTNET_NATIVE_LIBRARY}/version.cpp)
endif()
elseif(UNIX)
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
INSTALL_RPATH "$ORIGIN")
endif()
#################
## .Net Test ##
#################
# add_dotnet_tfm_test()
# CMake function to generate and build dotnet test for a specific TFM.
# warning: only net6.0, net7.0 and net8.0 are supported
# Parameters:
# the dotnet filename
# e.g.:
# add_dotnet_test(FooTests.cs net8.0)
function(add_dotnet_tfm_test FILE_NAME TEST_TFM)
message(STATUS " Configuring test ${FILE_NAME} (${TEST_TFM}) ...")
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
set(DOTNET_TEST_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${TEST_NAME})
if(TEST_TFM STREQUAL "net6.0")
set(TEST_SUFFIX net60)
elseif(TEST_TFM STREQUAL "net7.0")
set(TEST_SUFFIX net70)
elseif(TEST_TFM STREQUAL "net8.0")
set(TEST_SUFFIX net80)
else()
message(FATAL_ERROR "TFM: ${TEST_TFM} is not supported.")
endif()
add_custom_command(
OUTPUT ${DOTNET_TEST_DIR}/timestamp_${TEST_SUFFIX}
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} build --nologo --framework ${TEST_TFM} -c Release ${TEST_NAME}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_TEST_DIR}/timestamp
DEPENDS
${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
${DOTNET_TEST_DIR}/${TEST_NAME}.cs
dotnet_package
BYPRODUCTS
${DOTNET_TEST_DIR}/bin
${DOTNET_TEST_DIR}/obj
VERBATIM
COMMENT "Compiling .Net ${COMPONENT_NAME}/${TEST_NAME}.cs (${TEST_TFM})"
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
add_custom_target(dotnet_${COMPONENT_NAME}_${TEST_NAME}_${TEST_SUFFIX} ALL
DEPENDS
${DOTNET_TEST_DIR}/timestamp_${TEST_SUFFIX}
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
if(BUILD_TESTING)
add_test(
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_${TEST_SUFFIX}
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} test --nologo --framework ${TEST_TFM} -c Release
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
endif()
message(STATUS " Configuring test ${FILE_NAME} (${TEST_TFM}) ...DONE")
endfunction()
# add_dotnet_test()
# CMake function to generate and build dotnet test.
# Currently only net6.0, net7.0 and net8.0 are supported
# Parameters:
# the dotnet filename
# e.g.:
# add_dotnet_test(FooTests.cs)
function(add_dotnet_test FILE_NAME)
message(STATUS "Configuring test ${FILE_NAME} ...")
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
set(DOTNET_TEST_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${TEST_NAME})
message(STATUS "build path: ${DOTNET_TEST_DIR}")
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/Test.csproj.in
${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
@ONLY)
add_custom_command(
OUTPUT ${DOTNET_TEST_DIR}/${TEST_NAME}.cs
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_TEST_DIR}
COMMAND ${CMAKE_COMMAND} -E copy
${FILE_NAME}
${DOTNET_TEST_DIR}/
MAIN_DEPENDENCY ${FILE_NAME}
VERBATIM
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
if(USE_DOTNET_6)
add_dotnet_tfm_test(${FILE_NAME} net6.0)
endif()
if(USE_DOTNET_7)
add_dotnet_tfm_test(${FILE_NAME} net7.0)
endif()
if(USE_DOTNET_8)
add_dotnet_tfm_test(${FILE_NAME} net8.0)
endif()
message(STATUS "Configuring test ${FILE_NAME} ...DONE")
endfunction()
#######################
## DOTNET WRAPPERS ##
#######################
list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}")
# Swig wrap all libraries
foreach(SUBPROJECT IN ITEMS
Foo
Bar
FooBar)
add_subdirectory(${SUBPROJECT}/dotnet)
target_link_libraries(${DOTNET_NATIVE_LIBRARY} PRIVATE dotnet_${SUBPROJECT})
endforeach()
file(COPY ${PROJECT_SOURCE_DIR}/dotnet/logo.png DESTINATION ${PROJECT_BINARY_DIR}/dotnet)
set(DOTNET_LOGO_DIR "${PROJECT_BINARY_DIR}/dotnet")
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/README.dotnet.md
${PROJECT_BINARY_DIR}/dotnet/README.md
COPYONLY)
set(DOTNET_README_DIR "${PROJECT_BINARY_DIR}/dotnet")
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/Directory.Build.props.in
${PROJECT_BINARY_DIR}/dotnet/Directory.Build.props)
file(MAKE_DIRECTORY ${DOTNET_PACKAGES_DIR})
############################
## .Net Runtime Package ##
############################
# *.csproj.in contains:
# CMake variable(s) (@PROJECT_NAME@) that configure_file() can manage and
# generator expression ($<TARGET_FILE:...>) that file(GENERATE) can manage.
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PACKAGE}.runtime.csproj.in
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj.in
@ONLY)
file(GENERATE
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in
INPUT ${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj.in)
add_custom_command(
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E copy ./$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in ${DOTNET_NATIVE_PROJECT}.csproj
DEPENDS
${DOTNET_NATIVE_PROJECT_DIR}/$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})
add_custom_command(
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} build --nologo -c Release -p:Platform=${DOTNET_PLATFORM} ${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} pack --nologo -c Release -p:Platform=${DOTNET_PLATFORM} ${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_NATIVE_PROJECT_DIR}/timestamp
DEPENDS
${PROJECT_BINARY_DIR}/dotnet/Directory.Build.props
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj
${DOTNET_NATIVE_LIBRARY}
BYPRODUCTS
${DOTNET_NATIVE_PROJECT_DIR}/bin
${DOTNET_NATIVE_PROJECT_DIR}/obj
VERBATIM
COMMENT "Generate .Net native package ${DOTNET_NATIVE_PROJECT} (${DOTNET_NATIVE_PROJECT_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})
add_custom_target(dotnet_native_package
DEPENDS
${DOTNET_NATIVE_PROJECT_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})
####################
## .Net Package ##
####################
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PROJECT}.csproj.in
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj.in
@ONLY)
add_custom_command(
OUTPUT ${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E copy ${DOTNET_PROJECT}.csproj.in ${DOTNET_PROJECT}.csproj
DEPENDS
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj.in
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})
add_custom_command(
OUTPUT ${DOTNET_PROJECT_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} build --nologo -c Release -p:Platform=${DOTNET_PLATFORM} ${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} pack --nologo -c Release -p:Platform=${DOTNET_PLATFORM} ${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_PROJECT_DIR}/timestamp
DEPENDS
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj
dotnet_native_package
BYPRODUCTS
${DOTNET_PROJECT_DIR}/bin
${DOTNET_PROJECT_DIR}/obj
VERBATIM
COMMENT "Generate .Net package ${DOTNET_PROJECT} (${DOTNET_PROJECT_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})
add_custom_target(dotnet_package ALL
DEPENDS
${DOTNET_PROJECT_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})
####################
## .Net Example ##
####################
# add_dotnet_example()
# CMake function to generate and build dotnet example.
# Parameters:
# the dotnet filename
# e.g.:
# add_dotnet_example(Foo.cs net48)
function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
message(STATUS "Configuring example ${FILE_NAME} (${EXAMPLE_TFM}) ...")
get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
set(DOTNET_EXAMPLE_DIR
${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${EXAMPLE_NAME}_${EXAMPLE_TFM})
message(STATUS "build path: ${DOTNET_EXAMPLE_DIR}")
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/Example.csproj.in
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.csproj
@ONLY)
add_custom_command(
OUTPUT ${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.cs
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_EXAMPLE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy
${FILE_NAME}
${DOTNET_EXAMPLE_DIR}/
MAIN_DEPENDENCY ${FILE_NAME}
VERBATIM
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
add_custom_command(
OUTPUT ${DOTNET_EXAMPLE_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} build --nologo --framework ${EXAMPLE_TFM} -c Release ${EXAMPLE_NAME}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} pack --nologo -c Release ${EXAMPLE_NAME}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_EXAMPLE_DIR}/timestamp
DEPENDS
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.csproj
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.cs
dotnet_package
BYPRODUCTS
${DOTNET_EXAMPLE_DIR}/bin
${DOTNET_EXAMPLE_DIR}/obj
VERBATIM
COMMENT "Compiling .Net ${COMPONENT_NAME}/${EXAMPLE_NAME}.cs for ${EXAMPLE_TFM} (${DOTNET_EXAMPLE_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
add_custom_target(dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${EXAMPLE_TFM} ALL
DEPENDS
${DOTNET_EXAMPLE_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
if(BUILD_TESTING)
add_test(
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${EXAMPLE_TFM}
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} run --no-build --framework ${EXAMPLE_TFM} -c Release ${EXAMPLE_NAME}.csproj
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
endif()
message(STATUS "Configuring example ${FILE_NAME} (${EXAMPLE_TFM}) done")
endfunction()