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_A64FX})
105    set(${return_arch_string} "AARCH64_A64FX" PARENT_SCOPE)
106  elseif(${MIPS})
107    set(${return_arch_string} "MIPS" PARENT_SCOPE)
108  elseif(${MIPS64})
109    set(${return_arch_string} "MIPS64" PARENT_SCOPE)
110  elseif(${RISCV64})
111    set(${return_arch_string} "RISCV64" PARENT_SCOPE)
112  else()
113    set(${return_arch_string} "${LIBOMP_ARCH}" PARENT_SCOPE)
114    libomp_warning_say("libomp_get_legal_arch(): Warning: Unknown architecture: Using ${LIBOMP_ARCH}")
115  endif()
116endfunction()
117
118# void libomp_check_variable(string var, ...);
119# - runs through all values checking if ${var} == value
120# - uppercase and lowercase do not matter
121# - if the var is found, then just print it out
122# - if the var is not found, then error out
123function(libomp_check_variable var)
124  set(valid_flag 0)
125  string(TOLOWER "${${var}}" var_lower)
126  foreach(value IN LISTS ARGN)
127    string(TOLOWER "${value}" value_lower)
128    if("${var_lower}" STREQUAL "${value_lower}")
129      set(valid_flag 1)
130      set(the_value "${value}")
131    endif()
132  endforeach()
133  if(${valid_flag} EQUAL 0)
134    libomp_error_say("libomp_check_variable(): ${var} = ${${var}} is unknown")
135  endif()
136endfunction()
137
138# void libomp_get_build_number(string src_dir, string* return_build_number);
139# - grab the eight digit build number (or 00000000) from kmp_version.cpp
140function(libomp_get_build_number src_dir return_build_number)
141  # sets file_lines_list to a list of all lines in kmp_version.cpp
142  file(STRINGS "${src_dir}/src/kmp_version.cpp" file_lines_list)
143
144  # runs through each line in kmp_version.cpp
145  foreach(line IN LISTS file_lines_list)
146    # if the line begins with "#define KMP_VERSION_BUILD" then we take not of the build number
147    string(REGEX MATCH "^[ \t]*#define[ \t]+KMP_VERSION_BUILD" valid "${line}")
148    if(NOT "${valid}" STREQUAL "") # if we matched "#define KMP_VERSION_BUILD", then grab the build number
149      string(REGEX REPLACE "^[ \t]*#define[ \t]+KMP_VERSION_BUILD[ \t]+([0-9]+)" "\\1"
150           build_number "${line}"
151      )
152    endif()
153  endforeach()
154  set(${return_build_number} "${build_number}" PARENT_SCOPE) # return build number
155endfunction()
156
157# void libomp_get_legal_type(string* return_legal_type);
158# - set the legal type name Performance/Profiling/Stub
159function(libomp_get_legal_type return_legal_type)
160  if(${NORMAL_LIBRARY})
161    set(${return_legal_type} "Performance" PARENT_SCOPE)
162  elseif(${PROFILE_LIBRARY})
163    set(${return_legal_type} "Profiling" PARENT_SCOPE)
164  elseif(${STUBS_LIBRARY})
165    set(${return_legal_type} "Stub" PARENT_SCOPE)
166  endif()
167endfunction()
168
169# void libomp_add_suffix(string suffix, list<string>* list_of_items);
170# - returns list_of_items with suffix appended to all items
171# - original list is modified
172function(libomp_add_suffix suffix list_of_items)
173  set(local_list "")
174  foreach(item IN LISTS "${list_of_items}")
175    if(NOT "${item}" STREQUAL "")
176      list(APPEND local_list "${item}${suffix}")
177    endif()
178  endforeach()
179  set(${list_of_items} "${local_list}" PARENT_SCOPE)
180endfunction()
181
182# void libomp_list_to_string(list<string> list_of_things, string* return_string);
183# - converts a list to a space separated string
184function(libomp_list_to_string list_of_things return_string)
185  string(REPLACE ";" " " output_variable "${list_of_things}")
186  set(${return_string} "${output_variable}" PARENT_SCOPE)
187endfunction()
188
189# void libomp_string_to_list(string str, list<string>* return_list);
190# - converts a string to a semicolon separated list
191# - what it really does is just string_replace all running whitespace to a semicolon
192# - in cmake, a list is strings separated by semicolons: i.e., list of four items, list = "item1;item2;item3;item4"
193function(libomp_string_to_list str return_list)
194  set(outstr)
195  string(REGEX REPLACE "[ \t]+" ";" outstr "${str}")
196  set(${return_list} "${outstr}" PARENT_SCOPE)
197endfunction()
198
199