CMakeCompilerIdDetection.cmake 4.49 KB
Newer Older
1 2
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
3 4 5 6 7 8 9


function(_readFile file)
  include(${file})
  get_filename_component(name ${file} NAME_WE)
  string(REGEX REPLACE "-.*" "" CompilerId ${name})
  set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
10
  set(_compiler_id_simulate_${CompilerId} ${_compiler_id_simulate} PARENT_SCOPE)
11 12 13
  set(_compiler_id_pp_test_${CompilerId} ${_compiler_id_pp_test} PARENT_SCOPE)
endfunction()

14 15
include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)

16 17 18
function(compiler_id_detection outvar lang)

  if (NOT lang STREQUAL Fortran)
19
    file(GLOB lang_files
20
      "${CMAKE_ROOT}/Modules/Compiler/*-DetermineCompiler.cmake")
21 22 23 24 25 26 27 28
    set(nonlang CXX)
    if (lang STREQUAL CXX)
      set(nonlang C)
    endif()

    file(GLOB nonlang_files
      "${CMAKE_ROOT}/Modules/Compiler/*-${nonlang}-DetermineCompiler.cmake")
    list(REMOVE_ITEM lang_files ${nonlang_files})
29 30
  endif()

31
  set(files ${lang_files})
32 33 34 35 36
  if (files)
    foreach(file ${files})
      _readFile(${file})
    endforeach()

37
    set(options ID_STRING VERSION_STRINGS ID_DEFINE PLATFORM_DEFAULT_COMPILER)
38
    set(oneValueArgs PREFIX)
39 40 41 42 43
    cmake_parse_arguments(CID "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${ARGN})
    if (CID_UNPARSED_ARGUMENTS)
      message(FATAL_ERROR "Unrecognized arguments: \"${CID_UNPARSED_ARGUMENTS}\"")
    endif()

44 45 46 47 48 49 50 51
    # Order is relevant here. For example, compilers which pretend to be
    # GCC must appear before the actual GCC.
    if (lang STREQUAL CXX)
      list(APPEND ordered_compilers
        Comeau
      )
    endif()
    list(APPEND ordered_compilers
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      Intel
      PathScale
      Embarcadero
      Borland
      Watcom
      OpenWatcom
      SunPro
      HP
      Compaq
      zOS
      XL
      VisualAge
      PGI
      Cray
      TI
67
      Fujitsu
68 69 70 71
    )
    if (lang STREQUAL C)
      list(APPEND ordered_compilers
        TinyCC
72
        Bruce
73 74 75
      )
    endif()
    list(APPEND ordered_compilers
76
      SCO
77 78
      AppleClang
      Clang
79 80 81 82
      GNU
      MSVC
      ADSP
      IAR
83
      ARMCC
84 85 86 87 88 89 90
    )
    if (lang STREQUAL C)
      list(APPEND ordered_compilers
        SDCC
      )
    endif()
    list(APPEND ordered_compilers
91 92
      MIPSpro)

93 94
    if(CID_ID_DEFINE)
      foreach(Id ${ordered_compilers})
95
        set(CMAKE_${lang}_COMPILER_ID_CONTENT "${CMAKE_${lang}_COMPILER_ID_CONTENT}# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
96 97 98
      endforeach()
    endif()

99
    set(pp_if "#if")
100
    if (CID_VERSION_STRINGS)
101
      set(CMAKE_${lang}_COMPILER_ID_CONTENT "${CMAKE_${lang}_COMPILER_ID_CONTENT}\n/* Version number components: V=Version, R=Revision, P=Patch
102
   Version date components:   YYYY=Year, MM=Month,   DD=Day  */\n")
103
    endif()
104 105 106 107 108

    foreach(Id ${ordered_compilers})
      if (NOT _compiler_id_pp_test_${Id})
        message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
      endif()
109 110
      set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
      if (CID_ID_STRING)
111 112
        set(PREFIX ${CID_PREFIX})
        string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
113
        string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
114
      endif()
115
      if (CID_ID_DEFINE)
116 117
        string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
        string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
118
      endif()
119
      if (CID_VERSION_STRINGS)
120
        set(PREFIX ${CID_PREFIX})
121 122
        set(MACRO_DEC DEC)
        set(MACRO_HEX HEX)
123
        string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
124
        string(APPEND id_content "${VERSION_BLOCK}\n")
125
      endif()
126 127 128 129
      set(CMAKE_${lang}_COMPILER_ID_CONTENT "${CMAKE_${lang}_COMPILER_ID_CONTENT}\n${id_content}")
      set(pp_if "#elif")
    endforeach()

130 131
    if (CID_PLATFORM_DEFAULT_COMPILER)
      set(platform_compiler_detection "
132 133 134 135
/* These compilers are either not known or too old to define an
  identification macro.  Try to identify the platform and guess that
  it is the native compiler.  */
#elif defined(__sgi)
136
# define ${CID_PREFIX}COMPILER_ID \"MIPSpro\"
137 138

#elif defined(__hpux) || defined(__hpua)
139
# define ${CID_PREFIX}COMPILER_ID \"HP\"
140 141

#else /* unknown compiler */
142
# define ${CID_PREFIX}COMPILER_ID \"\"")
143
    endif()
144

145
    set(CMAKE_${lang}_COMPILER_ID_CONTENT "${CMAKE_${lang}_COMPILER_ID_CONTENT}\n${platform_compiler_detection}\n#endif")
146 147 148 149
  endif()

  set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
endfunction()