0

I'm trying to write the CMakeLists.txt for building a custom targets only project.

That is, it's not for the usual C/C++ project; all build recipes are provided using ADD_CUSTOM_COMMAND() and ADD_CUSTOM_TARGET(). The reason why I'm using CMake is to manage dependencies between build target stuffs and utilize the advantages of incremental build.

The problem is, when I execute cmake CMakeLists.txt on Windows cmd.exe, it tries to find Windows SDK or MSBuild.exe -- which are never needed for building targets in the project.

cmd.exe> cmake CMakeLists.txt
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17134.
CMake Error at CMakeLists.txt:6 (PROJECT):
  Failed to run MSBuild Command:

    MSBuild.exe

  to get the value of VCTargetsPath:

    Cannot find file

-- Configuring incomplete, errors occurred!
See also "CMakeFiles/CMakeOutput.log"

The header of the CMakeLists.txt is:

cmake_minimum_required ( VERSION 3.0 )
set ( CMAKE_GENERATOR "Unix Makefile" )
set ( CMAKE_VERBOSE_MAKEFILE true )

# Project name
project ( "awesome-project" NONE )  # <language>=NONE for skipping compiler check

...

As I mentioned above, all recipes of the build targets are provided in the CMakeLists.txt; neither Windows SDK nor Visual Studio is required.

I think there is some CMake directives for skipping the invocation of MSBuild, but I could not find it. How can I write the CMakeLists.txt in this case?

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • CMake uses `MSBuild.exe` not for the *compiling*, but for **building** the project - it will detect changes in dependent files and run corresponded commands you use in `add_custom_target` and `add_custom_command`. The thing is that CMake is only a **configuration** tool, not a *build* one. The tool used for build is choosen according to the [generator](https://cmake.org/cmake/help/v3.9/manual/cmake-generators.7.html), which is among a few things you cannot set inside `CMakeLists.txt`. Build tool `MSBuild.exe` corresponds to Visual Studio generator. – Tsyvarev Sep 26 '19 at 06:20

2 Answers2

0

As the answer here suggests, setting the CMAKE_GENERATOR variable in the CMake file has no effect when running CMake for the first time. CMake will use the default generator on the first attempt, if no generator is provided via the command line or via environment variable. So try adding the command line option -G to specify the generator like this:

cmake -G "Unix Makefiles" .

Then, on subsequent CMake runs, the generator setting will be cached and you no longer need to provide the -G generator option.

As a side note, you can simply provide the path to your top-level CMakeLists.txt file. So if it's in the current directory, you can just use . for the path.

Kevin
  • 16,549
  • 8
  • 60
  • 74
0

The documentation is pretty explicit The value of this variable should never be modified by project code. A generator may be selected via the cmake(1) -G option, interactively in cmake-gui(1), or via the CMAKE_GENERATOR environment variable.

You can always make a CMake script that runs CMake with the proper options to create the project.

You may want to keep watch on CMAKE_VERBOSE_MAKEFILE because This variable is a cache entry initialized (to FALSE) by the project() command. I wouldn't be surprised if it doesn't work as shown in your script. Instead You'd probably have to add a -D option on the command line to set it. https://bytefreaks.net/programming-2/make-building-with-cmake-verbose

fdk1342
  • 3,274
  • 1
  • 16
  • 17