blob: de00ab46ae9a6543cd1ebcaad6aa7b32824b4c63 [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})
75
76 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
77 foreach(src ${srcs})
78 get_filename_component(extension ${src} EXT)
79 if(extension STREQUAL ".cpp")
80 set(first_source_file ${src})
81 break()
82 endif()
83 endforeach()
84
85 # Force re-linking when the exports file changes. Actually, it
86 # forces recompilation of the source file. The LINK_DEPENDS target
87 # property only works for makefile-based generators.
88 set_property(SOURCE ${first_source_file} APPEND PROPERTY
89 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
90
91 set_property(DIRECTORY APPEND
92 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
93
94 add_dependencies(${target_name} ${target_name}_exports)
95endfunction(add_llvm_symbol_exports)
96
Nico Weber62588e12013-12-30 03:36:05 +000097function(add_dead_strip target_name)
Nico Weber34bac1f2014-01-05 00:37:45 +000098 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
99 # But MinSizeRel seems to add that automatically, so maybe disable these
100 # flags instead if LLVM_NO_DEAD_STRIP is set.
Yaron Keren03759bd2013-12-30 05:31:53 +0000101 if(NOT CYGWIN AND NOT MINGW AND NOT MSVC)
Nico Weber62588e12013-12-30 03:36:05 +0000102 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
103 SET(CMAKE_CXX_FLAGS
104 "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections"
105 PARENT_SCOPE)
106 endif()
107 endif()
108 if(NOT LLVM_NO_DEAD_STRIP)
109 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
110 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
111 LINK_FLAGS " -Wl,-dead_strip")
112 elseif(NOT WIN32)
113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " -Wl,--gc-sections")
115 endif()
116 endif()
117endfunction(add_dead_strip)
118
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000119# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
120# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
121# or a certain builder, for eaxample, msbuild.exe, would be confused.
122function(set_output_directory target bindir libdir)
123 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
124 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
125 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
126 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
127 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
128 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
129 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
Douglas Gregorbbebd812014-01-02 19:07:19 +0000130 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000131 endforeach()
132 else()
133 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
134 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
135 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
136 endif()
137endfunction()
138
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000139macro(add_llvm_library name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000140 llvm_process_sources( ALL_FILES ${ARGN} )
141 add_library( ${name} ${ALL_FILES} )
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000142 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes5ed96262011-02-18 22:06:14 +0000143 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Nico Weber62588e12013-12-30 03:36:05 +0000144 add_dead_strip( ${name} )
Oscar Fuentes8807bdd2008-09-22 18:21:51 +0000145 if( LLVM_COMMON_DEPENDS )
146 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
147 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000148
149 if( BUILD_SHARED_LIBS )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000150 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000151 if (MSVC)
152 set_target_properties(${name}
153 PROPERTIES
154 IMPORT_SUFFIX ".imp")
155 endif ()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000156
Nico Weber06473f82013-12-29 05:39:01 +0000157 if (LLVM_EXPORTED_SYMBOL_FILE)
158 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
159 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000160 endif()
161
Oscar Fuentes978e5282011-03-29 20:51:08 +0000162 # Ensure that the system libraries always comes last on the
163 # list. Without this, linking the unit tests on MinGW fails.
164 link_system_libs( ${name} )
165
Nick Lewycky61aed872011-04-29 02:12:06 +0000166 if( EXCLUDE_FROM_ALL )
167 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
168 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000169 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
170 install(TARGETS ${name}
171 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
172 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
173 endif()
Nick Lewycky61aed872011-04-29 02:12:06 +0000174 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000175 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000176
177 # Add the explicit dependency information for this library.
178 #
179 # It would be nice to verify that we have the dependencies for this library
180 # name, but using get_property(... SET) doesn't suffice to determine if a
181 # property has been set to an empty value.
182 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
183 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000184endmacro(add_llvm_library name)
185
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000186macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000187 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000188 message(STATUS "Loadable modules not supported on this platform.
189${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000190 # Add empty "phony" target
191 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000192 else()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000193 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes9f2b3842010-12-22 08:30:17 +0000194 if (MODULE)
195 set(libkind MODULE)
196 else()
197 set(libkind SHARED)
198 endif()
199
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000200 add_library( ${name} ${libkind} ${ALL_FILES} )
Jordan Rose353bdcd2014-01-02 19:47:45 +0000201 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000202 set_target_properties( ${name} PROPERTIES PREFIX "" )
Nico Weber62588e12013-12-30 03:36:05 +0000203 add_dead_strip( ${name} )
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000204
Nico Weberc27118d2013-12-28 23:31:44 +0000205 if (LLVM_EXPORTED_SYMBOL_FILE)
206 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
207 endif(LLVM_EXPORTED_SYMBOL_FILE)
208
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000209 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000210 link_system_libs( ${name} )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000211
Douglas Gregor66df54f2009-11-10 15:30:33 +0000212 if (APPLE)
213 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000214 set_property(TARGET ${name} APPEND_STRING PROPERTY
215 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000216 endif()
217
Alp Tokerc0b7bb52014-01-08 11:10:24 +0000218 if (MODULE)
219 set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
220 endif ()
221
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000222 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000223 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000224 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000225 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
226 install(TARGETS ${name}
227 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
228 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
229 endif()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000230 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000231 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000232
233 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000234endmacro(add_llvm_loadable_module name)
235
236
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000237macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000238 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000239 if( EXCLUDE_FROM_ALL )
240 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
241 else()
242 add_executable(${name} ${ALL_FILES})
243 endif()
Nico Weber62588e12013-12-30 03:36:05 +0000244 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000245
246 if (LLVM_EXPORTED_SYMBOL_FILE)
247 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
248 endif(LLVM_EXPORTED_SYMBOL_FILE)
249
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000250 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000251 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000252 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000253 if( LLVM_COMMON_DEPENDS )
254 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
255 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000256 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000257endmacro(add_llvm_executable name)
258
259
Hans Wennborg16546272013-08-24 00:20:36 +0000260set (LLVM_TOOLCHAIN_TOOLS
261 llvm-ar
262 llvm-objdump
263 )
264
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000265macro(add_llvm_tool name)
266 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000267 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000268 set(EXCLUDE_FROM_ALL ON)
269 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000270 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000271
272 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
273 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
274 if( LLVM_BUILD_TOOLS )
275 install(TARGETS ${name} RUNTIME DESTINATION bin)
276 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000277 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000278 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000279endmacro(add_llvm_tool name)
280
281
282macro(add_llvm_example name)
283# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000284 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000285 set(EXCLUDE_FROM_ALL ON)
286 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000287 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000288 if( LLVM_BUILD_EXAMPLES )
289 install(TARGETS ${name} RUNTIME DESTINATION examples)
290 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000291 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000292endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000293
294
Oscar Fuentes3145e922011-02-20 22:06:10 +0000295macro(add_llvm_utility name)
296 add_llvm_executable(${name} ${ARGN})
297 set_target_properties(${name} PROPERTIES FOLDER "Utils")
298endmacro(add_llvm_utility name)
299
300
Oscar Fuentescdc95492008-09-26 04:40:32 +0000301macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000302 include_directories(BEFORE
303 ${CMAKE_CURRENT_BINARY_DIR}
304 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000305 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000306 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000307endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000308
309# Add external project that may want to be built as part of llvm such as Clang,
310# lld, and Polly. This adds two options. One for the source directory of the
311# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000312# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000313# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000314macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000315 set(add_llvm_external_dir "${ARGN}")
316 if("${add_llvm_external_dir}" STREQUAL "")
317 set(add_llvm_external_dir ${name})
318 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000319 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000320 string(REPLACE "-" "_" nameUNDERSCORE ${name})
321 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
322 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000323 CACHE PATH "Path to ${name} source directory")
324 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
325 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
326 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
327 "Whether to build ${name} as part of LLVM" ON)
328 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000329 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000330 endif()
331 endif()
332endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000333
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000334macro(add_llvm_tool_subdirectory name)
335 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
336 add_subdirectory(${name})
337endmacro(add_llvm_tool_subdirectory)
338
339macro(ignore_llvm_tool_subdirectory name)
340 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
341endmacro(ignore_llvm_tool_subdirectory)
342
343function(add_llvm_implicit_external_projects)
344 set(list_of_implicit_subdirs "")
345 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
346 foreach(dir ${sub-dirs})
347 if(IS_DIRECTORY "${dir}")
348 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
349 if( tool_subdir_ignore EQUAL -1
350 AND EXISTS "${dir}/CMakeLists.txt")
351 get_filename_component(fn "${dir}" NAME)
352 list(APPEND list_of_implicit_subdirs "${fn}")
353 endif()
354 endif()
355 endforeach()
356
357 foreach(external_proj ${list_of_implicit_subdirs})
358 add_llvm_external_project("${external_proj}")
359 endforeach()
360endfunction(add_llvm_implicit_external_projects)
361
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000362# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000363function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000364 if( NOT LLVM_BUILD_TESTS )
365 set(EXCLUDE_FROM_ALL ON)
366 endif()
367
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000368 # Visual Studio 2012 only supports up to 8 template parameters in
369 # std::tr1::tuple by default, but gtest requires 10
370 if (MSVC AND MSVC_VERSION EQUAL 1700)
371 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
372 endif ()
373
374 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
375 if (NOT LLVM_ENABLE_THREADS)
376 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
377 endif ()
378
379 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
380 set(LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
381 endif ()
382
383 set(LLVM_NO_RTTI ON)
384
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000385 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000386 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
387 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000388 target_link_libraries(${test_name}
389 gtest
390 gtest_main
391 LLVMSupport # gtest needs it for raw_ostream.
392 )
393
394 add_dependencies(${test_suite} ${test_name})
395 get_target_property(test_suite_folder ${test_suite} FOLDER)
396 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
397 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
398 endif ()
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000399 llvm_update_compile_flags(${test_name})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000400endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000401
402# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000403# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000404# variables needed for the 'lit.site.cfg' files. This function bundles the
405# common variables that any Lit instance is likely to need, and custom
406# variables can be passed in.
407function(configure_lit_site_cfg input output)
408 foreach(c ${LLVM_TARGETS_TO_BUILD})
409 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
410 endforeach(c)
411 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
412
413 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000414
415 if(BUILD_SHARED_LIBS)
416 set(LLVM_SHARED_LIBS_ENABLED "1")
417 else()
418 set(LLVM_SHARED_LIBS_ENABLED "0")
419 endif(BUILD_SHARED_LIBS)
420
421 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
422 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
423 else() # Default for all other unix like systems.
424 # CMake hardcodes the library locaction using rpath.
425 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
426 # build dir. We pass it anyways.
427 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
428 endif()
429
430 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000431 if (CMAKE_CFG_INTDIR STREQUAL ".")
432 set(LLVM_BUILD_MODE ".")
433 else ()
434 set(LLVM_BUILD_MODE "%(build_mode)s")
435 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000436
437 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
438 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumie73df852013-12-16 16:03:08 +0000439 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
440 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Jordan Rose353bdcd2014-01-02 19:47:45 +0000441 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000442 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
443 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
444 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
445
446 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
447 set(ENABLE_ASSERTIONS "1")
448 else()
449 set(ENABLE_ASSERTIONS "0")
450 endif()
451
Tim Northoverbfe84682013-02-14 16:49:32 +0000452 set(HOST_OS ${CMAKE_SYSTEM_NAME})
453 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000454
Roman Divackyc9871112013-08-27 19:25:01 +0000455 if (CLANG_ENABLE_ARCMT)
456 set(ENABLE_CLANG_ARCMT "1")
457 else()
458 set(ENABLE_CLANG_ARCMT "0")
459 endif()
460 if (CLANG_ENABLE_REWRITER)
461 set(ENABLE_CLANG_REWRITER "1")
462 else()
463 set(ENABLE_CLANG_REWRITER "0")
464 endif()
465 if (CLANG_ENABLE_STATIC_ANALYZER)
466 set(ENABLE_CLANG_STATIC_ANALYZER "1")
467 else()
468 set(ENABLE_CLANG_STATIC_ANALYZER "0")
469 endif()
470
Chandler Carruth3511dd32012-06-28 06:36:24 +0000471 configure_file(${input} ${output} @ONLY)
472endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000473
474# A raw function to create a lit target. This is used to implement the testuite
475# management functions.
476function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000477 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000478 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
479 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000480 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
481 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
482 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000483 set(LIT_COMMAND
484 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000485 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000486 ${LIT_ARGS}
487 )
488 foreach(param ${ARG_PARAMS})
489 list(APPEND LIT_COMMAND --param ${param})
490 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000491 if( ARG_DEPENDS )
492 add_custom_target(${target}
493 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
494 COMMENT "${comment}"
495 )
496 add_dependencies(${target} ${ARG_DEPENDS})
497 else()
498 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000499 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000500 message(STATUS "${target} does nothing.")
501 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000502
503 # Tests should be excluded from "Build Solution".
504 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000505endfunction()
506
507# A function to add a set of lit test suites to be driven through 'check-*' targets.
508function(add_lit_testsuite target comment)
509 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
510
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000511 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
512 if(NOT EXCLUDE_FROM_ALL)
513 # Register the testsuites, params and depends for the global check rule.
514 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
515 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
516 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
517 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
518 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000519
520 # Produce a specific suffixed check rule.
521 add_lit_target(${target} ${comment}
522 ${ARG_DEFAULT_ARGS}
523 PARAMS ${ARG_PARAMS}
524 DEPENDS ${ARG_DEPENDS}
525 ARGS ${ARG_ARGS}
526 )
527endfunction()