如何使用外部c ++库加速c ++项目的docker映像构建?

时间:2019-04-11 08:14:11

标签: docker cmake docker-compose

简化的项目结构:

orka_engine
├── orka_engine_preprocessing
│   ├── CMakeLists.txt
│   └── src
│       └── main.cc
├── super_build
│    └── CMakeList.txt
└── lib
     └── // external checked out libs

其中super_build/CMakeList.txt将项目中的每个外部库和每个子目录都列为具有硬编码路径的外部项目。

现在构建项目的方法是

mkdir build
cd build
cmake ../super_build
make

这很好用。

当我尝试将这个项目泊坞化时,问题开始了。 cmake for super_build拒绝在没有源代码的情况下运行,这导致它在每次更新源代码时重新编译外部库。

Dockerfile中的相关行

COPY ./orka_engine ./orka_engine

RUN mkdir ${BUILD_DIR} && \
    cd ${BUILD_DIR} && \
    cmake ../orka_engine/${CMAKE_TARGET} && \
    make -j 5

我想在复制项目源之前构建外部库,以减少第一次构建后的docker构建时间。

我很乐意实现这一目标。

1 个答案:

答案 0 :(得分:0)

在这种情况下,请在您的super_build cmake中执行另一个CMakeLists.txt,其中包含要在配置时构建的外部项目。

SuperBuild CMakeLists.txt

project(SuperBuild LANGUAGES NONE)

message(STATUS "Building: DEP_A...")

# Download, build, install DEP_A at configure time
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/dep-a.CMakeLists.txt
    ${CMAKE_BINARY_DIR}/dep-a/CMakeLists.txt)

execute_process(COMMAND ${CMAKE_COMMAND} -H. -Bproject_build -G "${CMAKE_GENERATOR}"
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/dep-a)
if(result)
  message(FATAL_ERROR "CMake step for dep-a failed: ${result}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build project_build --config ${CMAKE_BUILD_TYPE}
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/dep-a)
if(result)
  message(FATAL_ERROR "Build step for dep-a failed: ${result}")
endif()

和dep-a.CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(dep-a-submodule NONE)

include(ExternalProject)

ExternalProject_Add(dep-a_project
  TMP_DIR "${CMAKE_BINARY_DIR}/dep-a/tmp"
  STAMP_DIR "${CMAKE_BINARY_DIR}/dep-a/stamp"
  DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/dep-a/download"
  SOURCE_DIR "${CMAKE_BINARY_DIR}/dep-a/source"
  BINARY_DIR "${CMAKE_BINARY_DIR}/dep-a/build"

  GIT_REPOSITORY https://github.com/dep-a/dep-a
  GIT_TAG "master"

  # UPDATE_COMMAND ""
  # PATCH_COMMAND git apply "${CMAKE_SOURCE_DIR}/patches/dep-a.patch"
  # CONFIGURE_COMMAND ""
  CMAKE_ARGS
    -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dependencies
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DBUILD_TESTING=OFF
  # BUILD_COMMAND ""
  # INSTALL_COMMAND ""
  TEST_COMMAND ""
)

看看https://github.com/Mizux/cmake-abseil/blob/install/README.md