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