blob: 6996c388b3edeaa986b89a56c0a9e69327a054a0 [file] [log] [blame]
Chandler Carruth69ce6652012-06-30 10:14:14 +00001include(LLVMParseArguments)
Oscar Fuentes751ea9d2008-11-14 22:06:14 +00002include(LLVMProcessSources)
Oscar Fuentesd8a6dd62011-04-05 17:02:48 +00003include(LLVM-Config)
Oscar Fuentesa229b3c2008-09-22 01:08:49 +00004
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00005function(llvm_update_compile_flags name)
NAKAMURA Takumia679f432014-01-28 09:44:06 +00006 set(ALL_SOURCES ${ARGN})
7 if("${ALL_SOURCES}" MATCHES "\\.c(;|$)")
8 set(update_src_props ON)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00009 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000010
11 if(LLVM_REQUIRES_EH)
12 set(LLVM_REQUIRES_RTTI ON)
13 else()
14 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
15 set(target_compile_flags "${target_compile_flags} -fno-exceptions")
16 elseif(MSVC)
17 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
18 set(target_compile_flags "${target_compile_flags} /EHs-c-")
19 endif()
20 endif()
21
22 if(NOT LLVM_REQUIRES_RTTI)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000023 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
24 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
25 set(target_compile_flags "${target_compile_flags} -fno-rtti")
26 elseif (MSVC)
NAKAMURA Takumia679f432014-01-28 09:44:06 +000027 set(target_compile_flags "${target_compile_flags} /GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000028 endif ()
29 endif()
30
NAKAMURA Takumia679f432014-01-28 09:44:06 +000031 if(update_src_props)
32 foreach(fn ${ALL_SOURCES})
33 get_filename_component(suf ${fn} EXT)
34 if("${suf}" STREQUAL ".cpp")
35 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
36 COMPILE_FLAGS "${target_compile_flags}")
37 endif()
38 endforeach()
39 else()
40 # Update target props, since all sources are C++.
41 set_property(TARGET ${name} APPEND_STRING PROPERTY
42 COMPILE_FLAGS "${target_compile_flags}")
43 endif()
44
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000045 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
46endfunction()
47
Nico Weberc27118d2013-12-28 23:31:44 +000048function(add_llvm_symbol_exports target_name export_file)
49 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000050 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000051 add_custom_command(OUTPUT ${native_export_file}
52 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000053 DEPENDS ${export_file}
54 VERBATIM
55 COMMENT "Creating export file for ${target_name}")
56 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000057 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000058 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
59 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000060 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000061 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000062 add_custom_command(OUTPUT ${native_export_file}
63 COMMAND echo "{" > ${native_export_file}
64 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
65 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
66 COMMAND echo " local: *;" >> ${native_export_file}
67 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000068 DEPENDS ${export_file}
69 VERBATIM
70 COMMENT "Creating export file for ${target_name}")
71 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000072 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000073 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000074 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000075
76 set(CAT "type")
77 if(CYGWIN)
78 set(CAT "cat")
79 endif()
80
Nico Weber5c8a4a32013-12-29 04:05:23 +000081 # Using ${export_file} in add_custom_command directly confuses cmd.exe.
82 file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
83
NAKAMURA Takumi81210752013-12-29 16:15:18 +000084 add_custom_command(OUTPUT ${native_export_file}
85 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
86 COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000087 DEPENDS ${export_file}
88 VERBATIM
89 COMMENT "Creating export file for ${target_name}")
Nico Weber49d6f482013-12-29 07:43:09 +000090 if(CYGWIN OR MINGW)
Nico Weber49da7582013-12-29 06:56:28 +000091 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000092 LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weber49da7582013-12-29 06:56:28 +000093 else()
94 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000095 LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +000096 endif()
Nico Weberc27118d2013-12-28 23:31:44 +000097 endif()
98
99 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000100 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000101
102 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
103 foreach(src ${srcs})
104 get_filename_component(extension ${src} EXT)
105 if(extension STREQUAL ".cpp")
106 set(first_source_file ${src})
107 break()
108 endif()
109 endforeach()
110
111 # Force re-linking when the exports file changes. Actually, it
112 # forces recompilation of the source file. The LINK_DEPENDS target
113 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000114 # FIXME: This is not safe because this will create the same target
115 # ${native_export_file} in several different file:
116 # - One where we emitted ${target_name}_exports
117 # - One where we emitted the build command for the following object.
118 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
119 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000120
121 set_property(DIRECTORY APPEND
122 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
123
124 add_dependencies(${target_name} ${target_name}_exports)
125endfunction(add_llvm_symbol_exports)
126
Nico Weber62588e12013-12-30 03:36:05 +0000127function(add_dead_strip target_name)
Nico Weber62588e12013-12-30 03:36:05 +0000128 if(NOT LLVM_NO_DEAD_STRIP)
129 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
130 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
131 LINK_FLAGS " -Wl,-dead_strip")
132 elseif(NOT WIN32)
NAKAMURA Takumibb50bce2014-01-28 09:43:55 +0000133 # Object files are compiled with -ffunction-data-sections.
Nico Weber62588e12013-12-30 03:36:05 +0000134 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
135 LINK_FLAGS " -Wl,--gc-sections")
136 endif()
137 endif()
138endfunction(add_dead_strip)
139
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000140# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
141# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
142# or a certain builder, for eaxample, msbuild.exe, would be confused.
143function(set_output_directory target bindir libdir)
144 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
145 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
146 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
147 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
148 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
149 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
150 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
Douglas Gregorbbebd812014-01-02 19:07:19 +0000151 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000152 endforeach()
153 else()
154 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
155 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
156 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
157 endif()
158endfunction()
159
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000160macro(add_llvm_library name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000161 llvm_process_sources( ALL_FILES ${ARGN} )
162 add_library( ${name} ${ALL_FILES} )
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000163 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes5ed96262011-02-18 22:06:14 +0000164 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000165 llvm_update_compile_flags(${name} ${ALL_FILES})
Nico Weber62588e12013-12-30 03:36:05 +0000166 add_dead_strip( ${name} )
Oscar Fuentes8807bdd2008-09-22 18:21:51 +0000167 if( LLVM_COMMON_DEPENDS )
168 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
169 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000170
171 if( BUILD_SHARED_LIBS )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000172 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000173 if (MSVC)
174 set_target_properties(${name}
175 PROPERTIES
176 IMPORT_SUFFIX ".imp")
177 endif ()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000178
Nico Weber06473f82013-12-29 05:39:01 +0000179 if (LLVM_EXPORTED_SYMBOL_FILE)
180 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
181 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000182 endif()
183
Oscar Fuentes978e5282011-03-29 20:51:08 +0000184 # Ensure that the system libraries always comes last on the
185 # list. Without this, linking the unit tests on MinGW fails.
186 link_system_libs( ${name} )
187
Nick Lewycky61aed872011-04-29 02:12:06 +0000188 if( EXCLUDE_FROM_ALL )
189 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
190 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000191 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
192 install(TARGETS ${name}
193 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
194 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
195 endif()
Nick Lewycky61aed872011-04-29 02:12:06 +0000196 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000197 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000198
199 # Add the explicit dependency information for this library.
200 #
201 # It would be nice to verify that we have the dependencies for this library
202 # name, but using get_property(... SET) doesn't suffice to determine if a
203 # property has been set to an empty value.
204 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
205 target_link_libraries(${name} ${lib_deps})
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000206endmacro(add_llvm_library name)
207
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000208macro(add_llvm_loadable_module name)
Oscar Fuentesff11a232010-10-22 19:03:24 +0000209 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000210 message(STATUS "Loadable modules not supported on this platform.
211${name} ignored.")
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000212 # Add empty "phony" target
213 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000214 else()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000215 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes9f2b3842010-12-22 08:30:17 +0000216 if (MODULE)
217 set(libkind MODULE)
218 else()
219 set(libkind SHARED)
220 endif()
221
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000222 add_library( ${name} ${libkind} ${ALL_FILES} )
Jordan Rose353bdcd2014-01-02 19:47:45 +0000223 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000224 set_target_properties( ${name} PROPERTIES PREFIX "" )
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000225 llvm_update_compile_flags(${name} ${ALL_FILES})
Nico Weber62588e12013-12-30 03:36:05 +0000226 add_dead_strip( ${name} )
NAKAMURA Takumi14d09b82013-12-29 16:15:31 +0000227
Nico Weberc27118d2013-12-28 23:31:44 +0000228 if (LLVM_EXPORTED_SYMBOL_FILE)
229 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
230 endif(LLVM_EXPORTED_SYMBOL_FILE)
231
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000232 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000233 link_system_libs( ${name} )
Oscar Fuentescc48b9a2011-03-12 16:48:54 +0000234
Douglas Gregor66df54f2009-11-10 15:30:33 +0000235 if (APPLE)
236 # Darwin-specific linker flags for loadable modules.
Nico Weberdc78dc92013-12-29 23:04:48 +0000237 set_property(TARGET ${name} APPEND_STRING PROPERTY
238 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Douglas Gregor66df54f2009-11-10 15:30:33 +0000239 endif()
240
Alp Tokerc0b7bb52014-01-08 11:10:24 +0000241 if (MODULE)
242 set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
243 endif ()
244
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000245 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000246 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000247 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000248 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
249 install(TARGETS ${name}
250 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
251 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
252 endif()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000253 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000254 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000255
256 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000257endmacro(add_llvm_loadable_module name)
258
259
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000260macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000261 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000262 if( EXCLUDE_FROM_ALL )
263 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
264 else()
265 add_executable(${name} ${ALL_FILES})
266 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000267 llvm_update_compile_flags(${name} ${ALL_FILES})
Nico Weber62588e12013-12-30 03:36:05 +0000268 add_dead_strip( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000269
270 if (LLVM_EXPORTED_SYMBOL_FILE)
271 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
272 endif(LLVM_EXPORTED_SYMBOL_FILE)
273
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000274 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000275 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000276 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000277 if( LLVM_COMMON_DEPENDS )
278 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
279 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes978e5282011-03-29 20:51:08 +0000280 link_system_libs( ${name} )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000281endmacro(add_llvm_executable name)
282
283
Hans Wennborg16546272013-08-24 00:20:36 +0000284set (LLVM_TOOLCHAIN_TOOLS
285 llvm-ar
286 llvm-objdump
287 )
288
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000289macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000290 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000291 set(EXCLUDE_FROM_ALL ON)
292 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000293 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000294
295 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
296 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
297 if( LLVM_BUILD_TOOLS )
298 install(TARGETS ${name} RUNTIME DESTINATION bin)
299 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000300 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000301 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000302endmacro(add_llvm_tool name)
303
304
305macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000306 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000307 set(EXCLUDE_FROM_ALL ON)
308 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000309 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000310 if( LLVM_BUILD_EXAMPLES )
311 install(TARGETS ${name} RUNTIME DESTINATION examples)
312 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000313 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000314endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000315
316
Oscar Fuentes3145e922011-02-20 22:06:10 +0000317macro(add_llvm_utility name)
318 add_llvm_executable(${name} ${ARGN})
319 set_target_properties(${name} PROPERTIES FOLDER "Utils")
320endmacro(add_llvm_utility name)
321
322
Oscar Fuentescdc95492008-09-26 04:40:32 +0000323macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000324 include_directories(BEFORE
325 ${CMAKE_CURRENT_BINARY_DIR}
326 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor85fedbe2009-06-23 17:57:35 +0000327 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000328 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000329endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000330
331# Add external project that may want to be built as part of llvm such as Clang,
332# lld, and Polly. This adds two options. One for the source directory of the
333# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000334# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000335# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000336macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000337 set(add_llvm_external_dir "${ARGN}")
338 if("${add_llvm_external_dir}" STREQUAL "")
339 set(add_llvm_external_dir ${name})
340 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000341 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000342 string(REPLACE "-" "_" nameUNDERSCORE ${name})
343 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
344 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000345 CACHE PATH "Path to ${name} source directory")
346 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
347 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
348 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
349 "Whether to build ${name} as part of LLVM" ON)
350 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000351 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000352 endif()
353 endif()
354endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000355
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000356macro(add_llvm_tool_subdirectory name)
357 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
358 add_subdirectory(${name})
359endmacro(add_llvm_tool_subdirectory)
360
361macro(ignore_llvm_tool_subdirectory name)
362 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
363endmacro(ignore_llvm_tool_subdirectory)
364
365function(add_llvm_implicit_external_projects)
366 set(list_of_implicit_subdirs "")
367 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
368 foreach(dir ${sub-dirs})
369 if(IS_DIRECTORY "${dir}")
370 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
371 if( tool_subdir_ignore EQUAL -1
372 AND EXISTS "${dir}/CMakeLists.txt")
373 get_filename_component(fn "${dir}" NAME)
374 list(APPEND list_of_implicit_subdirs "${fn}")
375 endif()
376 endif()
377 endforeach()
378
379 foreach(external_proj ${list_of_implicit_subdirs})
380 add_llvm_external_project("${external_proj}")
381 endforeach()
382endfunction(add_llvm_implicit_external_projects)
383
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000384# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000385function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000386 if( NOT LLVM_BUILD_TESTS )
387 set(EXCLUDE_FROM_ALL ON)
388 endif()
389
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000390 # Visual Studio 2012 only supports up to 8 template parameters in
391 # std::tr1::tuple by default, but gtest requires 10
392 if (MSVC AND MSVC_VERSION EQUAL 1700)
393 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
394 endif ()
395
396 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
397 if (NOT LLVM_ENABLE_THREADS)
398 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
399 endif ()
400
401 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
402 set(LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
403 endif ()
404
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000405 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000406
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000407 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000408 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
409 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000410 target_link_libraries(${test_name}
411 gtest
412 gtest_main
413 LLVMSupport # gtest needs it for raw_ostream.
414 )
415
416 add_dependencies(${test_suite} ${test_name})
417 get_target_property(test_suite_folder ${test_suite} FOLDER)
418 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
419 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
420 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000421endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000422
423# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000424# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000425# variables needed for the 'lit.site.cfg' files. This function bundles the
426# common variables that any Lit instance is likely to need, and custom
427# variables can be passed in.
428function(configure_lit_site_cfg input output)
429 foreach(c ${LLVM_TARGETS_TO_BUILD})
430 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
431 endforeach(c)
432 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
433
434 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000435
436 if(BUILD_SHARED_LIBS)
437 set(LLVM_SHARED_LIBS_ENABLED "1")
438 else()
439 set(LLVM_SHARED_LIBS_ENABLED "0")
440 endif(BUILD_SHARED_LIBS)
441
442 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
443 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
444 else() # Default for all other unix like systems.
445 # CMake hardcodes the library locaction using rpath.
446 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
447 # build dir. We pass it anyways.
448 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
449 endif()
450
451 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000452 if (CMAKE_CFG_INTDIR STREQUAL ".")
453 set(LLVM_BUILD_MODE ".")
454 else ()
455 set(LLVM_BUILD_MODE "%(build_mode)s")
456 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000457
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000458 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000459 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
460 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000461 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
462 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000463
464 # SHLIBDIR points the build tree.
465 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
466
Chandler Carruth3511dd32012-06-28 06:36:24 +0000467 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
468 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
469 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
470
471 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
472 set(ENABLE_ASSERTIONS "1")
473 else()
474 set(ENABLE_ASSERTIONS "0")
475 endif()
476
Tim Northoverbfe84682013-02-14 16:49:32 +0000477 set(HOST_OS ${CMAKE_SYSTEM_NAME})
478 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000479
Roman Divackyc9871112013-08-27 19:25:01 +0000480 if (CLANG_ENABLE_ARCMT)
481 set(ENABLE_CLANG_ARCMT "1")
482 else()
483 set(ENABLE_CLANG_ARCMT "0")
484 endif()
485 if (CLANG_ENABLE_REWRITER)
486 set(ENABLE_CLANG_REWRITER "1")
487 else()
488 set(ENABLE_CLANG_REWRITER "0")
489 endif()
490 if (CLANG_ENABLE_STATIC_ANALYZER)
491 set(ENABLE_CLANG_STATIC_ANALYZER "1")
492 else()
493 set(ENABLE_CLANG_STATIC_ANALYZER "0")
494 endif()
495
Chandler Carruth3511dd32012-06-28 06:36:24 +0000496 configure_file(${input} ${output} @ONLY)
497endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000498
499# A raw function to create a lit target. This is used to implement the testuite
500# management functions.
501function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000502 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000503 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
504 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000505 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
506 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
507 endif ()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000508 set(LIT_COMMAND
509 ${PYTHON_EXECUTABLE}
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000510 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
Chandler Carruth69ce6652012-06-30 10:14:14 +0000511 ${LIT_ARGS}
512 )
513 foreach(param ${ARG_PARAMS})
514 list(APPEND LIT_COMMAND --param ${param})
515 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000516 if( ARG_DEPENDS )
517 add_custom_target(${target}
518 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
519 COMMENT "${comment}"
520 )
521 add_dependencies(${target} ${ARG_DEPENDS})
522 else()
523 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000524 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000525 message(STATUS "${target} does nothing.")
526 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000527
528 # Tests should be excluded from "Build Solution".
529 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000530endfunction()
531
532# A function to add a set of lit test suites to be driven through 'check-*' targets.
533function(add_lit_testsuite target comment)
534 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
535
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000536 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
537 if(NOT EXCLUDE_FROM_ALL)
538 # Register the testsuites, params and depends for the global check rule.
539 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
540 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
541 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
542 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
543 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000544
545 # Produce a specific suffixed check rule.
546 add_lit_target(${target} ${comment}
547 ${ARG_DEFAULT_ARGS}
548 PARAMS ${ARG_PARAMS}
549 DEPENDS ${ARG_DEPENDS}
550 ARGS ${ARG_ARGS}
551 )
552endfunction()