I have to generate sources by external tool (I generate c++ classes from IDL-files for fastDDS messages), this tool also generate CMakeLists.txt file that allows me to compile generated files to <msgs_lib>.a file.
In my big superproject for one exec-target I wonna check existence of generated files and their building result. if <msgs_lib>.a does not exist I wonna call generation and building it.
I know about CMake commands add_custom_command
and add_custom_target
. But I can't use them in proper way, in some cases generation calls even if all files exists and they was built in proper order, in some cases nothing generates (and after cleaning nothing generates too), in some cases exec-target does not understand that it should check and call generation.
For example I'll write above one case for starting the discussion. I have simplify it a lot:
Project on github: https://github.com/gladijos/test_project
Project in one zip-file: mediaDrive-link
Or right here:
project directory tree:
.
├── CMakeLists.txt
├── deps
│ ├── gen_deps
│ └── gen_source
│ └── tb_msgs
│ ├── CMakeLists.txt
│ ├── tb_msg.cpp
│ └── tb_msg.h
├── scripts
│ └── build_msgs.sh
└── test_app
├── CMakeLists.txt
└── main.cpp
root-CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(test_project LANGUAGES CXX VERSION 0.0.3)
set(TB_MSGS ${CMAKE_HOME_DIRECTORY}/deps/gen_deps/tb_msgs/build/libtb_msgs_lib.a)
add_custom_command(
OUTPUT ${TB_MSGS}
WORKING_DIR ${CMAKE_HOME_DIRECTORY}
COMMAND ${CMAKE_HOME_DIRECTORY}/scripts/build_msgs.sh ${CMAKE_HOME_DIRECTORY}
)
add_custom_target(build_dds_msgs
# ALL
DEPENDS ${TB_MSGS}
)
# apps
add_subdirectory(test_app)
add_dependencies(test_app build_dds_msgs)
tb_msgs CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.1)
project("tb_lib")
set(CMAKE_CXX_STANDARD 11)
add_library(tb_msgs_lib
tb_msg.cpp
)
tb_msg.cpp:
#include "tb_msg.h"
MyClass::MyClass(){};
tb_msg.h:
#pragma once
class MyClass
{
public:
MyClass();
};
build_msgs.sh:
cp -rf $1/deps/gen_source/* $1/deps/gen_deps/;
cd $1/deps/gen_deps/tb_msgs;
mkdir -p $1/deps/gen_deps/tb_msgs/build;
cd $1/deps/gen_deps/tb_msgs/build;
cmake .. ;
make;
test_app CMakeListst.txt:
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(test_app LANGUAGES CXX VERSION 0.0.1)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME}
${CMAKE_HOME_DIRECTORY}/deps/gen_deps/tb_msgs/build/libtb_msgs_lib.a
)
test_app main.cpp:
#include "../deps/gen_deps/tb_msgs/tb_msg.h"
int main()
{
MyClass myClass;
return 0;
}
In this case I got call to scripts/build_msgs.sh every build , even if libtb_msgs_lib.a
exists, and was built before test_app
.
More one. In add_custom_command
OUTPUT
section I put .a
, but in best case here should be all files in dir gen_source/* but i think it another problem and I can bit it after resolving main problem.