CMake Policies Design Discussion

From KitwarePublic
Jump to navigationJump to search

This page describes a proposal for a formal backwards/forwards compatibility feature.

Motivating Examples


Consider code such as

 add_definitions("-DFOO=\\\"hello\\ world\\\"")

which tries to add the option

 -DFOO=\"hello\ world\"

to the compile command line. The code works in CMake 2.4's Unix Makefiles generator and produces a definition as if

 #define FOO "hello world"

appeared in the source code. It works only because of the way the makefile generator happens to place the definition string in the command line. It may not work with the VS IDE generators. In CMake HEAD we provide the COMPILE_DEFINITIONS directory property so that one may write


to get the correct behavior on all generators. Since CMake HEAD contains the appropriate escaping support it is desirable to allow the user to write

 add_definitions("-DFOO=\"hello world\"")

and get the expected behavior. Unfortunately if we were to start escaping the definitions given to add_definitions we would break compatibility with projects that are already adding their own escapes. In hindsight we should have either supported escapes from the beginning or make the command give an error that the definition value is not supported, but it is too late now. A similar problem appears in the add_custom_command command where support for properly escaping all arguments was added late. The solution currently used by that command is to require the user to add a VERBATIM argument to the command to get proper escaping. Using that solution for add_definitions would make the user write

 add_definitions(VERBATIM "-DFOO=\"hello world\"")

just to get CMake to do the right thing. For compatibility CMake would have to implement the wrong behavior by default forever.

Missing Link Directories

Projects used to be able to write this (wrong) code and it would work by accident:

  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/ B)

where "B" is meant to link "/path/to/". This code is incorrect because it asks CMake to link to B but does not provide the proper linker search path for it. It used to work by accident because the -L/path/to would get added as part of the implementation of linking to A. The correct code would be

  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/ B)

or even better

  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/ /path/to/

Currently we provide the CMAKE_OLD_LINK_PATHS variable to allow projects or users to quickly work around the problem. Full compatibility would require us to support thie behavior by default forever. That would allow new projects to be written with the same bug.

An alternative is to require all libraries to be linked via full path (where target names are expanded automatically). Whenever a non-full-path is given we could produce a warning that tells the user to start using find_library or something like that but then implement the old linker search path computation for compatibility. It is desirable to let projects that have been updated for newer CMake versions tell CMake that they know what they are doing and to not warn or use the compatibility behavior.

Proposed Solution

We propose the following solution to this problem.

Each change that introduces a compatibility issue is assigned a new identification number (like CM00001 or something). Then we try to detect cases in user code where it might be an issue and deal with it. We can maintain in the implementation of CMake a mapping from feature id to a rule to deal with the issue when encountered. The possible means of dealing with such cases are:

Rule Behavior Meaning Default?
QUIET old Project suppresses the diagnostic only in special cases
WARN old Emit warning when case is encountered most cases
ERROR none Emit error when case is encountered only for cases that can be detected with no false positives
FIXED new Project declares the case has been resolved distant future release after creation of issue


 cmake_feature(SET CM00001 FIXED)
 cmake_feature(VERSION 2.6)