blob: e8e119b2ad2fca9c8d7f595cbdfbe034064b6acb [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.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000084 add_custom_command(OUTPUT ${native_export_file}
85 COMMAND echo "{" > ${native_export_file}
86 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
87 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
88 COMMAND echo " local: *;" >> ${native_export_file}
89 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000090 DEPENDS ${export_file}
91 VERBATIM
92 COMMENT "Creating export file for ${target_name}")
Rafael Espindolaec1d0692015-06-22 12:34:54 +000093 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
94 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
95 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
96 else()
97 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
98 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
99 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000100 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +0000101 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +0000102
NAKAMURA Takumi81210752013-12-29 16:15:18 +0000103 add_custom_command(OUTPUT ${native_export_file}
NAKAMURA Takumi3eb06662015-07-05 08:56:38 +0000104 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
105 < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000106 DEPENDS ${export_file}
107 VERBATIM
108 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000109 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
110 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000111 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000112 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000115 endif()
116
117 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000118 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000119
120 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
121 foreach(src ${srcs})
122 get_filename_component(extension ${src} EXT)
123 if(extension STREQUAL ".cpp")
124 set(first_source_file ${src})
125 break()
126 endif()
127 endforeach()
128
129 # Force re-linking when the exports file changes. Actually, it
130 # forces recompilation of the source file. The LINK_DEPENDS target
131 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000132 # FIXME: This is not safe because this will create the same target
133 # ${native_export_file} in several different file:
134 # - One where we emitted ${target_name}_exports
135 # - One where we emitted the build command for the following object.
136 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
137 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000138
139 set_property(DIRECTORY APPEND
140 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
141
142 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000143
144 # Add dependency to *_exports later -- CMake issue 14747
145 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
146 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000147endfunction(add_llvm_symbol_exports)
148
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000149if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000150 execute_process(
151 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
152 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000153 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000154 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000155 if("${stdout}" MATCHES "GNU gold")
156 set(LLVM_LINKER_IS_GOLD ON)
157 endif()
158endif()
159
Rafael Espindola8cb78762014-11-02 12:14:22 +0000160function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000161 # Don't use linker optimizations in debug builds since it slows down the
162 # linker in a context where the optimizations are not important.
163 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000164
Rafael Espindola24766f92015-04-06 15:04:31 +0000165 # Pass -O3 to the linker. This enabled different optimizations on different
166 # linkers.
Chandler Carruth2aff7502016-07-19 22:46:39 +0000167 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000168 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000169 LINK_FLAGS " -Wl,-O3")
170 endif()
171
172 if(LLVM_LINKER_IS_GOLD)
173 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000174 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
175 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000176 # Note that there is a bug with -Wl,--icf=safe so it is not safe
177 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
178 endif()
179
180 if(NOT LLVM_NO_DEAD_STRIP)
181 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
182 # ld64's implementation of -dead_strip breaks tools that use plugins.
183 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
184 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindolac32ed572015-06-22 15:06:17 +0000185 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
186 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
187 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000188 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
189 # Object files are compiled with -ffunction-data-sections.
190 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
191 # tools that use plugins. Always pass --gc-sections once we require
192 # a newer linker.
193 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
194 LINK_FLAGS " -Wl,--gc-sections")
195 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000196 endif()
197 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000198endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000199
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000200# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
201# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
202# or a certain builder, for eaxample, msbuild.exe, would be confused.
John Brawnc11ef2a2015-09-30 15:20:51 +0000203function(set_output_directory target)
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +0000204 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000205
John Brawnc11ef2a2015-09-30 15:20:51 +0000206 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000207 # It affects output of add_library(MODULE).
208 if(WIN32 OR CYGWIN)
209 # DLL platform
John Brawnc11ef2a2015-09-30 15:20:51 +0000210 set(module_dir ${ARG_BINARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000211 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000212 set(module_dir ${ARG_LIBRARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000213 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000214 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
215 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
216 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
John Brawnc11ef2a2015-09-30 15:20:51 +0000217 if(ARG_BINARY_DIR)
218 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
219 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
220 endif()
221 if(ARG_LIBRARY_DIR)
222 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
223 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
224 endif()
225 if(module_dir)
226 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
227 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
228 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000229 endforeach()
230 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000231 if(ARG_BINARY_DIR)
232 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
233 endif()
234 if(ARG_LIBRARY_DIR)
235 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
236 endif()
237 if(module_dir)
238 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
239 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000240 endif()
241endfunction()
242
Greg Bedwell95213c32015-06-12 15:58:29 +0000243# If on Windows and building with MSVC, add the resource script containing the
244# VERSIONINFO data to the project. This embeds version resource information
245# into the output .exe or .dll.
246# TODO: Enable for MinGW Windows builds too.
247#
248function(add_windows_version_resource_file OUT_VAR)
249 set(sources ${ARGN})
250 if (MSVC)
251 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000252 if(EXISTS ${resource_file})
253 set(sources ${sources} ${resource_file})
254 source_group("Resource Files" ${resource_file})
255 set(windows_resource_file ${resource_file} PARENT_SCOPE)
256 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000257 endif(MSVC)
258
259 set(${OUT_VAR} ${sources} PARENT_SCOPE)
260endfunction(add_windows_version_resource_file)
261
262# set_windows_version_resource_properties(name resource_file...
263# VERSION_MAJOR int
264# Optional major version number (defaults to LLVM_VERSION_MAJOR)
265# VERSION_MINOR int
266# Optional minor version number (defaults to LLVM_VERSION_MINOR)
267# VERSION_PATCHLEVEL int
268# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
269# VERSION_STRING
270# Optional version string (defaults to PACKAGE_VERSION)
271# PRODUCT_NAME
272# Optional product name string (defaults to "LLVM")
273# )
274function(set_windows_version_resource_properties name resource_file)
275 cmake_parse_arguments(ARG
276 ""
277 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
278 ""
279 ${ARGN})
280
281 if (NOT DEFINED ARG_VERSION_MAJOR)
282 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
283 endif()
284
285 if (NOT DEFINED ARG_VERSION_MINOR)
286 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
287 endif()
288
289 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
290 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
291 endif()
292
293 if (NOT DEFINED ARG_VERSION_STRING)
294 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
295 endif()
296
297 if (NOT DEFINED ARG_PRODUCT_NAME)
298 set(ARG_PRODUCT_NAME "LLVM")
299 endif()
300
301 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000302 PROPERTY COMPILE_FLAGS /nologo)
303 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000304 PROPERTY COMPILE_DEFINITIONS
305 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
306 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
307 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
308 "RC_VERSION_FIELD_4=0"
309 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
310 "RC_INTERNAL_NAME=\"${name}\""
311 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
312 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
313endfunction(set_windows_version_resource_properties)
314
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000315# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000316# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000317# STATIC by default w/o BUILD_SHARED_LIBS.
318# SHARED by default w/ BUILD_SHARED_LIBS.
Axel Naumannb457beb2016-01-12 07:44:58 +0000319# OBJECT
320# Also create an OBJECT library target. Default if STATIC && SHARED.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000321# MODULE
322# Target ${name} might not be created on unsupported platforms.
323# Check with "if(TARGET ${name})".
Andrew Wilkins92113962015-09-01 03:14:31 +0000324# DISABLE_LLVM_LINK_LLVM_DYLIB
325# Do not link this library to libLLVM, even if
326# LLVM_LINK_LLVM_DYLIB is enabled.
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000327# OUTPUT_NAME name
328# Corresponds to OUTPUT_NAME in target properties.
329# DEPENDS targets...
330# Same semantics as add_dependencies().
331# LINK_COMPONENTS components...
332# Same as the variable LLVM_LINK_COMPONENTS.
333# LINK_LIBS lib_targets...
334# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000335# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000336# May specify header files for IDE generators.
Chris Bieneman5e96fe92015-11-04 23:11:12 +0000337# SONAME
338# Should set SONAME link flags and create symlinks
John Brawn3546c2f2016-05-26 11:16:43 +0000339# PLUGIN_TOOL
340# The tool (i.e. cmake target) that this plugin will link against
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000341# )
342function(llvm_add_library name)
343 cmake_parse_arguments(ARG
Axel Naumannb457beb2016-01-12 07:44:58 +0000344 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
John Brawn3546c2f2016-05-26 11:16:43 +0000345 "OUTPUT_NAME;PLUGIN_TOOL"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000346 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000347 ${ARGN})
348 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000349 if(ARG_ADDITIONAL_HEADERS)
350 # Pass through ADDITIONAL_HEADERS.
351 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
352 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000353 if(ARG_OBJLIBS)
354 set(ALL_FILES ${ARG_OBJLIBS})
355 else()
356 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
357 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000358
359 if(ARG_MODULE)
360 if(ARG_SHARED OR ARG_STATIC)
361 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
362 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000363 # Plugins that link against a tool are allowed even when plugins in general are not
364 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000365 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
366 return()
367 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000368 else()
John Brawn3546c2f2016-05-26 11:16:43 +0000369 if(ARG_PLUGIN_TOOL)
370 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
371 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000372 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
373 set(ARG_SHARED TRUE)
374 endif()
375 if(NOT ARG_SHARED)
376 set(ARG_STATIC TRUE)
377 endif()
378 endif()
379
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000380 # Generate objlib
Axel Naumannb457beb2016-01-12 07:44:58 +0000381 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000382 # Generate an obj library for both targets.
383 set(obj_name "obj.${name}")
384 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
385 ${ALL_FILES}
386 )
387 llvm_update_compile_flags(${obj_name})
388 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
389
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000390 # Do add_dependencies(obj) later due to CMake issue 14747.
391 list(APPEND objlibs ${obj_name})
392
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000393 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
394 endif()
395
396 if(ARG_SHARED AND ARG_STATIC)
397 # static
398 set(name_static "${name}_static")
399 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000400 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000401 endif()
402 # DEPENDS has been appended to LLVM_COMMON_LIBS.
403 llvm_add_library(${name_static} STATIC
404 ${output_name}
405 OBJLIBS ${ALL_FILES} # objlib
406 LINK_LIBS ${ARG_LINK_LIBS}
407 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
408 )
409 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
410 set(ARG_STATIC)
411 endif()
412
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000413 if(ARG_MODULE)
414 add_library(${name} MODULE ${ALL_FILES})
415 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000416 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000417 add_library(${name} SHARED ${ALL_FILES})
Chris Bieneman61e900b2016-11-01 17:44:58 +0000418
419 llvm_setup_rpath(${name})
420
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000421 else()
422 add_library(${name} STATIC ${ALL_FILES})
423 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000424
425 if(DEFINED windows_resource_file)
426 set_windows_version_resource_properties(${name} ${windows_resource_file})
427 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
428 endif()
429
John Brawnc11ef2a2015-09-30 15:20:51 +0000430 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000431 # $<TARGET_OBJECTS> doesn't require compile flags.
432 if(NOT obj_name)
433 llvm_update_compile_flags(${name})
434 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000435 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000436 if(ARG_OUTPUT_NAME)
437 set_target_properties(${name}
438 PROPERTIES
439 OUTPUT_NAME ${ARG_OUTPUT_NAME}
440 )
441 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000442
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000443 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000444 set_target_properties(${name} PROPERTIES
445 PREFIX ""
446 SUFFIX ${LLVM_PLUGIN_EXT}
447 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000448 endif()
449
450 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000451 if(WIN32)
452 set_target_properties(${name} PROPERTIES
453 PREFIX ""
454 )
455 endif()
Michal Gorny6b929d52016-10-04 06:09:18 +0000456
457 # Set SOVERSION on shared libraries that lack explicit SONAME
458 # specifier, on *nix systems that are not Darwin.
459 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
460 set_target_properties(${name}
461 PROPERTIES
462 # Concatenate the version numbers since ldconfig expects exactly
463 # one component indicating the ABI version, while LLVM uses
464 # major+minor for that.
465 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR}
466 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
467 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000468 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000469
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000470 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000471 # Do not add -Dname_EXPORTS to the command-line when building files in this
472 # target. Doing so is actively harmful for the modules build because it
473 # creates extra module variants, and not useful because we don't use these
474 # macros.
475 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
476
Nico Weber06473f82013-12-29 05:39:01 +0000477 if (LLVM_EXPORTED_SYMBOL_FILE)
478 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
479 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000480 endif()
481
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000482 if(ARG_SHARED AND UNIX)
483 if(NOT APPLE AND ARG_SONAME)
484 get_target_property(output_name ${name} OUTPUT_NAME)
485 if(${output_name} STREQUAL "output_name-NOTFOUND")
486 set(output_name ${name})
487 endif()
488 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
489 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
490 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
491 llvm_install_library_symlink(${api_name} ${library_name} SHARED
492 COMPONENT ${name}
493 ALWAYS_GENERATE)
494 llvm_install_library_symlink(${output_name} ${library_name} SHARED
495 COMPONENT ${name}
496 ALWAYS_GENERATE)
497 endif()
498 endif()
499
John Brawn3546c2f2016-05-26 11:16:43 +0000500 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
501 # On DLL platforms symbols are imported from the tool by linking against it.
502 set(llvm_libs ${ARG_PLUGIN_TOOL})
503 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000504 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
505 set(llvm_libs LLVM)
506 else()
507 llvm_map_components_to_libnames(llvm_libs
508 ${ARG_LINK_COMPONENTS}
509 ${LLVM_LINK_COMPONENTS}
510 )
511 endif()
Andrew Wilkins92113962015-09-01 03:14:31 +0000512 else()
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000513 # Components have not been defined explicitly in CMake, so add the
514 # dependency information for this library as defined by LLVMBuild.
515 #
516 # It would be nice to verify that we have the dependencies for this library
517 # name, but using get_property(... SET) doesn't suffice to determine if a
518 # property has been set to an empty value.
519 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
Andrew Wilkins92113962015-09-01 03:14:31 +0000520 endif()
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000521
Chris Bieneman6650b022016-06-08 22:48:01 +0000522 if(ARG_STATIC)
523 set(libtype INTERFACE)
Steven Wuec6f56e2016-05-26 04:35:35 +0000524 else()
525 # We can use PRIVATE since SO knows its dependent libs.
Chris Bieneman6650b022016-06-08 22:48:01 +0000526 set(libtype PRIVATE)
527 endif()
528
529 target_link_libraries(${name} ${libtype}
Steven Wuec6f56e2016-05-26 04:35:35 +0000530 ${ARG_LINK_LIBS}
531 ${lib_deps}
532 ${llvm_libs}
533 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000534
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000535 if(LLVM_COMMON_DEPENDS)
536 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000537 # Add dependencies also to objlibs.
538 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
539 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000540 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
541 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000542 endif()
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000543
544 if(ARG_SHARED OR ARG_MODULE)
545 llvm_externalize_debuginfo(${name})
546 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000547endfunction()
548
549macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000550 cmake_parse_arguments(ARG
Michael Gottesman31e93632016-09-09 19:45:34 +0000551 "SHARED;BUILDTREE_ONLY"
Chris Bieneman84271692015-04-16 16:56:18 +0000552 ""
553 ""
554 ${ARGN})
Michael Gottesman31e93632016-09-09 19:45:34 +0000555 if( BUILD_SHARED_LIBS OR ARG_SHARED )
556 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000557 else()
Michael Gottesman31e93632016-09-09 19:45:34 +0000558 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000559 endif()
Michael Gottesman31e93632016-09-09 19:45:34 +0000560
561 # Libraries that are meant to only be exposed via the build tree only are
562 # never installed and are only exported as a target in the special build tree
563 # config file.
564 if (NOT ARG_BUILDTREE_ONLY)
Dan Liew80189d22015-06-29 18:45:56 +0000565 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
566 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000567
Nick Lewycky61aed872011-04-29 02:12:06 +0000568 if( EXCLUDE_FROM_ALL )
569 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Michael Gottesman31e93632016-09-09 19:45:34 +0000570 elseif(ARG_BUILDTREE_ONLY)
571 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
572 else()
Derek Schuffeb224632016-09-16 18:50:39 +0000573 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR
574 (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM"))
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000575 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000576 if(ARG_SHARED OR BUILD_SHARED_LIBS)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000577 if(WIN32 OR CYGWIN OR MINGW)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000578 set(install_type RUNTIME)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000579 set(install_dir bin)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000580 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000581 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000582 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000583 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000584 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000585 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000586
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000587 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
588 NOT LLVM_DISTRIBUTION_COMPONENTS)
589 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000590 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000591 endif()
592
Chris Bienemana985e6b2015-04-16 18:08:33 +0000593 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000594 ${export_to_llvmexports}
595 ${install_type} DESTINATION ${install_dir}
596 COMPONENT ${name})
Chris Bienemana985e6b2015-04-16 18:08:33 +0000597
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000598 if (NOT CMAKE_CONFIGURATION_TYPES)
599 add_custom_target(install-${name}
600 DEPENDS ${name}
601 COMMAND "${CMAKE_COMMAND}"
602 -DCMAKE_INSTALL_COMPONENT=${name}
603 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
604 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000605 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000606 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000607 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000608 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000609endmacro(add_llvm_library name)
610
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000611macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000612 llvm_add_library(${name} MODULE ${ARGN})
613 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000614 # Add empty "phony" target
615 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000616 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000617 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000618 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000619 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000620 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000621 if(WIN32 OR CYGWIN)
622 # DLL platform
623 set(dlldir "bin")
624 else()
625 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
626 endif()
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000627
628 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
629 NOT LLVM_DISTRIBUTION_COMPONENTS)
630 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000631 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000632 endif()
633
Hans Wennborg16546272013-08-24 00:20:36 +0000634 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000635 ${export_to_llvmexports}
636 LIBRARY DESTINATION ${dlldir}
637 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Hans Wennborg16546272013-08-24 00:20:36 +0000638 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000639 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000640 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000641 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000642
643 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000644endmacro(add_llvm_loadable_module name)
645
646
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000647macro(add_llvm_executable name)
Jonas Hahnfeld277a09d2016-11-03 06:58:16 +0000648 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" "" "" ${ARGN})
Andrew Wilkins92113962015-09-01 03:14:31 +0000649 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000650
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +0000651 # Generate objlib
652 if(LLVM_ENABLE_OBJLIB)
653 # Generate an obj library for both targets.
654 set(obj_name "obj.${name}")
655 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
656 ${ALL_FILES}
657 )
658 llvm_update_compile_flags(${obj_name})
659 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
660
661 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
662 endif()
663
NAKAMURA Takumib5288ba2015-08-28 00:36:58 +0000664 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
665
Jordan Rose3d752972015-09-10 17:18:51 +0000666 if(XCODE)
667 # Note: the dummy.cpp source file provides no definitions. However,
668 # it forces Xcode to properly link the static library.
Jordan Rose85a22f82015-09-10 17:55:02 +0000669 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
Jordan Rose3d752972015-09-10 17:18:51 +0000670 endif()
671
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000672 if( EXCLUDE_FROM_ALL )
673 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
674 else()
675 add_executable(${name} ${ALL_FILES})
676 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000677
Jonas Hahnfeld277a09d2016-11-03 06:58:16 +0000678 if(NOT ARG_NO_INSTALL_RPATH)
679 llvm_setup_rpath(${name})
680 endif()
Chris Bieneman61e900b2016-11-01 17:44:58 +0000681
Greg Bedwell95213c32015-06-12 15:58:29 +0000682 if(DEFINED windows_resource_file)
683 set_windows_version_resource_properties(${name} ${windows_resource_file})
684 endif()
685
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000686 # $<TARGET_OBJECTS> doesn't require compile flags.
687 if(NOT LLVM_ENABLE_OBJLIB)
688 llvm_update_compile_flags(${name})
689 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000690 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000691
Richard Smith2b91a7f2014-09-26 22:40:15 +0000692 # Do not add -Dname_EXPORTS to the command-line when building files in this
693 # target. Doing so is actively harmful for the modules build because it
694 # creates extra module variants, and not useful because we don't use these
695 # macros.
696 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
697
Nico Weberc27118d2013-12-28 23:31:44 +0000698 if (LLVM_EXPORTED_SYMBOL_FILE)
699 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
700 endif(LLVM_EXPORTED_SYMBOL_FILE)
701
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000702 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
703 set(USE_SHARED USE_SHARED)
704 endif()
705
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000706 set(EXCLUDE_FROM_ALL OFF)
John Brawnc11ef2a2015-09-30 15:20:51 +0000707 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000708 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000709 if( LLVM_COMMON_DEPENDS )
710 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
711 endif( LLVM_COMMON_DEPENDS )
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000712
Chris Bieneman48eaa542015-12-08 21:51:48 +0000713 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
714 llvm_externalize_debuginfo(${name})
715 endif()
Artem Belevichdf7103d2016-06-21 19:34:40 +0000716 if (PTHREAD_LIB)
717 # libpthreads overrides some standard library symbols, so main
718 # executable must be linked with it in order to provide consistent
719 # API for all shared libaries loaded by this executable.
720 target_link_libraries(${name} ${PTHREAD_LIB})
721 endif()
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000722endmacro(add_llvm_executable name)
723
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000724function(export_executable_symbols target)
John Brawn3546c2f2016-05-26 11:16:43 +0000725 if (LLVM_EXPORTED_SYMBOL_FILE)
726 # The symbol file should contain the symbols we want the executable to
727 # export
728 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
729 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
730 # Extract the symbols to export from the static libraries that the
731 # executable links against.
732 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
733 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
734 # We need to consider not just the direct link dependencies, but also the
735 # transitive link dependencies. Do this by starting with the set of direct
736 # dependencies, then the dependencies of those dependencies, and so on.
737 get_target_property(new_libs ${target} LINK_LIBRARIES)
738 set(link_libs ${new_libs})
739 while(NOT "${new_libs}" STREQUAL "")
740 foreach(lib ${new_libs})
John Brawn24ca18e2016-07-05 13:16:54 +0000741 if(TARGET ${lib})
742 get_target_property(lib_type ${lib} TYPE)
743 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
744 list(APPEND static_libs ${lib})
745 else()
746 list(APPEND other_libs ${lib})
John Brawn3546c2f2016-05-26 11:16:43 +0000747 endif()
John Brawn24ca18e2016-07-05 13:16:54 +0000748 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
749 foreach(transitive_lib ${transitive_libs})
750 list(FIND link_libs ${transitive_lib} idx)
751 if(TARGET ${transitive_lib} AND idx EQUAL -1)
752 list(APPEND newer_libs ${transitive_lib})
753 list(APPEND link_libs ${transitive_lib})
754 endif()
755 endforeach(transitive_lib)
756 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000757 endforeach(lib)
758 set(new_libs ${newer_libs})
759 set(newer_libs "")
760 endwhile()
761 if (MSVC)
762 set(mangling microsoft)
763 else()
764 set(mangling itanium)
765 endif()
766 add_custom_command(OUTPUT ${exported_symbol_file}
767 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
768 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
769 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
770 VERBATIM
771 COMMENT "Generating export list for ${target}")
772 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
773 # If something links against this executable then we want a
774 # transitive link against only the libraries whose symbols
775 # we aren't exporting.
776 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
777 # The default import library suffix that cmake uses for cygwin/mingw is
778 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
779 # where the import libraries of both get named libclang.dll.a. Use a suffix
780 # of ".exe.a" to avoid this.
781 if(CYGWIN OR MINGW)
782 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
783 endif()
784 elseif(NOT (WIN32 OR CYGWIN))
785 # On Windows auto-exporting everything doesn't work because of the limit on
786 # the size of the exported symbol table, but on other platforms we can do
787 # it without any trouble.
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000788 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
Chris Bieneman914742b2015-11-24 00:58:58 +0000789 if (APPLE)
Chris Bienemanccabd0e2015-12-03 22:51:08 +0000790 set_property(TARGET ${target} APPEND_STRING PROPERTY
Chris Bienemanac6677a2015-12-03 22:55:36 +0000791 LINK_FLAGS " -rdynamic")
Chris Bieneman914742b2015-11-24 00:58:58 +0000792 endif()
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000793 endif()
794endfunction()
795
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000796if(NOT LLVM_TOOLCHAIN_TOOLS)
797 set (LLVM_TOOLCHAIN_TOOLS
798 llvm-ar
Chris Bieneman6d43afc2015-09-14 23:09:06 +0000799 llvm-ranlib
800 llvm-lib
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000801 llvm-objdump
802 )
803endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000804
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000805macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000806 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000807 set(EXCLUDE_FROM_ALL ON)
808 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000809 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000810
Chris Bieneman44d29902016-09-27 17:47:24 +0000811 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Hans Wennborg16546272013-08-24 00:20:36 +0000812 if( LLVM_BUILD_TOOLS )
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000813 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
814 NOT LLVM_DISTRIBUTION_COMPONENTS)
815 set(export_to_llvmexports EXPORT LLVMExports)
Justin Bogner80bee972016-11-08 05:02:18 +0000816 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000817 endif()
818
NAKAMURA Takumief976072014-02-09 16:36:03 +0000819 install(TARGETS ${name}
Justin Bogner4ce3e0c2016-11-04 21:55:23 +0000820 ${export_to_llvmexports}
Chris Bieneman2724f142016-06-08 21:19:26 +0000821 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000822 COMPONENT ${name})
823
824 if (NOT CMAKE_CONFIGURATION_TYPES)
825 add_custom_target(install-${name}
826 DEPENDS ${name}
827 COMMAND "${CMAKE_COMMAND}"
828 -DCMAKE_INSTALL_COMPONENT=${name}
829 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
830 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000831 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000832 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000833 if( LLVM_BUILD_TOOLS )
834 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
835 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000836 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000837endmacro(add_llvm_tool name)
838
839
840macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000841 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000842 set(EXCLUDE_FROM_ALL ON)
843 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000844 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000845 if( LLVM_BUILD_EXAMPLES )
846 install(TARGETS ${name} RUNTIME DESTINATION examples)
847 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000848 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000849endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000850
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000851# This is a macro that is used to create targets for executables that are needed
852# for development, but that are not intended to be installed by default.
Oscar Fuentes3145e922011-02-20 22:06:10 +0000853macro(add_llvm_utility name)
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000854 if ( NOT LLVM_BUILD_UTILS )
855 set(EXCLUDE_FROM_ALL ON)
856 endif()
857
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000858 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
Oscar Fuentes3145e922011-02-20 22:06:10 +0000859 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000860 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
Derek Schuffef9928e2015-03-27 16:53:06 +0000861 install (TARGETS ${name}
862 RUNTIME DESTINATION bin
863 COMPONENT ${name})
864 if (NOT CMAKE_CONFIGURATION_TYPES)
865 add_custom_target(install-${name}
866 DEPENDS ${name}
867 COMMAND "${CMAKE_COMMAND}"
868 -DCMAKE_INSTALL_COMPONENT=${name}
869 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
870 endif()
871 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000872endmacro(add_llvm_utility name)
873
874
Oscar Fuentescdc95492008-09-26 04:40:32 +0000875macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000876 include_directories(BEFORE
877 ${CMAKE_CURRENT_BINARY_DIR}
878 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000879 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000880 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000881endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000882
Chris Bieneman580d8152015-07-20 20:36:06 +0000883function(canonicalize_tool_name name output)
884 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
885 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
886 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
887 set(${output} "${nameUPPER}" PARENT_SCOPE)
888endfunction(canonicalize_tool_name)
889
Chris Bieneman70997c32015-10-20 16:42:58 +0000890# Custom add_subdirectory wrapper
Tobias Grossere3421b52016-04-29 13:03:40 +0000891# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
892# path if it differs from the name.
Chris Bieneman70997c32015-10-20 16:42:58 +0000893macro(add_llvm_subdirectory project type name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000894 set(add_llvm_external_dir "${ARGN}")
895 if("${add_llvm_external_dir}" STREQUAL "")
896 set(add_llvm_external_dir ${name})
897 endif()
Chris Bieneman580d8152015-07-20 20:36:06 +0000898 canonicalize_tool_name(${name} nameUPPER)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000899 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
900 # Treat it as in-tree subproject.
Chris Bieneman70997c32015-10-20 16:42:58 +0000901 option(${project}_${type}_${nameUPPER}_BUILD
902 "Whether to build ${name} as part of ${project}" On)
903 mark_as_advanced(${project}_${type}_${name}_BUILD)
904 if(${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000905 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
906 # Don't process it in add_llvm_implicit_projects().
Chris Bieneman70997c32015-10-20 16:42:58 +0000907 set(${project}_${type}_${nameUPPER}_BUILD OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000908 endif()
909 else()
910 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
911 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
912 CACHE PATH "Path to ${name} source directory")
Chris Bieneman70997c32015-10-20 16:42:58 +0000913 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000914 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
Chris Bieneman70997c32015-10-20 16:42:58 +0000915 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000916 endif()
917 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
Chris Bieneman70997c32015-10-20 16:42:58 +0000918 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000919 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000920 option(${project}_${type}_${nameUPPER}_BUILD
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000921 "Whether to build ${name} as part of LLVM"
Chris Bieneman70997c32015-10-20 16:42:58 +0000922 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
923 if (${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi6fa85322015-08-22 05:11:02 +0000924 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
925 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
926 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
927 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
928 endif()
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000929 # FIXME: It'd be redundant.
Chris Bieneman70997c32015-10-20 16:42:58 +0000930 set(${project}_${type}_${nameUPPER}_BUILD Off)
Michael J. Spencere734f542012-04-26 19:43:35 +0000931 endif()
932 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000933endmacro()
934
935# Add external project that may want to be built as part of llvm such as Clang,
936# lld, and Polly. This adds two options. One for the source directory of the
937# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
938# enable or disable building it with everything else.
939# Additional parameter can be specified as the name of directory.
940macro(add_llvm_external_project name)
Manuel Klimekf80abb92015-10-22 08:31:46 +0000941 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
Chris Bieneman70997c32015-10-20 16:42:58 +0000942endmacro()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000943
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000944macro(add_llvm_tool_subdirectory name)
Chris Bieneman580d8152015-07-20 20:36:06 +0000945 add_llvm_external_project(${name})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000946endmacro(add_llvm_tool_subdirectory)
947
Chris Bieneman580d8152015-07-20 20:36:06 +0000948function(get_project_name_from_src_var var output)
949 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
950 MACHED_TOOL "${var}")
951 if(MACHED_TOOL)
952 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
953 else()
954 set(${output} PARENT_SCOPE)
955 endif()
956endfunction()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000957
Chris Bieneman70997c32015-10-20 16:42:58 +0000958function(create_subdirectory_options project type)
Chris Bieneman580d8152015-07-20 20:36:06 +0000959 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
960 foreach(dir ${sub-dirs})
961 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
962 canonicalize_tool_name(${dir} name)
Chris Bieneman70997c32015-10-20 16:42:58 +0000963 option(${project}_${type}_${name}_BUILD
964 "Whether to build ${name} as part of ${project}" On)
965 mark_as_advanced(${project}_${type}_${name}_BUILD)
Chris Bieneman580d8152015-07-20 20:36:06 +0000966 endif()
967 endforeach()
Chris Bieneman70997c32015-10-20 16:42:58 +0000968endfunction(create_subdirectory_options)
969
970function(create_llvm_tool_options)
971 create_subdirectory_options(LLVM TOOL)
Chris Bieneman580d8152015-07-20 20:36:06 +0000972endfunction(create_llvm_tool_options)
973
Chris Bieneman74c98f02016-03-08 18:43:28 +0000974function(llvm_add_implicit_projects project)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000975 set(list_of_implicit_subdirs "")
976 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
977 foreach(dir ${sub-dirs})
Chris Bieneman580d8152015-07-20 20:36:06 +0000978 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
979 canonicalize_tool_name(${dir} name)
Chris Bieneman74c98f02016-03-08 18:43:28 +0000980 if (${project}_TOOL_${name}_BUILD)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000981 get_filename_component(fn "${dir}" NAME)
982 list(APPEND list_of_implicit_subdirs "${fn}")
983 endif()
984 endif()
985 endforeach()
986
987 foreach(external_proj ${list_of_implicit_subdirs})
Chris Bieneman74c98f02016-03-08 18:43:28 +0000988 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000989 endforeach()
Chris Bieneman74c98f02016-03-08 18:43:28 +0000990endfunction(llvm_add_implicit_projects)
991
992function(add_llvm_implicit_projects)
993 llvm_add_implicit_projects(LLVM)
Chris Bieneman580d8152015-07-20 20:36:06 +0000994endfunction(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000995
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000996# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000997function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000998 if( NOT LLVM_BUILD_TESTS )
999 set(EXCLUDE_FROM_ALL ON)
1000 endif()
1001
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001002 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1003 if (NOT LLVM_ENABLE_THREADS)
1004 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1005 endif ()
1006
1007 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +00001008 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001009 endif ()
1010
NAKAMURA Takumia679f432014-01-28 09:44:06 +00001011 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00001012
Andrew Wilkins095e22d2016-02-12 01:42:43 +00001013 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
Jonas Hahnfeld277a09d2016-11-03 06:58:16 +00001014 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +00001015 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
John Brawnc11ef2a2015-09-30 15:20:51 +00001016 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
Artem Belevichdf7103d2016-06-21 19:34:40 +00001017 # libpthreads overrides some standard library symbols, so main
1018 # executable must be linked with it in order to provide consistent
1019 # API for all shared libaries loaded by this executable.
1020 target_link_libraries(${test_name} gtest_main gtest ${PTHREAD_LIB})
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001021
1022 add_dependencies(${test_suite} ${test_name})
1023 get_target_property(test_suite_folder ${test_suite} FOLDER)
1024 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1025 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1026 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +00001027endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001028
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001029function(llvm_add_go_executable binary pkgpath)
1030 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1031
1032 if(LLVM_BINDINGS MATCHES "go")
1033 # FIXME: This should depend only on the libraries Go needs.
1034 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1035 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1036 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1037 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +00001038 set(cppflags "")
1039 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1040 foreach(d ${include_dirs})
1041 set(cppflags "${cppflags} -I${d}")
1042 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001043 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1044 add_custom_command(OUTPUT ${binpath}
Andrew Wilkins5bf7d812016-07-27 03:21:51 +00001045 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 +00001046 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1047 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1048 ${llvmlibs} ${ARG_DEPENDS}
1049 COMMENT "Building Go executable ${binary}"
1050 VERBATIM)
1051 if (ARG_ALL)
1052 add_custom_target(${binary} ALL DEPENDS ${binpath})
1053 else()
1054 add_custom_target(${binary} DEPENDS ${binpath})
1055 endif()
1056 endif()
1057endfunction()
1058
Chandler Carruth3511dd32012-06-28 06:36:24 +00001059# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +00001060# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +00001061# variables needed for the 'lit.site.cfg' files. This function bundles the
1062# common variables that any Lit instance is likely to need, and custom
1063# variables can be passed in.
1064function(configure_lit_site_cfg input output)
1065 foreach(c ${LLVM_TARGETS_TO_BUILD})
1066 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1067 endforeach(c)
1068 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1069
1070 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +00001071
Chandler Carruth3511dd32012-06-28 06:36:24 +00001072 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +00001073 if (CMAKE_CFG_INTDIR STREQUAL ".")
1074 set(LLVM_BUILD_MODE ".")
1075 else ()
1076 set(LLVM_BUILD_MODE "%(build_mode)s")
1077 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001078
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001079 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +00001080 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1081 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +00001082 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
1083 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001084
1085 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +00001086 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001087
Chandler Carruth3511dd32012-06-28 06:36:24 +00001088 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +00001089 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1090 # plugins. We may rename it.
1091 if(LLVM_ENABLE_PLUGINS)
1092 set(ENABLE_SHARED "1")
1093 else()
1094 set(ENABLE_SHARED "0")
1095 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001096
1097 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1098 set(ENABLE_ASSERTIONS "1")
1099 else()
1100 set(ENABLE_ASSERTIONS "0")
1101 endif()
1102
Kuba Breckaa62bbcc2015-01-24 01:42:44 +00001103 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +00001104 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +00001105
Peter Collingbournece800842014-10-17 18:32:36 +00001106 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1107 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +00001108 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +00001109
Alex Denisovd5cee4a2016-04-16 06:47:41 +00001110 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${input}\n## Do not edit!")
1111
Chandler Carruth3511dd32012-06-28 06:36:24 +00001112 configure_file(${input} ${output} @ONLY)
1113endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001114
1115# A raw function to create a lit target. This is used to implement the testuite
1116# management functions.
1117function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001118 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001119 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1120 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +00001121 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1122 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1123 endif ()
Michal Gornybab79432016-10-04 20:25:37 +00001124 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1125 # reset cache after erraneous r283029
1126 # TODO: remove this once all buildbots run
1127 if (LIT_COMMAND STREQUAL "${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py")
1128 unset(LIT_COMMAND CACHE)
1129 endif()
1130 set (LIT_COMMAND "${PYTHON_EXECUTABLE};${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py"
1131 CACHE STRING "Command used to spawn llvm-lit")
Greg Fitzgerald962a3392014-05-21 16:44:03 +00001132 else()
Michal Gornybab79432016-10-04 20:25:37 +00001133 find_program(LIT_COMMAND NAMES llvm-lit lit.py lit)
Greg Fitzgerald962a3392014-05-21 16:44:03 +00001134 endif ()
1135 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001136 foreach(param ${ARG_PARAMS})
1137 list(APPEND LIT_COMMAND --param ${param})
1138 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001139 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001140 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001141 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001142 COMMENT "${comment}"
Chris Bienemandf04e792016-06-08 22:19:25 +00001143 USES_TERMINAL
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001144 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001145 else()
1146 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +00001147 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001148 message(STATUS "${target} does nothing.")
1149 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +00001150 if (ARG_DEPENDS)
1151 add_dependencies(${target} ${ARG_DEPENDS})
1152 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +00001153
1154 # Tests should be excluded from "Build Solution".
1155 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +00001156endfunction()
1157
1158# A function to add a set of lit test suites to be driven through 'check-*' targets.
1159function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001160 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001161
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001162 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1163 if(NOT EXCLUDE_FROM_ALL)
1164 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001165 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001166 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1167 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1168 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1169 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001170
1171 # Produce a specific suffixed check rule.
1172 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001173 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +00001174 PARAMS ${ARG_PARAMS}
1175 DEPENDS ${ARG_DEPENDS}
1176 ARGS ${ARG_ARGS}
1177 )
1178endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001179
1180function(add_lit_testsuites project directory)
1181 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001182 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Justin Bognere8894622016-05-06 21:57:30 +00001183
1184 # Search recursively for test directories by assuming anything not
1185 # in a directory called Inputs contains tests.
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001186 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1187 foreach(lit_suite ${to_process})
1188 if(NOT IS_DIRECTORY ${lit_suite})
1189 continue()
1190 endif()
1191 string(FIND ${lit_suite} Inputs is_inputs)
Justin Bogner25ef9e32016-07-25 18:07:14 +00001192 string(FIND ${lit_suite} Output is_output)
1193 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001194 continue()
1195 endif()
Justin Bognere8894622016-05-06 21:57:30 +00001196
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001197 # Create a check- target for the directory.
1198 string(REPLACE ${directory} "" name_slash ${lit_suite})
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001199 if (name_slash)
1200 string(REPLACE "/" "-" name_slash ${name_slash})
1201 string(REPLACE "\\" "-" name_dashes ${name_slash})
1202 string(TOLOWER "${project}${name_dashes}" name_var)
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001203 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1204 ${lit_suite}
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001205 PARAMS ${ARG_PARAMS}
1206 DEPENDS ${ARG_DEPENDS}
1207 ARGS ${ARG_ARGS}
1208 )
1209 endif()
1210 endforeach()
1211 endif()
1212endfunction()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001213
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001214function(llvm_install_library_symlink name dest type)
1215 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1216 foreach(path ${CMAKE_MODULE_PATH})
1217 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1218 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1219 break()
1220 endif()
1221 endforeach()
1222
1223 set(component ${ARG_COMPONENT})
1224 if(NOT component)
1225 set(component ${name})
1226 endif()
1227
1228 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1229 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1230
Chris Bieneman021f8742015-11-11 16:19:39 +00001231 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001232 if(WIN32 AND "${type}" STREQUAL "SHARED")
1233 set(output_dir bin)
1234 endif()
1235
1236 install(SCRIPT ${INSTALL_SYMLINK}
1237 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1238 COMPONENT ${component})
1239
1240 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1241 add_custom_target(install-${name}
1242 DEPENDS ${name} ${dest} install-${dest}
1243 COMMAND "${CMAKE_COMMAND}"
1244 -DCMAKE_INSTALL_COMPONENT=${name}
1245 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1246 endif()
1247endfunction()
1248
Chris Bienemanbff03b02015-09-16 20:49:59 +00001249function(llvm_install_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001250 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman08249702015-09-18 17:39:58 +00001251 foreach(path ${CMAKE_MODULE_PATH})
1252 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1253 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1254 break()
1255 endif()
1256 endforeach()
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +00001257
Chris Bieneman895d96a2015-09-18 21:08:32 +00001258 if(ARG_ALWAYS_GENERATE)
1259 set(component ${dest})
1260 else()
1261 set(component ${name})
1262 endif()
1263
Chris Bieneman11a71602015-10-16 23:17:13 +00001264 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1265 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1266
Chris Bieneman08249702015-09-18 17:39:58 +00001267 install(SCRIPT ${INSTALL_SYMLINK}
Chris Bieneman2724f142016-06-08 21:19:26 +00001268 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
Chris Bieneman895d96a2015-09-18 21:08:32 +00001269 COMPONENT ${component})
Chris Bienemanbff03b02015-09-16 20:49:59 +00001270
Chris Bieneman895d96a2015-09-18 21:08:32 +00001271 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
Chris Bienemanbff03b02015-09-16 20:49:59 +00001272 add_custom_target(install-${name}
1273 DEPENDS ${name} ${dest} install-${dest}
1274 COMMAND "${CMAKE_COMMAND}"
1275 -DCMAKE_INSTALL_COMPONENT=${name}
1276 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1277 endif()
1278endfunction()
1279
Chris Bienemanbde7e452015-09-15 02:15:53 +00001280function(add_llvm_tool_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001281 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001282 if(UNIX)
1283 set(LLVM_LINK_OR_COPY create_symlink)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001284 set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001285 else()
1286 set(LLVM_LINK_OR_COPY copy)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001287 set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001288 endif()
1289
1290 set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1291
Chris Bieneman6ee60bc2016-09-16 22:19:19 +00001292 set(target_name ${name})
1293 if(TARGET ${name})
1294 set(target_name ${name}-link)
1295 endif()
1296
1297
Chris Bieneman895d96a2015-09-18 21:08:32 +00001298 if(ARG_ALWAYS_GENERATE)
1299 set_property(DIRECTORY APPEND PROPERTY
1300 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1301 add_custom_command(TARGET ${dest} POST_BUILD
1302 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1303 else()
1304 add_custom_command(OUTPUT ${output_path}
Chris Bienemanbde7e452015-09-15 02:15:53 +00001305 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1306 DEPENDS ${dest})
Chris Bieneman6ee60bc2016-09-16 22:19:19 +00001307 add_custom_target(${target_name} ALL DEPENDS ${output_path})
1308 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001309
Chris Bieneman44d29902016-09-27 17:47:24 +00001310 # Make sure both the link and target are toolchain tools
Petr Hosekacac3ab2016-09-30 06:29:28 +00001311 if (${name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${dest} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1312 set(TOOL_IS_TOOLCHAIN ON)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001313 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001314
Petr Hosekacac3ab2016-09-30 06:29:28 +00001315 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
Chris Bieneman44d29902016-09-27 17:47:24 +00001316 llvm_install_symlink(${name} ${dest})
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001317 endif()
1318 endif()
1319endfunction()
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001320
1321function(llvm_externalize_debuginfo name)
1322 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1323 return()
1324 endif()
1325
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001326 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1327 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1328 endif()
1329
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001330 if(APPLE)
1331 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1332 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1333
1334 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bienemanccabd0e2015-12-03 22:51:08 +00001335 set_property(TARGET ${name} APPEND_STRING PROPERTY
1336 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001337 endif()
1338 add_custom_command(TARGET ${name} POST_BUILD
1339 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001340 ${strip_command}
1341 )
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001342 else()
1343 message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1344 endif()
1345endfunction()
Chris Bieneman61e900b2016-11-01 17:44:58 +00001346
1347function(llvm_setup_rpath name)
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001348 if(CMAKE_INSTALL_RPATH)
1349 return()
1350 endif()
1351
Chris Bieneman61e900b2016-11-01 17:44:58 +00001352 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1353 set(extra_libdir ${LLVM_LIBRARY_DIR})
1354 elseif(LLVM_BUILD_LIBRARY_DIR)
1355 set(extra_libdir ${LLVM_LIBRARY_DIR})
1356 endif()
1357
1358 if (APPLE)
1359 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1360 set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1361 elseif(UNIX)
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001362 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1363 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1364 set_property(TARGET ${name} APPEND_STRING PROPERTY
1365 LINK_FLAGS " -Wl,-z,origin ")
1366 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT LLVM_LINKER_IS_GOLD)
1367 # $ORIGIN is not interpreted at link time by ld.bfd
1368 set_property(TARGET ${name} APPEND_STRING PROPERTY
1369 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
Chris Bieneman61e900b2016-11-01 17:44:58 +00001370 endif()
1371 else()
1372 return()
1373 endif()
1374
Mandeep Singh Grang96999de2016-11-08 00:45:05 +00001375 set_target_properties(${name} PROPERTIES
1376 BUILD_WITH_INSTALL_RPATH On
1377 INSTALL_RPATH "${_install_rpath}"
1378 ${_install_name_dir})
Chris Bieneman61e900b2016-11-01 17:44:58 +00001379endfunction()