Building a library added with EXCLUDE_FROM_ALL requires a different binary directory to be specified when building, inconsistently across platforms
Using CMake 3.11.2 on Windows 10, 3.10.2 on Ubuntu 18.04.
I've created a simple example here, but basically I have this structure:
CMakeLists.txt
README.md
cmake_ex/CMakeLists.txt
cmake_ex/cmake_ex.c
modules/magic/CMakeLists.txt
modules/magic/magic.c
The subdirectories contain almost nothing, those are really just skeleton files with add_library(...)
calls in their CMakeLists.txt
. The top level CMakeLists.txt
contains:
cmake_minimum_required(VERSION 3.8)
project("cmake_ex")
add_subdirectory("cmake_ex")
add_subdirectory("modules/magic" EXCLUDE_FROM_ALL)
Here's what I want: a cross platform way to repeatably build the magic
module from the command line eg. for an automated build system. So I figured this would do it:
mkdir build
cd build
cmake ..
cmake --build . --target "magic"
I've omitted the -G
flag for brevity, it might be fine to leave it out altogether for my use case here. But on my Windows system it defaults to Visual Studio 15 2017
, and on Linux Unix Makefiles
.
This works on Linux, but not on Windows. On Linux it just invokes GNU Make with the magic
target and it builds. On Windows I get:
> cmake --build . --target "magic"
Microsoft (R) Build Engine version 15.6.82.30579 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1009: Project file does not exist.
Switch: magic.vcxproj
I actually have to do:
cmake --build "modules/magic" --target "magic"
Now, this works, and is fine, and it also works on Linux. So maybe that's the cross-platform way to build, and that's all there is to it and it's Not A Bug. But the things that make me think it might, possibly, be an issue are:
-
It works inconsistently across platforms. Maybe it's the case that the
cmake --build . --target "magic"
version is simply not guaranteed to work anywhere, so if it does that's a bonus, but it's confusing. The help text says the directory given is the "project binary directory to be built", but there's only one project ("cmake_ex"). -
It suggests an inconsistency in how those projects are generated. The makefiles project has a build target for
magic
in the top level and inmodules/magic
... but the VS Projects don't. -
The behaviour is entirely determined by the
EXCLUDE_FROM_ALL
flag, and nothing about its documentation suggests that this will change the semantics ofcmake --build ...
. -
I am seeing issues with shared libraries added with theEXCLUDE_FROM_ALL
flag not finding libraries added without that flag. (I will add a separate comment about this when I've narrowed down that particular problem.)