cmake: how to force a project not to use cache? - caching

Projects generate a CMakeCache.txt file, and as a result the cmake instructions of the CMakeLists.txt will not run twice if the CMakeLists.txt is not changed.
I would like a specific project to not generate a CMakeCache.txt file (or alternatively, to ignore the one that has been generated). I would like this to be automated, i.e. not to have to delete the file manually.
Context: I am using colcon to compile several projects, and I would like the cache to be ignored only for a specific package, no all.
(An alternative could be to be able to force the execution of a specific macro, independently of whether or not the CMakeLists.txt file has been changed or not.)

Related

How can I continue an interrupted compilation after an error occurred?

After an error occurred because of a missing flag or incorrectly set environment variable, is it possible to continue compiling once the mistake has been fixed?
I regularly use CMake and make to compile toolkits that take quite a while to compile and, also regularly, I accidentally set variables incorrectly in the process. Just now for example, I was attempting to include OpenInventor headers which on my machine are located in the directory /Users/user/software/prod/coin/include/Inventor.
I mistakenly passed
-DINVENTOR_INCLUDE_DIR=/Users/user/software/prod/coin/include/Inventor
rather than the correct
-DINVENTOR_INCLUDE_DIR=/Users/user/software/prod/coin/include
This only became an issue after 30 minutes when about 95% of the compilation was completed. Because I knew that reconfiguring using CMake would force a recompilation from scratch, I tried to add -I/Users/user/software/prod/coin/include to CMAKE_CXX_FLAGS in CMakeCache.txt but to no avail–it still recompiled from scratch. Since only a single source file actually includes the headers in question, it would be desirable if I could start compiling from the point where it exited with an error once the relevant path has been corrected. How can I do this and, as an aside, why does it force the compiler to start from scratch?
I'm using CMake version 3.11.1 and clang (Apple LLVM version 9.1.0) on macOS 10.13
CMake does not need to recompile everything just because it regenerates its makefiles. It will still perform normal make avoidance operations. However CMake does track the compiler options used to build each target, so if you make a change in the compiler options for all the targets then they'll all need to be rebuilt.
If this compiler option is only needed for one target, you can add it to just that target an no others, with something like this:
set_property(SOURCE my_source.c APPEND PROPERTY
COMPILE_FLAGS -I/foo/bar)
then it should only rebuild that one source file.
CMake looks for files' "last modified" times to decide which files need recompilation. But if you change the input to CMake itself, then it needs to regenerate the Makefiles and therefore recompile everything. But still, one hack may be possible...
CMake stores information about the include directories and the libraries to be linked in various text files in the build directory. So one hack (not recommended, but works) can be to modify these text files.
In the particular example that you mentioned, the hack would be to search and replace all occurrences of /Users/user/software/prod/coin/include/Inventor with /Users/user/software/prod/coin/include in all the files of the build directory.
(As an aside, if you don't already know, you can use make -j <n> to build using multiple threads which can considerably decrease the build times.)

Is it possible to figure out how cmake is called from cmake files and caches?

Within the build directory, I can see values of particular cmake variables using ccmake .. (assuming CMakeLists.txt sits on parent directory).
This becomes less effective when the original call to cmake has set many different variables in a large cmake project. For instance, if I call cmake like so:
cmake ..
-DCMAKE_PREFIX_PATH="path_1;path_2;ect"
-DCMAKE_INSTALL_RPATH="yet_another_path"
-DCMAKE_INSTALL_PREFIX="yet_another_path"
-DCMAKE_BUILD_TYPE=...
-DPROJECT_SPECIFIC_FLAG_1=...
-DPROJECT_SPECIFIC_FLAG_2=...
Is there anyway to be find out about this original call to CMakeLists.txt file from the cache or files? Is this string stored somewhere?

cmake-gui show blank except source code directory and binaries directory

After installing cmake-3.8.1-win64-x64 I got thisenter image description here
So what can I do with this? Thanks.
cmake-gui does not help you create cmake configuration files, it parses these files to generate and configure projects.
In your source code directory, you should have a CMakeLists.txt file which defines the rules for CMAKE to configure your problem. That directory should be entered into the first box.
Next, you get to decide where to build the binaries. We could do it in the source directory, but the generated artifacts could pollute what is already there. "Cleaning" the build by deleting all of those artifacts while keeping the original sources is tedious at best, so it's a good idea to make an empty directory and use that as your binaries path.
Once you have those fields entered, you should be able to "Generate" or "Configure" your project. If you need help creating a CMakeLists.txt file (that's really the complicated part), then check out their tutorial.

Is it possible to change the name of generated makefile in CMake?

I have a project which uses Makefiles. On a branch, I have CMake based build system. Now some team-members wants the OLD make-files based system intact, when cmake is added. But this is not possible after cmake . command overwrites the old Makefile.
Now I can easily avoid it if I can tell CMake to generate makefiles with some non-standard names e.g. makefile.cmake etc. Is it possible?
I am open to consider other options as well. In any case, old Makefiles must not be touched.
Cmake creates a build system in the working directory. So create any empty directory, and run cmake <path-to-source> from there.
Unfortunately, the name "Makefile" in hard-coded several times, in the sources of CMake. You cannot change it. As Peter has pointed out in the other answer, that change is not necessary, because CMake support out-of-source builds.

Is it possible to create a cmake script that generates a build that handles unknown files generated at build time?

I have a system that produces generated code from a spec document which can change at any time. As such, the list of files being generated cannot be static, and must be able to be handled dynamically at build time.
From what I can tell the typical CMakeLists.txt is set up to define a rule for each file at cmake generation time.
Is there a way to get CMake to write generic rules so that the targets can be set at build time?
If not, what are the possible work-arounds?
First, you can put your code generation process into CMakeLists.txt in a form of a target (add_custom_command), and then make your target depends on this command. This was code generation steps will be run every time you issue make.
Alternatively, here is a hack:
add_custom_target(cmake_regen ALL
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/CMakeFiles/Makefile.cmake)
And a less hackish variant, which should preserve already built targets:
add_custom_target(cmake_regen ALL
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target rebuild_cache)
Adding such code into CMakeLists.txt forces CMake to regenerate makefiles on each run without running full configuration process.

Resources