How to determine platforms like ARM, MIPS and IA32?

懵懂的女人 提交于 2019-12-05 10:46:07
  • I don't think a variable named ARCHITECTURE_ID does exist in CMake to query. That's probably the reason why you can't find any hints in CMake's documentation. It's only used in CMakePlatformId.h to fill MSVC specific internal variable like MSVC_CXX_ARCHITECTURE_ID.

    /* For windows compilers MSVC and Intel we can determine
       the architecture of the compiler being used.  This is because
       the compilers do not have flags that can change the architecture,
       but rather depend on which compiler is being used
    */
    
  • What you are actually looking for is the cmake_host_system_information()command. The problem there is, that it does not export all the information it actually has. I'm thinking that to be a missing feature and probably will make a pull request for CMake's source Git over the next days to extend its functionality, but that won't help you for the near future.

    If you have CMake's source code on the system(s) in question you could run one of the tests

     $ cmsysTestscxx testSystemInformation
    

    to see what system information CMake actually has.

    𝓝𝓸𝓽𝓮: Querying the host system won't help when cross-compiling or e.g. compiling for 32 Bit on a 64 Bit machine (that could also be a possible flaw in your GNUMakefile example).

  • To answer your question about the if statement, this can simply be solved by an Regular Expression:

    cmake_minimum_required(VERSION 2.4)
    
    project(TestArchitectureId)
    
    if (ARCHITECTURE_ID MATCHES "^(X86|X32|X64|x64)$" )
        message(STATUS "Hello ${CMAKE_MATCH_1}")
    endif()
    

Otherwise isn't this somewhat related to Detect 32-bit x86 processor in CMakeList.txt?

Alternatives

CMake itself uses

if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "ia64")

or

if(NOT CMAKE_GENERATOR MATCHES "IA64")

or - even this may be going the extra mile - call try_compile() with rdrand.cpp:

# Choose a configuration for our compiler tests
if (NOT CMAKE_CONFIGURATION_TYPES AND 
    NOT CMAKE_NO_BUILD_TYPE)
    set(CMAKE_TRY_COMPILE_CONFIGURATION "${CMAKE_BUILD_TYPE}")
else()
    set(CMAKE_TRY_COMPILE_CONFIGURATION RelWithDebInfo)
endif()
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

string(TOUPPER "${CMAKE_TRY_COMPILE_CONFIGURATION}" cryptopp_TRY_COMPILE_CONFIGURATION) 
set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS}" "${CMAKE_CXX_FLAGS_${cryptopp_TRY_COMPILE_CONFIGURATION}}") 
get_directory_property(CMAKE_REQUIRED_INCLUDES INCLUDE_DIRECTORIES)
get_directory_property(CMAKE_REQUIRED_DEFINITIONS COMPILE_DEFINITIONS)
if (CMAKE_REQUIRED_DEFINITIONS)
    string(REPLACE ";" ";-D" CMAKE_REQUIRED_DEFINITIONS "-D${CMAKE_REQUIRED_DEFINITIONS}")
endif()

try_compile(
    cryptopp_RDRAND_WORKS 
    ${CMAKE_CURRENT_BINARY_DIR}
    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/rdrand.cpp
    CMAKE_FLAGS 
        -DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}
        -DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}
    COMPILE_DEFINITIONS 
        ${CMAKE_REQUIRED_DEFINITIONS}
)    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!