blob: 39badf41d77c15dd4001565d70cc801af21481ee [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...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000166# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000167# STATIC by default w/o BUILD_SHARED_LIBS.
168# SHARED by default w/ BUILD_SHARED_LIBS.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000169# MODULE
170# Target ${name} might not be created on unsupported platforms.
171# Check with "if(TARGET ${name})".
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000172# OUTPUT_NAME name
173# Corresponds to OUTPUT_NAME in target properties.
174# DEPENDS targets...
175# Same semantics as add_dependencies().
176# LINK_COMPONENTS components...
177# Same as the variable LLVM_LINK_COMPONENTS.
178# LINK_LIBS lib_targets...
179# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000180# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000181# May specify header files for IDE generators.
182# )
183function(llvm_add_library name)
184 cmake_parse_arguments(ARG
185 "MODULE;SHARED;STATIC"
186 "OUTPUT_NAME"
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000187 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000188 ${ARGN})
189 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000190 if(ARG_ADDITIONAL_HEADERS)
191 # Pass through ADDITIONAL_HEADERS.
192 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
193 endif()
194 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000195
196 if(ARG_MODULE)
197 if(ARG_SHARED OR ARG_STATIC)
198 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
199 endif()
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000200 if(NOT LLVM_ON_UNIX OR CYGWIN)
201 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
202 return()
203 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000204 else()
205 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
206 set(ARG_SHARED TRUE)
207 endif()
208 if(NOT ARG_SHARED)
209 set(ARG_STATIC TRUE)
210 endif()
211 endif()
212
213 if(ARG_MODULE)
214 add_library(${name} MODULE ${ALL_FILES})
215 elseif(ARG_SHARED)
216 add_library(${name} SHARED ${ALL_FILES})
217 else()
218 add_library(${name} STATIC ${ALL_FILES})
219 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000220 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000221 llvm_update_compile_flags(${name})
Nico Weber62588e12013-12-30 03:36:05 +0000222 add_dead_strip( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000223 if(ARG_OUTPUT_NAME)
224 set_target_properties(${name}
225 PROPERTIES
226 OUTPUT_NAME ${ARG_OUTPUT_NAME}
227 )
228 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000229
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000230 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000231 set_target_properties(${name} PROPERTIES
232 PREFIX ""
233 SUFFIX ${LLVM_PLUGIN_EXT}
234 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000235 endif()
236
237 if(ARG_SHARED)
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000238 if (MSVC)
239 set_target_properties(${name}
240 PROPERTIES
241 IMPORT_SUFFIX ".imp")
242 endif ()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000243 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000244
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000245 if(ARG_MODULE OR ARG_SHARED)
Nico Weber06473f82013-12-29 05:39:01 +0000246 if (LLVM_EXPORTED_SYMBOL_FILE)
247 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
248 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000249 endif()
250
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000251 target_link_libraries(${name} ${ARG_LINK_LIBS})
252
253 llvm_config(${name} ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
254
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000255 if(LLVM_COMMON_DEPENDS)
256 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
257 endif()
258endfunction()
259
260macro(add_llvm_library name)
261 if( BUILD_SHARED_LIBS )
262 llvm_add_library(${name} SHARED ${ARGN})
263 else()
264 llvm_add_library(${name} ${ARGN})
265 endif()
266 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
267
Nick Lewycky61aed872011-04-29 02:12:06 +0000268 if( EXCLUDE_FROM_ALL )
269 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
270 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000271 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
272 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000273 EXPORT LLVMExports
Hans Wennborg16546272013-08-24 00:20:36 +0000274 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
275 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
276 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000277 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000278 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000279 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000280
281 # Add the explicit dependency information for this library.
282 #
283 # It would be nice to verify that we have the dependencies for this library
284 # name, but using get_property(... SET) doesn't suffice to determine if a
285 # property has been set to an empty value.
286 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
287 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000288endmacro(add_llvm_library name)
289
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000290macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000291 llvm_add_library(${name} MODULE ${ARGN})
292 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000293 # Add empty "phony" target
294 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000295 else()
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 Fuentesa229b3c2008-09-22 01:08:49 +0000333endmacro(add_llvm_executable name)
334
335
Hans Wennborg16546272013-08-24 00:20:36 +0000336set (LLVM_TOOLCHAIN_TOOLS
337 llvm-ar
338 llvm-objdump
339 )
340
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000341macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000342 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000343 set(EXCLUDE_FROM_ALL ON)
344 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000345 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000346
347 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
348 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
349 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000350 install(TARGETS ${name}
351 EXPORT LLVMExports
352 RUNTIME DESTINATION bin)
Hans Wennborg16546272013-08-24 00:20:36 +0000353 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000354 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000355 if( LLVM_BUILD_TOOLS )
356 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
357 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000358 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000359endmacro(add_llvm_tool name)
360
361
362macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000363 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000364 set(EXCLUDE_FROM_ALL ON)
365 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000366 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000367 if( LLVM_BUILD_EXAMPLES )
368 install(TARGETS ${name} RUNTIME DESTINATION examples)
369 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000370 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000371endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000372
373
Oscar Fuentes3145e922011-02-20 22:06:10 +0000374macro(add_llvm_utility name)
375 add_llvm_executable(${name} ${ARGN})
376 set_target_properties(${name} PROPERTIES FOLDER "Utils")
377endmacro(add_llvm_utility name)
378
379
Oscar Fuentescdc95492008-09-26 04:40:32 +0000380macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000381 include_directories(BEFORE
382 ${CMAKE_CURRENT_BINARY_DIR}
383 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000384 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000385 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000386endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000387
388# Add external project that may want to be built as part of llvm such as Clang,
389# lld, and Polly. This adds two options. One for the source directory of the
390# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000391# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000392# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000393macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000394 set(add_llvm_external_dir "${ARGN}")
395 if("${add_llvm_external_dir}" STREQUAL "")
396 set(add_llvm_external_dir ${name})
397 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000398 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000399 string(REPLACE "-" "_" nameUNDERSCORE ${name})
400 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
401 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000402 CACHE PATH "Path to ${name} source directory")
403 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
404 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
405 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
406 "Whether to build ${name} as part of LLVM" ON)
407 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000408 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000409 endif()
410 endif()
411endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000412
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000413macro(add_llvm_tool_subdirectory name)
414 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
415 add_subdirectory(${name})
416endmacro(add_llvm_tool_subdirectory)
417
418macro(ignore_llvm_tool_subdirectory name)
419 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
420endmacro(ignore_llvm_tool_subdirectory)
421
422function(add_llvm_implicit_external_projects)
423 set(list_of_implicit_subdirs "")
424 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
425 foreach(dir ${sub-dirs})
426 if(IS_DIRECTORY "${dir}")
427 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
428 if( tool_subdir_ignore EQUAL -1
429 AND EXISTS "${dir}/CMakeLists.txt")
430 get_filename_component(fn "${dir}" NAME)
431 list(APPEND list_of_implicit_subdirs "${fn}")
432 endif()
433 endif()
434 endforeach()
435
436 foreach(external_proj ${list_of_implicit_subdirs})
437 add_llvm_external_project("${external_proj}")
438 endforeach()
439endfunction(add_llvm_implicit_external_projects)
440
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000441# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000442function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000443 if( NOT LLVM_BUILD_TESTS )
444 set(EXCLUDE_FROM_ALL ON)
445 endif()
446
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000447 # Visual Studio 2012 only supports up to 8 template parameters in
448 # std::tr1::tuple by default, but gtest requires 10
449 if (MSVC AND MSVC_VERSION EQUAL 1700)
450 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
451 endif ()
452
453 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
454 if (NOT LLVM_ENABLE_THREADS)
455 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
456 endif ()
457
458 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000459 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000460 endif ()
461
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000462 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000463
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000464 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000465 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
466 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000467 target_link_libraries(${test_name}
468 gtest
469 gtest_main
470 LLVMSupport # gtest needs it for raw_ostream.
471 )
472
473 add_dependencies(${test_suite} ${test_name})
474 get_target_property(test_suite_folder ${test_suite} FOLDER)
475 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
476 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
477 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000478endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000479
480# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000481# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000482# variables needed for the 'lit.site.cfg' files. This function bundles the
483# common variables that any Lit instance is likely to need, and custom
484# variables can be passed in.
485function(configure_lit_site_cfg input output)
486 foreach(c ${LLVM_TARGETS_TO_BUILD})
487 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
488 endforeach(c)
489 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
490
491 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000492
493 if(BUILD_SHARED_LIBS)
494 set(LLVM_SHARED_LIBS_ENABLED "1")
495 else()
496 set(LLVM_SHARED_LIBS_ENABLED "0")
497 endif(BUILD_SHARED_LIBS)
498
499 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
500 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
501 else() # Default for all other unix like systems.
502 # CMake hardcodes the library locaction using rpath.
503 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
504 # build dir. We pass it anyways.
505 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
506 endif()
507
508 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000509 if (CMAKE_CFG_INTDIR STREQUAL ".")
510 set(LLVM_BUILD_MODE ".")
511 else ()
512 set(LLVM_BUILD_MODE "%(build_mode)s")
513 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000514
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000515 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000516 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
517 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000518 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
519 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000520
521 # SHLIBDIR points the build tree.
522 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
523
Chandler Carruth3511dd32012-06-28 06:36:24 +0000524 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
525 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
526 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
527
528 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
529 set(ENABLE_ASSERTIONS "1")
530 else()
531 set(ENABLE_ASSERTIONS "0")
532 endif()
533
Tim Northoverbfe84682013-02-14 16:49:32 +0000534 set(HOST_OS ${CMAKE_SYSTEM_NAME})
535 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000536
Roman Divackyc9871112013-08-27 19:25:01 +0000537 if (CLANG_ENABLE_ARCMT)
538 set(ENABLE_CLANG_ARCMT "1")
539 else()
540 set(ENABLE_CLANG_ARCMT "0")
541 endif()
542 if (CLANG_ENABLE_REWRITER)
543 set(ENABLE_CLANG_REWRITER "1")
544 else()
545 set(ENABLE_CLANG_REWRITER "0")
546 endif()
547 if (CLANG_ENABLE_STATIC_ANALYZER)
548 set(ENABLE_CLANG_STATIC_ANALYZER "1")
549 else()
550 set(ENABLE_CLANG_STATIC_ANALYZER "0")
551 endif()
552
Chandler Carruth3511dd32012-06-28 06:36:24 +0000553 configure_file(${input} ${output} @ONLY)
554endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000555
556# A raw function to create a lit target. This is used to implement the testuite
557# management functions.
558function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000559 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000560 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
561 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000562 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
563 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
564 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000565 set(LIT_COMMAND
566 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000567 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000568 ${LIT_ARGS}
569 )
570 foreach(param ${ARG_PARAMS})
571 list(APPEND LIT_COMMAND --param ${param})
572 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000573 if( ARG_DEPENDS )
574 add_custom_target(${target}
575 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
576 COMMENT "${comment}"
577 )
578 add_dependencies(${target} ${ARG_DEPENDS})
579 else()
580 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000581 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000582 message(STATUS "${target} does nothing.")
583 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000584
585 # Tests should be excluded from "Build Solution".
586 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000587endfunction()
588
589# A function to add a set of lit test suites to be driven through 'check-*' targets.
590function(add_lit_testsuite target comment)
591 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
592
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000593 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
594 if(NOT EXCLUDE_FROM_ALL)
595 # Register the testsuites, params and depends for the global check rule.
596 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
597 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
598 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
599 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
600 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000601
602 # Produce a specific suffixed check rule.
603 add_lit_target(${target} ${comment}
604 ${ARG_DEFAULT_ARGS}
605 PARAMS ${ARG_PARAMS}
606 DEPENDS ${ARG_DEPENDS}
607 ARGS ${ARG_ARGS}
608 )
609endfunction()