blob: 46f8b262d2daf4ab0abfc5dc9ef74a75545c0aec [file] [log] [blame]
Jim Cownie3b81ce62014-08-05 09:32:28 +00001#
2#//===----------------------------------------------------------------------===//
3#//
4#// The LLVM Compiler Infrastructure
5#//
6#// This file is dual licensed under the MIT and the University of Illinois Open
7#// Source Licenses. See LICENSE.txt for details.
8#//
9#//===----------------------------------------------------------------------===//
10#
11
12####################################### FUNCTIONS/MACROS ###########################################
13# It should be noted that in cmake, functions can only be used on a single line with the return value
14# stored in a parameter you send to the function. There isn't a true return value. So technically,
15# all functions would have a C/C++ prototype of:
16# void function_name(parameter1, parameter2, ...);
17#
18# If you want a return value, you have to use a parameter so the function prototype would be:
19# void function_name(input_parameter1, input_parameter2, ..., return_value)
20# ##################
21
22# void say(string message_to_user);
23# - prints out message_to_user
24macro(say message_to_user)
25 message("${message_to_user}")
26endmacro()
27
28# void warning_say(string message_to_user);
29# - prints out message_to_user with a warning
30macro(warning_say message_to_user)
31 message(WARNING "${message_to_user}")
32endmacro()
33
34# void error_say(string message_to_user);
35# - prints out message_to_user with an error and exits cmake
36macro(error_say message_to_user)
37 message(FATAL_ERROR "${message_to_user}")
38endmacro()
39
40# void debug_say(string message_to_developer);
41# - prints out message when GLOBAL_DEBUG == 1 (for debugging cmake build)
42macro(debug_say message_to_developer)
43 if(${GLOBAL_DEBUG} STREQUAL "1")
44 say("DEBUG: ${message_to_developer}")
45 endif()
46endmacro()
47
48# void debug_say_var(variable var);
49# - prints the variable name and its value (for debugging cmake build)
50macro(debug_say_var var)
51 if(${GLOBAL_DEBUG} STREQUAL "1")
52 say("DEBUG: Variable: ${var} = ${${var}} ")
53 endif()
54endmacro()
55
56# void set_legal_arch(string* return_arch_string);
57# - returns (through return_arch_string) the formal architecture
58# string or warns user of unknown architecture
59function(set_legal_arch return_arch_string)
60 if(${IA32})
61 set(${return_arch_string} "IA-32" PARENT_SCOPE)
62 elseif(${INTEL64})
63 set(${return_arch_string} "Intel(R) 64" PARENT_SCOPE)
64 elseif(${MIC})
65 set(${return_arch_string} "Intel(R) Many Integrated Core Architecture" PARENT_SCOPE)
66 elseif(${arch} STREQUAL "l1")
67 set(${return_arch_string} "L1OM" PARENT_SCOPE)
68 elseif(${ARM})
69 set(${return_arch_string} "ARM" PARENT_SCOPE)
Andrey Churbanovcbda8682015-01-13 14:43:35 +000070 elseif(${PPC64})
71 set(${return_arch_string} "PPC64" PARENT_SCOPE)
72 elseif(${AARCH64})
73 set(${return_arch_string} "AARCH64" PARENT_SCOPE)
Jim Cownie3b81ce62014-08-05 09:32:28 +000074 else()
75 warning_say("set_legal_arch(): Warning: Unknown architecture...")
76 endif()
77endfunction()
78
79# void check_variable(string var, string var_name, list<string>values_list);
80# - runs through values_list checking if ${var} == values_list[i] for any i.
81# - if the var is found, then just print it out
82# - if the var is not found, then warn user
83function(check_variable var values_list)
84 set(valid_flag 0)
85 foreach(value IN LISTS values_list)
86 if("${${var}}" STREQUAL "${value}")
87 set(valid_flag 1)
88 set(the_value "${value}")
89 endif()
90 endforeach()
91 if(${valid_flag} EQUAL 0)
92 error_say("check_variable(): ${var} = ${${var}} is unknown")
93 endif()
94endfunction()
95
96# void _export_lib_dir(string export_dir, string platform, string suffix, string* return_value);
97# - basically a special case for mac platforms where it adds '.thin' to the output lib directory
98function(_export_lib_dir pltfrm return_value)
99 if(${MAC})
100 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib.thin" PARENT_SCOPE)
101 else()
102 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib" PARENT_SCOPE)
103 endif()
104endfunction()
105
106# void _export_lib_fat_dir(string export_dir, string platform, string suffix, string* return_value);
107# - another mac specialty case for fat libraries.
108# - this sets export_lib_fat_dir in the MAIN part of CMakeLists.txt
109function(_export_lib_fat_dir pltfrm return_value)
110 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib" PARENT_SCOPE)
111endfunction()
112
113# void get_build_number(string src_dir, string* return_build_number);
114# - grab the eight digit build number (or 00000000) from kmp_version.c
115function(get_build_number src_dir return_build_number)
116 # sets file_lines_list to a list of all lines in kmp_version.c
117 file(STRINGS "${src_dir}/src/kmp_version.c" file_lines_list)
118
119 # runs through each line in kmp_version.c
120 foreach(line IN LISTS file_lines_list)
121 # if the line begins with "#define KMP_VERSION_BUILD" then we take not of the build number
122 string(REGEX MATCH "^[ \t]*#define[ \t]+KMP_VERSION_BUILD" valid "${line}")
123 if(NOT "${valid}" STREQUAL "") # if we matched "#define KMP_VERSION_BUILD", then grab the build number
124 string(REGEX REPLACE "^[ \t]*#define[ \t]+KMP_VERSION_BUILD[ \t]+([0-9]+)" "\\1"
125 build_number "${line}"
126 )
127 endif()
128 endforeach()
129 set(${return_build_number} "${build_number}" PARENT_SCOPE) # return build number
130endfunction()
131
132# void set_legal_type(string* return_legal_type);
133# - set the legal type name Performance/Profiling/Stub
134function(set_legal_type return_legal_type)
135 if(${NORMAL_LIBRARY})
136 set(${return_legal_type} "Performance" PARENT_SCOPE)
137 elseif(${PROFILE_LIBRARY})
138 set(${return_legal_type} "Profiling" PARENT_SCOPE)
139 elseif(${STUBS_LIBRARY})
140 set(${return_legal_type} "Stub" PARENT_SCOPE)
141 endif()
142endfunction()
143
144# void set_mac_os_new(bool* return_mac_os_new);
145# - sets the return_mac_os_new variable to true or false based on macosx version
146# - no real "cmakey" way to do this. Have to call execute_process()
147function(set_mac_os_new return_mac_os_new)
148 execute_process(COMMAND "sw_vers" "-productVersion" OUTPUT_VARIABLE mac_osx_version)
149 if("${mac_osx_version}" VERSION_GREATER "10.6")
150 set(${return_mac_os_new} TRUE PARENT_SCOPE)
151 else()
152 set(${return_mac_os_new} FALSE PARENT_SCOPE)
153 endif()
154endfunction()
155
156# void add_prefix(string prefix, list<string>* list_of_items);
157# - returns list_of_items with prefix prepended to all items
158# - original list is modified
159function(add_prefix prefix list_of_items)
160 set(local_list "")
161 foreach(item IN LISTS "${list_of_items}")
162 if(NOT "${item}" STREQUAL "")
163 list(APPEND local_list "${prefix}${item}")
164 endif()
165 endforeach()
166 set(${list_of_items} "${local_list}" PARENT_SCOPE)
167endfunction()
168
169# void 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(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 strip_suffix(list<string> list_of_items, list<string>* return_list);
183# - returns a new list with suffix stripped (i.e., foo.c => foo)
184# - list_of_items is not modified, return_list is modified
185function(strip_suffix list_of_items return_list)
186 set(local_list "")
187 foreach(item IN LISTS "${list_of_items}")
188 if(NOT "${item}" STREQUAL "")
189 get_filename_component(filename "${item}" NAME_WE)
190 list(APPEND local_list "${filename}")
191 endif()
192 endforeach()
193 set(${return_list} "${local_list}" PARENT_SCOPE)
194endfunction()
195
196# void list_to_string(list<string> list_of_things, string* return_string);
197# - converts a list to a space separated string
198function(list_to_string list_of_things return_string)
199 string(REPLACE ";" " " output_variable "${list_of_things}")
200 set(${return_string} "${output_variable}" PARENT_SCOPE)
201endfunction()
202
203# void string_to_list(string str, list<string>* return_list);
204# - converts a string to a semicolon separated list
205# - what it really does is just string_replace all running whitespace to a semicolon
206# - in cmake, a list is strings separated by semicolons: i.e., list of four items, list = "item1;item2;item3;item4"
207function(string_to_list str return_list)
208 set(outstr)
209 string(REGEX REPLACE "[ \t]+" ";" outstr "${str}")
210 set(${return_list} "${outstr}" PARENT_SCOPE)
211endfunction()
212
213# void get_date(string* return_date);
214# - returns the current date "yyyy-mm-dd hh:mm:ss UTC"
215# - this function alone causes the need for CMake v2.8.11 (TIMESTAMP)
216#function(get_date return_date)
217# string(TIMESTAMP local_date "%Y-%m-%d %H:%M:%S UTC" UTC)
218# set(${return_date} ${local_date} PARENT_SCOPE)
219#endfunction()
220
221# void find_a_program(string program_name, list<string> extra_paths, bool fail_on_not_found, string return_variable_name);
222# - returns the full path of a program_name
223# - first looks in the list of extra_paths
224# - if not found in extra_paths, then look through system path
225# - errors out if fail_on_not_found == true and cmake could not find program_name.
226function(find_a_program program_name extra_paths fail_on_not_found return_variable_name)
227 # first try to find the program in the extra_paths
228 find_program(${return_variable_name} "${program_name}" PATHS "${extra_paths}" DOC "Path to ${program_name}" NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
229 if("${${return_variable_name}}" MATCHES NOTFOUND)
230 # if no extra_paths, or couldn't find it, then look in system $PATH
231 find_program(${return_variable_name} "${program_name}" DOC "Path to ${program_name}")
232 if("${${return_variable_name}}" MATCHES NOTFOUND AND ${fail_on_not_found})
233 error_say("Error: Could not find program: ${program_name}")
234 endif()
235 endif()
236
237 if(NOT "${${return_variable_name}}" MATCHES NOTFOUND)
238 say("-- Found ${program_name}: ${${return_variable_name}}")
239 endif()
240
241 set(${return_variable_name} ${${return_variable_name}} PARENT_SCOPE)
242endfunction()
243
244# WINDOWS SPECIFIC
245# void replace_md_with_mt(string flags_var)
246# - This macro replaces the /MD compiler flags (Windows specific) with /MT compiler flags
247# - This does nothing if no /MD flags were replaced.
248macro(replace_md_with_mt flags_var)
249 set(flags_var_name ${flags_var}) # i.e., CMAKE_C_FLAGS_RELEASE
250 set(flags_var_value ${${flags_var}}) # i.e., "/MD /O2 ..."
251 string(REPLACE /MD /MT temp_out "${flags_var_value}")
252 string(COMPARE NOTEQUAL "${temp_out}" "${flags_var_value}" something_was_replaced)
253 if("${something_was_replaced}")
254 unset(${flags_var_name} CACHE)
255 set(${flags_var_name} ${temp_out} CACHE STRING "Replaced /MD with /MT compiler flags")
256 endif()
257endmacro()
258