blob: 6840cfca81fad270080e5baa68daa4cd1d725e5b [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)
80 if(NOT CYGWIN AND NOT MINGW)
81 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
Oscar Fuentesa229b3c2008-09-22 01:08:49 +000098macro(add_llvm_library name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +000099 llvm_process_sources( ALL_FILES ${ARGN} )
100 add_library( ${name} ${ALL_FILES} )
Oscar Fuentes5ed96262011-02-18 22:06:14 +0000101 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Nico Weber62588e12013-12-30 03:36:05 +0000102 add_dead_strip( ${name} )
Oscar Fuentes8807bdd2008-09-22 18:21:51 +0000103 if( LLVM_COMMON_DEPENDS )
104 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
105 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000106
107 if( BUILD_SHARED_LIBS )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000108 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000109 if (MSVC)
110 set_target_properties(${name}
111 PROPERTIES
112 IMPORT_SUFFIX ".imp")
113 endif ()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000114
Nico Weber06473f82013-12-29 05:39:01 +0000115 if (LLVM_EXPORTED_SYMBOL_FILE)
116 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
117 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000118 endif()
119
Oscar Fuentes978e5282011-03-29 20:51:08 +0000120 # Ensure that the system libraries always comes last on the
121 # list. Without this, linking the unit tests on MinGW fails.
122 link_system_libs( ${name} )
123
Nick Lewycky61aed872011-04-29 02:12:06 +0000124 if( EXCLUDE_FROM_ALL )
125 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
126 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000127 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
128 install(TARGETS ${name}
129 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
130 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
131 endif()
Nick Lewycky61aed872011-04-29 02:12:06 +0000132 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000133 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000134
135 # Add the explicit dependency information for this library.
136 #
137 # It would be nice to verify that we have the dependencies for this library
138 # name, but using get_property(... SET) doesn't suffice to determine if a
139 # property has been set to an empty value.
140 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
141 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000142endmacro(add_llvm_library name)
143
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000144macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000145 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000146 message(STATUS "Loadable modules not supported on this platform.
147${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000148 # Add empty "phony" target
149 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000150 else()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000151 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes9f2b3842010-12-22 08:30:17 +0000152 if (MODULE)
153 set(libkind MODULE)
154 else()
155 set(libkind SHARED)
156 endif()
157
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000158 add_library( ${name} ${libkind} ${ALL_FILES} )
159 set_target_properties( ${name} PROPERTIES PREFIX "" )
Nico Weber62588e12013-12-30 03:36:05 +0000160 add_dead_strip( ${name} )
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000161
Nico Weberc27118d2013-12-28 23:31:44 +0000162 if (LLVM_EXPORTED_SYMBOL_FILE)
163 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
164 endif(LLVM_EXPORTED_SYMBOL_FILE)
165
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000166 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000167 link_system_libs( ${name} )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000168
Douglas Gregor66df54f2009-11-10 15:30:33 +0000169 if (APPLE)
170 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000171 set_property(TARGET ${name} APPEND_STRING PROPERTY
172 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000173 endif()
174
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000175 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000176 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000177 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000178 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
179 install(TARGETS ${name}
180 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
181 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
182 endif()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000183 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000184 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000185
186 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000187endmacro(add_llvm_loadable_module name)
188
189
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000190macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000191 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000192 if( EXCLUDE_FROM_ALL )
193 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
194 else()
195 add_executable(${name} ${ALL_FILES})
196 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000197 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000198
199 if (LLVM_EXPORTED_SYMBOL_FILE)
200 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
201 endif(LLVM_EXPORTED_SYMBOL_FILE)
202
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000203 set(EXCLUDE_FROM_ALL OFF)
Oscar Fuentes978e5282011-03-29 20:51:08 +0000204 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000205 if( LLVM_COMMON_DEPENDS )
206 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
207 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000208 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000209endmacro(add_llvm_executable name)
210
211
Hans Wennborg16546272013-08-24 00:20:36 +0000212set (LLVM_TOOLCHAIN_TOOLS
213 llvm-ar
214 llvm-objdump
215 )
216
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000217macro(add_llvm_tool name)
218 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000219 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000220 set(EXCLUDE_FROM_ALL ON)
221 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000222 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000223
224 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
225 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
226 if( LLVM_BUILD_TOOLS )
227 install(TARGETS ${name} RUNTIME DESTINATION bin)
228 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000229 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000230 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000231endmacro(add_llvm_tool name)
232
233
234macro(add_llvm_example name)
235# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000236 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000237 set(EXCLUDE_FROM_ALL ON)
238 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000239 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000240 if( LLVM_BUILD_EXAMPLES )
241 install(TARGETS ${name} RUNTIME DESTINATION examples)
242 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000243 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000244endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000245
246
Oscar Fuentes3145e922011-02-20 22:06:10 +0000247macro(add_llvm_utility name)
248 add_llvm_executable(${name} ${ARGN})
249 set_target_properties(${name} PROPERTIES FOLDER "Utils")
250endmacro(add_llvm_utility name)
251
252
Oscar Fuentescdc95492008-09-26 04:40:32 +0000253macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000254 include_directories(BEFORE
255 ${CMAKE_CURRENT_BINARY_DIR}
256 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000257 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000258 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000259endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000260
261# Add external project that may want to be built as part of llvm such as Clang,
262# lld, and Polly. This adds two options. One for the source directory of the
263# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000264# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000265# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000266macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000267 set(add_llvm_external_dir "${ARGN}")
268 if("${add_llvm_external_dir}" STREQUAL "")
269 set(add_llvm_external_dir ${name})
270 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000271 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000272 string(REPLACE "-" "_" nameUNDERSCORE ${name})
273 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
274 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000275 CACHE PATH "Path to ${name} source directory")
276 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
277 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
278 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
279 "Whether to build ${name} as part of LLVM" ON)
280 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000281 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000282 endif()
283 endif()
284endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000285
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000286macro(add_llvm_tool_subdirectory name)
287 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
288 add_subdirectory(${name})
289endmacro(add_llvm_tool_subdirectory)
290
291macro(ignore_llvm_tool_subdirectory name)
292 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
293endmacro(ignore_llvm_tool_subdirectory)
294
295function(add_llvm_implicit_external_projects)
296 set(list_of_implicit_subdirs "")
297 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
298 foreach(dir ${sub-dirs})
299 if(IS_DIRECTORY "${dir}")
300 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
301 if( tool_subdir_ignore EQUAL -1
302 AND EXISTS "${dir}/CMakeLists.txt")
303 get_filename_component(fn "${dir}" NAME)
304 list(APPEND list_of_implicit_subdirs "${fn}")
305 endif()
306 endif()
307 endforeach()
308
309 foreach(external_proj ${list_of_implicit_subdirs})
310 add_llvm_external_project("${external_proj}")
311 endforeach()
312endfunction(add_llvm_implicit_external_projects)
313
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000314# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000315function(add_unittest test_suite test_name)
NAKAMURA Takumic42bdfc2013-01-27 12:46:53 +0000316 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000317 if( NOT LLVM_BUILD_TESTS )
318 set(EXCLUDE_FROM_ALL ON)
319 endif()
320
321 add_llvm_executable(${test_name} ${ARGN})
322 target_link_libraries(${test_name}
323 gtest
324 gtest_main
325 LLVMSupport # gtest needs it for raw_ostream.
326 )
327
328 add_dependencies(${test_suite} ${test_name})
329 get_target_property(test_suite_folder ${test_suite} FOLDER)
330 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
331 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
332 endif ()
333
334 # Visual Studio 2012 only supports up to 8 template parameters in
335 # std::tr1::tuple by default, but gtest requires 10
336 if (MSVC AND MSVC_VERSION EQUAL 1700)
337 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
338 endif ()
339
340 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
341 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000342 if (NOT LLVM_ENABLE_THREADS)
343 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
344 endif ()
345
Chandler Carruth582e8a52012-06-21 18:44:24 +0000346 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
347 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
348 set(target_compile_flags "${target_compile_flags} -fno-rtti")
349 elseif (MSVC)
Aaron Ballmanbde2cd12013-08-06 23:34:04 +0000350 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000351 endif ()
Chandler Carruth582e8a52012-06-21 18:44:24 +0000352
353 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
354 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
355 endif ()
356 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000357endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000358
359# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000360# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000361# variables needed for the 'lit.site.cfg' files. This function bundles the
362# common variables that any Lit instance is likely to need, and custom
363# variables can be passed in.
364function(configure_lit_site_cfg input output)
365 foreach(c ${LLVM_TARGETS_TO_BUILD})
366 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
367 endforeach(c)
368 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
369
370 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
NAKAMURA Takumie73df852013-12-16 16:03:08 +0000371 set(SHLIBDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000372
373 if(BUILD_SHARED_LIBS)
374 set(LLVM_SHARED_LIBS_ENABLED "1")
375 else()
376 set(LLVM_SHARED_LIBS_ENABLED "0")
377 endif(BUILD_SHARED_LIBS)
378
379 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
380 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
381 else() # Default for all other unix like systems.
382 # CMake hardcodes the library locaction using rpath.
383 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
384 # build dir. We pass it anyways.
385 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
386 endif()
387
388 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000389 if (CMAKE_CFG_INTDIR STREQUAL ".")
390 set(LLVM_BUILD_MODE ".")
391 else ()
392 set(LLVM_BUILD_MODE "%(build_mode)s")
393 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000394
395 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
396 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumie73df852013-12-16 16:03:08 +0000397 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
398 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000399 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
400 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
401 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
402
403 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
404 set(ENABLE_ASSERTIONS "1")
405 else()
406 set(ENABLE_ASSERTIONS "0")
407 endif()
408
Tim Northoverbfe84682013-02-14 16:49:32 +0000409 set(HOST_OS ${CMAKE_SYSTEM_NAME})
410 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000411
Roman Divackyc9871112013-08-27 19:25:01 +0000412 if (CLANG_ENABLE_ARCMT)
413 set(ENABLE_CLANG_ARCMT "1")
414 else()
415 set(ENABLE_CLANG_ARCMT "0")
416 endif()
417 if (CLANG_ENABLE_REWRITER)
418 set(ENABLE_CLANG_REWRITER "1")
419 else()
420 set(ENABLE_CLANG_REWRITER "0")
421 endif()
422 if (CLANG_ENABLE_STATIC_ANALYZER)
423 set(ENABLE_CLANG_STATIC_ANALYZER "1")
424 else()
425 set(ENABLE_CLANG_STATIC_ANALYZER "0")
426 endif()
427
Chandler Carruth3511dd32012-06-28 06:36:24 +0000428 configure_file(${input} ${output} @ONLY)
429endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000430
431# A raw function to create a lit target. This is used to implement the testuite
432# management functions.
433function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000434 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000435 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
436 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000437 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
438 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
439 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000440 set(LIT_COMMAND
441 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000442 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000443 ${LIT_ARGS}
444 )
445 foreach(param ${ARG_PARAMS})
446 list(APPEND LIT_COMMAND --param ${param})
447 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000448 if( ARG_DEPENDS )
449 add_custom_target(${target}
450 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
451 COMMENT "${comment}"
452 )
453 add_dependencies(${target} ${ARG_DEPENDS})
454 else()
455 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000456 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000457 message(STATUS "${target} does nothing.")
458 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000459
460 # Tests should be excluded from "Build Solution".
461 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000462endfunction()
463
464# A function to add a set of lit test suites to be driven through 'check-*' targets.
465function(add_lit_testsuite target comment)
466 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
467
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000468 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
469 if(NOT EXCLUDE_FROM_ALL)
470 # Register the testsuites, params and depends for the global check rule.
471 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
472 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
473 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
474 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
475 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000476
477 # Produce a specific suffixed check rule.
478 add_lit_target(${target} ${comment}
479 ${ARG_DEFAULT_ARGS}
480 PARAMS ${ARG_PARAMS}
481 DEPENDS ${ARG_DEPENDS}
482 ARGS ${ARG_ARGS}
483 )
484endfunction()