blob: 582f318e42111b9a613cd5f3056032e441b86d51 [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()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000018 else()
19 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000020 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000021 elseif(MSVC)
22 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000023 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000024 endif()
25 endif()
26
Dan Liewa5bdc842014-07-22 15:41:18 +000027 # LLVM_REQUIRES_RTTI is an internal flag that individual
28 # targets can use to force RTTI
Tom Stellard18bf6262015-11-04 20:57:43 +000029 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
Dan Liewa5bdc842014-07-22 15:41:18 +000030 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
Tom Stellard18bf6262015-11-04 20:57:43 +000031 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000032 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
33 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000034 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000035 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000036 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000037 endif ()
38 endif()
39
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000040 # Assume that;
41 # - LLVM_COMPILE_FLAGS is list.
42 # - PROPERTY COMPILE_FLAGS is string.
Andrew Wilkins92113962015-09-01 03:14:31 +000043 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000044
NAKAMURA Takumia679f432014-01-28 09:44:06 +000045 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000046 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000047 get_filename_component(suf ${fn} EXT)
48 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000049 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
50 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000051 endif()
52 endforeach()
53 else()
54 # Update target props, since all sources are C++.
55 set_property(TARGET ${name} APPEND_STRING PROPERTY
56 COMPILE_FLAGS "${target_compile_flags}")
57 endif()
58
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000059 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
60endfunction()
61
Nico Weberc27118d2013-12-28 23:31:44 +000062function(add_llvm_symbol_exports target_name export_file)
63 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000064 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000065 add_custom_command(OUTPUT ${native_export_file}
66 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000067 DEPENDS ${export_file}
68 VERBATIM
69 COMMENT "Creating export file for ${target_name}")
70 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000071 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Chandler Carruth2aff7502016-07-19 22:46:39 +000072 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
73 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
74 LINK_FLAGS " -Wl,-bE:${export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000075 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
76 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000077 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000078 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000079 add_custom_command(OUTPUT ${native_export_file}
80 COMMAND echo "{" > ${native_export_file}
81 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
82 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
83 COMMAND echo " local: *;" >> ${native_export_file}
84 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000085 DEPENDS ${export_file}
86 VERBATIM
87 COMMENT "Creating export file for ${target_name}")
Rafael Espindolaec1d0692015-06-22 12:34:54 +000088 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
89 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
90 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
91 else()
92 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
93 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
94 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000095 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000096 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000097
NAKAMURA Takumi81210752013-12-29 16:15:18 +000098 add_custom_command(OUTPUT ${native_export_file}
NAKAMURA Takumi3eb06662015-07-05 08:56:38 +000099 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
100 < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000101 DEPENDS ${export_file}
102 VERBATIM
103 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000104 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
105 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000106 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000107 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000108 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
109 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000110 endif()
111
112 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000113 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000114
115 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
116 foreach(src ${srcs})
117 get_filename_component(extension ${src} EXT)
118 if(extension STREQUAL ".cpp")
119 set(first_source_file ${src})
120 break()
121 endif()
122 endforeach()
123
124 # Force re-linking when the exports file changes. Actually, it
125 # forces recompilation of the source file. The LINK_DEPENDS target
126 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000127 # FIXME: This is not safe because this will create the same target
128 # ${native_export_file} in several different file:
129 # - One where we emitted ${target_name}_exports
130 # - One where we emitted the build command for the following object.
131 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
132 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000133
134 set_property(DIRECTORY APPEND
135 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
136
137 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000138
139 # Add dependency to *_exports later -- CMake issue 14747
140 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
141 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000142endfunction(add_llvm_symbol_exports)
143
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000144if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000145 execute_process(
146 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
147 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000148 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000149 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000150 if("${stdout}" MATCHES "GNU gold")
151 set(LLVM_LINKER_IS_GOLD ON)
152 endif()
153endif()
154
Rafael Espindola8cb78762014-11-02 12:14:22 +0000155function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000156 # Don't use linker optimizations in debug builds since it slows down the
157 # linker in a context where the optimizations are not important.
158 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000159
Rafael Espindola24766f92015-04-06 15:04:31 +0000160 # Pass -O3 to the linker. This enabled different optimizations on different
161 # linkers.
Chandler Carruth2aff7502016-07-19 22:46:39 +0000162 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000163 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000164 LINK_FLAGS " -Wl,-O3")
165 endif()
166
167 if(LLVM_LINKER_IS_GOLD)
168 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000169 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
170 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000171 # Note that there is a bug with -Wl,--icf=safe so it is not safe
172 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
173 endif()
174
175 if(NOT LLVM_NO_DEAD_STRIP)
176 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
177 # ld64's implementation of -dead_strip breaks tools that use plugins.
178 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
179 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindolac32ed572015-06-22 15:06:17 +0000180 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
181 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
182 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000183 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
184 # Object files are compiled with -ffunction-data-sections.
185 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
186 # tools that use plugins. Always pass --gc-sections once we require
187 # a newer linker.
188 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
189 LINK_FLAGS " -Wl,--gc-sections")
190 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000191 endif()
192 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000193endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000194
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000195# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
196# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
197# or a certain builder, for eaxample, msbuild.exe, would be confused.
John Brawnc11ef2a2015-09-30 15:20:51 +0000198function(set_output_directory target)
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +0000199 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000200
John Brawnc11ef2a2015-09-30 15:20:51 +0000201 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000202 # It affects output of add_library(MODULE).
203 if(WIN32 OR CYGWIN)
204 # DLL platform
John Brawnc11ef2a2015-09-30 15:20:51 +0000205 set(module_dir ${ARG_BINARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000206 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000207 set(module_dir ${ARG_LIBRARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000208 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000209 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
210 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
211 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
John Brawnc11ef2a2015-09-30 15:20:51 +0000212 if(ARG_BINARY_DIR)
213 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
214 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
215 endif()
216 if(ARG_LIBRARY_DIR)
217 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
218 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
219 endif()
220 if(module_dir)
221 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
222 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
223 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000224 endforeach()
225 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000226 if(ARG_BINARY_DIR)
227 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
228 endif()
229 if(ARG_LIBRARY_DIR)
230 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
231 endif()
232 if(module_dir)
233 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
234 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000235 endif()
236endfunction()
237
Greg Bedwell95213c32015-06-12 15:58:29 +0000238# If on Windows and building with MSVC, add the resource script containing the
239# VERSIONINFO data to the project. This embeds version resource information
240# into the output .exe or .dll.
241# TODO: Enable for MinGW Windows builds too.
242#
243function(add_windows_version_resource_file OUT_VAR)
244 set(sources ${ARGN})
245 if (MSVC)
246 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000247 if(EXISTS ${resource_file})
248 set(sources ${sources} ${resource_file})
249 source_group("Resource Files" ${resource_file})
250 set(windows_resource_file ${resource_file} PARENT_SCOPE)
251 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000252 endif(MSVC)
253
254 set(${OUT_VAR} ${sources} PARENT_SCOPE)
255endfunction(add_windows_version_resource_file)
256
257# set_windows_version_resource_properties(name resource_file...
258# VERSION_MAJOR int
259# Optional major version number (defaults to LLVM_VERSION_MAJOR)
260# VERSION_MINOR int
261# Optional minor version number (defaults to LLVM_VERSION_MINOR)
262# VERSION_PATCHLEVEL int
263# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
264# VERSION_STRING
265# Optional version string (defaults to PACKAGE_VERSION)
266# PRODUCT_NAME
267# Optional product name string (defaults to "LLVM")
268# )
269function(set_windows_version_resource_properties name resource_file)
270 cmake_parse_arguments(ARG
271 ""
272 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
273 ""
274 ${ARGN})
275
276 if (NOT DEFINED ARG_VERSION_MAJOR)
277 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
278 endif()
279
280 if (NOT DEFINED ARG_VERSION_MINOR)
281 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
282 endif()
283
284 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
285 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
286 endif()
287
288 if (NOT DEFINED ARG_VERSION_STRING)
289 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
290 endif()
291
292 if (NOT DEFINED ARG_PRODUCT_NAME)
293 set(ARG_PRODUCT_NAME "LLVM")
294 endif()
295
296 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000297 PROPERTY COMPILE_FLAGS /nologo)
298 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000299 PROPERTY COMPILE_DEFINITIONS
300 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
301 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
302 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
303 "RC_VERSION_FIELD_4=0"
304 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
305 "RC_INTERNAL_NAME=\"${name}\""
306 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
307 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
308endfunction(set_windows_version_resource_properties)
309
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000310# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000311# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000312# STATIC by default w/o BUILD_SHARED_LIBS.
313# SHARED by default w/ BUILD_SHARED_LIBS.
Axel Naumannb457beb2016-01-12 07:44:58 +0000314# OBJECT
315# Also create an OBJECT library target. Default if STATIC && SHARED.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000316# MODULE
317# Target ${name} might not be created on unsupported platforms.
318# Check with "if(TARGET ${name})".
Andrew Wilkins92113962015-09-01 03:14:31 +0000319# DISABLE_LLVM_LINK_LLVM_DYLIB
320# Do not link this library to libLLVM, even if
321# LLVM_LINK_LLVM_DYLIB is enabled.
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000322# OUTPUT_NAME name
323# Corresponds to OUTPUT_NAME in target properties.
324# DEPENDS targets...
325# Same semantics as add_dependencies().
326# LINK_COMPONENTS components...
327# Same as the variable LLVM_LINK_COMPONENTS.
328# LINK_LIBS lib_targets...
329# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000330# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000331# May specify header files for IDE generators.
Chris Bieneman5e96fe92015-11-04 23:11:12 +0000332# SONAME
333# Should set SONAME link flags and create symlinks
John Brawn3546c2f2016-05-26 11:16:43 +0000334# PLUGIN_TOOL
335# The tool (i.e. cmake target) that this plugin will link against
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000336# )
337function(llvm_add_library name)
338 cmake_parse_arguments(ARG
Axel Naumannb457beb2016-01-12 07:44:58 +0000339 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
John Brawn3546c2f2016-05-26 11:16:43 +0000340 "OUTPUT_NAME;PLUGIN_TOOL"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000341 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000342 ${ARGN})
343 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000344 if(ARG_ADDITIONAL_HEADERS)
345 # Pass through ADDITIONAL_HEADERS.
346 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
347 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000348 if(ARG_OBJLIBS)
349 set(ALL_FILES ${ARG_OBJLIBS})
350 else()
351 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
352 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000353
354 if(ARG_MODULE)
355 if(ARG_SHARED OR ARG_STATIC)
356 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
357 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000358 # Plugins that link against a tool are allowed even when plugins in general are not
359 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000360 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
361 return()
362 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000363 else()
John Brawn3546c2f2016-05-26 11:16:43 +0000364 if(ARG_PLUGIN_TOOL)
365 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
366 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000367 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
368 set(ARG_SHARED TRUE)
369 endif()
370 if(NOT ARG_SHARED)
371 set(ARG_STATIC TRUE)
372 endif()
373 endif()
374
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000375 # Generate objlib
Axel Naumannb457beb2016-01-12 07:44:58 +0000376 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000377 # Generate an obj library for both targets.
378 set(obj_name "obj.${name}")
379 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
380 ${ALL_FILES}
381 )
382 llvm_update_compile_flags(${obj_name})
383 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
384
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000385 # Do add_dependencies(obj) later due to CMake issue 14747.
386 list(APPEND objlibs ${obj_name})
387
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000388 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
389 endif()
390
391 if(ARG_SHARED AND ARG_STATIC)
392 # static
393 set(name_static "${name}_static")
394 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000395 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000396 endif()
397 # DEPENDS has been appended to LLVM_COMMON_LIBS.
398 llvm_add_library(${name_static} STATIC
399 ${output_name}
400 OBJLIBS ${ALL_FILES} # objlib
401 LINK_LIBS ${ARG_LINK_LIBS}
402 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
403 )
404 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
405 set(ARG_STATIC)
406 endif()
407
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000408 if(ARG_MODULE)
409 add_library(${name} MODULE ${ALL_FILES})
410 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000411 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000412 add_library(${name} SHARED ${ALL_FILES})
413 else()
414 add_library(${name} STATIC ${ALL_FILES})
415 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000416
417 if(DEFINED windows_resource_file)
418 set_windows_version_resource_properties(${name} ${windows_resource_file})
419 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
420 endif()
421
John Brawnc11ef2a2015-09-30 15:20:51 +0000422 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000423 # $<TARGET_OBJECTS> doesn't require compile flags.
424 if(NOT obj_name)
425 llvm_update_compile_flags(${name})
426 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000427 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000428 if(ARG_OUTPUT_NAME)
429 set_target_properties(${name}
430 PROPERTIES
431 OUTPUT_NAME ${ARG_OUTPUT_NAME}
432 )
433 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000434
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000435 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000436 set_target_properties(${name} PROPERTIES
437 PREFIX ""
438 SUFFIX ${LLVM_PLUGIN_EXT}
439 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000440 endif()
441
442 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000443 if(WIN32)
444 set_target_properties(${name} PROPERTIES
445 PREFIX ""
446 )
447 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000448 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000449
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000450 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000451 # Do not add -Dname_EXPORTS to the command-line when building files in this
452 # target. Doing so is actively harmful for the modules build because it
453 # creates extra module variants, and not useful because we don't use these
454 # macros.
455 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
456
Nico Weber06473f82013-12-29 05:39:01 +0000457 if (LLVM_EXPORTED_SYMBOL_FILE)
458 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
459 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000460 endif()
461
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000462 if(ARG_SHARED AND UNIX)
463 if(NOT APPLE AND ARG_SONAME)
464 get_target_property(output_name ${name} OUTPUT_NAME)
465 if(${output_name} STREQUAL "output_name-NOTFOUND")
466 set(output_name ${name})
467 endif()
468 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
469 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
470 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
471 llvm_install_library_symlink(${api_name} ${library_name} SHARED
472 COMPONENT ${name}
473 ALWAYS_GENERATE)
474 llvm_install_library_symlink(${output_name} ${library_name} SHARED
475 COMPONENT ${name}
476 ALWAYS_GENERATE)
477 endif()
478 endif()
479
John Brawn3546c2f2016-05-26 11:16:43 +0000480 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
481 # On DLL platforms symbols are imported from the tool by linking against it.
482 set(llvm_libs ${ARG_PLUGIN_TOOL})
483 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000484 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
485 set(llvm_libs LLVM)
486 else()
487 llvm_map_components_to_libnames(llvm_libs
488 ${ARG_LINK_COMPONENTS}
489 ${LLVM_LINK_COMPONENTS}
490 )
491 endif()
Andrew Wilkins92113962015-09-01 03:14:31 +0000492 else()
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000493 # Components have not been defined explicitly in CMake, so add the
494 # dependency information for this library as defined by LLVMBuild.
495 #
496 # It would be nice to verify that we have the dependencies for this library
497 # name, but using get_property(... SET) doesn't suffice to determine if a
498 # property has been set to an empty value.
499 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
Andrew Wilkins92113962015-09-01 03:14:31 +0000500 endif()
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000501
Chris Bieneman6650b022016-06-08 22:48:01 +0000502 if(ARG_STATIC)
503 set(libtype INTERFACE)
Steven Wuec6f56e2016-05-26 04:35:35 +0000504 else()
505 # We can use PRIVATE since SO knows its dependent libs.
Chris Bieneman6650b022016-06-08 22:48:01 +0000506 set(libtype PRIVATE)
507 endif()
508
509 target_link_libraries(${name} ${libtype}
Steven Wuec6f56e2016-05-26 04:35:35 +0000510 ${ARG_LINK_LIBS}
511 ${lib_deps}
512 ${llvm_libs}
513 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000514
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000515 if(LLVM_COMMON_DEPENDS)
516 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000517 # Add dependencies also to objlibs.
518 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
519 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000520 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
521 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000522 endif()
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000523
524 if(ARG_SHARED OR ARG_MODULE)
525 llvm_externalize_debuginfo(${name})
526 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000527endfunction()
528
529macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000530 cmake_parse_arguments(ARG
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000531 "SHARED"
Chris Bieneman84271692015-04-16 16:56:18 +0000532 ""
533 ""
534 ${ARGN})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000535 if( BUILD_SHARED_LIBS )
536 llvm_add_library(${name} SHARED ${ARGN})
537 else()
538 llvm_add_library(${name} ${ARGN})
539 endif()
Dan Liew80189d22015-06-29 18:45:56 +0000540 # The gtest libraries should not be installed or exported as a target
541 if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
542 set(_is_gtest TRUE)
543 else()
544 set(_is_gtest FALSE)
545 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
546 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000547
Nick Lewycky61aed872011-04-29 02:12:06 +0000548 if( EXCLUDE_FROM_ALL )
549 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Dan Liew80189d22015-06-29 18:45:56 +0000550 elseif(NOT _is_gtest)
Hans Wennborg16546272013-08-24 00:20:36 +0000551 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000552 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000553 if(ARG_SHARED OR BUILD_SHARED_LIBS)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000554 if(WIN32 OR CYGWIN OR MINGW)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000555 set(install_type RUNTIME)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000556 set(install_dir bin)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000557 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000558 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000559 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000560 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000561 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000562 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000563
Chris Bienemana985e6b2015-04-16 18:08:33 +0000564 install(TARGETS ${name}
565 EXPORT LLVMExports
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000566 ${install_type} DESTINATION ${install_dir}
Chris Bienemana985e6b2015-04-16 18:08:33 +0000567 COMPONENT ${name})
568
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000569 if (NOT CMAKE_CONFIGURATION_TYPES)
570 add_custom_target(install-${name}
571 DEPENDS ${name}
572 COMMAND "${CMAKE_COMMAND}"
573 -DCMAKE_INSTALL_COMPONENT=${name}
574 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
575 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000576 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000577 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000578 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000579 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000580endmacro(add_llvm_library name)
581
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000582macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000583 llvm_add_library(${name} MODULE ${ARGN})
584 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000585 # Add empty "phony" target
586 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000587 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000588 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000589 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000590 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000591 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000592 if(WIN32 OR CYGWIN)
593 # DLL platform
594 set(dlldir "bin")
595 else()
596 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
597 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000598 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000599 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000600 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000601 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
602 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000603 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000604 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000605 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000606
607 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000608endmacro(add_llvm_loadable_module name)
609
610
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000611macro(add_llvm_executable name)
Chris Bieneman48eaa542015-12-08 21:51:48 +0000612 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO" "" "" ${ARGN})
Andrew Wilkins92113962015-09-01 03:14:31 +0000613 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000614
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +0000615 # Generate objlib
616 if(LLVM_ENABLE_OBJLIB)
617 # Generate an obj library for both targets.
618 set(obj_name "obj.${name}")
619 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
620 ${ALL_FILES}
621 )
622 llvm_update_compile_flags(${obj_name})
623 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
624
625 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
626 endif()
627
NAKAMURA Takumib5288ba2015-08-28 00:36:58 +0000628 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
629
Jordan Rose3d752972015-09-10 17:18:51 +0000630 if(XCODE)
631 # Note: the dummy.cpp source file provides no definitions. However,
632 # it forces Xcode to properly link the static library.
Jordan Rose85a22f82015-09-10 17:55:02 +0000633 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
Jordan Rose3d752972015-09-10 17:18:51 +0000634 endif()
635
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000636 if( EXCLUDE_FROM_ALL )
637 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
638 else()
639 add_executable(${name} ${ALL_FILES})
640 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000641
642 if(DEFINED windows_resource_file)
643 set_windows_version_resource_properties(${name} ${windows_resource_file})
644 endif()
645
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000646 # $<TARGET_OBJECTS> doesn't require compile flags.
647 if(NOT LLVM_ENABLE_OBJLIB)
648 llvm_update_compile_flags(${name})
649 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000650 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000651
Richard Smith2b91a7f2014-09-26 22:40:15 +0000652 # Do not add -Dname_EXPORTS to the command-line when building files in this
653 # target. Doing so is actively harmful for the modules build because it
654 # creates extra module variants, and not useful because we don't use these
655 # macros.
656 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
657
Nico Weberc27118d2013-12-28 23:31:44 +0000658 if (LLVM_EXPORTED_SYMBOL_FILE)
659 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
660 endif(LLVM_EXPORTED_SYMBOL_FILE)
661
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000662 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
663 set(USE_SHARED USE_SHARED)
664 endif()
665
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000666 set(EXCLUDE_FROM_ALL OFF)
John Brawnc11ef2a2015-09-30 15:20:51 +0000667 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000668 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000669 if( LLVM_COMMON_DEPENDS )
670 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
671 endif( LLVM_COMMON_DEPENDS )
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000672
Chris Bieneman48eaa542015-12-08 21:51:48 +0000673 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
674 llvm_externalize_debuginfo(${name})
675 endif()
Artem Belevichdf7103d2016-06-21 19:34:40 +0000676 if (PTHREAD_LIB)
677 # libpthreads overrides some standard library symbols, so main
678 # executable must be linked with it in order to provide consistent
679 # API for all shared libaries loaded by this executable.
680 target_link_libraries(${name} ${PTHREAD_LIB})
681 endif()
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000682endmacro(add_llvm_executable name)
683
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000684function(export_executable_symbols target)
John Brawn3546c2f2016-05-26 11:16:43 +0000685 if (LLVM_EXPORTED_SYMBOL_FILE)
686 # The symbol file should contain the symbols we want the executable to
687 # export
688 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
689 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
690 # Extract the symbols to export from the static libraries that the
691 # executable links against.
692 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
693 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
694 # We need to consider not just the direct link dependencies, but also the
695 # transitive link dependencies. Do this by starting with the set of direct
696 # dependencies, then the dependencies of those dependencies, and so on.
697 get_target_property(new_libs ${target} LINK_LIBRARIES)
698 set(link_libs ${new_libs})
699 while(NOT "${new_libs}" STREQUAL "")
700 foreach(lib ${new_libs})
John Brawn24ca18e2016-07-05 13:16:54 +0000701 if(TARGET ${lib})
702 get_target_property(lib_type ${lib} TYPE)
703 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
704 list(APPEND static_libs ${lib})
705 else()
706 list(APPEND other_libs ${lib})
John Brawn3546c2f2016-05-26 11:16:43 +0000707 endif()
John Brawn24ca18e2016-07-05 13:16:54 +0000708 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
709 foreach(transitive_lib ${transitive_libs})
710 list(FIND link_libs ${transitive_lib} idx)
711 if(TARGET ${transitive_lib} AND idx EQUAL -1)
712 list(APPEND newer_libs ${transitive_lib})
713 list(APPEND link_libs ${transitive_lib})
714 endif()
715 endforeach(transitive_lib)
716 endif()
John Brawn3546c2f2016-05-26 11:16:43 +0000717 endforeach(lib)
718 set(new_libs ${newer_libs})
719 set(newer_libs "")
720 endwhile()
721 if (MSVC)
722 set(mangling microsoft)
723 else()
724 set(mangling itanium)
725 endif()
726 add_custom_command(OUTPUT ${exported_symbol_file}
727 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
728 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
729 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
730 VERBATIM
731 COMMENT "Generating export list for ${target}")
732 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
733 # If something links against this executable then we want a
734 # transitive link against only the libraries whose symbols
735 # we aren't exporting.
736 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
737 # The default import library suffix that cmake uses for cygwin/mingw is
738 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
739 # where the import libraries of both get named libclang.dll.a. Use a suffix
740 # of ".exe.a" to avoid this.
741 if(CYGWIN OR MINGW)
742 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
743 endif()
744 elseif(NOT (WIN32 OR CYGWIN))
745 # On Windows auto-exporting everything doesn't work because of the limit on
746 # the size of the exported symbol table, but on other platforms we can do
747 # it without any trouble.
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000748 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
Chris Bieneman914742b2015-11-24 00:58:58 +0000749 if (APPLE)
Chris Bienemanccabd0e2015-12-03 22:51:08 +0000750 set_property(TARGET ${target} APPEND_STRING PROPERTY
Chris Bienemanac6677a2015-12-03 22:55:36 +0000751 LINK_FLAGS " -rdynamic")
Chris Bieneman914742b2015-11-24 00:58:58 +0000752 endif()
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000753 endif()
754endfunction()
755
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000756if(NOT LLVM_TOOLCHAIN_TOOLS)
757 set (LLVM_TOOLCHAIN_TOOLS
758 llvm-ar
Chris Bieneman6d43afc2015-09-14 23:09:06 +0000759 llvm-ranlib
760 llvm-lib
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000761 llvm-objdump
762 )
763endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000764
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000765macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000766 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000767 set(EXCLUDE_FROM_ALL ON)
768 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000769 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000770
771 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
772 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
773 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000774 install(TARGETS ${name}
775 EXPORT LLVMExports
Chris Bieneman2724f142016-06-08 21:19:26 +0000776 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000777 COMPONENT ${name})
778
779 if (NOT CMAKE_CONFIGURATION_TYPES)
780 add_custom_target(install-${name}
781 DEPENDS ${name}
782 COMMAND "${CMAKE_COMMAND}"
783 -DCMAKE_INSTALL_COMPONENT=${name}
784 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
785 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000786 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000787 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000788 if( LLVM_BUILD_TOOLS )
789 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
790 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000791 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000792endmacro(add_llvm_tool name)
793
794
795macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000796 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000797 set(EXCLUDE_FROM_ALL ON)
798 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000799 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000800 if( LLVM_BUILD_EXAMPLES )
801 install(TARGETS ${name} RUNTIME DESTINATION examples)
802 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000803 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000804endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000805
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000806# This is a macro that is used to create targets for executables that are needed
807# for development, but that are not intended to be installed by default.
Oscar Fuentes3145e922011-02-20 22:06:10 +0000808macro(add_llvm_utility name)
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000809 if ( NOT LLVM_BUILD_UTILS )
810 set(EXCLUDE_FROM_ALL ON)
811 endif()
812
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000813 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
Oscar Fuentes3145e922011-02-20 22:06:10 +0000814 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Michael Gottesmanbe5b7872016-07-10 02:43:47 +0000815 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
Derek Schuffef9928e2015-03-27 16:53:06 +0000816 install (TARGETS ${name}
817 RUNTIME DESTINATION bin
818 COMPONENT ${name})
819 if (NOT CMAKE_CONFIGURATION_TYPES)
820 add_custom_target(install-${name}
821 DEPENDS ${name}
822 COMMAND "${CMAKE_COMMAND}"
823 -DCMAKE_INSTALL_COMPONENT=${name}
824 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
825 endif()
826 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000827endmacro(add_llvm_utility name)
828
829
Oscar Fuentescdc95492008-09-26 04:40:32 +0000830macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000831 include_directories(BEFORE
832 ${CMAKE_CURRENT_BINARY_DIR}
833 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000834 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000835 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000836endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000837
Chris Bieneman580d8152015-07-20 20:36:06 +0000838function(canonicalize_tool_name name output)
839 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
840 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
841 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
842 set(${output} "${nameUPPER}" PARENT_SCOPE)
843endfunction(canonicalize_tool_name)
844
Chris Bieneman70997c32015-10-20 16:42:58 +0000845# Custom add_subdirectory wrapper
Tobias Grossere3421b52016-04-29 13:03:40 +0000846# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
847# path if it differs from the name.
Chris Bieneman70997c32015-10-20 16:42:58 +0000848macro(add_llvm_subdirectory project type name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000849 set(add_llvm_external_dir "${ARGN}")
850 if("${add_llvm_external_dir}" STREQUAL "")
851 set(add_llvm_external_dir ${name})
852 endif()
Chris Bieneman580d8152015-07-20 20:36:06 +0000853 canonicalize_tool_name(${name} nameUPPER)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000854 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
855 # Treat it as in-tree subproject.
Chris Bieneman70997c32015-10-20 16:42:58 +0000856 option(${project}_${type}_${nameUPPER}_BUILD
857 "Whether to build ${name} as part of ${project}" On)
858 mark_as_advanced(${project}_${type}_${name}_BUILD)
859 if(${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000860 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
861 # Don't process it in add_llvm_implicit_projects().
Chris Bieneman70997c32015-10-20 16:42:58 +0000862 set(${project}_${type}_${nameUPPER}_BUILD OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000863 endif()
864 else()
865 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
866 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
867 CACHE PATH "Path to ${name} source directory")
Chris Bieneman70997c32015-10-20 16:42:58 +0000868 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000869 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
Chris Bieneman70997c32015-10-20 16:42:58 +0000870 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000871 endif()
872 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
Chris Bieneman70997c32015-10-20 16:42:58 +0000873 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000874 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000875 option(${project}_${type}_${nameUPPER}_BUILD
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000876 "Whether to build ${name} as part of LLVM"
Chris Bieneman70997c32015-10-20 16:42:58 +0000877 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
878 if (${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi6fa85322015-08-22 05:11:02 +0000879 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
880 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
881 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
882 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
883 endif()
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000884 # FIXME: It'd be redundant.
Chris Bieneman70997c32015-10-20 16:42:58 +0000885 set(${project}_${type}_${nameUPPER}_BUILD Off)
Michael J. Spencere734f542012-04-26 19:43:35 +0000886 endif()
887 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000888endmacro()
889
890# Add external project that may want to be built as part of llvm such as Clang,
891# lld, and Polly. This adds two options. One for the source directory of the
892# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
893# enable or disable building it with everything else.
894# Additional parameter can be specified as the name of directory.
895macro(add_llvm_external_project name)
Manuel Klimekf80abb92015-10-22 08:31:46 +0000896 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
Chris Bieneman70997c32015-10-20 16:42:58 +0000897endmacro()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000898
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000899macro(add_llvm_tool_subdirectory name)
Chris Bieneman580d8152015-07-20 20:36:06 +0000900 add_llvm_external_project(${name})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000901endmacro(add_llvm_tool_subdirectory)
902
Chris Bieneman580d8152015-07-20 20:36:06 +0000903function(get_project_name_from_src_var var output)
904 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
905 MACHED_TOOL "${var}")
906 if(MACHED_TOOL)
907 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
908 else()
909 set(${output} PARENT_SCOPE)
910 endif()
911endfunction()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000912
Chris Bieneman70997c32015-10-20 16:42:58 +0000913function(create_subdirectory_options project type)
Chris Bieneman580d8152015-07-20 20:36:06 +0000914 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
915 foreach(dir ${sub-dirs})
916 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
917 canonicalize_tool_name(${dir} name)
Chris Bieneman70997c32015-10-20 16:42:58 +0000918 option(${project}_${type}_${name}_BUILD
919 "Whether to build ${name} as part of ${project}" On)
920 mark_as_advanced(${project}_${type}_${name}_BUILD)
Chris Bieneman580d8152015-07-20 20:36:06 +0000921 endif()
922 endforeach()
Chris Bieneman70997c32015-10-20 16:42:58 +0000923endfunction(create_subdirectory_options)
924
925function(create_llvm_tool_options)
926 create_subdirectory_options(LLVM TOOL)
Chris Bieneman580d8152015-07-20 20:36:06 +0000927endfunction(create_llvm_tool_options)
928
Chris Bieneman74c98f02016-03-08 18:43:28 +0000929function(llvm_add_implicit_projects project)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000930 set(list_of_implicit_subdirs "")
931 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
932 foreach(dir ${sub-dirs})
Chris Bieneman580d8152015-07-20 20:36:06 +0000933 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
934 canonicalize_tool_name(${dir} name)
Chris Bieneman74c98f02016-03-08 18:43:28 +0000935 if (${project}_TOOL_${name}_BUILD)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000936 get_filename_component(fn "${dir}" NAME)
937 list(APPEND list_of_implicit_subdirs "${fn}")
938 endif()
939 endif()
940 endforeach()
941
942 foreach(external_proj ${list_of_implicit_subdirs})
Chris Bieneman74c98f02016-03-08 18:43:28 +0000943 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000944 endforeach()
Chris Bieneman74c98f02016-03-08 18:43:28 +0000945endfunction(llvm_add_implicit_projects)
946
947function(add_llvm_implicit_projects)
948 llvm_add_implicit_projects(LLVM)
Chris Bieneman580d8152015-07-20 20:36:06 +0000949endfunction(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000950
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000951# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000952function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000953 if( NOT LLVM_BUILD_TESTS )
954 set(EXCLUDE_FROM_ALL ON)
955 endif()
956
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000957 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
958 if (NOT LLVM_ENABLE_THREADS)
959 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
960 endif ()
961
962 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000963 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000964 endif ()
965
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000966 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000967
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000968 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
Chris Bieneman48eaa542015-12-08 21:51:48 +0000969 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000970 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
John Brawnc11ef2a2015-09-30 15:20:51 +0000971 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
Artem Belevichdf7103d2016-06-21 19:34:40 +0000972 # libpthreads overrides some standard library symbols, so main
973 # executable must be linked with it in order to provide consistent
974 # API for all shared libaries loaded by this executable.
975 target_link_libraries(${test_name} gtest_main gtest ${PTHREAD_LIB})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000976
977 add_dependencies(${test_suite} ${test_name})
978 get_target_property(test_suite_folder ${test_suite} FOLDER)
979 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
980 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
981 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000982endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000983
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000984function(llvm_add_go_executable binary pkgpath)
985 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
986
987 if(LLVM_BINDINGS MATCHES "go")
988 # FIXME: This should depend only on the libraries Go needs.
989 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
990 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
991 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
992 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +0000993 set(cppflags "")
994 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
995 foreach(d ${include_dirs})
996 set(cppflags "${cppflags} -I${d}")
997 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000998 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
999 add_custom_command(OUTPUT ${binpath}
Andrew Wilkinsdfd60882016-01-20 04:03:09 +00001000 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +00001001 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1002 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1003 ${llvmlibs} ${ARG_DEPENDS}
1004 COMMENT "Building Go executable ${binary}"
1005 VERBATIM)
1006 if (ARG_ALL)
1007 add_custom_target(${binary} ALL DEPENDS ${binpath})
1008 else()
1009 add_custom_target(${binary} DEPENDS ${binpath})
1010 endif()
1011 endif()
1012endfunction()
1013
Chandler Carruth3511dd32012-06-28 06:36:24 +00001014# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +00001015# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +00001016# variables needed for the 'lit.site.cfg' files. This function bundles the
1017# common variables that any Lit instance is likely to need, and custom
1018# variables can be passed in.
1019function(configure_lit_site_cfg input output)
1020 foreach(c ${LLVM_TARGETS_TO_BUILD})
1021 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1022 endforeach(c)
1023 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1024
1025 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +00001026
Chandler Carruth3511dd32012-06-28 06:36:24 +00001027 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +00001028 if (CMAKE_CFG_INTDIR STREQUAL ".")
1029 set(LLVM_BUILD_MODE ".")
1030 else ()
1031 set(LLVM_BUILD_MODE "%(build_mode)s")
1032 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001033
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001034 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +00001035 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1036 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +00001037 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
1038 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001039
1040 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +00001041 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +00001042
Chandler Carruth3511dd32012-06-28 06:36:24 +00001043 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +00001044 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1045 # plugins. We may rename it.
1046 if(LLVM_ENABLE_PLUGINS)
1047 set(ENABLE_SHARED "1")
1048 else()
1049 set(ENABLE_SHARED "0")
1050 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +00001051
1052 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1053 set(ENABLE_ASSERTIONS "1")
1054 else()
1055 set(ENABLE_ASSERTIONS "0")
1056 endif()
1057
Kuba Breckaa62bbcc2015-01-24 01:42:44 +00001058 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +00001059 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +00001060
Peter Collingbournece800842014-10-17 18:32:36 +00001061 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1062 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +00001063 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +00001064
Alex Denisovd5cee4a2016-04-16 06:47:41 +00001065 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${input}\n## Do not edit!")
1066
Chandler Carruth3511dd32012-06-28 06:36:24 +00001067 configure_file(${input} ${output} @ONLY)
1068endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001069
1070# A raw function to create a lit target. This is used to implement the testuite
1071# management functions.
1072function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001073 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001074 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1075 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +00001076 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1077 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1078 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +00001079 if (LLVM_MAIN_SRC_DIR)
1080 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1081 else()
1082 find_program(LIT_COMMAND llvm-lit)
1083 endif ()
1084 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001085 foreach(param ${ARG_PARAMS})
1086 list(APPEND LIT_COMMAND --param ${param})
1087 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001088 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001089 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001090 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001091 COMMENT "${comment}"
Chris Bienemandf04e792016-06-08 22:19:25 +00001092 USES_TERMINAL
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001093 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001094 else()
1095 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +00001096 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001097 message(STATUS "${target} does nothing.")
1098 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +00001099 if (ARG_DEPENDS)
1100 add_dependencies(${target} ${ARG_DEPENDS})
1101 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +00001102
1103 # Tests should be excluded from "Build Solution".
1104 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +00001105endfunction()
1106
1107# A function to add a set of lit test suites to be driven through 'check-*' targets.
1108function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001109 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001110
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001111 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1112 if(NOT EXCLUDE_FROM_ALL)
1113 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001114 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001115 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1116 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1117 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1118 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001119
1120 # Produce a specific suffixed check rule.
1121 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001122 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +00001123 PARAMS ${ARG_PARAMS}
1124 DEPENDS ${ARG_DEPENDS}
1125 ARGS ${ARG_ARGS}
1126 )
1127endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001128
1129function(add_lit_testsuites project directory)
1130 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001131 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Justin Bognere8894622016-05-06 21:57:30 +00001132
1133 # Search recursively for test directories by assuming anything not
1134 # in a directory called Inputs contains tests.
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001135 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1136 foreach(lit_suite ${to_process})
1137 if(NOT IS_DIRECTORY ${lit_suite})
1138 continue()
1139 endif()
1140 string(FIND ${lit_suite} Inputs is_inputs)
1141 if (NOT is_inputs EQUAL -1)
1142 continue()
1143 endif()
Justin Bognere8894622016-05-06 21:57:30 +00001144
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001145 # Create a check- target for the directory.
1146 string(REPLACE ${directory} "" name_slash ${lit_suite})
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001147 if (name_slash)
1148 string(REPLACE "/" "-" name_slash ${name_slash})
1149 string(REPLACE "\\" "-" name_dashes ${name_slash})
1150 string(TOLOWER "${project}${name_dashes}" name_var)
Justin Bogner3adaa8c2016-06-08 22:36:37 +00001151 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1152 ${lit_suite}
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001153 PARAMS ${ARG_PARAMS}
1154 DEPENDS ${ARG_DEPENDS}
1155 ARGS ${ARG_ARGS}
1156 )
1157 endif()
1158 endforeach()
1159 endif()
1160endfunction()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001161
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001162function(llvm_install_library_symlink name dest type)
1163 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1164 foreach(path ${CMAKE_MODULE_PATH})
1165 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1166 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1167 break()
1168 endif()
1169 endforeach()
1170
1171 set(component ${ARG_COMPONENT})
1172 if(NOT component)
1173 set(component ${name})
1174 endif()
1175
1176 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1177 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1178
Chris Bieneman021f8742015-11-11 16:19:39 +00001179 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001180 if(WIN32 AND "${type}" STREQUAL "SHARED")
1181 set(output_dir bin)
1182 endif()
1183
1184 install(SCRIPT ${INSTALL_SYMLINK}
1185 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1186 COMPONENT ${component})
1187
1188 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1189 add_custom_target(install-${name}
1190 DEPENDS ${name} ${dest} install-${dest}
1191 COMMAND "${CMAKE_COMMAND}"
1192 -DCMAKE_INSTALL_COMPONENT=${name}
1193 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1194 endif()
1195endfunction()
1196
Chris Bienemanbff03b02015-09-16 20:49:59 +00001197function(llvm_install_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001198 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman08249702015-09-18 17:39:58 +00001199 foreach(path ${CMAKE_MODULE_PATH})
1200 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1201 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1202 break()
1203 endif()
1204 endforeach()
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +00001205
Chris Bieneman895d96a2015-09-18 21:08:32 +00001206 if(ARG_ALWAYS_GENERATE)
1207 set(component ${dest})
1208 else()
1209 set(component ${name})
1210 endif()
1211
Chris Bieneman11a71602015-10-16 23:17:13 +00001212 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1213 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1214
Chris Bieneman08249702015-09-18 17:39:58 +00001215 install(SCRIPT ${INSTALL_SYMLINK}
Chris Bieneman2724f142016-06-08 21:19:26 +00001216 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
Chris Bieneman895d96a2015-09-18 21:08:32 +00001217 COMPONENT ${component})
Chris Bienemanbff03b02015-09-16 20:49:59 +00001218
Chris Bieneman895d96a2015-09-18 21:08:32 +00001219 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
Chris Bienemanbff03b02015-09-16 20:49:59 +00001220 add_custom_target(install-${name}
1221 DEPENDS ${name} ${dest} install-${dest}
1222 COMMAND "${CMAKE_COMMAND}"
1223 -DCMAKE_INSTALL_COMPONENT=${name}
1224 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1225 endif()
1226endfunction()
1227
Chris Bienemanbde7e452015-09-15 02:15:53 +00001228function(add_llvm_tool_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001229 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001230 if(UNIX)
1231 set(LLVM_LINK_OR_COPY create_symlink)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001232 set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001233 else()
1234 set(LLVM_LINK_OR_COPY copy)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001235 set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001236 endif()
1237
1238 set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1239
Chris Bieneman895d96a2015-09-18 21:08:32 +00001240 if(ARG_ALWAYS_GENERATE)
1241 set_property(DIRECTORY APPEND PROPERTY
1242 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1243 add_custom_command(TARGET ${dest} POST_BUILD
1244 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1245 else()
1246 add_custom_command(OUTPUT ${output_path}
Chris Bienemanbde7e452015-09-15 02:15:53 +00001247 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1248 DEPENDS ${dest})
Chris Bieneman895d96a2015-09-18 21:08:32 +00001249 add_custom_target(${name} ALL DEPENDS ${output_path})
1250 set_target_properties(${name} PROPERTIES FOLDER Tools)
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001251
Chris Bieneman895d96a2015-09-18 21:08:32 +00001252 # Make sure the parent tool is a toolchain tool, otherwise exclude this tool
1253 list(FIND LLVM_TOOLCHAIN_TOOLS ${dest} LLVM_IS_${dest}_TOOLCHAIN_TOOL)
1254 if (NOT LLVM_IS_${dest}_TOOLCHAIN_TOOL GREATER -1)
1255 set(LLVM_IS_${name}_TOOLCHAIN_TOOL ${LLVM_IS_${dest}_TOOLCHAIN_TOOL})
1256 else()
1257 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
1258 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001259
Chris Bieneman895d96a2015-09-18 21:08:32 +00001260 # LLVM_IS_${name}_TOOLCHAIN_TOOL will only be greater than -1 if both this
1261 # tool and its parent tool are in LLVM_TOOLCHAIN_TOOLS
1262 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1263 if( LLVM_BUILD_TOOLS )
1264 llvm_install_symlink(${name} ${dest})
1265 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001266 endif()
1267 endif()
1268endfunction()
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001269
1270function(llvm_externalize_debuginfo name)
1271 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1272 return()
1273 endif()
1274
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001275 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1276 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1277 endif()
1278
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001279 if(APPLE)
1280 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1281 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1282
1283 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bienemanccabd0e2015-12-03 22:51:08 +00001284 set_property(TARGET ${name} APPEND_STRING PROPERTY
1285 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001286 endif()
1287 add_custom_command(TARGET ${name} POST_BUILD
1288 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001289 ${strip_command}
1290 )
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001291 else()
1292 message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1293 endif()
1294endfunction()