CMakeLists.txt 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #
  2. # helper CMakeLists.txt file that can be used to generate input files
  3. # for the Tests/RunCMake/ParseImplicit[Include|Lib]Info tests.
  4. #
  5. # usage:
  6. # [create a temporary build directory and chdir to it]
  7. # cmake [-D options] $CMAKE_SRC/Tests/RunCMake/ParseImplicitIncludeInfo/data
  8. #
  9. # where useful -D options include:
  10. # -DLANGUAGES="C;CXX" -- list of languages to generate inputs for
  11. # -DUNAME="Darwin" -- operating system name (def: CMAKE_SYSTEM_NAME)
  12. #
  13. cmake_minimum_required(VERSION 3.3)
  14. if(POLICY CMP0089)
  15. cmake_policy(SET CMP0089 NEW)
  16. endif()
  17. set(lngs C CXX)
  18. set(LANGUAGES "${lngs}" CACHE STRING "List of languages to generate inputs for")
  19. project(gen_implicit_include_data ${LANGUAGES})
  20. set(UNAME "${CMAKE_SYSTEM_NAME}" CACHE STRING "System uname")
  21. string(TOLOWER "${UNAME}" UNAME)
  22. message("Generate input for system type: ${UNAME}")
  23. # CMAKE_<LANG>_COMPILER_* variables we save in the resultfile
  24. set(compvars ABI AR ARCHITECTURE_ID EXTERNAL_TOOLCHAIN ID LAUNCHER LOADED
  25. RANLIB TARGET VERSION VERSION_INTERAL)
  26. foreach(lang IN ITEMS ${LANGUAGES})
  27. if("${lang}" STREQUAL "C")
  28. set(file ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c)
  29. elseif("${lang}" STREQUAL "CXX")
  30. set(file ${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp)
  31. elseif("${lang}" STREQUAL "CUDA")
  32. set(file ${CMAKE_ROOT}/Modules/CMakeCUDACompilerABI.cu)
  33. elseif("${lang}" STREQUAL "Fortran")
  34. set(file ${CMAKE_ROOT}/Modules/CMakeFortranCompilerABI.F)
  35. else()
  36. message(FATAL_ERROR "unknown language ${lang}")
  37. endif()
  38. set(resultfile "${CMAKE_BINARY_DIR}/")
  39. string(APPEND resultfile ${UNAME}-${lang}-${CMAKE_${lang}_COMPILER_ID})
  40. string(APPEND resultfile -${CMAKE_${lang}_COMPILER_VERSION})
  41. string(APPEND resultfile .input)
  42. message("Generate input for language ${lang}")
  43. message("Input file: ${file}")
  44. message("Result file: ${resultfile}")
  45. # replicate logic from CMakeDetermineCompilerABI
  46. set(outfile "${CMAKE_PLATFORM_INFO_DIR}/test${lang}.out")
  47. set(CMAKE_FLAGS )
  48. set(COMPILE_DEFINITIONS )
  49. if(DEFINED CMAKE_${lang}_VERBOSE_FLAG)
  50. set(CMAKE_FLAGS "-DEXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}")
  51. set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_FLAG}")
  52. endif()
  53. if(DEFINED CMAKE_${lang}_VERBOSE_COMPILE_FLAG)
  54. set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_COMPILE_FLAG}")
  55. endif()
  56. if(NOT "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC")
  57. # Avoid adding our own platform standard libraries for compilers
  58. # from which we might detect implicit link libraries.
  59. list(APPEND CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD_LIBRARIES=")
  60. endif()
  61. try_compile(rv ${CMAKE_BINARY_DIR} ${file}
  62. CMAKE_FLAGS ${CMAKE_FLAGS}
  63. COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS}
  64. CMAKE_FLAGS ${CMAKE_FLAGS}
  65. OUTPUT_VARIABLE output
  66. COPY_FILE "${outfile}"
  67. COPY_FILE_ERROR copy_error)
  68. if(NOT rv)
  69. message(FATAL_ERROR "${lang} compile failed!!")
  70. endif()
  71. set(result "CMAKE_LANG=${lang}\n")
  72. list(APPEND result "CMAKE_LINKER=${CMAKE_LINKER}\n")
  73. foreach(var IN ITEMS ${compvars})
  74. list(APPEND result
  75. "CMAKE_${lang}_COMPILER_${var}=${CMAKE_${lang}_COMPILER_${var}}\n")
  76. endforeach()
  77. file(WRITE ${resultfile} ${result} ${output})
  78. endforeach()