blob: 83f57a708a3ae12f0c7f21dae595188e0e4c69a1 [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
11 if(LLVM_REQUIRES_EH)
12 set(LLVM_REQUIRES_RTTI ON)
13 else()
14 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000015 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000016 elseif(MSVC)
17 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000018 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000019 endif()
20 endif()
21
22 if(NOT LLVM_REQUIRES_RTTI)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000023 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
24 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000025 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000026 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000027 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000028 endif ()
29 endif()
30
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000031 # Assume that;
32 # - LLVM_COMPILE_FLAGS is list.
33 # - PROPERTY COMPILE_FLAGS is string.
34 string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
35
NAKAMURA Takumia679f432014-01-28 09:44:06 +000036 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000037 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000038 get_filename_component(suf ${fn} EXT)
39 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000040 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
41 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000042 endif()
43 endforeach()
44 else()
45 # Update target props, since all sources are C++.
46 set_property(TARGET ${name} APPEND_STRING PROPERTY
47 COMPILE_FLAGS "${target_compile_flags}")
48 endif()
49
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000050 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
51endfunction()
52
Nico Weberc27118d2013-12-28 23:31:44 +000053function(add_llvm_symbol_exports target_name export_file)
54 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000055 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000056 add_custom_command(OUTPUT ${native_export_file}
57 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000058 DEPENDS ${export_file}
59 VERBATIM
60 COMMENT "Creating export file for ${target_name}")
61 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000062 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000063 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
64 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000065 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000066 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000067 add_custom_command(OUTPUT ${native_export_file}
68 COMMAND echo "{" > ${native_export_file}
69 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
70 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
71 COMMAND echo " local: *;" >> ${native_export_file}
72 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000073 DEPENDS ${export_file}
74 VERBATIM
75 COMMENT "Creating export file for ${target_name}")
76 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000077 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000078 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000079 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000080
81 set(CAT "type")
82 if(CYGWIN)
83 set(CAT "cat")
84 endif()
85
Nico Weber5c8a4a32013-12-29 04:05:23 +000086 # Using ${export_file} in add_custom_command directly confuses cmd.exe.
87 file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
88
NAKAMURA Takumi81210752013-12-29 16:15:18 +000089 add_custom_command(OUTPUT ${native_export_file}
90 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
91 COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000092 DEPENDS ${export_file}
93 VERBATIM
94 COMMENT "Creating export file for ${target_name}")
Nico Weber49d6f482013-12-29 07:43:09 +000095 if(CYGWIN OR MINGW)
Nico Weber49da7582013-12-29 06:56:28 +000096 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000097 LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weber49da7582013-12-29 06:56:28 +000098 else()
99 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +0000100 LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000101 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000102 endif()
103
104 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000105 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000106
107 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
108 foreach(src ${srcs})
109 get_filename_component(extension ${src} EXT)
110 if(extension STREQUAL ".cpp")
111 set(first_source_file ${src})
112 break()
113 endif()
114 endforeach()
115
116 # Force re-linking when the exports file changes. Actually, it
117 # forces recompilation of the source file. The LINK_DEPENDS target
118 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000119 # FIXME: This is not safe because this will create the same target
120 # ${native_export_file} in several different file:
121 # - One where we emitted ${target_name}_exports
122 # - One where we emitted the build command for the following object.
123 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
124 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000125
126 set_property(DIRECTORY APPEND
127 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
128
129 add_dependencies(${target_name} ${target_name}_exports)
130endfunction(add_llvm_symbol_exports)
131
Nico Weber62588e12013-12-30 03:36:05 +0000132function(add_dead_strip target_name)
Nico Weber62588e12013-12-30 03:36:05 +0000133 if(NOT LLVM_NO_DEAD_STRIP)
134 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
135 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
136 LINK_FLAGS " -Wl,-dead_strip")
137 elseif(NOT WIN32)
NAKAMURA Takumibb50bce2014-01-28 09:43:55 +0000138 # Object files are compiled with -ffunction-data-sections.
Nico Weber62588e12013-12-30 03:36:05 +0000139 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
140 LINK_FLAGS " -Wl,--gc-sections")
141 endif()
142 endif()
143endfunction(add_dead_strip)
144
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000145# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
146# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
147# or a certain builder, for eaxample, msbuild.exe, would be confused.
148function(set_output_directory target bindir libdir)
149 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
150 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
151 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
152 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
153 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
154 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
155 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
Douglas Gregorbbebd812014-01-02 19:07:19 +0000156 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000157 endforeach()
158 else()
159 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
160 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
161 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
162 endif()
163endfunction()
164
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000165# llvm_add_library(name sources...
166# MODULE;SHARED;STATIC
167# STATIC by default w/o BUILD_SHARED_LIBS.
168# SHARED by default w/ BUILD_SHARED_LIBS.
169# OUTPUT_NAME name
170# Corresponds to OUTPUT_NAME in target properties.
171# DEPENDS targets...
172# Same semantics as add_dependencies().
173# LINK_COMPONENTS components...
174# Same as the variable LLVM_LINK_COMPONENTS.
175# LINK_LIBS lib_targets...
176# Same semantics as target_link_libraries().
177# ADDITIONAL_HEADERS (implemented in LLVMProcessSources)
178# May specify header files for IDE generators.
179# )
180function(llvm_add_library name)
181 cmake_parse_arguments(ARG
182 "MODULE;SHARED;STATIC"
183 "OUTPUT_NAME"
184 "DEPENDS;LINK_COMPONENTS;LINK_LIBS"
185 ${ARGN})
186 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
187 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS})
188
189 if(ARG_MODULE)
190 if(ARG_SHARED OR ARG_STATIC)
191 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
192 endif()
193 else()
194 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
195 set(ARG_SHARED TRUE)
196 endif()
197 if(NOT ARG_SHARED)
198 set(ARG_STATIC TRUE)
199 endif()
200 endif()
201
202 if(ARG_MODULE)
203 add_library(${name} MODULE ${ALL_FILES})
204 elseif(ARG_SHARED)
205 add_library(${name} SHARED ${ALL_FILES})
206 else()
207 add_library(${name} STATIC ${ALL_FILES})
208 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000209 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000210 llvm_update_compile_flags(${name})
Nico Weber62588e12013-12-30 03:36:05 +0000211 add_dead_strip( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000212 if(ARG_OUTPUT_NAME)
213 set_target_properties(${name}
214 PROPERTIES
215 OUTPUT_NAME ${ARG_OUTPUT_NAME}
216 )
217 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000218
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000219 if(ARG_MODULE)
220 set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
221 endif()
222
223 if(ARG_SHARED)
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000224 if (MSVC)
225 set_target_properties(${name}
226 PROPERTIES
227 IMPORT_SUFFIX ".imp")
228 endif ()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000229 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000230
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000231 if(ARG_MODULE OR ARG_SHARED)
Nico Weber06473f82013-12-29 05:39:01 +0000232 if (LLVM_EXPORTED_SYMBOL_FILE)
233 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
234 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000235 endif()
236
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000237 target_link_libraries(${name} ${ARG_LINK_LIBS})
238
239 llvm_config(${name} ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
240
Oscar Fuentes978e5282011-03-29 20:51:08 +0000241 # Ensure that the system libraries always comes last on the
242 # list. Without this, linking the unit tests on MinGW fails.
243 link_system_libs( ${name} )
244
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000245 if(LLVM_COMMON_DEPENDS)
246 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
247 endif()
248endfunction()
249
250macro(add_llvm_library name)
251 if( BUILD_SHARED_LIBS )
252 llvm_add_library(${name} SHARED ${ARGN})
253 else()
254 llvm_add_library(${name} ${ARGN})
255 endif()
256 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
257
Nick Lewycky61aed872011-04-29 02:12:06 +0000258 if( EXCLUDE_FROM_ALL )
259 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
260 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000261 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
262 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000263 EXPORT LLVMExports
Hans Wennborg16546272013-08-24 00:20:36 +0000264 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
265 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
266 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000267 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000268 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000269 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000270
271 # Add the explicit dependency information for this library.
272 #
273 # It would be nice to verify that we have the dependencies for this library
274 # name, but using get_property(... SET) doesn't suffice to determine if a
275 # property has been set to an empty value.
276 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
277 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000278endmacro(add_llvm_library name)
279
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000280macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000281 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000282 message(STATUS "Loadable modules not supported on this platform.
283${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000284 # Add empty "phony" target
285 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000286 else()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000287 llvm_add_library(${name} MODULE ${ARGN})
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000288 set_target_properties( ${name} PROPERTIES PREFIX "" )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000289
Douglas Gregor66df54f2009-11-10 15:30:33 +0000290 if (APPLE)
291 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000292 set_property(TARGET ${name} APPEND_STRING PROPERTY
293 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000294 endif()
295
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000296 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000297 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000298 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000299 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
300 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000301 EXPORT LLVMExports
Hans Wennborg16546272013-08-24 00:20:36 +0000302 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
303 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
304 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000305 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000306 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000307 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000308
309 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000310endmacro(add_llvm_loadable_module name)
311
312
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000313macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000314 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000315 if( EXCLUDE_FROM_ALL )
316 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
317 else()
318 add_executable(${name} ${ALL_FILES})
319 endif()
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000320 llvm_update_compile_flags(${name})
Nico Weber62588e12013-12-30 03:36:05 +0000321 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000322
323 if (LLVM_EXPORTED_SYMBOL_FILE)
324 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
325 endif(LLVM_EXPORTED_SYMBOL_FILE)
326
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000327 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000328 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000329 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000330 if( LLVM_COMMON_DEPENDS )
331 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
332 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000333 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000334endmacro(add_llvm_executable name)
335
336
Hans Wennborg16546272013-08-24 00:20:36 +0000337set (LLVM_TOOLCHAIN_TOOLS
338 llvm-ar
339 llvm-objdump
340 )
341
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000342macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000343 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000344 set(EXCLUDE_FROM_ALL ON)
345 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000346 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000347
348 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
349 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
350 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000351 install(TARGETS ${name}
352 EXPORT LLVMExports
353 RUNTIME DESTINATION bin)
Hans Wennborg16546272013-08-24 00:20:36 +0000354 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000355 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000356 if( LLVM_BUILD_TOOLS )
357 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
358 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000359 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000360endmacro(add_llvm_tool name)
361
362
363macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000364 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000365 set(EXCLUDE_FROM_ALL ON)
366 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000367 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000368 if( LLVM_BUILD_EXAMPLES )
369 install(TARGETS ${name} RUNTIME DESTINATION examples)
370 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000371 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000372endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000373
374
Oscar Fuentes3145e922011-02-20 22:06:10 +0000375macro(add_llvm_utility name)
376 add_llvm_executable(${name} ${ARGN})
377 set_target_properties(${name} PROPERTIES FOLDER "Utils")
378endmacro(add_llvm_utility name)
379
380
Oscar Fuentescdc95492008-09-26 04:40:32 +0000381macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000382 include_directories(BEFORE
383 ${CMAKE_CURRENT_BINARY_DIR}
384 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000385 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000386 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000387endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000388
389# Add external project that may want to be built as part of llvm such as Clang,
390# lld, and Polly. This adds two options. One for the source directory of the
391# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000392# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000393# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000394macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000395 set(add_llvm_external_dir "${ARGN}")
396 if("${add_llvm_external_dir}" STREQUAL "")
397 set(add_llvm_external_dir ${name})
398 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000399 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000400 string(REPLACE "-" "_" nameUNDERSCORE ${name})
401 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
402 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000403 CACHE PATH "Path to ${name} source directory")
404 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
405 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
406 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
407 "Whether to build ${name} as part of LLVM" ON)
408 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000409 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000410 endif()
411 endif()
412endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000413
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000414macro(add_llvm_tool_subdirectory name)
415 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
416 add_subdirectory(${name})
417endmacro(add_llvm_tool_subdirectory)
418
419macro(ignore_llvm_tool_subdirectory name)
420 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
421endmacro(ignore_llvm_tool_subdirectory)
422
423function(add_llvm_implicit_external_projects)
424 set(list_of_implicit_subdirs "")
425 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
426 foreach(dir ${sub-dirs})
427 if(IS_DIRECTORY "${dir}")
428 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
429 if( tool_subdir_ignore EQUAL -1
430 AND EXISTS "${dir}/CMakeLists.txt")
431 get_filename_component(fn "${dir}" NAME)
432 list(APPEND list_of_implicit_subdirs "${fn}")
433 endif()
434 endif()
435 endforeach()
436
437 foreach(external_proj ${list_of_implicit_subdirs})
438 add_llvm_external_project("${external_proj}")
439 endforeach()
440endfunction(add_llvm_implicit_external_projects)
441
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000442# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000443function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000444 if( NOT LLVM_BUILD_TESTS )
445 set(EXCLUDE_FROM_ALL ON)
446 endif()
447
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000448 # Visual Studio 2012 only supports up to 8 template parameters in
449 # std::tr1::tuple by default, but gtest requires 10
450 if (MSVC AND MSVC_VERSION EQUAL 1700)
451 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
452 endif ()
453
454 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
455 if (NOT LLVM_ENABLE_THREADS)
456 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
457 endif ()
458
459 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000460 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000461 endif ()
462
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000463 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000464
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000465 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000466 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
467 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000468 target_link_libraries(${test_name}
469 gtest
470 gtest_main
471 LLVMSupport # gtest needs it for raw_ostream.
472 )
473
474 add_dependencies(${test_suite} ${test_name})
475 get_target_property(test_suite_folder ${test_suite} FOLDER)
476 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
477 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
478 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000479endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000480
481# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000482# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000483# variables needed for the 'lit.site.cfg' files. This function bundles the
484# common variables that any Lit instance is likely to need, and custom
485# variables can be passed in.
486function(configure_lit_site_cfg input output)
487 foreach(c ${LLVM_TARGETS_TO_BUILD})
488 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
489 endforeach(c)
490 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
491
492 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000493
494 if(BUILD_SHARED_LIBS)
495 set(LLVM_SHARED_LIBS_ENABLED "1")
496 else()
497 set(LLVM_SHARED_LIBS_ENABLED "0")
498 endif(BUILD_SHARED_LIBS)
499
500 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
501 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
502 else() # Default for all other unix like systems.
503 # CMake hardcodes the library locaction using rpath.
504 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
505 # build dir. We pass it anyways.
506 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
507 endif()
508
509 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000510 if (CMAKE_CFG_INTDIR STREQUAL ".")
511 set(LLVM_BUILD_MODE ".")
512 else ()
513 set(LLVM_BUILD_MODE "%(build_mode)s")
514 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000515
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000516 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000517 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
518 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000519 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
520 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000521
522 # SHLIBDIR points the build tree.
523 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
524
Chandler Carruth3511dd32012-06-28 06:36:24 +0000525 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
526 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
527 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
528
529 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
530 set(ENABLE_ASSERTIONS "1")
531 else()
532 set(ENABLE_ASSERTIONS "0")
533 endif()
534
Tim Northoverbfe84682013-02-14 16:49:32 +0000535 set(HOST_OS ${CMAKE_SYSTEM_NAME})
536 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000537
Roman Divackyc9871112013-08-27 19:25:01 +0000538 if (CLANG_ENABLE_ARCMT)
539 set(ENABLE_CLANG_ARCMT "1")
540 else()
541 set(ENABLE_CLANG_ARCMT "0")
542 endif()
543 if (CLANG_ENABLE_REWRITER)
544 set(ENABLE_CLANG_REWRITER "1")
545 else()
546 set(ENABLE_CLANG_REWRITER "0")
547 endif()
548 if (CLANG_ENABLE_STATIC_ANALYZER)
549 set(ENABLE_CLANG_STATIC_ANALYZER "1")
550 else()
551 set(ENABLE_CLANG_STATIC_ANALYZER "0")
552 endif()
553
Chandler Carruth3511dd32012-06-28 06:36:24 +0000554 configure_file(${input} ${output} @ONLY)
555endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000556
557# A raw function to create a lit target. This is used to implement the testuite
558# management functions.
559function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000560 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000561 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
562 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000563 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
564 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
565 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000566 set(LIT_COMMAND
567 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000568 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000569 ${LIT_ARGS}
570 )
571 foreach(param ${ARG_PARAMS})
572 list(APPEND LIT_COMMAND --param ${param})
573 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000574 if( ARG_DEPENDS )
575 add_custom_target(${target}
576 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
577 COMMENT "${comment}"
578 )
579 add_dependencies(${target} ${ARG_DEPENDS})
580 else()
581 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000582 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000583 message(STATUS "${target} does nothing.")
584 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000585
586 # Tests should be excluded from "Build Solution".
587 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000588endfunction()
589
590# A function to add a set of lit test suites to be driven through 'check-*' targets.
591function(add_lit_testsuite target comment)
592 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
593
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000594 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
595 if(NOT EXCLUDE_FROM_ALL)
596 # Register the testsuites, params and depends for the global check rule.
597 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
598 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
599 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
600 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
601 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000602
603 # Produce a specific suffixed check rule.
604 add_lit_target(${target} ${comment}
605 ${ARG_DEFAULT_ARGS}
606 PARAMS ${ARG_PARAMS}
607 DEPENDS ${ARG_DEPENDS}
608 ARGS ${ARG_ARGS}
609 )
610endfunction()