blob: 596c8c6dbb8fb0fa9e600725f6ccc33cb1c9fea3 [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
Nico Weberc27118d2013-12-28 23:31:44 +00005function(add_llvm_symbol_exports target_name export_file)
6 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +00007 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +00008 add_custom_command(OUTPUT ${native_export_file}
9 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000010 DEPENDS ${export_file}
11 VERBATIM
12 COMMENT "Creating export file for ${target_name}")
13 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000014 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000015 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
16 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000017 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000018 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000019 add_custom_command(OUTPUT ${native_export_file}
20 COMMAND echo "{" > ${native_export_file}
21 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
22 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
23 COMMAND echo " local: *;" >> ${native_export_file}
24 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000025 DEPENDS ${export_file}
26 VERBATIM
27 COMMENT "Creating export file for ${target_name}")
28 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000029 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000030 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000031 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000032
33 set(CAT "type")
34 if(CYGWIN)
35 set(CAT "cat")
36 endif()
37
Nico Weber5c8a4a32013-12-29 04:05:23 +000038 # Using ${export_file} in add_custom_command directly confuses cmd.exe.
39 file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
40
NAKAMURA Takumi81210752013-12-29 16:15:18 +000041 add_custom_command(OUTPUT ${native_export_file}
42 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
43 COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000044 DEPENDS ${export_file}
45 VERBATIM
46 COMMENT "Creating export file for ${target_name}")
Nico Weber49d6f482013-12-29 07:43:09 +000047 if(CYGWIN OR MINGW)
Nico Weber49da7582013-12-29 06:56:28 +000048 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000049 LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weber49da7582013-12-29 06:56:28 +000050 else()
51 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000052 LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +000053 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000054 endif()
55
56 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
57
58 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
59 foreach(src ${srcs})
60 get_filename_component(extension ${src} EXT)
61 if(extension STREQUAL ".cpp")
62 set(first_source_file ${src})
63 break()
64 endif()
65 endforeach()
66
67 # Force re-linking when the exports file changes. Actually, it
68 # forces recompilation of the source file. The LINK_DEPENDS target
69 # property only works for makefile-based generators.
70 set_property(SOURCE ${first_source_file} APPEND PROPERTY
71 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
72
73 set_property(DIRECTORY APPEND
74 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
75
76 add_dependencies(${target_name} ${target_name}_exports)
77endfunction(add_llvm_symbol_exports)
78
Nico Weber62588e12013-12-30 03:36:05 +000079function(add_dead_strip target_name)
Yaron Keren03759bd2013-12-30 05:31:53 +000080 if(NOT CYGWIN AND NOT MINGW AND NOT MSVC)
Nico Weber62588e12013-12-30 03:36:05 +000081 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
82 SET(CMAKE_CXX_FLAGS
83 "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections"
84 PARENT_SCOPE)
85 endif()
86 endif()
87 if(NOT LLVM_NO_DEAD_STRIP)
88 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
89 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
90 LINK_FLAGS " -Wl,-dead_strip")
91 elseif(NOT WIN32)
92 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
93 LINK_FLAGS " -Wl,--gc-sections")
94 endif()
95 endif()
96endfunction(add_dead_strip)
97
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +000098# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
99# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
100# or a certain builder, for eaxample, msbuild.exe, would be confused.
101function(set_output_directory target bindir libdir)
102 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
103 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
104 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
105 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
106 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
107 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
108 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
Douglas Gregorbbebd812014-01-02 19:07:19 +0000109 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000110 endforeach()
111 else()
112 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
113 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
114 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
115 endif()
116endfunction()
117
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000118macro(add_llvm_library name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000119 llvm_process_sources( ALL_FILES ${ARGN} )
120 add_library( ${name} ${ALL_FILES} )
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000121 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes5ed96262011-02-18 22:06:14 +0000122 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Nico Weber62588e12013-12-30 03:36:05 +0000123 add_dead_strip( ${name} )
Oscar Fuentes8807bdd2008-09-22 18:21:51 +0000124 if( LLVM_COMMON_DEPENDS )
125 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
126 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000127
128 if( BUILD_SHARED_LIBS )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000129 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000130 if (MSVC)
131 set_target_properties(${name}
132 PROPERTIES
133 IMPORT_SUFFIX ".imp")
134 endif ()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000135
Nico Weber06473f82013-12-29 05:39:01 +0000136 if (LLVM_EXPORTED_SYMBOL_FILE)
137 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
138 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000139 endif()
140
Oscar Fuentes978e5282011-03-29 20:51:08 +0000141 # Ensure that the system libraries always comes last on the
142 # list. Without this, linking the unit tests on MinGW fails.
143 link_system_libs( ${name} )
144
Nick Lewycky61aed872011-04-29 02:12:06 +0000145 if( EXCLUDE_FROM_ALL )
146 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
147 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000148 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
149 install(TARGETS ${name}
150 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
151 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
152 endif()
Nick Lewycky61aed872011-04-29 02:12:06 +0000153 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000154 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000155
156 # Add the explicit dependency information for this library.
157 #
158 # It would be nice to verify that we have the dependencies for this library
159 # name, but using get_property(... SET) doesn't suffice to determine if a
160 # property has been set to an empty value.
161 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
162 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000163endmacro(add_llvm_library name)
164
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000165macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000166 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000167 message(STATUS "Loadable modules not supported on this platform.
168${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000169 # Add empty "phony" target
170 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000171 else()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000172 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes9f2b3842010-12-22 08:30:17 +0000173 if (MODULE)
174 set(libkind MODULE)
175 else()
176 set(libkind SHARED)
177 endif()
178
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000179 add_library( ${name} ${libkind} ${ALL_FILES} )
180 set_target_properties( ${name} PROPERTIES PREFIX "" )
Nico Weber62588e12013-12-30 03:36:05 +0000181 add_dead_strip( ${name} )
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000182
Nico Weberc27118d2013-12-28 23:31:44 +0000183 if (LLVM_EXPORTED_SYMBOL_FILE)
184 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
185 endif(LLVM_EXPORTED_SYMBOL_FILE)
186
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000187 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000188 link_system_libs( ${name} )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000189
Douglas Gregor66df54f2009-11-10 15:30:33 +0000190 if (APPLE)
191 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000192 set_property(TARGET ${name} APPEND_STRING PROPERTY
193 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000194 endif()
195
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000196 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000197 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000198 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000199 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
200 install(TARGETS ${name}
201 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
202 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
203 endif()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000204 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000205 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000206
207 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000208endmacro(add_llvm_loadable_module name)
209
210
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000211macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000212 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000213 if( EXCLUDE_FROM_ALL )
214 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
215 else()
216 add_executable(${name} ${ALL_FILES})
217 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000218 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000219
220 if (LLVM_EXPORTED_SYMBOL_FILE)
221 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
222 endif(LLVM_EXPORTED_SYMBOL_FILE)
223
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000224 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000225 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000226 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000227 if( LLVM_COMMON_DEPENDS )
228 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
229 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000230 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000231endmacro(add_llvm_executable name)
232
233
Hans Wennborg16546272013-08-24 00:20:36 +0000234set (LLVM_TOOLCHAIN_TOOLS
235 llvm-ar
236 llvm-objdump
237 )
238
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000239macro(add_llvm_tool name)
240 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000241 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000242 set(EXCLUDE_FROM_ALL ON)
243 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000244 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000245
246 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
247 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
248 if( LLVM_BUILD_TOOLS )
249 install(TARGETS ${name} RUNTIME DESTINATION bin)
250 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000251 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000252 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000253endmacro(add_llvm_tool name)
254
255
256macro(add_llvm_example name)
257# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000258 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000259 set(EXCLUDE_FROM_ALL ON)
260 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000261 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000262 if( LLVM_BUILD_EXAMPLES )
263 install(TARGETS ${name} RUNTIME DESTINATION examples)
264 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000265 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000266endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000267
268
Oscar Fuentes3145e922011-02-20 22:06:10 +0000269macro(add_llvm_utility name)
270 add_llvm_executable(${name} ${ARGN})
271 set_target_properties(${name} PROPERTIES FOLDER "Utils")
272endmacro(add_llvm_utility name)
273
274
Oscar Fuentescdc95492008-09-26 04:40:32 +0000275macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000276 include_directories(BEFORE
277 ${CMAKE_CURRENT_BINARY_DIR}
278 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000279 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000280 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000281endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000282
283# Add external project that may want to be built as part of llvm such as Clang,
284# lld, and Polly. This adds two options. One for the source directory of the
285# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000286# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000287# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000288macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000289 set(add_llvm_external_dir "${ARGN}")
290 if("${add_llvm_external_dir}" STREQUAL "")
291 set(add_llvm_external_dir ${name})
292 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000293 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000294 string(REPLACE "-" "_" nameUNDERSCORE ${name})
295 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
296 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000297 CACHE PATH "Path to ${name} source directory")
298 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
299 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
300 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
301 "Whether to build ${name} as part of LLVM" ON)
302 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000303 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000304 endif()
305 endif()
306endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000307
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000308macro(add_llvm_tool_subdirectory name)
309 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
310 add_subdirectory(${name})
311endmacro(add_llvm_tool_subdirectory)
312
313macro(ignore_llvm_tool_subdirectory name)
314 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
315endmacro(ignore_llvm_tool_subdirectory)
316
317function(add_llvm_implicit_external_projects)
318 set(list_of_implicit_subdirs "")
319 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
320 foreach(dir ${sub-dirs})
321 if(IS_DIRECTORY "${dir}")
322 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
323 if( tool_subdir_ignore EQUAL -1
324 AND EXISTS "${dir}/CMakeLists.txt")
325 get_filename_component(fn "${dir}" NAME)
326 list(APPEND list_of_implicit_subdirs "${fn}")
327 endif()
328 endif()
329 endforeach()
330
331 foreach(external_proj ${list_of_implicit_subdirs})
332 add_llvm_external_project("${external_proj}")
333 endforeach()
334endfunction(add_llvm_implicit_external_projects)
335
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000336# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000337function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000338 if( NOT LLVM_BUILD_TESTS )
339 set(EXCLUDE_FROM_ALL ON)
340 endif()
341
342 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000343 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
344 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000345 target_link_libraries(${test_name}
346 gtest
347 gtest_main
348 LLVMSupport # gtest needs it for raw_ostream.
349 )
350
351 add_dependencies(${test_suite} ${test_name})
352 get_target_property(test_suite_folder ${test_suite} FOLDER)
353 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
354 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
355 endif ()
356
357 # Visual Studio 2012 only supports up to 8 template parameters in
358 # std::tr1::tuple by default, but gtest requires 10
359 if (MSVC AND MSVC_VERSION EQUAL 1700)
360 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
361 endif ()
362
363 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
364 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000365 if (NOT LLVM_ENABLE_THREADS)
366 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
367 endif ()
368
Chandler Carruth582e8a52012-06-21 18:44:24 +0000369 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
370 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
371 set(target_compile_flags "${target_compile_flags} -fno-rtti")
372 elseif (MSVC)
Aaron Ballmanbde2cd12013-08-06 23:34:04 +0000373 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000374 endif ()
Chandler Carruth582e8a52012-06-21 18:44:24 +0000375
376 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
377 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
378 endif ()
379 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000380endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000381
382# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000383# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000384# variables needed for the 'lit.site.cfg' files. This function bundles the
385# common variables that any Lit instance is likely to need, and custom
386# variables can be passed in.
387function(configure_lit_site_cfg input output)
388 foreach(c ${LLVM_TARGETS_TO_BUILD})
389 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
390 endforeach(c)
391 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
392
393 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
NAKAMURA Takumie73df852013-12-16 16:03:08 +0000394 set(SHLIBDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000395
396 if(BUILD_SHARED_LIBS)
397 set(LLVM_SHARED_LIBS_ENABLED "1")
398 else()
399 set(LLVM_SHARED_LIBS_ENABLED "0")
400 endif(BUILD_SHARED_LIBS)
401
402 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
403 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
404 else() # Default for all other unix like systems.
405 # CMake hardcodes the library locaction using rpath.
406 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
407 # build dir. We pass it anyways.
408 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
409 endif()
410
411 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000412 if (CMAKE_CFG_INTDIR STREQUAL ".")
413 set(LLVM_BUILD_MODE ".")
414 else ()
415 set(LLVM_BUILD_MODE "%(build_mode)s")
416 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000417
418 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
419 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumie73df852013-12-16 16:03:08 +0000420 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
421 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000422 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
423 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
424 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
425
426 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
427 set(ENABLE_ASSERTIONS "1")
428 else()
429 set(ENABLE_ASSERTIONS "0")
430 endif()
431
Tim Northoverbfe84682013-02-14 16:49:32 +0000432 set(HOST_OS ${CMAKE_SYSTEM_NAME})
433 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000434
Roman Divackyc9871112013-08-27 19:25:01 +0000435 if (CLANG_ENABLE_ARCMT)
436 set(ENABLE_CLANG_ARCMT "1")
437 else()
438 set(ENABLE_CLANG_ARCMT "0")
439 endif()
440 if (CLANG_ENABLE_REWRITER)
441 set(ENABLE_CLANG_REWRITER "1")
442 else()
443 set(ENABLE_CLANG_REWRITER "0")
444 endif()
445 if (CLANG_ENABLE_STATIC_ANALYZER)
446 set(ENABLE_CLANG_STATIC_ANALYZER "1")
447 else()
448 set(ENABLE_CLANG_STATIC_ANALYZER "0")
449 endif()
450
Chandler Carruth3511dd32012-06-28 06:36:24 +0000451 configure_file(${input} ${output} @ONLY)
452endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000453
454# A raw function to create a lit target. This is used to implement the testuite
455# management functions.
456function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000457 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000458 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
459 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000460 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
461 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
462 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000463 set(LIT_COMMAND
464 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000465 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000466 ${LIT_ARGS}
467 )
468 foreach(param ${ARG_PARAMS})
469 list(APPEND LIT_COMMAND --param ${param})
470 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000471 if( ARG_DEPENDS )
472 add_custom_target(${target}
473 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
474 COMMENT "${comment}"
475 )
476 add_dependencies(${target} ${ARG_DEPENDS})
477 else()
478 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000479 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000480 message(STATUS "${target} does nothing.")
481 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000482
483 # Tests should be excluded from "Build Solution".
484 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000485endfunction()
486
487# A function to add a set of lit test suites to be driven through 'check-*' targets.
488function(add_lit_testsuite target comment)
489 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
490
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000491 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
492 if(NOT EXCLUDE_FROM_ALL)
493 # Register the testsuites, params and depends for the global check rule.
494 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
495 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
496 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
497 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
498 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000499
500 # Produce a specific suffixed check rule.
501 add_lit_target(${target} ${comment}
502 ${ARG_DEFAULT_ARGS}
503 PARAMS ${ARG_PARAMS}
504 DEPENDS ${ARG_DEPENDS}
505 ARGS ${ARG_ARGS}
506 )
507endfunction()