Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check CMake Cache Variable in Toolchain File

Tags:

cmake

I'm having trouble setting a configuration variable via the command line. I can't determine it from the system, so I expect the user to specify:

cmake -DCMAKE_TOOLCHAIN_FILE=../android.toolchain -DANDROID_ABI:STRING="arm64" ..

Inside my android.toolchain, I have the following:

message(STATUS "Android ABI: ${ANDROID_ABI}")
if( "${ANDROID_ABI}" STREQUAL "" )
   message(FATAL_ERROR "Please specifiy ABI at cmake call -DANDROID_ABI:STRING=armeabi or -DANDROID_ABI:STRING=arm64")
endif()

No matter what, it fails at this line EVEN THOUGH it prints out the correct arm64:

 -- Android ABI: arm64
CMake Error at yaml-cpp/android.toolchain:45 (message):
Please specifiy ABI at cmake call -DANDROID_ABI:STRING=armeabi or -DANDROID_ABI:STRING=arm64

Could anyone direct me to what I'm doing wrong?


I think this has to do with:

  • -D adds a cache variable instead of a normal variable
  • This is in a toolchain file... it seems to ignore cache variables

Any thoughts or suggestions?

like image 908
Constantin Avatar asked Feb 19 '15 17:02

Constantin


People also ask

Where is CMake cache file?

CMake cache txt file. When you load a project for the first time, this file is generated in the build directory (cmake-build-debug or cmake-build-release by default) according to the contents of CMakeLists. txt. icon in the CMake Tool Window or locate the file in the project tree.

What are CMake cache variables?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.

Where are CMake variables stored?

Cache variables are stored in the CMake cache file, and are persisted across CMake runs. Both types can exist at the same time with the same name but different values.

How do I set CMake cache variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .


1 Answers

I don't pretend to fully understand what's going on behind the scenes, but here's a workaround that works for me:

# Problem: CMake runs toolchain files multiple times, but can't read cache variables on some runs.
# Workaround: On first run (in which cache variables are always accessible), set an intermediary environment variable.

if (FOO)
    # Environment variables are always preserved.
    set(ENV{_FOO} "${FOO}")
else ()
    set(FOO "$ENV{_FOO}")
endif ()
like image 128
sorbet Avatar answered Oct 15 '22 07:10

sorbet