0

I'm building DCMTK using the command line, using the following command:

cmake -DBUILD_APPS=BOOL:OFF ../latest_dcmtk

I want to move the BUILD_APPS setting from the command line into the top-level CMakeLists.txt configuration file. No matter how I attempt set the BUILD_APPS option in the top level configuration file, I never get the correct behavior unless I explicitly set the BUILD_APPS option from the command line.

Is there a way to explicitly set command line options from the top-level CMakeLists.txt file?

I've borrowed this from the DCMTK sources & it doesn't work:

SET (BUILD_APPS OFF CACHE BOOL "" FORCE)                                                             
MESSAGE ("-- BUILD_APPS is set to " ${BUILD_APPS})

When I look at the CMakeCache.txt file that's generated by the SET command above, I see the option set correctly:

./CMakeCache.txt:BUILD_APPS:BOOL=OFF

Unfortunately, the option doesn't work.

russes
  • 1,110
  • 11
  • 17
  • please provide a [mcve] – m.s. Oct 29 '15 at 17:57
  • Because DCMTK is a decent sized open-source project, I won't be able to strip the code down to a minimal example. At best, I could provide the top-level CMake configuration file. Even if I gave you that file, you'd need a Mac to build the equivalent project. – russes Oct 29 '15 at 18:19
  • the example just has to consist of a stripped down CMakeLists.txt file with that option which is not working – m.s. Oct 29 '15 at 18:42
  • 1
    Option set with command `option(BUILD_APPS "" OFF)` or `set(BUILD_APPS OFF CACHE BOOL "")` (FORCE is not needed) is equivalent for option set with `cmake` command line option `-DBUILD_APPS=BOOL:OFF`. If the second way works, but the first one doesn't, then `CMakeLists.txt` does something very special. – Tsyvarev Oct 29 '15 at 18:43
  • 2
    Just to be sure, I have given it a try again and the `-D` does force the Cache entry. So there should be no difference between using `-D` command line option and `set(... FORCE)`. I agree something is weird and suggest adding `variable_watch(BUILD_APPS)` at the top of the main `CMakeLists.txt` to find the codepart that is hiding/overwriting the variable. For more details see e.g. [here](http://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables). – Florian Oct 29 '15 at 21:25
  • Where do you set the variable? At the very beginning or towards the end? – usr1234567 Oct 29 '15 at 21:48

1 Answers1

0

Generally speaking, there are two things you have to keep in mind when using CMake:

  1. CMake is a script language, so all code is processed sequential
  2. The variable cache of CMake is there to persist values between two runs

So I agree with @Tsyvarev and @usr1234567 that this is most likely is an ordering issue:

  • You have to do the set(BUILD_APPS OFF CACHE BOOL "" FORCE) before you/the code does check for if (BUILD_APPS)
  • You have to check that there are no normal variable set(BUILD_APPS ...) afterwards in your main CMakeLists.txt hiding your cached variable
  • Whatever is in the CMakeCache.txt file is only the state of those variables after all processing of your code has finished (at the end of your main CMakeLists.txt)

I have given it a try again and the -D does force the Cache entry. So there should be no difference between using -D command line option and set(... CACHE ... FORCE). Just you can't use them in combination (the later will always overwrite any command line given option).

Please try adding variable_watch(BUILD_APPS) at the top of the main CMakeLists.txt to find the codepart that is hiding/overwriting the variable.

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149