diff options
author | Sumit Bhardwaj <bhardwajs@outlook.com> | 2021-12-15 16:21:34 (GMT) |
---|---|---|
committer | Sumit Bhardwaj <bhardwajs@outlook.com> | 2021-12-21 17:35:49 (GMT) |
commit | 0eea32a376fc54b198fa4690ca57b829a2d14baa (patch) | |
tree | c786935ed240c0857c0e82d87ffc27149fdade33 /Tests | |
parent | a450cc9533ac8a54d14ec5e0cbf6b379a2014e7a (diff) | |
download | CMake-0eea32a376fc54b198fa4690ca57b829a2d14baa.zip CMake-0eea32a376fc54b198fa4690ca57b829a2d14baa.tar.gz CMake-0eea32a376fc54b198fa4690ca57b829a2d14baa.tar.bz2 |
VS: Add DOTNET_SDK property to generate SDK-style C# projects
Changes in cmVisualStudio10TargetGenerator::Generate to write .Net
SDK-style project for VS generators VS 19 and above. Also adds
documentation and tests.
Issue: #20227
Diffstat (limited to 'Tests')
14 files changed, 170 insertions, 0 deletions
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 5dc7031..dec147b 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -616,6 +616,10 @@ if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([^9]|9[0-9])") endif() endif() +if(CMAKE_GENERATOR MATCHES "^Visual Studio (1[6-9]|[2-9][0-9])") + add_RunCMake_test(VsDotnetSdk) +endif() + if(XCODE_VERSION) add_RunCMake_test(XcodeProject -DXCODE_VERSION=${XCODE_VERSION}) add_RunCMake_test(XcodeProject-Embed -DXCODE_VERSION=${XCODE_VERSION}) diff --git a/Tests/RunCMake/VsDotnetSdk/CMakeLists.txt b/Tests/RunCMake/VsDotnetSdk/CMakeLists.txt new file mode 100644 index 0000000..e597708 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.22.0) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables-check.cmake b/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables-check.cmake new file mode 100644 index 0000000..7a5cd1d --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables-check.cmake @@ -0,0 +1,52 @@ +set(files foo.csproj bar.csproj baz.csproj) + +set(inLib1 FALSE) +set(dotnetSdkInLib1 FALSE) + +set(inLib2 FALSE) +set(dotnetSdkWebInLib2 FALSE) + +set(inLib3 FALSE) +set(classicProjInLib3 FALSE) + +foreach(file ${files}) + set(csProjectFile ${RunCMake_TEST_BINARY_DIR}/${file}) + + if(NOT EXISTS "${csProjectFile}") + set(RunCMake_TEST_FAILED "Project file ${csProjectFile} does not exist.") + return() + endif() + + file(STRINGS "${csProjectFile}" lines) + + foreach(line IN LISTS lines) + if(NOT inLib1) + if(line MATCHES "<Project Sdk=\"Microsoft\.NET\.Sdk\">") + set(dotnetSdkInLib1 TRUE) + set(inLib1 TRUE) + endif() + elseif(NOT inLib2) + if(line MATCHES "<Project Sdk=\"Microsoft\.NET\.Sdk\.Web\">") + set(dotnetSdkWebInLib2 TRUE) + set(inLib2 TRUE) + endif() + elseif(NOT inLib3) + if(line MATCHES "<Project DefaultTargets=\"Build\" ToolsVersion=\"") + set(classicProjInLib3 TRUE) + set(inLib3 TRUE) + endif() + endif() + endforeach() +endforeach() + +if(NOT dotnetSdkInLib1) + set(RunCMake_TEST_FAILED ".Net SDK not set correctly.") +endif() + +if(NOT dotnetSdkWebInLib2) + set(RunCMake_TEST_FAILED ".Net Web SDK not set correctly.") +endif() + +if(NOT classicProjInLib3) + set(RunCMake_TEST_FAILED "Empty DOTNET_SDK doesn't build Classic project.") +endif() diff --git a/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables.cmake b/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables.cmake new file mode 100644 index 0000000..f080edd --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/DotnetSdkVariables.cmake @@ -0,0 +1,14 @@ +enable_language(CSharp) + +if(NOT CMAKE_CSharp_COMPILER) + return() +endif() + +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") +add_library(foo SHARED lib1.cs) + +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk.Web") +add_library(bar SHARED lib1.cs) + +set(CMAKE_DOTNET_SDK "") +add_library(baz SHARED lib1.cs) diff --git a/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake b/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake new file mode 100644 index 0000000..b174c25 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake @@ -0,0 +1,17 @@ +cmake_policy(SET CMP0053 NEW) +include(RunCMake) + +run_cmake(VsDotnetSdkCustomCommandsTarget) +run_cmake(VsDotnetSdkCustomCommandsSource) +run_cmake(DotnetSdkVariables) + +function(run_VsDotnetSdk) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/VsDotnetSdk-build) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + run_cmake(VsDotnetSdk) + set(build_flags /restore) + run_cmake_command(VsDotnetSdk-build ${CMAKE_COMMAND} --build . -- ${build_flags}) +endfunction() +run_VsDotnetSdk() diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdk.cmake b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdk.cmake new file mode 100644 index 0000000..60066ab --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdk.cmake @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.22) + +# a simple CSharp only test case +project (DotNetSdk CSharp) + +set(CMAKE_DOTNET_TARGET_FRAMEWORK net472) +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") + +add_library(dotNetSdkLib1 SHARED lib1.cs) +set_target_properties(dotNetSdkLib1 + PROPERTIES + VS_GLOBAL_RuntimeIdentifier win10-x64) + +add_executable(DotNetSdk csharponly.cs) +target_link_libraries(DotNetSdk dotNetSdkLib1) +set_target_properties(DotNetSdk + PROPERTIES + VS_GLOBAL_RuntimeIdentifier win10-x64) diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt new file mode 100644 index 0000000..90af627 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt @@ -0,0 +1,7 @@ +CMake Error in CMakeLists.txt: + The target "foo" does not currently support add_custom_command as the + Visual Studio generators have not yet learned how to generate custom + commands in .Net SDK-style projects. + + +CMake Generate step failed. Build files cannot be regenerated correctly. diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake new file mode 100644 index 0000000..af18946 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake @@ -0,0 +1,15 @@ +enable_language(CSharp) + +if(NOT CMAKE_CSharp_COMPILER) + return() +endif() + +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") +add_custom_command( + OUTPUT bar.cs + COMMAND copy /A ${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs + bar.cs + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs + VERBATIM) + +add_library(foo SHARED bar.cs) diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-result.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-result.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-result.txt diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-stderr.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-stderr.txt new file mode 100644 index 0000000..90af627 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget-stderr.txt @@ -0,0 +1,7 @@ +CMake Error in CMakeLists.txt: + The target "foo" does not currently support add_custom_command as the + Visual Studio generators have not yet learned how to generate custom + commands in .Net SDK-style projects. + + +CMake Generate step failed. Build files cannot be regenerated correctly. diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget.cmake b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget.cmake new file mode 100644 index 0000000..f5cd317 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsTarget.cmake @@ -0,0 +1,12 @@ +enable_language(CSharp) + +if(NOT CMAKE_CSharp_COMPILER) + return() +endif() + +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") +add_library(foo SHARED lib1.cs) +add_custom_command(TARGET foo + PRE_BUILD + COMMAND echo "This shouldn't happen!" + VERBATIM) diff --git a/Tests/RunCMake/VsDotnetSdk/csharponly.cs b/Tests/RunCMake/VsDotnetSdk/csharponly.cs new file mode 100644 index 0000000..f02e8a3 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/csharponly.cs @@ -0,0 +1,11 @@ +namespace CSharpOnly +{ + class CSharpOnly + { + public static void Main(string[] args) + { + int val = Lib1.getResult(); + return; + } + } +} diff --git a/Tests/RunCMake/VsDotnetSdk/lib1.cs b/Tests/RunCMake/VsDotnetSdk/lib1.cs new file mode 100644 index 0000000..7a7ae10 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/lib1.cs @@ -0,0 +1,10 @@ +namespace CSharpOnly +{ + public class Lib1 + { + public static int getResult() + { + return 23; + } + } +} |