[CMake] Dual use for single source file

christoph at ruediger.engineering christoph at ruediger.engineering
Mon Jan 15 06:19:37 EST 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180115/9643ce72/attachment.html>


More information about the CMake mailing list