blob: 5bc3c22cf893404f3ef9d56e941a4fec7454f8ca [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)
6 get_property(target_compile_flags TARGET ${name} PROPERTY COMPILE_FLAGS)
7 if(NOT "${LLVM_COMPILE_FLAGS}" STREQUAL "")
8 set(target_compile_flags "${target_compile_flags} ${LLVM_COMPILE_FLAGS}")
9 endif()
10 if(LLVM_NO_RTTI)
11 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
12 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
13 set(target_compile_flags "${target_compile_flags} -fno-rtti")
14 elseif (MSVC)
15 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
16 endif ()
17 endif()
18
19 set_property(TARGET ${name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
20 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
21endfunction()
22
Nico Weberc27118d2013-12-28 23:31:44 +000023function(add_llvm_symbol_exports target_name export_file)
24 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000025 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000026 add_custom_command(OUTPUT ${native_export_file}
27 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000028 DEPENDS ${export_file}
29 VERBATIM
30 COMMENT "Creating export file for ${target_name}")
31 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000032 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000033 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
34 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000035 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000036 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000037 add_custom_command(OUTPUT ${native_export_file}
38 COMMAND echo "{" > ${native_export_file}
39 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
40 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
41 COMMAND echo " local: *;" >> ${native_export_file}
42 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000043 DEPENDS ${export_file}
44 VERBATIM
45 COMMENT "Creating export file for ${target_name}")
46 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000047 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000048 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000049 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000050
51 set(CAT "type")
52 if(CYGWIN)
53 set(CAT "cat")
54 endif()
55
Nico Weber5c8a4a32013-12-29 04:05:23 +000056 # Using ${export_file} in add_custom_command directly confuses cmd.exe.
57 file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
58
NAKAMURA Takumi81210752013-12-29 16:15:18 +000059 add_custom_command(OUTPUT ${native_export_file}
60 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
61 COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000062 DEPENDS ${export_file}
63 VERBATIM
64 COMMENT "Creating export file for ${target_name}")
Nico Weber49d6f482013-12-29 07:43:09 +000065 if(CYGWIN OR MINGW)
Nico Weber49da7582013-12-29 06:56:28 +000066 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000067 LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weber49da7582013-12-29 06:56:28 +000068 else()
69 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000070 LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +000071 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000072 endif()
73
74 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +000075 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +000076
77 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
78 foreach(src ${srcs})
79 get_filename_component(extension ${src} EXT)
80 if(extension STREQUAL ".cpp")
81 set(first_source_file ${src})
82 break()
83 endif()
84 endforeach()
85
86 # Force re-linking when the exports file changes. Actually, it
87 # forces recompilation of the source file. The LINK_DEPENDS target
88 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +000089 # FIXME: This is not safe because this will create the same target
90 # ${native_export_file} in several different file:
91 # - One where we emitted ${target_name}_exports
92 # - One where we emitted the build command for the following object.
93 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
94 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +000095
96 set_property(DIRECTORY APPEND
97 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
98
99 add_dependencies(${target_name} ${target_name}_exports)
100endfunction(add_llvm_symbol_exports)
101
Nico Weber62588e12013-12-30 03:36:05 +0000102function(add_dead_strip target_name)
Nico Weber62588e12013-12-30 03:36:05 +0000103 if(NOT LLVM_NO_DEAD_STRIP)
104 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
105 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
106 LINK_FLAGS " -Wl,-dead_strip")
107 elseif(NOT WIN32)
NAKAMURA Takumibb50bce2014-01-28 09:43:55 +0000108 # Object files are compiled with -ffunction-data-sections.
Nico Weber62588e12013-12-30 03:36:05 +0000109 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
110 LINK_FLAGS " -Wl,--gc-sections")
111 endif()
112 endif()
113endfunction(add_dead_strip)
114
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000115# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
116# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
117# or a certain builder, for eaxample, msbuild.exe, would be confused.
118function(set_output_directory target bindir libdir)
119 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
120 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
121 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
122 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
123 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
124 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
125 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
Douglas Gregorbbebd812014-01-02 19:07:19 +0000126 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000127 endforeach()
128 else()
129 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
130 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
131 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
132 endif()
133endfunction()
134
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000135macro(add_llvm_library name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000136 llvm_process_sources( ALL_FILES ${ARGN} )
137 add_library( ${name} ${ALL_FILES} )
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000138 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes5ed96262011-02-18 22:06:14 +0000139 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Nico Weber62588e12013-12-30 03:36:05 +0000140 add_dead_strip( ${name} )
Oscar Fuentes8807bdd2008-09-22 18:21:51 +0000141 if( LLVM_COMMON_DEPENDS )
142 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
143 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000144
145 if( BUILD_SHARED_LIBS )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000146 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000147 if (MSVC)
148 set_target_properties(${name}
149 PROPERTIES
150 IMPORT_SUFFIX ".imp")
151 endif ()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000152
Nico Weber06473f82013-12-29 05:39:01 +0000153 if (LLVM_EXPORTED_SYMBOL_FILE)
154 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
155 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000156 endif()
157
Oscar Fuentes978e5282011-03-29 20:51:08 +0000158 # Ensure that the system libraries always comes last on the
159 # list. Without this, linking the unit tests on MinGW fails.
160 link_system_libs( ${name} )
161
Nick Lewycky61aed872011-04-29 02:12:06 +0000162 if( EXCLUDE_FROM_ALL )
163 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
164 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000165 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
166 install(TARGETS ${name}
167 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
168 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
169 endif()
Nick Lewycky61aed872011-04-29 02:12:06 +0000170 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000171 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000172
173 # Add the explicit dependency information for this library.
174 #
175 # It would be nice to verify that we have the dependencies for this library
176 # name, but using get_property(... SET) doesn't suffice to determine if a
177 # property has been set to an empty value.
178 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
179 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000180endmacro(add_llvm_library name)
181
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000182macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000183 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000184 message(STATUS "Loadable modules not supported on this platform.
185${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000186 # Add empty "phony" target
187 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000188 else()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000189 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes9f2b3842010-12-22 08:30:17 +0000190 if (MODULE)
191 set(libkind MODULE)
192 else()
193 set(libkind SHARED)
194 endif()
195
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000196 add_library( ${name} ${libkind} ${ALL_FILES} )
Jordan Rose353bdcd2014-01-02 19:47:45 +0000197 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000198 set_target_properties( ${name} PROPERTIES PREFIX "" )
Nico Weber62588e12013-12-30 03:36:05 +0000199 add_dead_strip( ${name} )
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000200
Nico Weberc27118d2013-12-28 23:31:44 +0000201 if (LLVM_EXPORTED_SYMBOL_FILE)
202 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
203 endif(LLVM_EXPORTED_SYMBOL_FILE)
204
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000205 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000206 link_system_libs( ${name} )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000207
Douglas Gregor66df54f2009-11-10 15:30:33 +0000208 if (APPLE)
209 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000210 set_property(TARGET ${name} APPEND_STRING PROPERTY
211 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000212 endif()
213
Alp Tokerc0b7bb52014-01-08 11:10:24 +0000214 if (MODULE)
215 set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
216 endif ()
217
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000218 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000219 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000220 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000221 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
222 install(TARGETS ${name}
223 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
224 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
225 endif()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000226 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000227 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000228
229 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000230endmacro(add_llvm_loadable_module name)
231
232
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000233macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000234 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000235 if( EXCLUDE_FROM_ALL )
236 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
237 else()
238 add_executable(${name} ${ALL_FILES})
239 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000240 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000241
242 if (LLVM_EXPORTED_SYMBOL_FILE)
243 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
244 endif(LLVM_EXPORTED_SYMBOL_FILE)
245
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000246 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000247 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000248 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000249 if( LLVM_COMMON_DEPENDS )
250 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
251 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000252 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000253endmacro(add_llvm_executable name)
254
255
Hans Wennborg16546272013-08-24 00:20:36 +0000256set (LLVM_TOOLCHAIN_TOOLS
257 llvm-ar
258 llvm-objdump
259 )
260
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000261macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000262 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000263 set(EXCLUDE_FROM_ALL ON)
264 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000265 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000266
267 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
268 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
269 if( LLVM_BUILD_TOOLS )
270 install(TARGETS ${name} RUNTIME DESTINATION bin)
271 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000272 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000273 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000274endmacro(add_llvm_tool name)
275
276
277macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000278 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000279 set(EXCLUDE_FROM_ALL ON)
280 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000281 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000282 if( LLVM_BUILD_EXAMPLES )
283 install(TARGETS ${name} RUNTIME DESTINATION examples)
284 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000285 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000286endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000287
288
Oscar Fuentes3145e922011-02-20 22:06:10 +0000289macro(add_llvm_utility name)
290 add_llvm_executable(${name} ${ARGN})
291 set_target_properties(${name} PROPERTIES FOLDER "Utils")
292endmacro(add_llvm_utility name)
293
294
Oscar Fuentescdc95492008-09-26 04:40:32 +0000295macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000296 include_directories(BEFORE
297 ${CMAKE_CURRENT_BINARY_DIR}
298 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000299 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000300 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000301endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000302
303# Add external project that may want to be built as part of llvm such as Clang,
304# lld, and Polly. This adds two options. One for the source directory of the
305# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000306# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000307# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000308macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000309 set(add_llvm_external_dir "${ARGN}")
310 if("${add_llvm_external_dir}" STREQUAL "")
311 set(add_llvm_external_dir ${name})
312 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000313 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000314 string(REPLACE "-" "_" nameUNDERSCORE ${name})
315 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
316 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000317 CACHE PATH "Path to ${name} source directory")
318 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
319 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
320 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
321 "Whether to build ${name} as part of LLVM" ON)
322 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000323 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000324 endif()
325 endif()
326endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000327
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000328macro(add_llvm_tool_subdirectory name)
329 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
330 add_subdirectory(${name})
331endmacro(add_llvm_tool_subdirectory)
332
333macro(ignore_llvm_tool_subdirectory name)
334 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
335endmacro(ignore_llvm_tool_subdirectory)
336
337function(add_llvm_implicit_external_projects)
338 set(list_of_implicit_subdirs "")
339 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
340 foreach(dir ${sub-dirs})
341 if(IS_DIRECTORY "${dir}")
342 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
343 if( tool_subdir_ignore EQUAL -1
344 AND EXISTS "${dir}/CMakeLists.txt")
345 get_filename_component(fn "${dir}" NAME)
346 list(APPEND list_of_implicit_subdirs "${fn}")
347 endif()
348 endif()
349 endforeach()
350
351 foreach(external_proj ${list_of_implicit_subdirs})
352 add_llvm_external_project("${external_proj}")
353 endforeach()
354endfunction(add_llvm_implicit_external_projects)
355
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000356# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000357function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000358 if( NOT LLVM_BUILD_TESTS )
359 set(EXCLUDE_FROM_ALL ON)
360 endif()
361
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000362 # Visual Studio 2012 only supports up to 8 template parameters in
363 # std::tr1::tuple by default, but gtest requires 10
364 if (MSVC AND MSVC_VERSION EQUAL 1700)
365 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
366 endif ()
367
368 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
369 if (NOT LLVM_ENABLE_THREADS)
370 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
371 endif ()
372
373 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
374 set(LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
375 endif ()
376
377 set(LLVM_NO_RTTI ON)
378
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000379 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000380 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
381 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000382 target_link_libraries(${test_name}
383 gtest
384 gtest_main
385 LLVMSupport # gtest needs it for raw_ostream.
386 )
387
388 add_dependencies(${test_suite} ${test_name})
389 get_target_property(test_suite_folder ${test_suite} FOLDER)
390 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
391 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
392 endif ()
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000393 llvm_update_compile_flags(${test_name})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000394endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000395
396# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000397# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000398# variables needed for the 'lit.site.cfg' files. This function bundles the
399# common variables that any Lit instance is likely to need, and custom
400# variables can be passed in.
401function(configure_lit_site_cfg input output)
402 foreach(c ${LLVM_TARGETS_TO_BUILD})
403 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
404 endforeach(c)
405 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
406
407 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000408
409 if(BUILD_SHARED_LIBS)
410 set(LLVM_SHARED_LIBS_ENABLED "1")
411 else()
412 set(LLVM_SHARED_LIBS_ENABLED "0")
413 endif(BUILD_SHARED_LIBS)
414
415 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
416 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
417 else() # Default for all other unix like systems.
418 # CMake hardcodes the library locaction using rpath.
419 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
420 # build dir. We pass it anyways.
421 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
422 endif()
423
424 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000425 if (CMAKE_CFG_INTDIR STREQUAL ".")
426 set(LLVM_BUILD_MODE ".")
427 else ()
428 set(LLVM_BUILD_MODE "%(build_mode)s")
429 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000430
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000431 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000432 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
433 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000434 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
435 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000436
437 # SHLIBDIR points the build tree.
438 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
439
Chandler Carruth3511dd32012-06-28 06:36:24 +0000440 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
441 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
442 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
443
444 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
445 set(ENABLE_ASSERTIONS "1")
446 else()
447 set(ENABLE_ASSERTIONS "0")
448 endif()
449
Tim Northoverbfe84682013-02-14 16:49:32 +0000450 set(HOST_OS ${CMAKE_SYSTEM_NAME})
451 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000452
Roman Divackyc9871112013-08-27 19:25:01 +0000453 if (CLANG_ENABLE_ARCMT)
454 set(ENABLE_CLANG_ARCMT "1")
455 else()
456 set(ENABLE_CLANG_ARCMT "0")
457 endif()
458 if (CLANG_ENABLE_REWRITER)
459 set(ENABLE_CLANG_REWRITER "1")
460 else()
461 set(ENABLE_CLANG_REWRITER "0")
462 endif()
463 if (CLANG_ENABLE_STATIC_ANALYZER)
464 set(ENABLE_CLANG_STATIC_ANALYZER "1")
465 else()
466 set(ENABLE_CLANG_STATIC_ANALYZER "0")
467 endif()
468
Chandler Carruth3511dd32012-06-28 06:36:24 +0000469 configure_file(${input} ${output} @ONLY)
470endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000471
472# A raw function to create a lit target. This is used to implement the testuite
473# management functions.
474function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000475 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000476 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
477 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000478 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
479 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
480 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000481 set(LIT_COMMAND
482 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000483 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000484 ${LIT_ARGS}
485 )
486 foreach(param ${ARG_PARAMS})
487 list(APPEND LIT_COMMAND --param ${param})
488 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000489 if( ARG_DEPENDS )
490 add_custom_target(${target}
491 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
492 COMMENT "${comment}"
493 )
494 add_dependencies(${target} ${ARG_DEPENDS})
495 else()
496 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000497 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000498 message(STATUS "${target} does nothing.")
499 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000500
501 # Tests should be excluded from "Build Solution".
502 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000503endfunction()
504
505# A function to add a set of lit test suites to be driven through 'check-*' targets.
506function(add_lit_testsuite target comment)
507 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
508
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000509 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
510 if(NOT EXCLUDE_FROM_ALL)
511 # Register the testsuites, params and depends for the global check rule.
512 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
513 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
514 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
515 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
516 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000517
518 # Produce a specific suffixed check rule.
519 add_lit_target(${target} ${comment}
520 ${ARG_DEFAULT_ARGS}
521 PARAMS ${ARG_PARAMS}
522 DEPENDS ${ARG_DEPENDS}
523 ARGS ${ARG_ARGS}
524 )
525endfunction()