Skip to content

Commit 9cb9ed2

Browse files
committed
dotnet: Only generate .Net test for each .Net enable
* .Net tests do not support .Net Framework which need a static Main.
1 parent f934768 commit 9cb9ed2

File tree

1 file changed

+70
-49
lines changed

1 file changed

+70
-49
lines changed

cmake/dotnet.cmake

+70-49
Original file line numberDiff line numberDiff line change
@@ -131,40 +131,35 @@ endif()
131131
#################
132132
## .Net Test ##
133133
#################
134-
# add_dotnet_test()
135-
# CMake function to generate and build dotnet test.
134+
# add_dotnet_tfm_test()
135+
# CMake function to generate and build dotnet test for a specific TFM.
136+
# warning: only net6.0, net7.0 and net8.0 are supported
136137
# Parameters:
137138
# the dotnet filename
138139
# e.g.:
139-
# add_dotnet_test(FooTests.cs)
140-
function(add_dotnet_test FILE_NAME)
141-
message(STATUS "Configuring test ${FILE_NAME} ...")
140+
# add_dotnet_test(FooTests.cs net8.0)
141+
function(add_dotnet_tfm_test FILE_NAME TEST_TFM)
142+
message(STATUS " Configuring test ${FILE_NAME} (${TEST_TFM}) ...")
142143
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
143144
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
144145
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
145146

146147
set(DOTNET_TEST_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${TEST_NAME})
147-
message(STATUS "build path: ${DOTNET_TEST_DIR}")
148-
149-
configure_file(
150-
${PROJECT_SOURCE_DIR}/dotnet/Test.csproj.in
151-
${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
152-
@ONLY)
153148

154-
add_custom_command(
155-
OUTPUT ${DOTNET_TEST_DIR}/${TEST_NAME}.cs
156-
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_TEST_DIR}
157-
COMMAND ${CMAKE_COMMAND} -E copy
158-
${FILE_NAME}
159-
${DOTNET_TEST_DIR}/
160-
MAIN_DEPENDENCY ${FILE_NAME}
161-
VERBATIM
162-
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
149+
if(TEST_TFM STREQUAL "net6.0")
150+
set(TEST_SUFFIX net60)
151+
elseif(TEST_TFM STREQUAL "net7.0")
152+
set(TEST_SUFFIX net70)
153+
elseif(TEST_TFM STREQUAL "net8.0")
154+
set(TEST_SUFFIX net80)
155+
else()
156+
message(FATAL_ERROR "TFM: ${TEST_TFM} is not supported.")
157+
endif()
163158

164159
add_custom_command(
165-
OUTPUT ${DOTNET_TEST_DIR}/timestamp
160+
OUTPUT ${DOTNET_TEST_DIR}/timestamp_${TEST_SUFFIX}
166161
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
167-
${DOTNET_EXECUTABLE} build --nologo -c Release ${TEST_NAME}.csproj
162+
${DOTNET_EXECUTABLE} build --nologo --framework ${TEST_TFM} -c Release ${TEST_NAME}.csproj
168163
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_TEST_DIR}/timestamp
169164
DEPENDS
170165
${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
@@ -174,40 +169,66 @@ function(add_dotnet_test FILE_NAME)
174169
${DOTNET_TEST_DIR}/bin
175170
${DOTNET_TEST_DIR}/obj
176171
VERBATIM
177-
COMMENT "Compiling .Net ${COMPONENT_NAME}/${TEST_NAME}.cs (${DOTNET_TEST_DIR}/timestamp)"
172+
COMMENT "Compiling .Net ${COMPONENT_NAME}/${TEST_NAME}.cs (${TEST_TFM})"
178173
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
179174

180-
add_custom_target(dotnet_${COMPONENT_NAME}_${TEST_NAME} ALL
175+
add_custom_target(dotnet_${COMPONENT_NAME}_${TEST_NAME}_${TEST_SUFFIX} ALL
181176
DEPENDS
182-
${DOTNET_TEST_DIR}/timestamp
177+
${DOTNET_TEST_DIR}/timestamp_${TEST_SUFFIX}
183178
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
184179

185180
if(BUILD_TESTING)
186-
if(USE_DOTNET_6)
187-
add_test(
188-
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net60
189-
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
190-
${DOTNET_EXECUTABLE} test --nologo --framework net6.0 -c Release
191-
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
192-
endif()
193-
if(USE_DOTNET_7)
194181
add_test(
195-
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net70
182+
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_${TEST_SUFFIX}
196183
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
197-
${DOTNET_EXECUTABLE} test --nologo --framework net7.0 -c Release
184+
${DOTNET_EXECUTABLE} test --nologo --framework ${TEST_TFM} -c Release
198185
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
199-
endif()
200-
if(USE_DOTNET_8)
201-
add_test(
202-
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net80
203-
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
204-
${DOTNET_EXECUTABLE} test --nologo --framework net8.0 -c Release
205-
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
206-
endif()
207186
endif()
208-
message(STATUS "Configuring test ${FILE_NAME} ...DONE")
187+
message(STATUS " Configuring test ${FILE_NAME} (${TEST_TFM}) ...DONE")
209188
endfunction()
210189

190+
# add_dotnet_test()
191+
# CMake function to generate and build dotnet test.
192+
# Currently only net6.0, net7.0 and net8.0 are supported
193+
# Parameters:
194+
# the dotnet filename
195+
# e.g.:
196+
# add_dotnet_test(FooTests.cs)
197+
function(add_dotnet_test FILE_NAME)
198+
message(STATUS "Configuring test ${FILE_NAME} ...")
199+
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
200+
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
201+
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
202+
203+
set(DOTNET_TEST_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${TEST_NAME})
204+
message(STATUS "build path: ${DOTNET_TEST_DIR}")
205+
206+
configure_file(
207+
${PROJECT_SOURCE_DIR}/dotnet/Test.csproj.in
208+
${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
209+
@ONLY)
210+
211+
add_custom_command(
212+
OUTPUT ${DOTNET_TEST_DIR}/${TEST_NAME}.cs
213+
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_TEST_DIR}
214+
COMMAND ${CMAKE_COMMAND} -E copy
215+
${FILE_NAME}
216+
${DOTNET_TEST_DIR}/
217+
MAIN_DEPENDENCY ${FILE_NAME}
218+
VERBATIM
219+
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
220+
221+
if(USE_DOTNET_6)
222+
add_dotnet_tfm_test(${FILE_NAME} net6.0)
223+
endif()
224+
if(USE_DOTNET_7)
225+
add_dotnet_tfm_test(${FILE_NAME} net7.0)
226+
endif()
227+
if(USE_DOTNET_8)
228+
add_dotnet_tfm_test(${FILE_NAME} net8.0)
229+
endif()
230+
message(STATUS "Configuring test ${FILE_NAME} ...DONE")
231+
endfunction()
211232

212233
#######################
213234
## DOTNET WRAPPERS ##
@@ -328,7 +349,7 @@ add_custom_target(dotnet_package ALL
328349
# e.g.:
329350
# add_dotnet_example(Foo.cs net48)
330351
function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
331-
message(STATUS "Configuring example ${FILE_NAME} ...")
352+
message(STATUS "Configuring example ${FILE_NAME} (${EXAMPLE_TFM}) ...")
332353
get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE)
333354
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
334355
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
@@ -355,7 +376,7 @@ function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
355376
add_custom_command(
356377
OUTPUT ${DOTNET_EXAMPLE_DIR}/timestamp
357378
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
358-
${DOTNET_EXECUTABLE} build --nologo -r ${DOTNET_RID} -c Release ${EXAMPLE_NAME}.csproj
379+
${DOTNET_EXECUTABLE} build --nologo --framework ${EXAMPLE_TFM} -c Release ${EXAMPLE_NAME}.csproj
359380
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
360381
${DOTNET_EXECUTABLE} pack --nologo -c Release ${EXAMPLE_NAME}.csproj
361382
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_EXAMPLE_DIR}/timestamp
@@ -370,7 +391,7 @@ function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
370391
COMMENT "Compiling .Net ${COMPONENT_NAME}/${EXAMPLE_NAME}.cs for ${EXAMPLE_TFM} (${DOTNET_EXAMPLE_DIR}/timestamp)"
371392
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
372393

373-
add_custom_target(dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME} ALL
394+
add_custom_target(dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${EXAMPLE_TFM} ALL
374395
DEPENDS
375396
${DOTNET_EXAMPLE_DIR}/timestamp
376397
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
@@ -379,8 +400,8 @@ function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
379400
add_test(
380401
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${EXAMPLE_TFM}
381402
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
382-
${DOTNET_EXECUTABLE} run --no-build -r ${DOTNET_RID} -c Release ${EXAMPLE_NAME}.csproj
403+
${DOTNET_EXECUTABLE} run --no-build --framework ${EXAMPLE_TFM} -c Release ${EXAMPLE_NAME}.csproj
383404
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
384405
endif()
385-
message(STATUS "Configuring example ${FILE_NAME} done")
406+
message(STATUS "Configuring example ${FILE_NAME} (${EXAMPLE_TFM}) done")
386407
endfunction()

0 commit comments

Comments
 (0)