CrayPrgEnv.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Guard against multiple inclusions
  2. if(__cmake_craype_crayprgenv)
  3. return()
  4. endif()
  5. set(__cmake_craype_crayprgenv 1)
  6. # CrayPrgEnv: loaded when compiling through the Cray compiler wrapper.
  7. # The compiler wrapper can run on a front-end node or a compute node.
  8. cmake_policy(PUSH)
  9. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  10. # One-time setup of the craype environment. First, check the wrapper config.
  11. # The wrapper's selection of a compiler (gcc, clang, intel, etc.) and
  12. # default include/library paths is selected using the "module" command.
  13. # The CRAYPE_LINK_TYPE environment variable partly controls if static
  14. # or dynamic binaries are generated (see __cmake_craype_linktype below).
  15. # Running cmake and then changing module and/or linktype configuration
  16. # may cause build problems (since the data in the cmake cache may no
  17. # longer be correct after the change). We can look for this and warn
  18. # the user about it. Second, use the "module" provided PKG_CONFIG_PATH-like
  19. # environment variable to add additional prefixes to the system prefix
  20. # path.
  21. function(__cmake_craype_setupenv)
  22. if(NOT DEFINED __cmake_craype_setupenv_done) # only done once per run
  23. set(__cmake_craype_setupenv_done 1 PARENT_SCOPE)
  24. unset(__cmake_check)
  25. set(CMAKE_CRAYPE_LINKTYPE "$ENV{CRAYPE_LINK_TYPE}" CACHE STRING
  26. "saved value of CRAYPE_LINK_TYPE environment variable")
  27. set(CMAKE_CRAYPE_LOADEDMODULES "$ENV{LOADEDMODULES}" CACHE STRING
  28. "saved value of LOADEDMODULES environment variable")
  29. mark_as_advanced(CMAKE_CRAYPE_LINKTYPE CMAKE_CRAYPE_LOADEDMODULES)
  30. if (NOT "${CMAKE_CRAYPE_LINKTYPE}" STREQUAL "$ENV{CRAYPE_LINK_TYPE}")
  31. string(APPEND __cmake_check "CRAYPE_LINK_TYPE ")
  32. endif()
  33. if (NOT "${CMAKE_CRAYPE_LOADEDMODULES}" STREQUAL "$ENV{LOADEDMODULES}")
  34. string(APPEND __cmake_check "LOADEDMODULES ")
  35. endif()
  36. if(DEFINED __cmake_check)
  37. message(STATUS "NOTE: ${__cmake_check}changed since initial config!")
  38. message(STATUS "NOTE: this may cause unexpected build errors.")
  39. endif()
  40. # loop over variables of interest
  41. foreach(pkgcfgvar PKG_CONFIG_PATH PKG_CONFIG_PATH_DEFAULT
  42. PE_PKG_CONFIG_PATH)
  43. file(TO_CMAKE_PATH "$ENV{${pkgcfgvar}}" pkgcfg)
  44. foreach(path ${pkgcfg})
  45. string(REGEX REPLACE "(.*)/lib[^/]*/pkgconfig$" "\\1" path "${path}")
  46. if(NOT "${path}" STREQUAL "" AND
  47. NOT "${path}" IN_LIST CMAKE_SYSTEM_PREFIX_PATH)
  48. list(APPEND CMAKE_SYSTEM_PREFIX_PATH "${path}")
  49. endif()
  50. endforeach()
  51. endforeach()
  52. # push it up out of this function into the parent scope
  53. set(CMAKE_SYSTEM_PREFIX_PATH "${CMAKE_SYSTEM_PREFIX_PATH}" PARENT_SCOPE)
  54. endif()
  55. endfunction()
  56. # The wrapper disables dynamic linking by default. Dynamic linking is
  57. # enabled either by setting $ENV{CRAYPE_LINK_TYPE} to "dynamic" or by
  58. # specifying "-dynamic" to the wrapper when linking. Specifying "-static"
  59. # to the wrapper when linking takes priority over $ENV{CRAYPE_LINK_TYPE}.
  60. # Furthermore, if you specify multiple "-dynamic" and "-static" flags to
  61. # the wrapper when linking, the last one will win. In this case, the
  62. # wrapper will also print a warning like:
  63. # Warning: -dynamic was already seen on command line, overriding with -static.
  64. #
  65. # note that cmake applies both CMAKE_${lang}_FLAGS and CMAKE_EXE_LINKER_FLAGS
  66. # (in that order) to the linking command, so -dynamic can appear in either
  67. # variable.
  68. #
  69. # Note: As of CrayPE v19.06 (which translates to the craype/2.6.0 module)
  70. # the default has changed and is now dynamic by default. This is handled
  71. # accordingly
  72. function(__cmake_craype_linktype lang rv)
  73. # start with ENV, but allow flags to override
  74. if(("$ENV{CRAYPE_VERSION}" STREQUAL "") OR
  75. ("$ENV{CRAYPE_VERSION}" VERSION_LESS "2.6"))
  76. if("$ENV{CRAYPE_LINK_TYPE}" STREQUAL "dynamic")
  77. set(linktype dynamic)
  78. else()
  79. set(linktype static)
  80. endif()
  81. else()
  82. if("$ENV{CRAYPE_LINK_TYPE}" STREQUAL "static")
  83. set(linktype static)
  84. else()
  85. set(linktype dynamic)
  86. endif()
  87. endif()
  88. # combine flags and convert to a list so we can apply the flags in order
  89. set(linkflags "${CMAKE_${lang}_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
  90. string(REPLACE " " ";" linkflags "${linkflags}")
  91. foreach(flag IN LISTS linkflags)
  92. if("${flag}" STREQUAL "-dynamic")
  93. set(linktype dynamic)
  94. elseif("${flag}" STREQUAL "-static")
  95. set(linktype static)
  96. endif()
  97. endforeach()
  98. set(${rv} ${linktype} PARENT_SCOPE)
  99. endfunction()
  100. macro(__CrayPrgEnv_setup lang)
  101. if(DEFINED ENV{CRAYPE_VERSION})
  102. message(STATUS "Cray Programming Environment $ENV{CRAYPE_VERSION} ${lang}")
  103. elseif(DEFINED ENV{ASYNCPE_VERSION})
  104. message(STATUS "Cray XT Programming Environment $ENV{ASYNCPE_VERSION} ${lang}")
  105. else()
  106. message(STATUS "Cray Programming Environment (unknown version) ${lang}")
  107. endif()
  108. # setup the craype environment
  109. __cmake_craype_setupenv()
  110. # Flags for the Cray wrappers
  111. set(CMAKE_STATIC_LIBRARY_LINK_${lang}_FLAGS "-static")
  112. set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
  113. set(CMAKE_SHARED_LIBRARY_LINK_${lang}_FLAGS "-dynamic")
  114. # determine linktype from environment and compiler flags
  115. __cmake_craype_linktype(${lang} __cmake_craype_${lang}_linktype)
  116. # switch off shared libs if we get a static linktype
  117. if("${__cmake_craype_${lang}_linktype}" STREQUAL "static")
  118. set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
  119. set(BUILD_SHARED_LIBS FALSE CACHE BOOL "")
  120. set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  121. set(CMAKE_LINK_SEARCH_START_STATIC TRUE)
  122. endif()
  123. endmacro()
  124. cmake_policy(POP)