6

I am using CMake GUI (no version) with CMake 3.6.1. I am using an external module with add_subdirectory that shows me some warnings that I do not like (because of the annoying pollution):

CMake Warning (dev) at D:/Sources/.../external/g3log/latest/Build.cmake:11 (IF):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  D:/Sources/.../external/g3log/latest/CMakeLists.txt:72 (INCLUDE)
This warning is for project developers.  Use -Wno-dev to suppress it.

I want to hide these warnings without touching the external files. -Wno-dev would be ok if it will affect only the external module (g3log).

I tried using cmake_policy like following with no effect:

cmake_policy(PUSH)
cmake_policy(SET CMP0054 OLD)
add_subdirectory(${g3log_DIR} ${CMAKE_BINARY_DIR}/../g3log)
cmake_policy(POP)
jww
  • 97,681
  • 90
  • 411
  • 885
Liviu
  • 1,859
  • 2
  • 22
  • 48
  • Related: http://stackoverflow.com/questions/29123444/what-is-the-scope-of-cmake-policies – Liviu Jan 17 '17 at 11:32
  • That sounds like your external module does have a `project()` command. This resets the policies for this sub-module and below. You can try setting [`CMAKE_POLICY_DEFAULT_CMP0054`](https://cmake.org/cmake/help/latest/variable/CMAKE_POLICY_DEFAULT_CMPNNNN.html) instead of calling `cmake_policy()`. – Florian Jan 17 '17 at 13:37
  • There is a `project (g3log)` indeed. So you are saying is impossible :( ? If you want, you can formulate an answer. – Liviu Jan 17 '17 at 14:25
  • No. I tried to say `cmake_policy(SET CMP0054 ...)` will not work, but `CMAKE_POLICY_DEFAULT_CMP0054` should. Just haven't setup a test to verify it. – Florian Jan 17 '17 at 14:35
  • Forgive my ignorance, but how? `set(CMAKE_POLICY_DEFAULT_CMP0054, OLD)` ? – Liviu Jan 17 '17 at 14:53
  • Yes, just without the comma. I've tested it and added an answer accordingly. – Florian Jan 17 '17 at 20:25

1 Answers1

4

Turning my comments into an answer

That sounds like your external module does have a project() command. This resets the policies for this sub-module and below.

To demonstrate a possible solution, lets say you have a external project as the following:

g3log/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(g3log NONE)

set(VAR1 "Hello World")
set(VAR2 "VAR1")
if ("${VAR2}" STREQUAL "${VAR1}")
    message("CMP0054 old behavior")
endif()

You can now set CMAKE_POLICY_DEFAULT_CMP0054 to OLD (or even better to NEW; nobody really wanted the "OLD" behavior) to get rid of the "Policy CMP0054 is not set" warnings you will get with newer versions of CMake:

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(PolicyOverwrite NONE)

set(CMAKE_POLICY_DEFAULT_CMP0054 NEW)
add_subdirectory(g3log)

Now you have set a default for policy CMP0054 to be used if none is explicitly given in your project or one of the external projects you are using.

Florian
  • 39,996
  • 9
  • 133
  • 149
  • "nobody really wanted the "OLD" behavior" - nobody except this module, g3log, isn't it? `set(CMAKE_POLICY_DEFAULT_CMP0054 NEW)` before `add_subdirectory(g3log)` would change this module's behaviour, isn't it? So the better answer would be: `set(CMAKE_POLICY_DEFAULT_CMP0054 OLD) add_subdirectory(${SOURCE_BASE_EXTERNAL}/corealpi/platform/cmake/core ${CMAKE_BINARY_DIR}/corealpi/core) set(CMAKE_POLICY_DEFAULT_CMP0054 NEW)` – Liviu Jan 18 '17 at 07:37
  • @Liviu Could you please add/post the g3log CMake script lines in question? I've never came across a case where someone was expecting that something I put in quotes does evaluate again for a variable name (see also [here](http://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables)). So e.g. that `if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")` is false, even CMAKE_CXX_COMPILER_ID does contain `MSVC`? – Florian Jan 18 '17 at 08:18
  • `IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang")` Also [github](https://github.com/KjellKod/g3log/blob/master/Build.cmake), line 23 . I periodically understand and then forget this peculiar syntax. – Liviu Jan 18 '17 at 08:32
  • @Liviu It's a RegEx for everything that ends with `Clang`. It wants to match against a string, so definitely `NEW`. I don't think they were aware of this bug in older versions of CMake (that `"${CMAKE_CXX_COMPILER_ID}"` could evaluate to `1`). – Florian Jan 18 '17 at 11:47
  • When I say strange syntax, I do not talk about the regex, but about CMake and quotes. Thank you! I will accept the anwser in a few days, to be sure. – Liviu Jan 18 '17 at 12:53