[CMake] Using the -C option to cmake

Ateljevich, Eli eli at water.ca.gov
Fri Jun 29 13:38:35 EDT 2012


The mechanics of this are not due to -C but scope issues. I would not advocate overstepping the "defined" behavior of -C, but learning the reasons why your function did not work was informative for me as a new user.

The -C option and <init-cache> act the way they do because of the way SET works and how early you are calling it, not because of additional magic in -C. Here is the behavior and some interesting addenda to what you can do with set:
1. You can do anything you want in the file, not just SET. It will work the way a normal cmake script does if it were called very early in a nearly empty scope. See discussion of your case below.
2. You can use "FORCE" and the SETs will work and change the cache even if it is not the first invocation
3. A -D initialization on the command line will trump the SET even if you FORCE
4. You are quite limited in what variables are already in scope, so hardwired or $ENV values (and maybe some internal CMake defs) are the only thing you can use to create derived values. Other project values are not defined yet.

Your issue was you wanted something to work in  <init-cache> that would not work in an ordinary CMakeLists.txt file. You defined your function one place and wanted it available in a different scope. In the context of CMakeLists.txt you seem to recognize that doesn't work for functions like it does for variables -- you need to INCLUDE functions in every file. At least that seems to be the default -- I'd love to hear how to have function def scope passed like variable scope.  If you define AND use the function in <init-cache> it works fine, that just isn't super useful.

Some of the capabilities I describe are "undefined behavior" of -C according to the docs and at some point the defined behavior could be expanded or the implementation contracted. If the powers that be ever decide to tighten up the rules and keep cmake syntax, I hope they will keep in mind the MESSAGE function works fine and is very useful in <init-cache>. 

However ... if they really want to tighten things up I hope they will please consider using the cache name-value syntax which is much easier on users, evokes the connection to the cache, and naturally conveys the limits on what you can do.

Regards,
Eli

________________________________________
From: cmake-bounces at cmake.org [cmake-bounces at cmake.org] On Behalf Of David Cole [david.cole at kitware.com]
Sent: Friday, June 29, 2012 6:27 AM
To: Christian Arnault
Cc: cmake at cmake.org
Subject: Re: [CMake] Using the -C option to cmake

The documentation for the CMake -C command line option says:

"When cmake is first run in an empty build tree, it creates a
CMakeCache.txt file and populates it with customizable settings for
the project. This option may be used to specify a file from which to
load cache entries before the first pass through the project's cmake
listfiles. The loaded entries take priority over the  project's default
values. The given file should be a CMake script containing SET
commands that use the CACHE option, not a cache-format file."

  http://cmake.org/cmake/help/v2.8.8/cmake.html#opt:-Cinitial-cache

The ONLY thing that should be in a -C file is a sequence of SET( ...
CACHE ... ) commands. Anything else is unexpected, but (unfortunately)
not strictly prohibited. We should have made it an error / mistake to
put anything BUT "set(CACHE" commands in -C files, but to do so now
would break backwards compatibility. We should probably do so now
anyhow and introduce a policy that says to make it an error to put
anything else into -C files.

So... avoid putting anything else in your Init.cmake file, and you should be ok.


HTH,
David


On Fri, Jun 29, 2012 at 8:44 AM, Christian Arnault <arnault at lal.in2p3.fr> wrote:
> Hi,
>
> I wanted to factor out some initial stuff of my CMakeLists.txt that I
> systematically include.
>
> typically I include the following lines at the beginning of all my
> CMakeLists.txt:
>
> cmake_minimum_required(VERSION 2.8)
> function (AA)
>   message ("calling AA")
> endfunction()
>
> AA()
>
> Then I thought by using the -C option as follows:
>
>> cmake --build=. -C %CMTROOT%\Init.cmake ..\CMakeLists.txt
>
> with Init.cmake file containing
>
> function(AA)
>   message("calling aaaa")
> endfunction()
>
> This would be equivalent.
>
> Apparently this is not the case. In particular, the function definitions are
> not transmitted by this mechanism
>
> loading initial cache file ...\Init.cmake
>
> ... the functions defined in Init.cmake are not recognized
>
> Of course I could 'manually' include my module in all CMakeLists.txt but ...
>
> Can someone explain why?
>
> Or, how could I obtain what I need?
>
>
>
> Thanks for any hint
>
> Regards
> Christian
>
> --
> --------------------------------------------
> | Christian Arnault                        |
> | LAL Bat 200 pièce 03a                    |
> | 91405 Orsay CEDEX                        |
> | phone   : (33) 1 64 46 84 24             |
> | gsm     : (33) 6 77 27 62 30             |
> | fax     : (33) 1 69 07 94 04             |
> | e-mail  : christian.arnault at lal.in2p3.fr |
> --------------------------------------------
>
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list