How to detect target architecture using CMake?

后端 未结 8 1445
感情败类
感情败类 2020-12-08 02:17

I\'ve done a lot of research and been unable to find an answer to this... how can I reliably find the target architecture I\'m compiling for, using CMake? Basically, the equ

8条回答
  •  孤城傲影
    2020-12-08 02:45

    This post is old, so sorry if resurrecting the dead here but I just thought I'd share the solution I made.

    I didn't want to use any external application and unfortunately the toolchain.cmake file we're using doesn't have the arch set in another variable so I detect it by looking at the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS variables looking for the -march argument to GCC. If there isn't one, it falls back to CMAKE_HOST_SYSTEM_PROCESSOR.

    A quick look at the Clang documentation seems to indicate that this wouldn't work for that one but it would just need a second regex step to match its expected argument.

    set(TARGET_ARCH_REGEX "^.*-march[= ]([^ ]+).*$")
    string(REGEX MATCH "${TARGET_ARCH_REGEX}" TARGET_ARCH_MATCH ${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS})
    if (TARGET_ARCH_MATCH)
        string(REGEX REPLACE "${TARGET_ARCH_REGEX}" "\\1" TARGET_ARCH ${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS})
    else()
        set(TARGET_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
    endif()
    

提交回复
热议问题