2021年3月25日星期四

CMake incremental compilation through toolchain upgrade

I am trying to find a way to enable incremental compilation with CMake through a toolchain upgrade. Here is the problematic scenario :

  • Branch main uses g++-9 (using CMAKE_CXX_COMPILER=g++-9)
  • A new branch uses g++-10 (using CMAKE_CXX_COMPILER=g++-10)
  • Commits are happening on both branches
  • Incremental builds on one branch work fine
  • Switching to the other branch and explicitly invoking CMake fails

My question is the following : I'm looking for the proper way to make the invocation of CMake succeed and rebuild all the project from scratch when a toolchain change happens.

Here is a script that will make it quick and easy to reproduce the problem. This script requires Docker. It will create folders Sources and Build at the location where it is executed to avoid littering your filesystem. It then creates Dockerfiles to build docker containers with both g++ and cmake. It then creates a dummy Hello World C++ CMake project. Finally, it creates a folder for build artifacts and then executes the build with g++-9 and then g++-10. The second build fails because CMake generates an error.

#!/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!"  
https://stackoverflow.com/questions/66808292/cmake-incremental-compilation-through-toolchain-upgrade March 26, 2021 at 06:11AM

没有评论:

发表评论