0

I have a file /home/me/.config/Kitware/CMakeCache.txt that contains some initial cache settings I'd like to use everywhere. I run cmake -C /home/me/.config/Kitware/CMakeCache.txt and it gives me a parse error on line 3 with this file:

set(CMAKE_CXX_FLAGS "-march=native -mtune=native" CACHE STRING "Flags used by the compiler during all build types.")

set(CMAKE_CXX_FLAGS_DEBUG "-Og -ggdb" CACHE STRING "Flags used by the compiler during debug builds.")

This is the error I get:

CMake Error: Parse error in cache file /home/me/.config/Kitware/CMakeCache.txt on line 3. Offending entry: set(CMAKE_CXX_FLAGS_DEBUG "-Og -ggdb" CACHE STRING "Flags used by the compiler during debug builds.")

But, if I change the line (adding =) to:

set(CMAKE_CXX_FLAGS_DEBUG "=-Og -ggdb" CACHE STRING "Flags used by the compiler during debug builds.")

it works fine, but then the resulting cache file in my project has the option set to =-Og -ggdb. In fact, the = sign can appear anywhere in the entire set statement. But it results in different problems no matter where you put it.

If I rename the file to /home/me/.config/Kitware/default.cmake (or anything else besides CMakeCache.txt it also works fine and doesn't require an = sign in the value for every option.

This is very weird behavior, and I don't understand it. What's going on here?

xaxxon
  • 19,189
  • 5
  • 50
  • 80
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • The line looks good. I know it's the obvious, but did you try to save the initial cache file without encoding? Sometimes the `-` is not actually a minus sign (when copied out of a browser). – Florian Sep 25 '17 at 07:29
  • @Florian - Well, I was using emacs. And the line wasn't copied out of a browser. I typed it by hand. And that also doesn't explain why the exact same file worked when I gave it a different name. But, I'll try copying my currently working `defaults.cmake` file to `CMakeCache.txt` again and see if I get the same error. – Omnifarious Sep 25 '17 at 07:33
  • Ah, I missed that one. Is your main `CMakeLists.txt` file in the same directory? Then the `CMakeCache.txt` file is loaded as you actual Cache file and the error is somewhat understandable (see also [here](https://stackoverflow.com/questions/30503631/cmake-in-which-order-are-files-parsed-cache-toolchain)). – Florian Sep 25 '17 at 07:36
  • @Florian - Yep, `cmake -C ~/.config/Kitware/CMakeCache.txt -D CMAKE_BUILD_TYPE=Release ..` and I get a whole ton of 'parse error' errors. `cmake -C ~/.config/Kitware/default.cmake -D CMAKE_BUILD_TYPE=Release ..` works fine and gives me exactly what I would expect. The two different files have identical sha256 hashes. – Omnifarious Sep 25 '17 at 07:36
  • @Florian - And those two commands were run in a fresh `Release` directory I created under my project root. And my project root only has a `CMakeLists.txt` file and some `.h` and `.cpp `files in it. Try it yourself. My project root is in `~/src/employer/project_name`. – Omnifarious Sep 25 '17 at 07:38
  • @Florian - My guess is that cmake used to decide what file parser to use based on the filename it was parsing, and they changed to a more nuanced approached but didn't fix everything, and so now there's something in there that says "Oh, CMakeCache.txt, those are supposed to have an `=` on every line so I can split them up into variable and value.". – Omnifarious Sep 25 '17 at 07:43
  • You are right: see [`cmake.cxx`](https://github.com/Kitware/CMake/blob/master/Source/cmake.cxx#L768): Anything in the command line that has the name `cmakelists.txt` or `cmakecache.txt` is taken as the path to those files. – Florian Sep 25 '17 at 07:48
  • @Florian - Looks like a bug I stumbled across. This weird "I'm going to parse it like a CMakeLists.txt file, except every line must contain an `=` sign." is clearly broken behavior. You should put your research into `cmake.cxx` into an answer so I can accept t. Though, it's bedtime now, so it won't be until morning. :-) – Omnifarious Sep 25 '17 at 07:54

1 Answers1

1

Turning my comments into an answer

See cmake.cxx: Anything in the command line that has the name cmakelists.txt or cmakecache.txt is taken as the path to those files.

So CMake is ignoring your -C option here and tries to load your CMakeCache.txt as an actual variable cache file. And those files have a different formatting/syntax of NAME:TYPE=VALUE.

You can consider this behavior a bug in CMake. Or you just avoid using CMakeCache.txt for your initial cache file name.

Florian
  • 39,996
  • 9
  • 133
  • 149