xref: /llvm-project/openmp/runtime/cmake/LibompUtils.cmake (revision c5bbdb64940d75e7702d1d450e77534d908fc9f1)
1#
2#//===----------------------------------------------------------------------===//
3#//
4#// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5#// See https://llvm.org/LICENSE.txt for license information.
6#// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#//
8#//===----------------------------------------------------------------------===//
9#
10
11# void libomp_say(string message_to_user);
12# - prints out message_to_user
13macro(libomp_say message_to_user)
14  message(STATUS "LIBOMP: ${message_to_user}")
15endmacro()
16
17# void libomp_warning_say(string message_to_user);
18# - prints out message_to_user with a warning
19macro(libomp_warning_say message_to_user)
20  message(WARNING "LIBOMP: ${message_to_user}")
21endmacro()
22
23# void libomp_error_say(string message_to_user);
24# - prints out message_to_user with an error and exits cmake
25macro(libomp_error_say message_to_user)
26  message(FATAL_ERROR "LIBOMP: ${message_to_user}")
27endmacro()
28
29# libomp_append(<flag> <flags_list> [(IF_TRUE | IF_FALSE | IF_TRUE_1_0 ) BOOLEAN])
30#
31# libomp_append(<flag> <flags_list>)
32#   - unconditionally appends <flag> to the list of definitions
33#
34# libomp_append(<flag> <flags_list> <BOOLEAN>)
35#   - appends <flag> to the list of definitions if BOOLEAN is true
36#
37# libomp_append(<flag> <flags_list> IF_TRUE <BOOLEAN>)
38#   - appends <flag> to the list of definitions if BOOLEAN is true
39#
40# libomp_append(<flag> <flags_list> IF_FALSE <BOOLEAN>)
41#   - appends <flag> to the list of definitions if BOOLEAN is false
42#
43# libomp_append(<flag> <flags_list> IF_DEFINED <VARIABLE>)
44#   - appends <flag> to the list of definitions if VARIABLE is defined
45#
46# libomp_append(<flag> <flags_list> IF_TRUE_1_0 <BOOLEAN>)
47#   - appends <flag>=1 to the list of definitions if <BOOLEAN> is true, <flag>=0 otherwise
48# e.g., libomp_append("-D USE_FEATURE" IF_TRUE_1_0 HAVE_FEATURE)
49#     appends "-D USE_FEATURE=1" if HAVE_FEATURE is true
50#     or "-D USE_FEATURE=0" if HAVE_FEATURE is false
51macro(libomp_append flags flag)
52  if(NOT (${ARGC} EQUAL 2 OR ${ARGC} EQUAL 3 OR ${ARGC} EQUAL 4))
53    libomp_error_say("libomp_append: takes 2, 3, or 4 arguments")
54  endif()
55  if(${ARGC} EQUAL 2)
56    list(APPEND ${flags} "${flag}")
57  elseif(${ARGC} EQUAL 3)
58    if(${ARGV2})
59      list(APPEND ${flags} "${flag}")
60    endif()
61  else()
62    if(${ARGV2} STREQUAL "IF_TRUE")
63      if(${ARGV3})
64        list(APPEND ${flags} "${flag}")
65      endif()
66    elseif(${ARGV2} STREQUAL "IF_FALSE")
67      if(NOT ${ARGV3})
68        list(APPEND ${flags} "${flag}")
69      endif()
70    elseif(${ARGV2} STREQUAL "IF_DEFINED")
71      if(DEFINED ${ARGV3})
72        list(APPEND ${flags} "${flag}")
73      endif()
74    elseif(${ARGV2} STREQUAL "IF_TRUE_1_0")
75      if(${ARGV3})
76        list(APPEND ${flags} "${flag}=1")
77      else()
78        list(APPEND ${flags} "${flag}=0")
79      endif()
80    else()
81      libomp_error_say("libomp_append: third argument must be one of IF_TRUE, IF_FALSE, IF_DEFINED, IF_TRUE_1_0")
82    endif()
83  endif()
84endmacro()
85
86# void libomp_get_legal_arch(string* return_arch_string);
87# - returns (through return_arch_string) the formal architecture
88#   string or warns user of unknown architecture
89function(libomp_get_legal_arch return_arch_string)
90  if(${IA32})
91    set(${return_arch_string} "IA-32" PARENT_SCOPE)
92  elseif(${INTEL64})
93    set(${return_arch_string} "Intel(R) 64" PARENT_SCOPE)
94  elseif(${MIC})
95    set(${return_arch_string} "Intel(R) Many Integrated Core Architecture" PARENT_SCOPE)
96  elseif(${ARM})
97    set(${return_arch_string} "ARM" PARENT_SCOPE)
98  elseif(${PPC64BE})
99    set(${return_arch_string} "PPC64BE" PARENT_SCOPE)
100  elseif(${PPC64LE})
101    set(${return_arch_string} "PPC64LE" PARENT_SCOPE)
102  elseif(${AARCH64})
103    set(${return_arch_string} "AARCH64" PARENT_SCOPE)
104  elseif(${AARCH64_32})
105    set(${return_arch_string} "AARCH64_32" PARENT_SCOPE)
106  elseif(${AARCH64_A64FX})
107    set(${return_arch_string} "AARCH64_A64FX" PARENT_SCOPE)
108  elseif(${MIPS})
109    set(${return_arch_string} "MIPS" PARENT_SCOPE)
110  elseif(${MIPS64})
111    set(${return_arch_string} "MIPS64" PARENT_SCOPE)
112  elseif(${RISCV64})
113    set(${return_arch_string} "RISCV64" PARENT_SCOPE)
114  elseif(${LOONGARCH64})
115    set(${return_arch_string} "LOONGARCH64" PARENT_SCOPE)
116  elseif(${VE})
117    set(${return_arch_string} "VE" PARENT_SCOPE)
118  elseif(${S390X})
119    set(${return_arch_string} "S390X" PARENT_SCOPE)
120  else()
121    set(${return_arch_string} "${LIBOMP_ARCH}" PARENT_SCOPE)
122    libomp_warning_say("libomp_get_legal_arch(): Warning: Unknown architecture: Using ${LIBOMP_ARCH}")
123  endif()
124endfunction()
125
126# void libomp_check_variable(string var, ...);
127# - runs through all values checking if ${var} == value
128# - uppercase and lowercase do not matter
129# - if the var is found, then just print it out
130# - if the var is not found, then error out
131function(libomp_check_variable var)
132  set(valid_flag 0)
133  string(TOLOWER "${${var}}" var_lower)
134  foreach(value IN LISTS ARGN)
135    string(TOLOWER "${value}" value_lower)
136    if("${var_lower}" STREQUAL "${value_lower}")
137      set(valid_flag 1)
138      set(the_value "${value}")
139    endif()
140  endforeach()
141  if(${valid_flag} EQUAL 0)
142    libomp_error_say("libomp_check_variable(): ${var} = ${${var}} is unknown")
143  endif()
144endfunction()
145
146# void libomp_get_build_number(string src_dir, string* return_build_number);
147# - grab the eight digit build number (or 00000000) from kmp_version.cpp
148function(libomp_get_build_number src_dir return_build_number)
149  # sets file_lines_list to a list of all lines in kmp_version.cpp
150  file(STRINGS "${src_dir}/src/kmp_version.cpp" file_lines_list)
151
152  # runs through each line in kmp_version.cpp
153  foreach(line IN LISTS file_lines_list)
154    # if the line begins with "#define KMP_VERSION_BUILD" then we take not of the build number
155    string(REGEX MATCH "^[ \t]*#define[ \t]+KMP_VERSION_BUILD" valid "${line}")
156    if(NOT "${valid}" STREQUAL "") # if we matched "#define KMP_VERSION_BUILD", then grab the build number
157      string(REGEX REPLACE "^[ \t]*#define[ \t]+KMP_VERSION_BUILD[ \t]+([0-9]+)" "\\1"
158           build_number "${line}"
159      )
160    endif()
161  endforeach()
162  set(${return_build_number} "${build_number}" PARENT_SCOPE) # return build number
163endfunction()
164
165# void libomp_get_legal_type(string* return_legal_type);
166# - set the legal type name Performance/Profiling/Stub
167function(libomp_get_legal_type return_legal_type)
168  if(${NORMAL_LIBRARY})
169    set(${return_legal_type} "Performance" PARENT_SCOPE)
170  elseif(${PROFILE_LIBRARY})
171    set(${return_legal_type} "Profiling" PARENT_SCOPE)
172  elseif(${STUBS_LIBRARY})
173    set(${return_legal_type} "Stub" PARENT_SCOPE)
174  endif()
175endfunction()
176
177# void libomp_add_suffix(string suffix, list<string>* list_of_items);
178# - returns list_of_items with suffix appended to all items
179# - original list is modified
180function(libomp_add_suffix suffix list_of_items)
181  set(local_list "")
182  foreach(item IN LISTS "${list_of_items}")
183    if(NOT "${item}" STREQUAL "")
184      list(APPEND local_list "${item}${suffix}")
185    endif()
186  endforeach()
187  set(${list_of_items} "${local_list}" PARENT_SCOPE)
188endfunction()
189
190# void libomp_list_to_string(list<string> list_of_things, string* return_string);
191# - converts a list to a space separated string
192function(libomp_list_to_string list_of_things return_string)
193  string(REPLACE ";" " " output_variable "${list_of_things}")
194  set(${return_string} "${output_variable}" PARENT_SCOPE)
195endfunction()
196
197# void libomp_string_to_list(string str, list<string>* return_list);
198# - converts a string to a semicolon separated list
199# - what it really does is just string_replace all running whitespace to a semicolon
200# - in cmake, a list is strings separated by semicolons: i.e., list of four items, list = "item1;item2;item3;item4"
201function(libomp_string_to_list str return_list)
202  set(outstr)
203  string(REGEX REPLACE "[ \t]+" ";" outstr "${str}")
204  set(${return_list} "${outstr}" PARENT_SCOPE)
205endfunction()
206
207