Skip to content

Commit

Permalink
CMake: CSharp: default to dotnet 6 (OSGeo#6843)
Browse files Browse the repository at this point in the history
- Updates the default Dotnet SDK level from the "out of support" net5 to net6. No one should be using the default in production - they should explicitly set the SDK they want - but this will avoid "out of support" messages appearing if they do.
- Adds a check that the requested Dotnet SDK is available on the machine and fails more gracefully if it is not.
  • Loading branch information
runette authored Dec 6, 2022
1 parent 3ba8444 commit b2248c5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
1 change: 0 additions & 1 deletion cmake/modules/thirdparty/FindCSharp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ elseif( CSHARP_MONO_FOUND )
set( CSHARP_VERSION ${CSHARP_MONO_VERSION} CACHE STRING "C# Mono compiler version" FORCE )
set( CSHARP_COMPILER ${CSHARP_MONO_COMPILER_${CSHARP_MONO_VERSION}} CACHE STRING "Full path to Mono compiler" FORCE )
set( CSHARP_INTERPRETER ${CSHARP_MONO_INTERPRETER_${CSHARP_MONO_VERSION}} CACHE STRING "Full path to Mono interpreter" FORCE )
set( CSHARP_SDK "/sdk:2" CACHE STRING "C# Mono SDK commandline switch (e.g. /sdk:2, /sdk:4, /sdk:5)" )
endif( )

# Handle WIN32 specific issues
Expand Down
18 changes: 17 additions & 1 deletion cmake/modules/thirdparty/FindDotnet.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# DOTNET_FOUND - True if dotnet executable is found
# DOTNET_EXE - Dotnet executable
# DOTNET_VERSION - Dotnet version as reported by dotnet executable
# DOTNET_SDKS - Dotnet SDKs loaded as reported by dotnet executable
# NUGET_EXE - Nuget executable (WIN32 only)
# NUGET_CACHE_PATH - Nuget package cache path
#
Expand Down Expand Up @@ -132,11 +133,26 @@ IF(NOT DOTNET_EXE)
ENDIF()

EXECUTE_PROCESS(
COMMAND ${DOTNET_EXE} --version
COMMAND "${DOTNET_EXE}" --version
OUTPUT_VARIABLE DOTNET_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

EXECUTE_PROCESS(
COMMAND "${DOTNET_EXE}" --list-sdks
OUTPUT_VARIABLE DOTNET_SDK_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(REGEX REPLACE "[\r\n]+" ";" DOTNET_SDK_STRING "${DOTNET_SDK_STRING}" )

set(DOTNET_SDKS)
foreach(ITR ${DOTNET_SDK_STRING} )
string(REGEX MATCH "^[0-9]+\.[0-9]+" SDK "${ITR}" )
list(APPEND DOTNET_SDKS ${SDK} )
endforeach()
list(REMOVE_DUPLICATES DOTNET_SDKS )

IF(WIN32)
FIND_PROGRAM(NUGET_EXE nuget PATHS ${CMAKE_BINARY_DIR}/tools)
IF(NUGET_EXE)
Expand Down
85 changes: 67 additions & 18 deletions swig/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,79 @@ set(GDAL_CSHARP_INSTALL_DIR
"${CMAKE_INSTALL_DATADIR}/csharp"
CACHE PATH "Installation sub-directory for CSharp bindings")

set(CSHARP_LIBRARY_VERSION
"net5.0"

if (DOTNET_FOUND)
set(CSHARP_LIBRARY_VERSION
"net6.0"
CACHE STRING ".NET version to be used for libraries")
set(CSHARP_APPLICATION_VERSION
"net6.0"
CACHE STRING ".NET version to be used for the sample Applications")
else ()
set(CSHARP_LIBRARY_VERSION
"4.8"
CACHE STRING ".NET version to be used for libraries")
set(CSHARP_APPLICATION_VERSION
"net5.0"
set(CSHARP_APPLICATION_VERSION
"4.8"
CACHE STRING ".NET version to be used for the sample Applications")
endif ()

set(CSHARP_CONFIG
"RELEASE"
CACHE STRING "Config to be used to compile the C# artifacts > RELEASE|CONFIG")

message(STATUS "Available SDKS: " "${DOTNET_SDKS}")


string(REGEX MATCH "[0-9\.]+" _CSHARP_LIBRARY_SDK_NUMBER "${CSHARP_LIBRARY_VERSION}" )
message(STATUS "Requested Library DotNet SDK Level: " ${_CSHARP_LIBRARY_SDK_NUMBER} )
string(REGEX MATCH "[0-9\.]+" _CSHARP_APPLICATION_SDK_NUMBER "${CSHARP_APPLICATION_VERSION}" )
message(STATUS "Requested Application DotNet SDK Level: " ${_CSHARP_APPLICATION_SDK_NUMBER} )

if ( DOTNET_FOUND )
if ( "${_CSHARP_LIBRARY_SDK_NUMBER}" IN_LIST DOTNET_SDKS)
message(STATUS "DotNet SDK " ${CSHARP_LIBRARY_VERSION} " found" )
else ()
message(STATUS "The Requested DotNet SDK was not found. C# Bindings will not be built" )
RETURN ()
endif ()
if ( NOT ( _CSHARP_LIBRARY_SDK_NUMBER STREQUAL _CSHARP_APPLICATION_SDK_NUMBER ) )
if ( "${_CSHARP_APPLICATION_SDK_NUMBER}" IN_LIST DOTNET_SDKS )
message(STATUS "DotNet SDK " ${CSHARP_APPLICATION_VERSION} " found" )
else ()
message(STATUS "The Requested DotNet SDK was not found. C# Bindings will not be built" )
RETURN ()
endif ()
endif ()
endif()

set(_VERSION_STRING ${GDAL_VERSION_NO_DEV_SUFFIX})
if (GDAL_DEV_SUFFIX)
set(_VERSION_STRING ${_VERSION_STRING}-${GDAL_DEV_SUFFIX})
endif ()

# based on https://stackoverflow.com/questions/39258250/how-to-detect-if-64-bit-msvc-with-cmake TODO - this not going to
# cope with ARM architectures
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CSHARP_RID "osx-x64")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CSHARP_RID "linux-x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "x64")
set(CSHARP_RID "win-x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32")
set(CSHARP_RID "win-x86")
if (DOTNET_FOUND)
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CSHARP_RID "osx-x64")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CSHARP_RID "linux-x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "x64")
set(CSHARP_RID "win-x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32")
set(CSHARP_RID "win-x86")
endif ()
else ()
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CSHARP_RID "x64")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CSHARP_RID "x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "x64")
set(CSHARP_RID "x64")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32")
set(CSHARP_RID "x86")
endif ()
endif ()

# needed because of differences in the packages
Expand All @@ -55,12 +103,12 @@ if (DOTNET_FOUND)
dotnet_register_local_repository("local" ${_DN_REPO_PATH})
endif ()

# Cop[y build files
# Copy build files
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/gdal.snk ${CMAKE_CURRENT_BINARY_DIR}/gdal.snk COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Directory.Build.props ${CMAKE_CURRENT_BINARY_DIR}/Directory.Build.props
COPYONLY)

# Crate some placemarker packages for dependencies
# Create some placemarker packages for dependencies
if (DOTNET_FOUND)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/osgeo.gdal.core.csproj.in"
"${CMAKE_CURRENT_BINARY_DIR}/osgeo.gdal.core.csproj")
Expand Down Expand Up @@ -145,7 +193,7 @@ function (gdal_csharp_dll)
set(_CSHARP_PROJ_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_CSHARP_TARGET_SUBDIR}")
set(_CSHARP_DLL_TARGET "${CMAKE_CURRENT_BINARY_DIR}/${_CSHARP_TARGET_SUBDIR}/${_CSHARP_TARGET}")
endif()
set(CSC_OPTIONS /unsafe /debug:full /target:library /out:${_CSHARP_DLL_TARGET})
set(CSC_OPTIONS /unsafe /debug:full /target:library /sdk:${_CSHARP_LIBRARY_SDK_NUMBER} /platform:${CSHARP_RID} /out:${_CSHARP_DLL_TARGET} )
if (WIN32)
list(APPEND CSC_OPTIONS /define:CLR4)
endif ()
Expand Down Expand Up @@ -204,7 +252,7 @@ function (gdal_csharp_dll)

else ()
if (CMAKE_VERBOSE_MAKEFILE)
message(STATUS "BUILDING : " ${_CSHARP_TARGET} " : " ${CSHARP_COMPILER} ${CSC_OPTIONS} /out:${_CSHARP_TARGET})
message(STATUS "BUILDING : " ${_CSHARP_TARGET} " : " ${CSHARP_COMPILER} ${CSC_OPTIONS} )
endif ()
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_CSHARP_TARGET}
Expand Down Expand Up @@ -397,15 +445,16 @@ function (gdal_build_csharp_sample)
${_GBCS_SYSTEM_DEPENDS})

else ()
list(APPEND CSC_OPTIONS /sdk:${_CSHARP_APPLICATION_SDK_NUMBER} /platform:${CSHARP_RID} /out:${_GBCS_OUTPUT_NATIVE} ${SOURCE_NATIVE})
if (CMAKE_VERBOSE_MAKEFILE)
message(
STATUS
"BUILDING : ${_GBCS_OUTPUT} : ${CSHARP_COMPILER} ${CSC_OPTIONS} /out:${_GBCS_OUTPUT_NATIVE} ${SOURCE_NATIVE}")
"BUILDING : " ${_GBCS_OUTPUT} " : " ${CSHARP_COMPILER} ${CSC_OPTIONS} )
endif ()
add_custom_command(
OUTPUT ${OUTPUT_FILENAME}
COMMAND ${CMAKE_COMMAND} -E echo "Building ${_GBCS_OUTPUT_NATIVE}"
COMMAND ${CSHARP_COMPILER} ${CSC_OPTIONS} /out:${_GBCS_OUTPUT_NATIVE} ${SOURCE_NATIVE}
COMMAND ${CSHARP_COMPILER} ${CSC_OPTIONS}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
DEPENDS csharp_binding ${_GBCS_SOURCE})
Expand Down
1 change: 0 additions & 1 deletion swig/csharp/add_dll_xsc.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

file(GLOB SOURCES "${TARGET_SUBDIR}/*.cs")
list(APPEND SOURCES ${SOURCE_DIR}/AssemblyInfo.cs)
set(NATIVE_SOURCES)
Expand Down

0 comments on commit b2248c5

Please sign in to comment.