blob: 2f470105ff5d161e14e746b7fbf294c4208521dd [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001include(LLVMProcessSources)
2include(LLVM-Config)
3include(DetermineGCCCompatible)
4
5function(llvm_update_compile_flags name)
6 get_property(sources TARGET ${name} PROPERTY SOURCES)
7 if("${sources}" MATCHES "\\.c(;|$)")
8 set(update_src_props ON)
9 endif()
10
11 # 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)
14 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()
18 if(MSVC)
19 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
20 endif()
21 else()
22 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
23 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
24 elseif(MSVC)
25 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
26 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
27 endif()
28 endif()
29
30 # LLVM_REQUIRES_RTTI is an internal flag that individual
31 # targets can use to force RTTI
32 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
33 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
34 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
35 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
36 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
37 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
38 elseif (MSVC)
39 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
40 endif ()
41 elseif(MSVC)
42 list(APPEND LLVM_COMPILE_FLAGS "/GR")
43 endif()
44
45 # Assume that;
46 # - LLVM_COMPILE_FLAGS is list.
47 # - PROPERTY COMPILE_FLAGS is string.
48 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
49
50 if(update_src_props)
51 foreach(fn ${sources})
52 get_filename_component(suf ${fn} EXT)
53 if("${suf}" STREQUAL ".cpp")
54 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
55 COMPILE_FLAGS "${target_compile_flags}")
56 endif()
57 endforeach()
58 else()
59 # Update target props, since all sources are C++.
60 set_property(TARGET ${name} APPEND_STRING PROPERTY
61 COMPILE_FLAGS "${target_compile_flags}")
62 endif()
63
64 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
65endfunction()
66
67function(add_llvm_symbol_exports target_name export_file)
68 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
69 set(native_export_file "${target_name}.exports")
70 add_custom_command(OUTPUT ${native_export_file}
71 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
72 DEPENDS ${export_file}
73 VERBATIM
74 COMMENT "Creating export file for ${target_name}")
75 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
76 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
77 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
78 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
79 LINK_FLAGS " -Wl,-bE:${export_file}")
80 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
81 # Gold and BFD ld require a version script rather than a plain list.
82 set(native_export_file "${target_name}.exports")
83 # FIXME: Don't write the "local:" line on OpenBSD.
84 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
85 add_custom_command(OUTPUT ${native_export_file}
86 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
87 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
88 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
89 COMMAND echo " local: *;" >> ${native_export_file}
90 COMMAND echo "};" >> ${native_export_file}
91 DEPENDS ${export_file}
92 VERBATIM
93 COMMENT "Creating export file for ${target_name}")
94 if (${LLVM_LINKER_IS_SOLARISLD})
95 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
96 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
97 else()
98 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
99 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
100 endif()
101 else()
102 set(native_export_file "${target_name}.def")
103
104 add_custom_command(OUTPUT ${native_export_file}
105 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
106 < ${export_file} > ${native_export_file}
107 DEPENDS ${export_file}
108 VERBATIM
109 COMMENT "Creating export file for ${target_name}")
110 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
111 if(MSVC)
112 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
113 endif()
114 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
115 LINK_FLAGS " ${export_file_linker_flag}")
116 endif()
117
118 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
119 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
120
121 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
122 foreach(src ${srcs})
123 get_filename_component(extension ${src} EXT)
124 if(extension STREQUAL ".cpp")
125 set(first_source_file ${src})
126 break()
127 endif()
128 endforeach()
129
130 # Force re-linking when the exports file changes. Actually, it
131 # forces recompilation of the source file. The LINK_DEPENDS target
132 # property only works for makefile-based generators.
133 # FIXME: This is not safe because this will create the same target
134 # ${native_export_file} in several different file:
135 # - One where we emitted ${target_name}_exports
136 # - One where we emitted the build command for the following object.
137 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
138 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
139
140 set_property(DIRECTORY APPEND
141 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
142
143 add_dependencies(${target_name} ${target_name}_exports)
144
145 # Add dependency to *_exports later -- CMake issue 14747
146 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
147 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
148endfunction(add_llvm_symbol_exports)
149
150if(NOT WIN32 AND NOT APPLE)
151 # Detect what linker we have here
152 if( LLVM_USE_LINKER )
153 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
154 else()
155 set(command ${CMAKE_C_COMPILER} -Wl,--version)
156 endif()
157 execute_process(
158 COMMAND ${command}
159 OUTPUT_VARIABLE stdout
160 ERROR_VARIABLE stderr
161 )
162 set(LLVM_LINKER_DETECTED ON)
163 if("${stdout}" MATCHES "GNU gold")
164 set(LLVM_LINKER_IS_GOLD ON)
165 message(STATUS "Linker detection: GNU Gold")
166 elseif("${stdout}" MATCHES "^LLD")
167 set(LLVM_LINKER_IS_LLD ON)
168 message(STATUS "Linker detection: LLD")
169 elseif("${stdout}" MATCHES "GNU ld")
170 set(LLVM_LINKER_IS_GNULD ON)
171 message(STATUS "Linker detection: GNU ld")
172 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
173 "${stdout}" MATCHES "Solaris Link Editors")
174 set(LLVM_LINKER_IS_SOLARISLD ON)
175 message(STATUS "Linker detection: Solaris ld")
176 else()
177 set(LLVM_LINKER_DETECTED OFF)
178 message(STATUS "Linker detection: unknown")
179 endif()
180endif()
181
182function(add_link_opts target_name)
183 # Don't use linker optimizations in debug builds since it slows down the
184 # linker in a context where the optimizations are not important.
185 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
186
187 # Pass -O3 to the linker. This enabled different optimizations on different
188 # linkers.
189 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
190 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
191 LINK_FLAGS " -Wl,-O3")
192 endif()
193
194 if(LLVM_LINKER_IS_GOLD)
195 # With gold gc-sections is always safe.
196 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
197 LINK_FLAGS " -Wl,--gc-sections")
198 # Note that there is a bug with -Wl,--icf=safe so it is not safe
199 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
200 endif()
201
202 if(NOT LLVM_NO_DEAD_STRIP)
203 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
204 # ld64's implementation of -dead_strip breaks tools that use plugins.
205 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
206 LINK_FLAGS " -Wl,-dead_strip")
207 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
208 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
209 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
210 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
211 # Object files are compiled with -ffunction-data-sections.
212 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
213 # tools that use plugins. Always pass --gc-sections once we require
214 # a newer linker.
215 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
216 LINK_FLAGS " -Wl,--gc-sections")
217 endif()
218 endif()
219 endif()
220endfunction(add_link_opts)
221
222# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
223# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
224# or a certain builder, for eaxample, msbuild.exe, would be confused.
225function(set_output_directory target)
226 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
227
228 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
229 # It affects output of add_library(MODULE).
230 if(WIN32 OR CYGWIN)
231 # DLL platform
232 set(module_dir ${ARG_BINARY_DIR})
233 else()
234 set(module_dir ${ARG_LIBRARY_DIR})
235 endif()
236 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
237 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
238 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
239 if(ARG_BINARY_DIR)
240 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
241 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
242 endif()
243 if(ARG_LIBRARY_DIR)
244 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
245 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
246 endif()
247 if(module_dir)
248 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
249 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
250 endif()
251 endforeach()
252 else()
253 if(ARG_BINARY_DIR)
254 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
255 endif()
256 if(ARG_LIBRARY_DIR)
257 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
258 endif()
259 if(module_dir)
260 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
261 endif()
262 endif()
263endfunction()
264
265# If on Windows and building with MSVC, add the resource script containing the
266# VERSIONINFO data to the project. This embeds version resource information
267# into the output .exe or .dll.
268# TODO: Enable for MinGW Windows builds too.
269#
270function(add_windows_version_resource_file OUT_VAR)
271 set(sources ${ARGN})
272 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
273 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
274 if(EXISTS ${resource_file})
275 set(sources ${sources} ${resource_file})
276 source_group("Resource Files" ${resource_file})
277 set(windows_resource_file ${resource_file} PARENT_SCOPE)
278 endif()
279 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
280
281 set(${OUT_VAR} ${sources} PARENT_SCOPE)
282endfunction(add_windows_version_resource_file)
283
284# set_windows_version_resource_properties(name resource_file...
285# VERSION_MAJOR int
286# Optional major version number (defaults to LLVM_VERSION_MAJOR)
287# VERSION_MINOR int
288# Optional minor version number (defaults to LLVM_VERSION_MINOR)
289# VERSION_PATCHLEVEL int
290# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
291# VERSION_STRING
292# Optional version string (defaults to PACKAGE_VERSION)
293# PRODUCT_NAME
294# Optional product name string (defaults to "LLVM")
295# )
296function(set_windows_version_resource_properties name resource_file)
297 cmake_parse_arguments(ARG
298 ""
299 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
300 ""
301 ${ARGN})
302
303 if (NOT DEFINED ARG_VERSION_MAJOR)
304 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
305 endif()
306
307 if (NOT DEFINED ARG_VERSION_MINOR)
308 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
309 endif()
310
311 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
312 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
313 endif()
314
315 if (NOT DEFINED ARG_VERSION_STRING)
316 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
317 endif()
318
319 if (NOT DEFINED ARG_PRODUCT_NAME)
320 set(ARG_PRODUCT_NAME "LLVM")
321 endif()
322
323 set_property(SOURCE ${resource_file}
324 PROPERTY COMPILE_FLAGS /nologo)
325 set_property(SOURCE ${resource_file}
326 PROPERTY COMPILE_DEFINITIONS
327 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
328 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
329 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
330 "RC_VERSION_FIELD_4=0"
331 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
332 "RC_INTERNAL_NAME=\"${name}\""
333 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
334 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
335endfunction(set_windows_version_resource_properties)
336
337# llvm_add_library(name sources...
338# SHARED;STATIC
339# STATIC by default w/o BUILD_SHARED_LIBS.
340# SHARED by default w/ BUILD_SHARED_LIBS.
341# OBJECT
342# Also create an OBJECT library target. Default if STATIC && SHARED.
343# MODULE
344# Target ${name} might not be created on unsupported platforms.
345# Check with "if(TARGET ${name})".
346# DISABLE_LLVM_LINK_LLVM_DYLIB
347# Do not link this library to libLLVM, even if
348# LLVM_LINK_LLVM_DYLIB is enabled.
349# OUTPUT_NAME name
350# Corresponds to OUTPUT_NAME in target properties.
351# DEPENDS targets...
352# Same semantics as add_dependencies().
353# LINK_COMPONENTS components...
354# Same as the variable LLVM_LINK_COMPONENTS.
355# LINK_LIBS lib_targets...
356# Same semantics as target_link_libraries().
357# ADDITIONAL_HEADERS
358# May specify header files for IDE generators.
359# SONAME
360# Should set SONAME link flags and create symlinks
361# PLUGIN_TOOL
362# The tool (i.e. cmake target) that this plugin will link against
363# )
364function(llvm_add_library name)
365 cmake_parse_arguments(ARG
366 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
367 "OUTPUT_NAME;PLUGIN_TOOL"
368 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
369 ${ARGN})
370 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
371 if(ARG_ADDITIONAL_HEADERS)
372 # Pass through ADDITIONAL_HEADERS.
373 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
374 endif()
375 if(ARG_OBJLIBS)
376 set(ALL_FILES ${ARG_OBJLIBS})
377 else()
378 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
379 endif()
380
381 if(ARG_MODULE)
382 if(ARG_SHARED OR ARG_STATIC)
383 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
384 endif()
385 # Plugins that link against a tool are allowed even when plugins in general are not
386 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
387 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
388 return()
389 endif()
390 else()
391 if(ARG_PLUGIN_TOOL)
392 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
393 endif()
394 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
395 set(ARG_SHARED TRUE)
396 endif()
397 if(NOT ARG_SHARED)
398 set(ARG_STATIC TRUE)
399 endif()
400 endif()
401
402 # Generate objlib
403 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
404 # Generate an obj library for both targets.
405 set(obj_name "obj.${name}")
406 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
407 ${ALL_FILES}
408 )
409 llvm_update_compile_flags(${obj_name})
410 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
411
412 # Do add_dependencies(obj) later due to CMake issue 14747.
413 list(APPEND objlibs ${obj_name})
414
415 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
416 endif()
417
418 if(ARG_SHARED AND ARG_STATIC)
419 # static
420 set(name_static "${name}_static")
421 if(ARG_OUTPUT_NAME)
422 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
423 endif()
424 # DEPENDS has been appended to LLVM_COMMON_LIBS.
425 llvm_add_library(${name_static} STATIC
426 ${output_name}
427 OBJLIBS ${ALL_FILES} # objlib
428 LINK_LIBS ${ARG_LINK_LIBS}
429 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
430 )
431 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
432 set(ARG_STATIC)
433 endif()
434
435 if(ARG_MODULE)
436 add_library(${name} MODULE ${ALL_FILES})
437 llvm_setup_rpath(${name})
438 elseif(ARG_SHARED)
439 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
440 add_library(${name} SHARED ${ALL_FILES})
441
442 llvm_setup_rpath(${name})
443
444 else()
445 add_library(${name} STATIC ${ALL_FILES})
446 endif()
447
448 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
449
450 if(DEFINED windows_resource_file)
451 set_windows_version_resource_properties(${name} ${windows_resource_file})
452 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
453 endif()
454
455 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
456 # $<TARGET_OBJECTS> doesn't require compile flags.
457 if(NOT obj_name)
458 llvm_update_compile_flags(${name})
459 endif()
460 add_link_opts( ${name} )
461 if(ARG_OUTPUT_NAME)
462 set_target_properties(${name}
463 PROPERTIES
464 OUTPUT_NAME ${ARG_OUTPUT_NAME}
465 )
466 endif()
467
468 if(ARG_MODULE)
469 set_target_properties(${name} PROPERTIES
470 PREFIX ""
471 SUFFIX ${LLVM_PLUGIN_EXT}
472 )
473 endif()
474
475 if(ARG_SHARED)
476 if(WIN32)
477 set_target_properties(${name} PROPERTIES
478 PREFIX ""
479 )
480 endif()
481
482 # Set SOVERSION on shared libraries that lack explicit SONAME
483 # specifier, on *nix systems that are not Darwin.
484 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
485 set_target_properties(${name}
486 PROPERTIES
487 # Since 4.0.0, the ABI version is indicated by the major version
488 SOVERSION ${LLVM_VERSION_MAJOR}
489 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
490 endif()
491 endif()
492
493 if(ARG_MODULE OR ARG_SHARED)
494 # Do not add -Dname_EXPORTS to the command-line when building files in this
495 # target. Doing so is actively harmful for the modules build because it
496 # creates extra module variants, and not useful because we don't use these
497 # macros.
498 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
499
500 if (LLVM_EXPORTED_SYMBOL_FILE)
501 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
502 endif()
503 endif()
504
505 if(ARG_SHARED AND UNIX)
506 if(NOT APPLE AND ARG_SONAME)
507 get_target_property(output_name ${name} OUTPUT_NAME)
508 if(${output_name} STREQUAL "output_name-NOTFOUND")
509 set(output_name ${name})
510 endif()
511 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
512 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
513 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
514 llvm_install_library_symlink(${api_name} ${library_name} SHARED
515 COMPONENT ${name}
516 ALWAYS_GENERATE)
517 llvm_install_library_symlink(${output_name} ${library_name} SHARED
518 COMPONENT ${name}
519 ALWAYS_GENERATE)
520 endif()
521 endif()
522
523 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
524 # On DLL platforms symbols are imported from the tool by linking against it.
525 set(llvm_libs ${ARG_PLUGIN_TOOL})
526 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
527 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
528 set(llvm_libs LLVM)
529 else()
530 llvm_map_components_to_libnames(llvm_libs
531 ${ARG_LINK_COMPONENTS}
532 ${LLVM_LINK_COMPONENTS}
533 )
534 endif()
535 else()
536 # Components have not been defined explicitly in CMake, so add the
537 # dependency information for this library as defined by LLVMBuild.
538 #
539 # It would be nice to verify that we have the dependencies for this library
540 # name, but using get_property(... SET) doesn't suffice to determine if a
541 # property has been set to an empty value.
542 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
543 endif()
544
545 if(ARG_STATIC)
546 set(libtype INTERFACE)
547 else()
548 # We can use PRIVATE since SO knows its dependent libs.
549 set(libtype PRIVATE)
550 endif()
551
552 target_link_libraries(${name} ${libtype}
553 ${ARG_LINK_LIBS}
554 ${lib_deps}
555 ${llvm_libs}
556 )
557
558 if(LLVM_COMMON_DEPENDS)
559 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
560 # Add dependencies also to objlibs.
561 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
562 foreach(objlib ${objlibs})
563 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
564 endforeach()
565 endif()
566
567 if(ARG_SHARED OR ARG_MODULE)
568 llvm_externalize_debuginfo(${name})
569 endif()
570endfunction()
571
572function(add_llvm_install_targets target)
573 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN})
574 if(ARG_COMPONENT)
575 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
576 endif()
577 if(ARG_PREFIX)
578 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
579 endif()
580
581 add_custom_target(${target}
582 DEPENDS ${ARG_DEPENDS}
583 COMMAND "${CMAKE_COMMAND}"
584 ${component_option}
585 ${prefix_option}
586 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
587 USES_TERMINAL)
588 add_custom_target(${target}-stripped
589 DEPENDS ${ARG_DEPENDS}
590 COMMAND "${CMAKE_COMMAND}"
591 ${component_option}
592 ${prefix_option}
593 -DCMAKE_INSTALL_DO_STRIP=1
594 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
595 USES_TERMINAL)
596endfunction()
597
598macro(add_llvm_library name)
599 cmake_parse_arguments(ARG
600 "SHARED;BUILDTREE_ONLY"
601 ""
602 ""
603 ${ARGN})
604 if( BUILD_SHARED_LIBS OR ARG_SHARED )
605 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
606 else()
607 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
608 endif()
609
610 # Libraries that are meant to only be exposed via the build tree only are
611 # never installed and are only exported as a target in the special build tree
612 # config file.
613 if (NOT ARG_BUILDTREE_ONLY)
614 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
615 endif()
616
617 if( EXCLUDE_FROM_ALL )
618 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
619 elseif(ARG_BUILDTREE_ONLY)
620 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
621 else()
622 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR
623 (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM"))
624 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
625 if(ARG_SHARED OR BUILD_SHARED_LIBS)
626 if(WIN32 OR CYGWIN OR MINGW)
627 set(install_type RUNTIME)
628 set(install_dir bin)
629 else()
630 set(install_type LIBRARY)
631 endif()
632 else()
633 set(install_type ARCHIVE)
634 endif()
635
636 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
637 NOT LLVM_DISTRIBUTION_COMPONENTS)
638 set(export_to_llvmexports EXPORT LLVMExports)
639 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
640 endif()
641
642 install(TARGETS ${name}
643 ${export_to_llvmexports}
644 ${install_type} DESTINATION ${install_dir}
645 COMPONENT ${name})
646
647 if (NOT CMAKE_CONFIGURATION_TYPES)
648 add_llvm_install_targets(install-${name}
649 DEPENDS ${name}
650 COMPONENT ${name})
651 endif()
652 endif()
653 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
654 endif()
655 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
656endmacro(add_llvm_library name)
657
658macro(add_llvm_loadable_module name)
659 llvm_add_library(${name} MODULE ${ARGN})
660 if(NOT TARGET ${name})
661 # Add empty "phony" target
662 add_custom_target(${name})
663 else()
664 if( EXCLUDE_FROM_ALL )
665 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
666 else()
667 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
668 if(WIN32 OR CYGWIN)
669 # DLL platform
670 set(dlldir "bin")
671 else()
672 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
673 endif()
674
675 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
676 NOT LLVM_DISTRIBUTION_COMPONENTS)
677 set(export_to_llvmexports EXPORT LLVMExports)
678 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
679 endif()
680
681 install(TARGETS ${name}
682 ${export_to_llvmexports}
683 LIBRARY DESTINATION ${dlldir}
684 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
685 endif()
686 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
687 endif()
688 endif()
689
690 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
691endmacro(add_llvm_loadable_module name)
692
693
694macro(add_llvm_executable name)
695 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" "" "DEPENDS" ${ARGN})
696 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
697
698 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
699
700 # Generate objlib
701 if(LLVM_ENABLE_OBJLIB)
702 # Generate an obj library for both targets.
703 set(obj_name "obj.${name}")
704 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
705 ${ALL_FILES}
706 )
707 llvm_update_compile_flags(${obj_name})
708 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
709
710 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
711 endif()
712
713 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
714
715 if(XCODE)
716 # Note: the dummy.cpp source file provides no definitions. However,
717 # it forces Xcode to properly link the static library.
718 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
719 endif()
720
721 if( EXCLUDE_FROM_ALL )
722 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
723 else()
724 add_executable(${name} ${ALL_FILES})
725 endif()
726
727 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
728
729 if(NOT ARG_NO_INSTALL_RPATH)
730 llvm_setup_rpath(${name})
731 endif()
732
733 if(DEFINED windows_resource_file)
734 set_windows_version_resource_properties(${name} ${windows_resource_file})
735 endif()
736
737 # $<TARGET_OBJECTS> doesn't require compile flags.
738 if(NOT LLVM_ENABLE_OBJLIB)
739 llvm_update_compile_flags(${name})
740 endif()
741 add_link_opts( ${name} )
742
743 # Do not add -Dname_EXPORTS to the command-line when building files in this
744 # target. Doing so is actively harmful for the modules build because it
745 # creates extra module variants, and not useful because we don't use these
746 # macros.
747 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
748
749 if (LLVM_EXPORTED_SYMBOL_FILE)
750 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
751 endif(LLVM_EXPORTED_SYMBOL_FILE)
752
753 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
754 set(USE_SHARED USE_SHARED)
755 endif()
756
757 set(EXCLUDE_FROM_ALL OFF)
758 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
759 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
760 if( LLVM_COMMON_DEPENDS )
761 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
762 endif( LLVM_COMMON_DEPENDS )
763
764 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
765 llvm_externalize_debuginfo(${name})
766 endif()
767 if (LLVM_PTHREAD_LIB)
768 # libpthreads overrides some standard library symbols, so main
769 # executable must be linked with it in order to provide consistent
770 # API for all shared libaries loaded by this executable.
771 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
772 endif()
773endmacro(add_llvm_executable name)
774
775function(export_executable_symbols target)
776 if (LLVM_EXPORTED_SYMBOL_FILE)
777 # The symbol file should contain the symbols we want the executable to
778 # export
779 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
780 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
781 # Extract the symbols to export from the static libraries that the
782 # executable links against.
783 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
784 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
785 # We need to consider not just the direct link dependencies, but also the
786 # transitive link dependencies. Do this by starting with the set of direct
787 # dependencies, then the dependencies of those dependencies, and so on.
788 get_target_property(new_libs ${target} LINK_LIBRARIES)
789 set(link_libs ${new_libs})
790 while(NOT "${new_libs}" STREQUAL "")
791 foreach(lib ${new_libs})
792 if(TARGET ${lib})
793 get_target_property(lib_type ${lib} TYPE)
794 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
795 list(APPEND static_libs ${lib})
796 else()
797 list(APPEND other_libs ${lib})
798 endif()
799 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
800 foreach(transitive_lib ${transitive_libs})
801 list(FIND link_libs ${transitive_lib} idx)
802 if(TARGET ${transitive_lib} AND idx EQUAL -1)
803 list(APPEND newer_libs ${transitive_lib})
804 list(APPEND link_libs ${transitive_lib})
805 endif()
806 endforeach(transitive_lib)
807 endif()
808 endforeach(lib)
809 set(new_libs ${newer_libs})
810 set(newer_libs "")
811 endwhile()
812 if (MSVC)
813 set(mangling microsoft)
814 else()
815 set(mangling itanium)
816 endif()
817 add_custom_command(OUTPUT ${exported_symbol_file}
818 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
819 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
820 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
821 VERBATIM
822 COMMENT "Generating export list for ${target}")
823 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
824 # If something links against this executable then we want a
825 # transitive link against only the libraries whose symbols
826 # we aren't exporting.
827 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
828 # The default import library suffix that cmake uses for cygwin/mingw is
829 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
830 # where the import libraries of both get named libclang.dll.a. Use a suffix
831 # of ".exe.a" to avoid this.
832 if(CYGWIN OR MINGW)
833 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
834 endif()
835 elseif(NOT (WIN32 OR CYGWIN))
836 # On Windows auto-exporting everything doesn't work because of the limit on
837 # the size of the exported symbol table, but on other platforms we can do
838 # it without any trouble.
839 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
840 if (APPLE)
841 set_property(TARGET ${target} APPEND_STRING PROPERTY
842 LINK_FLAGS " -rdynamic")
843 endif()
844 endif()
845endfunction()
846
847if(NOT LLVM_TOOLCHAIN_TOOLS)
848 set (LLVM_TOOLCHAIN_TOOLS
849 llvm-ar
850 llvm-ranlib
851 llvm-lib
852 llvm-objdump
853 )
854endif()
855
856macro(add_llvm_tool name)
857 if( NOT LLVM_BUILD_TOOLS )
858 set(EXCLUDE_FROM_ALL ON)
859 endif()
860 add_llvm_executable(${name} ${ARGN})
861
862 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
863 if( LLVM_BUILD_TOOLS )
864 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
865 NOT LLVM_DISTRIBUTION_COMPONENTS)
866 set(export_to_llvmexports EXPORT LLVMExports)
867 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
868 endif()
869
870 install(TARGETS ${name}
871 ${export_to_llvmexports}
872 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
873 COMPONENT ${name})
874
875 if (NOT CMAKE_CONFIGURATION_TYPES)
876 add_llvm_install_targets(install-${name}
877 DEPENDS ${name}
878 COMPONENT ${name})
879 endif()
880 endif()
881 endif()
882 if( LLVM_BUILD_TOOLS )
883 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
884 endif()
885 set_target_properties(${name} PROPERTIES FOLDER "Tools")
886endmacro(add_llvm_tool name)
887
888
889macro(add_llvm_example name)
890 if( NOT LLVM_BUILD_EXAMPLES )
891 set(EXCLUDE_FROM_ALL ON)
892 endif()
893 add_llvm_executable(${name} ${ARGN})
894 if( LLVM_BUILD_EXAMPLES )
895 install(TARGETS ${name} RUNTIME DESTINATION examples)
896 endif()
897 set_target_properties(${name} PROPERTIES FOLDER "Examples")
898endmacro(add_llvm_example name)
899
900# This is a macro that is used to create targets for executables that are needed
901# for development, but that are not intended to be installed by default.
902macro(add_llvm_utility name)
903 if ( NOT LLVM_BUILD_UTILS )
904 set(EXCLUDE_FROM_ALL ON)
905 endif()
906
907 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
908 set_target_properties(${name} PROPERTIES FOLDER "Utils")
909 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
910 install (TARGETS ${name}
911 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
912 COMPONENT ${name})
913 if (NOT CMAKE_CONFIGURATION_TYPES)
914 add_llvm_install_targets(install-${name}
915 DEPENDS ${name}
916 COMPONENT ${name})
917 endif()
918 endif()
919endmacro(add_llvm_utility name)
920
921macro(add_llvm_fuzzer name)
922 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
923 if( LLVM_LIB_FUZZING_ENGINE )
924 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
925 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
926 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
927 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
928 elseif( LLVM_USE_SANITIZE_COVERAGE )
929 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
930 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
931 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
932 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
933 elseif( ARG_DUMMY_MAIN )
934 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
935 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
936 endif()
937endmacro()
938
939macro(add_llvm_target target_name)
940 include_directories(BEFORE
941 ${CMAKE_CURRENT_BINARY_DIR}
942 ${CMAKE_CURRENT_SOURCE_DIR})
943 add_llvm_library(LLVM${target_name} ${ARGN})
944 set( CURRENT_LLVM_TARGET LLVM${target_name} )
945endmacro(add_llvm_target)
946
947function(canonicalize_tool_name name output)
948 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
949 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
950 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
951 set(${output} "${nameUPPER}" PARENT_SCOPE)
952endfunction(canonicalize_tool_name)
953
954# Custom add_subdirectory wrapper
955# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
956# path if it differs from the name.
957macro(add_llvm_subdirectory project type name)
958 set(add_llvm_external_dir "${ARGN}")
959 if("${add_llvm_external_dir}" STREQUAL "")
960 set(add_llvm_external_dir ${name})
961 endif()
962 canonicalize_tool_name(${name} nameUPPER)
963 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
964 # Treat it as in-tree subproject.
965 option(${project}_${type}_${nameUPPER}_BUILD
966 "Whether to build ${name} as part of ${project}" On)
967 mark_as_advanced(${project}_${type}_${name}_BUILD)
968 if(${project}_${type}_${nameUPPER}_BUILD)
969 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
970 # Don't process it in add_llvm_implicit_projects().
971 set(${project}_${type}_${nameUPPER}_BUILD OFF)
972 endif()
973 else()
974 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
975 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
976 CACHE PATH "Path to ${name} source directory")
977 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
978 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
979 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
980 endif()
981 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
982 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
983 endif()
984 option(${project}_${type}_${nameUPPER}_BUILD
985 "Whether to build ${name} as part of LLVM"
986 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
987 if (${project}_${type}_${nameUPPER}_BUILD)
988 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
989 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
990 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
991 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
992 endif()
993 # FIXME: It'd be redundant.
994 set(${project}_${type}_${nameUPPER}_BUILD Off)
995 endif()
996 endif()
997endmacro()
998
999# Add external project that may want to be built as part of llvm such as Clang,
1000# lld, and Polly. This adds two options. One for the source directory of the
1001# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1002# enable or disable building it with everything else.
1003# Additional parameter can be specified as the name of directory.
1004macro(add_llvm_external_project name)
1005 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1006endmacro()
1007
1008macro(add_llvm_tool_subdirectory name)
1009 add_llvm_external_project(${name})
1010endmacro(add_llvm_tool_subdirectory)
1011
1012function(get_project_name_from_src_var var output)
1013 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1014 MACHED_TOOL "${var}")
1015 if(MACHED_TOOL)
1016 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1017 else()
1018 set(${output} PARENT_SCOPE)
1019 endif()
1020endfunction()
1021
1022function(create_subdirectory_options project type)
1023 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1024 foreach(dir ${sub-dirs})
1025 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1026 canonicalize_tool_name(${dir} name)
1027 option(${project}_${type}_${name}_BUILD
1028 "Whether to build ${name} as part of ${project}" On)
1029 mark_as_advanced(${project}_${type}_${name}_BUILD)
1030 endif()
1031 endforeach()
1032endfunction(create_subdirectory_options)
1033
1034function(create_llvm_tool_options)
1035 create_subdirectory_options(LLVM TOOL)
1036endfunction(create_llvm_tool_options)
1037
1038function(llvm_add_implicit_projects project)
1039 set(list_of_implicit_subdirs "")
1040 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1041 foreach(dir ${sub-dirs})
1042 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1043 canonicalize_tool_name(${dir} name)
1044 if (${project}_TOOL_${name}_BUILD)
1045 get_filename_component(fn "${dir}" NAME)
1046 list(APPEND list_of_implicit_subdirs "${fn}")
1047 endif()
1048 endif()
1049 endforeach()
1050
1051 foreach(external_proj ${list_of_implicit_subdirs})
1052 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1053 endforeach()
1054endfunction(llvm_add_implicit_projects)
1055
1056function(add_llvm_implicit_projects)
1057 llvm_add_implicit_projects(LLVM)
1058endfunction(add_llvm_implicit_projects)
1059
1060# Generic support for adding a unittest.
1061function(add_unittest test_suite test_name)
1062 if( NOT LLVM_BUILD_TESTS )
1063 set(EXCLUDE_FROM_ALL ON)
1064 endif()
1065
1066 # Our current version of gtest does not properly recognize C++11 support
1067 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1068 # itself requires C++11, we can safely force it on unconditionally so that
1069 # we don't have to fight with the buggy gtest check.
1070 add_definitions(-DGTEST_LANG_CXX11=1)
1071 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1072
1073 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1074 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1075 if (NOT LLVM_ENABLE_THREADS)
1076 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1077 endif ()
1078
1079 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1080 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1081 endif ()
1082 # Some parts of gtest rely on this GNU extension, don't warn on it.
1083 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1084 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1085 endif()
1086
1087 set(LLVM_REQUIRES_RTTI OFF)
1088
1089 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1090 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1091 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1092 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1093 # libpthreads overrides some standard library symbols, so main
1094 # executable must be linked with it in order to provide consistent
1095 # API for all shared libaries loaded by this executable.
1096 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1097
1098 add_dependencies(${test_suite} ${test_name})
1099 get_target_property(test_suite_folder ${test_suite} FOLDER)
1100 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1101 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1102 endif ()
1103endfunction()
1104
1105function(llvm_add_go_executable binary pkgpath)
1106 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1107
1108 if(LLVM_BINDINGS MATCHES "go")
1109 # FIXME: This should depend only on the libraries Go needs.
1110 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1111 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1112 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1113 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1114 set(cppflags "")
1115 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1116 foreach(d ${include_dirs})
1117 set(cppflags "${cppflags} -I${d}")
1118 endforeach(d)
1119 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1120 add_custom_command(OUTPUT ${binpath}
1121 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1122 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1123 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1124 ${llvmlibs} ${ARG_DEPENDS}
1125 COMMENT "Building Go executable ${binary}"
1126 VERBATIM)
1127 if (ARG_ALL)
1128 add_custom_target(${binary} ALL DEPENDS ${binpath})
1129 else()
1130 add_custom_target(${binary} DEPENDS ${binpath})
1131 endif()
1132 endif()
1133endfunction()
1134
1135# This function canonicalize the CMake variables passed by names
1136# from CMake boolean to 0/1 suitable for passing into Python or C++,
1137# in place.
1138function(llvm_canonicalize_cmake_booleans)
1139 foreach(var ${ARGN})
1140 if(${var})
1141 set(${var} 1 PARENT_SCOPE)
1142 else()
1143 set(${var} 0 PARENT_SCOPE)
1144 endif()
1145 endforeach()
1146endfunction(llvm_canonicalize_cmake_booleans)
1147
1148macro(set_llvm_build_mode)
1149 # Configuration-time: See Unit/lit.site.cfg.in
1150 if (CMAKE_CFG_INTDIR STREQUAL ".")
1151 set(LLVM_BUILD_MODE ".")
1152 else ()
1153 set(LLVM_BUILD_MODE "%(build_mode)s")
1154 endif ()
1155endmacro()
1156
1157# This function provides an automatic way to 'configure'-like generate a file
1158# based on a set of common and custom variables, specifically targeting the
1159# variables needed for the 'lit.site.cfg' files. This function bundles the
1160# common variables that any Lit instance is likely to need, and custom
1161# variables can be passed in.
1162function(configure_lit_site_cfg site_in site_out)
1163 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1164
1165 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1166 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1167 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1168 endif()
1169 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1170 set(ARG_OUTPUT_MAPPING "${site_out}")
1171 endif()
1172
1173 foreach(c ${LLVM_TARGETS_TO_BUILD})
1174 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1175 endforeach(c)
1176 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1177
1178 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1179
1180 set_llvm_build_mode()
1181
1182 # They below might not be the build tree but provided binary tree.
1183 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1184 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1185 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1186 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1187
1188 # SHLIBDIR points the build tree.
1189 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1190
1191 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1192 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1193 # plugins. We may rename it.
1194 if(LLVM_ENABLE_PLUGINS)
1195 set(ENABLE_SHARED "1")
1196 else()
1197 set(ENABLE_SHARED "0")
1198 endif()
1199
1200 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1201 set(ENABLE_ASSERTIONS "1")
1202 else()
1203 set(ENABLE_ASSERTIONS "0")
1204 endif()
1205
1206 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1207 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1208
1209 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1210 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1211 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1212
1213 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1214
1215 # Override config_target_triple (and the env)
1216 if(LLVM_TARGET_TRIPLE_ENV)
1217 # This is expanded into the heading.
1218 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1219 "import os\n"
1220 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1221 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1222 )
1223
1224 # This is expanded to; config.target_triple = ""+config.target_triple+""
1225 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1226 endif()
1227
1228 string(CONCAT LIT_SITE_CFG_IN_FOOTER
1229 "import lit.llvm\n"
1230 "lit.llvm.initialize(lit_config, config)\n")
1231
1232 configure_file(${site_in} ${site_out} @ONLY)
1233 if (EXISTS "${ARG_MAIN_CONFIG}")
1234 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
1235 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1236 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1237 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1238 endif()
1239endfunction()
1240
1241function(dump_all_cmake_variables)
1242 get_cmake_property(_variableNames VARIABLES)
1243 foreach (_variableName ${_variableNames})
1244 message(STATUS "${_variableName}=${${_variableName}}")
1245 endforeach()
1246endfunction()
1247
1248function(get_llvm_lit_path base_dir file_name)
1249 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1250
1251 if (ARG_ALLOW_EXTERNAL)
1252 set(LLVM_DEFAULT_EXTERNAL_LIT "${LLVM_EXTERNAL_LIT}")
1253 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1254 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1255 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1256 endif()
1257
1258 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1259 if (EXISTS ${LLVM_EXTERNAL_LIT})
1260 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1261 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1262 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1263 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1264 return()
1265 else()
1266 message(WARN "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1267 endif()
1268 endif()
1269 endif()
1270
1271 set(lit_file_name "llvm-lit")
1272 if (WIN32 AND NOT CYGWIN)
1273 # llvm-lit needs suffix.py for multiprocess to find a main module.
1274 set(lit_file_name "${lit_file_name}.py")
1275 endif ()
1276 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1277
1278 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1279 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1280 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1281 endif()
1282
1283 # Allow individual projects to provide an override
1284 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1285 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1286 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1287 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1288 else()
1289 set(LLVM_LIT_BASE_DIR "")
1290 endif()
1291
1292 # Cache this so we don't have to do it again and have subsequent calls
1293 # potentially disagree on the value.
1294 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1295 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1296endfunction()
1297
1298# A raw function to create a lit target. This is used to implement the testuite
1299# management functions.
1300function(add_lit_target target comment)
1301 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1302 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1303 separate_arguments(LIT_ARGS)
1304 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1305 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1306 endif ()
1307
1308 # Get the path to the lit to *run* tests with. This can be overriden by
1309 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1310 get_llvm_lit_path(
1311 lit_base_dir
1312 lit_file_name
1313 ALLOW_EXTERNAL
1314 )
1315
1316 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1317 list(APPEND LIT_COMMAND ${LIT_ARGS})
1318 foreach(param ${ARG_PARAMS})
1319 list(APPEND LIT_COMMAND --param ${param})
1320 endforeach()
1321 if (ARG_UNPARSED_ARGUMENTS)
1322 add_custom_target(${target}
1323 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1324 COMMENT "${comment}"
1325 USES_TERMINAL
1326 )
1327 else()
1328 add_custom_target(${target}
1329 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1330 message(STATUS "${target} does nothing.")
1331 endif()
1332 if (ARG_DEPENDS)
1333 add_dependencies(${target} ${ARG_DEPENDS})
1334 endif()
1335
1336 # Tests should be excluded from "Build Solution".
1337 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1338endfunction()
1339
1340# A function to add a set of lit test suites to be driven through 'check-*' targets.
1341function(add_lit_testsuite target comment)
1342 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1343
1344 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1345 if(NOT EXCLUDE_FROM_ALL)
1346 # Register the testsuites, params and depends for the global check rule.
1347 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1348 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1349 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1350 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1351 endif()
1352
1353 # Produce a specific suffixed check rule.
1354 add_lit_target(${target} ${comment}
1355 ${ARG_UNPARSED_ARGUMENTS}
1356 PARAMS ${ARG_PARAMS}
1357 DEPENDS ${ARG_DEPENDS}
1358 ARGS ${ARG_ARGS}
1359 )
1360endfunction()
1361
1362function(add_lit_testsuites project directory)
1363 if (NOT CMAKE_CONFIGURATION_TYPES)
1364 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1365
1366 # Search recursively for test directories by assuming anything not
1367 # in a directory called Inputs contains tests.
1368 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1369 foreach(lit_suite ${to_process})
1370 if(NOT IS_DIRECTORY ${lit_suite})
1371 continue()
1372 endif()
1373 string(FIND ${lit_suite} Inputs is_inputs)
1374 string(FIND ${lit_suite} Output is_output)
1375 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1376 continue()
1377 endif()
1378
1379 # Create a check- target for the directory.
1380 string(REPLACE ${directory} "" name_slash ${lit_suite})
1381 if (name_slash)
1382 string(REPLACE "/" "-" name_slash ${name_slash})
1383 string(REPLACE "\\" "-" name_dashes ${name_slash})
1384 string(TOLOWER "${project}${name_dashes}" name_var)
1385 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1386 ${lit_suite}
1387 PARAMS ${ARG_PARAMS}
1388 DEPENDS ${ARG_DEPENDS}
1389 ARGS ${ARG_ARGS}
1390 )
1391 endif()
1392 endforeach()
1393 endif()
1394endfunction()
1395
1396function(llvm_install_library_symlink name dest type)
1397 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1398 foreach(path ${CMAKE_MODULE_PATH})
1399 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1400 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1401 break()
1402 endif()
1403 endforeach()
1404
1405 set(component ${ARG_COMPONENT})
1406 if(NOT component)
1407 set(component ${name})
1408 endif()
1409
1410 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1411 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1412
1413 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1414 if(WIN32 AND "${type}" STREQUAL "SHARED")
1415 set(output_dir bin)
1416 endif()
1417
1418 install(SCRIPT ${INSTALL_SYMLINK}
1419 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1420 COMPONENT ${component})
1421
1422 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1423 add_llvm_install_targets(install-${name}
1424 DEPENDS ${name} ${dest} install-${dest}
1425 COMPONENT ${name})
1426 endif()
1427endfunction()
1428
1429function(llvm_install_symlink name dest)
1430 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1431 foreach(path ${CMAKE_MODULE_PATH})
1432 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1433 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1434 break()
1435 endif()
1436 endforeach()
1437
1438 if(ARG_COMPONENT)
1439 set(component ${ARG_COMPONENT})
1440 else()
1441 if(ARG_ALWAYS_GENERATE)
1442 set(component ${dest})
1443 else()
1444 set(component ${name})
1445 endif()
1446 endif()
1447
1448 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1449 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1450
1451 install(SCRIPT ${INSTALL_SYMLINK}
1452 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1453 COMPONENT ${component})
1454
1455 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1456 add_llvm_install_targets(install-${name}
1457 DEPENDS ${name} ${dest} install-${dest}
1458 COMPONENT ${name})
1459 endif()
1460endfunction()
1461
1462function(add_llvm_tool_symlink link_name target)
1463 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1464 set(dest_binary "$<TARGET_FILE:${target}>")
1465
1466 # This got a bit gross... For multi-configuration generators the target
1467 # properties return the resolved value of the string, not the build system
1468 # expression. To reconstruct the platform-agnostic path we have to do some
1469 # magic. First we grab one of the types, and a type-specific path. Then from
1470 # the type-specific path we find the last occurrence of the type in the path,
1471 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1472 # agnostic again.
1473 if(NOT ARG_OUTPUT_DIR)
1474 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1475 # the same directory.
1476 if(CMAKE_HOST_UNIX)
1477 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1478 endif()
1479 if(CMAKE_CONFIGURATION_TYPES)
1480 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1481 string(TOUPPER ${first_type} first_type_upper)
1482 set(first_type_suffix _${first_type_upper})
1483 endif()
1484 get_target_property(target_type ${target} TYPE)
1485 if(${target_type} STREQUAL "STATIC_LIBRARY")
1486 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1487 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1488 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1489 else()
1490 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1491 endif()
1492 if(CMAKE_CONFIGURATION_TYPES)
1493 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1494 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1495 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1496 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1497 path_suffix ${path_suffix})
1498 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1499 endif()
1500 endif()
1501
1502 if(CMAKE_HOST_UNIX)
1503 set(LLVM_LINK_OR_COPY create_symlink)
1504 else()
1505 set(LLVM_LINK_OR_COPY copy)
1506 endif()
1507
1508 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1509
1510 set(target_name ${link_name})
1511 if(TARGET ${link_name})
1512 set(target_name ${link_name}-link)
1513 endif()
1514
1515
1516 if(ARG_ALWAYS_GENERATE)
1517 set_property(DIRECTORY APPEND PROPERTY
1518 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1519 add_custom_command(TARGET ${target} POST_BUILD
1520 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1521 else()
1522 add_custom_command(OUTPUT ${output_path}
1523 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1524 DEPENDS ${target})
1525 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1526 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1527
1528 # Make sure both the link and target are toolchain tools
1529 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1530 set(TOOL_IS_TOOLCHAIN ON)
1531 endif()
1532
1533 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1534 llvm_install_symlink(${link_name} ${target})
1535 endif()
1536 endif()
1537endfunction()
1538
1539function(llvm_externalize_debuginfo name)
1540 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1541 return()
1542 endif()
1543
1544 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1545 if(APPLE)
1546 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1547 else()
1548 set(strip_command COMMAND strip -gx $<TARGET_FILE:${name}>)
1549 endif()
1550 endif()
1551
1552 if(APPLE)
1553 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1554 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1555
1556 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1557 set_property(TARGET ${name} APPEND_STRING PROPERTY
1558 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1559 endif()
1560 add_custom_command(TARGET ${name} POST_BUILD
1561 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
1562 ${strip_command}
1563 )
1564 else()
1565 add_custom_command(TARGET ${name} POST_BUILD
1566 COMMAND objcopy --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1567 ${strip_command} -R .gnu_debuglink
1568 COMMAND objcopy --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1569 )
1570 endif()
1571endfunction()
1572
1573function(llvm_setup_rpath name)
1574 if(CMAKE_INSTALL_RPATH)
1575 return()
1576 endif()
1577
1578 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1579 set(extra_libdir ${LLVM_LIBRARY_DIR})
1580 elseif(LLVM_BUILD_LIBRARY_DIR)
1581 set(extra_libdir ${LLVM_LIBRARY_DIR})
1582 endif()
1583
1584 if (APPLE)
1585 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1586 set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1587 elseif(UNIX)
1588 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1589 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1590 set_property(TARGET ${name} APPEND_STRING PROPERTY
1591 LINK_FLAGS " -Wl,-z,origin ")
1592 endif()
1593 if(LLVM_LINKER_IS_GNULD)
1594 # $ORIGIN is not interpreted at link time by ld.bfd
1595 set_property(TARGET ${name} APPEND_STRING PROPERTY
1596 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1597 endif()
1598 else()
1599 return()
1600 endif()
1601
1602 set_target_properties(${name} PROPERTIES
1603 BUILD_WITH_INSTALL_RPATH On
1604 INSTALL_RPATH "${_install_rpath}"
1605 ${_install_name_dir})
1606endfunction()
1607
1608function(setup_dependency_debugging name)
1609 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1610 return()
1611 endif()
1612
1613 if("intrinsics_gen" IN_LIST ARGN)
1614 return()
1615 endif()
1616
1617 set(deny_attributes_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.gen\"))")
1618 set(deny_intrinsics_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.gen\"))")
1619
1620 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_gen} ${deny_intrinsics_gen}'")
1621 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1622endfunction()
1623
1624# Figure out if we can track VC revisions.
1625function(find_first_existing_file out_var)
1626 foreach(file ${ARGN})
1627 if(EXISTS "${file}")
1628 set(${out_var} "${file}" PARENT_SCOPE)
1629 return()
1630 endif()
1631 endforeach()
1632endfunction()
1633
1634macro(find_first_existing_vc_file out_var path)
1635 find_program(git_executable NAMES git git.exe git.cmd)
1636 # Run from a subdirectory to force git to print an absolute path.
1637 execute_process(COMMAND ${git_executable} rev-parse --git-dir
1638 WORKING_DIRECTORY ${path}/cmake
1639 RESULT_VARIABLE git_result
1640 OUTPUT_VARIABLE git_dir
1641 ERROR_QUIET)
1642 if(git_result EQUAL 0)
1643 string(STRIP "${git_dir}" git_dir)
1644 set(${out_var} "${git_dir}/logs/HEAD")
1645 # some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1646 if (NOT EXISTS "${git_dir}/logs/HEAD")
1647 file(WRITE "${git_dir}/logs/HEAD" "")
1648 endif()
1649 else()
1650 find_first_existing_file(${out_var}
1651 "${path}/.svn/wc.db" # SVN 1.7
1652 "${path}/.svn/entries" # SVN 1.6
1653 )
1654 endif()
1655endmacro()