Skip to content

Commit

Permalink
cmake: Rework TFM management
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Sep 11, 2024
1 parent 1d0ac39 commit 9c21d27
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
41 changes: 25 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,49 @@ set(DOTNET_LANG "8.0" CACHE STRING "Specify the C# language version (default \"8
message(STATUS ".Net C# language version: ${DOTNET_LANG}")

# Targeted Framework Moniker
# see: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
# see: https://learn.microsoft.com/en-us/dotnet/standard/frameworks
# see: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
option(USE_DOTNET_46 "Use .Net Framework 4.6 support" OFF)

## .Net Standard
option(USE_DOTNET_STD_20 "Use .Net Standard 2.0 support" OFF)
message(STATUS ".Net: Use .Net Standard 2.0 support: ${USE_DOTNET_STD_20}")
option(USE_DOTNET_STD_21 "Use .Net Standard 2.1 support" OFF)
message(STATUS ".Net: Use .Net Standard 2.1 support: ${USE_DOTNET_STD_21}")

## .Net Framework
# see: https://learn.microsoft.com/en-us/lifecycle/products/microsoft-net-framework
option(USE_DOTNET_46 "Use .Net Framework 4.6 support" OFF) # EOL: 2022-04-22
message(STATUS ".Net: Use .Net Framework 4.6 support: ${USE_DOTNET_46}")
option(USE_DOTNET_461 "Use .Net Framework 4.6.1 support" OFF)
option(USE_DOTNET_461 "Use .Net Framework 4.6.1 support" OFF) # EOL: 2022-04-22
message(STATUS ".Net: Use .Net Framework 4.6.1 support: ${USE_DOTNET_461}")
option(USE_DOTNET_462 "Use .Net Framework 4.6.2 support" OFF)
option(USE_DOTNET_462 "Use .Net Framework 4.6.2 support" OFF) # EOL: 2027-01-12
message(STATUS ".Net: Use .Net Framework 4.6.2 support: ${USE_DOTNET_462}")

option(USE_DOTNET_48 "Use .Net Framework 4.8 support" OFF)
message(STATUS ".Net: Use .Net Framework 4.8 support: ${USE_DOTNET_48}")

option(USE_DOTNET_STD_20 "Use .Net Standard 2.0 support" OFF)
message(STATUS ".Net: Use .Net Framework 2.0 support: ${USE_DOTNET_STD_20}")
option(USE_DOTNET_47 "Use .Net Framework 4.7 support" OFF) # EOL: NA
message(STATUS ".Net: Use .Net Framework 4.7 support: ${USE_DOTNET_47}")

option(USE_DOTNET_STD_21 "Use .Net Standard 2.1 support" OFF)
message(STATUS ".Net: Use .Net Framework 2.1 support: ${USE_DOTNET_STD_21}")
option(USE_DOTNET_48 "Use .Net Framework 4.8 support" OFF) # EOL: NA
message(STATUS ".Net: Use .Net Framework 4.8 support: ${USE_DOTNET_48}")

## .Net and .Net Core
# see: https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core
# .Net Core 3.1 LTS is not available for osx arm64
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(USE_DOTNET_CORE_31 OFF)
else()
option(USE_DOTNET_CORE_31 "Use .Net Core 3.1 LTS support" OFF)
option(USE_DOTNET_CORE_31 "Use .Net Core 3.1 support" OFF) # EOL: 2022-12-13
endif()
message(STATUS ".Net: Use .Net Core 3.1 LTS support: ${USE_DOTNET_CORE_31}")
message(STATUS ".Net: Use .Net Core 3.1 support: ${USE_DOTNET_CORE_31}")

option(USE_DOTNET_6 "Use .Net 6 LTS support" ON)
option(USE_DOTNET_6 "Use .Net 6 LTS support" ON) # EOL: 2024-11-12
message(STATUS ".Net: Use .Net 6 LTS support: ${USE_DOTNET_6}")

option(USE_DOTNET_7 "Use .Net 7.0 support" OFF)
option(USE_DOTNET_7 "Use .Net 7.0 support" OFF) # EOL: 2024-05-14
message(STATUS ".Net: Use .Net 7.0 support: ${USE_DOTNET_7}")

option(USE_DOTNET_8 "Use .Net 8.0 LTS support" OFF)
option(USE_DOTNET_8 "Use .Net 8.0 LTS support" OFF) # EOL: 2026-11-10
message(STATUS ".Net: Use .Net 8.0 support: ${USE_DOTNET_8}")

include(dotnet)

add_subdirectory(tests)
Expand Down
52 changes: 37 additions & 15 deletions cmake/dotnet.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ 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()
Expand All @@ -59,15 +66,13 @@ 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_STD_20)
list(APPEND TFM "netstandard2.0")
endif()
if(USE_DOTNET_STD_21)
list(APPEND TFM "netstandard2.1")
endif()

if(USE_DOTNET_CORE_31)
list(APPEND TFM "netcoreapp3.1")
endif()
Expand Down Expand Up @@ -178,11 +183,27 @@ function(add_dotnet_test FILE_NAME)
WORKING_DIRECTORY ${DOTNET_TEST_DIR})

if(BUILD_TESTING)
add_test(
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} test --nologo --no-build -c Release ${TEST_NAME}.csproj
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
if(USE_DOTNET_6)
add_test(
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net60
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} test --nologo --framework net6.0 -c Release
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
endif()
if(USE_DOTNET_7)
add_test(
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net70
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} test --nologo --framework net7.0 -c Release
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
endif()
if(USE_DOTNET_8)
add_test(
NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}_net80
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} test --nologo --framework net8.0 -c Release
WORKING_DIRECTORY ${DOTNET_TEST_DIR})
endif()
endif()
message(STATUS "Configuring test ${FILE_NAME} ...DONE")
endfunction()
Expand Down Expand Up @@ -306,13 +327,14 @@ add_custom_target(dotnet_package ALL
# the dotnet filename
# e.g.:
# add_dotnet_example(Foo.cs net48)
function(add_dotnet_example FILE_NAME TFM)
function(add_dotnet_example FILE_NAME EXAMPLE_TFM)
message(STATUS "Configuring example ${FILE_NAME} ...")
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}_${TFM})
set(DOTNET_EXAMPLE_DIR
${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${EXAMPLE_NAME}_${EXAMPLE_TFM})
message(STATUS "build path: ${DOTNET_EXAMPLE_DIR}")

configure_file(
Expand Down Expand Up @@ -345,7 +367,7 @@ function(add_dotnet_example FILE_NAME TFM)
${DOTNET_EXAMPLE_DIR}/bin
${DOTNET_EXAMPLE_DIR}/obj
VERBATIM
COMMENT "Compiling .Net ${COMPONENT_NAME}/${EXAMPLE_NAME}.cs for ${TFM} (${DOTNET_EXAMPLE_DIR}/timestamp)"
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} ALL
Expand All @@ -355,7 +377,7 @@ function(add_dotnet_example FILE_NAME TFM)

if(BUILD_TESTING)
add_test(
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${TFM}
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_${EXAMPLE_TFM}
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME
${DOTNET_EXECUTABLE} run --no-build -r ${DOTNET_RID} -c Release ${EXAMPLE_NAME}.csproj
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Example.csproj.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>@DOTNET_LANG@</LangVersion>
<TargetFramework>@TFM@</TargetFramework>
<TargetFramework>@EXAMPLE_TFM@</TargetFramework>
<EnableDefaultItems>false</EnableDefaultItems>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<!-- see https://github.com/dotnet/docs/issues/12237 -->
Expand Down

0 comments on commit 9c21d27

Please sign in to comment.