cmake how can I reset the cmake cache? - cmake

During out-of-source build, after changing the cmake parameters, I want to rebuild the project without remove the entire build dir. However, there are some CMakeCache.txt files retain the old configuration from last build. In Clion there is any option to reset the cmake cache and reload the project without touching the built result. However, I cannot reproduce this behaviour with cmake command. Anyone can help me out here?
here is what I tried:
I try to remove the CMakeCache.txt files to get rid of the caches.
if I only remove the top one, it actually works fine. However, I have lots of sub-directories also contain CMakeCache.txt. Sometime the cache in subdir conflict with the newly generated top CMakeCache.txt.
Then I try to remove all of them. But it won't work. The cmake complains that it cannot add subdirectories because it is not the child dir of source code. I guess this is because the cmake getting confused when seeing the sub projects but cannot find cache variables.

Related

How do I stop CMake from searching for a file I removed from my dependencies entirely?

I used to have a file called game/test.dae in my current CMake project. It was used in an add_custom_command call as a dependency.
I removed this file as a dependency from my CMakeLists.txt quite some time ago, but for some reason, my windows build environment hasn't gotten the memo (it works on macOS, though):
NMAKE : fatal error U1073: don't know how to make '..\game\test.dae'
I keep running into this for old dependencies whenever I remove them in this one particular project, on windows only, and it seems the only 'fix' is to delete my build directory and start over - which is incredibly inconvenient, as I use some third-party libraries that can take up to 10 mins. to build.
Is there any way to force CMake to forget this file was ever a dependency without purging all my cmake files, cache, binaries, etc?
While it looks like a bug in CMake, it can be worked around by deleting every Makefile in the build directory.
On Unix-like it can be achieved with find build -name Makefile -delete.

How to make CMake re-run from scratch without deleting and re-creating the build directory

Is there a way to make CMake do a distclean?
For example, I create a build directory, and run cmake in there. Then I make a change to the CMakeLists.txt, and re-run CMake. It's not clear whether this causes my changes to be used, or whether CMake just takes the answers from the cache.
So I remove the build directory, re-create it, and then run cmake again.
Is there a way to accomplish this in CMake without deleting the build directory? Is there an equivalent target to the standard distclean target?
This is what the CMake pre-defined target ZERO_CHECK is for. It is a build system regeneration target. When you compile one of your pre-existing CMake projects, the ZERO_CHECK target should always run first to check to see if any of your CMake files have changed since the last build. If they have, CMake will re-run and regenerate the build system using your latest CMake files.
You just have to be careful with cached variables. These look something like this in the code, and typically appear in the CMake GUI display for easy editing:
set(MY_VAR "ExampleString" CACHE STRING "My string variable")
The documentation states:
Since cache entries are meant to provide user-settable values this does not overwrite existing cache entries by default. Use the FORCE option to overwrite existing entries.
Cached variables will retain their original value unless you explicitly change or unset them, by adding the FORCE attribute to the set() command above, using the unset() command, or deleting them from the cache. So, changing the string MY_VAR to something else in the code:
set(MY_VAR "OtherString" CACHE STRING "My string variable")
will not change the variable's value, because the original string is cached.
You can delete the variable from cache (or modify the cached value) from the CMake GUI window, or by manually modifying the CMakeCache.txt file in your CMake build folder.
If you're looking for a way to shortcut removing and re-creating the build directory, simply deleting the CMakeCache.txt file in the build folder should achieve the same effect.

Why CMake attempts to compile my trashed file?

I added, as error protection, the lines
if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} )
message( FATAL_ERROR "In-source builds not allowed." )
endif()
in front of my real stuff. For testing purposes, I cd .. and cmake . The protection works OK, those lines prevent in-source builds, and print the error message I expect. After that, I cd build and cmake ... Now I receive the same error message. Even, if I cd ..;rmdir build;mkdir build;cd build;cmake ... I noticed, that, despite the protection, the CMake generated files were written in my source directory. If I remove manually all those files, comment out the protection line, CMake want to put its files in the source, rather that in its build subdirectory.
In the console, I receive message
CMake Error: The current CMakeCache.txt directory /home/me/.local/share/Trash/files/build_3686731566269/CMakeCache.txt is different than the directory /home/me/DEVEL/cpp/sandbox/myprog/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
CMake Error: The source directory "/home/me/.local/share/Trash/files" does not appear to contain CMakeLists.txt.
which is correct, but I do not want to compile from Trash, and not the deleted files. I removed the old work directory, crated a new one, copied the source files to the new directory, rebooted. The error keeps standing. In addition, if I use the same CMakeLists file from Kdevelop, it works fine.
Finally I emptied Trash, recreated build, and now everytring works fine again.
What is going on here?

CLion: issue with Cmake configuration types

I understand that by default, Clion creates the binary files for a project loaded in Clion in all the four configurations:
(Debug;Release;MinSizeRel;RelWithDebInfo)
as well as one called: __default__.
I am using a third party cmake module which downloads an external project in a way that add_subdirectory() can be run on it so it would be included in the root project.
add_subdirectory(${downloaded_proj_src_dir} ${downloaded_proj_bin_dir} EXCLUDE_FROM_ALL)
In this setup, if I decide to place the child project outside the binary directory of the root project, I get:
Error:Binary directories outside of CMake build directory are not supported. Most likely this error is caused by an add_subdirectory command with an explicitly specified binary_dir argument.
which is an understandable restriction by CMake.
now if I instead decide to set the binary directory of the downloaded project in a subdirectory of the binary directory of the parent project, ie:
set(downloaded_proj_bin_dir "${CMAKE_BINARY_DIR}/${downloaded_proj}-build")
...
add_subdirectory(${downloaded_proj_src_dir} ${downloaded_proj_bin_dir} EXCLUDE_FROM_ALL)
I will get the file created in the parent binary directory of all the build configurations because ${CMAKE_BINARY_DIR} is different for each configuration. To avoid seeing all these directories listed on the project view sidebar, I have set the CMAKE_CONFIGURATION_TYPES to be Debug. But even then, I get:
Error:Configuration Debug
The current CMakeCache.txt directory /path/Debug/downloaded_proj_bin/CMakeCache.txt is different than the directory /path/__default__/downloaded_proj_bin/CMakeCache.txt where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
Clearly something is going on with this __default__ configuration which I don't understand. So the question is, what is the significance of this default configuration and why should there be a conflict here?
P.s. Setting the configuration to __default__ does not solve the problem as I will have a __default__0 configuration created instead and get the same error.
Update: some further observations
My enviornment variables set in IDE don't have any effect on the cmake builds.
Cmake "options" however which presumably will be passed as arguments to cmake do seem to work.
-D CMAKE_CONFIGURATION_TYPES=Debug.
When I specify the command line option, I get
Warning:Manually-specified variables were not used by the project:
CMAKE_CONFIGURATION_TYPES
But it clearly does have the effect of no longer creating the other build configurations. My guess is that this message relates to the __default__ build which is ignoring the argument.
Even in the case of specifying CMAKE_CONFIGURATION_TYPES in the IDE, I still get the additional __default__ build which is apparently unaffected by the CMAKE_CONFIGURATION_TYPES assignment.
Logging: message("Build type: ${CMAKE_BUILD_TYPE} ) does not return any build_type.
Outputting message(and generator: ${CMAKE_GENERATOR} ") returns "Unix-make files" for both, so both are being generated with the same generator.
Taking a diff from the CMakeCache.txt files, I can see that they are identical.
Do you have in DownloadProject.cmake the right setting? for:
set(_DownloadProjectDir "${CMAKE_CURRENT_LIST_DIR}")
I had the same problem trying to set google test(with the help of https://github.com/Crascit/DownloadProject) and my _DownloadProjectDir was setted as "test". Maybe when I moved this cmake file in my project Clion changed that automatically.
So, it turns out that you can sort this out quite easily by adding the following line above line 145 in DownloadProject.cmake:
file(REMOVE "${DL_ARGS_DOWNLOAD_DIR}/CMakeCache.txt")
This seems to be because CLion copies the default across to the other configurations and doesn't clear the cache. This is a problem only because DownloadProject creates a project within the project (I think...). Anyway, deleting this file before configuring the CMakeLists.txt by-passes this issue. I'll submit a pull request to the DownloadProject repository as this doesn't seem to have any adverse effects when not using CLion.

How do I fix an apparently corrupted CMake build?

I am getting the following error from CMakeSetup on our source tree:
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_FIND_LIBRARY_PREFIXES
Deleting the cache doesn't help, so something in one of the CMakeLists must be the problem. The weird part is, if I copy in a CMakeCache.txt from an old version of the tree, and edit it so that the paths match, CMake will then configure successfully... and, even after deleting that fixed cache, it continues to configure successfully.
Any idea what I should look for?
There are two variables missing from the bad CMakeCache.txt when it's generated: Project_BINARY_DIR and Project_SOURCE_DIR.
Is your Project declared at the top and in your base CMakeLists.txt file or at the very least this has to be declared before it is needed, of which at the top is easiest.
It appears this is a bug in cmake. http://www.mail-archive.com/cmake#cmake.org/msg13392.html
i.e.
PROJECT(inkscape)
SET(INKSCAPE_VERSION 0.46+devel)
SET(PROJECT_NAME inkscape)
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.6)
SET(CMAKE_INCLUDE_CURRENT_DIR TRUE)
...