双环境构建的 CMake 设置
CMake Setup for Dual Environment Builds
目标
我正在使用 CMake 为我的项目构建它是一个基于 ARM Cortex 平台的嵌入式固件,使用 arm-none-eabi-gcc-6.3.1 编译器,使用 VSCode 编辑器,在 Windows 主机上.我正在尝试在我用于同一项目的 Windows 系统上进行第二次构建以进行测试。
第一个解题
我遇到的问题是,每当我需要将我的构建从生产切换到测试时,我必须删除构建文件并使用测试参数重新运行 CMake 命令,当我不这样做时,构建不会将 ARM 编译器 更改为我打算使用的编译器(我猜这是一个缓存问题)。我试过 make clean
和 make rebuild_cache
以为它可以清理缓存并为我解决问题,但没有用。
全新构建示例:
Project\build> cmake -G"MinGW Makefiles" -DTARGET_GROUP=test ..
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/MinGW/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/MinGW/bin/g++.exe
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: Path\to\Project\build
Project\build>make
(successful make)
第二个版本:
Project\build> cmake -G"MinGW Makefiles" -DTARGET_GROUP=production ..
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= Path/to/arm-gnu-toolchain/bin/arm-none-eabi-gcc.exe
CMAKE_CXX_COMPILER= Path/to/arm-gnu-toolchain/bin/arm-none-eabi-g++.exe
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: Path\to\Project\build
Project\build>make
(Cached make -did not change the compiler or the files to be built)
CMakeLists.txt:
set(TARGET_GROUP test CACHE STRING "Group to build")
if(TARGET_GROUP STREQUAL production)
# ARM Lib
include("arm-gnu.cmake")
else()
include("win-gcc-for-testing.cmake")
endif()
# include the files based on the TARGET_GROUP value
# ...
当前解决方案
目前,我有一个临时解决方案,即为每种建筑类型创建一个单独的文件夹,它们工作得很好。
问题
当前的解决方案(分别为不同的环境创建两个构建目录)在最佳实践方面是否正确?如果没有,还有什么更好的?
正如@Tsyvarev 所说,如果你想要不同的构建,你必须使用不同的目录。在这里,您试图在同一个 build
目录中构建两者。相反,创建一个子目录 test
来构建 -DTARGET_GROUP=test
,并创建一个不同的子目录 prod
来构建 -DTARGET_GROUP=production
:
Project> mkdir test
Project> cd test
Project\test> cmake -G"MinGW Makefiles" -DTARGET_GROUP=test ..
Project\test> cd ..
Project> mkdir prod
Project> cd prod
Project\prod> cmake -G"MinGW Makefiles" -DTARGET_GROUP=production ..
当然你可以随意调用目录。
由于您希望将其集成到 VSCode 中,因此请使用 CMake 构建工具包进行配置更改,这是在调用 CMake 之前设置编译器的内容。您可以指定其他用户工具包定义,并在 VSCode 界面中选择编译器工具包。
重写 CMake 文件,使其足以根据编译器的选择来调节其他所有内容。
然后,VSCode cmake-tools 扩展支持 variable expansion 作为构建目录的名称。选项包括 ${buildKit}
和 ${buildKitTargetArch}
。将此后缀作为构建目录设置的一部分(您可以选择是仅在您的工作区中设置它,还是在您的系统中更全局地设置它)。
现在,当您切换工具包时,CMake 将选择目录更改并愉快地存在于两个不同的构建目录中。
或者,cmake-tools现在提供了variants,可用于在正常的Release、Debug、RelWithDebInfo等基础上添加额外的Production和Test。还有额外的构建目录扩展变量,用于信息来自当前选择的变体。
目标
我正在使用 CMake 为我的项目构建它是一个基于 ARM Cortex 平台的嵌入式固件,使用 arm-none-eabi-gcc-6.3.1 编译器,使用 VSCode 编辑器,在 Windows 主机上.我正在尝试在我用于同一项目的 Windows 系统上进行第二次构建以进行测试。
第一个解题
我遇到的问题是,每当我需要将我的构建从生产切换到测试时,我必须删除构建文件并使用测试参数重新运行 CMake 命令,当我不这样做时,构建不会将 ARM 编译器 更改为我打算使用的编译器(我猜这是一个缓存问题)。我试过 make clean
和 make rebuild_cache
以为它可以清理缓存并为我解决问题,但没有用。
全新构建示例:
Project\build> cmake -G"MinGW Makefiles" -DTARGET_GROUP=test ..
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/MinGW/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/MinGW/bin/g++.exe
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: Path\to\Project\build
Project\build>make
(successful make)
第二个版本:
Project\build> cmake -G"MinGW Makefiles" -DTARGET_GROUP=production ..
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= Path/to/arm-gnu-toolchain/bin/arm-none-eabi-gcc.exe
CMAKE_CXX_COMPILER= Path/to/arm-gnu-toolchain/bin/arm-none-eabi-g++.exe
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: Path\to\Project\build
Project\build>make
(Cached make -did not change the compiler or the files to be built)
CMakeLists.txt:
set(TARGET_GROUP test CACHE STRING "Group to build")
if(TARGET_GROUP STREQUAL production)
# ARM Lib
include("arm-gnu.cmake")
else()
include("win-gcc-for-testing.cmake")
endif()
# include the files based on the TARGET_GROUP value
# ...
当前解决方案
目前,我有一个临时解决方案,即为每种建筑类型创建一个单独的文件夹,它们工作得很好。
问题
当前的解决方案(分别为不同的环境创建两个构建目录)在最佳实践方面是否正确?如果没有,还有什么更好的?
正如@Tsyvarev 所说,如果你想要不同的构建,你必须使用不同的目录。在这里,您试图在同一个 build
目录中构建两者。相反,创建一个子目录 test
来构建 -DTARGET_GROUP=test
,并创建一个不同的子目录 prod
来构建 -DTARGET_GROUP=production
:
Project> mkdir test
Project> cd test
Project\test> cmake -G"MinGW Makefiles" -DTARGET_GROUP=test ..
Project\test> cd ..
Project> mkdir prod
Project> cd prod
Project\prod> cmake -G"MinGW Makefiles" -DTARGET_GROUP=production ..
当然你可以随意调用目录。
由于您希望将其集成到 VSCode 中,因此请使用 CMake 构建工具包进行配置更改,这是在调用 CMake 之前设置编译器的内容。您可以指定其他用户工具包定义,并在 VSCode 界面中选择编译器工具包。
重写 CMake 文件,使其足以根据编译器的选择来调节其他所有内容。
然后,VSCode cmake-tools 扩展支持 variable expansion 作为构建目录的名称。选项包括 ${buildKit}
和 ${buildKitTargetArch}
。将此后缀作为构建目录设置的一部分(您可以选择是仅在您的工作区中设置它,还是在您的系统中更全局地设置它)。
现在,当您切换工具包时,CMake 将选择目录更改并愉快地存在于两个不同的构建目录中。
或者,cmake-tools现在提供了variants,可用于在正常的Release、Debug、RelWithDebInfo等基础上添加额外的Production和Test。还有额外的构建目录扩展变量,用于信息来自当前选择的变体。