blob: 6ab580827aa6faced19405a6877b550d3a7db261 [file] [log] [blame]
Oscar Fuentes751ea9d2008-11-14 22:06:14 +00001include(LLVMProcessSources)
Oscar Fuentesd8a6dd62011-04-05 17:02:48 +00002include(LLVM-Config)
Oscar Fuentesa229b3c2008-09-22 01:08:49 +00003
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00004function(llvm_update_compile_flags name)
NAKAMURA Takumib524c222014-01-28 11:40:04 +00005 get_property(sources TARGET ${name} PROPERTY SOURCES)
6 if("${sources}" MATCHES "\\.c(;|$)")
NAKAMURA Takumia679f432014-01-28 09:44:06 +00007 set(update_src_props ON)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00008 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +00009
Dan Liewa5bdc842014-07-22 15:41:18 +000010 # LLVM_REQUIRES_EH is an internal flag that individual
11 # targets can use to force EH
Zachary Turner66bc9082015-02-25 20:42:19 +000012 if((LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) AND NOT CLANG_CL)
Dan Liewa5bdc842014-07-22 15:41:18 +000013 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
14 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
15 set(LLVM_REQUIRES_RTTI ON)
16 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000017 else()
18 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000019 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000020 elseif(MSVC)
21 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000022 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000023 endif()
Zachary Turner66bc9082015-02-25 20:42:19 +000024 if (CLANG_CL)
25 # FIXME: Remove this once clang-cl supports SEH
26 list(APPEND LLVM_COMPILE_DEFINITIONS "GTEST_HAS_SEH=0")
27 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000028 endif()
29
Dan Liewa5bdc842014-07-22 15:41:18 +000030 # LLVM_REQUIRES_RTTI is an internal flag that individual
31 # targets can use to force RTTI
32 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000033 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
34 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000035 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000036 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000037 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000038 endif ()
39 endif()
40
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000041 # Assume that;
42 # - LLVM_COMPILE_FLAGS is list.
43 # - PROPERTY COMPILE_FLAGS is string.
44 string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
45
NAKAMURA Takumia679f432014-01-28 09:44:06 +000046 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000047 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000048 get_filename_component(suf ${fn} EXT)
49 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000050 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
51 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000052 endif()
53 endforeach()
54 else()
55 # Update target props, since all sources are C++.
56 set_property(TARGET ${name} APPEND_STRING PROPERTY
57 COMPILE_FLAGS "${target_compile_flags}")
58 endif()
59
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000060 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
61endfunction()
62
Nico Weberc27118d2013-12-28 23:31:44 +000063function(add_llvm_symbol_exports target_name export_file)
64 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000065 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000066 add_custom_command(OUTPUT ${native_export_file}
67 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000068 DEPENDS ${export_file}
69 VERBATIM
70 COMMENT "Creating export file for ${target_name}")
71 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000072 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000073 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
74 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000075 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000076 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000077 add_custom_command(OUTPUT ${native_export_file}
78 COMMAND echo "{" > ${native_export_file}
79 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
80 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
81 COMMAND echo " local: *;" >> ${native_export_file}
82 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000083 DEPENDS ${export_file}
84 VERBATIM
85 COMMENT "Creating export file for ${target_name}")
Rafael Espindolaec1d0692015-06-22 12:34:54 +000086 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
87 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
88 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
89 else()
90 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
91 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
92 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000093 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000094 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000095
NAKAMURA Takumi81210752013-12-29 16:15:18 +000096 add_custom_command(OUTPUT ${native_export_file}
NAKAMURA Takumi3eb06662015-07-05 08:56:38 +000097 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
98 < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000099 DEPENDS ${export_file}
100 VERBATIM
101 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000102 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
103 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000104 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000105 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000106 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
107 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000108 endif()
109
110 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000111 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000112
113 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
114 foreach(src ${srcs})
115 get_filename_component(extension ${src} EXT)
116 if(extension STREQUAL ".cpp")
117 set(first_source_file ${src})
118 break()
119 endif()
120 endforeach()
121
122 # Force re-linking when the exports file changes. Actually, it
123 # forces recompilation of the source file. The LINK_DEPENDS target
124 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000125 # FIXME: This is not safe because this will create the same target
126 # ${native_export_file} in several different file:
127 # - One where we emitted ${target_name}_exports
128 # - One where we emitted the build command for the following object.
129 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
130 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000131
132 set_property(DIRECTORY APPEND
133 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
134
135 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000136
137 # Add dependency to *_exports later -- CMake issue 14747
138 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
139 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000140endfunction(add_llvm_symbol_exports)
141
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000142if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000143 execute_process(
144 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
145 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000146 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000147 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000148 if("${stdout}" MATCHES "GNU gold")
149 set(LLVM_LINKER_IS_GOLD ON)
150 endif()
151endif()
152
Rafael Espindola8cb78762014-11-02 12:14:22 +0000153function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000154 # Don't use linker optimizations in debug builds since it slows down the
155 # linker in a context where the optimizations are not important.
156 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000157
Rafael Espindola24766f92015-04-06 15:04:31 +0000158 # Pass -O3 to the linker. This enabled different optimizations on different
159 # linkers.
Rafael Espindola95fa3462015-06-22 12:41:52 +0000160 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000161 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000162 LINK_FLAGS " -Wl,-O3")
163 endif()
164
165 if(LLVM_LINKER_IS_GOLD)
166 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000167 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
168 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000169 # Note that there is a bug with -Wl,--icf=safe so it is not safe
170 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
171 endif()
172
173 if(NOT LLVM_NO_DEAD_STRIP)
174 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
175 # ld64's implementation of -dead_strip breaks tools that use plugins.
176 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
177 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindolac32ed572015-06-22 15:06:17 +0000178 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
179 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
180 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000181 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
182 # Object files are compiled with -ffunction-data-sections.
183 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
184 # tools that use plugins. Always pass --gc-sections once we require
185 # a newer linker.
186 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
187 LINK_FLAGS " -Wl,--gc-sections")
188 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000189 endif()
190 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000191endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000192
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000193# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
194# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
195# or a certain builder, for eaxample, msbuild.exe, would be confused.
196function(set_output_directory target bindir libdir)
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000197 # Do nothing if *_OUTPUT_INTDIR is empty.
198 if("${bindir}" STREQUAL "")
199 return()
200 endif()
201
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000202 # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
203 # It affects output of add_library(MODULE).
204 if(WIN32 OR CYGWIN)
205 # DLL platform
206 set(moddir ${bindir})
207 else()
208 set(moddir ${libdir})
209 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000210 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
211 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
212 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
213 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
214 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000215 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000216 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
217 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000218 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000219 endforeach()
220 else()
221 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
222 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000223 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000224 endif()
225endfunction()
226
Greg Bedwell95213c32015-06-12 15:58:29 +0000227# If on Windows and building with MSVC, add the resource script containing the
228# VERSIONINFO data to the project. This embeds version resource information
229# into the output .exe or .dll.
230# TODO: Enable for MinGW Windows builds too.
231#
232function(add_windows_version_resource_file OUT_VAR)
233 set(sources ${ARGN})
234 if (MSVC)
235 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000236 if(EXISTS ${resource_file})
237 set(sources ${sources} ${resource_file})
238 source_group("Resource Files" ${resource_file})
239 set(windows_resource_file ${resource_file} PARENT_SCOPE)
240 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000241 endif(MSVC)
242
243 set(${OUT_VAR} ${sources} PARENT_SCOPE)
244endfunction(add_windows_version_resource_file)
245
246# set_windows_version_resource_properties(name resource_file...
247# VERSION_MAJOR int
248# Optional major version number (defaults to LLVM_VERSION_MAJOR)
249# VERSION_MINOR int
250# Optional minor version number (defaults to LLVM_VERSION_MINOR)
251# VERSION_PATCHLEVEL int
252# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
253# VERSION_STRING
254# Optional version string (defaults to PACKAGE_VERSION)
255# PRODUCT_NAME
256# Optional product name string (defaults to "LLVM")
257# )
258function(set_windows_version_resource_properties name resource_file)
259 cmake_parse_arguments(ARG
260 ""
261 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
262 ""
263 ${ARGN})
264
265 if (NOT DEFINED ARG_VERSION_MAJOR)
266 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
267 endif()
268
269 if (NOT DEFINED ARG_VERSION_MINOR)
270 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
271 endif()
272
273 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
274 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
275 endif()
276
277 if (NOT DEFINED ARG_VERSION_STRING)
278 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
279 endif()
280
281 if (NOT DEFINED ARG_PRODUCT_NAME)
282 set(ARG_PRODUCT_NAME "LLVM")
283 endif()
284
285 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000286 PROPERTY COMPILE_FLAGS /nologo)
287 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000288 PROPERTY COMPILE_DEFINITIONS
289 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
290 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
291 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
292 "RC_VERSION_FIELD_4=0"
293 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
294 "RC_INTERNAL_NAME=\"${name}\""
295 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
296 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
297endfunction(set_windows_version_resource_properties)
298
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000299# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000300# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000301# STATIC by default w/o BUILD_SHARED_LIBS.
302# SHARED by default w/ BUILD_SHARED_LIBS.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000303# MODULE
304# Target ${name} might not be created on unsupported platforms.
305# Check with "if(TARGET ${name})".
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000306# OUTPUT_NAME name
307# Corresponds to OUTPUT_NAME in target properties.
308# DEPENDS targets...
309# Same semantics as add_dependencies().
310# LINK_COMPONENTS components...
311# Same as the variable LLVM_LINK_COMPONENTS.
312# LINK_LIBS lib_targets...
313# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000314# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000315# May specify header files for IDE generators.
316# )
317function(llvm_add_library name)
318 cmake_parse_arguments(ARG
319 "MODULE;SHARED;STATIC"
320 "OUTPUT_NAME"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000321 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000322 ${ARGN})
323 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000324 if(ARG_ADDITIONAL_HEADERS)
325 # Pass through ADDITIONAL_HEADERS.
326 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
327 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000328 if(ARG_OBJLIBS)
329 set(ALL_FILES ${ARG_OBJLIBS})
330 else()
331 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
332 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000333
334 if(ARG_MODULE)
335 if(ARG_SHARED OR ARG_STATIC)
336 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
337 endif()
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000338 if(NOT LLVM_ENABLE_PLUGINS)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000339 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
340 return()
341 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000342 else()
343 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
344 set(ARG_SHARED TRUE)
345 endif()
346 if(NOT ARG_SHARED)
347 set(ARG_STATIC TRUE)
348 endif()
349 endif()
350
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000351 # Generate objlib
352 if(ARG_SHARED AND ARG_STATIC)
353 # Generate an obj library for both targets.
354 set(obj_name "obj.${name}")
355 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
356 ${ALL_FILES}
357 )
358 llvm_update_compile_flags(${obj_name})
359 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
360
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000361 # Do add_dependencies(obj) later due to CMake issue 14747.
362 list(APPEND objlibs ${obj_name})
363
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000364 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
365 endif()
366
367 if(ARG_SHARED AND ARG_STATIC)
368 # static
369 set(name_static "${name}_static")
370 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000371 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000372 endif()
373 # DEPENDS has been appended to LLVM_COMMON_LIBS.
374 llvm_add_library(${name_static} STATIC
375 ${output_name}
376 OBJLIBS ${ALL_FILES} # objlib
377 LINK_LIBS ${ARG_LINK_LIBS}
378 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
379 )
380 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
381 set(ARG_STATIC)
382 endif()
383
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000384 if(ARG_MODULE)
385 add_library(${name} MODULE ${ALL_FILES})
386 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000387 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000388 add_library(${name} SHARED ${ALL_FILES})
389 else()
390 add_library(${name} STATIC ${ALL_FILES})
391 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000392
393 if(DEFINED windows_resource_file)
394 set_windows_version_resource_properties(${name} ${windows_resource_file})
395 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
396 endif()
397
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000398 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000399 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000400 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000401 if(ARG_OUTPUT_NAME)
402 set_target_properties(${name}
403 PROPERTIES
404 OUTPUT_NAME ${ARG_OUTPUT_NAME}
405 )
406 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000407
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000408 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000409 set_target_properties(${name} PROPERTIES
410 PREFIX ""
411 SUFFIX ${LLVM_PLUGIN_EXT}
412 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000413 endif()
414
415 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000416 if(WIN32)
417 set_target_properties(${name} PROPERTIES
418 PREFIX ""
419 )
420 endif()
Reid Kleckner2e3d1e02015-02-27 18:22:46 +0000421
Chris Bienemana8a05962015-02-18 18:52:06 +0000422 set_target_properties(${name}
423 PROPERTIES
Chris Bienemanbf2e90a2015-03-02 17:50:13 +0000424 SOVERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}
Chris Bienemana8a05962015-02-18 18:52:06 +0000425 VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000426 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000427
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000428 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000429 # Do not add -Dname_EXPORTS to the command-line when building files in this
430 # target. Doing so is actively harmful for the modules build because it
431 # creates extra module variants, and not useful because we don't use these
432 # macros.
433 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
434
Nico Weber06473f82013-12-29 05:39:01 +0000435 if (LLVM_EXPORTED_SYMBOL_FILE)
436 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
437 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000438 endif()
439
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000440 # Add the explicit dependency information for this library.
441 #
442 # It would be nice to verify that we have the dependencies for this library
443 # name, but using get_property(... SET) doesn't suffice to determine if a
444 # property has been set to an empty value.
445 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
446
447 llvm_map_components_to_libnames(llvm_libs
448 ${ARG_LINK_COMPONENTS}
449 ${LLVM_LINK_COMPONENTS}
450 )
451
452 if(CMAKE_VERSION VERSION_LESS 2.8.12)
453 # Link libs w/o keywords, assuming PUBLIC.
454 target_link_libraries(${name}
455 ${ARG_LINK_LIBS}
456 ${lib_deps}
457 ${llvm_libs}
458 )
459 elseif(ARG_STATIC)
460 target_link_libraries(${name} INTERFACE
461 ${ARG_LINK_LIBS}
462 ${lib_deps}
463 ${llvm_libs}
464 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000465 else()
Rafael Espindolaf9dcf902014-11-07 15:33:56 +0000466 # We can use PRIVATE since SO knows its dependent libs.
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000467 target_link_libraries(${name} PRIVATE
468 ${ARG_LINK_LIBS}
469 ${lib_deps}
470 ${llvm_libs}
471 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000472 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000473
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000474 if(LLVM_COMMON_DEPENDS)
475 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000476 # Add dependencies also to objlibs.
477 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
478 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000479 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
480 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000481 endif()
482endfunction()
483
484macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000485 cmake_parse_arguments(ARG
486 "SHARED"
487 ""
488 ""
489 ${ARGN})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000490 if( BUILD_SHARED_LIBS )
491 llvm_add_library(${name} SHARED ${ARGN})
492 else()
493 llvm_add_library(${name} ${ARGN})
494 endif()
Dan Liew80189d22015-06-29 18:45:56 +0000495 # The gtest libraries should not be installed or exported as a target
496 if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
497 set(_is_gtest TRUE)
498 else()
499 set(_is_gtest FALSE)
500 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
501 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000502
Nick Lewycky61aed872011-04-29 02:12:06 +0000503 if( EXCLUDE_FROM_ALL )
504 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Dan Liew80189d22015-06-29 18:45:56 +0000505 elseif(NOT _is_gtest)
Hans Wennborg16546272013-08-24 00:20:36 +0000506 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000507 if(ARG_SHARED OR BUILD_SHARED_LIBS)
508 if(WIN32 OR CYGWIN)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000509 set(install_type RUNTIME)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000510 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000511 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000512 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000513 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000514 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000515 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000516
Chris Bienemana985e6b2015-04-16 18:08:33 +0000517 install(TARGETS ${name}
518 EXPORT LLVMExports
519 ${install_type} DESTINATION lib${LLVM_LIBDIR_SUFFIX}
520 COMPONENT ${name})
521
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000522 if (NOT CMAKE_CONFIGURATION_TYPES)
523 add_custom_target(install-${name}
524 DEPENDS ${name}
525 COMMAND "${CMAKE_COMMAND}"
526 -DCMAKE_INSTALL_COMPONENT=${name}
527 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
528 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000529 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000530 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000531 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000532 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000533endmacro(add_llvm_library name)
534
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000535macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000536 llvm_add_library(${name} MODULE ${ARGN})
537 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000538 # Add empty "phony" target
539 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000540 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000541 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000542 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000543 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000544 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000545 if(WIN32 OR CYGWIN)
546 # DLL platform
547 set(dlldir "bin")
548 else()
549 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
550 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000551 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000552 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000553 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000554 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
555 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000556 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000557 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000558 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000559
560 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000561endmacro(add_llvm_loadable_module name)
562
563
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000564macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000565 llvm_process_sources( ALL_FILES ${ARGN} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000566 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
567
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000568 if( EXCLUDE_FROM_ALL )
569 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
570 else()
571 add_executable(${name} ${ALL_FILES})
572 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000573
574 if(DEFINED windows_resource_file)
575 set_windows_version_resource_properties(${name} ${windows_resource_file})
576 endif()
577
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000578 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000579 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000580
Richard Smith2b91a7f2014-09-26 22:40:15 +0000581 # Do not add -Dname_EXPORTS to the command-line when building files in this
582 # target. Doing so is actively harmful for the modules build because it
583 # creates extra module variants, and not useful because we don't use these
584 # macros.
585 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
586
Nico Weberc27118d2013-12-28 23:31:44 +0000587 if (LLVM_EXPORTED_SYMBOL_FILE)
588 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
589 endif(LLVM_EXPORTED_SYMBOL_FILE)
590
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000591 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000592 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000593 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000594 if( LLVM_COMMON_DEPENDS )
595 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
596 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000597endmacro(add_llvm_executable name)
598
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000599function(export_executable_symbols target)
600 if (NOT MSVC) # MSVC's linker doesn't support exporting all symbols.
601 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
602 endif()
603endfunction()
604
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000605
Hans Wennborg16546272013-08-24 00:20:36 +0000606set (LLVM_TOOLCHAIN_TOOLS
607 llvm-ar
608 llvm-objdump
609 )
610
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000611macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000612 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000613 set(EXCLUDE_FROM_ALL ON)
614 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000615 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000616
617 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
618 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
619 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000620 install(TARGETS ${name}
621 EXPORT LLVMExports
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000622 RUNTIME DESTINATION bin
623 COMPONENT ${name})
624
625 if (NOT CMAKE_CONFIGURATION_TYPES)
626 add_custom_target(install-${name}
627 DEPENDS ${name}
628 COMMAND "${CMAKE_COMMAND}"
629 -DCMAKE_INSTALL_COMPONENT=${name}
630 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
631 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000632 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000633 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000634 if( LLVM_BUILD_TOOLS )
635 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
636 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000637 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000638endmacro(add_llvm_tool name)
639
640
641macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000642 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000643 set(EXCLUDE_FROM_ALL ON)
644 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000645 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000646 if( LLVM_BUILD_EXAMPLES )
647 install(TARGETS ${name} RUNTIME DESTINATION examples)
648 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000649 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000650endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000651
652
Oscar Fuentes3145e922011-02-20 22:06:10 +0000653macro(add_llvm_utility name)
654 add_llvm_executable(${name} ${ARGN})
655 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Derek Schuffef9928e2015-03-27 16:53:06 +0000656 if( LLVM_INSTALL_UTILS )
657 install (TARGETS ${name}
658 RUNTIME DESTINATION bin
659 COMPONENT ${name})
660 if (NOT CMAKE_CONFIGURATION_TYPES)
661 add_custom_target(install-${name}
662 DEPENDS ${name}
663 COMMAND "${CMAKE_COMMAND}"
664 -DCMAKE_INSTALL_COMPONENT=${name}
665 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
666 endif()
667 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000668endmacro(add_llvm_utility name)
669
670
Oscar Fuentescdc95492008-09-26 04:40:32 +0000671macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000672 include_directories(BEFORE
673 ${CMAKE_CURRENT_BINARY_DIR}
674 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000675 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000676 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000677endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000678
Chris Bieneman580d8152015-07-20 20:36:06 +0000679function(canonicalize_tool_name name output)
680 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
681 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
682 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
683 set(${output} "${nameUPPER}" PARENT_SCOPE)
684endfunction(canonicalize_tool_name)
685
Michael J. Spencere734f542012-04-26 19:43:35 +0000686# Add external project that may want to be built as part of llvm such as Clang,
687# lld, and Polly. This adds two options. One for the source directory of the
688# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000689# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000690# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000691macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000692 set(add_llvm_external_dir "${ARGN}")
693 if("${add_llvm_external_dir}" STREQUAL "")
694 set(add_llvm_external_dir ${name})
695 endif()
Chris Bieneman580d8152015-07-20 20:36:06 +0000696 canonicalize_tool_name(${name} nameUPPER)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000697 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
698 # Treat it as in-tree subproject.
Chris Bieneman077f3fe2015-07-21 00:44:47 +0000699 option(LLVM_TOOL_${nameUPPER}_BUILD
Chris Bieneman95ba62c2015-07-21 00:39:53 +0000700 "Whether to build ${name} as part of LLVM" On)
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000701 mark_as_advanced(LLVM_TOOL_${name}_BUILD)
702 if(LLVM_TOOL_${nameUPPER}_BUILD)
703 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
704 # Don't process it in add_llvm_implicit_projects().
705 set(LLVM_TOOL_${nameUPPER}_BUILD OFF)
706 endif()
707 else()
708 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
709 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
710 CACHE PATH "Path to ${name} source directory")
711 set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT ON)
712 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
713 set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT OFF)
714 endif()
715 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
716 set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT OFF)
717 endif()
718 option(LLVM_TOOL_${nameUPPER}_BUILD
719 "Whether to build ${name} as part of LLVM"
720 ${LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT})
721 if (LLVM_TOOL_${nameUPPER}_BUILD)
NAKAMURA Takumi58843ee2015-07-08 02:35:43 +0000722 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
NAKAMURA Takumi04e2da52015-08-22 04:53:52 +0000723 # FIXME: It'd be redundant.
Chris Bieneman580d8152015-07-20 20:36:06 +0000724 set(LLVM_TOOL_${nameUPPER}_BUILD Off)
Michael J. Spencere734f542012-04-26 19:43:35 +0000725 endif()
726 endif()
727endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000728
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000729macro(add_llvm_tool_subdirectory name)
Chris Bieneman580d8152015-07-20 20:36:06 +0000730 add_llvm_external_project(${name})
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000731endmacro(add_llvm_tool_subdirectory)
732
Chris Bieneman580d8152015-07-20 20:36:06 +0000733function(get_project_name_from_src_var var output)
734 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
735 MACHED_TOOL "${var}")
736 if(MACHED_TOOL)
737 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
738 else()
739 set(${output} PARENT_SCOPE)
740 endif()
741endfunction()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000742
Chris Bieneman580d8152015-07-20 20:36:06 +0000743function(create_llvm_tool_options)
744 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
745 foreach(dir ${sub-dirs})
746 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
747 canonicalize_tool_name(${dir} name)
748 option(LLVM_TOOL_${name}_BUILD
749 "Whether to build ${name} as part of LLVM" On)
750 mark_as_advanced(LLVM_TOOL_${name}_BUILD)
751 endif()
752 endforeach()
Chris Bieneman580d8152015-07-20 20:36:06 +0000753endfunction(create_llvm_tool_options)
754
755function(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000756 set(list_of_implicit_subdirs "")
757 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
758 foreach(dir ${sub-dirs})
Chris Bieneman580d8152015-07-20 20:36:06 +0000759 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
760 canonicalize_tool_name(${dir} name)
761 if (LLVM_TOOL_${name}_BUILD)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000762 get_filename_component(fn "${dir}" NAME)
763 list(APPEND list_of_implicit_subdirs "${fn}")
764 endif()
765 endif()
766 endforeach()
767
768 foreach(external_proj ${list_of_implicit_subdirs})
769 add_llvm_external_project("${external_proj}")
770 endforeach()
Chris Bieneman580d8152015-07-20 20:36:06 +0000771endfunction(add_llvm_implicit_projects)
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000772
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000773# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000774function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000775 if( NOT LLVM_BUILD_TESTS )
776 set(EXCLUDE_FROM_ALL ON)
777 endif()
778
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000779 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
780 if (NOT LLVM_ENABLE_THREADS)
781 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
782 endif ()
783
784 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000785 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000786 endif ()
787
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000788 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000789
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000790 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000791 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
792 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000793 target_link_libraries(${test_name}
794 gtest
795 gtest_main
796 LLVMSupport # gtest needs it for raw_ostream.
797 )
798
799 add_dependencies(${test_suite} ${test_name})
800 get_target_property(test_suite_folder ${test_suite} FOLDER)
801 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
802 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
803 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000804endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000805
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000806function(llvm_add_go_executable binary pkgpath)
807 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
808
809 if(LLVM_BINDINGS MATCHES "go")
810 # FIXME: This should depend only on the libraries Go needs.
811 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
812 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
813 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
814 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +0000815 set(cppflags "")
816 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
817 foreach(d ${include_dirs})
818 set(cppflags "${cppflags} -I${d}")
819 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000820 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
821 add_custom_command(OUTPUT ${binpath}
Andrew Wilkins6c1bfa92015-07-21 00:46:23 +0000822 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000823 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
824 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
825 ${llvmlibs} ${ARG_DEPENDS}
826 COMMENT "Building Go executable ${binary}"
827 VERBATIM)
828 if (ARG_ALL)
829 add_custom_target(${binary} ALL DEPENDS ${binpath})
830 else()
831 add_custom_target(${binary} DEPENDS ${binpath})
832 endif()
833 endif()
834endfunction()
835
Chandler Carruth3511dd32012-06-28 06:36:24 +0000836# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000837# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000838# variables needed for the 'lit.site.cfg' files. This function bundles the
839# common variables that any Lit instance is likely to need, and custom
840# variables can be passed in.
841function(configure_lit_site_cfg input output)
842 foreach(c ${LLVM_TARGETS_TO_BUILD})
843 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
844 endforeach(c)
845 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
846
847 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000848
Chandler Carruth3511dd32012-06-28 06:36:24 +0000849 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000850 if (CMAKE_CFG_INTDIR STREQUAL ".")
851 set(LLVM_BUILD_MODE ".")
852 else ()
853 set(LLVM_BUILD_MODE "%(build_mode)s")
854 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000855
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000856 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000857 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
858 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000859 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
860 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000861
862 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +0000863 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000864
Chandler Carruth3511dd32012-06-28 06:36:24 +0000865 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000866 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
867 # plugins. We may rename it.
868 if(LLVM_ENABLE_PLUGINS)
869 set(ENABLE_SHARED "1")
870 else()
871 set(ENABLE_SHARED "0")
872 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000873
874 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
875 set(ENABLE_ASSERTIONS "1")
876 else()
877 set(ENABLE_ASSERTIONS "0")
878 endif()
879
Kuba Breckaa62bbcc2015-01-24 01:42:44 +0000880 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +0000881 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000882
Peter Collingbournece800842014-10-17 18:32:36 +0000883 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
884 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +0000885 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +0000886
Chandler Carruth3511dd32012-06-28 06:36:24 +0000887 configure_file(${input} ${output} @ONLY)
888endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000889
890# A raw function to create a lit target. This is used to implement the testuite
891# management functions.
892function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000893 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000894 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
895 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000896 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
897 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
898 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +0000899 if (LLVM_MAIN_SRC_DIR)
900 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
901 else()
902 find_program(LIT_COMMAND llvm-lit)
903 endif ()
904 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000905 foreach(param ${ARG_PARAMS})
906 list(APPEND LIT_COMMAND --param ${param})
907 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000908 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000909 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000910 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000911 COMMENT "${comment}"
Peter Collingbournea7fb5cf2014-11-17 22:16:15 +0000912 ${cmake_3_2_USES_TERMINAL}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000913 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000914 else()
915 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000916 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000917 message(STATUS "${target} does nothing.")
918 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +0000919 if (ARG_DEPENDS)
920 add_dependencies(${target} ${ARG_DEPENDS})
921 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000922
923 # Tests should be excluded from "Build Solution".
924 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000925endfunction()
926
927# A function to add a set of lit test suites to be driven through 'check-*' targets.
928function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000929 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000930
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000931 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
932 if(NOT EXCLUDE_FROM_ALL)
933 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000934 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000935 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
936 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
937 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
938 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000939
940 # Produce a specific suffixed check rule.
941 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000942 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +0000943 PARAMS ${ARG_PARAMS}
944 DEPENDS ${ARG_DEPENDS}
945 ARGS ${ARG_ARGS}
946 )
947endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000948
949function(add_lit_testsuites project directory)
950 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000951 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000952 file(GLOB_RECURSE litCfg ${directory}/lit*.cfg)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000953 set(lit_suites)
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000954 foreach(f ${litCfg})
955 get_filename_component(dir ${f} DIRECTORY)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000956 set(lit_suites ${lit_suites} ${dir})
957 endforeach()
958 list(REMOVE_DUPLICATES lit_suites)
959 foreach(dir ${lit_suites})
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000960 string(REPLACE ${directory} "" name_slash ${dir})
961 if (name_slash)
962 string(REPLACE "/" "-" name_slash ${name_slash})
963 string(REPLACE "\\" "-" name_dashes ${name_slash})
964 string(TOLOWER "${project}${name_dashes}" name_var)
965 add_lit_target("check-${name_var}" "Running lit suite ${dir}"
966 ${dir}
967 PARAMS ${ARG_PARAMS}
968 DEPENDS ${ARG_DEPENDS}
969 ARGS ${ARG_ARGS}
970 )
971 endif()
972 endforeach()
973 endif()
974endfunction()