如何在生成之前在cmake中执行命令?

时间:2017-04-13 21:03:17

标签: c++ c makefile cmake

我希望CMake在开始生成过程之前运行命令。

因此我在* CMakeLists.txt`的最开头添加execute_process,但预期的提示只是零星的。

cmake_minimum_required(VERSION 3.5)

project(amba_ctrl_datalinkmsg)

execute_process(COMMAND @echo 'HUHU HUHU HUHU')
execute_process(COMMAND pwd)
execute_process(COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/msg -f Makefile)

include_directories(include)
add_library(
    ${PROJECT_NAME} STATIC
    "src/source.cpp"
)

任何想法?

为什么我要这样做:

我需要从一些简单的定义文件中生成一个Cpp类。

  1. 检查生成器的源或定义文件是否比生成的类文件
  2. 更新
  3. 根据需要构建生成器
  4. 生成源文件
  5. 构建静态库
  6. 点1到3我用一个简单的Makefile解决了。 4.使用CMakeList.txt完成,因为这个库是更大项目中的子项目。

    从上面输出 CMakeList.txt 而没有echo

    Process package 'amba_ctrl_datalinkmsg' with context:
    --------------------------------------------------------------------------------
     source_space => /home/.../src/amba_ctrl/amba_ctrl_datalinkmsg
      build_space => /home/.../build/amba_ctrl_datalinkmsg
    install_space => /home/.../install
       make_flags => -j8, -l8
      build_tests => False
    --------------------------------------------------------------------------------
    +++ Building 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make cmake_check_build_system' in '/home/.../build/amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' in '/home/.../build/amba_ctrl_datalinkmsg'
    make[2]: *** No rule to make target '/home/.../src/source.cpp', needed by 'CMakeFiles/amba_ctrl_datalinkmsg.dir/src/source.cpp.o'. Stop.
    CMakeFiles/Makefile2:99: recipe for target 'CMakeFiles/amba_ctrl_datalinkmsg.dir/all' failed
    make[1]: *** [CMakeFiles/amba_ctrl_datalinkmsg.dir/all] Error 2
    Makefile:127: recipe for target 'all' failed
    make: *** [all] Error 2
    

    如果正确调用了makefile,cmake可以找到source.cpp。但没有电话就没有源和标头存在......

    调用msgGen之前的树

    .
    ├── CMakeLists.txt
    ├── msg
    │   ├── def1.msg
    │   ├── def2.msg
    │   ├── def3.msg
    │   ├── Makefile
    │   └── msgGen.cpp
    ├── package.xml
    └── README.md
    

    调用msgGen后的树

    .
    ├── CMakeLists.txt
    ├── include
    │   └── source.h
    ├── msg
    │   ├── def1.msg
    │   ├── def2.msg
    │   ├── def3.msg
    │   ├── Makefile
    │   ├── msgGen
    │   └── msgGen.cpp
    ├── package.xml
    ├── README.md
    └── src
        └── source.cpp
    

    添加RESULT_VARIABLE

    alex:overlay_ws$ ament build --only-packages amba_ctrl_datalinkmsg
    # Topological order
     - amba_ctrl_datalinkmsg
    
    Process package 'amba_ctrl_datalinkmsg' with context:
    --------------------------------------------------------------------------------
     source_space => /home/.../src/amba_ctrl/amba_ctrl_datalinkmsg
      build_space => /home/.../build/amba_ctrl_datalinkmsg
    install_space => /home/.../install
       make_flags => -j8, -l8
      build_tests => False
    --------------------------------------------------------------------------------
    +++ Building 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make cmake_check_build_system' in '/home/.../build/amba_ctrl_datalinkmsg'
    make[1]: Entering directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    make gen
    make[2]: Entering directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    msgGen: build msgGen
    g++ msgGen.cpp -o msgGen --std=c++11 -D'GENTARGETCPP="../src/source.cpp"' -D'GENTARGETH="../include/source.h"'
    msgGen: run msgGen
    dlCpm.msg dlIomReceiver.msg dlIomSender.msg
    mkdir -p ../src/
    mkdir -p ../include/
    ./msgGen
    make[2]: Leaving directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    msgGen: ready
    make[1]: Leaving directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    -- Found ament_cmake: 0.0.0 (/home/alex/git/AMBA/Software/ros2_ws/install/share/ament_cmake/cmake)
    -- Using PYTHON_EXECUTABLE: /usr/bin/python3
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/.../build/amba_ctrl_datalinkmsg
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' in '/home/.../build/amba_ctrl_datalinkmsg'
    [ 50%] Building CXX object CMakeFiles/amba_ctrl_datalinkmsg.dir/src/source.cpp.o
    [100%] Linking CXX static library libamba_ctrl_datalinkmsg.a
    [100%] Built target amba_ctrl_datalinkmsg
    +++ Installing 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__install.sh && /usr/bin/make install' in '/home/.../build/amba_ctrl_datalinkmsg'
    [100%] Built target amba_ctrl_datalinkmsg
    Install the project...
    -- Install configuration: ""
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/environment/library_path.sh
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/package_run_dependencies/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/parent_prefix_path/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.bash
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.sh
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.zsh
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/packages/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsg.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_dependencies-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_include_directories-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_libraries-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsgConfig.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsgConfig-version.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/package.xml
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/register_amba_ctrl_datalinkmsg_implementation.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/get_amba_ctrl_datalinkmsg_typesupport.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/configure_amba_ctrl_datalinkmsg_library.cmake
    -- Up-to-date: /home/.../install/include/amba_ctrl_datalinkmsg
    -- Installing: /home/.../install/include/amba_ctrl_datalinkmsg/source.h
    -- Installing: /home/.../install/lib/libamba_ctrl_datalinkmsg.a
    -- [ament] Deploying: /home/.../install/local_setup.bash
    -- [ament] Deploying: /home/.../install/local_setup.sh
    -- [ament] Deploying: /home/.../install/local_setup.zsh
    -- [ament] Deploying: /home/.../install/setup.bash
    -- [ament] Deploying: /home/.../install/setup.sh
    -- [ament] Deploying: /home/.../install/setup.zsh
    -- [ament] Deploying: /home/.../install/_order_packages.py
    

    删除 src 包含 dir

    alex:overlay_ws$ ament build --only-packages amba_ctrl_datalinkmsg
    # Topological order
     - amba_ctrl_datalinkmsg
    
    Process package 'amba_ctrl_datalinkmsg' with context:
    --------------------------------------------------------------------------------
     source_space => /home/.../src/amba_ctrl/amba_ctrl_datalinkmsg
      build_space => /home/.../build/amba_ctrl_datalinkmsg
    install_space => /home/.../install
       make_flags => -j8, -l8
      build_tests => False
    --------------------------------------------------------------------------------
    +++ Building 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make cmake_check_build_system' in '/home/.../build/amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' in '/home/.../build/amba_ctrl_datalinkmsg'
    make[2]: *** No rule to make target '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/src/source.cpp', needed by 'CMakeFiles/amba_ctrl_datalinkmsg.dir/src/source.cpp.o'. Stop.
    CMakeFiles/Makefile2:99: recipe for target 'CMakeFiles/amba_ctrl_datalinkmsg.dir/all' failed
    make[1]: *** [CMakeFiles/amba_ctrl_datalinkmsg.dir/all] Error 2
    Makefile:127: recipe for target 'all' failed
    make: *** [all] Error 2
    
    <== Command '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' failed in '/home/.../build/amba_ctrl_datalinkmsg' with exit code '2'
    <== Command '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' failed in '/home/.../build/amba_ctrl_datalinkmsg' with exit code '2'
    

    更改为ERROR_VARIABLE

    alex:overlay_ws$ ament build --only-packages amba_ctrl_datalinkmsg
    # Topological order
     - amba_ctrl_datalinkmsg
    
    Process package 'amba_ctrl_datalinkmsg' with context:
    --------------------------------------------------------------------------------
     source_space => /home/.../src/amba_ctrl/amba_ctrl_datalinkmsg
      build_space => /home/.../build/amba_ctrl_datalinkmsg
    install_space => /home/.../install
       make_flags => -j8, -l8
      build_tests => False
    --------------------------------------------------------------------------------
    +++ Building 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make cmake_check_build_system' in '/home/.../build/amba_ctrl_datalinkmsg'
    make[1]: Entering directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    make gen
    make[2]: Entering directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    msgGen: build msgGen
    g++ msgGen.cpp -o msgGen --std=c++11 -D'GENTARGETCPP="../src/source.cpp"' -D'GENTARGETH="../include/source.h"'
    msgGen: run msgGen
    dlCpm.msg dlIomReceiver.msg dlIomSender.msg
    mkdir -p ../src/
    mkdir -p ../include/
    ./msgGen
    make[2]: Leaving directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    msgGen: ready
    make[1]: Leaving directory '/home/.../src/amba_ctrl/amba_ctrl_datalinkmsg/msg'
    -- Found ament_cmake: 0.0.0 (/home/alex/git/AMBA/Software/ros2_ws/install/share/ament_cmake/cmake)
    -- Using PYTHON_EXECUTABLE: /usr/bin/python3
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/.../build/amba_ctrl_datalinkmsg
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__build.sh && /usr/bin/make -j8 -l8' in '/home/.../build/amba_ctrl_datalinkmsg'
    [ 50%] Building CXX object CMakeFiles/amba_ctrl_datalinkmsg.dir/src/source.cpp.o
    [100%] Linking CXX static library libamba_ctrl_datalinkmsg.a
    [100%] Built target amba_ctrl_datalinkmsg
    +++ Installing 'amba_ctrl_datalinkmsg'
    ==> '. /home/.../build/amba_ctrl_datalinkmsg/cmake__install.sh && /usr/bin/make install' in '/home/.../build/amba_ctrl_datalinkmsg'
    [100%] Built target amba_ctrl_datalinkmsg
    Install the project...
    -- Install configuration: ""
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/environment/library_path.sh
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/package_run_dependencies/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/parent_prefix_path/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.bash
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.sh
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/local_setup.zsh
    -- Up-to-date: /home/.../install/share/ament_index/resource_index/packages/amba_ctrl_datalinkmsg
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsg.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_dependencies-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_include_directories-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/ament_cmake_export_libraries-extras.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsgConfig.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/amba_ctrl_datalinkmsgConfig-version.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/package.xml
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/register_amba_ctrl_datalinkmsg_implementation.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/get_amba_ctrl_datalinkmsg_typesupport.cmake
    -- Up-to-date: /home/.../install/share/amba_ctrl_datalinkmsg/cmake/configure_amba_ctrl_datalinkmsg_library.cmake
    -- Up-to-date: /home/.../install/include/amba_ctrl_datalinkmsg
    -- Installing: /home/.../install/include/amba_ctrl_datalinkmsg/source.h
    -- Installing: /home/.../install/lib/libamba_ctrl_datalinkmsg.a
    -- [ament] Deploying: /home/.../install/local_setup.bash
    -- [ament] Deploying: /home/.../install/local_setup.sh
    -- [ament] Deploying: /home/.../install/local_setup.zsh
    -- [ament] Deploying: /home/.../install/setup.bash
    -- [ament] Deploying: /home/.../install/setup.sh
    -- [ament] Deploying: /home/.../install/setup.zsh
    -- [ament] Deploying: /home/.../install/_order_packages.py
    alex:overlay_ws$ 
    

1 个答案:

答案 0 :(得分:0)

感谢@Tsyvarev和其他人。这是我选择的方式:

CMakeList.txt

cmake_minimum_required(VERSION 3.5)

project(datalinkmsg)

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/DatalinkMsg.cpp
                   COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/msg -f Makefile)

add_custom_target( generate
                   ALL make -C ${CMAKE_CURRENT_SOURCE_DIR}/msg -f Makefile
                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/DatalinkMsg.cpp
                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/msg/msgGen.cpp
                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/msg/*.msg )
...

使用相应的 msg / Makefile

运行
MSGDEFS = *.msg

TARGET = msgGen
SOURCE = msgGen.cpp

GENTARGETCPP = ../src/DatalinkMsg.cpp
GENTARGETH   = ../include/DatalinkMsg.h

all: $(GENTARGETCPP) $(GENTARGETH) $(MSGDEFS) $(SOURCE)

$(GENTARGETCPP): $(MSGDEFS) $(SOURCE)
    make gen

$(GENTARGETH): $(MSGDEFS) $(SOURCE)
    make gen

gen: $(TARGET) $(MSGDEFS) $(SOURCE)
    mkdir -p ../src/
    mkdir -p ../include/
    ./$<

$(TARGET): msgGen.cpp
    g++ $< -o $@ --std=c++11

clean:
    rm -f $(TARGET) $(GENTARGETCPP) $(GENTARGETH)