DetermineTargetArchitecture.cmake 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # - Determines the target architecture of the compilation
  2. #
  3. # This function checks the architecture that will be built by the compiler
  4. # and sets a variable to the architecture
  5. #
  6. # determine_target_architecture(<OUTPUT_VAR>)
  7. #
  8. # - Example
  9. #
  10. # include(DetermineTargetArchitecture)
  11. # determine_target_architecture(PROJECT_NAME_ARCHITECTURE)
  12. if(__determine_target_architecture)
  13. return()
  14. endif()
  15. set(__determine_target_architecture INCLUDED)
  16. function(determine_target_architecture FLAG)
  17. if (MSVC)
  18. if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "X86")
  19. set(ARCH "i686")
  20. elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "x64")
  21. set(ARCH "x86_64")
  22. elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM")
  23. set(ARCH "arm")
  24. else()
  25. message(FATAL_ERROR "Failed to determine the MSVC target architecture: ${MSVC_C_ARCHITECTURE_ID}")
  26. endif()
  27. else()
  28. execute_process(
  29. COMMAND ${CMAKE_C_COMPILER} -dumpmachine
  30. RESULT_VARIABLE RESULT
  31. OUTPUT_VARIABLE ARCH
  32. ERROR_QUIET
  33. )
  34. if (RESULT)
  35. message(FATAL_ERROR "Failed to determine target architecture triplet: ${RESULT}")
  36. endif()
  37. string(REGEX MATCH "([^-]+).*" ARCH_MATCH ${ARCH})
  38. if (NOT CMAKE_MATCH_1 OR NOT ARCH_MATCH)
  39. message(FATAL_ERROR "Failed to match the target architecture triplet: ${ARCH}")
  40. endif()
  41. set(ARCH ${CMAKE_MATCH_1})
  42. endif()
  43. message(STATUS "Target architecture - ${ARCH}")
  44. set(FLAG ${ARCH} PARENT_SCOPE)
  45. endfunction()