Not understanding why an if statement isn't working

message("LINUX: " ${LINUX}) 
message("APPLE: " ${APPLE})
message("CMAKE_SYSTEM_NAME: " ${CMAKE_SYSTEM_NAME})
if (LINUX OR (APPLE AND ${CMAKE_OSX_ARCHITECTURES} EQUAL "x86_64"))
message("Setting AVX compile options")
  :
endif()

On macOS and building with CMAKE_OSX_ARCHITECTURES set to x86_64 I get:

1> [CMake] CMAKE_OSX_ARCHITECTURES: x86_64
1> [CMake] LINUX: 
1> [CMake] APPLE: 1
1> [CMake] CMAKE_SYSTEM_NAME: Darwin

Note that the message “Setting AVX compile options” wasn’t issued???

I can’t see why not though. Am I being stupid?
D.

To compare strings, use operator STREQUAL.

I tried

message("CMAKE_OSX_ARCHITECTURES: " ${CMAKE_OSX_ARCHITECTURES})
message("LINUX: " ${LINUX}) 
message("APPLE: " ${APPLE})
message("CMAKE_SYSTEM_NAME: " ${CMAKE_SYSTEM_NAME})
if (LINUX OR (APPLE AND (${CMAKE_OSX_ARCHITECTURES} STREQUAL "x86_64"))
message("Setting AVX compile options")

on LINUX and got:

1> [CMake] CMAKE_OSX_ARCHITECTURES: 
1> [CMake] LINUX: 1
1> [CMake] APPLE: 
1> [CMake] CMAKE_SYSTEM_NAME: Linux
1> [CMake] CMake Error at DeepSkyStackerKernel/CMakeLists.txt:227 (if):
1> [CMake]   if given arguments:
1> [CMake] 
1> [CMake]     "LINUX" "OR" "(" "APPLE" "AND" "(" "STREQUAL" "x86_64" ")" ")"
1> [CMake] 
1> [CMake]   Unknown arguments specified

clearly my conditional CMake isn’t quite there yet …

D.

As shown in the output CMAKE_OSX_ARCHITECTURES was empty, so when ${CMAKE_OSX_ARCHITECTURES} is expanded, it expands to empty text, not an empty string. Try "${CMAKE_OSX_ARCHITECTURES}" to get the variable expanded into a string literal and then STREQUAL is comparing two string literals.

CMAKE_OSX_ARCHITECTURES by default is empty:
https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_ARCHITECTURES.html

It’s typically set by users. Either on the command line or a toolchain file. That way it’s registered before the first project call.

It’s a variable intended for when you want to build a binary with multiple architectures. Apple calls this a universal binary.

1 Like

Thank you that works :slight_smile:
David