blob: 908e7ee51ca8f652b5cbaa8b7da3f1cca65bd19a [file] [log] [blame]
Oscar Fuentes751ea9d2008-11-14 22:06:14 +00001include(LLVMProcessSources)
Oscar Fuentesd8a6dd62011-04-05 17:02:48 +00002include(LLVM-Config)
John Brawnc23801d2015-09-29 14:33:58 +00003include(DetermineGCCCompatible)
Oscar Fuentesa229b3c2008-09-22 01:08:49 +00004
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00005function(llvm_update_compile_flags name)
NAKAMURA Takumib524c222014-01-28 11:40:04 +00006 get_property(sources TARGET ${name} PROPERTY SOURCES)
7 if("${sources}" MATCHES "\\.c(;|$)")
NAKAMURA Takumia679f432014-01-28 09:44:06 +00008 set(update_src_props ON)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00009 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000010
Reid Kleckner8c180192016-03-30 17:28:21 +000011 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to
12 # force EH
13 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
Dan Liewa5bdc842014-07-22 15:41:18 +000014 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
15 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
16 set(LLVM_REQUIRES_RTTI ON)
17 endif()
Andrey Bokhankob7201cc2016-09-01 14:39:54 +000018 if(MSVC)
19 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
20 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000021 else()
22 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000023 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000024 elseif(MSVC)
25 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000026 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000027 endif()
28 endif()
29
Dan Liewa5bdc842014-07-22 15:41:18 +000030 # LLVM_REQUIRES_RTTI is an internal flag that individual
31 # targets can use to force RTTI
Tom Stellard18bf6262015-11-04 20:57:43 +000032 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
Dan Liewa5bdc842014-07-22 15:41:18 +000033 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
Tom Stellard18bf6262015-11-04 20:57:43 +000034 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000035 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
36 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000037 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000038 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000039 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000040 endif ()
Andrey Bokhankob7201cc2016-09-01 14:39:54 +000041 elseif(MSVC)
42 list(APPEND LLVM_COMPILE_FLAGS "/GR")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000043 endif()
44
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000045 # Assume that;
46 # - LLVM_COMPILE_FLAGS is list.
47 # - PROPERTY COMPILE_FLAGS is string.
Andrew Wilkins92113962015-09-01 03:14:31 +000048 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000049
NAKAMURA Takumia679f432014-01-28 09:44:06 +000050 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000051 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000052 get_filename_component(suf ${fn} EXT)
53 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000054 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
55 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000056 endif()
57 endforeach()
58 else()
59 # Update target props, since all sources are C++.
60 set_property(TARGET ${name} APPEND_STRING PROPERTY
61 COMPILE_FLAGS "${target_compile_flags}")
62 endif()
63
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000064 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
65endfunction()
66
Nico Weberc27118d2013-12-28 23:31:44 +000067function(add_llvm_symbol_exports target_name export_file)
68 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000069 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000070 add_custom_command(OUTPUT ${native_export_file}
71 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000072 DEPENDS ${export_file}
73 VERBATIM
74 COMMENT "Creating export file for ${target_name}")
75 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000076 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Chandler Carruth2aff7502016-07-19 22:46:39 +000077 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
78 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
79 LINK_FLAGS " -Wl,-bE:${export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000080 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
81 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000082 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000083 # FIXME: Don't write the "local:" line on OpenBSD.
Sylvestre Ledrub0cec312017-04-17 20:51:50 +000084 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
NAKAMURA Takumi81210752013-12-29 16:15:18 +000085 add_custom_command(OUTPUT ${native_export_file}
Sylvestre Ledrub0cec312017-04-17 20:51:50 +000086 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} {" > ${native_export_file}
NAKAMURA Takumi81210752013-12-29 16:15:18 +000087 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
88 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
89 COMMAND echo " local: *;" >> ${native_export_file}
90 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000091 DEPENDS ${export_file}
92 VERBATIM
93 COMMENT "Creating export file for ${target_name}")
Rui Ueyama845b9812017-07-12 21:43:14 +000094 if (${LLVM_LINKER_IS_SOLARISLD})
Rafael Espindolaec1d0692015-06-22 12:34:54 +000095 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
96 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
97 else()
98 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
99 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
100 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000101 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +0000102 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +0000103
NAKAMURA Takumi81210752013-12-29 16:15:18 +0000104 add_custom_command(OUTPUT ${native_export_file}
NAKAMURA Takumi3eb06662015-07-05 08:56:38 +0000105 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
106 < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000107 DEPENDS ${export_file}
108 VERBATIM
109 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000110 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
111 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000112 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000113 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000114 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
115 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000116 endif()
117
118 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000119 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000120
121 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
122 foreach(src ${srcs})
123 get_filename_component(extension ${src} EXT)
124 if(extension STREQUAL ".cpp")
125 set(first_source_file ${src})
126 break()
127 endif()
128 endforeach()
129
130 # Force re-linking when the exports file changes. Actually, it
131 # forces recompilation of the source file. The LINK_DEPENDS target
132 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000133 # FIXME: This is not safe because this will create the same target
134 # ${native_export_file} in several different file:
135 # - One where we emitted ${target_name}_exports
136 # - One where we emitted the build command for the following object.
137 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
138 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000139
140 set_property(DIRECTORY APPEND
141 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
142
143 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000144
145 # Add dependency to *_exports later -- CMake issue 14747
146 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
147 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000148endfunction(add_llvm_symbol_exports)
149
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000150if(NOT WIN32 AND NOT APPLE)
Rui Ueyama845b9812017-07-12 21:43:14 +0000151 # Detect what linker we have here
Tim Shen5a166042017-10-30 21:12:14 +0000152 if( LLVM_USE_LINKER )
153 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
154 else()
155 set(command ${CMAKE_C_COMPILER} -Wl,--version)
156 endif()
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000157 execute_process(
Tim Shen5a166042017-10-30 21:12:14 +0000158 COMMAND ${command}
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000159 OUTPUT_VARIABLE stdout
Rui Ueyama845b9812017-07-12 21:43:14 +0000160 ERROR_VARIABLE stderr
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000161 )
Rui Ueyama845b9812017-07-12 21:43:14 +0000162 set(LLVM_LINKER_DETECTED ON)
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000163 if("${stdout}" MATCHES "GNU gold")
164 set(LLVM_LINKER_IS_GOLD ON)
Rui Ueyama845b9812017-07-12 21:43:14 +0000165 message(STATUS "Linker detection: GNU Gold")
166 elseif("${stdout}" MATCHES "^LLD")
167 set(LLVM_LINKER_IS_LLD ON)
168 message(STATUS "Linker detection: LLD")
169 elseif("${stdout}" MATCHES "GNU ld")
170 set(LLVM_LINKER_IS_GNULD ON)
171 message(STATUS "Linker detection: GNU ld")
Michal Gorny0eaaf272017-11-17 17:12:14 +0000172 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
173 "${stdout}" MATCHES "Solaris Link Editors")
Rui Ueyama845b9812017-07-12 21:43:14 +0000174 set(LLVM_LINKER_IS_SOLARISLD ON)
175 message(STATUS "Linker detection: Solaris ld")
176 else()
177 set(LLVM_LINKER_DETECTED OFF)
178 message(STATUS "Linker detection: unknown")
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000179 endif()
180endif()
181
Rafael Espindola8cb78762014-11-02 12:14:22 +0000182function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000183 # Don't use linker optimizations in debug builds since it slows down the
184 # linker in a context where the optimizations are not important.
185 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000186
Rafael Espindola24766f92015-04-06 15:04:31 +0000187 # Pass -O3 to the linker. This enabled different optimizations on different
188 # linkers.
Chandler Carruth2aff7502016-07-19 22:46:39 +0000189 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000190 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000191 LINK_FLAGS " -Wl,-O3")
192 endif()
193
194 if(LLVM_LINKER_IS_GOLD)
195 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000196 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
197 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000198 # Note that there is a bug with -Wl,--icf=safe so it is not safe
199 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
200 endif()
201
202 if(NOT LLVM_NO_DEAD_STRIP)
203 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
204 # ld64's implementation of -dead_strip breaks tools that use plugins.
205 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
206 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindolac32ed572015-06-22 15:06:17 +0000207 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
208 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
209 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000210 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
211 # Object files are compiled with -ffunction-data-sections.
212 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
213 # tools that use plugins. Always pass --gc-sections once we require
214 # a newer linker.
215 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
216 LINK_FLAGS " -Wl,--gc-sections")
217 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000218 endif()
219 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000220endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000221
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000222# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
Brian Gesiak615a3bb2017-09-29 19:50:41 +0000223# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
224# or a certain builder, for eaxample, msbuild.exe, would be confused.
John Brawnc11ef2a2015-09-30 15:20:51 +0000225function(set_output_directory target)
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +0000226 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000227
John Brawnc11ef2a2015-09-30 15:20:51 +0000228 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000229 # It affects output of add_library(MODULE).
230 if(WIN32 OR CYGWIN)
231 # DLL platform
John Brawnc11ef2a2015-09-30 15:20:51 +0000232 set(module_dir ${ARG_BINARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000233 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000234 set(module_dir ${ARG_LIBRARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000235 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000236 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
237 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
238 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
John Brawnc11ef2a2015-09-30 15:20:51 +0000239 if(ARG_BINARY_DIR)
240 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
241 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
242 endif()
243 if(ARG_LIBRARY_DIR)
244 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
245 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
246 endif()
247 if(module_dir)
248 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
249 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
250 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000251 endforeach()
252 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000253 if(ARG_BINARY_DIR)
254 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
255 endif()
256 if(ARG_LIBRARY_DIR)
257 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
258 endif()
259 if(module_dir)
260 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
261 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000262 endif()
263endfunction()
264
Greg Bedwell95213c32015-06-12 15:58:29 +0000265# If on Windows and building with MSVC, add the resource script containing the
266# VERSIONINFO data to the project. This embeds version resource information
267# into the output .exe or .dll.
268# TODO: Enable for MinGW Windows builds too.
269#
270function(add_windows_version_resource_file OUT_VAR)
271 set(sources ${ARGN})
Shoaib Meenai63914582017-10-25 17:11:28 +0000272 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
Greg Bedwell95213c32015-06-12 15:58:29 +0000273 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000274 if(EXISTS ${resource_file})
275 set(sources ${sources} ${resource_file})
276 source_group("Resource Files" ${resource_file})
277 set(windows_resource_file ${resource_file} PARENT_SCOPE)
278 endif()
Shoaib Meenai63914582017-10-25 17:11:28 +0000279 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
Greg Bedwell95213c32015-06-12 15:58:29 +0000280
281 set(${OUT_VAR} ${sources} PARENT_SCOPE)
282endfunction(add_windows_version_resource_file)
283
284# set_windows_version_resource_properties(name resource_file...
285# VERSION_MAJOR int
286# Optional major version number (defaults to LLVM_VERSION_MAJOR)
287# VERSION_MINOR int
288# Optional minor version number (defaults to LLVM_VERSION_MINOR)
289# VERSION_PATCHLEVEL int
290# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
291# VERSION_STRING
292# Optional version string (defaults to PACKAGE_VERSION)
293# PRODUCT_NAME
294# Optional product name string (defaults to "LLVM")
295# )
296function(set_windows_version_resource_properties name resource_file)
297 cmake_parse_arguments(ARG
298 ""
299 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
300 ""
301 ${ARGN})
302
303 if (NOT DEFINED ARG_VERSION_MAJOR)
304 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
305 endif()
306
307 if (NOT DEFINED ARG_VERSION_MINOR)
308 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
309 endif()
310
311 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
312 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
313 endif()
314
315 if (NOT DEFINED ARG_VERSION_STRING)
316 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
317 endif()
318
319 if (NOT DEFINED ARG_PRODUCT_NAME)
320 set(ARG_PRODUCT_NAME "LLVM")
321 endif()
322
323 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000324 PROPERTY COMPILE_FLAGS /nologo)
325 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000326 PROPERTY COMPILE_DEFINITIONS
327 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
328 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
329 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
330 "RC_VERSION_FIELD_4=0"
331 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
332 "RC_INTERNAL_NAME=\"${name}\""
333 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
334 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
335endfunction(set_windows_version_resource_properties)
336
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000337# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000338# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000339# STATIC by default w/o BUILD_SHARED_LIBS.
340# SHARED by default w/ BUILD_SHARED_LIBS.
Axel Naumannb457beb2016-01-12 07:44:58 +0000341# OBJECT
342# Also create an OBJECT library target. Default if STATIC && SHARED.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000343# MODULE
344# Target ${name} might not be created on unsupported platforms.
345# Check with "if(TARGET ${name})".
Andrew Wilkins92113962015-09-01 03:14:31 +0000346# DISABLE_LLVM_LINK_LLVM_DYLIB
347# Do not link this library to libLLVM, even if
348# LLVM_LINK_LLVM_DYLIB is enabled.
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000349# OUTPUT_NAME name
350# Corresponds to OUTPUT_NAME in target properties.
351# DEPENDS targets...
352# Same semantics as add_dependencies().
353# LINK_COMPONENTS components...
354# Same as the variable LLVM_LINK_COMPONENTS.
355# LINK_LIBS lib_targets...
356# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000357# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000358# May specify header files for IDE generators.
Chris Bieneman5e96fe92015-11-04 23:11:12 +0000359# SONAME
360# Should set SONAME link flags and create symlinks
John Brawn3546c2f2016-05-26 11:16:43 +0000361# PLUGIN_TOOL
362# The tool (i.e. cmake target) that this plugin will link against
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000363# )
364function(llvm_add_library name)
365 cmake_parse_arguments(ARG
Axel Naumannb457beb2016-01-12 07:44:58 +0000366 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
John Brawn3546c2f2016-05-26 11:16:43 +0000367 "OUTPUT_NAME;PLUGIN_TOOL"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000368 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000369 ${ARGN})
370 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000371 if(ARG_ADDITIONAL_HEADERS)
372 # Pass through ADDITIONAL_HEADERS.
373 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
374 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000375 if(ARG_OBJLIBS)
376 set(ALL_FILES ${ARG_OBJLIBS})
377 else()
378 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
379 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000380
381 if(ARG_MODULE)
382 if(ARG_SHARED OR ARG_STATIC)
383 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
384 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000385 # Plugins that link against a tool are allowed even when plugins in general are not
386 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000387 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
388 return()
389 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000390 else()
John Brawn3546c2f2016-05-26 11:16:43 +0000391 if(ARG_PLUGIN_TOOL)
392 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
393 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000394 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
395 set(ARG_SHARED TRUE)
396 endif()
397 if(NOT ARG_SHARED)
398 set(ARG_STATIC TRUE)
399 endif()
400 endif()
401
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000402 # Generate objlib
Axel Naumannb457beb2016-01-12 07:44:58 +0000403 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000404 # Generate an obj library for both targets.
405 set(obj_name "obj.${name}")
406 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
407 ${ALL_FILES}
408 )
409 llvm_update_compile_flags(${obj_name})
410 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
411
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000412 # Do add_dependencies(obj) later due to CMake issue 14747.
413 list(APPEND objlibs ${obj_name})
414
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000415 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
416 endif()
417
418 if(ARG_SHARED AND ARG_STATIC)
419 # static
420 set(name_static "${name}_static")
421 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000422 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000423 endif()
424 # DEPENDS has been appended to LLVM_COMMON_LIBS.
425 llvm_add_library(${name_static} STATIC
426 ${output_name}
427 OBJLIBS ${ALL_FILES} # objlib
428 LINK_LIBS ${ARG_LINK_LIBS}
429 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
430 )
431 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
432 set(ARG_STATIC)
433 endif()
434
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000435 if(ARG_MODULE)
436 add_library(${name} MODULE ${ALL_FILES})
Peter Collingbourneb192a202016-11-28 21:59:14 +0000437 llvm_setup_rpath(${name})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000438 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000439 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000440 add_library(${name} SHARED ${ALL_FILES})
Chris Bieneman61e900b2016-11-01 17:44:58 +0000441
442 llvm_setup_rpath(${name})
443
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000444 else()
445 add_library(${name} STATIC ${ALL_FILES})
446 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000447
Chris Bieneman958edcb2016-11-19 01:32:09 +0000448 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
449
Greg Bedwell95213c32015-06-12 15:58:29 +0000450 if(DEFINED windows_resource_file)
451 set_windows_version_resource_properties(${name} ${windows_resource_file})
452 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
453 endif()
454
John Brawnc11ef2a2015-09-30 15:20:51 +0000455 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000456 # $<TARGET_OBJECTS> doesn't require compile flags.
457 if(NOT obj_name)
458 llvm_update_compile_flags(${name})
459 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000460 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000461 if(ARG_OUTPUT_NAME)
462 set_target_properties(${name}
463 PROPERTIES
464 OUTPUT_NAME ${ARG_OUTPUT_NAME}
465 )
466 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000467
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000468 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000469 set_target_properties(${name} PROPERTIES
470 PREFIX ""
471 SUFFIX ${LLVM_PLUGIN_EXT}
472 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000473 endif()
474
475 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000476 if(WIN32)
477 set_target_properties(${name} PROPERTIES
478 PREFIX ""
479 )
480 endif()
Michal Gorny6b929d52016-10-04 06:09:18 +0000481
482 # Set SOVERSION on shared libraries that lack explicit SONAME
483 # specifier, on *nix systems that are not Darwin.
484 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
485 set_target_properties(${name}
486 PROPERTIES
Michal Gorny60831912017-01-17 21:04:19 +0000487 # Since 4.0.0, the ABI version is indicated by the major version
488 SOVERSION ${LLVM_VERSION_MAJOR}
489 VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
Michal Gorny6b929d52016-10-04 06:09:18 +0000490 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000491 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000492
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000493 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000494 # Do not add -Dname_EXPORTS to the command-line when building files in this
495 # target. Doing so is actively harmful for the modules build because it
496 # creates extra module variants, and not useful because we don't use these
497 # macros.
498 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
499
Nico Weber06473f82013-12-29 05:39:01 +0000500 if (LLVM_EXPORTED_SYMBOL_FILE)
501 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
502 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000503 endif()
504
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000505 if(ARG_SHARED AND UNIX)
506 if(NOT APPLE AND ARG_SONAME)
507 get_target_property(output_name ${name} OUTPUT_NAME)
508 if(${output_name} STREQUAL "output_name-NOTFOUND")
509 set(output_name ${name})
510 endif()
511 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
512 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
513 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
514 llvm_install_library_symlink(${api_name} ${library_name} SHARED
515 COMPONENT ${name}
516 ALWAYS_GENERATE)
517 llvm_install_library_symlink(${output_name} ${library_name} SHARED
518 COMPONENT ${name}
519 ALWAYS_GENERATE)
520 endif()
521 endif()
522
John Brawn3546c2f2016-05-26 11:16:43 +0000523 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
524 # On DLL platforms symbols are imported from the tool by linking against it.
525 set(llvm_libs ${ARG_PLUGIN_TOOL})
526 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000527 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
528 set(llvm_libs LLVM)
529 else()
530 llvm_map_components_to_libnames(llvm_libs
531 ${ARG_LINK_COMPONENTS}
532 ${LLVM_LINK_COMPONENTS}
533 )
534 endif()
Andrew Wilkins92113962015-09-01 03:14:31 +0000535 else()
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000536 # Components have not been defined explicitly in CMake, so add the
537 # dependency information for this library as defined by LLVMBuild.
538 #
539 # It would be nice to verify that we have the dependencies for this library
540 # name, but using get_property(... SET) doesn't suffice to determine if a
541 # property has been set to an empty value.
542 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
Andrew Wilkins92113962015-09-01 03:14:31 +0000543 endif()
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000544
Chris Bieneman6650b022016-06-08 22:48:01 +0000545 if(ARG_STATIC)
546 set(libtype INTERFACE)
Steven Wuec6f56e2016-05-26 04:35:35 +0000547 else()
548 # We can use PRIVATE since SO knows its dependent libs.
Chris Bieneman6650b022016-06-08 22:48:01 +0000549 set(libtype PRIVATE)
550 endif()
551
552 target_link_libraries(${name} ${libtype}
Steven Wuec6f56e2016-05-26 04:35:35 +0000553 ${ARG_LINK_LIBS}
554 ${lib_deps}
555 ${llvm_libs}
556 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000557
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000558 if(LLVM_COMMON_DEPENDS)
559 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000560 # Add dependencies also to objlibs.
561 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
562 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000563 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
564 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000565 endif()
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000566
567 if(ARG_SHARED OR ARG_MODULE)
568 llvm_externalize_debuginfo(${name})
569 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000570endfunction()
571
572macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000573 cmake_parse_arguments(ARG
Michael Gottesman31e93632016-09-09 19:45:34 +0000574 "SHARED;BUILDTREE_ONLY"
Chris Bieneman84271692015-04-16 16:56:18 +0000575 ""
576 ""
577 ${ARGN})
Michael Gottesman31e93632016-09-09 19:45:34 +0000578 if( BUILD_SHARED_LIBS OR ARG_SHARED )
579 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000580 else()
Michael Gottesman31e93632016-09-09 19:45:34 +0000581 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000582 endif()
Michael Gottesman31e93632016-09-09 19:45:34 +0000583
584 # Libraries that are meant to only be exposed via the build tree only are
585 # never installed and are only exported as a target in the special build tree
586 # config file.
587 if (NOT ARG_BUILDTREE_ONLY)
Dan Liew80189d22015-06-29 18:45:56 +0000588 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
589 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000590
Nick Lewycky61aed872011-04-29 02:12:06 +0000591 if( EXCLUDE_FROM_ALL )
592 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Michael Gottesman31e93632016-09-09 19:45:34 +0000593 elseif(ARG_BUILDTREE_ONLY)
594 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
595 else()
Derek Schuffeb224632016-09-16 18:50:39 +0000596 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR
597 (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM"))
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000598 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000599 if(ARG_SHARED OR BUILD_SHARED_LIBS)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000600 if(WIN32 OR CYGWIN OR MINGW)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000601 set(install_type RUNTIME)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000602 set(install_dir bin)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000603 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000604 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000605 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000606 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000607 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000608 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000609
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000610 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
611 NOT LLVM_DISTRIBUTION_COMPONENTS)
612 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000613 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000614 endif()
615
Chris Bienemana985e6b2015-04-16 18:08:33 +0000616 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000617 ${export_to_llvmexports}
618 ${install_type} DESTINATION ${install_dir}
619 COMPONENT ${name})
Chris Bienemana985e6b2015-04-16 18:08:33 +0000620
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000621 if (NOT CMAKE_CONFIGURATION_TYPES)
622 add_custom_target(install-${name}
623 DEPENDS ${name}
624 COMMAND "${CMAKE_COMMAND}"
625 -DCMAKE_INSTALL_COMPONENT=${name}
626 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
627 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000628 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000629 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000630 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000631 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000632endmacro(add_llvm_library name)
633
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000634macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000635 llvm_add_library(${name} MODULE ${ARGN})
636 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000637 # Add empty "phony" target
638 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000639 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000640 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000641 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000642 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000643 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000644 if(WIN32 OR CYGWIN)
645 # DLL platform
646 set(dlldir "bin")
647 else()
648 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
649 endif()
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000650
651 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
652 NOT LLVM_DISTRIBUTION_COMPONENTS)
653 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000654 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000655 endif()
656
Hans Wennborg16546272013-08-24 00:20:36 +0000657 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000658 ${export_to_llvmexports}
659 LIBRARY DESTINATION ${dlldir}
660 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Hans Wennborg16546272013-08-24 00:20:36 +0000661 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000662 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000663 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000664 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000665
666 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000667endmacro(add_llvm_loadable_module name)
668
669
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000670macro(add_llvm_executable name)
Chris Bieneman8036e0b2016-11-17 04:36:59 +0000671 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" "" "DEPENDS" ${ARGN})
Andrew Wilkins92113962015-09-01 03:14:31 +0000672 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000673
Chris Bieneman8036e0b2016-11-17 04:36:59 +0000674 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
675
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +0000676 # Generate objlib
677 if(LLVM_ENABLE_OBJLIB)
678 # Generate an obj library for both targets.
679 set(obj_name "obj.${name}")
680 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
681 ${ALL_FILES}
682 )
683 llvm_update_compile_flags(${obj_name})
684 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
685
686 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
687 endif()
688
NAKAMURA Takumib5288ba2015-08-28 00:36:58 +0000689 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
690
Jordan Rose3d752972015-09-10 17:18:51 +0000691 if(XCODE)
692 # Note: the dummy.cpp source file provides no definitions. However,
693 # it forces Xcode to properly link the static library.
Jordan Rose85a22f82015-09-10 17:55:02 +0000694 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
Jordan Rose3d752972015-09-10 17:18:51 +0000695 endif()
Zachary Turnerce92db12017-09-15 22:10:46 +0000696
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000697 if( EXCLUDE_FROM_ALL )
698 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
699 else()
700 add_executable(${name} ${ALL_FILES})
701 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000702
Chris Bieneman958edcb2016-11-19 01:32:09 +0000703 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
704
Jonas Hahnfeld277a09d2016-11-03 06:58:16 +0000705 if(NOT ARG_NO_INSTALL_RPATH)
706 llvm_setup_rpath(${name})
707 endif()
Chris Bieneman61e900b2016-11-01 17:44:58 +0000708
Greg Bedwell95213c32015-06-12 15:58:29 +0000709 if(DEFINED windows_resource_file)
710 set_windows_version_resource_properties(${name} ${windows_resource_file})
711 endif()
712
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000713 # $<TARGET_OBJECTS> doesn't require compile flags.
714 if(NOT LLVM_ENABLE_OBJLIB)
715 llvm_update_compile_flags(${name})
716 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000717 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000718
Richard Smith2b91a7f2014-09-26 22:40:15 +0000719 # Do not add -Dname_EXPORTS to the command-line when building files in this
720 # target. Doing so is actively harmful for the modules build because it
721 # creates extra module variants, and not useful because we don't use these
722 # macros.
723 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
724
Nico Weberc27118d2013-12-28 23:31:44 +0000725 if (LLVM_EXPORTED_SYMBOL_FILE)
726 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
727 endif(LLVM_EXPORTED_SYMBOL_FILE)
728
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000729 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
730 set(USE_SHARED USE_SHARED)
731 endif()
732
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000733 set(EXCLUDE_FROM_ALL OFF)
John Brawnc11ef2a2015-09-30 15:20:51 +0000734 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000735 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000736 if( LLVM_COMMON_DEPENDS )
737 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
738 endif( LLVM_COMMON_DEPENDS )
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000739
Chris Bieneman48eaa542015-12-08 21:51:48 +0000740 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
741 llvm_externalize_debuginfo(${name})
742 endif()
Eric Fiselier87c87f42017-02-10 01:59:20 +0000743 if (LLVM_PTHREAD_LIB)
Artem Belevichdf7103d2016-06-21 19:34:40 +0000744 # libpthreads overrides some standard library symbols, so main
745 # executable must be linked with it in order to provide consistent
746 # API for all shared libaries loaded by this executable.
Eric Fiselier87c87f42017-02-10 01:59:20 +0000747 target_link_libraries(${name} ${LLVM_PTHREAD_LIB})
Artem Belevichdf7103d2016-06-21 19:34:40 +0000748 endif()
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000749endmacro(add_llvm_executable name)
750
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000751function(export_executable_symbols target)
John Brawn3546c2f2016-05-26 11:16:43 +0000752 if (LLVM_EXPORTED_SYMBOL_FILE)
753 # The symbol file should contain the symbols we want the executable to
754 # export
755 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
756 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
757 # Extract the symbols to export from the static libraries that the
758 # executable links against.
759 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
760 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
761 # We need to consider not just the direct link dependencies, but also the
762 # transitive link dependencies. Do this by starting with the set of direct
763 # dependencies, then the dependencies of those dependencies, and so on.
764 get_target_property(new_libs ${target} LINK_LIBRARIES)
765 set(link_libs ${new_libs})
766 while(NOT "${new_libs}" STREQUAL "")
767 foreach(lib ${new_libs})
John Brawn24ca18e2016-07-05 13:16:54 +0000768 if(TARGET ${lib})
769 get_target_property(lib_type ${lib} TYPE)
770 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
771 list(APPEND static_libs ${lib})
772 else()
773 list(APPEND other_libs ${lib})
John Brawn3546c2f2016-05-26 11:16:43 +0000774 endif()
John Brawn24ca18e2016-07-05 13:16:54 +0000775 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
776 foreach(transitive_lib ${transitive_libs})
777 list(FIND link_libs ${transitive_lib} idx)
778 if(TARGET ${transitive_lib} AND idx EQUAL -1)
779 list(APPEND newer_libs ${transitive_lib})
780 list(APPEND link_libs ${transitive_lib})
781 endif()
782 endforeach(transitive_lib)
783 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000784 endforeach(lib)
785 set(new_libs ${newer_libs})
786 set(newer_libs "")
787 endwhile()
788 if (MSVC)
789 set(mangling microsoft)
790 else()
791 set(mangling itanium)
792 endif()
793 add_custom_command(OUTPUT ${exported_symbol_file}
794 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
795 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
796 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
797 VERBATIM
798 COMMENT "Generating export list for ${target}")
799 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
800 # If something links against this executable then we want a
801 # transitive link against only the libraries whose symbols
802 # we aren't exporting.
803 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
804 # The default import library suffix that cmake uses for cygwin/mingw is
805 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
806 # where the import libraries of both get named libclang.dll.a. Use a suffix
807 # of ".exe.a" to avoid this.
808 if(CYGWIN OR MINGW)
809 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
810 endif()
811 elseif(NOT (WIN32 OR CYGWIN))
812 # On Windows auto-exporting everything doesn't work because of the limit on
813 # the size of the exported symbol table, but on other platforms we can do
814 # it without any trouble.
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000815 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
Chris Bieneman914742b2015-11-24 00:58:58 +0000816 if (APPLE)
Chris Bienemanccabd0e2015-12-03 22:51:08 +0000817 set_property(TARGET ${target} APPEND_STRING PROPERTY
Chris Bienemanac6677a2015-12-03 22:55:36 +0000818 LINK_FLAGS " -rdynamic")
Chris Bieneman914742b2015-11-24 00:58:58 +0000819 endif()
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000820 endif()
821endfunction()
822
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000823if(NOT LLVM_TOOLCHAIN_TOOLS)
824 set (LLVM_TOOLCHAIN_TOOLS
825 llvm-ar
Chris Bieneman6d43afc2015-09-14 23:09:06 +0000826 llvm-ranlib
827 llvm-lib
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000828 llvm-objdump
829 )
830endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000831
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000832macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000833 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000834 set(EXCLUDE_FROM_ALL ON)
835 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000836 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000837
Chris Bieneman44d29902016-09-27 17:47:24 +0000838 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Hans Wennborg16546272013-08-24 00:20:36 +0000839 if( LLVM_BUILD_TOOLS )
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000840 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
841 NOT LLVM_DISTRIBUTION_COMPONENTS)
842 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000843 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000844 endif()
845
NAKAMURA Takumief976072014-02-09 16:36:03 +0000846 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000847 ${export_to_llvmexports}
Chris Bieneman2724f142016-06-08 21:19:26 +0000848 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000849 COMPONENT ${name})
850
851 if (NOT CMAKE_CONFIGURATION_TYPES)
852 add_custom_target(install-${name}
853 DEPENDS ${name}
854 COMMAND "${CMAKE_COMMAND}"
855 -DCMAKE_INSTALL_COMPONENT=${name}
856 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
857 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000858 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000859 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000860 if( LLVM_BUILD_TOOLS )
861 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
862 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000863 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000864endmacro(add_llvm_tool name)
865
866
867macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000868 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000869 set(EXCLUDE_FROM_ALL ON)
870 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000871 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000872 if( LLVM_BUILD_EXAMPLES )
873 install(TARGETS ${name} RUNTIME DESTINATION examples)
874 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000875 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000876endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000877
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000878# This is a macro that is used to create targets for executables that are needed
879# for development, but that are not intended to be installed by default.
Oscar Fuentes3145e922011-02-20 22:06:10 +0000880macro(add_llvm_utility name)
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000881 if ( NOT LLVM_BUILD_UTILS )
882 set(EXCLUDE_FROM_ALL ON)
883 endif()
884
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000885 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
Oscar Fuentes3145e922011-02-20 22:06:10 +0000886 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000887 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
Derek Schuffef9928e2015-03-27 16:53:06 +0000888 install (TARGETS ${name}
Tom Stellardedd69bc2017-07-05 12:57:30 +0000889 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
Derek Schuffef9928e2015-03-27 16:53:06 +0000890 COMPONENT ${name})
891 if (NOT CMAKE_CONFIGURATION_TYPES)
892 add_custom_target(install-${name}
893 DEPENDS ${name}
894 COMMAND "${CMAKE_COMMAND}"
895 -DCMAKE_INSTALL_COMPONENT=${name}
896 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
897 endif()
898 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000899endmacro(add_llvm_utility name)
900
Justin Bogner52047172017-08-31 00:36:33 +0000901macro(add_llvm_fuzzer name)
Justin Bognerc3b67fb2017-09-01 17:02:22 +0000902 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
Matt Morehouse54368662017-10-12 21:58:41 +0000903 if( LLVM_LIB_FUZZING_ENGINE )
904 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
905 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
906 target_link_libraries(${name} ${LLVM_LIB_FUZZING_ENGINE})
907 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
908 elseif( LLVM_USE_SANITIZE_COVERAGE )
Justin Bogner52047172017-08-31 00:36:33 +0000909 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
Justin Bogner7f28d732017-09-02 23:43:04 +0000910 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
Justin Bognerc3b67fb2017-09-01 17:02:22 +0000911 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
Justin Bogner52047172017-08-31 00:36:33 +0000912 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
Justin Bognerc3b67fb2017-09-01 17:02:22 +0000913 elseif( ARG_DUMMY_MAIN )
914 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
915 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
Matt Morehouse54368662017-10-12 21:58:41 +0000916 endif()
Justin Bogner52047172017-08-31 00:36:33 +0000917endmacro()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000918
Oscar Fuentescdc95492008-09-26 04:40:32 +0000919macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000920 include_directories(BEFORE
921 ${CMAKE_CURRENT_BINARY_DIR}
922 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000923 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000924 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000925endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000926
Chris Bieneman580d8152015-07-20 20:36:06 +0000927function(canonicalize_tool_name name output)
928 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
929 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
930 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
931 set(${output} "${nameUPPER}" PARENT_SCOPE)
932endfunction(canonicalize_tool_name)
933
Chris Bieneman70997c32015-10-20 16:42:58 +0000934# Custom add_subdirectory wrapper
Tobias Grossere3421b52016-04-29 13:03:40 +0000935# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
936# path if it differs from the name.
Chris Bieneman70997c32015-10-20 16:42:58 +0000937macro(add_llvm_subdirectory project type name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000938 set(add_llvm_external_dir "${ARGN}")
939 if("${add_llvm_external_dir}" STREQUAL "")
940 set(add_llvm_external_dir ${name})
941 endif()
Chris Bieneman580d8152015-07-20 20:36:06 +0000942 canonicalize_tool_name(${name} nameUPPER)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000943 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
944 # Treat it as in-tree subproject.
Chris Bieneman70997c32015-10-20 16:42:58 +0000945 option(${project}_${type}_${nameUPPER}_BUILD
946 "Whether to build ${name} as part of ${project}" On)
947 mark_as_advanced(${project}_${type}_${name}_BUILD)
948 if(${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000949 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
950 # Don't process it in add_llvm_implicit_projects().
Chris Bieneman70997c32015-10-20 16:42:58 +0000951 set(${project}_${type}_${nameUPPER}_BUILD OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000952 endif()
953 else()
954 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
955 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
956 CACHE PATH "Path to ${name} source directory")
Chris Bieneman70997c32015-10-20 16:42:58 +0000957 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000958 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
Chris Bieneman70997c32015-10-20 16:42:58 +0000959 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000960 endif()
961 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
Chris Bieneman70997c32015-10-20 16:42:58 +0000962 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000963 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000964 option(${project}_${type}_${nameUPPER}_BUILD
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000965 "Whether to build ${name} as part of LLVM"
Chris Bieneman70997c32015-10-20 16:42:58 +0000966 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
967 if (${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi6fa85322015-08-22 05:11:02 +0000968 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
969 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
970 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
971 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
972 endif()
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000973 # FIXME: It'd be redundant.
Chris Bieneman70997c32015-10-20 16:42:58 +0000974 set(${project}_${type}_${nameUPPER}_BUILD Off)
Michael J. Spencere734f542012-04-26 19:43:35 +0000975 endif()
976 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000977endmacro()
978
979# Add external project that may want to be built as part of llvm such as Clang,
980# lld, and Polly. This adds two options. One for the source directory of the
981# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
982# enable or disable building it with everything else.
983# Additional parameter can be specified as the name of directory.
984macro(add_llvm_external_project name)
Manuel Klimekf80abb92015-10-22 08:31:46 +0000985 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
Chris Bieneman70997c32015-10-20 16:42:58 +0000986endmacro()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000987
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000988macro(add_llvm_tool_subdirectory name)
Chris Bieneman580d8152015-07-20 20:36:06 +0000989 add_llvm_external_project(${name})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000990endmacro(add_llvm_tool_subdirectory)
991
Chris Bieneman580d8152015-07-20 20:36:06 +0000992function(get_project_name_from_src_var var output)
993 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
994 MACHED_TOOL "${var}")
995 if(MACHED_TOOL)
996 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
997 else()
998 set(${output} PARENT_SCOPE)
999 endif()
1000endfunction()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +00001001
Chris Bieneman70997c32015-10-20 16:42:58 +00001002function(create_subdirectory_options project type)
Chris Bieneman580d8152015-07-20 20:36:06 +00001003 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1004 foreach(dir ${sub-dirs})
1005 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1006 canonicalize_tool_name(${dir} name)
Chris Bieneman70997c32015-10-20 16:42:58 +00001007 option(${project}_${type}_${name}_BUILD
1008 "Whether to build ${name} as part of ${project}" On)
1009 mark_as_advanced(${project}_${type}_${name}_BUILD)
Chris Bieneman580d8152015-07-20 20:36:06 +00001010 endif()
1011 endforeach()
Chris Bieneman70997c32015-10-20 16:42:58 +00001012endfunction(create_subdirectory_options)
1013
1014function(create_llvm_tool_options)
1015 create_subdirectory_options(LLVM TOOL)
Chris Bieneman580d8152015-07-20 20:36:06 +00001016endfunction(create_llvm_tool_options)
1017
Chris Bieneman74c98f02016-03-08 18:43:28 +00001018function(llvm_add_implicit_projects project)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +00001019 set(list_of_implicit_subdirs "")
1020 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1021 foreach(dir ${sub-dirs})
Chris Bieneman580d8152015-07-20 20:36:06 +00001022 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1023 canonicalize_tool_name(${dir} name)
Chris Bieneman74c98f02016-03-08 18:43:28 +00001024 if (${project}_TOOL_${name}_BUILD)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +00001025 get_filename_component(fn "${dir}" NAME)
1026 list(APPEND list_of_implicit_subdirs "${fn}")
1027 endif()
1028 endif()
1029 endforeach()
1030
1031 foreach(external_proj ${list_of_implicit_subdirs})
Chris Bieneman74c98f02016-03-08 18:43:28 +00001032 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +00001033 endforeach()
Chris Bieneman74c98f02016-03-08 18:43:28 +00001034endfunction(llvm_add_implicit_projects)
1035
1036function(add_llvm_implicit_projects)
1037 llvm_add_implicit_projects(LLVM)
Chris Bieneman580d8152015-07-20 20:36:06 +00001038endfunction(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +00001039
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001040# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +00001041function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001042 if( NOT LLVM_BUILD_TESTS )
1043 set(EXCLUDE_FROM_ALL ON)
1044 endif()
1045
Zachary Turner94f50322017-10-27 21:12:28 +00001046 # Our current version of gtest does not properly recognize C++11 support
1047 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1048 # itself requires C++11, we can safely force it on unconditionally so that
1049 # we don't have to fight with the buggy gtest check.
1050 add_definitions(-DGTEST_LANG_CXX11=1)
1051 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1052
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001053 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
Chandler Carruthbf6a4e02017-01-10 22:32:26 +00001054 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001055 if (NOT LLVM_ENABLE_THREADS)
1056 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1057 endif ()
1058
Chandler Carruth25657e82017-01-06 23:16:00 +00001059 if (SUPPORTS_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +00001060 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001061 endif ()
Chandler Carruthdd9c27b2017-01-04 23:40:06 +00001062 # Some parts of gtest rely on this GNU extension, don't warn on it.
Chandler Carruth25657e82017-01-06 23:16:00 +00001063 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
Chandler Carruthdd9c27b2017-01-04 23:40:06 +00001064 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1065 endif()
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001066
NAKAMURA Takumia679f432014-01-28 09:44:06 +00001067 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001068
Andrew Wilkins095e22d2016-02-12 01:42:43 +00001069 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
Jonas Hahnfeld277a09d2016-11-03 06:58:16 +00001070 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +00001071 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
John Brawnc11ef2a2015-09-30 15:20:51 +00001072 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
Artem Belevichdf7103d2016-06-21 19:34:40 +00001073 # libpthreads overrides some standard library symbols, so main
1074 # executable must be linked with it in order to provide consistent
1075 # API for all shared libaries loaded by this executable.
Eric Fiselier87c87f42017-02-10 01:59:20 +00001076 target_link_libraries(${test_name} gtest_main gtest ${LLVM_PTHREAD_LIB})
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001077
1078 add_dependencies(${test_suite} ${test_name})
1079 get_target_property(test_suite_folder ${test_suite} FOLDER)
1080 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1081 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1082 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001083endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001084
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001085function(llvm_add_go_executable binary pkgpath)
1086 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1087
1088 if(LLVM_BINDINGS MATCHES "go")
1089 # FIXME: This should depend only on the libraries Go needs.
1090 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1091 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1092 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1093 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +00001094 set(cppflags "")
1095 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1096 foreach(d ${include_dirs})
1097 set(cppflags "${cppflags} -I${d}")
1098 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001099 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1100 add_custom_command(OUTPUT ${binpath}
Andrew Wilkins5bf7d812016-07-27 03:21:51 +00001101 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001102 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1103 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1104 ${llvmlibs} ${ARG_DEPENDS}
1105 COMMENT "Building Go executable ${binary}"
1106 VERBATIM)
1107 if (ARG_ALL)
1108 add_custom_target(${binary} ALL DEPENDS ${binpath})
1109 else()
1110 add_custom_target(${binary} DEPENDS ${binpath})
1111 endif()
1112 endif()
1113endfunction()
1114
Michal Gorny9283f5b2017-01-06 21:33:48 +00001115# This function canonicalize the CMake variables passed by names
1116# from CMake boolean to 0/1 suitable for passing into Python or C++,
1117# in place.
1118function(llvm_canonicalize_cmake_booleans)
1119 foreach(var ${ARGN})
1120 if(${var})
1121 set(${var} 1 PARENT_SCOPE)
1122 else()
1123 set(${var} 0 PARENT_SCOPE)
1124 endif()
1125 endforeach()
1126endfunction(llvm_canonicalize_cmake_booleans)
1127
Greg Bedwell99e183c2017-11-13 12:40:05 +00001128macro(set_llvm_build_mode)
1129 # Configuration-time: See Unit/lit.site.cfg.in
1130 if (CMAKE_CFG_INTDIR STREQUAL ".")
1131 set(LLVM_BUILD_MODE ".")
1132 else ()
1133 set(LLVM_BUILD_MODE "%(build_mode)s")
1134 endif ()
1135endmacro()
1136
Chandler Carruth3511dd32012-06-28 06:36:24 +00001137# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +00001138# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +00001139# variables needed for the 'lit.site.cfg' files. This function bundles the
1140# common variables that any Lit instance is likely to need, and custom
1141# variables can be passed in.
Zachary Turner957d6112017-09-21 00:24:52 +00001142function(configure_lit_site_cfg site_in site_out)
Zachary Turner71deeee2017-09-21 16:18:28 +00001143 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
Zachary Turner957d6112017-09-21 00:24:52 +00001144
1145 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
Zachary Turner71deeee2017-09-21 16:18:28 +00001146 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1147 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1148 endif()
1149 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1150 set(ARG_OUTPUT_MAPPING "${site_out}")
Zachary Turner957d6112017-09-21 00:24:52 +00001151 endif()
1152
Chandler Carruth3511dd32012-06-28 06:36:24 +00001153 foreach(c ${LLVM_TARGETS_TO_BUILD})
1154 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1155 endforeach(c)
1156 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1157
1158 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +00001159
Greg Bedwell99e183c2017-11-13 12:40:05 +00001160 set_llvm_build_mode()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001161
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001162 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +00001163 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1164 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
Zachary Turner50105d22017-09-19 17:19:10 +00001165 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1166 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001167
1168 # SHLIBDIR points the build tree.
Zachary Turner50105d22017-09-19 17:19:10 +00001169 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001170
Chandler Carruth3511dd32012-06-28 06:36:24 +00001171 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +00001172 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1173 # plugins. We may rename it.
1174 if(LLVM_ENABLE_PLUGINS)
1175 set(ENABLE_SHARED "1")
1176 else()
1177 set(ENABLE_SHARED "0")
1178 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001179
1180 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1181 set(ENABLE_ASSERTIONS "1")
1182 else()
1183 set(ENABLE_ASSERTIONS "0")
1184 endif()
1185
Kuba Breckaa62bbcc2015-01-24 01:42:44 +00001186 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +00001187 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +00001188
Peter Collingbournece800842014-10-17 18:32:36 +00001189 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1190 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +00001191 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +00001192
Zachary Turner957d6112017-09-21 00:24:52 +00001193 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
Alex Denisovd5cee4a2016-04-16 06:47:41 +00001194
NAKAMURA Takumifc7f3b72017-06-17 03:19:08 +00001195 # Override config_target_triple (and the env)
1196 if(LLVM_TARGET_TRIPLE_ENV)
1197 # This is expanded into the heading.
1198 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1199 "import os\n"
1200 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1201 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1202 )
1203
1204 # This is expanded to; config.target_triple = ""+config.target_triple+""
1205 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1206 endif()
1207
Zachary Turnerc3023d12017-09-16 18:46:21 +00001208 string(CONCAT LIT_SITE_CFG_IN_FOOTER
1209 "import lit.llvm\n"
1210 "lit.llvm.initialize(lit_config, config)\n")
1211
Zachary Turner957d6112017-09-21 00:24:52 +00001212 configure_file(${site_in} ${site_out} @ONLY)
Zachary Turner957d6112017-09-21 00:24:52 +00001213 if (EXISTS "${ARG_MAIN_CONFIG}")
1214 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
Zachary Turnerce92db12017-09-15 22:10:46 +00001215 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1216 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1217 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1218 endif()
1219endfunction()
1220
Zachary Turnerf8c80f912017-09-18 21:52:02 +00001221function(dump_all_cmake_variables)
1222 get_cmake_property(_variableNames VARIABLES)
1223 foreach (_variableName ${_variableNames})
1224 message(STATUS "${_variableName}=${${_variableName}}")
1225 endforeach()
1226endfunction()
1227
Zachary Turnerce92db12017-09-15 22:10:46 +00001228function(get_llvm_lit_path base_dir file_name)
1229 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1230
1231 if (ARG_ALLOW_EXTERNAL)
Zachary Turner3dd23562017-09-20 16:01:50 +00001232 set(LLVM_DEFAULT_EXTERNAL_LIT "${LLVM_EXTERNAL_LIT}")
Zachary Turnerce92db12017-09-15 22:10:46 +00001233 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
Zachary Turner3dd23562017-09-20 16:01:50 +00001234 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1235 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1236 endif()
1237
Zachary Turnerce92db12017-09-15 22:10:46 +00001238 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1239 if (EXISTS ${LLVM_EXTERNAL_LIT})
1240 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1241 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1242 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1243 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1244 return()
1245 else()
1246 message(WARN "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1247 endif()
1248 endif()
1249 endif()
1250
1251 set(lit_file_name "llvm-lit")
1252 if (WIN32 AND NOT CYGWIN)
1253 # llvm-lit needs suffix.py for multiprocess to find a main module.
1254 set(lit_file_name "${lit_file_name}.py")
1255 endif ()
1256 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1257
1258 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1259 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1260 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1261 endif()
1262
1263 # Allow individual projects to provide an override
1264 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1265 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1266 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1267 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1268 else()
Zachary Turner3dd23562017-09-20 16:01:50 +00001269 set(LLVM_LIT_BASE_DIR "")
Zachary Turnerce92db12017-09-15 22:10:46 +00001270 endif()
1271
1272 # Cache this so we don't have to do it again and have subsequent calls
1273 # potentially disagree on the value.
1274 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1275 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
Chandler Carruth3511dd32012-06-28 06:36:24 +00001276endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001277
1278# A raw function to create a lit target. This is used to implement the testuite
1279# management functions.
1280function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001281 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001282 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1283 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +00001284 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1285 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1286 endif ()
Zachary Turnerce92db12017-09-15 22:10:46 +00001287
1288 # Get the path to the lit to *run* tests with. This can be overriden by
1289 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1290 get_llvm_lit_path(
1291 lit_base_dir
1292 lit_file_name
1293 ALLOW_EXTERNAL
1294 )
1295
1296 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
Greg Fitzgerald962a3392014-05-21 16:44:03 +00001297 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001298 foreach(param ${ARG_PARAMS})
1299 list(APPEND LIT_COMMAND --param ${param})
1300 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001301 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001302 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001303 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001304 COMMENT "${comment}"
Chris Bienemandf04e792016-06-08 22:19:25 +00001305 USES_TERMINAL
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001306 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001307 else()
1308 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +00001309 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001310 message(STATUS "${target} does nothing.")
1311 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +00001312 if (ARG_DEPENDS)
1313 add_dependencies(${target} ${ARG_DEPENDS})
1314 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +00001315
1316 # Tests should be excluded from "Build Solution".
1317 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +00001318endfunction()
1319
1320# A function to add a set of lit test suites to be driven through 'check-*' targets.
1321function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001322 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001323
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001324 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1325 if(NOT EXCLUDE_FROM_ALL)
1326 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001327 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001328 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1329 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1330 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1331 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001332
1333 # Produce a specific suffixed check rule.
1334 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001335 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +00001336 PARAMS ${ARG_PARAMS}
1337 DEPENDS ${ARG_DEPENDS}
1338 ARGS ${ARG_ARGS}
1339 )
1340endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001341
1342function(add_lit_testsuites project directory)
1343 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001344 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Justin Bognere8894622016-05-06 21:57:30 +00001345
1346 # Search recursively for test directories by assuming anything not
1347 # in a directory called Inputs contains tests.
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001348 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1349 foreach(lit_suite ${to_process})
1350 if(NOT IS_DIRECTORY ${lit_suite})
1351 continue()
1352 endif()
1353 string(FIND ${lit_suite} Inputs is_inputs)
Justin Bogner25ef9e32016-07-25 18:07:14 +00001354 string(FIND ${lit_suite} Output is_output)
Ayman Musa2d338692017-04-13 09:26:49 +00001355 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001356 continue()
1357 endif()
Justin Bognere8894622016-05-06 21:57:30 +00001358
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001359 # Create a check- target for the directory.
1360 string(REPLACE ${directory} "" name_slash ${lit_suite})
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001361 if (name_slash)
1362 string(REPLACE "/" "-" name_slash ${name_slash})
1363 string(REPLACE "\\" "-" name_dashes ${name_slash})
1364 string(TOLOWER "${project}${name_dashes}" name_var)
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001365 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1366 ${lit_suite}
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001367 PARAMS ${ARG_PARAMS}
1368 DEPENDS ${ARG_DEPENDS}
1369 ARGS ${ARG_ARGS}
1370 )
1371 endif()
1372 endforeach()
1373 endif()
1374endfunction()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001375
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001376function(llvm_install_library_symlink name dest type)
1377 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1378 foreach(path ${CMAKE_MODULE_PATH})
1379 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1380 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1381 break()
1382 endif()
1383 endforeach()
1384
1385 set(component ${ARG_COMPONENT})
1386 if(NOT component)
1387 set(component ${name})
1388 endif()
1389
1390 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1391 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1392
Chris Bieneman021f8742015-11-11 16:19:39 +00001393 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001394 if(WIN32 AND "${type}" STREQUAL "SHARED")
1395 set(output_dir bin)
1396 endif()
1397
1398 install(SCRIPT ${INSTALL_SYMLINK}
1399 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1400 COMPONENT ${component})
1401
1402 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1403 add_custom_target(install-${name}
1404 DEPENDS ${name} ${dest} install-${dest}
1405 COMMAND "${CMAKE_COMMAND}"
1406 -DCMAKE_INSTALL_COMPONENT=${name}
1407 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1408 endif()
1409endfunction()
1410
Chris Bienemanbff03b02015-09-16 20:49:59 +00001411function(llvm_install_symlink name dest)
Saleem Abdulrasool27227bb2016-12-03 22:03:24 +00001412 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
Chris Bieneman08249702015-09-18 17:39:58 +00001413 foreach(path ${CMAKE_MODULE_PATH})
1414 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1415 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1416 break()
1417 endif()
1418 endforeach()
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +00001419
Saleem Abdulrasool27227bb2016-12-03 22:03:24 +00001420 if(ARG_COMPONENT)
1421 set(component ${ARG_COMPONENT})
Chris Bieneman895d96a2015-09-18 21:08:32 +00001422 else()
Saleem Abdulrasool27227bb2016-12-03 22:03:24 +00001423 if(ARG_ALWAYS_GENERATE)
1424 set(component ${dest})
1425 else()
1426 set(component ${name})
1427 endif()
Chris Bieneman895d96a2015-09-18 21:08:32 +00001428 endif()
1429
Chris Bieneman11a71602015-10-16 23:17:13 +00001430 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1431 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1432
Chris Bieneman08249702015-09-18 17:39:58 +00001433 install(SCRIPT ${INSTALL_SYMLINK}
Chris Bieneman2724f142016-06-08 21:19:26 +00001434 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
Chris Bieneman895d96a2015-09-18 21:08:32 +00001435 COMPONENT ${component})
Chris Bienemanbff03b02015-09-16 20:49:59 +00001436
Chris Bieneman895d96a2015-09-18 21:08:32 +00001437 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
Chris Bienemanbff03b02015-09-16 20:49:59 +00001438 add_custom_target(install-${name}
1439 DEPENDS ${name} ${dest} install-${dest}
1440 COMMAND "${CMAKE_COMMAND}"
1441 -DCMAKE_INSTALL_COMPONENT=${name}
1442 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1443 endif()
1444endfunction()
1445
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001446function(add_llvm_tool_symlink link_name target)
1447 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
Chris Bienemandc9b0db2016-12-15 18:17:07 +00001448 set(dest_binary "$<TARGET_FILE:${target}>")
1449
Chris Bieneman28f05ac2016-12-05 17:02:11 +00001450 # This got a bit gross... For multi-configuration generators the target
1451 # properties return the resolved value of the string, not the build system
1452 # expression. To reconstruct the platform-agnostic path we have to do some
1453 # magic. First we grab one of the types, and a type-specific path. Then from
1454 # the type-specific path we find the last occurrence of the type in the path,
1455 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
Zachary Turnerce92db12017-09-15 22:10:46 +00001456 # agnostic again.
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001457 if(NOT ARG_OUTPUT_DIR)
Chris Bienemandc9b0db2016-12-15 18:17:07 +00001458 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1459 # the same directory.
1460 if(UNIX)
1461 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1462 endif()
Chris Bieneman28f05ac2016-12-05 17:02:11 +00001463 if(CMAKE_CONFIGURATION_TYPES)
1464 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1465 string(TOUPPER ${first_type} first_type_upper)
1466 set(first_type_suffix _${first_type_upper})
1467 endif()
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001468 get_target_property(target_type ${target} TYPE)
1469 if(${target_type} STREQUAL "STATIC_LIBRARY")
Chris Bieneman28f05ac2016-12-05 17:02:11 +00001470 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001471 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
Chris Bieneman28f05ac2016-12-05 17:02:11 +00001472 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001473 else()
Chris Bieneman28f05ac2016-12-05 17:02:11 +00001474 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1475 endif()
1476 if(CMAKE_CONFIGURATION_TYPES)
1477 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1478 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1479 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1480 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1481 path_suffix ${path_suffix})
1482 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001483 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001484 endif()
1485
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001486 if(UNIX)
1487 set(LLVM_LINK_OR_COPY create_symlink)
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001488 else()
1489 set(LLVM_LINK_OR_COPY copy)
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001490 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001491
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001492 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1493
1494 set(target_name ${link_name})
1495 if(TARGET ${link_name})
1496 set(target_name ${link_name}-link)
Chris Bieneman6ee60bc2016-09-16 22:19:19 +00001497 endif()
1498
1499
Chris Bieneman895d96a2015-09-18 21:08:32 +00001500 if(ARG_ALWAYS_GENERATE)
1501 set_property(DIRECTORY APPEND PROPERTY
1502 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001503 add_custom_command(TARGET ${target} POST_BUILD
Chris Bieneman895d96a2015-09-18 21:08:32 +00001504 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1505 else()
1506 add_custom_command(OUTPUT ${output_path}
Chris Bienemanbde7e452015-09-15 02:15:53 +00001507 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001508 DEPENDS ${target})
1509 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
Chris Bieneman6ee60bc2016-09-16 22:19:19 +00001510 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001511
Chris Bieneman44d29902016-09-27 17:47:24 +00001512 # Make sure both the link and target are toolchain tools
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001513 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
Petr Hosekacac3ab2016-09-30 06:29:28 +00001514 set(TOOL_IS_TOOLCHAIN ON)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001515 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001516
Petr Hosekacac3ab2016-09-30 06:29:28 +00001517 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
Chris Bieneman89e08ed2016-12-05 03:28:03 +00001518 llvm_install_symlink(${link_name} ${target})
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001519 endif()
1520 endif()
1521endfunction()
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001522
1523function(llvm_externalize_debuginfo name)
1524 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1525 return()
1526 endif()
1527
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001528 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
Petr Hosek7acbeaf2017-02-17 19:29:12 +00001529 if(APPLE)
1530 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1531 else()
1532 set(strip_command COMMAND strip -gx $<TARGET_FILE:${name}>)
1533 endif()
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001534 endif()
1535
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001536 if(APPLE)
1537 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1538 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1539
1540 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bienemanccabd0e2015-12-03 22:51:08 +00001541 set_property(TARGET ${name} APPEND_STRING PROPERTY
1542 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001543 endif()
1544 add_custom_command(TARGET ${name} POST_BUILD
1545 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001546 ${strip_command}
1547 )
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001548 else()
Petr Hosek7acbeaf2017-02-17 19:29:12 +00001549 add_custom_command(TARGET ${name} POST_BUILD
1550 COMMAND objcopy --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1551 ${strip_command} -R .gnu_debuglink
1552 COMMAND objcopy --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1553 )
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001554 endif()
1555endfunction()
Chris Bieneman61e900b2016-11-01 17:44:58 +00001556
1557function(llvm_setup_rpath name)
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001558 if(CMAKE_INSTALL_RPATH)
1559 return()
1560 endif()
1561
Chris Bieneman61e900b2016-11-01 17:44:58 +00001562 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1563 set(extra_libdir ${LLVM_LIBRARY_DIR})
1564 elseif(LLVM_BUILD_LIBRARY_DIR)
1565 set(extra_libdir ${LLVM_LIBRARY_DIR})
1566 endif()
1567
1568 if (APPLE)
1569 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1570 set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1571 elseif(UNIX)
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001572 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1573 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1574 set_property(TARGET ${name} APPEND_STRING PROPERTY
1575 LINK_FLAGS " -Wl,-z,origin ")
1576 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT LLVM_LINKER_IS_GOLD)
1577 # $ORIGIN is not interpreted at link time by ld.bfd
1578 set_property(TARGET ${name} APPEND_STRING PROPERTY
1579 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
Chris Bieneman61e900b2016-11-01 17:44:58 +00001580 endif()
1581 else()
1582 return()
1583 endif()
1584
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001585 set_target_properties(${name} PROPERTIES
1586 BUILD_WITH_INSTALL_RPATH On
1587 INSTALL_RPATH "${_install_rpath}"
1588 ${_install_name_dir})
Chris Bieneman61e900b2016-11-01 17:44:58 +00001589endfunction()
Chris Bieneman8036e0b2016-11-17 04:36:59 +00001590
1591function(setup_dependency_debugging name)
1592 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1593 return()
1594 endif()
1595
1596 if("intrinsics_gen" IN_LIST ARGN)
1597 return()
1598 endif()
1599
1600 set(deny_attributes_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.gen\"))")
1601 set(deny_intrinsics_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.gen\"))")
1602
1603 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_gen} ${deny_intrinsics_gen}'")
Chris Bieneman958edcb2016-11-19 01:32:09 +00001604 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
Chris Bieneman8036e0b2016-11-17 04:36:59 +00001605endfunction()
Don Hinton76d02ce2017-09-02 17:28:39 +00001606
1607# Figure out if we can track VC revisions.
1608function(find_first_existing_file out_var)
1609 foreach(file ${ARGN})
1610 if(EXISTS "${file}")
1611 set(${out_var} "${file}" PARENT_SCOPE)
1612 return()
1613 endif()
1614 endforeach()
1615endfunction()
1616
1617macro(find_first_existing_vc_file out_var path)
1618 find_program(git_executable NAMES git git.exe git.cmd)
1619 # Run from a subdirectory to force git to print an absolute path.
1620 execute_process(COMMAND ${git_executable} rev-parse --git-dir
1621 WORKING_DIRECTORY ${path}/cmake
1622 RESULT_VARIABLE git_result
1623 OUTPUT_VARIABLE git_dir
1624 ERROR_QUIET)
1625 if(git_result EQUAL 0)
1626 string(STRIP "${git_dir}" git_dir)
1627 set(${out_var} "${git_dir}/logs/HEAD")
1628 # some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1629 if (NOT EXISTS "${git_dir}/logs/HEAD")
1630 file(WRITE "${git_dir}/logs/HEAD" "")
1631 endif()
1632 else()
1633 find_first_existing_file(${out_var}
1634 "${path}/.svn/wc.db" # SVN 1.7
1635 "${path}/.svn/entries" # SVN 1.6
1636 )
1637 endif()
1638endmacro()