install() rules from add_subdirectory() executed in wrong order
If CMakeLists.txt
has top-level install()
rules, and calls add_subdirectory()
which itself has install()
rules, the subdirectory rules always get run after the top-level rules, even if the subdirectory rules were declared before the top-level rules.
Minimal example to reproduce the issue:
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.0)
project(test NONE)
add_subdirectory(sub)
install(CODE "message(STATUS second)")
sub/CMakeLists.txt
:
install(CODE "message(STATUS first)")
Commands:
cmake ..
make DESTDIR=$(pwd)/install install
Expected output:
Install the project...
-- Install configuration: ""
-- first
-- second
Actual output:
Install the project...
-- Install configuration: ""
-- second
-- first