How to detect target architecture using CMake?

后端 未结 8 1348
感情败类
感情败类 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()
    
    0 讨论(0)
  • 2020-12-08 02:49

    I've a Solution for the Case where the Host and Target System are the same.

    First you need to call "uname -m" to get the "machine hardware name". Afterwards you need to cut off the trailing "Carriage Return" to get the actual Value back into the provided Variable.

    EXECUTE_PROCESS( COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE )
    

    Now you can print out the Variable ${ARCHITECTURE}:

    message( STATUS "Architecture: ${ARCHITECTURE}" )
    

    or do some Canonisation to map, e.g. "x86_64", "amd64", ... to e.g. "64Bit". Same goes for 32Bit. With this you can execute archtecture dependend Compilation like:

    if( ${ARCHITECTURE} STREQUAL "64Bit" )
        set( BLA_LIBRARY "/opt/lib/libBla.so" )
    else()
        set( BLA_LIBRARY "/opt/lib32/libBla.so" )
    endif()
    
    0 讨论(0)
提交回复
热议问题