blob: 67c20a220a243d03653fa3a5c5219cea69f53ff3 [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
Chris Bieneman970555b2014-10-30 22:37:58 +000096 set(CAT "cat")
97 set(export_file_nativeslashes ${export_file})
Yaron Kerene6545932015-05-19 12:59:23 +000098 if(WIN32 AND NOT CYGWIN AND NOT MSYS)
Chris Bieneman970555b2014-10-30 22:37:58 +000099 set(CAT "type")
100 # Convert ${export_file} to native format (backslashes) for "type"
101 # Does not use file(TO_NATIVE_PATH) as it doesn't create a native
102 # path but a build-system specific format (see CMake bug
103 # http://public.kitware.com/Bug/print_bug_page.php?bug_id=5939 )
104 string(REPLACE / \\ export_file_nativeslashes ${export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000105 endif()
106
NAKAMURA Takumi81210752013-12-29 16:15:18 +0000107 add_custom_command(OUTPUT ${native_export_file}
108 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
Chris Bieneman970555b2014-10-30 22:37:58 +0000109 COMMAND ${CAT} ${export_file_nativeslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000110 DEPENDS ${export_file}
111 VERBATIM
112 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000113 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
114 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000115 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000116 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000117 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
118 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000119 endif()
120
121 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000122 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000123
124 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
125 foreach(src ${srcs})
126 get_filename_component(extension ${src} EXT)
127 if(extension STREQUAL ".cpp")
128 set(first_source_file ${src})
129 break()
130 endif()
131 endforeach()
132
133 # Force re-linking when the exports file changes. Actually, it
134 # forces recompilation of the source file. The LINK_DEPENDS target
135 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000136 # FIXME: This is not safe because this will create the same target
137 # ${native_export_file} in several different file:
138 # - One where we emitted ${target_name}_exports
139 # - One where we emitted the build command for the following object.
140 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
141 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000142
143 set_property(DIRECTORY APPEND
144 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
145
146 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000147
148 # Add dependency to *_exports later -- CMake issue 14747
149 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
150 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000151endfunction(add_llvm_symbol_exports)
152
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000153if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000154 execute_process(
155 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
156 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000157 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000158 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000159 if("${stdout}" MATCHES "GNU gold")
160 set(LLVM_LINKER_IS_GOLD ON)
161 endif()
162endif()
163
Rafael Espindola8cb78762014-11-02 12:14:22 +0000164function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000165 # Don't use linker optimizations in debug builds since it slows down the
166 # linker in a context where the optimizations are not important.
167 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000168
Rafael Espindola24766f92015-04-06 15:04:31 +0000169 # Pass -O3 to the linker. This enabled different optimizations on different
170 # linkers.
Rafael Espindola95fa3462015-06-22 12:41:52 +0000171 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000172 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000173 LINK_FLAGS " -Wl,-O3")
174 endif()
175
176 if(LLVM_LINKER_IS_GOLD)
177 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000178 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
179 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000180 # Note that there is a bug with -Wl,--icf=safe so it is not safe
181 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
182 endif()
183
184 if(NOT LLVM_NO_DEAD_STRIP)
185 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
186 # ld64's implementation of -dead_strip breaks tools that use plugins.
187 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
188 LINK_FLAGS " -Wl,-dead_strip")
189 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
190 # Object files are compiled with -ffunction-data-sections.
191 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
192 # tools that use plugins. Always pass --gc-sections once we require
193 # a newer linker.
194 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
195 LINK_FLAGS " -Wl,--gc-sections")
196 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000197 endif()
198 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000199endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000200
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000201# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
202# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
203# or a certain builder, for eaxample, msbuild.exe, would be confused.
204function(set_output_directory target bindir libdir)
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000205 # Do nothing if *_OUTPUT_INTDIR is empty.
206 if("${bindir}" STREQUAL "")
207 return()
208 endif()
209
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000210 # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
211 # It affects output of add_library(MODULE).
212 if(WIN32 OR CYGWIN)
213 # DLL platform
214 set(moddir ${bindir})
215 else()
216 set(moddir ${libdir})
217 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000218 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
219 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
220 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
221 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
222 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000223 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000224 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
225 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000226 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000227 endforeach()
228 else()
229 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
230 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000231 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000232 endif()
233endfunction()
234
Greg Bedwell95213c32015-06-12 15:58:29 +0000235# If on Windows and building with MSVC, add the resource script containing the
236# VERSIONINFO data to the project. This embeds version resource information
237# into the output .exe or .dll.
238# TODO: Enable for MinGW Windows builds too.
239#
240function(add_windows_version_resource_file OUT_VAR)
241 set(sources ${ARGN})
242 if (MSVC)
243 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000244 if(EXISTS ${resource_file})
245 set(sources ${sources} ${resource_file})
246 source_group("Resource Files" ${resource_file})
247 set(windows_resource_file ${resource_file} PARENT_SCOPE)
248 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000249 endif(MSVC)
250
251 set(${OUT_VAR} ${sources} PARENT_SCOPE)
252endfunction(add_windows_version_resource_file)
253
254# set_windows_version_resource_properties(name resource_file...
255# VERSION_MAJOR int
256# Optional major version number (defaults to LLVM_VERSION_MAJOR)
257# VERSION_MINOR int
258# Optional minor version number (defaults to LLVM_VERSION_MINOR)
259# VERSION_PATCHLEVEL int
260# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
261# VERSION_STRING
262# Optional version string (defaults to PACKAGE_VERSION)
263# PRODUCT_NAME
264# Optional product name string (defaults to "LLVM")
265# )
266function(set_windows_version_resource_properties name resource_file)
267 cmake_parse_arguments(ARG
268 ""
269 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
270 ""
271 ${ARGN})
272
273 if (NOT DEFINED ARG_VERSION_MAJOR)
274 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
275 endif()
276
277 if (NOT DEFINED ARG_VERSION_MINOR)
278 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
279 endif()
280
281 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
282 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
283 endif()
284
285 if (NOT DEFINED ARG_VERSION_STRING)
286 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
287 endif()
288
289 if (NOT DEFINED ARG_PRODUCT_NAME)
290 set(ARG_PRODUCT_NAME "LLVM")
291 endif()
292
293 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000294 PROPERTY COMPILE_FLAGS /nologo)
295 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000296 PROPERTY COMPILE_DEFINITIONS
297 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
298 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
299 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
300 "RC_VERSION_FIELD_4=0"
301 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
302 "RC_INTERNAL_NAME=\"${name}\""
303 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
304 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
305endfunction(set_windows_version_resource_properties)
306
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000307# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000308# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000309# STATIC by default w/o BUILD_SHARED_LIBS.
310# SHARED by default w/ BUILD_SHARED_LIBS.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000311# MODULE
312# Target ${name} might not be created on unsupported platforms.
313# Check with "if(TARGET ${name})".
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000314# OUTPUT_NAME name
315# Corresponds to OUTPUT_NAME in target properties.
316# DEPENDS targets...
317# Same semantics as add_dependencies().
318# LINK_COMPONENTS components...
319# Same as the variable LLVM_LINK_COMPONENTS.
320# LINK_LIBS lib_targets...
321# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000322# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000323# May specify header files for IDE generators.
324# )
325function(llvm_add_library name)
326 cmake_parse_arguments(ARG
327 "MODULE;SHARED;STATIC"
328 "OUTPUT_NAME"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000329 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000330 ${ARGN})
331 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000332 if(ARG_ADDITIONAL_HEADERS)
333 # Pass through ADDITIONAL_HEADERS.
334 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
335 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000336 if(ARG_OBJLIBS)
337 set(ALL_FILES ${ARG_OBJLIBS})
338 else()
339 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
340 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000341
342 if(ARG_MODULE)
343 if(ARG_SHARED OR ARG_STATIC)
344 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
345 endif()
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000346 if(NOT LLVM_ENABLE_PLUGINS)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000347 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
348 return()
349 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000350 else()
351 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
352 set(ARG_SHARED TRUE)
353 endif()
354 if(NOT ARG_SHARED)
355 set(ARG_STATIC TRUE)
356 endif()
357 endif()
358
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000359 # Generate objlib
360 if(ARG_SHARED AND ARG_STATIC)
361 # Generate an obj library for both targets.
362 set(obj_name "obj.${name}")
363 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
364 ${ALL_FILES}
365 )
366 llvm_update_compile_flags(${obj_name})
367 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
368
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000369 # Do add_dependencies(obj) later due to CMake issue 14747.
370 list(APPEND objlibs ${obj_name})
371
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000372 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
373 endif()
374
375 if(ARG_SHARED AND ARG_STATIC)
376 # static
377 set(name_static "${name}_static")
378 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000379 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000380 endif()
381 # DEPENDS has been appended to LLVM_COMMON_LIBS.
382 llvm_add_library(${name_static} STATIC
383 ${output_name}
384 OBJLIBS ${ALL_FILES} # objlib
385 LINK_LIBS ${ARG_LINK_LIBS}
386 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
387 )
388 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
389 set(ARG_STATIC)
390 endif()
391
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000392 if(ARG_MODULE)
393 add_library(${name} MODULE ${ALL_FILES})
394 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000395 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000396 add_library(${name} SHARED ${ALL_FILES})
397 else()
398 add_library(${name} STATIC ${ALL_FILES})
399 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000400
401 if(DEFINED windows_resource_file)
402 set_windows_version_resource_properties(${name} ${windows_resource_file})
403 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
404 endif()
405
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000406 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000407 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000408 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000409 if(ARG_OUTPUT_NAME)
410 set_target_properties(${name}
411 PROPERTIES
412 OUTPUT_NAME ${ARG_OUTPUT_NAME}
413 )
414 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000415
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000416 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000417 set_target_properties(${name} PROPERTIES
418 PREFIX ""
419 SUFFIX ${LLVM_PLUGIN_EXT}
420 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000421 endif()
422
423 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000424 if(WIN32)
425 set_target_properties(${name} PROPERTIES
426 PREFIX ""
427 )
428 endif()
Reid Kleckner2e3d1e02015-02-27 18:22:46 +0000429
Chris Bienemana8a05962015-02-18 18:52:06 +0000430 set_target_properties(${name}
431 PROPERTIES
Chris Bienemanbf2e90a2015-03-02 17:50:13 +0000432 SOVERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}
Chris Bienemana8a05962015-02-18 18:52:06 +0000433 VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000434 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000435
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000436 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000437 # Do not add -Dname_EXPORTS to the command-line when building files in this
438 # target. Doing so is actively harmful for the modules build because it
439 # creates extra module variants, and not useful because we don't use these
440 # macros.
441 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
442
Nico Weber06473f82013-12-29 05:39:01 +0000443 if (LLVM_EXPORTED_SYMBOL_FILE)
444 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
445 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000446 endif()
447
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000448 # Add the explicit dependency information for this library.
449 #
450 # It would be nice to verify that we have the dependencies for this library
451 # name, but using get_property(... SET) doesn't suffice to determine if a
452 # property has been set to an empty value.
453 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
454
455 llvm_map_components_to_libnames(llvm_libs
456 ${ARG_LINK_COMPONENTS}
457 ${LLVM_LINK_COMPONENTS}
458 )
459
460 if(CMAKE_VERSION VERSION_LESS 2.8.12)
461 # Link libs w/o keywords, assuming PUBLIC.
462 target_link_libraries(${name}
463 ${ARG_LINK_LIBS}
464 ${lib_deps}
465 ${llvm_libs}
466 )
467 elseif(ARG_STATIC)
468 target_link_libraries(${name} INTERFACE
469 ${ARG_LINK_LIBS}
470 ${lib_deps}
471 ${llvm_libs}
472 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000473 else()
Rafael Espindolaf9dcf902014-11-07 15:33:56 +0000474 # We can use PRIVATE since SO knows its dependent libs.
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000475 target_link_libraries(${name} PRIVATE
476 ${ARG_LINK_LIBS}
477 ${lib_deps}
478 ${llvm_libs}
479 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000480 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000481
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000482 if(LLVM_COMMON_DEPENDS)
483 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000484 # Add dependencies also to objlibs.
485 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
486 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000487 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
488 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000489 endif()
490endfunction()
491
492macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000493 cmake_parse_arguments(ARG
494 "SHARED"
495 ""
496 ""
497 ${ARGN})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000498 if( BUILD_SHARED_LIBS )
499 llvm_add_library(${name} SHARED ${ARGN})
500 else()
501 llvm_add_library(${name} ${ARGN})
502 endif()
503 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
504
Nick Lewycky61aed872011-04-29 02:12:06 +0000505 if( EXCLUDE_FROM_ALL )
506 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
507 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000508 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000509 if(ARG_SHARED OR BUILD_SHARED_LIBS)
510 if(WIN32 OR CYGWIN)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000511 set(install_type RUNTIME)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000512 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000513 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000514 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000515 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000516 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000517 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000518
Chris Bienemana985e6b2015-04-16 18:08:33 +0000519 install(TARGETS ${name}
520 EXPORT LLVMExports
521 ${install_type} DESTINATION lib${LLVM_LIBDIR_SUFFIX}
522 COMPONENT ${name})
523
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000524 if (NOT CMAKE_CONFIGURATION_TYPES)
525 add_custom_target(install-${name}
526 DEPENDS ${name}
527 COMMAND "${CMAKE_COMMAND}"
528 -DCMAKE_INSTALL_COMPONENT=${name}
529 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
530 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000531 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000532 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000533 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000534 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000535endmacro(add_llvm_library name)
536
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000537macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000538 llvm_add_library(${name} MODULE ${ARGN})
539 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000540 # Add empty "phony" target
541 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000542 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000543 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000544 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000545 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000546 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000547 if(WIN32 OR CYGWIN)
548 # DLL platform
549 set(dlldir "bin")
550 else()
551 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
552 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000553 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000554 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000555 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000556 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
557 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000558 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000559 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000560 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000561
562 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000563endmacro(add_llvm_loadable_module name)
564
565
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000566macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000567 llvm_process_sources( ALL_FILES ${ARGN} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000568 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
569
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000570 if( EXCLUDE_FROM_ALL )
571 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
572 else()
573 add_executable(${name} ${ALL_FILES})
574 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000575
576 if(DEFINED windows_resource_file)
577 set_windows_version_resource_properties(${name} ${windows_resource_file})
578 endif()
579
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000580 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000581 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000582
Richard Smith2b91a7f2014-09-26 22:40:15 +0000583 # Do not add -Dname_EXPORTS to the command-line when building files in this
584 # target. Doing so is actively harmful for the modules build because it
585 # creates extra module variants, and not useful because we don't use these
586 # macros.
587 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
588
Nico Weberc27118d2013-12-28 23:31:44 +0000589 if (LLVM_EXPORTED_SYMBOL_FILE)
590 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
591 endif(LLVM_EXPORTED_SYMBOL_FILE)
592
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000593 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000594 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000595 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000596 if( LLVM_COMMON_DEPENDS )
597 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
598 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000599endmacro(add_llvm_executable name)
600
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000601function(export_executable_symbols target)
602 if (NOT MSVC) # MSVC's linker doesn't support exporting all symbols.
603 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
604 endif()
605endfunction()
606
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000607
Hans Wennborg16546272013-08-24 00:20:36 +0000608set (LLVM_TOOLCHAIN_TOOLS
609 llvm-ar
610 llvm-objdump
611 )
612
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000613macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000614 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000615 set(EXCLUDE_FROM_ALL ON)
616 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000617 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000618
619 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
620 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
621 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000622 install(TARGETS ${name}
623 EXPORT LLVMExports
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000624 RUNTIME DESTINATION bin
625 COMPONENT ${name})
626
627 if (NOT CMAKE_CONFIGURATION_TYPES)
628 add_custom_target(install-${name}
629 DEPENDS ${name}
630 COMMAND "${CMAKE_COMMAND}"
631 -DCMAKE_INSTALL_COMPONENT=${name}
632 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
633 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000634 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000635 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000636 if( LLVM_BUILD_TOOLS )
637 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
638 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000639 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000640endmacro(add_llvm_tool name)
641
642
643macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000644 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000645 set(EXCLUDE_FROM_ALL ON)
646 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000647 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000648 if( LLVM_BUILD_EXAMPLES )
649 install(TARGETS ${name} RUNTIME DESTINATION examples)
650 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000651 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000652endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000653
654
Oscar Fuentes3145e922011-02-20 22:06:10 +0000655macro(add_llvm_utility name)
656 add_llvm_executable(${name} ${ARGN})
657 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Derek Schuffef9928e2015-03-27 16:53:06 +0000658 if( LLVM_INSTALL_UTILS )
659 install (TARGETS ${name}
660 RUNTIME DESTINATION bin
661 COMPONENT ${name})
662 if (NOT CMAKE_CONFIGURATION_TYPES)
663 add_custom_target(install-${name}
664 DEPENDS ${name}
665 COMMAND "${CMAKE_COMMAND}"
666 -DCMAKE_INSTALL_COMPONENT=${name}
667 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
668 endif()
669 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000670endmacro(add_llvm_utility name)
671
672
Oscar Fuentescdc95492008-09-26 04:40:32 +0000673macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000674 include_directories(BEFORE
675 ${CMAKE_CURRENT_BINARY_DIR}
676 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000677 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000678 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000679endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000680
681# Add external project that may want to be built as part of llvm such as Clang,
682# lld, and Polly. This adds two options. One for the source directory of the
683# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000684# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000685# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000686macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000687 set(add_llvm_external_dir "${ARGN}")
688 if("${add_llvm_external_dir}" STREQUAL "")
689 set(add_llvm_external_dir ${name})
690 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000691 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000692 string(REPLACE "-" "_" nameUNDERSCORE ${name})
693 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
694 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000695 CACHE PATH "Path to ${name} source directory")
696 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
697 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
698 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
699 "Whether to build ${name} as part of LLVM" ON)
700 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000701 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000702 endif()
703 endif()
704endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000705
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000706macro(add_llvm_tool_subdirectory name)
707 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
708 add_subdirectory(${name})
709endmacro(add_llvm_tool_subdirectory)
710
711macro(ignore_llvm_tool_subdirectory name)
712 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
713endmacro(ignore_llvm_tool_subdirectory)
714
715function(add_llvm_implicit_external_projects)
716 set(list_of_implicit_subdirs "")
717 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
718 foreach(dir ${sub-dirs})
719 if(IS_DIRECTORY "${dir}")
720 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
721 if( tool_subdir_ignore EQUAL -1
722 AND EXISTS "${dir}/CMakeLists.txt")
723 get_filename_component(fn "${dir}" NAME)
724 list(APPEND list_of_implicit_subdirs "${fn}")
725 endif()
726 endif()
727 endforeach()
728
729 foreach(external_proj ${list_of_implicit_subdirs})
730 add_llvm_external_project("${external_proj}")
731 endforeach()
732endfunction(add_llvm_implicit_external_projects)
733
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000734# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000735function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000736 if( NOT LLVM_BUILD_TESTS )
737 set(EXCLUDE_FROM_ALL ON)
738 endif()
739
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000740 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
741 if (NOT LLVM_ENABLE_THREADS)
742 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
743 endif ()
744
745 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000746 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000747 endif ()
748
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000749 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000750
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000751 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000752 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
753 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000754 target_link_libraries(${test_name}
755 gtest
756 gtest_main
757 LLVMSupport # gtest needs it for raw_ostream.
758 )
759
760 add_dependencies(${test_suite} ${test_name})
761 get_target_property(test_suite_folder ${test_suite} FOLDER)
762 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
763 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
764 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000765endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000766
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000767function(llvm_add_go_executable binary pkgpath)
768 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
769
770 if(LLVM_BINDINGS MATCHES "go")
771 # FIXME: This should depend only on the libraries Go needs.
772 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
773 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
774 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
775 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +0000776 set(cppflags "")
777 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
778 foreach(d ${include_dirs})
779 set(cppflags "${cppflags} -I${d}")
780 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000781 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
782 add_custom_command(OUTPUT ${binpath}
Chandler Carruthcab512d2014-12-29 22:50:30 +0000783 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000784 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
785 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
786 ${llvmlibs} ${ARG_DEPENDS}
787 COMMENT "Building Go executable ${binary}"
788 VERBATIM)
789 if (ARG_ALL)
790 add_custom_target(${binary} ALL DEPENDS ${binpath})
791 else()
792 add_custom_target(${binary} DEPENDS ${binpath})
793 endif()
794 endif()
795endfunction()
796
Chandler Carruth3511dd32012-06-28 06:36:24 +0000797# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000798# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000799# variables needed for the 'lit.site.cfg' files. This function bundles the
800# common variables that any Lit instance is likely to need, and custom
801# variables can be passed in.
802function(configure_lit_site_cfg input output)
803 foreach(c ${LLVM_TARGETS_TO_BUILD})
804 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
805 endforeach(c)
806 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
807
808 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000809
Chandler Carruth3511dd32012-06-28 06:36:24 +0000810 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000811 if (CMAKE_CFG_INTDIR STREQUAL ".")
812 set(LLVM_BUILD_MODE ".")
813 else ()
814 set(LLVM_BUILD_MODE "%(build_mode)s")
815 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000816
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000817 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000818 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
819 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000820 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
821 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000822
823 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +0000824 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000825
Chandler Carruth3511dd32012-06-28 06:36:24 +0000826 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000827 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
828 # plugins. We may rename it.
829 if(LLVM_ENABLE_PLUGINS)
830 set(ENABLE_SHARED "1")
831 else()
832 set(ENABLE_SHARED "0")
833 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000834
835 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
836 set(ENABLE_ASSERTIONS "1")
837 else()
838 set(ENABLE_ASSERTIONS "0")
839 endif()
840
Kuba Breckaa62bbcc2015-01-24 01:42:44 +0000841 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +0000842 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000843
Peter Collingbournece800842014-10-17 18:32:36 +0000844 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
845 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +0000846 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +0000847
Chandler Carruth3511dd32012-06-28 06:36:24 +0000848 configure_file(${input} ${output} @ONLY)
849endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000850
851# A raw function to create a lit target. This is used to implement the testuite
852# management functions.
853function(add_lit_target target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000854 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000855 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
856 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000857 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
858 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
859 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +0000860 if (LLVM_MAIN_SRC_DIR)
861 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
862 else()
863 find_program(LIT_COMMAND llvm-lit)
864 endif ()
865 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000866 foreach(param ${ARG_PARAMS})
867 list(APPEND LIT_COMMAND --param ${param})
868 endforeach()
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000869 if (ARG_UNPARSED_ARGUMENTS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000870 add_custom_target(${target}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000871 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000872 COMMENT "${comment}"
Peter Collingbournea7fb5cf2014-11-17 22:16:15 +0000873 ${cmake_3_2_USES_TERMINAL}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000874 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000875 else()
876 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000877 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000878 message(STATUS "${target} does nothing.")
879 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +0000880 if (ARG_DEPENDS)
881 add_dependencies(${target} ${ARG_DEPENDS})
882 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000883
884 # Tests should be excluded from "Build Solution".
885 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000886endfunction()
887
888# A function to add a set of lit test suites to be driven through 'check-*' targets.
889function(add_lit_testsuite target comment)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000890 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000891
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000892 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
893 if(NOT EXCLUDE_FROM_ALL)
894 # Register the testsuites, params and depends for the global check rule.
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000895 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000896 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
897 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
898 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
899 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000900
901 # Produce a specific suffixed check rule.
902 add_lit_target(${target} ${comment}
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000903 ${ARG_UNPARSED_ARGUMENTS}
Chandler Carruth69ce6652012-06-30 10:14:14 +0000904 PARAMS ${ARG_PARAMS}
905 DEPENDS ${ARG_DEPENDS}
906 ARGS ${ARG_ARGS}
907 )
908endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000909
910function(add_lit_testsuites project directory)
911 if (NOT CMAKE_CONFIGURATION_TYPES)
Filipe Cabecinhasc1b849a2015-06-19 03:45:40 +0000912 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000913 file(GLOB_RECURSE litCfg ${directory}/lit*.cfg)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000914 set(lit_suites)
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000915 foreach(f ${litCfg})
916 get_filename_component(dir ${f} DIRECTORY)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000917 set(lit_suites ${lit_suites} ${dir})
918 endforeach()
919 list(REMOVE_DUPLICATES lit_suites)
920 foreach(dir ${lit_suites})
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000921 string(REPLACE ${directory} "" name_slash ${dir})
922 if (name_slash)
923 string(REPLACE "/" "-" name_slash ${name_slash})
924 string(REPLACE "\\" "-" name_dashes ${name_slash})
925 string(TOLOWER "${project}${name_dashes}" name_var)
926 add_lit_target("check-${name_var}" "Running lit suite ${dir}"
927 ${dir}
928 PARAMS ${ARG_PARAMS}
929 DEPENDS ${ARG_DEPENDS}
930 ARGS ${ARG_ARGS}
931 )
932 endif()
933 endforeach()
934 endif()
935endfunction()