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