首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过工具链升级实现CMake增量编译

通过工具链升级实现CMake增量编译
EN

Stack Overflow用户
提问于 2021-03-25 22:11:24
回答 2查看 341关注 0票数 0

我正试图通过工具链升级找到一种使用CMake启用增量编译的方法。下面是一个有问题的场景:

使用CMAKE_CXX_COMPILER=g++-9)

  • A,新分支使用g++-10 (使用CMAKE_CXX_COMPILER=g++-10)

  • Commits在两个分支上发生branches

  • Incremental构建,一个分支工作fine

  • Switching到另一个分支,并显式调用CMake fails

f 219

我的问题如下:当工具链发生更改时,我正在寻找的适当方式,以使成功地调用CMake并从头开始重新构建整个项目。

这里有一个脚本,它将使它快速和容易地再现问题。这个脚本需要Docker。它将在执行文件系统的位置创建文件夹SourcesBuild,以避免丢弃文件系统。然后,它将创建Dockerfile,以便使用g++和cmake构建停靠容器。然后创建一个虚拟的Hello C++ CMake项目。最后,它为构建工件创建一个文件夹,然后使用g++-9和g++-10执行构建。第二个构建失败是因为CMake生成了一个错误。

代码语言:javascript
复制
#!/bin/bash
set -e

mkdir -p Sources
mkdir -p Build

# Creates a script that will be executed inside the docker container to perform builds
cat << EOF > Sources/Compile.sh
cd /Build \
    && cmake /Sources \
    && make \
    && ./IncrementalBuild
EOF

# Creates a Dockerfile that will be used to have both gcc-9 and cmake
cat << EOF > Sources/Dockerfile-gcc9
FROM gcc:9
RUN apt-get update && apt-get install -y cmake
RUN ln -s /usr/local/bin/g++ /usr/local/bin/g++-9
ADD Compile.sh /Compile.sh
RUN chmod +x /Compile.sh
ENTRYPOINT /Compile.sh
EOF

# Creates a Dockerfile that will be used to have both gcc-10 and cmake
cat << EOF > Sources/Dockerfile-gcc10
FROM gcc:10
RUN apt-get update && apt-get install -y cmake
RUN ln -s /usr/local/bin/g++ /usr/local/bin/g++-10
ADD Compile.sh /Compile.sh
RUN chmod +x /Compile.sh
ENTRYPOINT /Compile.sh
EOF

# Creates a dummy C++ program that will be compiled
cat << EOF > Sources/main.cpp
#include <iostream>
  
int main()
{
  std::cout << "Hello World!\n";
}
EOF

# Creates CMakeLists.txt that will be used to compile the dummy C++ program
cat << EOF > Sources/CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
  
project(IncrementalBuild CXX)
add_executable(IncrementalBuild main.cpp)
set_target_properties(IncrementalBuild PROPERTIES CXX_STANDARD 17)
EOF

# Build the docker images with both Dockerfiles created earlier
docker build -t cmake-gcc:9 -f Sources/Dockerfile-gcc9 Sources
docker build -t cmake-gcc:10 -f Sources/Dockerfile-gcc10 Sources

# Run a build with g++-9
echo ""
echo "### Compiling with g++-9 and then running the result..."
docker run --rm --user $(id -u):$(id -g) -v $(pwd)/Sources:/Sources -v $(pwd)/Build:/Build -e CXX=g++-9 cmake-gcc:9
echo ""

# Run a build with g++-10
echo "### Compiling with g++-10 and then running the result..."
docker run --rm --user $(id -u):$(id -g) -v $(pwd)/Sources:/Sources -v $(pwd)/Build:/Build -e CXX=g++-10 cmake-gcc:10
echo ""

# Print success if we reach this point
echo "SUCCESS!"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-26 00:02:42

--我正在寻找一种适当的方法,使CMake调用成功,并在工具链发生更改时从头开始重建所有项目。

正确的方法是使用一个新的二进制目录。要么在更改时删除二进制目录,然后让它重新创建,要么为每个工具链使用单独的不同目录。

Build/gcc10二进制目录用于gcc10生成,Build/gcc9用于gcc9生成。

不需要cd Buildmkdir与现在的cmake -使用cmake -S. -BBuild.也不要使用make -更喜欢的cmake --build Build让你以后切换生成器。

票数 0
EN

Stack Overflow用户

发布于 2021-03-26 01:04:16

“如果您更改了工具链,您应该从新构建开始。有太多的事情假设工具链不会改变,虽然您可能能够找到看似有效的解决方案,但我建议您始终为不同的工具链使用新的构建树。如果就地更新现有的工具链(例如,在Linux上更新GCC的更新版本,在macOS上更新Xcode,等等),同样的逻辑也适用。CMake查询编译器功能并缓存结果。如果您以CMake无法捕捉到的方式更改工具链,则最终会为新的/更新的工具链使用陈旧的缓存功能。别这样“-克雷格·斯科特

所以基本上我认为这是不可能的。你只需要吹掉你的身材。如果CMake没有为您做这件事,您可以做的最好的事情就是提醒用户。

也许对此也有答复:https://discourse.cmake.org/t/how-to-change-toolchain-without-breaking-developer-workflows/1166

或者开始另一个话题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66808292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档