Vs: fix CSharp custom command by introducing inline MSBuild <Targets>s

The custom command implementation is based on the Microsoft support article:

https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-extend-the-visual-studio-build-process

Fixes: #16960
This commit is contained in:
Michael Stürmer
2017-06-22 21:40:48 +02:00
parent dcdab5cf23
commit ec409a116f
9 changed files with 170 additions and 6 deletions
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.3)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)
@@ -0,0 +1,21 @@
if(checkLevel EQUAL 0)
message("checking generation (${srcName} does not exist)")
if(EXISTS "${generatedFileName}")
set(RunCMake_TEST_FAILED "file \"${generatedFileName}\" should not exist")
endif()
elseif(checkLevel EQUAL 1)
message("checking build 1 (generate ${srcName})")
if(NOT "${actual_stdout}" MATCHES "${commandComment}")
set(RunCMake_TEST_FAILED "command not executed")
endif()
elseif(checkLevel EQUAL 2)
message("checking build 2 (no change in ${srcName}.in)")
if("${actual_stdout}" MATCHES "${commandComment}")
set(RunCMake_TEST_FAILED "command executed")
endif()
elseif(checkLevel EQUAL 3)
message("checking build 3 (update ${srcName})")
if(NOT "${actual_stdout}" MATCHES "${commandComment}")
set(RunCMake_TEST_FAILED "command not executed")
endif()
endif()
@@ -0,0 +1,13 @@
enable_language(CSharp)
add_executable(CSharpCustomCommand dummy.cs)
add_custom_command(OUTPUT ${generatedFileName}
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${inputFileName} ${generatedFileName}
MAIN_DEPENDENCY ${inputFileName}
COMMENT "${commandComment}")
target_sources(CSharpCustomCommand PRIVATE
${inputFileName}
${generatedFileName})
@@ -0,0 +1,34 @@
include(RunCMake)
# Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CommandWithOutput-build)
set(RunCMake_TEST_NO_CLEAN 1)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
set(RunCMake-check-file CommandWithOutput-check.cmake)
set(srcName "test.cs")
set(srcFileName "${CMAKE_CURRENT_LIST_DIR}/${srcName}.in")
set(inputFileName "${RunCMake_TEST_BINARY_DIR}/${srcName}.in")
set(generatedFileName "${RunCMake_TEST_BINARY_DIR}/${srcName}")
set(commandComment "Generating ${srcName}")
# copy the input file to build dir to avoid changing files in cmake
# source tree.
file(COPY "${srcFileName}" DESTINATION "${RunCMake_TEST_BINARY_DIR}")
set(RunCMake_TEST_OPTIONS ${RunCMake_TEST_OPTIONS}
"-DinputFileName=${inputFileName}"
"-DgeneratedFileName=${generatedFileName}"
"-DcommandComment=${commandComment}")
set(checkLevel 0)
run_cmake(CommandWithOutput)
set(checkLevel 1)
run_cmake_command(CommandWithOutput-build1 ${CMAKE_COMMAND} --build . --config Debug)
set(checkLevel 2)
run_cmake_command(CommandWithOutput-build2 ${CMAKE_COMMAND} --build . --config Debug)
# change file content to trigger custom command with next build
file(APPEND ${inputFileName} "\n")
set(checkLevel 3)
run_cmake_command(CommandWithOutput-build3 ${CMAKE_COMMAND} --build . --config Debug)
@@ -0,0 +1,8 @@
class TestCs
{
public static int Main(string[] args)
{
System.Console.WriteLine("Test C#");
return 0;
}
}