POST_BUILD not running even though always fails for C# Visual Studio 2017
The problem is that the project "Test" isn't being rebuilt, instead the program is being run even though the POST_BUILD command has never succeeded.
Steps to reproduce:
-
Start with a fresh project using the below CMake and main.cs files.
-
Configure project for a Visual Studio 2017 Win64 solution.
-
Open project and press the "Start" button at the top beside the green play icon. The build should fail as the POST_BUILD command will always fail
-
Run again by pressing the "Start" button again. The build will continue to fail as expected.
-
Close Visual Studio 2017 and reopen the project.
Note: this behavior also happens if you edit the CMakeLists.txt file and the Visual Studio projects are reloaded. Basically anything that causes the project to be closed and reopened.
-
Press the "start" button again, and now the program will be run instead of being built.
-
Delete the outputted executable file.
-
Press the "start" button again more than once and now the expected bahavior occurs. It seems that the Test.exe being present makes CMake think the POST_BUILD script has succeeded or it doesn't check that the command has succeeded.
This is different than the behavior for an equivalent C++ project. When Visual Studio is closed and reopened with a C++ project the project is rebuilt if the POST_BUILD command has not succeeded.
cmake_minimum_required(VERSION 3.12.4)
project(Test CSharp)
ADD_EXECUTABLE(${PROJECT_NAME} main.cs)
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${PROJECT_NAME})
class Test
{
public static int Main(string[] args)
{
return -1; // Never succeed
}
}