kaushal kaushal - 2 months ago 13
Linux Question

CMAKE: Unique variable names ignored

I've a cross platform project running on multiple platforms. There is a CmakeLists.txt in every directory in the project and most cmake files have code similar to:

Input:

# set target os
if (DEFINED target_os)
SET (HRAD_SDK_TARGET_OS "${target_os}")
else ()
SET (HRAD_SDK_TARGET_OS "${CMAKE_SYSTEM_NAME}")
endif ()
message(STATUS "=============== SDK: ccccccccccccccccccccccccccccccccccccccccccccccc==============")
message(STATUS "target_os: ${target_os}")
message(STATUS "HRAD_SDK_TARGET_OS: ${HRAD_SDK_TARGET_OS}")

# set target bit
if ("${HRAD_SDK_TARGET_OS}" MATCHES "Linux")
message(STATUS "=============== SDK: ddddddddddddddddddddddddddddddddddddddddddddddd==============")
message(STATUS "target_os: ${target_os}")
message(STATUS "HRAD_SDK_TARGET_OS: ${HRAD_SDK_TARGET_OS}")

if ("${target_arch_bit}" MATCHES "64")
SET (TARGET_ARCH_BIT -m64)
else ()
SET (TARGET_ARCH_BIT -m32)
endif ()
endif ()
message(STATUS "TARGET_ARCH_BIT: ${TARGET_ARCH_BIT}")
# set flags
SET (ADDITION_C_FLAGS "-Wno-unused -Wno-parentheses")
if ("${HRAD_SDK_TARGET_OS}" MATCHES "LinuxARM")
message(STATUS "=============== SDK: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==============")
message(STATUS "target_os: ${target_os}")
message(STATUS "HRAD_SDK_TARGET_OS: ${HRAD_SDK_TARGET_OS}")


In the above sample,
target_os
can have values such as
Linux, LinuxARM, MacOSX, QNX
etc.

However, for some reason it doesn't like the name
LinuxARM
, it seems to treat it as
Linux
.

In the code above even if
target_os
is
LinuxARM
, somehow the loop with
Linux
check is true and I get output as below and compilation fails -

Output:

-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- =============== SDK: ccccccccccccccccccccccccccccccccccccccccccccccc==============
-- target_os: LinuxARM
-- HRAD_SDK_TARGET_OS: LinuxARM
-- =============== SDK: ddddddddddddddddddddddddddddddddddddddddddddddd==============
-- target_os: LinuxARM
-- HRAD_SDK_TARGET_OS: LinuxARM
-- TARGET_ARCH_BIT: -m32
-- =============== SDK: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==============
-- target_os: LinuxARM
-- HRAD_SDK_TARGET_OS: LinuxARM
-- =============== utils: CMakeLists.txt ==============
-- target os: LinuxARM
-- TARGET_OS:
-- CMAKE_C_FLAGS: -fPIC -Wno-unused -Wno-parentheses -D_REENTRANT
-- CMAKE_CPP_FLAGS: -fPIC -Wno-unused -Wno-parentheses -std=gnu++0x -D_REENTRANT
-- CMAKE_CXX_FLAGS: -fPIC -Wno-unused -Wno-parentheses -std=gnu++0x -D_REENTRANT
-- CMAKE_LD_FLAGS: -Wl -rpath-link,/usr/arm-linux-gnueabihf/lib
-- CMAKE_C_COMPILER: /usr/bin/arm-linux-gnueabihf-gcc
-- CMAKE_CPP_COMPILER: /usr/bin/arm-linux-gnueabihf-g++
-- CMAKE_CXX_COMPILER: /usr/bin/arm-linux-gnueabihf-g++


As you can see it shouldn't have gone in the block containing the line
ddddddddddddddddddddddddd


If I rename
LinuxARM
to so some other name such as
LINUX_ARM
or totally random such as
NEWHLOS
then the code compiles fine.

Seems like since
Linux
is a subset of
LinuxARM
, it just treats it as
Linux
.

Can someone help why it doesn't like
LinuxARM
? I read that cmake variable names are case sensitive, but in my cases the variables are completely unique.

Also, I deleted all the cmake cache files and deleted all previous build files but issue persists.

Update 1:

LINUX_ARM
seems to have no issue with
Linux
. It works fine.

Answer

The MATCHES subcommand of if() does a regular expression match, as documented. Of course the text LinuxARM matches the regex Linux. What you want is STREQUAL, not MATCHES:

if ("${HRAD_SDK_TARGET_OS}" STREQUAL "Linux")