blob: 97c9980c7de3173344790188f486faa4f485d237 [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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020011 list(APPEND LLVM_COMPILE_CFLAGS " ${LLVM_COMPILE_FLAGS}")
12
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010013 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to
14 # force EH
15 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
16 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
17 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
18 set(LLVM_REQUIRES_RTTI ON)
19 endif()
20 if(MSVC)
21 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
22 endif()
23 else()
24 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
25 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
Andrew Walbran3d2c1972020-04-07 12:24:26 +010026 if(NOT LLVM_ENABLE_UNWIND_TABLES)
27 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables")
28 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables")
29 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030 elseif(MSVC)
31 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
32 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020033 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
34 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035 endif()
36 endif()
37
38 # LLVM_REQUIRES_RTTI is an internal flag that individual
39 # targets can use to force RTTI
40 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
41 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
42 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
43 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
44 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
45 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
46 elseif (MSVC)
47 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020048 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
49 list(APPEND LLVM_COMPILE_FLAGS "-qnortti")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 endif ()
51 elseif(MSVC)
52 list(APPEND LLVM_COMPILE_FLAGS "/GR")
53 endif()
54
55 # Assume that;
56 # - LLVM_COMPILE_FLAGS is list.
57 # - PROPERTY COMPILE_FLAGS is string.
58 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020059 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060
61 if(update_src_props)
62 foreach(fn ${sources})
63 get_filename_component(suf ${fn} EXT)
64 if("${suf}" STREQUAL ".cpp")
65 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
66 COMPILE_FLAGS "${target_compile_flags}")
67 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020068 if("${suf}" STREQUAL ".c")
69 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
70 COMPILE_FLAGS "${target_compile_cflags}")
71 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 endforeach()
73 else()
74 # Update target props, since all sources are C++.
75 set_property(TARGET ${name} APPEND_STRING PROPERTY
76 COMPILE_FLAGS "${target_compile_flags}")
77 endif()
78
79 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
80endfunction()
81
82function(add_llvm_symbol_exports target_name export_file)
83 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
84 set(native_export_file "${target_name}.exports")
85 add_custom_command(OUTPUT ${native_export_file}
86 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
87 DEPENDS ${export_file}
88 VERBATIM
89 COMMENT "Creating export file for ${target_name}")
90 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Andrew Walbran16937d02019-10-22 13:54:20 +010091 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020093 # FIXME: `-Wl,-bE:` bypasses whatever handling there is in the build
94 # compiler driver to defer to the specified export list.
95 set(native_export_file "${export_file}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
97 LINK_FLAGS " -Wl,-bE:${export_file}")
98 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
99 # Gold and BFD ld require a version script rather than a plain list.
100 set(native_export_file "${target_name}.exports")
101 # FIXME: Don't write the "local:" line on OpenBSD.
102 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
103 add_custom_command(OUTPUT ${native_export_file}
104 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
105 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
106 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
107 COMMAND echo " local: *;" >> ${native_export_file}
108 COMMAND echo "};" >> ${native_export_file}
109 DEPENDS ${export_file}
110 VERBATIM
111 COMMENT "Creating export file for ${target_name}")
112 if (${LLVM_LINKER_IS_SOLARISLD})
113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Andrew Walbran16937d02019-10-22 13:54:20 +0100114 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115 else()
116 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Andrew Walbran16937d02019-10-22 13:54:20 +0100117 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 endif()
119 else()
120 set(native_export_file "${target_name}.def")
121
122 add_custom_command(OUTPUT ${native_export_file}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200123 COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100124 < ${export_file} > ${native_export_file}
125 DEPENDS ${export_file}
126 VERBATIM
127 COMMENT "Creating export file for ${target_name}")
128 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
129 if(MSVC)
130 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
131 endif()
132 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
133 LINK_FLAGS " ${export_file_linker_flag}")
134 endif()
135
136 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
137 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
138
139 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
140 foreach(src ${srcs})
141 get_filename_component(extension ${src} EXT)
142 if(extension STREQUAL ".cpp")
143 set(first_source_file ${src})
144 break()
145 endif()
146 endforeach()
147
148 # Force re-linking when the exports file changes. Actually, it
149 # forces recompilation of the source file. The LINK_DEPENDS target
150 # property only works for makefile-based generators.
151 # FIXME: This is not safe because this will create the same target
152 # ${native_export_file} in several different file:
153 # - One where we emitted ${target_name}_exports
154 # - One where we emitted the build command for the following object.
155 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
156 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
157
158 set_property(DIRECTORY APPEND
159 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
160
161 add_dependencies(${target_name} ${target_name}_exports)
162
163 # Add dependency to *_exports later -- CMake issue 14747
164 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
165 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
166endfunction(add_llvm_symbol_exports)
167
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168if (NOT DEFINED LLVM_LINKER_DETECTED)
169 if(APPLE)
170 execute_process(
171 COMMAND "${CMAKE_LINKER}" -v
172 ERROR_VARIABLE stderr
173 )
174 if("${stderr}" MATCHES "PROJECT:ld64")
175 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
176 set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "")
177 message(STATUS "Linker detection: ld64")
178 else()
179 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
180 message(STATUS "Linker detection: unknown")
181 endif()
182 elseif(NOT WIN32)
183 # Detect what linker we have here
184 if( LLVM_USE_LINKER )
185 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
186 else()
187 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
188 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version)
189 endif()
190 execute_process(
191 COMMAND ${command}
192 OUTPUT_VARIABLE stdout
193 ERROR_VARIABLE stderr
194 )
195 if("${stdout}" MATCHES "GNU gold")
196 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
197 set(LLVM_LINKER_IS_GOLD YES CACHE INTERNAL "")
198 message(STATUS "Linker detection: GNU Gold")
199 elseif("${stdout}" MATCHES "^LLD")
200 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
201 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
202 message(STATUS "Linker detection: LLD")
203 elseif("${stdout}" MATCHES "GNU ld")
204 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
205 set(LLVM_LINKER_IS_GNULD YES CACHE INTERNAL "")
206 message(STATUS "Linker detection: GNU ld")
207 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
208 "${stdout}" MATCHES "Solaris Link Editors")
209 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
210 set(LLVM_LINKER_IS_SOLARISLD YES CACHE INTERNAL "")
211 message(STATUS "Linker detection: Solaris ld")
212 else()
213 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
214 message(STATUS "Linker detection: unknown")
215 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216 endif()
217endif()
218
219function(add_link_opts target_name)
220 # Don't use linker optimizations in debug builds since it slows down the
221 # linker in a context where the optimizations are not important.
222 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
223
224 # Pass -O3 to the linker. This enabled different optimizations on different
225 # linkers.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200226 if(NOT (CMAKE_SYSTEM_NAME MATCHES "Darwin|SunOS|AIX|OS390" OR WIN32))
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
228 LINK_FLAGS " -Wl,-O3")
229 endif()
230
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100231 if(NOT LLVM_NO_DEAD_STRIP)
232 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
233 # ld64's implementation of -dead_strip breaks tools that use plugins.
234 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
235 LINK_FLAGS " -Wl,-dead_strip")
236 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200237 # Support for ld -z discard-unused=sections was only added in
238 # Solaris 11.4.
239 include(CheckLinkerFlag)
240 check_linker_flag("-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
241 if (LINKER_SUPPORTS_Z_DISCARD_UNUSED)
242 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
243 LINK_FLAGS " -Wl,-z,discard-unused=sections")
244 endif()
245 elseif(NOT WIN32 AND NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD|AIX|OS390")
246 # TODO Revisit this later on z/OS.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100247 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
248 LINK_FLAGS " -Wl,--gc-sections")
249 endif()
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100250 else() #LLVM_NO_DEAD_STRIP
251 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
252 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
253 LINK_FLAGS " -Wl,-bnogc")
254 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100255 endif()
256 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200257
258 if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
259 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
260 LINK_FLAGS " -Wl,-brtl")
261 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262endfunction(add_link_opts)
263
264# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
265# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
266# or a certain builder, for eaxample, msbuild.exe, would be confused.
267function(set_output_directory target)
268 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
269
270 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
271 # It affects output of add_library(MODULE).
272 if(WIN32 OR CYGWIN)
273 # DLL platform
274 set(module_dir ${ARG_BINARY_DIR})
275 else()
276 set(module_dir ${ARG_LIBRARY_DIR})
277 endif()
278 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
279 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
280 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
281 if(ARG_BINARY_DIR)
282 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
283 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
284 endif()
285 if(ARG_LIBRARY_DIR)
286 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
287 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
288 endif()
289 if(module_dir)
290 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
291 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
292 endif()
293 endforeach()
294 else()
295 if(ARG_BINARY_DIR)
296 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
297 endif()
298 if(ARG_LIBRARY_DIR)
299 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
300 endif()
301 if(module_dir)
302 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
303 endif()
304 endif()
305endfunction()
306
307# If on Windows and building with MSVC, add the resource script containing the
308# VERSIONINFO data to the project. This embeds version resource information
309# into the output .exe or .dll.
310# TODO: Enable for MinGW Windows builds too.
311#
312function(add_windows_version_resource_file OUT_VAR)
313 set(sources ${ARGN})
314 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
315 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
316 if(EXISTS ${resource_file})
317 set(sources ${sources} ${resource_file})
318 source_group("Resource Files" ${resource_file})
319 set(windows_resource_file ${resource_file} PARENT_SCOPE)
320 endif()
321 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
322
323 set(${OUT_VAR} ${sources} PARENT_SCOPE)
324endfunction(add_windows_version_resource_file)
325
326# set_windows_version_resource_properties(name resource_file...
327# VERSION_MAJOR int
328# Optional major version number (defaults to LLVM_VERSION_MAJOR)
329# VERSION_MINOR int
330# Optional minor version number (defaults to LLVM_VERSION_MINOR)
331# VERSION_PATCHLEVEL int
332# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
333# VERSION_STRING
334# Optional version string (defaults to PACKAGE_VERSION)
335# PRODUCT_NAME
336# Optional product name string (defaults to "LLVM")
337# )
338function(set_windows_version_resource_properties name resource_file)
339 cmake_parse_arguments(ARG
340 ""
341 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
342 ""
343 ${ARGN})
344
345 if (NOT DEFINED ARG_VERSION_MAJOR)
346 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
347 endif()
348
349 if (NOT DEFINED ARG_VERSION_MINOR)
350 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
351 endif()
352
353 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
354 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
355 endif()
356
357 if (NOT DEFINED ARG_VERSION_STRING)
358 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
359 endif()
360
361 if (NOT DEFINED ARG_PRODUCT_NAME)
362 set(ARG_PRODUCT_NAME "LLVM")
363 endif()
364
365 set_property(SOURCE ${resource_file}
366 PROPERTY COMPILE_FLAGS /nologo)
367 set_property(SOURCE ${resource_file}
368 PROPERTY COMPILE_DEFINITIONS
369 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
370 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
371 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
372 "RC_VERSION_FIELD_4=0"
373 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
374 "RC_INTERNAL_NAME=\"${name}\""
375 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
376 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
377endfunction(set_windows_version_resource_properties)
378
379# llvm_add_library(name sources...
380# SHARED;STATIC
381# STATIC by default w/o BUILD_SHARED_LIBS.
382# SHARED by default w/ BUILD_SHARED_LIBS.
383# OBJECT
384# Also create an OBJECT library target. Default if STATIC && SHARED.
385# MODULE
386# Target ${name} might not be created on unsupported platforms.
387# Check with "if(TARGET ${name})".
388# DISABLE_LLVM_LINK_LLVM_DYLIB
389# Do not link this library to libLLVM, even if
390# LLVM_LINK_LLVM_DYLIB is enabled.
391# OUTPUT_NAME name
392# Corresponds to OUTPUT_NAME in target properties.
393# DEPENDS targets...
394# Same semantics as add_dependencies().
395# LINK_COMPONENTS components...
396# Same as the variable LLVM_LINK_COMPONENTS.
397# LINK_LIBS lib_targets...
398# Same semantics as target_link_libraries().
399# ADDITIONAL_HEADERS
400# May specify header files for IDE generators.
401# SONAME
402# Should set SONAME link flags and create symlinks
Andrew Walbran16937d02019-10-22 13:54:20 +0100403# NO_INSTALL_RPATH
404# Suppress default RPATH settings in shared libraries.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100405# PLUGIN_TOOL
406# The tool (i.e. cmake target) that this plugin will link against
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200407# COMPONENT_LIB
408# This is used to specify that this is a component library of
409# LLVM which means that the source resides in llvm/lib/ and it is a
410# candidate for inclusion into libLLVM.so.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100411# )
412function(llvm_add_library name)
413 cmake_parse_arguments(ARG
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200414 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100415 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100416 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
417 ${ARGN})
418 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
419 if(ARG_ADDITIONAL_HEADERS)
420 # Pass through ADDITIONAL_HEADERS.
421 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
422 endif()
423 if(ARG_OBJLIBS)
424 set(ALL_FILES ${ARG_OBJLIBS})
425 else()
426 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
427 endif()
428
429 if(ARG_MODULE)
430 if(ARG_SHARED OR ARG_STATIC)
431 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
432 endif()
433 # Plugins that link against a tool are allowed even when plugins in general are not
434 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
435 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
436 return()
437 endif()
438 else()
439 if(ARG_PLUGIN_TOOL)
440 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
441 endif()
442 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
443 set(ARG_SHARED TRUE)
444 endif()
445 if(NOT ARG_SHARED)
446 set(ARG_STATIC TRUE)
447 endif()
448 endif()
449
450 # Generate objlib
451 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
452 # Generate an obj library for both targets.
453 set(obj_name "obj.${name}")
454 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
455 ${ALL_FILES}
456 )
457 llvm_update_compile_flags(${obj_name})
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100458 if(CMAKE_GENERATOR STREQUAL "Xcode")
459 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
460 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
461 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
462 endif()
463 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100464
465 # Do add_dependencies(obj) later due to CMake issue 14747.
466 list(APPEND objlibs ${obj_name})
467
468 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200469 if(ARG_DEPENDS)
470 add_dependencies(${obj_name} ${ARG_DEPENDS})
471 endif()
472 # Treat link libraries like PUBLIC dependencies. LINK_LIBS might
473 # result in generating header files. Add a dependendency so that
474 # the generated header is created before this object library.
475 if(ARG_LINK_LIBS)
476 cmake_parse_arguments(LINK_LIBS_ARG
477 ""
478 ""
479 "PUBLIC;PRIVATE"
480 ${ARG_LINK_LIBS})
481 foreach(link_lib ${LINK_LIBS_ARG_PUBLIC})
482 if(LLVM_PTHREAD_LIB)
483 # Can't specify a dependence on -lpthread
484 if(NOT ${link_lib} STREQUAL ${LLVM_PTHREAD_LIB})
485 add_dependencies(${obj_name} ${link_lib})
486 endif()
487 else()
488 add_dependencies(${obj_name} ${link_lib})
489 endif()
490 endforeach()
491 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100492 endif()
493
494 if(ARG_SHARED AND ARG_STATIC)
495 # static
496 set(name_static "${name}_static")
497 if(ARG_OUTPUT_NAME)
498 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
499 endif()
500 # DEPENDS has been appended to LLVM_COMMON_LIBS.
501 llvm_add_library(${name_static} STATIC
502 ${output_name}
503 OBJLIBS ${ALL_FILES} # objlib
504 LINK_LIBS ${ARG_LINK_LIBS}
505 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
506 )
507 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
508 set(ARG_STATIC)
509 endif()
510
511 if(ARG_MODULE)
512 add_library(${name} MODULE ${ALL_FILES})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100513 elseif(ARG_SHARED)
514 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
515 add_library(${name} SHARED ${ALL_FILES})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100516 else()
517 add_library(${name} STATIC ${ALL_FILES})
518 endif()
519
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200520 if(ARG_COMPONENT_LIB)
521 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE)
522 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name})
523 endif()
524
Andrew Walbran16937d02019-10-22 13:54:20 +0100525 if(NOT ARG_NO_INSTALL_RPATH)
526 if(ARG_MODULE OR ARG_SHARED)
527 llvm_setup_rpath(${name})
528 endif()
529 endif()
530
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100531 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
532
533 if(DEFINED windows_resource_file)
534 set_windows_version_resource_properties(${name} ${windows_resource_file})
535 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
536 endif()
537
538 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
539 # $<TARGET_OBJECTS> doesn't require compile flags.
540 if(NOT obj_name)
541 llvm_update_compile_flags(${name})
542 endif()
543 add_link_opts( ${name} )
544 if(ARG_OUTPUT_NAME)
545 set_target_properties(${name}
546 PROPERTIES
547 OUTPUT_NAME ${ARG_OUTPUT_NAME}
548 )
549 endif()
550
551 if(ARG_MODULE)
552 set_target_properties(${name} PROPERTIES
553 PREFIX ""
554 SUFFIX ${LLVM_PLUGIN_EXT}
555 )
556 endif()
557
558 if(ARG_SHARED)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200559 if(MSVC)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100560 set_target_properties(${name} PROPERTIES
561 PREFIX ""
562 )
563 endif()
564
565 # Set SOVERSION on shared libraries that lack explicit SONAME
566 # specifier, on *nix systems that are not Darwin.
567 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
568 set_target_properties(${name}
569 PROPERTIES
570 # Since 4.0.0, the ABI version is indicated by the major version
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100571 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100572 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
573 endif()
574 endif()
575
576 if(ARG_MODULE OR ARG_SHARED)
577 # Do not add -Dname_EXPORTS to the command-line when building files in this
578 # target. Doing so is actively harmful for the modules build because it
579 # creates extra module variants, and not useful because we don't use these
580 # macros.
581 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
582
583 if (LLVM_EXPORTED_SYMBOL_FILE)
584 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
585 endif()
586 endif()
587
588 if(ARG_SHARED AND UNIX)
589 if(NOT APPLE AND ARG_SONAME)
590 get_target_property(output_name ${name} OUTPUT_NAME)
591 if(${output_name} STREQUAL "output_name-NOTFOUND")
592 set(output_name ${name})
593 endif()
594 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
595 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
596 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
597 llvm_install_library_symlink(${api_name} ${library_name} SHARED
598 COMPONENT ${name}
599 ALWAYS_GENERATE)
600 llvm_install_library_symlink(${output_name} ${library_name} SHARED
601 COMPONENT ${name}
602 ALWAYS_GENERATE)
603 endif()
604 endif()
605
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200606 if(ARG_STATIC)
607 set(libtype PUBLIC)
608 else()
609 # We can use PRIVATE since SO knows its dependent libs.
610 set(libtype PRIVATE)
611 endif()
612
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100613 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
614 # On DLL platforms symbols are imported from the tool by linking against it.
615 set(llvm_libs ${ARG_PLUGIN_TOOL})
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200616 elseif (NOT ARG_COMPONENT_LIB)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100617 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
618 set(llvm_libs LLVM)
619 else()
620 llvm_map_components_to_libnames(llvm_libs
621 ${ARG_LINK_COMPONENTS}
622 ${LLVM_LINK_COMPONENTS}
623 )
624 endif()
625 else()
626 # Components have not been defined explicitly in CMake, so add the
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200627 # dependency information for this library through their name, and let
628 # LLVMBuildResolveComponentsLink resolve the mapping.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100629 #
630 # It would be nice to verify that we have the dependencies for this library
631 # name, but using get_property(... SET) doesn't suffice to determine if a
632 # property has been set to an empty value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200633 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100634
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200635 # These two properties are internal properties only used to make sure the
636 # link step applied in LLVMBuildResolveComponentsLink uses the same
637 # properties as the target_link_libraries call below.
638 set_property(TARGET ${name} PROPERTY LLVM_LINK_LIBS ${ARG_LINK_LIBS})
639 set_property(TARGET ${name} PROPERTY LLVM_LIBTYPE ${libtype})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100640 endif()
641
642 target_link_libraries(${name} ${libtype}
643 ${ARG_LINK_LIBS}
644 ${lib_deps}
645 ${llvm_libs}
646 )
647
648 if(LLVM_COMMON_DEPENDS)
649 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
650 # Add dependencies also to objlibs.
651 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
652 foreach(objlib ${objlibs})
653 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
654 endforeach()
655 endif()
656
657 if(ARG_SHARED OR ARG_MODULE)
658 llvm_externalize_debuginfo(${name})
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100659 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
660 endif()
661 # clang and newer versions of ninja use high-resolutions timestamps,
662 # but older versions of libtool on Darwin don't, so the archive will
663 # often get an older timestamp than the last object that was added
664 # or updated. To fix this, we add a custom command to touch archive
665 # after it's been built so that ninja won't rebuild it unnecessarily
666 # the next time it's run.
667 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
668 add_custom_command(TARGET ${name}
669 POST_BUILD
670 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
671 )
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100672 endif()
673endfunction()
674
675function(add_llvm_install_targets target)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200676 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100677 if(ARG_COMPONENT)
678 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
679 endif()
680 if(ARG_PREFIX)
681 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
682 endif()
683
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100684 set(file_dependencies)
685 set(target_dependencies)
686 foreach(dependency ${ARG_DEPENDS})
687 if(TARGET ${dependency})
688 list(APPEND target_dependencies ${dependency})
689 else()
690 list(APPEND file_dependencies ${dependency})
691 endif()
692 endforeach()
693
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100694 add_custom_target(${target}
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100695 DEPENDS ${file_dependencies}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100696 COMMAND "${CMAKE_COMMAND}"
697 ${component_option}
698 ${prefix_option}
699 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
700 USES_TERMINAL)
701 add_custom_target(${target}-stripped
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100702 DEPENDS ${file_dependencies}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100703 COMMAND "${CMAKE_COMMAND}"
704 ${component_option}
705 ${prefix_option}
706 -DCMAKE_INSTALL_DO_STRIP=1
707 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
708 USES_TERMINAL)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100709 if(target_dependencies)
710 add_dependencies(${target} ${target_dependencies})
711 add_dependencies(${target}-stripped ${target_dependencies})
712 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200713
714 if(ARG_SYMLINK)
715 add_dependencies(${target} install-${ARG_SYMLINK})
716 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped)
717 endif()
718endfunction()
719
720# Define special targets that behave like a component group. They don't have any
721# source attached but other components can add themselves to them. If the
722# component supports is a Target and it supports JIT compilation, HAS_JIT must
723# be passed. One can use ADD_TO_COMPONENT option from add_llvm_component_library
724# to link extra component into an existing group.
725function(add_llvm_component_group name)
726 cmake_parse_arguments(ARG "HAS_JIT" "" "LINK_COMPONENTS" ${ARGN})
727 add_custom_target(${name})
728 if(ARG_HAS_JIT)
729 set_property(TARGET ${name} PROPERTY COMPONENT_HAS_JIT ON)
730 endif()
731 if(ARG_LINK_COMPONENTS)
732 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
733 endif()
734endfunction()
735
736# An LLVM component is a cmake target with the following cmake properties
737# eventually set:
738# - LLVM_COMPONENT_NAME: the name of the component, which can be the name of
739# the associated library or the one specified through COMPONENT_NAME
740# - LLVM_LINK_COMPONENTS: a list of component this component depends on
741# - COMPONENT_HAS_JIT: (only for group component) whether this target group
742# supports JIT compilation
743# Additionnaly, the ADD_TO_COMPONENT <component> option make it possible to add this
744# component to the LLVM_LINK_COMPONENTS of <component>.
745function(add_llvm_component_library name)
746 cmake_parse_arguments(ARG
747 ""
748 "COMPONENT_NAME;ADD_TO_COMPONENT"
749 ""
750 ${ARGN})
751 add_llvm_library(${name} COMPONENT_LIB ${ARG_UNPARSED_ARGUMENTS})
752 string(REGEX REPLACE "^LLVM" "" component_name ${name})
753 set_property(TARGET ${name} PROPERTY LLVM_COMPONENT_NAME ${component_name})
754
755 if(ARG_COMPONENT_NAME)
756 set_property(GLOBAL PROPERTY LLVM_COMPONENT_NAME_${ARG_COMPONENT_NAME} ${component_name})
757 endif()
758
759 if(ARG_ADD_TO_COMPONENT)
760 set_property(TARGET ${ARG_ADD_TO_COMPONENT} APPEND PROPERTY LLVM_LINK_COMPONENTS ${component_name})
761 endif()
762
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100763endfunction()
764
765macro(add_llvm_library name)
766 cmake_parse_arguments(ARG
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200767 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100768 ""
769 ""
770 ${ARGN})
Andrew Walbran16937d02019-10-22 13:54:20 +0100771 if(ARG_MODULE)
772 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
773 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100774 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
775 else()
776 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
777 endif()
778
779 # Libraries that are meant to only be exposed via the build tree only are
780 # never installed and are only exported as a target in the special build tree
781 # config file.
Andrew Walbran16937d02019-10-22 13:54:20 +0100782 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100783 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100784 set(in_llvm_libs YES)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100785 endif()
786
Andrew Walbran16937d02019-10-22 13:54:20 +0100787 if (ARG_MODULE AND NOT TARGET ${name})
788 # Add empty "phony" target
789 add_custom_target(${name})
790 elseif( EXCLUDE_FROM_ALL )
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100791 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
792 elseif(ARG_BUILDTREE_ONLY)
793 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
794 else()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200795 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100796
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100797 set(export_to_llvmexports)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100798 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100799 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100800 NOT LLVM_DISTRIBUTION_COMPONENTS)
801 set(export_to_llvmexports EXPORT LLVMExports)
802 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
803 endif()
804
805 install(TARGETS ${name}
806 ${export_to_llvmexports}
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100807 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
808 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
809 RUNTIME DESTINATION bin COMPONENT ${name})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100810
Andrew Walbran16937d02019-10-22 13:54:20 +0100811 if (NOT LLVM_ENABLE_IDE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100812 add_llvm_install_targets(install-${name}
813 DEPENDS ${name}
814 COMPONENT ${name})
815 endif()
816 endif()
817 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
818 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +0100819 if (ARG_MODULE)
820 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
821 else()
822 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
823 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100824endmacro(add_llvm_library name)
825
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100826macro(add_llvm_executable name)
Andrew Walbran16937d02019-10-22 13:54:20 +0100827 cmake_parse_arguments(ARG
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200828 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS"
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100829 "ENTITLEMENTS;BUNDLE_PATH"
Andrew Walbran16937d02019-10-22 13:54:20 +0100830 "DEPENDS"
831 ${ARGN})
832
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100833 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
834
835 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
836
837 # Generate objlib
838 if(LLVM_ENABLE_OBJLIB)
839 # Generate an obj library for both targets.
840 set(obj_name "obj.${name}")
841 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
842 ${ALL_FILES}
843 )
844 llvm_update_compile_flags(${obj_name})
845 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
846
847 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
848 endif()
849
850 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
851
852 if(XCODE)
853 # Note: the dummy.cpp source file provides no definitions. However,
854 # it forces Xcode to properly link the static library.
855 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
856 endif()
857
858 if( EXCLUDE_FROM_ALL )
859 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
860 else()
861 add_executable(${name} ${ALL_FILES})
862 endif()
863
864 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
865
866 if(NOT ARG_NO_INSTALL_RPATH)
867 llvm_setup_rpath(${name})
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200868 elseif(NOT "${LLVM_LOCAL_RPATH}" STREQUAL "")
869 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set.
870 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
871 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
872 endif()
873
874 set_property(TARGET ${name} PROPERTY INSTALL_RPATH "${LLVM_LOCAL_RPATH}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100875 endif()
876
877 if(DEFINED windows_resource_file)
878 set_windows_version_resource_properties(${name} ${windows_resource_file})
879 endif()
880
881 # $<TARGET_OBJECTS> doesn't require compile flags.
882 if(NOT LLVM_ENABLE_OBJLIB)
883 llvm_update_compile_flags(${name})
884 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200885
886 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
887 set(LLVM_NO_DEAD_STRIP On)
888 endif()
889
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100890 add_link_opts( ${name} )
891
892 # Do not add -Dname_EXPORTS to the command-line when building files in this
893 # target. Doing so is actively harmful for the modules build because it
894 # creates extra module variants, and not useful because we don't use these
895 # macros.
896 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
897
898 if (LLVM_EXPORTED_SYMBOL_FILE)
899 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
900 endif(LLVM_EXPORTED_SYMBOL_FILE)
901
902 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
903 set(USE_SHARED USE_SHARED)
904 endif()
905
906 set(EXCLUDE_FROM_ALL OFF)
907 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
908 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
909 if( LLVM_COMMON_DEPENDS )
910 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
911 endif( LLVM_COMMON_DEPENDS )
912
913 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
914 llvm_externalize_debuginfo(${name})
915 endif()
916 if (LLVM_PTHREAD_LIB)
917 # libpthreads overrides some standard library symbols, so main
918 # executable must be linked with it in order to provide consistent
919 # API for all shared libaries loaded by this executable.
920 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
921 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100922
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100923 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100924endmacro(add_llvm_executable name)
925
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200926# add_llvm_pass_plugin(name [NO_MODULE] ...)
927# Add ${name} as an llvm plugin.
928# If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically.
929# Otherwise a pluggable shared library is registered.
930#
931# If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF,
932# only an object library is built, and no module is built. This is specific to the Polly use case.
933#
934# The SUBPROJECT argument contains the LLVM project the plugin belongs
935# to. If set, the plugin will link statically by default it if the
936# project was enabled.
937function(add_llvm_pass_plugin name)
938 cmake_parse_arguments(ARG
939 "NO_MODULE" "SUBPROJECT" ""
940 ${ARGN})
941
942 string(TOUPPER ${name} name_upper)
943
944 # Enable the plugin by default if it was explicitly enabled by the user.
945 # Note: If was set to "all", LLVM's CMakeLists.txt replaces it with a
946 # list of all projects, counting as explicitly enabled.
947 set(link_into_tools_default OFF)
948 if (ARG_SUBPROJECT AND LLVM_TOOL_${name_upper}_BUILD)
949 set(link_into_tools_default ON)
950 endif()
951 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" ${link_into_tools_default})
952
953 # If we statically link the plugin, don't use llvm dylib because we're going
954 # to be part of it.
955 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
956 list(APPEND ARG_UNPARSED_ARGUMENTS DISABLE_LLVM_LINK_LLVM_DYLIB)
957 endif()
958
959 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
960 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY)
961 # process_llvm_pass_plugins takes care of the actual linking, just create an
962 # object library as of now
963 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
964 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
965 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
966 if (TARGET intrinsics_gen)
967 add_dependencies(obj.${name} intrinsics_gen)
968 endif()
969 if (TARGET omp_gen)
970 add_dependencies(obj.${name} omp_gen)
971 endif()
972 if (TARGET acc_gen)
973 add_dependencies(obj.${name} acc_gen)
974 endif()
975 set_property(GLOBAL APPEND PROPERTY LLVM_STATIC_EXTENSIONS ${name})
976 elseif(NOT ARG_NO_MODULE)
977 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
978 else()
979 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
980 endif()
981 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
982
983endfunction(add_llvm_pass_plugin)
984
985# process_llvm_pass_plugins([GEN_CONFIG])
986#
987# Correctly set lib dependencies between plugins and tools, based on tools
988# registered with the ENABLE_PLUGINS option.
989#
990# if GEN_CONFIG option is set, also generate X Macro file for extension
991# handling. It provides a HANDLE_EXTENSION(extension_namespace, ExtensionProject)
992# call for each extension allowing client code to define
993# HANDLE_EXTENSION to have a specific code be run for each extension.
994#
995function(process_llvm_pass_plugins)
996 cmake_parse_arguments(ARG
997 "GEN_CONFIG" "" ""
998 ${ARGN})
999
1000 if(ARG_GEN_CONFIG)
1001 get_property(LLVM_STATIC_EXTENSIONS GLOBAL PROPERTY LLVM_STATIC_EXTENSIONS)
1002 else()
1003 include(LLVMConfigExtensions)
1004 endif()
1005
1006 # Add static plugins to the Extension component
1007 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1008 set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
1009 set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
1010 endforeach()
1011
1012 # Eventually generate the extension headers, and store config to a cmake file
1013 # for usage in third-party configuration.
1014 if(ARG_GEN_CONFIG)
1015
1016 ## Part 1: Extension header to be included whenever we need extension
1017 # processing.
1018 set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
1019 set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
1020 file(WRITE
1021 "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake"
1022 "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})")
1023 install(FILES
1024 ${llvm_cmake_builddir}/LLVMConfigExtensions.cmake
1025 DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
1026 COMPONENT cmake-exports)
1027
1028 set(ExtensionDef "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def")
1029 file(WRITE "${ExtensionDef}.tmp" "//extension handlers\n")
1030 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1031 file(APPEND "${ExtensionDef}.tmp" "HANDLE_EXTENSION(${llvm_extension})\n")
1032 endforeach()
1033 file(APPEND "${ExtensionDef}.tmp" "#undef HANDLE_EXTENSION\n")
1034
1035 # only replace if there's an actual change
1036 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1037 "${ExtensionDef}.tmp"
1038 "${ExtensionDef}")
1039 file(REMOVE "${ExtensionDef}.tmp")
1040
1041 ## Part 2: Extension header that captures each extension dependency, to be
1042 # used by llvm-config.
1043 set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc")
1044
1045 # Max needed to correctly size the required library array.
1046 set(llvm_plugin_max_deps_length 0)
1047 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1048 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1049 list(LENGTH llvm_plugin_deps llvm_plugin_deps_length)
1050 if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length)
1051 set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length})
1052 endif()
1053 endforeach()
1054
1055 list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count)
1056 file(WRITE
1057 "${ExtensionDeps}.tmp"
1058 "#include <array>\n\
1059 struct ExtensionDescriptor {\n\
1060 const char* Name;\n\
1061 const char* RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\
1062 };\n\
1063 std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n")
1064
1065 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1066 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1067
1068 file(APPEND "${ExtensionDeps}.tmp" "ExtensionDescriptor{\"${llvm_extension}\", {")
1069 foreach(llvm_plugin_dep ${llvm_plugin_deps})
1070 # Turn library dependency back to component name, if possible.
1071 # That way llvm-config can avoid redundant dependencies.
1072 STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep})
1073 STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name})
1074 if(is_llvm_library)
1075 STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name})
1076 STRING(TOLOWER ${plugin_dep_name} plugin_dep_name)
1077 endif()
1078 file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ")
1079 endforeach()
1080
1081 # Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions.
1082 file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n")
1083 endforeach()
1084 file(APPEND "${ExtensionDeps}.tmp" "};\n")
1085
1086 # only replace if there's an actual change
1087 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1088 "${ExtensionDeps}.tmp"
1089 "${ExtensionDeps}")
1090 file(REMOVE "${ExtensionDeps}.tmp")
1091 endif()
1092endfunction()
1093
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001094function(export_executable_symbols target)
1095 if (LLVM_EXPORTED_SYMBOL_FILE)
1096 # The symbol file should contain the symbols we want the executable to
1097 # export
1098 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1099 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1100 # Extract the symbols to export from the static libraries that the
1101 # executable links against.
1102 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1103 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
1104 # We need to consider not just the direct link dependencies, but also the
1105 # transitive link dependencies. Do this by starting with the set of direct
1106 # dependencies, then the dependencies of those dependencies, and so on.
1107 get_target_property(new_libs ${target} LINK_LIBRARIES)
1108 set(link_libs ${new_libs})
1109 while(NOT "${new_libs}" STREQUAL "")
1110 foreach(lib ${new_libs})
1111 if(TARGET ${lib})
1112 get_target_property(lib_type ${lib} TYPE)
1113 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
1114 list(APPEND static_libs ${lib})
1115 else()
1116 list(APPEND other_libs ${lib})
1117 endif()
1118 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
1119 foreach(transitive_lib ${transitive_libs})
1120 list(FIND link_libs ${transitive_lib} idx)
1121 if(TARGET ${transitive_lib} AND idx EQUAL -1)
1122 list(APPEND newer_libs ${transitive_lib})
1123 list(APPEND link_libs ${transitive_lib})
1124 endif()
1125 endforeach(transitive_lib)
1126 endif()
1127 endforeach(lib)
1128 set(new_libs ${newer_libs})
1129 set(newer_libs "")
1130 endwhile()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001131 list(REMOVE_DUPLICATES static_libs)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001132 if (MSVC)
1133 set(mangling microsoft)
1134 else()
1135 set(mangling itanium)
1136 endif()
1137 add_custom_command(OUTPUT ${exported_symbol_file}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001138 COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001139 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
1140 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
1141 VERBATIM
1142 COMMENT "Generating export list for ${target}")
1143 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
1144 # If something links against this executable then we want a
1145 # transitive link against only the libraries whose symbols
1146 # we aren't exporting.
1147 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
1148 # The default import library suffix that cmake uses for cygwin/mingw is
1149 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
1150 # where the import libraries of both get named libclang.dll.a. Use a suffix
1151 # of ".exe.a" to avoid this.
1152 if(CYGWIN OR MINGW)
1153 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
1154 endif()
1155 elseif(NOT (WIN32 OR CYGWIN))
1156 # On Windows auto-exporting everything doesn't work because of the limit on
1157 # the size of the exported symbol table, but on other platforms we can do
1158 # it without any trouble.
1159 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1160 if (APPLE)
1161 set_property(TARGET ${target} APPEND_STRING PROPERTY
1162 LINK_FLAGS " -rdynamic")
1163 endif()
1164 endif()
1165endfunction()
1166
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001167# Export symbols if LLVM plugins are enabled.
1168function(export_executable_symbols_for_plugins target)
1169 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1170 export_executable_symbols(${target})
1171 endif()
1172endfunction()
1173
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001174if(NOT LLVM_TOOLCHAIN_TOOLS)
1175 set (LLVM_TOOLCHAIN_TOOLS
1176 llvm-ar
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001177 llvm-cov
1178 llvm-cxxfilt
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001179 llvm-ranlib
1180 llvm-lib
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001181 llvm-nm
1182 llvm-objcopy
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001183 llvm-objdump
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001184 llvm-rc
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001185 llvm-size
1186 llvm-strings
1187 llvm-strip
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001188 llvm-profdata
1189 llvm-symbolizer
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001190 # symlink version of some of above tools that are enabled by
1191 # LLVM_INSTALL_BINUTILS_SYMLINKS.
1192 addr2line
1193 ar
1194 c++filt
1195 ranlib
1196 nm
1197 objcopy
1198 objdump
1199 size
1200 strings
1201 strip
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001202 )
1203endif()
1204
1205macro(add_llvm_tool name)
1206 if( NOT LLVM_BUILD_TOOLS )
1207 set(EXCLUDE_FROM_ALL ON)
1208 endif()
1209 add_llvm_executable(${name} ${ARGN})
1210
1211 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1212 if( LLVM_BUILD_TOOLS )
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001213 set(export_to_llvmexports)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001214 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
1215 NOT LLVM_DISTRIBUTION_COMPONENTS)
1216 set(export_to_llvmexports EXPORT LLVMExports)
1217 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
1218 endif()
1219
1220 install(TARGETS ${name}
1221 ${export_to_llvmexports}
1222 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
1223 COMPONENT ${name})
1224
Andrew Walbran16937d02019-10-22 13:54:20 +01001225 if (NOT LLVM_ENABLE_IDE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001226 add_llvm_install_targets(install-${name}
1227 DEPENDS ${name}
1228 COMPONENT ${name})
1229 endif()
1230 endif()
1231 endif()
1232 if( LLVM_BUILD_TOOLS )
1233 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1234 endif()
1235 set_target_properties(${name} PROPERTIES FOLDER "Tools")
1236endmacro(add_llvm_tool name)
1237
1238
1239macro(add_llvm_example name)
1240 if( NOT LLVM_BUILD_EXAMPLES )
1241 set(EXCLUDE_FROM_ALL ON)
1242 endif()
1243 add_llvm_executable(${name} ${ARGN})
1244 if( LLVM_BUILD_EXAMPLES )
1245 install(TARGETS ${name} RUNTIME DESTINATION examples)
1246 endif()
1247 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1248endmacro(add_llvm_example name)
1249
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001250macro(add_llvm_example_library name)
1251 if( NOT LLVM_BUILD_EXAMPLES )
1252 set(EXCLUDE_FROM_ALL ON)
1253 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN})
1254 else()
1255 add_llvm_library(${name} ${ARGN})
1256 endif()
1257
1258 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1259endmacro(add_llvm_example_library name)
1260
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001261# This is a macro that is used to create targets for executables that are needed
1262# for development, but that are not intended to be installed by default.
1263macro(add_llvm_utility name)
1264 if ( NOT LLVM_BUILD_UTILS )
1265 set(EXCLUDE_FROM_ALL ON)
1266 endif()
1267
1268 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
1269 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001270 if ( ${name} IN_LIST LLVM_TOOLCHAIN_UTILITIES OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Andrew Walbran16937d02019-10-22 13:54:20 +01001271 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001272 set(export_to_llvmexports)
Andrew Walbran16937d02019-10-22 13:54:20 +01001273 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
1274 NOT LLVM_DISTRIBUTION_COMPONENTS)
1275 set(export_to_llvmexports EXPORT LLVMExports)
1276 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
1277 endif()
1278
1279 install(TARGETS ${name}
1280 ${export_to_llvmexports}
1281 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
1282 COMPONENT ${name})
1283
1284 if (NOT LLVM_ENABLE_IDE)
1285 add_llvm_install_targets(install-${name}
1286 DEPENDS ${name}
1287 COMPONENT ${name})
1288 endif()
1289 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1290 elseif(LLVM_BUILD_UTILS)
1291 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001292 endif()
1293 endif()
1294endmacro(add_llvm_utility name)
1295
1296macro(add_llvm_fuzzer name)
1297 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
1298 if( LLVM_LIB_FUZZING_ENGINE )
1299 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1300 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1301 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
1302 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1303 elseif( LLVM_USE_SANITIZE_COVERAGE )
1304 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
1305 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1306 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1307 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1308 elseif( ARG_DUMMY_MAIN )
1309 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
1310 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1311 endif()
1312endmacro()
1313
1314macro(add_llvm_target target_name)
1315 include_directories(BEFORE
1316 ${CMAKE_CURRENT_BINARY_DIR}
1317 ${CMAKE_CURRENT_SOURCE_DIR})
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001318 add_llvm_component_library(LLVM${target_name} ${ARGN})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001319 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1320endmacro(add_llvm_target)
1321
1322function(canonicalize_tool_name name output)
1323 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1324 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1325 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1326 set(${output} "${nameUPPER}" PARENT_SCOPE)
1327endfunction(canonicalize_tool_name)
1328
1329# Custom add_subdirectory wrapper
1330# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1331# path if it differs from the name.
Andrew Walbran16937d02019-10-22 13:54:20 +01001332function(add_llvm_subdirectory project type name)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001333 set(add_llvm_external_dir "${ARGN}")
1334 if("${add_llvm_external_dir}" STREQUAL "")
1335 set(add_llvm_external_dir ${name})
1336 endif()
1337 canonicalize_tool_name(${name} nameUPPER)
Andrew Walbran16937d02019-10-22 13:54:20 +01001338 set(canonical_full_name ${project}_${type}_${nameUPPER})
1339 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1340 if(already_processed)
1341 return()
1342 endif()
1343 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1344
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001345 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1346 # Treat it as in-tree subproject.
Andrew Walbran16937d02019-10-22 13:54:20 +01001347 option(${canonical_full_name}_BUILD
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001348 "Whether to build ${name} as part of ${project}" On)
1349 mark_as_advanced(${project}_${type}_${name}_BUILD)
Andrew Walbran16937d02019-10-22 13:54:20 +01001350 if(${canonical_full_name}_BUILD)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001351 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001352 endif()
1353 else()
1354 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1355 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1356 CACHE PATH "Path to ${name} source directory")
Andrew Walbran16937d02019-10-22 13:54:20 +01001357 set(${canonical_full_name}_BUILD_DEFAULT ON)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001358 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
Andrew Walbran16937d02019-10-22 13:54:20 +01001359 set(${canonical_full_name}_BUILD_DEFAULT OFF)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001360 endif()
1361 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
Andrew Walbran16937d02019-10-22 13:54:20 +01001362 set(${canonical_full_name}_BUILD_DEFAULT OFF)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001363 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +01001364 option(${canonical_full_name}_BUILD
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001365 "Whether to build ${name} as part of LLVM"
Andrew Walbran16937d02019-10-22 13:54:20 +01001366 ${${canonical_full_name}_BUILD_DEFAULT})
1367 if (${canonical_full_name}_BUILD)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001368 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1369 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1370 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1371 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1372 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001373 endif()
1374 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +01001375endfunction()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001376
1377# Add external project that may want to be built as part of llvm such as Clang,
1378# lld, and Polly. This adds two options. One for the source directory of the
1379# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1380# enable or disable building it with everything else.
1381# Additional parameter can be specified as the name of directory.
1382macro(add_llvm_external_project name)
1383 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1384endmacro()
1385
1386macro(add_llvm_tool_subdirectory name)
1387 add_llvm_external_project(${name})
1388endmacro(add_llvm_tool_subdirectory)
1389
1390function(get_project_name_from_src_var var output)
1391 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1392 MACHED_TOOL "${var}")
1393 if(MACHED_TOOL)
1394 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1395 else()
1396 set(${output} PARENT_SCOPE)
1397 endif()
1398endfunction()
1399
1400function(create_subdirectory_options project type)
1401 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1402 foreach(dir ${sub-dirs})
1403 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1404 canonicalize_tool_name(${dir} name)
1405 option(${project}_${type}_${name}_BUILD
1406 "Whether to build ${name} as part of ${project}" On)
1407 mark_as_advanced(${project}_${type}_${name}_BUILD)
1408 endif()
1409 endforeach()
1410endfunction(create_subdirectory_options)
1411
1412function(create_llvm_tool_options)
1413 create_subdirectory_options(LLVM TOOL)
1414endfunction(create_llvm_tool_options)
1415
1416function(llvm_add_implicit_projects project)
1417 set(list_of_implicit_subdirs "")
1418 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1419 foreach(dir ${sub-dirs})
1420 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1421 canonicalize_tool_name(${dir} name)
1422 if (${project}_TOOL_${name}_BUILD)
1423 get_filename_component(fn "${dir}" NAME)
1424 list(APPEND list_of_implicit_subdirs "${fn}")
1425 endif()
1426 endif()
1427 endforeach()
1428
1429 foreach(external_proj ${list_of_implicit_subdirs})
1430 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1431 endforeach()
1432endfunction(llvm_add_implicit_projects)
1433
1434function(add_llvm_implicit_projects)
1435 llvm_add_implicit_projects(LLVM)
1436endfunction(add_llvm_implicit_projects)
1437
1438# Generic support for adding a unittest.
1439function(add_unittest test_suite test_name)
1440 if( NOT LLVM_BUILD_TESTS )
1441 set(EXCLUDE_FROM_ALL ON)
1442 endif()
1443
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001444 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1445 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1446 endif ()
1447 # Some parts of gtest rely on this GNU extension, don't warn on it.
1448 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1449 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1450 endif()
1451
1452 set(LLVM_REQUIRES_RTTI OFF)
1453
1454 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1455 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1456 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1457 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1458 # libpthreads overrides some standard library symbols, so main
1459 # executable must be linked with it in order to provide consistent
1460 # API for all shared libaries loaded by this executable.
1461 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1462
1463 add_dependencies(${test_suite} ${test_name})
1464 get_target_property(test_suite_folder ${test_suite} FOLDER)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001465 if (test_suite_folder)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001466 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1467 endif ()
1468endfunction()
1469
Andrew Scull0372a572018-11-16 15:47:06 +00001470# Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1471# is discouraged.
1472function(add_unittest_with_input_files test_suite test_name)
1473 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1474 configure_file(
1475 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1476 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1477
1478 add_unittest(${test_suite} ${test_name} ${ARGN})
1479endfunction()
1480
1481# Generic support for adding a benchmark.
1482function(add_benchmark benchmark_name)
1483 if( NOT LLVM_BUILD_BENCHMARKS )
1484 set(EXCLUDE_FROM_ALL ON)
1485 endif()
1486
1487 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1488 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1489 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1490 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1491 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1492endfunction()
1493
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001494# This function canonicalize the CMake variables passed by names
1495# from CMake boolean to 0/1 suitable for passing into Python or C++,
1496# in place.
1497function(llvm_canonicalize_cmake_booleans)
1498 foreach(var ${ARGN})
1499 if(${var})
1500 set(${var} 1 PARENT_SCOPE)
1501 else()
1502 set(${var} 0 PARENT_SCOPE)
1503 endif()
1504 endforeach()
1505endfunction(llvm_canonicalize_cmake_booleans)
1506
1507macro(set_llvm_build_mode)
1508 # Configuration-time: See Unit/lit.site.cfg.in
1509 if (CMAKE_CFG_INTDIR STREQUAL ".")
1510 set(LLVM_BUILD_MODE ".")
1511 else ()
1512 set(LLVM_BUILD_MODE "%(build_mode)s")
1513 endif ()
1514endmacro()
1515
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001516# Takes a list of path names in pathlist and a base directory, and returns
1517# a list of paths relative to the base directory in out_pathlist.
1518# Paths that are on a different drive than the basedir (on Windows) or that
1519# contain symlinks are returned absolute.
1520# Use with LLVM_LIT_PATH_FUNCTION below.
1521function(make_paths_relative out_pathlist basedir pathlist)
1522 # Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip
1523 # empty list entries. So escape the ;s in the list and do the splitting
1524 # ourselves. cmake has no relpath function, so use Python for that.
1525 string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}")
1526 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n
1527import os, sys\n
1528base = sys.argv[1]
1529def haslink(p):\n
1530 if not p or p == os.path.dirname(p): return False\n
1531 return os.path.islink(p) or haslink(os.path.dirname(p))\n
1532def relpath(p):\n
1533 if not p: return ''\n
1534 if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n
1535 if haslink(p) or haslink(base): return p\n
1536 return os.path.relpath(p, base)\n
1537if len(sys.argv) < 3: sys.exit(0)\n
1538sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))"
1539 ${basedir}
1540 ${pathlist_escaped}
1541 OUTPUT_VARIABLE pathlist_relative
1542 ERROR_VARIABLE error
1543 RESULT_VARIABLE result)
1544 if (NOT result EQUAL 0)
1545 message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}")
1546 endif()
1547 set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE)
1548endfunction()
1549
1550# Converts a file that's relative to the current python file to an absolute
1551# path. Since this uses __file__, it has to be emitted into python files that
1552# use it and can't be in a lit module. Use with make_paths_relative().
1553string(CONCAT LLVM_LIT_PATH_FUNCTION
1554 "# Allow generated file to be relocatable.\n"
1555 "def path(p):\n"
1556 " if not p: return ''\n"
1557 " return os.path.join(os.path.dirname(os.path.abspath(__file__)), p)\n"
1558 )
1559
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001560# This function provides an automatic way to 'configure'-like generate a file
1561# based on a set of common and custom variables, specifically targeting the
1562# variables needed for the 'lit.site.cfg' files. This function bundles the
1563# common variables that any Lit instance is likely to need, and custom
1564# variables can be passed in.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001565# The keyword PATHS is followed by a list of cmake variable names that are
1566# mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that
1567# list are treated as paths that are relative to the directory the generated
1568# lit.cfg.py file is in, and the `path()` function converts the relative
1569# path back to absolute form. This makes it possible to move a build directory
1570# containing lit.cfg.py files from one machine to another.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001571function(configure_lit_site_cfg site_in site_out)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001572 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001573
1574 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1575 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1576 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1577 endif()
1578 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1579 set(ARG_OUTPUT_MAPPING "${site_out}")
1580 endif()
1581
1582 foreach(c ${LLVM_TARGETS_TO_BUILD})
1583 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1584 endforeach(c)
1585 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1586
1587 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1588
1589 set_llvm_build_mode()
1590
1591 # They below might not be the build tree but provided binary tree.
1592 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1593 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1594 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1595 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1596
1597 # SHLIBDIR points the build tree.
1598 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1599
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001600 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1601 # plugins. We may rename it.
1602 if(LLVM_ENABLE_PLUGINS)
1603 set(ENABLE_SHARED "1")
1604 else()
1605 set(ENABLE_SHARED "0")
1606 endif()
1607
1608 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1609 set(ENABLE_ASSERTIONS "1")
1610 else()
1611 set(ENABLE_ASSERTIONS "0")
1612 endif()
1613
1614 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1615 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1616
1617 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1618 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1619 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1620
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001621 string(CONCAT LIT_SITE_CFG_IN_HEADER
1622 "# Autogenerated from ${site_in}\n# Do not edit!\n\n"
1623 "${LLVM_LIT_PATH_FUNCTION}"
1624 )
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001625
1626 # Override config_target_triple (and the env)
1627 if(LLVM_TARGET_TRIPLE_ENV)
1628 # This is expanded into the heading.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001629 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001630 "import os\n"
1631 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1632 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1633 )
1634
1635 # This is expanded to; config.target_triple = ""+config.target_triple+""
1636 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1637 endif()
1638
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001639 if (ARG_PATHS)
1640 # Walk ARG_PATHS and collect the current value of the variables in there.
1641 # list(APPEND) ignores empty elements exactly if the list is empty,
1642 # so start the list with a dummy element and drop it, to make sure that
1643 # even empty values make it into the values list.
1644 set(ARG_PATH_VALUES "dummy")
1645 foreach(path ${ARG_PATHS})
1646 list(APPEND ARG_PATH_VALUES "${${path}}")
1647 endforeach()
1648 list(REMOVE_AT ARG_PATH_VALUES 0)
1649
1650 get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY)
1651 make_paths_relative(
1652 ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}")
1653
1654 list(LENGTH ARG_PATHS len_paths)
1655 list(LENGTH ARG_PATH_VALUES len_path_values)
1656 list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels)
1657 if ((NOT ${len_paths} EQUAL ${len_path_values}) OR
1658 (NOT ${len_paths} EQUAL ${len_path_value_rels}))
1659 message(SEND_ERROR "PATHS lengths got confused")
1660 endif()
1661
1662 # Transform variables mentioned in ARG_PATHS to relative paths for
1663 # the configure_file() call. Variables are copied to subscopeds by cmake,
1664 # so this only modifies the local copy of the variables.
1665 math(EXPR arg_path_limit "${len_paths} - 1")
1666 foreach(i RANGE ${arg_path_limit})
1667 list(GET ARG_PATHS ${i} val1)
1668 list(GET ARG_PATH_VALUES_RELATIVE ${i} val2)
1669 set(${val1} ${val2})
1670 endforeach()
1671 endif()
1672
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001673 configure_file(${site_in} ${site_out} @ONLY)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001674
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001675 if (EXISTS "${ARG_MAIN_CONFIG}")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001676 # Remember main config / generated site config for llvm-lit.in.
1677 get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES)
1678 list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}")
1679 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001680 endif()
1681endfunction()
1682
1683function(dump_all_cmake_variables)
1684 get_cmake_property(_variableNames VARIABLES)
1685 foreach (_variableName ${_variableNames})
1686 message(STATUS "${_variableName}=${${_variableName}}")
1687 endforeach()
1688endfunction()
1689
1690function(get_llvm_lit_path base_dir file_name)
1691 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1692
1693 if (ARG_ALLOW_EXTERNAL)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001694 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1695 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1696 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1697 endif()
1698
1699 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1700 if (EXISTS ${LLVM_EXTERNAL_LIT})
1701 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1702 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1703 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1704 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1705 return()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001706 elseif (NOT DEFINED CACHE{LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE})
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001707 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001708 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001709 endif()
1710 endif()
1711 endif()
1712
1713 set(lit_file_name "llvm-lit")
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001714 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001715 # llvm-lit needs suffix.py for multiprocess to find a main module.
1716 set(lit_file_name "${lit_file_name}.py")
1717 endif ()
1718 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1719
1720 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1721 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1722 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1723 endif()
1724
1725 # Allow individual projects to provide an override
1726 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1727 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1728 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1729 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1730 else()
1731 set(LLVM_LIT_BASE_DIR "")
1732 endif()
1733
1734 # Cache this so we don't have to do it again and have subsequent calls
1735 # potentially disagree on the value.
1736 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1737 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1738endfunction()
1739
1740# A raw function to create a lit target. This is used to implement the testuite
1741# management functions.
1742function(add_lit_target target comment)
1743 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1744 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1745 separate_arguments(LIT_ARGS)
1746 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1747 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1748 endif ()
1749
1750 # Get the path to the lit to *run* tests with. This can be overriden by
1751 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1752 get_llvm_lit_path(
1753 lit_base_dir
1754 lit_file_name
1755 ALLOW_EXTERNAL
1756 )
1757
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001758 set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001759 list(APPEND LIT_COMMAND ${LIT_ARGS})
1760 foreach(param ${ARG_PARAMS})
1761 list(APPEND LIT_COMMAND --param ${param})
1762 endforeach()
1763 if (ARG_UNPARSED_ARGUMENTS)
1764 add_custom_target(${target}
1765 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1766 COMMENT "${comment}"
1767 USES_TERMINAL
1768 )
1769 else()
1770 add_custom_target(${target}
1771 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1772 message(STATUS "${target} does nothing.")
1773 endif()
Andrew Scull0372a572018-11-16 15:47:06 +00001774
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001775 if (ARG_DEPENDS)
1776 add_dependencies(${target} ${ARG_DEPENDS})
1777 endif()
1778
1779 # Tests should be excluded from "Build Solution".
1780 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1781endfunction()
1782
1783# A function to add a set of lit test suites to be driven through 'check-*' targets.
1784function(add_lit_testsuite target comment)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001785 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001786
1787 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001788 if(NOT ARG_EXCLUDE_FROM_CHECK_ALL)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001789 # Register the testsuites, params and depends for the global check rule.
1790 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1791 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1792 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1793 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1794 endif()
1795
1796 # Produce a specific suffixed check rule.
1797 add_lit_target(${target} ${comment}
1798 ${ARG_UNPARSED_ARGUMENTS}
1799 PARAMS ${ARG_PARAMS}
1800 DEPENDS ${ARG_DEPENDS}
1801 ARGS ${ARG_ARGS}
1802 )
1803endfunction()
1804
1805function(add_lit_testsuites project directory)
Andrew Walbran16937d02019-10-22 13:54:20 +01001806 if (NOT LLVM_ENABLE_IDE)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001807 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001808
1809 # Search recursively for test directories by assuming anything not
1810 # in a directory called Inputs contains tests.
1811 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1812 foreach(lit_suite ${to_process})
1813 if(NOT IS_DIRECTORY ${lit_suite})
1814 continue()
1815 endif()
1816 string(FIND ${lit_suite} Inputs is_inputs)
1817 string(FIND ${lit_suite} Output is_output)
1818 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1819 continue()
1820 endif()
1821
1822 # Create a check- target for the directory.
1823 string(REPLACE ${directory} "" name_slash ${lit_suite})
1824 if (name_slash)
1825 string(REPLACE "/" "-" name_slash ${name_slash})
1826 string(REPLACE "\\" "-" name_dashes ${name_slash})
1827 string(TOLOWER "${project}${name_dashes}" name_var)
1828 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1829 ${lit_suite}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001830 ${EXCLUDE_FROM_CHECK_ALL}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001831 PARAMS ${ARG_PARAMS}
1832 DEPENDS ${ARG_DEPENDS}
1833 ARGS ${ARG_ARGS}
1834 )
1835 endif()
1836 endforeach()
1837 endif()
1838endfunction()
1839
1840function(llvm_install_library_symlink name dest type)
1841 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1842 foreach(path ${CMAKE_MODULE_PATH})
1843 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1844 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1845 break()
1846 endif()
1847 endforeach()
1848
1849 set(component ${ARG_COMPONENT})
1850 if(NOT component)
1851 set(component ${name})
1852 endif()
1853
1854 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1855 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1856
1857 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1858 if(WIN32 AND "${type}" STREQUAL "SHARED")
1859 set(output_dir bin)
1860 endif()
1861
1862 install(SCRIPT ${INSTALL_SYMLINK}
1863 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1864 COMPONENT ${component})
1865
Andrew Walbran16937d02019-10-22 13:54:20 +01001866 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001867 add_llvm_install_targets(install-${name}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001868 DEPENDS ${name} ${dest}
1869 COMPONENT ${name}
1870 SYMLINK ${dest})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001871 endif()
1872endfunction()
1873
1874function(llvm_install_symlink name dest)
1875 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1876 foreach(path ${CMAKE_MODULE_PATH})
1877 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1878 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1879 break()
1880 endif()
1881 endforeach()
1882
1883 if(ARG_COMPONENT)
1884 set(component ${ARG_COMPONENT})
1885 else()
1886 if(ARG_ALWAYS_GENERATE)
1887 set(component ${dest})
1888 else()
1889 set(component ${name})
1890 endif()
1891 endif()
1892
1893 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1894 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1895
1896 install(SCRIPT ${INSTALL_SYMLINK}
1897 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1898 COMPONENT ${component})
1899
Andrew Walbran16937d02019-10-22 13:54:20 +01001900 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001901 add_llvm_install_targets(install-${name}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001902 DEPENDS ${name} ${dest}
1903 COMPONENT ${name}
1904 SYMLINK ${dest})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001905 endif()
1906endfunction()
1907
1908function(add_llvm_tool_symlink link_name target)
1909 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1910 set(dest_binary "$<TARGET_FILE:${target}>")
1911
1912 # This got a bit gross... For multi-configuration generators the target
1913 # properties return the resolved value of the string, not the build system
1914 # expression. To reconstruct the platform-agnostic path we have to do some
1915 # magic. First we grab one of the types, and a type-specific path. Then from
1916 # the type-specific path we find the last occurrence of the type in the path,
1917 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1918 # agnostic again.
1919 if(NOT ARG_OUTPUT_DIR)
1920 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1921 # the same directory.
1922 if(CMAKE_HOST_UNIX)
1923 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1924 endif()
1925 if(CMAKE_CONFIGURATION_TYPES)
1926 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1927 string(TOUPPER ${first_type} first_type_upper)
1928 set(first_type_suffix _${first_type_upper})
1929 endif()
1930 get_target_property(target_type ${target} TYPE)
1931 if(${target_type} STREQUAL "STATIC_LIBRARY")
1932 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1933 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1934 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1935 else()
1936 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1937 endif()
1938 if(CMAKE_CONFIGURATION_TYPES)
1939 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1940 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1941 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1942 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1943 path_suffix ${path_suffix})
1944 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1945 endif()
1946 endif()
1947
1948 if(CMAKE_HOST_UNIX)
1949 set(LLVM_LINK_OR_COPY create_symlink)
1950 else()
1951 set(LLVM_LINK_OR_COPY copy)
1952 endif()
1953
1954 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1955
1956 set(target_name ${link_name})
1957 if(TARGET ${link_name})
1958 set(target_name ${link_name}-link)
1959 endif()
1960
1961
1962 if(ARG_ALWAYS_GENERATE)
1963 set_property(DIRECTORY APPEND PROPERTY
1964 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1965 add_custom_command(TARGET ${target} POST_BUILD
1966 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1967 else()
1968 add_custom_command(OUTPUT ${output_path}
1969 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1970 DEPENDS ${target})
1971 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1972 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1973
1974 # Make sure both the link and target are toolchain tools
1975 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1976 set(TOOL_IS_TOOLCHAIN ON)
1977 endif()
1978
1979 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1980 llvm_install_symlink(${link_name} ${target})
1981 endif()
1982 endif()
1983endfunction()
1984
1985function(llvm_externalize_debuginfo name)
1986 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1987 return()
1988 endif()
1989
1990 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1991 if(APPLE)
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001992 if(NOT CMAKE_STRIP)
1993 set(CMAKE_STRIP xcrun strip)
1994 endif()
1995 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001996 else()
Andrew Scull0372a572018-11-16 15:47:06 +00001997 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001998 endif()
1999 endif()
2000
2001 if(APPLE)
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002002 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
2003 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
2004 else()
2005 set(file_ext dSYM)
2006 endif()
2007
2008 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
2009
2010 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
2011 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
2012 else()
2013 set(output_path "-o=${output_name}")
2014 endif()
2015
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002016 if(CMAKE_CXX_FLAGS MATCHES "-flto"
2017 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
2018
2019 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
2020 set_property(TARGET ${name} APPEND_STRING PROPERTY
2021 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
2022 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002023 if(NOT CMAKE_DSYMUTIL)
2024 set(CMAKE_DSYMUTIL xcrun dsymutil)
2025 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002026 add_custom_command(TARGET ${name} POST_BUILD
Andrew Walbran16937d02019-10-22 13:54:20 +01002027 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002028 ${strip_command}
2029 )
2030 else()
2031 add_custom_command(TARGET ${name} POST_BUILD
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002032 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002033 ${strip_command} -R .gnu_debuglink
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002034 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002035 )
2036 endif()
2037endfunction()
2038
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002039# Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002040function(llvm_codesign name)
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002041 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
Andrew Walbran16937d02019-10-22 13:54:20 +01002042
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002043 if(NOT LLVM_CODESIGNING_IDENTITY)
2044 return()
2045 endif()
2046
Andrew Walbran16937d02019-10-22 13:54:20 +01002047 if(CMAKE_GENERATOR STREQUAL "Xcode")
2048 set_target_properties(${name} PROPERTIES
2049 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
2050 )
2051 if(DEFINED ARG_ENTITLEMENTS)
2052 set_target_properties(${name} PROPERTIES
2053 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
2054 )
2055 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002056 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002057 if(NOT CMAKE_CODESIGN)
2058 set(CMAKE_CODESIGN xcrun codesign)
2059 endif()
2060 if(NOT CMAKE_CODESIGN_ALLOCATE)
2061 execute_process(
2062 COMMAND xcrun -f codesign_allocate
2063 OUTPUT_STRIP_TRAILING_WHITESPACE
2064 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
2065 )
2066 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +01002067 if(DEFINED ARG_ENTITLEMENTS)
2068 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
2069 endif()
2070
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002071 if (NOT ARG_BUNDLE_PATH)
2072 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
2073 endif()
2074
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002075 # ld64 now always codesigns the binaries it creates. Apply the force arg
2076 # unconditionally so that we can - for example - add entitlements to the
2077 # targets that need it.
2078 set(force_flag "-f")
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002079
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002080 add_custom_command(
2081 TARGET ${name} POST_BUILD
2082 COMMAND ${CMAKE_COMMAND} -E
2083 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
2084 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002085 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002086 )
2087 endif()
2088endfunction()
2089
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002090function(llvm_setup_rpath name)
2091 if(CMAKE_INSTALL_RPATH)
2092 return()
2093 endif()
2094
2095 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
2096 set(extra_libdir ${LLVM_LIBRARY_DIR})
2097 elseif(LLVM_BUILD_LIBRARY_DIR)
2098 set(extra_libdir ${LLVM_LIBRARY_DIR})
2099 endif()
2100
2101 if (APPLE)
2102 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002103 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002104 elseif(UNIX)
2105 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2106 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
2107 set_property(TARGET ${name} APPEND_STRING PROPERTY
2108 LINK_FLAGS " -Wl,-z,origin ")
2109 endif()
2110 if(LLVM_LINKER_IS_GNULD)
2111 # $ORIGIN is not interpreted at link time by ld.bfd
2112 set_property(TARGET ${name} APPEND_STRING PROPERTY
2113 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
2114 endif()
2115 else()
2116 return()
2117 endif()
2118
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002119 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set.
2120 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
2121 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
2122 endif()
2123
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002124 set_target_properties(${name} PROPERTIES
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002125 INSTALL_RPATH "${_install_rpath}"
2126 ${_install_name_dir})
2127endfunction()
2128
2129function(setup_dependency_debugging name)
2130 if(NOT LLVM_DEPENDENCY_DEBUGGING)
2131 return()
2132 endif()
2133
2134 if("intrinsics_gen" IN_LIST ARGN)
2135 return()
2136 endif()
2137
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002138 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
2139 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002140
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002141 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002142 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
2143endfunction()
2144
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002145# If the sources at the given `path` are under version control, set `out_var`
2146# to the the path of a file which will be modified when the VCS revision
2147# changes, attempting to create that file if it does not exist; if no such
2148# file exists and one cannot be created, instead set `out_var` to the
2149# empty string.
2150#
2151# If the sources are not under version control, do not define `out_var`.
Andrew Walbran16937d02019-10-22 13:54:20 +01002152function(find_first_existing_vc_file path out_var)
Andrew Walbran3d2c1972020-04-07 12:24:26 +01002153 if(NOT EXISTS "${path}")
2154 return()
2155 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002156 find_package(Git)
2157 if(GIT_FOUND)
2158 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
2159 WORKING_DIRECTORY ${path}
2160 RESULT_VARIABLE git_result
2161 OUTPUT_VARIABLE git_output
2162 ERROR_QUIET)
2163 if(git_result EQUAL 0)
2164 string(STRIP "${git_output}" git_output)
2165 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
2166 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
2167 if (NOT EXISTS "${git_dir}/logs/HEAD")
2168 execute_process(COMMAND ${CMAKE_COMMAND} -E touch HEAD
2169 WORKING_DIRECTORY "${git_dir}/logs"
2170 RESULT_VARIABLE touch_head_result
2171 ERROR_QUIET)
2172 if (NOT touch_head_result EQUAL 0)
2173 set(${out_var} "" PARENT_SCOPE)
2174 return()
Andrew Walbran16937d02019-10-22 13:54:20 +01002175 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +01002176 endif()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02002177 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002178 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +01002179 endif()
2180endfunction()