LoginSignup
0
1

More than 5 years have passed since last update.

[CMake] ソースファイルプロパティは同じディレクトリのターゲットにしか見えない

Posted at

Source file properties are visible only to targets added in the same directory (CMakeLists.txt).

なので、add_custom_commandで指定したOUTPUTを他のディレクトリのadd_custom_targetSOURCESに指定する事は出来ない。

例えば、次の様な構成のCMakeシステムがあったとする。

project
+-- CMakeLists.txt
|
+-- foo
| +-- CMakeLists.txt
|
+-- bar
  +-- CMakeLists.txt
project/CMakeLists.txt
add_subdirectory (foo)
add_subdirectory (bar)
project/foo/CMakeLists.txt
add_custom_command (OUTPUT a.txt COMMAND ...)
project/bar/CMakeLists.txt
add_custom_target (BAR_TARGET SOURCES ${PROJECT_BINARY_DIR}/foo/a.txt ...)

fooの中でなんらかのファイルを生成して、barでそのファイルを利用するという良くある構成だ。
add_custom_commandを用いる事で、a.txtを何度も生成しようとするのを防ぐ事ができる。
ところが、cmakeしてみると、

Cannot find source file:

    /project/build/foo/a.txt

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

等と怒られる。

cmakeadd_custom_commandによって生成されるまだ存在しないファイルを認識するが、これはadd_custom_commandを実行したディレクトリ内でしか有効では無い。
なので、barからはfoo/a.txtadd_custom_commandによって将来的に生成されるファイルだとは認識できず、また、"foo/a.txt"がまだ存在しないので、エラーを吐く。

対処方は簡単で、fooの中に、a.txtに依存するターゲットを作ってやれば良い。

project/foo/CMakeLists.txt
add_custom_command (OUTPUT a.txt COMMAND ...)
add_custom_target (FOO_GENERATE_ATXT SOURCES a.txt)
project/bar/CMakeLists.txt
add_custom_target (BAR_TARGET DEPENDS FOO_GENERATE_ATXT ...)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1