[cmake-developers] Ninja : $subdir/{test, install, package} and $subdir/all targets

Alexis Murzeau amubtdx at outlook.fr
Sun Oct 23 14:01:41 EDT 2016


Hi,

When generating ninja build from a `CMakeLists.txt` containing
`add_subdirectory(test test_bin)` (see also test_subdir.zip in
attachment), I get this warning :
```
CMake Warning (dev):
  Policy CMP0058 is not set: Ninja requires custom command byproducts
to be explicit.  Run "cmake --help-policy CMP0058" for policy details.
Use the cmake_policy command to set the policy and suppress this
warning.

  This project specifies custom command DEPENDS on files in the build
tree that are not specified as the OUTPUT or BYPRODUCTS of any
add_custom_command or add_custom_target:

   test_bin\all
[...]
```

This is because cmake generates a `test_bin\package` depending on
`test_bin\all` but the generated $subdir\all target is named
`test\all`.


When generating `$subdir\package` target, the prefix is the current
binary directory name [1]. So the target name becomes
`test_bin\package`.
Likewise, the `$subdir\package` target depends on `$subdir\all` where
$subdir is also the current binary directory name [2].
So `$subdir` is `test_bin` and cmake generates a target
`test_bin\package` depending on `test_bin\all`.

For the `$subdir\all` target, `$subdir` is the current source
directory name [3], that is `test` (and not `test_bin`). So the
`$subdir\all` target is named `test\all`.

So cmake detects that `test_bin\package` depends on `test_bin\all` but
there is no `test_bin\all` target (only `test\all`) and outputs the
earlier said warning.
The consequence in the OLD behavior of CMP0058 is to add a phony
target `test_bin\all`.
The effect of this is that `ninja test_bin\package` errors like this :
```
CMake Error at .../test_subdir/build-dir/test/cmake_install.cmake:31 (file):
  file INSTALL cannot find
  ".../test_subdir/build-dir/test/test_subdir.exe".
```

My though about this is that `$subdir\all` should use the current
binary directory and not the current source directory like
`$subdir\package` does. It wasn't detected before because in most
cases, the current source and binary dirs are the same relatively to
the root source and binary dirs respectively.

To fix the generation of $subdir\all using the current binary dir, I
propose a patch in attachment. This makes "ninja test\package"
correctly compile test_subdir.exe via the "test\all" target.

[1] https://gitlab.kitware.com/cmake/cmake/blob/v3.7.0-rc2/Source/cmNinjaUtilityTargetGenerator.cxx#L37
[2] https://gitlab.kitware.com/cmake/cmake/blob/v3.7.0-rc2/Source/cmGlobalNinjaGenerator.cxx#L978
[3] https://gitlab.kitware.com/cmake/cmake/blob/v3.7.0-rc2/Source/cmGlobalNinjaGenerator.cxx#L1084

Alexis Murzeau
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_subdir.zip
Type: application/zip
Size: 11586 bytes
Desc: test_subdir.zip
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20161023/79a389a2/attachment-0001.zip>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Ninja-Use-current-binary-dir-for-subdir-all.patch
Type: application/octet-stream
Size: 11174 bytes
Desc: 0001-Ninja-Use-current-binary-dir-for-subdir-all.patch
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20161023/79a389a2/attachment-0001.obj>


More information about the cmake-developers mailing list