blob: ba1476118eac3566ae99d8c7059b1743d81540b2 [file] [log] [blame]
Chandler Carruth69ce6652012-06-30 10:14:14 +00001include(LLVMParseArguments)
Oscar Fuentes751ea9d2008-11-14 22:06:14 +00002include(LLVMProcessSources)
Oscar Fuentesd8a6dd62011-04-05 17:02:48 +00003include(LLVM-Config)
Oscar Fuentesa229b3c2008-09-22 01:08:49 +00004
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00005function(llvm_update_compile_flags name)
NAKAMURA Takumib524c222014-01-28 11:40:04 +00006 get_property(sources TARGET ${name} PROPERTY SOURCES)
7 if("${sources}" MATCHES "\\.c(;|$)")
NAKAMURA Takumia679f432014-01-28 09:44:06 +00008 set(update_src_props ON)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00009 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000010
Dan Liewa5bdc842014-07-22 15:41:18 +000011 # LLVM_REQUIRES_EH is an internal flag that individual
12 # targets can use to force EH
Zachary Turner66bc9082015-02-25 20:42:19 +000013 if((LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) AND NOT CLANG_CL)
Dan Liewa5bdc842014-07-22 15:41:18 +000014 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
15 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
16 set(LLVM_REQUIRES_RTTI ON)
17 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000018 else()
19 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000020 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000021 elseif(MSVC)
22 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000023 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000024 endif()
Zachary Turner66bc9082015-02-25 20:42:19 +000025 if (CLANG_CL)
26 # FIXME: Remove this once clang-cl supports SEH
27 list(APPEND LLVM_COMPILE_DEFINITIONS "GTEST_HAS_SEH=0")
28 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000029 endif()
30
Dan Liewa5bdc842014-07-22 15:41:18 +000031 # LLVM_REQUIRES_RTTI is an internal flag that individual
32 # targets can use to force RTTI
33 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000034 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
35 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000036 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000037 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000038 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000039 endif ()
40 endif()
41
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000042 # Assume that;
43 # - LLVM_COMPILE_FLAGS is list.
44 # - PROPERTY COMPILE_FLAGS is string.
45 string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
46
NAKAMURA Takumia679f432014-01-28 09:44:06 +000047 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000048 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000049 get_filename_component(suf ${fn} EXT)
50 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000051 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
52 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000053 endif()
54 endforeach()
55 else()
56 # Update target props, since all sources are C++.
57 set_property(TARGET ${name} APPEND_STRING PROPERTY
58 COMPILE_FLAGS "${target_compile_flags}")
59 endif()
60
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000061 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
62endfunction()
63
Nico Weberc27118d2013-12-28 23:31:44 +000064function(add_llvm_symbol_exports target_name export_file)
65 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000066 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000067 add_custom_command(OUTPUT ${native_export_file}
68 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000069 DEPENDS ${export_file}
70 VERBATIM
71 COMMENT "Creating export file for ${target_name}")
72 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000073 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000074 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
75 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000076 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000077 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000078 add_custom_command(OUTPUT ${native_export_file}
79 COMMAND echo "{" > ${native_export_file}
80 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
81 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
82 COMMAND echo " local: *;" >> ${native_export_file}
83 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000084 DEPENDS ${export_file}
85 VERBATIM
86 COMMENT "Creating export file for ${target_name}")
87 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000088 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000089 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000090 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000091
Chris Bieneman970555b2014-10-30 22:37:58 +000092 set(CAT "cat")
93 set(export_file_nativeslashes ${export_file})
Yaron Kerene6545932015-05-19 12:59:23 +000094 if(WIN32 AND NOT CYGWIN AND NOT MSYS)
Chris Bieneman970555b2014-10-30 22:37:58 +000095 set(CAT "type")
96 # Convert ${export_file} to native format (backslashes) for "type"
97 # Does not use file(TO_NATIVE_PATH) as it doesn't create a native
98 # path but a build-system specific format (see CMake bug
99 # http://public.kitware.com/Bug/print_bug_page.php?bug_id=5939 )
100 string(REPLACE / \\ export_file_nativeslashes ${export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000101 endif()
102
NAKAMURA Takumi81210752013-12-29 16:15:18 +0000103 add_custom_command(OUTPUT ${native_export_file}
104 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
Chris Bieneman970555b2014-10-30 22:37:58 +0000105 COMMAND ${CAT} ${export_file_nativeslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000106 DEPENDS ${export_file}
107 VERBATIM
108 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000109 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
110 if(MSVC)
Reid Klecknerec6789b2015-04-22 16:23:00 +0000111 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000112 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000115 endif()
116
117 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000118 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000119
120 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
121 foreach(src ${srcs})
122 get_filename_component(extension ${src} EXT)
123 if(extension STREQUAL ".cpp")
124 set(first_source_file ${src})
125 break()
126 endif()
127 endforeach()
128
129 # Force re-linking when the exports file changes. Actually, it
130 # forces recompilation of the source file. The LINK_DEPENDS target
131 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000132 # FIXME: This is not safe because this will create the same target
133 # ${native_export_file} in several different file:
134 # - One where we emitted ${target_name}_exports
135 # - One where we emitted the build command for the following object.
136 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
137 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000138
139 set_property(DIRECTORY APPEND
140 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
141
142 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000143
144 # Add dependency to *_exports later -- CMake issue 14747
145 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
146 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000147endfunction(add_llvm_symbol_exports)
148
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000149if(NOT WIN32 AND NOT APPLE)
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000150 execute_process(
151 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
152 OUTPUT_VARIABLE stdout
NAKAMURA Takumi2f3e3dd2015-01-06 09:44:44 +0000153 ERROR_QUIET
NAKAMURA Takumi8b1578f2015-01-06 09:44:29 +0000154 )
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000155 if("${stdout}" MATCHES "GNU gold")
156 set(LLVM_LINKER_IS_GOLD ON)
157 endif()
158endif()
159
Rafael Espindola8cb78762014-11-02 12:14:22 +0000160function(add_link_opts target_name)
Rafael Espindola24766f92015-04-06 15:04:31 +0000161 # Don't use linker optimizations in debug builds since it slows down the
162 # linker in a context where the optimizations are not important.
163 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
Rafael Espindola8cb78762014-11-02 12:14:22 +0000164
Rafael Espindola24766f92015-04-06 15:04:31 +0000165 # Pass -O3 to the linker. This enabled different optimizations on different
166 # linkers.
167 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32))
Nico Weber62588e12013-12-30 03:36:05 +0000168 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola24766f92015-04-06 15:04:31 +0000169 LINK_FLAGS " -Wl,-O3")
170 endif()
171
172 if(LLVM_LINKER_IS_GOLD)
173 # With gold gc-sections is always safe.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000174 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
175 LINK_FLAGS " -Wl,--gc-sections")
Rafael Espindola24766f92015-04-06 15:04:31 +0000176 # Note that there is a bug with -Wl,--icf=safe so it is not safe
177 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
178 endif()
179
180 if(NOT LLVM_NO_DEAD_STRIP)
181 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
182 # ld64's implementation of -dead_strip breaks tools that use plugins.
183 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
184 LINK_FLAGS " -Wl,-dead_strip")
185 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
186 # Object files are compiled with -ffunction-data-sections.
187 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
188 # tools that use plugins. Always pass --gc-sections once we require
189 # a newer linker.
190 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
191 LINK_FLAGS " -Wl,--gc-sections")
192 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000193 endif()
194 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000195endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000196
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000197# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
198# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
199# or a certain builder, for eaxample, msbuild.exe, would be confused.
200function(set_output_directory target bindir libdir)
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000201 # Do nothing if *_OUTPUT_INTDIR is empty.
202 if("${bindir}" STREQUAL "")
203 return()
204 endif()
205
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000206 # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
207 # It affects output of add_library(MODULE).
208 if(WIN32 OR CYGWIN)
209 # DLL platform
210 set(moddir ${bindir})
211 else()
212 set(moddir ${libdir})
213 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000214 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
215 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
216 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
217 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
218 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000219 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000220 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
221 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000222 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000223 endforeach()
224 else()
225 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
226 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000227 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000228 endif()
229endfunction()
230
Greg Bedwell95213c32015-06-12 15:58:29 +0000231# If on Windows and building with MSVC, add the resource script containing the
232# VERSIONINFO data to the project. This embeds version resource information
233# into the output .exe or .dll.
234# TODO: Enable for MinGW Windows builds too.
235#
236function(add_windows_version_resource_file OUT_VAR)
237 set(sources ${ARGN})
238 if (MSVC)
239 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
NAKAMURA Takumiec782b42015-06-14 21:47:29 +0000240 if(EXISTS ${resource_file})
241 set(sources ${sources} ${resource_file})
242 source_group("Resource Files" ${resource_file})
243 set(windows_resource_file ${resource_file} PARENT_SCOPE)
244 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000245 endif(MSVC)
246
247 set(${OUT_VAR} ${sources} PARENT_SCOPE)
248endfunction(add_windows_version_resource_file)
249
250# set_windows_version_resource_properties(name resource_file...
251# VERSION_MAJOR int
252# Optional major version number (defaults to LLVM_VERSION_MAJOR)
253# VERSION_MINOR int
254# Optional minor version number (defaults to LLVM_VERSION_MINOR)
255# VERSION_PATCHLEVEL int
256# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
257# VERSION_STRING
258# Optional version string (defaults to PACKAGE_VERSION)
259# PRODUCT_NAME
260# Optional product name string (defaults to "LLVM")
261# )
262function(set_windows_version_resource_properties name resource_file)
263 cmake_parse_arguments(ARG
264 ""
265 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
266 ""
267 ${ARGN})
268
269 if (NOT DEFINED ARG_VERSION_MAJOR)
270 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
271 endif()
272
273 if (NOT DEFINED ARG_VERSION_MINOR)
274 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
275 endif()
276
277 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
278 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
279 endif()
280
281 if (NOT DEFINED ARG_VERSION_STRING)
282 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
283 endif()
284
285 if (NOT DEFINED ARG_PRODUCT_NAME)
286 set(ARG_PRODUCT_NAME "LLVM")
287 endif()
288
289 set_property(SOURCE ${resource_file}
Peter Collingbournea89468b2015-06-18 01:15:18 +0000290 PROPERTY COMPILE_FLAGS /nologo)
291 set_property(SOURCE ${resource_file}
Greg Bedwell95213c32015-06-12 15:58:29 +0000292 PROPERTY COMPILE_DEFINITIONS
293 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
294 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
295 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
296 "RC_VERSION_FIELD_4=0"
297 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
298 "RC_INTERNAL_NAME=\"${name}\""
299 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
300 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
301endfunction(set_windows_version_resource_properties)
302
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000303# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000304# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000305# STATIC by default w/o BUILD_SHARED_LIBS.
306# SHARED by default w/ BUILD_SHARED_LIBS.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000307# MODULE
308# Target ${name} might not be created on unsupported platforms.
309# Check with "if(TARGET ${name})".
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000310# OUTPUT_NAME name
311# Corresponds to OUTPUT_NAME in target properties.
312# DEPENDS targets...
313# Same semantics as add_dependencies().
314# LINK_COMPONENTS components...
315# Same as the variable LLVM_LINK_COMPONENTS.
316# LINK_LIBS lib_targets...
317# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000318# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000319# May specify header files for IDE generators.
320# )
321function(llvm_add_library name)
322 cmake_parse_arguments(ARG
323 "MODULE;SHARED;STATIC"
324 "OUTPUT_NAME"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000325 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000326 ${ARGN})
327 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000328 if(ARG_ADDITIONAL_HEADERS)
329 # Pass through ADDITIONAL_HEADERS.
330 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
331 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000332 if(ARG_OBJLIBS)
333 set(ALL_FILES ${ARG_OBJLIBS})
334 else()
335 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
336 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000337
338 if(ARG_MODULE)
339 if(ARG_SHARED OR ARG_STATIC)
340 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
341 endif()
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000342 if(NOT LLVM_ENABLE_PLUGINS)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000343 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
344 return()
345 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000346 else()
347 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
348 set(ARG_SHARED TRUE)
349 endif()
350 if(NOT ARG_SHARED)
351 set(ARG_STATIC TRUE)
352 endif()
353 endif()
354
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000355 # Generate objlib
356 if(ARG_SHARED AND ARG_STATIC)
357 # Generate an obj library for both targets.
358 set(obj_name "obj.${name}")
359 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
360 ${ALL_FILES}
361 )
362 llvm_update_compile_flags(${obj_name})
363 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
364
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000365 # Do add_dependencies(obj) later due to CMake issue 14747.
366 list(APPEND objlibs ${obj_name})
367
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000368 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
369 endif()
370
371 if(ARG_SHARED AND ARG_STATIC)
372 # static
373 set(name_static "${name}_static")
374 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000375 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000376 endif()
377 # DEPENDS has been appended to LLVM_COMMON_LIBS.
378 llvm_add_library(${name_static} STATIC
379 ${output_name}
380 OBJLIBS ${ALL_FILES} # objlib
381 LINK_LIBS ${ARG_LINK_LIBS}
382 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
383 )
384 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
385 set(ARG_STATIC)
386 endif()
387
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000388 if(ARG_MODULE)
389 add_library(${name} MODULE ${ALL_FILES})
390 elseif(ARG_SHARED)
Greg Bedwell95213c32015-06-12 15:58:29 +0000391 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000392 add_library(${name} SHARED ${ALL_FILES})
393 else()
394 add_library(${name} STATIC ${ALL_FILES})
395 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000396
397 if(DEFINED windows_resource_file)
398 set_windows_version_resource_properties(${name} ${windows_resource_file})
399 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
400 endif()
401
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000402 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000403 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000404 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000405 if(ARG_OUTPUT_NAME)
406 set_target_properties(${name}
407 PROPERTIES
408 OUTPUT_NAME ${ARG_OUTPUT_NAME}
409 )
410 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000411
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000412 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000413 set_target_properties(${name} PROPERTIES
414 PREFIX ""
415 SUFFIX ${LLVM_PLUGIN_EXT}
416 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000417 endif()
418
419 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000420 if(WIN32)
421 set_target_properties(${name} PROPERTIES
422 PREFIX ""
423 )
424 endif()
Reid Kleckner2e3d1e02015-02-27 18:22:46 +0000425
Chris Bienemana8a05962015-02-18 18:52:06 +0000426 set_target_properties(${name}
427 PROPERTIES
Chris Bienemanbf2e90a2015-03-02 17:50:13 +0000428 SOVERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}
Chris Bienemana8a05962015-02-18 18:52:06 +0000429 VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000430 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000431
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000432 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000433 # Do not add -Dname_EXPORTS to the command-line when building files in this
434 # target. Doing so is actively harmful for the modules build because it
435 # creates extra module variants, and not useful because we don't use these
436 # macros.
437 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
438
Nico Weber06473f82013-12-29 05:39:01 +0000439 if (LLVM_EXPORTED_SYMBOL_FILE)
440 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
441 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000442 endif()
443
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000444 # Add the explicit dependency information for this library.
445 #
446 # It would be nice to verify that we have the dependencies for this library
447 # name, but using get_property(... SET) doesn't suffice to determine if a
448 # property has been set to an empty value.
449 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
450
451 llvm_map_components_to_libnames(llvm_libs
452 ${ARG_LINK_COMPONENTS}
453 ${LLVM_LINK_COMPONENTS}
454 )
455
456 if(CMAKE_VERSION VERSION_LESS 2.8.12)
457 # Link libs w/o keywords, assuming PUBLIC.
458 target_link_libraries(${name}
459 ${ARG_LINK_LIBS}
460 ${lib_deps}
461 ${llvm_libs}
462 )
463 elseif(ARG_STATIC)
464 target_link_libraries(${name} INTERFACE
465 ${ARG_LINK_LIBS}
466 ${lib_deps}
467 ${llvm_libs}
468 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000469 else()
Rafael Espindolaf9dcf902014-11-07 15:33:56 +0000470 # We can use PRIVATE since SO knows its dependent libs.
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000471 target_link_libraries(${name} PRIVATE
472 ${ARG_LINK_LIBS}
473 ${lib_deps}
474 ${llvm_libs}
475 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000476 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000477
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000478 if(LLVM_COMMON_DEPENDS)
479 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000480 # Add dependencies also to objlibs.
481 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
482 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000483 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
484 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000485 endif()
486endfunction()
487
488macro(add_llvm_library name)
Chris Bieneman84271692015-04-16 16:56:18 +0000489 cmake_parse_arguments(ARG
490 "SHARED"
491 ""
492 ""
493 ${ARGN})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000494 if( BUILD_SHARED_LIBS )
495 llvm_add_library(${name} SHARED ${ARGN})
496 else()
497 llvm_add_library(${name} ${ARGN})
498 endif()
499 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
500
Nick Lewycky61aed872011-04-29 02:12:06 +0000501 if( EXCLUDE_FROM_ALL )
502 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
503 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000504 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000505 if(ARG_SHARED OR BUILD_SHARED_LIBS)
506 if(WIN32 OR CYGWIN)
Chris Bienemana985e6b2015-04-16 18:08:33 +0000507 set(install_type RUNTIME)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000508 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000509 set(install_type LIBRARY)
Chris Bienemanc2368aa2015-04-16 17:40:51 +0000510 endif()
Chris Bieneman84271692015-04-16 16:56:18 +0000511 else()
Chris Bienemana985e6b2015-04-16 18:08:33 +0000512 set(install_type ARCHIVE)
Chris Bieneman84271692015-04-16 16:56:18 +0000513 endif()
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000514
Chris Bienemana985e6b2015-04-16 18:08:33 +0000515 install(TARGETS ${name}
516 EXPORT LLVMExports
517 ${install_type} DESTINATION lib${LLVM_LIBDIR_SUFFIX}
518 COMPONENT ${name})
519
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000520 if (NOT CMAKE_CONFIGURATION_TYPES)
521 add_custom_target(install-${name}
522 DEPENDS ${name}
523 COMMAND "${CMAKE_COMMAND}"
524 -DCMAKE_INSTALL_COMPONENT=${name}
525 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
526 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000527 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000528 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000529 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000530 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000531endmacro(add_llvm_library name)
532
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000533macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000534 llvm_add_library(${name} MODULE ${ARGN})
535 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000536 # Add empty "phony" target
537 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000538 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000539 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000540 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000541 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000542 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000543 if(WIN32 OR CYGWIN)
544 # DLL platform
545 set(dlldir "bin")
546 else()
547 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
548 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000549 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000550 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000551 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000552 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
553 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000554 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000555 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000556 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000557
558 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000559endmacro(add_llvm_loadable_module name)
560
561
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000562macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000563 llvm_process_sources( ALL_FILES ${ARGN} )
Greg Bedwell95213c32015-06-12 15:58:29 +0000564 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
565
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000566 if( EXCLUDE_FROM_ALL )
567 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
568 else()
569 add_executable(${name} ${ALL_FILES})
570 endif()
Greg Bedwell95213c32015-06-12 15:58:29 +0000571
572 if(DEFINED windows_resource_file)
573 set_windows_version_resource_properties(${name} ${windows_resource_file})
574 endif()
575
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000576 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000577 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000578
Richard Smith2b91a7f2014-09-26 22:40:15 +0000579 # Do not add -Dname_EXPORTS to the command-line when building files in this
580 # target. Doing so is actively harmful for the modules build because it
581 # creates extra module variants, and not useful because we don't use these
582 # macros.
583 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
584
Nico Weberc27118d2013-12-28 23:31:44 +0000585 if (LLVM_EXPORTED_SYMBOL_FILE)
586 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
587 endif(LLVM_EXPORTED_SYMBOL_FILE)
588
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000589 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000590 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000591 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000592 if( LLVM_COMMON_DEPENDS )
593 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
594 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000595endmacro(add_llvm_executable name)
596
Reid Kleckner3e8c4452015-03-18 20:09:13 +0000597function(export_executable_symbols target)
598 if (NOT MSVC) # MSVC's linker doesn't support exporting all symbols.
599 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
600 endif()
601endfunction()
602
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000603
Hans Wennborg16546272013-08-24 00:20:36 +0000604set (LLVM_TOOLCHAIN_TOOLS
605 llvm-ar
606 llvm-objdump
607 )
608
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000609macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000610 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000611 set(EXCLUDE_FROM_ALL ON)
612 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000613 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000614
615 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
616 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
617 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000618 install(TARGETS ${name}
619 EXPORT LLVMExports
Chris Bieneman0d9289d2015-02-18 19:25:47 +0000620 RUNTIME DESTINATION bin
621 COMPONENT ${name})
622
623 if (NOT CMAKE_CONFIGURATION_TYPES)
624 add_custom_target(install-${name}
625 DEPENDS ${name}
626 COMMAND "${CMAKE_COMMAND}"
627 -DCMAKE_INSTALL_COMPONENT=${name}
628 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
629 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000630 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000631 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000632 if( LLVM_BUILD_TOOLS )
633 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
634 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000635 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000636endmacro(add_llvm_tool name)
637
638
639macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000640 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000641 set(EXCLUDE_FROM_ALL ON)
642 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000643 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000644 if( LLVM_BUILD_EXAMPLES )
645 install(TARGETS ${name} RUNTIME DESTINATION examples)
646 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000647 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000648endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000649
650
Oscar Fuentes3145e922011-02-20 22:06:10 +0000651macro(add_llvm_utility name)
652 add_llvm_executable(${name} ${ARGN})
653 set_target_properties(${name} PROPERTIES FOLDER "Utils")
Derek Schuffef9928e2015-03-27 16:53:06 +0000654 if( LLVM_INSTALL_UTILS )
655 install (TARGETS ${name}
656 RUNTIME DESTINATION bin
657 COMPONENT ${name})
658 if (NOT CMAKE_CONFIGURATION_TYPES)
659 add_custom_target(install-${name}
660 DEPENDS ${name}
661 COMMAND "${CMAKE_COMMAND}"
662 -DCMAKE_INSTALL_COMPONENT=${name}
663 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
664 endif()
665 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000666endmacro(add_llvm_utility name)
667
668
Oscar Fuentescdc95492008-09-26 04:40:32 +0000669macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000670 include_directories(BEFORE
671 ${CMAKE_CURRENT_BINARY_DIR}
672 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000673 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000674 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000675endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000676
677# Add external project that may want to be built as part of llvm such as Clang,
678# lld, and Polly. This adds two options. One for the source directory of the
679# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000680# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000681# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000682macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000683 set(add_llvm_external_dir "${ARGN}")
684 if("${add_llvm_external_dir}" STREQUAL "")
685 set(add_llvm_external_dir ${name})
686 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000687 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000688 string(REPLACE "-" "_" nameUNDERSCORE ${name})
689 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
690 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000691 CACHE PATH "Path to ${name} source directory")
692 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
693 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
694 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
695 "Whether to build ${name} as part of LLVM" ON)
696 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000697 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000698 endif()
699 endif()
700endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000701
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000702macro(add_llvm_tool_subdirectory name)
703 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
704 add_subdirectory(${name})
705endmacro(add_llvm_tool_subdirectory)
706
707macro(ignore_llvm_tool_subdirectory name)
708 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
709endmacro(ignore_llvm_tool_subdirectory)
710
711function(add_llvm_implicit_external_projects)
712 set(list_of_implicit_subdirs "")
713 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
714 foreach(dir ${sub-dirs})
715 if(IS_DIRECTORY "${dir}")
716 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
717 if( tool_subdir_ignore EQUAL -1
718 AND EXISTS "${dir}/CMakeLists.txt")
719 get_filename_component(fn "${dir}" NAME)
720 list(APPEND list_of_implicit_subdirs "${fn}")
721 endif()
722 endif()
723 endforeach()
724
725 foreach(external_proj ${list_of_implicit_subdirs})
726 add_llvm_external_project("${external_proj}")
727 endforeach()
728endfunction(add_llvm_implicit_external_projects)
729
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000730# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000731function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000732 if( NOT LLVM_BUILD_TESTS )
733 set(EXCLUDE_FROM_ALL ON)
734 endif()
735
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000736 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
737 if (NOT LLVM_ENABLE_THREADS)
738 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
739 endif ()
740
741 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000742 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000743 endif ()
744
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000745 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000746
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000747 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000748 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
749 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000750 target_link_libraries(${test_name}
751 gtest
752 gtest_main
753 LLVMSupport # gtest needs it for raw_ostream.
754 )
755
756 add_dependencies(${test_suite} ${test_name})
757 get_target_property(test_suite_folder ${test_suite} FOLDER)
758 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
759 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
760 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000761endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000762
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000763function(llvm_add_go_executable binary pkgpath)
764 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
765
766 if(LLVM_BINDINGS MATCHES "go")
767 # FIXME: This should depend only on the libraries Go needs.
768 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
769 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
770 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
771 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruthcab512d2014-12-29 22:50:30 +0000772 set(cppflags "")
773 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
774 foreach(d ${include_dirs})
775 set(cppflags "${cppflags} -I${d}")
776 endforeach(d)
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000777 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
778 add_custom_command(OUTPUT ${binpath}
Chandler Carruthcab512d2014-12-29 22:50:30 +0000779 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000780 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
781 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
782 ${llvmlibs} ${ARG_DEPENDS}
783 COMMENT "Building Go executable ${binary}"
784 VERBATIM)
785 if (ARG_ALL)
786 add_custom_target(${binary} ALL DEPENDS ${binpath})
787 else()
788 add_custom_target(${binary} DEPENDS ${binpath})
789 endif()
790 endif()
791endfunction()
792
Chandler Carruth3511dd32012-06-28 06:36:24 +0000793# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000794# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000795# variables needed for the 'lit.site.cfg' files. This function bundles the
796# common variables that any Lit instance is likely to need, and custom
797# variables can be passed in.
798function(configure_lit_site_cfg input output)
799 foreach(c ${LLVM_TARGETS_TO_BUILD})
800 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
801 endforeach(c)
802 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
803
804 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000805
Chandler Carruth3511dd32012-06-28 06:36:24 +0000806 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000807 if (CMAKE_CFG_INTDIR STREQUAL ".")
808 set(LLVM_BUILD_MODE ".")
809 else ()
810 set(LLVM_BUILD_MODE "%(build_mode)s")
811 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000812
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000813 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000814 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
815 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000816 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
817 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000818
819 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +0000820 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000821
Chandler Carruth3511dd32012-06-28 06:36:24 +0000822 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000823 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
824 # plugins. We may rename it.
825 if(LLVM_ENABLE_PLUGINS)
826 set(ENABLE_SHARED "1")
827 else()
828 set(ENABLE_SHARED "0")
829 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000830
831 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
832 set(ENABLE_ASSERTIONS "1")
833 else()
834 set(ENABLE_ASSERTIONS "0")
835 endif()
836
Kuba Breckaa62bbcc2015-01-24 01:42:44 +0000837 set(HOST_OS ${CMAKE_SYSTEM_NAME})
Tim Northoverbfe84682013-02-14 16:49:32 +0000838 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000839
Peter Collingbournece800842014-10-17 18:32:36 +0000840 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
841 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +0000842 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +0000843
Chandler Carruth3511dd32012-06-28 06:36:24 +0000844 configure_file(${input} ${output} @ONLY)
845endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000846
847# A raw function to create a lit target. This is used to implement the testuite
848# management functions.
849function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000850 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000851 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
852 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000853 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
854 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
855 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +0000856 if (LLVM_MAIN_SRC_DIR)
857 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
858 else()
859 find_program(LIT_COMMAND llvm-lit)
860 endif ()
861 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000862 foreach(param ${ARG_PARAMS})
863 list(APPEND LIT_COMMAND --param ${param})
864 endforeach()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +0000865 if (ARG_DEFAULT_ARGS)
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000866 add_custom_target(${target}
867 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
868 COMMENT "${comment}"
Peter Collingbournea7fb5cf2014-11-17 22:16:15 +0000869 ${cmake_3_2_USES_TERMINAL}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000870 )
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000871 else()
872 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000873 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000874 message(STATUS "${target} does nothing.")
875 endif()
Peter Collingbourne2ba7f7b2015-02-18 22:25:35 +0000876 if (ARG_DEPENDS)
877 add_dependencies(${target} ${ARG_DEPENDS})
878 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000879
880 # Tests should be excluded from "Build Solution".
881 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000882endfunction()
883
884# A function to add a set of lit test suites to be driven through 'check-*' targets.
885function(add_lit_testsuite target comment)
886 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
887
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000888 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
889 if(NOT EXCLUDE_FROM_ALL)
890 # Register the testsuites, params and depends for the global check rule.
891 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
892 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
893 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
894 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
895 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000896
897 # Produce a specific suffixed check rule.
898 add_lit_target(${target} ${comment}
899 ${ARG_DEFAULT_ARGS}
900 PARAMS ${ARG_PARAMS}
901 DEPENDS ${ARG_DEPENDS}
902 ARGS ${ARG_ARGS}
903 )
904endfunction()
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000905
906function(add_lit_testsuites project directory)
907 if (NOT CMAKE_CONFIGURATION_TYPES)
908 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
909 file(GLOB_RECURSE litCfg ${directory}/lit*.cfg)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000910 set(lit_suites)
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000911 foreach(f ${litCfg})
912 get_filename_component(dir ${f} DIRECTORY)
Chris Bienemanabc8fb22015-03-27 21:45:49 +0000913 set(lit_suites ${lit_suites} ${dir})
914 endforeach()
915 list(REMOVE_DUPLICATES lit_suites)
916 foreach(dir ${lit_suites})
Chris Bieneman9ea37d92015-03-23 20:04:00 +0000917 string(REPLACE ${directory} "" name_slash ${dir})
918 if (name_slash)
919 string(REPLACE "/" "-" name_slash ${name_slash})
920 string(REPLACE "\\" "-" name_dashes ${name_slash})
921 string(TOLOWER "${project}${name_dashes}" name_var)
922 add_lit_target("check-${name_var}" "Running lit suite ${dir}"
923 ${dir}
924 PARAMS ${ARG_PARAMS}
925 DEPENDS ${ARG_DEPENDS}
926 ARGS ${ARG_ARGS}
927 )
928 endif()
929 endforeach()
930 endif()
931endfunction()