[CMake] Dual use for single source file

christoph at ruediger.engineering christoph at ruediger.engineering
Tue Jan 16 09:15:35 EST 2018


Hi,

this solved the issue here as well.

I added a pull request for a documentation update according to the MAIN_DEPENDENCY: https://gitlab.kitware.com/cmake/cmake/merge_requests/1665

Regards
Christoph

rüdiger.engineering
Christoph Rüdiger
Düsseldorfer Str. 12
45145 Essen
Germany

phone: +49 201 458 478 58

Von: Hendrik Sattler <post at hendrik-sattler.de>
Datum: Montag, 15. Januar 2018 um 14:47
An: "cmake at cmake.org" <cmake at cmake.org>, "christoph at ruediger.engineering" <christoph at ruediger.engineering>, "cmake at cmake.org" <cmake at cmake.org>
Betreff: Re: [CMake] Dual use for single source file

Hi,

I also fell into this trap before:
Don't use MAIN_DEPENDENCY but DEPENDS.

HS


Am 15. Januar 2018 12:19:37 MEZ schrieb "christoph at ruediger.engineering<mailto:christoph at ruediger.engineering>" <christoph at ruediger.engineering<mailto:christoph at ruediger.engineering>>:
Hi folks,

we’re using a clang-based tool for co-processing a bunch of our source
files. The benefit is, that it understands the compile_commands.json
database and hence we do not need to pull out the include paths and the
compile definitions by hand. However, CMake does not compile the source
file any longer when used as input to a custom command.

Here is the simplest project I could come up with to demonstrate the
behavior:

--- snip ---
# CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(hello-world)

add_executable(hello main.cxx hello.cxx)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reflections.h
COMMAND echo "// This is a tool which creates type reflections"
reflections.h
  MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/hello.cxx
  COMMENT "Generating reflections"
)
--- snip ---

--- snip ---
// main.cxx
void print_hello();

int main() {
  print_hello();
  return 0;
}
--- snip ---

--- snip ---
// hello.cxx
#include <iostream>

void print_hello() {
  std::cout << "Hello world!\n";
}
--- snip ---

What happens is, that CMake does not compile hello.cxx any longer. It
only executes the custom command. Consequently, the
compile_commands.json database does not contain any information about
hello.cxx. Tested on cmake 3.9.1 and 3.10.1.

The reason for this behavior seems to be, that there can be only one
kind for each given source file. In
cmGeneratorTarget::ComputeKindedSources, there is an explicit check for
not adding a source file multiple times to the KindedSources vector.
And when checking which kind a source file is of, the first check is
for custom commands. CMake says “Bingo, this is a custom command” and
does not check whether it can be of any other kind as well.

Is this a keep-it-simple implementation or is there any deeper
knowledge behind this behavior?

And more important: how can I force CMake to compile a source file and
process it by a custom command?

Thanks for your help,
Christoph

--
rüdiger.engineering
Christoph Rüdiger
Düsseldorfer Str. 12
45145 Essen
Germany

phone: +49 201 458 478 58

--
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180116/3c2f5501/attachment-0001.html>


More information about the CMake mailing list