Я пытаюсь обновить переменную cmake на основе значения переменной env, если она задана заранее. И я также кеширую эту переменную. Однако в cmake он не обновляется должным образом, но в кеше я вижу обновленное значение.
Вот мой тестовый файл CMakeLists.txt
Я установил переменную evirnoment
Код: Выделить всё
compiler_verion=clang15
Код: Выделить всё
cmake_minimum_required(VERSION 3.26)
project(test)
if(NOT compiler_version)
set(compiler_version "gcc")
message("++++++++++++++++++++++++++++++ default ${compiler_version}")
endif()
if(NOT "$ENV{compiler_version}" STREQUAL "")
set(compiler_version $ENV{compiler_version} CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++ env compiler_version : $ENV{compiler_version} cmake_compiler_version ${compiler_version} ++++++++++++++++++++++++++++")
elseif(NOT compiler_version)
set(compiler_version "gcc" CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++++++ else if ${compiler_version}")
else()
set(compiler_version ${compiler_version} CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++++++ else ${compiler_version}")
endif()
Код: Выделить всё
cmake ..
Вот фактический результат.
Код: Выделить всё
-- The C compiler identification is GNU 8.5.0
-- The CXX compiler identification is GNU 8.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
++++++++++++++++++++++++++++++ default gcc
++++++++++++++++++++++++++ env compiler_verion : clang15 cmake_compiler_version gcc ++++++++++++++++++++++++++++
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /test/buil
Код: Выделить всё
++++++++++++++++++++++++++ env compiler_version : clang15 cmake_compiler_version gcc
Код: Выделить всё
compiler_version дляclang15
Если я перезапущу это, когда файл кэша уже доступен, он будет работать как положено.
Код: Выделить всё
++++++++++++++++++++++++++ env compiler_verion : clang15 cmake_compiler_version clang15 ++++++++++++++++++++++++++++
-- Configuring done (0.0s)
-- Generating done (0.0s)
I am using cmake version 3.26 on linux platform.
Источник: https://stackoverflow.com/questions/781 ... e-variable