Check CMake Cache Variable in Toolchain File

2019-01-24 11:15发布

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?

标签: cmake
1条回答
地球回转人心会变
2楼-- · 2019-01-24 11:45

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 accessable), set an intermediary environment variable.

if (FOO)
    # Environment variables are always preserved.
    set(ENV{_FOO} "${FOO}")
else ()
    set(FOO "$ENV{_FOO}")
endif ()
查看更多
登录 后发表回答