底辺

ゑ?

CMakeで複数のテスト実行バイナリを一つのビルドターゲットとする

CMakeで複数のテスト実行バイナリを一つのターゲットとしてまとめる

→cmake --buildの--targetにいちいち全部書かなくて良い。

add_custom_target, add_dependenciesを使って出来る。

CMakeLists.txt

# それぞれのディレクトリでそれぞれのテスト実行バイナリを
# 生成するようになっているとする(add_executable)
add_subdirectory(xxx)
add_subdirectory(yyy)
add_subdirectory(zzz)

add_custom_target(hoge_tests
  COMMENT "Building all hoge tests executables..."
)
add_dependencies(hoge_tests xxx_test yyy_test zzz_test)


Makefile

# hoge_testsというターゲットにテストが全部含まれているので
# --targetにはhoge_testsを指定しておけばmake build-testsでテスト実行バイナリを全部ビルド出来る
build-tests:
    cmake -S . -B build.cpp && cmake --build build.cpp --target hoge_tests


テスト実行コマンド例

cd hoge && make clean && make build-tests && cd build.cpp && ctest