Add custom command to target create in top level CMakeLists.txt file
I do have a project with a top level CMakeLists.txt
file and a sub project. In the sub project I want to add a custom command to a target created in the top level CMakeLists.txt
file. This will result in the following warning:
CMake Warning (dev) at sub_project/CMakeLists.txt:5 (add_custom_command):
Policy CMP0040 is not set: The target in the TARGET signature of
add_custom_command() must exist. Run "cmake --help-policy CMP0040" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
TARGET 'run_tests' was not created in this directory.
This warning is for project developers. Use -Wno-dev to suppress it.
This warning is wrong / missleading as I check, whether the target exists. And why do I get a warning at all? Is this use case of add_custom_command
not supported?
I tried using a cmake trunk build and version 3.5.1 on Ubuntu 16.04.
The project structure is as follows:
├── CMakeLists.txt
└── sub_project
└── CMakeLists.txt
Toplevel CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project(Project)
add_custom_target(run_tests)
add_subdirectory(sub_project)
Sub project CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project(SubProject)
if (TARGET run_tests)
add_custom_command(TARGET run_tests COMMAND "ls")
endif()