blob: 06d26673e9630a4d15764fa1800a9d9cf9b2f563 [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}")
Nico Weberc27118d2013-12-28 23:31:44 +000072 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
73 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000074 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000075 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000076 add_custom_command(OUTPUT ${native_export_file}
77 COMMAND echo "{" > ${native_export_file}
78 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
79 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
80 COMMAND echo " local: *;" >> ${native_export_file}
81 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000082 DEPENDS ${export_file}
83 VERBATIM
84 COMMENT "Creating export file for ${target_name}")
Rafael Espindolaec1d0692015-06-22 12:34:54 +000085 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
86 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
87 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
88 else()
89 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
90 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
91 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000092 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000093 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000094
NAKAMURA Takumi81210752013-12-29 16:15:18 +000095 add_custom_command(OUTPUT ${native_export_file}
NAKAMURA Takumi3eb06662015-07-05 08:56:38 +000096 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
97 < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000098 DEPENDS ${export_file}
99 VERBATIM
100 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000101 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
102 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000103 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000104 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000105 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
106 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000107 endif()
108
109 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000110 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000111
112 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
113 foreach(src ${srcs})
114 get_filename_component(extension ${src} EXT)
115 if(extension STREQUAL ".cpp")
116 set(first_source_file ${src})
117 break()
118 endif()
119 endforeach()
120
121 # Force re-linking when the exports file changes. Actually, it
122 # forces recompilation of the source file. The LINK_DEPENDS target
123 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000124 # FIXME: This is not safe because this will create the same target
125 # ${native_export_file} in several different file:
126 # - One where we emitted ${target_name}_exports
127 # - One where we emitted the build command for the following object.
128 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
129 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000130
131 set_property(DIRECTORY APPEND
132 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
133
134 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000135
136 # Add dependency to *_exports later -- CMake issue 14747
137 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
138 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000139endfunction(add_llvm_symbol_exports)
140
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000141if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000142 execute_process(
143 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
144 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000145 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000146 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000147 if("${stdout}" MATCHES "GNU gold")
148 set(LLVM_LINKER_IS_GOLD ON)
149 endif()
150endif()
151
Rafael Espindola8cb78762014-11-02 12:14:22 +0000152function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000153 # Don't use linker optimizations in debug builds since it slows down the
154 # linker in a context where the optimizations are not important.
155 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000156
Rafael Espindola24766f92015-04-06 15:04:31 +0000157 # Pass -O3 to the linker. This enabled different optimizations on different
158 # linkers.
Rafael Espindola95fa3462015-06-22 12:41:52 +0000159 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000160 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000161 LINK_FLAGS " -Wl,-O3")
162 endif()
163
164 if(LLVM_LINKER_IS_GOLD)
165 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000166 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
167 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000168 # Note that there is a bug with -Wl,--icf=safe so it is not safe
169 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
170 endif()
171
172 if(NOT LLVM_NO_DEAD_STRIP)
173 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
174 # ld64's implementation of -dead_strip breaks tools that use plugins.
175 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
176 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindolac32ed572015-06-22 15:06:17 +0000177 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
178 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
179 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000180 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
181 # Object files are compiled with -ffunction-data-sections.
182 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
183 # tools that use plugins. Always pass --gc-sections once we require
184 # a newer linker.
185 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
186 LINK_FLAGS " -Wl,--gc-sections")
187 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000188 endif()
189 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000190endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000191
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000192# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
193# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
194# or a certain builder, for eaxample, msbuild.exe, would be confused.
John Brawnc11ef2a2015-09-30 15:20:51 +0000195function(set_output_directory target)
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +0000196 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000197
John Brawnc11ef2a2015-09-30 15:20:51 +0000198 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000199 # It affects output of add_library(MODULE).
200 if(WIN32 OR CYGWIN)
201 # DLL platform
John Brawnc11ef2a2015-09-30 15:20:51 +0000202 set(module_dir ${ARG_BINARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000203 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000204 set(module_dir ${ARG_LIBRARY_DIR})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000205 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000206 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
207 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
208 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
John Brawnc11ef2a2015-09-30 15:20:51 +0000209 if(ARG_BINARY_DIR)
210 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
211 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
212 endif()
213 if(ARG_LIBRARY_DIR)
214 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
215 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
216 endif()
217 if(module_dir)
218 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
219 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
220 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000221 endforeach()
222 else()
John Brawnc11ef2a2015-09-30 15:20:51 +0000223 if(ARG_BINARY_DIR)
224 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
225 endif()
226 if(ARG_LIBRARY_DIR)
227 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
228 endif()
229 if(module_dir)
230 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
231 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000232 endif()
233endfunction()
234
Greg Bedwell95213c32015-06-12 15:58:29 +0000235# If on Windows and building with MSVC, add the resource script containing the
236# VERSIONINFO data to the project. This embeds version resource information
237# into the output .exe or .dll.
238# TODO: Enable for MinGW Windows builds too.
239#
240function(add_windows_version_resource_file OUT_VAR)
241 set(sources ${ARGN})
242 if (MSVC)
243 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000244 if(EXISTS ${resource_file})
245 set(sources ${sources} ${resource_file})
246 source_group("Resource Files" ${resource_file})
247 set(windows_resource_file ${resource_file} PARENT_SCOPE)
248 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000249 endif(MSVC)
250
251 set(${OUT_VAR} ${sources} PARENT_SCOPE)
252endfunction(add_windows_version_resource_file)
253
254# set_windows_version_resource_properties(name resource_file...
255# VERSION_MAJOR int
256# Optional major version number (defaults to LLVM_VERSION_MAJOR)
257# VERSION_MINOR int
258# Optional minor version number (defaults to LLVM_VERSION_MINOR)
259# VERSION_PATCHLEVEL int
260# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
261# VERSION_STRING
262# Optional version string (defaults to PACKAGE_VERSION)
263# PRODUCT_NAME
264# Optional product name string (defaults to "LLVM")
265# )
266function(set_windows_version_resource_properties name resource_file)
267 cmake_parse_arguments(ARG
268 ""
269 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
270 ""
271 ${ARGN})
272
273 if (NOT DEFINED ARG_VERSION_MAJOR)
274 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
275 endif()
276
277 if (NOT DEFINED ARG_VERSION_MINOR)
278 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
279 endif()
280
281 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
282 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
283 endif()
284
285 if (NOT DEFINED ARG_VERSION_STRING)
286 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
287 endif()
288
289 if (NOT DEFINED ARG_PRODUCT_NAME)
290 set(ARG_PRODUCT_NAME "LLVM")
291 endif()
292
293 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000294 PROPERTY COMPILE_FLAGS /nologo)
295 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000296 PROPERTY COMPILE_DEFINITIONS
297 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
298 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
299 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
300 "RC_VERSION_FIELD_4=0"
301 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
302 "RC_INTERNAL_NAME=\"${name}\""
303 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
304 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
305endfunction(set_windows_version_resource_properties)
306
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000307# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000308# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000309# STATIC by default w/o BUILD_SHARED_LIBS.
310# SHARED by default w/ BUILD_SHARED_LIBS.
Axel Naumannb457beb2016-01-12 07:44:58 +0000311# OBJECT
312# Also create an OBJECT library target. Default if STATIC && SHARED.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000313# MODULE
314# Target ${name} might not be created on unsupported platforms.
315# Check with "if(TARGET ${name})".
Andrew Wilkins92113962015-09-01 03:14:31 +0000316# DISABLE_LLVM_LINK_LLVM_DYLIB
317# Do not link this library to libLLVM, even if
318# LLVM_LINK_LLVM_DYLIB is enabled.
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000319# OUTPUT_NAME name
320# Corresponds to OUTPUT_NAME in target properties.
321# DEPENDS targets...
322# Same semantics as add_dependencies().
323# LINK_COMPONENTS components...
324# Same as the variable LLVM_LINK_COMPONENTS.
325# LINK_LIBS lib_targets...
326# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000327# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000328# May specify header files for IDE generators.
Chris Bieneman5e96fe92015-11-04 23:11:12 +0000329# SONAME
330# Should set SONAME link flags and create symlinks
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000331# )
332function(llvm_add_library name)
333 cmake_parse_arguments(ARG
Axel Naumannb457beb2016-01-12 07:44:58 +0000334 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000335 "OUTPUT_NAME"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000336 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000337 ${ARGN})
338 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000339 if(ARG_ADDITIONAL_HEADERS)
340 # Pass through ADDITIONAL_HEADERS.
341 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
342 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000343 if(ARG_OBJLIBS)
344 set(ALL_FILES ${ARG_OBJLIBS})
345 else()
346 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
347 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000348
349 if(ARG_MODULE)
350 if(ARG_SHARED OR ARG_STATIC)
351 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
352 endif()
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000353 if(NOT LLVM_ENABLE_PLUGINS)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000354 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
355 return()
356 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000357 else()
358 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
359 set(ARG_SHARED TRUE)
360 endif()
361 if(NOT ARG_SHARED)
362 set(ARG_STATIC TRUE)
363 endif()
364 endif()
365
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000366 # Generate objlib
Axel Naumannb457beb2016-01-12 07:44:58 +0000367 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000368 # Generate an obj library for both targets.
369 set(obj_name "obj.${name}")
370 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
371 ${ALL_FILES}
372 )
373 llvm_update_compile_flags(${obj_name})
374 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
375
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000376 # Do add_dependencies(obj) later due to CMake issue 14747.
377 list(APPEND objlibs ${obj_name})
378
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000379 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
380 endif()
381
382 if(ARG_SHARED AND ARG_STATIC)
383 # static
384 set(name_static "${name}_static")
385 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000386 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000387 endif()
388 # DEPENDS has been appended to LLVM_COMMON_LIBS.
389 llvm_add_library(${name_static} STATIC
390 ${output_name}
391 OBJLIBS ${ALL_FILES} # objlib
392 LINK_LIBS ${ARG_LINK_LIBS}
393 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
394 )
395 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
396 set(ARG_STATIC)
397 endif()
398
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000399 if(ARG_MODULE)
400 add_library(${name} MODULE ${ALL_FILES})
401 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000402 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000403 add_library(${name} SHARED ${ALL_FILES})
404 else()
405 add_library(${name} STATIC ${ALL_FILES})
406 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000407
408 if(DEFINED windows_resource_file)
409 set_windows_version_resource_properties(${name} ${windows_resource_file})
410 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
411 endif()
412
John Brawnc11ef2a2015-09-30 15:20:51 +0000413 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000414 # $<TARGET_OBJECTS> doesn't require compile flags.
415 if(NOT obj_name)
416 llvm_update_compile_flags(${name})
417 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000418 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000419 if(ARG_OUTPUT_NAME)
420 set_target_properties(${name}
421 PROPERTIES
422 OUTPUT_NAME ${ARG_OUTPUT_NAME}
423 )
424 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000425
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000426 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000427 set_target_properties(${name} PROPERTIES
428 PREFIX ""
429 SUFFIX ${LLVM_PLUGIN_EXT}
430 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000431 endif()
432
433 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000434 if(WIN32)
435 set_target_properties(${name} PROPERTIES
436 PREFIX ""
437 )
438 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000439 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000440
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000441 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000442 # Do not add -Dname_EXPORTS to the command-line when building files in this
443 # target. Doing so is actively harmful for the modules build because it
444 # creates extra module variants, and not useful because we don't use these
445 # macros.
446 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
447
Nico Weber06473f82013-12-29 05:39:01 +0000448 if (LLVM_EXPORTED_SYMBOL_FILE)
449 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
450 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000451 endif()
452
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000453 if(ARG_SHARED AND UNIX)
454 if(NOT APPLE AND ARG_SONAME)
455 get_target_property(output_name ${name} OUTPUT_NAME)
456 if(${output_name} STREQUAL "output_name-NOTFOUND")
457 set(output_name ${name})
458 endif()
459 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
460 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
461 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
462 llvm_install_library_symlink(${api_name} ${library_name} SHARED
463 COMPONENT ${name}
464 ALWAYS_GENERATE)
465 llvm_install_library_symlink(${output_name} ${library_name} SHARED
466 COMPONENT ${name}
467 ALWAYS_GENERATE)
468 endif()
469 endif()
470
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000471 if (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
472 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
473 set(llvm_libs LLVM)
474 else()
475 llvm_map_components_to_libnames(llvm_libs
476 ${ARG_LINK_COMPONENTS}
477 ${LLVM_LINK_COMPONENTS}
478 )
479 endif()
Andrew Wilkins92113962015-09-01 03:14:31 +0000480 else()
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000481 # Components have not been defined explicitly in CMake, so add the
482 # dependency information for this library as defined by LLVMBuild.
483 #
484 # It would be nice to verify that we have the dependencies for this library
485 # name, but using get_property(... SET) doesn't suffice to determine if a
486 # property has been set to an empty value.
487 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
Andrew Wilkins92113962015-09-01 03:14:31 +0000488 endif()
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000489
490 if(CMAKE_VERSION VERSION_LESS 2.8.12)
491 # Link libs w/o keywords, assuming PUBLIC.
Steven Wuec6f56e2016-05-26 04:35:35 +0000492 target_link_libraries(${name}
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000493 ${ARG_LINK_LIBS}
494 ${lib_deps}
495 ${llvm_libs}
496 )
Steven Wuec6f56e2016-05-26 04:35:35 +0000497 elseif(ARG_STATIC)
498 target_link_libraries(${name} INTERFACE
499 ${ARG_LINK_LIBS}
500 ${lib_deps}
501 ${llvm_libs}
502 )
503 else()
504 # We can use PRIVATE since SO knows its dependent libs.
505 target_link_libraries(${name} PRIVATE
506 ${ARG_LINK_LIBS}
507 ${lib_deps}
508 ${llvm_libs}
509 )
510 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000511
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000512 if(LLVM_COMMON_DEPENDS)
513 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000514 # Add dependencies also to objlibs.
515 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
516 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000517 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
518 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000519 endif()
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000520
521 if(ARG_SHARED OR ARG_MODULE)
522 llvm_externalize_debuginfo(${name})
523 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000524endfunction()
525
526macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000527 cmake_parse_arguments(ARG
Andrew Wilkinsa4f37052015-11-10 23:19:21 +0000528 "SHARED"
Chris Bieneman84271692015-04-16 16:56:18 +0000529 ""
530 ""
531 ${ARGN})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000532 if( BUILD_SHARED_LIBS )
533 llvm_add_library(${name} SHARED ${ARGN})
534 else()
535 llvm_add_library(${name} ${ARGN})
536 endif()
Dan Liew80189d22015-06-29 18:45:56 +0000537 # The gtest libraries should not be installed or exported as a target
538 if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
539 set(_is_gtest TRUE)
540 else()
541 set(_is_gtest FALSE)
542 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
543 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000544
Nick Lewycky61aed872011-04-29 02:12:06 +0000545 if( EXCLUDE_FROM_ALL )
546 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Dan Liew80189d22015-06-29 18:45:56 +0000547 elseif(NOT _is_gtest)
Hans Wennborg16546272013-08-24 00:20:36 +0000548 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000549 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000550 if(ARG_SHARED OR BUILD_SHARED_LIBS)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000551 if(WIN32 OR CYGWIN OR MINGW)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000552 set(install_type RUNTIME)
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000553 set(install_dir bin)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000554 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000555 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000556 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000557 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000558 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000559 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000560
Chris Bienemana985e6b2015-04-16 18:08:33 +0000561 install(TARGETS ${name}
562 EXPORT LLVMExports
Sumanth Gundapaneni279c73f2015-12-16 17:07:15 +0000563 ${install_type} DESTINATION ${install_dir}
Chris Bienemana985e6b2015-04-16 18:08:33 +0000564 COMPONENT ${name})
565
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000566 if (NOT CMAKE_CONFIGURATION_TYPES)
567 add_custom_target(install-${name}
568 DEPENDS ${name}
569 COMMAND "${CMAKE_COMMAND}"
570 -DCMAKE_INSTALL_COMPONENT=${name}
571 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
572 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000573 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000574 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000575 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000576 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000577endmacro(add_llvm_library name)
578
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000579macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000580 llvm_add_library(${name} MODULE ${ARGN})
581 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000582 # Add empty "phony" target
583 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000584 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000585 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000586 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000587 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000588 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000589 if(WIN32 OR CYGWIN)
590 # DLL platform
591 set(dlldir "bin")
592 else()
593 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
594 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000595 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000596 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000597 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000598 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
599 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000600 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000601 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000602 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000603
604 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000605endmacro(add_llvm_loadable_module name)
606
607
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000608macro(add_llvm_executable name)
Chris Bieneman48eaa542015-12-08 21:51:48 +0000609 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO" "" "" ${ARGN})
Andrew Wilkins92113962015-09-01 03:14:31 +0000610 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000611
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +0000612 # Generate objlib
613 if(LLVM_ENABLE_OBJLIB)
614 # Generate an obj library for both targets.
615 set(obj_name "obj.${name}")
616 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
617 ${ALL_FILES}
618 )
619 llvm_update_compile_flags(${obj_name})
620 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
621
622 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
623 endif()
624
NAKAMURA Takumib5288ba2015-08-28 00:36:58 +0000625 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
626
Jordan Rose3d752972015-09-10 17:18:51 +0000627 if(XCODE)
628 # Note: the dummy.cpp source file provides no definitions. However,
629 # it forces Xcode to properly link the static library.
Jordan Rose85a22f82015-09-10 17:55:02 +0000630 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
Jordan Rose3d752972015-09-10 17:18:51 +0000631 endif()
632
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000633 if( EXCLUDE_FROM_ALL )
634 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
635 else()
636 add_executable(${name} ${ALL_FILES})
637 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000638
639 if(DEFINED windows_resource_file)
640 set_windows_version_resource_properties(${name} ${windows_resource_file})
641 endif()
642
NAKAMURA Takumibb7483d2015-09-08 07:42:06 +0000643 # $<TARGET_OBJECTS> doesn't require compile flags.
644 if(NOT LLVM_ENABLE_OBJLIB)
645 llvm_update_compile_flags(${name})
646 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000647 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000648
Richard Smith2b91a7f2014-09-26 22:40:15 +0000649 # Do not add -Dname_EXPORTS to the command-line when building files in this
650 # target. Doing so is actively harmful for the modules build because it
651 # creates extra module variants, and not useful because we don't use these
652 # macros.
653 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
654
Nico Weberc27118d2013-12-28 23:31:44 +0000655 if (LLVM_EXPORTED_SYMBOL_FILE)
656 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
657 endif(LLVM_EXPORTED_SYMBOL_FILE)
658
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000659 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
660 set(USE_SHARED USE_SHARED)
661 endif()
662
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000663 set(EXCLUDE_FROM_ALL OFF)
John Brawnc11ef2a2015-09-30 15:20:51 +0000664 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000665 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000666 if( LLVM_COMMON_DEPENDS )
667 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
668 endif( LLVM_COMMON_DEPENDS )
Chris Bienemanbf2c4122015-12-03 18:45:39 +0000669
Chris Bieneman48eaa542015-12-08 21:51:48 +0000670 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
671 llvm_externalize_debuginfo(${name})
672 endif()
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000673endmacro(add_llvm_executable name)
674
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000675function(export_executable_symbols target)
676 if (NOT MSVC) # MSVC's linker doesn't support exporting all symbols.
677 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
Chris Bieneman914742b2015-11-24 00:58:58 +0000678 if (APPLE)
Chris Bienemanccabd0e2015-12-03 22:51:08 +0000679 set_property(TARGET ${target} APPEND_STRING PROPERTY
Chris Bienemanac6677a2015-12-03 22:55:36 +0000680 LINK_FLAGS " -rdynamic")
Chris Bieneman914742b2015-11-24 00:58:58 +0000681 endif()
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000682 endif()
683endfunction()
684
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000685if(NOT LLVM_TOOLCHAIN_TOOLS)
686 set (LLVM_TOOLCHAIN_TOOLS
687 llvm-ar
Chris Bieneman6d43afc2015-09-14 23:09:06 +0000688 llvm-ranlib
689 llvm-lib
Chris Bieneman170fd9cb2015-09-10 17:23:32 +0000690 llvm-objdump
691 )
692endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000693
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000694macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000695 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000696 set(EXCLUDE_FROM_ALL ON)
697 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000698 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000699
700 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
701 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
702 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000703 install(TARGETS ${name}
704 EXPORT LLVMExports
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000705 RUNTIME DESTINATION bin
706 COMPONENT ${name})
707
708 if (NOT CMAKE_CONFIGURATION_TYPES)
709 add_custom_target(install-${name}
710 DEPENDS ${name}
711 COMMAND "${CMAKE_COMMAND}"
712 -DCMAKE_INSTALL_COMPONENT=${name}
713 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
714 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000715 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000716 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000717 if( LLVM_BUILD_TOOLS )
718 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
719 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000720 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000721endmacro(add_llvm_tool name)
722
723
724macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000725 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000726 set(EXCLUDE_FROM_ALL ON)
727 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000728 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000729 if( LLVM_BUILD_EXAMPLES )
730 install(TARGETS ${name} RUNTIME DESTINATION examples)
731 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000732 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000733endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000734
735
Oscar Fuentes3145e922011-02-20 22:06:10 +0000736macro(add_llvm_utility name)
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +0000737 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
Oscar Fuentes3145e922011-02-20 22:06:10 +0000738 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Derek Schuffef9928e2015-03-27 16:53:06 +0000739 if( LLVM_INSTALL_UTILS )
740 install (TARGETS ${name}
741 RUNTIME DESTINATION bin
742 COMPONENT ${name})
743 if (NOT CMAKE_CONFIGURATION_TYPES)
744 add_custom_target(install-${name}
745 DEPENDS ${name}
746 COMMAND "${CMAKE_COMMAND}"
747 -DCMAKE_INSTALL_COMPONENT=${name}
748 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
749 endif()
750 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000751endmacro(add_llvm_utility name)
752
753
Oscar Fuentescdc95492008-09-26 04:40:32 +0000754macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000755 include_directories(BEFORE
756 ${CMAKE_CURRENT_BINARY_DIR}
757 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000758 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000759 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000760endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000761
Chris Bieneman580d8152015-07-20 20:36:06 +0000762function(canonicalize_tool_name name output)
763 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
764 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
765 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
766 set(${output} "${nameUPPER}" PARENT_SCOPE)
767endfunction(canonicalize_tool_name)
768
Chris Bieneman70997c32015-10-20 16:42:58 +0000769# Custom add_subdirectory wrapper
Tobias Grossere3421b52016-04-29 13:03:40 +0000770# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
771# path if it differs from the name.
Chris Bieneman70997c32015-10-20 16:42:58 +0000772macro(add_llvm_subdirectory project type name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000773 set(add_llvm_external_dir "${ARGN}")
774 if("${add_llvm_external_dir}" STREQUAL "")
775 set(add_llvm_external_dir ${name})
776 endif()
Chris Bieneman580d8152015-07-20 20:36:06 +0000777 canonicalize_tool_name(${name} nameUPPER)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000778 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
779 # Treat it as in-tree subproject.
Chris Bieneman70997c32015-10-20 16:42:58 +0000780 option(${project}_${type}_${nameUPPER}_BUILD
781 "Whether to build ${name} as part of ${project}" On)
782 mark_as_advanced(${project}_${type}_${name}_BUILD)
783 if(${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000784 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
785 # Don't process it in add_llvm_implicit_projects().
Chris Bieneman70997c32015-10-20 16:42:58 +0000786 set(${project}_${type}_${nameUPPER}_BUILD OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000787 endif()
788 else()
789 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
790 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
791 CACHE PATH "Path to ${name} source directory")
Chris Bieneman70997c32015-10-20 16:42:58 +0000792 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000793 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
Chris Bieneman70997c32015-10-20 16:42:58 +0000794 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000795 endif()
796 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
Chris Bieneman70997c32015-10-20 16:42:58 +0000797 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000798 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000799 option(${project}_${type}_${nameUPPER}_BUILD
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000800 "Whether to build ${name} as part of LLVM"
Chris Bieneman70997c32015-10-20 16:42:58 +0000801 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
802 if (${project}_${type}_${nameUPPER}_BUILD)
NAKAMURA Takumi6fa85322015-08-22 05:11:02 +0000803 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
804 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
805 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
806 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
807 endif()
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000808 # FIXME: It'd be redundant.
Chris Bieneman70997c32015-10-20 16:42:58 +0000809 set(${project}_${type}_${nameUPPER}_BUILD Off)
Michael J. Spencere734f542012-04-26 19:43:35 +0000810 endif()
811 endif()
Chris Bieneman70997c32015-10-20 16:42:58 +0000812endmacro()
813
814# Add external project that may want to be built as part of llvm such as Clang,
815# lld, and Polly. This adds two options. One for the source directory of the
816# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
817# enable or disable building it with everything else.
818# Additional parameter can be specified as the name of directory.
819macro(add_llvm_external_project name)
Manuel Klimekf80abb92015-10-22 08:31:46 +0000820 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
Chris Bieneman70997c32015-10-20 16:42:58 +0000821endmacro()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000822
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000823macro(add_llvm_tool_subdirectory name)
Chris Bieneman580d8152015-07-20 20:36:06 +0000824 add_llvm_external_project(${name})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000825endmacro(add_llvm_tool_subdirectory)
826
Chris Bieneman580d8152015-07-20 20:36:06 +0000827function(get_project_name_from_src_var var output)
828 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
829 MACHED_TOOL "${var}")
830 if(MACHED_TOOL)
831 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
832 else()
833 set(${output} PARENT_SCOPE)
834 endif()
835endfunction()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000836
Chris Bieneman70997c32015-10-20 16:42:58 +0000837function(create_subdirectory_options project type)
Chris Bieneman580d8152015-07-20 20:36:06 +0000838 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
839 foreach(dir ${sub-dirs})
840 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
841 canonicalize_tool_name(${dir} name)
Chris Bieneman70997c32015-10-20 16:42:58 +0000842 option(${project}_${type}_${name}_BUILD
843 "Whether to build ${name} as part of ${project}" On)
844 mark_as_advanced(${project}_${type}_${name}_BUILD)
Chris Bieneman580d8152015-07-20 20:36:06 +0000845 endif()
846 endforeach()
Chris Bieneman70997c32015-10-20 16:42:58 +0000847endfunction(create_subdirectory_options)
848
849function(create_llvm_tool_options)
850 create_subdirectory_options(LLVM TOOL)
Chris Bieneman580d8152015-07-20 20:36:06 +0000851endfunction(create_llvm_tool_options)
852
Chris Bieneman74c98f02016-03-08 18:43:28 +0000853function(llvm_add_implicit_projects project)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000854 set(list_of_implicit_subdirs "")
855 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
856 foreach(dir ${sub-dirs})
Chris Bieneman580d8152015-07-20 20:36:06 +0000857 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
858 canonicalize_tool_name(${dir} name)
Chris Bieneman74c98f02016-03-08 18:43:28 +0000859 if (${project}_TOOL_${name}_BUILD)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000860 get_filename_component(fn "${dir}" NAME)
861 list(APPEND list_of_implicit_subdirs "${fn}")
862 endif()
863 endif()
864 endforeach()
865
866 foreach(external_proj ${list_of_implicit_subdirs})
Chris Bieneman74c98f02016-03-08 18:43:28 +0000867 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000868 endforeach()
Chris Bieneman74c98f02016-03-08 18:43:28 +0000869endfunction(llvm_add_implicit_projects)
870
871function(add_llvm_implicit_projects)
872 llvm_add_implicit_projects(LLVM)
Chris Bieneman580d8152015-07-20 20:36:06 +0000873endfunction(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000874
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000875# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000876function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000877 if( NOT LLVM_BUILD_TESTS )
878 set(EXCLUDE_FROM_ALL ON)
879 endif()
880
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000881 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
882 if (NOT LLVM_ENABLE_THREADS)
883 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
884 endif ()
885
886 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000887 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000888 endif ()
889
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000890 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000891
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000892 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
Chris Bieneman48eaa542015-12-08 21:51:48 +0000893 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000894 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
John Brawnc11ef2a2015-09-30 15:20:51 +0000895 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
Andrew Wilkins095e22d2016-02-12 01:42:43 +0000896 target_link_libraries(${test_name} gtest_main gtest)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000897
898 add_dependencies(${test_suite} ${test_name})
899 get_target_property(test_suite_folder ${test_suite} FOLDER)
900 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
901 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
902 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000903endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000904
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000905function(llvm_add_go_executable binary pkgpath)
906 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
907
908 if(LLVM_BINDINGS MATCHES "go")
909 # FIXME: This should depend only on the libraries Go needs.
910 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
911 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
912 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
913 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +0000914 set(cppflags "")
915 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
916 foreach(d ${include_dirs})
917 set(cppflags "${cppflags} -I${d}")
918 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000919 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
920 add_custom_command(OUTPUT ${binpath}
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000921 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 +0000922 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
923 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
924 ${llvmlibs} ${ARG_DEPENDS}
925 COMMENT "Building Go executable ${binary}"
926 VERBATIM)
927 if (ARG_ALL)
928 add_custom_target(${binary} ALL DEPENDS ${binpath})
929 else()
930 add_custom_target(${binary} DEPENDS ${binpath})
931 endif()
932 endif()
933endfunction()
934
Chandler Carruth3511dd32012-06-28 06:36:24 +0000935# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000936# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000937# variables needed for the 'lit.site.cfg' files. This function bundles the
938# common variables that any Lit instance is likely to need, and custom
939# variables can be passed in.
940function(configure_lit_site_cfg input output)
941 foreach(c ${LLVM_TARGETS_TO_BUILD})
942 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
943 endforeach(c)
944 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
945
946 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000947
Chandler Carruth3511dd32012-06-28 06:36:24 +0000948 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000949 if (CMAKE_CFG_INTDIR STREQUAL ".")
950 set(LLVM_BUILD_MODE ".")
951 else ()
952 set(LLVM_BUILD_MODE "%(build_mode)s")
953 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000954
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000955 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000956 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
957 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000958 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
959 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000960
961 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +0000962 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000963
Chandler Carruth3511dd32012-06-28 06:36:24 +0000964 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000965 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
966 # plugins. We may rename it.
967 if(LLVM_ENABLE_PLUGINS)
968 set(ENABLE_SHARED "1")
969 else()
970 set(ENABLE_SHARED "0")
971 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000972
973 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
974 set(ENABLE_ASSERTIONS "1")
975 else()
976 set(ENABLE_ASSERTIONS "0")
977 endif()
978
Kuba Breckaa62bbcc2015-01-24 01:42:44 +0000979 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +0000980 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000981
Peter Collingbournece800842014-10-17 18:32:36 +0000982 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
983 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +0000984 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +0000985
Alex Denisovd5cee4a2016-04-16 06:47:41 +0000986 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${input}\n## Do not edit!")
987
Chandler Carruth3511dd32012-06-28 06:36:24 +0000988 configure_file(${input} ${output} @ONLY)
989endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000990
991# A raw function to create a lit target. This is used to implement the testuite
992# management functions.
993function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000994 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000995 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
996 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000997 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
998 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
999 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +00001000 if (LLVM_MAIN_SRC_DIR)
1001 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1002 else()
1003 find_program(LIT_COMMAND llvm-lit)
1004 endif ()
1005 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001006 foreach(param ${ARG_PARAMS})
1007 list(APPEND LIT_COMMAND --param ${param})
1008 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001009 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001010 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001011 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001012 COMMENT "${comment}"
Peter Collingbournea7fb5cf2014-11-17 22:16:15 +00001013 ${cmake_3_2_USES_TERMINAL}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001014 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001015 else()
1016 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +00001017 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +00001018 message(STATUS "${target} does nothing.")
1019 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +00001020 if (ARG_DEPENDS)
1021 add_dependencies(${target} ${ARG_DEPENDS})
1022 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +00001023
1024 # Tests should be excluded from "Build Solution".
1025 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +00001026endfunction()
1027
1028# A function to add a set of lit test suites to be driven through 'check-*' targets.
1029function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001030 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +00001031
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001032 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1033 if(NOT EXCLUDE_FROM_ALL)
1034 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001035 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +00001036 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1037 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1038 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1039 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +00001040
1041 # Produce a specific suffixed check rule.
1042 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001043 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +00001044 PARAMS ${ARG_PARAMS}
1045 DEPENDS ${ARG_DEPENDS}
1046 ARGS ${ARG_ARGS}
1047 )
1048endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001049
1050function(add_lit_testsuites project directory)
1051 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +00001052 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Justin Bognere8894622016-05-06 21:57:30 +00001053
1054 # Search recursively for test directories by assuming anything not
1055 # in a directory called Inputs contains tests.
Chris Bienemanabc8fb22015-03-27 21:45:49 +00001056 set(lit_suites)
Justin Bognere8894622016-05-06 21:57:30 +00001057 file(GLOB to_process ${directory}/*)
1058 while(to_process)
1059 set(cur_to_process ${to_process})
1060 set(to_process)
1061 foreach(lit_suite ${cur_to_process})
Justin Bogner658c0a22016-05-06 22:22:25 +00001062 if(IS_DIRECTORY ${lit_suite})
1063 string(FIND ${lit_suite} Inputs is_inputs)
1064 if (is_inputs EQUAL -1)
1065 list(APPEND lit_suites "${lit_suite}")
1066 file(GLOB subdirs ${lit_suite}/*)
1067 list(APPEND to_process ${subdirs})
1068 endif()
Justin Bognere8894622016-05-06 21:57:30 +00001069 endif()
1070 endforeach()
1071 endwhile()
1072
1073 # Now create a check- target for each test directory.
Chris Bienemanabc8fb22015-03-27 21:45:49 +00001074 foreach(dir ${lit_suites})
Chris Bieneman9ea37d92015-03-23 20:04:00 +00001075 string(REPLACE ${directory} "" name_slash ${dir})
1076 if (name_slash)
1077 string(REPLACE "/" "-" name_slash ${name_slash})
1078 string(REPLACE "\\" "-" name_dashes ${name_slash})
1079 string(TOLOWER "${project}${name_dashes}" name_var)
1080 add_lit_target("check-${name_var}" "Running lit suite ${dir}"
1081 ${dir}
1082 PARAMS ${ARG_PARAMS}
1083 DEPENDS ${ARG_DEPENDS}
1084 ARGS ${ARG_ARGS}
1085 )
1086 endif()
1087 endforeach()
1088 endif()
1089endfunction()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001090
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001091function(llvm_install_library_symlink name dest type)
1092 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1093 foreach(path ${CMAKE_MODULE_PATH})
1094 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1095 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1096 break()
1097 endif()
1098 endforeach()
1099
1100 set(component ${ARG_COMPONENT})
1101 if(NOT component)
1102 set(component ${name})
1103 endif()
1104
1105 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1106 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1107
Chris Bieneman021f8742015-11-11 16:19:39 +00001108 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
Chris Bieneman5e96fe92015-11-04 23:11:12 +00001109 if(WIN32 AND "${type}" STREQUAL "SHARED")
1110 set(output_dir bin)
1111 endif()
1112
1113 install(SCRIPT ${INSTALL_SYMLINK}
1114 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1115 COMPONENT ${component})
1116
1117 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1118 add_custom_target(install-${name}
1119 DEPENDS ${name} ${dest} install-${dest}
1120 COMMAND "${CMAKE_COMMAND}"
1121 -DCMAKE_INSTALL_COMPONENT=${name}
1122 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1123 endif()
1124endfunction()
1125
Chris Bienemanbff03b02015-09-16 20:49:59 +00001126function(llvm_install_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001127 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman08249702015-09-18 17:39:58 +00001128 foreach(path ${CMAKE_MODULE_PATH})
1129 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1130 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1131 break()
1132 endif()
1133 endforeach()
Ramkumar Ramachandracad135a2015-11-10 18:26:34 +00001134
Chris Bieneman895d96a2015-09-18 21:08:32 +00001135 if(ARG_ALWAYS_GENERATE)
1136 set(component ${dest})
1137 else()
1138 set(component ${name})
1139 endif()
1140
Chris Bieneman11a71602015-10-16 23:17:13 +00001141 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1142 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1143
Chris Bieneman08249702015-09-18 17:39:58 +00001144 install(SCRIPT ${INSTALL_SYMLINK}
Chris Bieneman11a71602015-10-16 23:17:13 +00001145 CODE "install_symlink(${full_name} ${full_dest} bin)"
Chris Bieneman895d96a2015-09-18 21:08:32 +00001146 COMPONENT ${component})
Chris Bienemanbff03b02015-09-16 20:49:59 +00001147
Chris Bieneman895d96a2015-09-18 21:08:32 +00001148 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
Chris Bienemanbff03b02015-09-16 20:49:59 +00001149 add_custom_target(install-${name}
1150 DEPENDS ${name} ${dest} install-${dest}
1151 COMMAND "${CMAKE_COMMAND}"
1152 -DCMAKE_INSTALL_COMPONENT=${name}
1153 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1154 endif()
1155endfunction()
1156
Chris Bienemanbde7e452015-09-15 02:15:53 +00001157function(add_llvm_tool_symlink name dest)
Chris Bieneman895d96a2015-09-18 21:08:32 +00001158 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001159 if(UNIX)
1160 set(LLVM_LINK_OR_COPY create_symlink)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001161 set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001162 else()
1163 set(LLVM_LINK_OR_COPY copy)
Chris Bienemanbde7e452015-09-15 02:15:53 +00001164 set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001165 endif()
1166
1167 set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1168
Chris Bieneman895d96a2015-09-18 21:08:32 +00001169 if(ARG_ALWAYS_GENERATE)
1170 set_property(DIRECTORY APPEND PROPERTY
1171 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1172 add_custom_command(TARGET ${dest} POST_BUILD
1173 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1174 else()
1175 add_custom_command(OUTPUT ${output_path}
Chris Bienemanbde7e452015-09-15 02:15:53 +00001176 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1177 DEPENDS ${dest})
Chris Bieneman895d96a2015-09-18 21:08:32 +00001178 add_custom_target(${name} ALL DEPENDS ${output_path})
1179 set_target_properties(${name} PROPERTIES FOLDER Tools)
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001180
Chris Bieneman895d96a2015-09-18 21:08:32 +00001181 # Make sure the parent tool is a toolchain tool, otherwise exclude this tool
1182 list(FIND LLVM_TOOLCHAIN_TOOLS ${dest} LLVM_IS_${dest}_TOOLCHAIN_TOOL)
1183 if (NOT LLVM_IS_${dest}_TOOLCHAIN_TOOL GREATER -1)
1184 set(LLVM_IS_${name}_TOOLCHAIN_TOOL ${LLVM_IS_${dest}_TOOLCHAIN_TOOL})
1185 else()
1186 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
1187 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001188
Chris Bieneman895d96a2015-09-18 21:08:32 +00001189 # LLVM_IS_${name}_TOOLCHAIN_TOOL will only be greater than -1 if both this
1190 # tool and its parent tool are in LLVM_TOOLCHAIN_TOOLS
1191 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1192 if( LLVM_BUILD_TOOLS )
1193 llvm_install_symlink(${name} ${dest})
1194 endif()
Chris Bieneman6d43afc2015-09-14 23:09:06 +00001195 endif()
1196 endif()
1197endfunction()
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001198
1199function(llvm_externalize_debuginfo name)
1200 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1201 return()
1202 endif()
1203
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001204 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1205 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1206 endif()
1207
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001208 if(APPLE)
1209 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1210 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1211
1212 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bienemanccabd0e2015-12-03 22:51:08 +00001213 set_property(TARGET ${name} APPEND_STRING PROPERTY
1214 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001215 endif()
1216 add_custom_command(TARGET ${name} POST_BUILD
1217 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman6099a4e2016-03-31 20:03:19 +00001218 ${strip_command}
1219 )
Chris Bienemanbf2c4122015-12-03 18:45:39 +00001220 else()
1221 message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1222 endif()
1223endfunction()