blob: 88e309b932473762e04db2262115145e5b124eb0 [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)
Jim Cownie3051f972014-08-07 10:12:54 +000070 elseif(${PPC64})
71 set(${return_arch_string} "PPC64" PARENT_SCOPE)
Jim Cownie3b81ce62014-08-05 09:32:28 +000072 else()
73 warning_say("set_legal_arch(): Warning: Unknown architecture...")
74 endif()
75endfunction()
76
77# void check_variable(string var, string var_name, list<string>values_list);
78# - runs through values_list checking if ${var} == values_list[i] for any i.
79# - if the var is found, then just print it out
80# - if the var is not found, then warn user
81function(check_variable var values_list)
82 set(valid_flag 0)
83 foreach(value IN LISTS values_list)
84 if("${${var}}" STREQUAL "${value}")
85 set(valid_flag 1)
86 set(the_value "${value}")
87 endif()
88 endforeach()
89 if(${valid_flag} EQUAL 0)
90 error_say("check_variable(): ${var} = ${${var}} is unknown")
91 endif()
92endfunction()
93
94# void _export_lib_dir(string export_dir, string platform, string suffix, string* return_value);
95# - basically a special case for mac platforms where it adds '.thin' to the output lib directory
96function(_export_lib_dir pltfrm return_value)
97 if(${MAC})
98 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib.thin" PARENT_SCOPE)
99 else()
100 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib" PARENT_SCOPE)
101 endif()
102endfunction()
103
104# void _export_lib_fat_dir(string export_dir, string platform, string suffix, string* return_value);
105# - another mac specialty case for fat libraries.
106# - this sets export_lib_fat_dir in the MAIN part of CMakeLists.txt
107function(_export_lib_fat_dir pltfrm return_value)
108 set(${return_value} "${export_dir}/${pltfrm}${suffix}/lib" PARENT_SCOPE)
109endfunction()
110
111# void get_build_number(string src_dir, string* return_build_number);
112# - grab the eight digit build number (or 00000000) from kmp_version.c
113function(get_build_number src_dir return_build_number)
114 # sets file_lines_list to a list of all lines in kmp_version.c
115 file(STRINGS "${src_dir}/src/kmp_version.c" file_lines_list)
116
117 # runs through each line in kmp_version.c
118 foreach(line IN LISTS file_lines_list)
119 # if the line begins with "#define KMP_VERSION_BUILD" then we take not of the build number
120 string(REGEX MATCH "^[ \t]*#define[ \t]+KMP_VERSION_BUILD" valid "${line}")
121 if(NOT "${valid}" STREQUAL "") # if we matched "#define KMP_VERSION_BUILD", then grab the build number
122 string(REGEX REPLACE "^[ \t]*#define[ \t]+KMP_VERSION_BUILD[ \t]+([0-9]+)" "\\1"
123 build_number "${line}"
124 )
125 endif()
126 endforeach()
127 set(${return_build_number} "${build_number}" PARENT_SCOPE) # return build number
128endfunction()
129
130# void set_legal_type(string* return_legal_type);
131# - set the legal type name Performance/Profiling/Stub
132function(set_legal_type return_legal_type)
133 if(${NORMAL_LIBRARY})
134 set(${return_legal_type} "Performance" PARENT_SCOPE)
135 elseif(${PROFILE_LIBRARY})
136 set(${return_legal_type} "Profiling" PARENT_SCOPE)
137 elseif(${STUBS_LIBRARY})
138 set(${return_legal_type} "Stub" PARENT_SCOPE)
139 endif()
140endfunction()
141
142# void set_mac_os_new(bool* return_mac_os_new);
143# - sets the return_mac_os_new variable to true or false based on macosx version
144# - no real "cmakey" way to do this. Have to call execute_process()
145function(set_mac_os_new return_mac_os_new)
146 execute_process(COMMAND "sw_vers" "-productVersion" OUTPUT_VARIABLE mac_osx_version)
147 if("${mac_osx_version}" VERSION_GREATER "10.6")
148 set(${return_mac_os_new} TRUE PARENT_SCOPE)
149 else()
150 set(${return_mac_os_new} FALSE PARENT_SCOPE)
151 endif()
152endfunction()
153
154# void add_prefix(string prefix, list<string>* list_of_items);
155# - returns list_of_items with prefix prepended to all items
156# - original list is modified
157function(add_prefix prefix list_of_items)
158 set(local_list "")
159 foreach(item IN LISTS "${list_of_items}")
160 if(NOT "${item}" STREQUAL "")
161 list(APPEND local_list "${prefix}${item}")
162 endif()
163 endforeach()
164 set(${list_of_items} "${local_list}" PARENT_SCOPE)
165endfunction()
166
167# void add_suffix(string suffix, list<string>* list_of_items);
168# - returns list_of_items with suffix appended to all items
169# - original list is modified
170function(add_suffix suffix list_of_items)
171 set(local_list "")
172 foreach(item IN LISTS "${list_of_items}")
173 if(NOT "${item}" STREQUAL "")
174 list(APPEND local_list "${item}${suffix}")
175 endif()
176 endforeach()
177 set(${list_of_items} "${local_list}" PARENT_SCOPE)
178endfunction()
179
180# void strip_suffix(list<string> list_of_items, list<string>* return_list);
181# - returns a new list with suffix stripped (i.e., foo.c => foo)
182# - list_of_items is not modified, return_list is modified
183function(strip_suffix list_of_items return_list)
184 set(local_list "")
185 foreach(item IN LISTS "${list_of_items}")
186 if(NOT "${item}" STREQUAL "")
187 get_filename_component(filename "${item}" NAME_WE)
188 list(APPEND local_list "${filename}")
189 endif()
190 endforeach()
191 set(${return_list} "${local_list}" PARENT_SCOPE)
192endfunction()
193
194# void list_to_string(list<string> list_of_things, string* return_string);
195# - converts a list to a space separated string
196function(list_to_string list_of_things return_string)
197 string(REPLACE ";" " " output_variable "${list_of_things}")
198 set(${return_string} "${output_variable}" PARENT_SCOPE)
199endfunction()
200
201# void string_to_list(string str, list<string>* return_list);
202# - converts a string to a semicolon separated list
203# - what it really does is just string_replace all running whitespace to a semicolon
204# - in cmake, a list is strings separated by semicolons: i.e., list of four items, list = "item1;item2;item3;item4"
205function(string_to_list str return_list)
206 set(outstr)
207 string(REGEX REPLACE "[ \t]+" ";" outstr "${str}")
208 set(${return_list} "${outstr}" PARENT_SCOPE)
209endfunction()
210
211# void get_date(string* return_date);
212# - returns the current date "yyyy-mm-dd hh:mm:ss UTC"
213# - this function alone causes the need for CMake v2.8.11 (TIMESTAMP)
214#function(get_date return_date)
215# string(TIMESTAMP local_date "%Y-%m-%d %H:%M:%S UTC" UTC)
216# set(${return_date} ${local_date} PARENT_SCOPE)
217#endfunction()
218
219# void find_a_program(string program_name, list<string> extra_paths, bool fail_on_not_found, string return_variable_name);
220# - returns the full path of a program_name
221# - first looks in the list of extra_paths
222# - if not found in extra_paths, then look through system path
223# - errors out if fail_on_not_found == true and cmake could not find program_name.
224function(find_a_program program_name extra_paths fail_on_not_found return_variable_name)
225 # first try to find the program in the extra_paths
226 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)
227 if("${${return_variable_name}}" MATCHES NOTFOUND)
228 # if no extra_paths, or couldn't find it, then look in system $PATH
229 find_program(${return_variable_name} "${program_name}" DOC "Path to ${program_name}")
230 if("${${return_variable_name}}" MATCHES NOTFOUND AND ${fail_on_not_found})
231 error_say("Error: Could not find program: ${program_name}")
232 endif()
233 endif()
234
235 if(NOT "${${return_variable_name}}" MATCHES NOTFOUND)
236 say("-- Found ${program_name}: ${${return_variable_name}}")
237 endif()
238
239 set(${return_variable_name} ${${return_variable_name}} PARENT_SCOPE)
240endfunction()
241
242# WINDOWS SPECIFIC
243# void replace_md_with_mt(string flags_var)
244# - This macro replaces the /MD compiler flags (Windows specific) with /MT compiler flags
245# - This does nothing if no /MD flags were replaced.
246macro(replace_md_with_mt flags_var)
247 set(flags_var_name ${flags_var}) # i.e., CMAKE_C_FLAGS_RELEASE
248 set(flags_var_value ${${flags_var}}) # i.e., "/MD /O2 ..."
249 string(REPLACE /MD /MT temp_out "${flags_var_value}")
250 string(COMPARE NOTEQUAL "${temp_out}" "${flags_var_value}" something_was_replaced)
251 if("${something_was_replaced}")
252 unset(${flags_var_name} CACHE)
253 set(${flags_var_name} ${temp_out} CACHE STRING "Replaced /MD with /MT compiler flags")
254 endif()
255endmacro()
256