1

I'm trying to set a bunch of CXX flags based on processor type.

IF(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DAMD64")
ELSE()
    STRING(FIND ${CMAKE_SYSTEM_PROCESSOR} "86" 86_res)
    IF(${86_res} EQUAL -1)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIA32")
    ENDIF()
    STRING(FIND ${CMAKE_SYSTEM_PROCESSOR} "arm" arm_res)
    IF(${arm_res} EQUAL -1)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DARM")
    ENDIF()
ENDIF()

But for whatever reason CMAKE_SYSTEM_PROCESSOR is empty and can't run the STREQUAL comparison. Why is this empty?

For now I set this by running

EXEC_PROGRAM(uname ARGS -p OUTPUT_VARIABLE CMAKE_SYSTEM_PROCESSOR)

But that won't work on Windows so I'm not sure what I should do here.

  • What system are you getting the empty variable on? For some details on the variables behavior see e.g. [here](https://public.kitware.com/Bug/view.php?id=9065): "CMAKE_SYSTEM_PROCESSOR: The name of the CPU CMake is building for. On systems that support uname, this variable is set to the output of uname -p, on windows it is set to the value of the environment variable PROCESSOR_ARCHITECTURE." – Florian Jun 25 '18 at 13:53
  • 2
    Forgot to mention that those values are only set after the `project()` call. – Florian Jun 25 '18 at 14:04
  • `Linux 4.4.0-112-generic #135-Ubuntu SMP Fri Jan 19 11:48:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux` The project() call makes it a bit tricky. I have the OS related stuff set in a separate cmake file that I include in my CMakeLists.txt file. Putting the project before the include gives me an infinite loop of `You have changed variables that require your cache to be deleted. Configure will be re-run and you may have to reset some variables.` – m_deployment Jun 25 '18 at 14:45
  • Can you please add the content of the file preparing your system? Just a guess, but did you know that most of those variables have an `..._INIT` counterpart? And I admit that the only variables I know the above warning would give is to set the compiler paths or the generator itself (both which should not be part of your `CMakeLists.txt`). – Florian Jun 25 '18 at 19:33

1 Answers1

3

The macro must be used AFTER setting the project name with PROJECT(...).

MESSAGE("CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") # EMPTY!
PROJECT(dummy)
MESSAGE("CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") # OK!

Output:

1> CMAKE_SYSTEM_PROCESSOR:
1>-- Selecting Windows SDK version...
1> CMAKE_SYSTEM_PROCESSOR: AMD64

It could be assigned by toolchains when enabling cross-compiling.

Maestro
  • 348
  • 4
  • 13