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