blob: c8f1083be1f6b4e556db81cb91291451b1ec62a7 [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 Takumib524c222014-01-28 11:40:04 +00006 get_property(sources TARGET ${name} PROPERTY SOURCES)
7 if("${sources}" MATCHES "\\.c(;|$)")
NAKAMURA Takumia679f432014-01-28 09:44:06 +00008 set(update_src_props ON)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +00009 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000010
Dan Liewa5bdc842014-07-22 15:41:18 +000011 # LLVM_REQUIRES_EH is an internal flag that individual
12 # targets can use to force EH
13 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
14 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
15 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
16 set(LLVM_REQUIRES_RTTI ON)
17 endif()
NAKAMURA Takumia679f432014-01-28 09:44:06 +000018 else()
19 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000020 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000021 elseif(MSVC)
22 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000023 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000024 endif()
25 endif()
26
Dan Liewa5bdc842014-07-22 15:41:18 +000027 # LLVM_REQUIRES_RTTI is an internal flag that individual
28 # targets can use to force RTTI
29 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000030 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
31 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000032 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000033 elseif (MSVC)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000034 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000035 endif ()
36 endif()
37
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +000038 # Assume that;
39 # - LLVM_COMPILE_FLAGS is list.
40 # - PROPERTY COMPILE_FLAGS is string.
41 string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
42
NAKAMURA Takumia679f432014-01-28 09:44:06 +000043 if(update_src_props)
NAKAMURA Takumib524c222014-01-28 11:40:04 +000044 foreach(fn ${sources})
NAKAMURA Takumia679f432014-01-28 09:44:06 +000045 get_filename_component(suf ${fn} EXT)
46 if("${suf}" STREQUAL ".cpp")
NAKAMURA Takumid40cdc92014-01-31 17:32:42 +000047 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
48 COMPILE_FLAGS "${target_compile_flags}")
NAKAMURA Takumia679f432014-01-28 09:44:06 +000049 endif()
50 endforeach()
51 else()
52 # Update target props, since all sources are C++.
53 set_property(TARGET ${name} APPEND_STRING PROPERTY
54 COMPILE_FLAGS "${target_compile_flags}")
55 endif()
56
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +000057 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
58endfunction()
59
Nico Weberc27118d2013-12-28 23:31:44 +000060function(add_llvm_symbol_exports target_name export_file)
61 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000062 set(native_export_file "${target_name}.exports")
NAKAMURA Takumi81210752013-12-29 16:15:18 +000063 add_custom_command(OUTPUT ${native_export_file}
64 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000065 DEPENDS ${export_file}
66 VERBATIM
67 COMMENT "Creating export file for ${target_name}")
68 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
NAKAMURA Takumi81210752013-12-29 16:15:18 +000069 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000070 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
71 # Gold and BFD ld require a version script rather than a plain list.
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000072 set(native_export_file "${target_name}.exports")
Nico Weberc27118d2013-12-28 23:31:44 +000073 # FIXME: Don't write the "local:" line on OpenBSD.
NAKAMURA Takumi81210752013-12-29 16:15:18 +000074 add_custom_command(OUTPUT ${native_export_file}
75 COMMAND echo "{" > ${native_export_file}
76 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
77 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
78 COMMAND echo " local: *;" >> ${native_export_file}
79 COMMAND echo "};" >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +000080 DEPENDS ${export_file}
81 VERBATIM
82 COMMENT "Creating export file for ${target_name}")
83 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Nico Weberdc78dc92013-12-29 23:04:48 +000084 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
Nico Weberc27118d2013-12-28 23:31:44 +000085 else()
NAKAMURA Takumibbd2aaa2013-12-29 16:15:26 +000086 set(native_export_file "${target_name}.def")
Nico Weberc27118d2013-12-28 23:31:44 +000087
Chris Bieneman970555b2014-10-30 22:37:58 +000088 set(CAT "cat")
89 set(export_file_nativeslashes ${export_file})
90 if(WIN32 AND NOT CYGWIN)
91 set(CAT "type")
92 # Convert ${export_file} to native format (backslashes) for "type"
93 # Does not use file(TO_NATIVE_PATH) as it doesn't create a native
94 # path but a build-system specific format (see CMake bug
95 # http://public.kitware.com/Bug/print_bug_page.php?bug_id=5939 )
96 string(REPLACE / \\ export_file_nativeslashes ${export_file})
Nico Weberc27118d2013-12-28 23:31:44 +000097 endif()
98
NAKAMURA Takumi81210752013-12-29 16:15:18 +000099 add_custom_command(OUTPUT ${native_export_file}
100 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
Chris Bieneman970555b2014-10-30 22:37:58 +0000101 COMMAND ${CAT} ${export_file_nativeslashes} >> ${native_export_file}
Nico Weberc27118d2013-12-28 23:31:44 +0000102 DEPENDS ${export_file}
103 VERBATIM
104 COMMENT "Creating export file for ${target_name}")
Chris Bieneman970555b2014-10-30 22:37:58 +0000105 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
106 if(MSVC)
107 set(export_file_linker_flag "/DEF:${export_file_linker_flag}")
NAKAMURA Takumi0a062bd2013-12-29 16:19:13 +0000108 endif()
Chris Bieneman970555b2014-10-30 22:37:58 +0000109 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
110 LINK_FLAGS " ${export_file_linker_flag}")
Nico Weberc27118d2013-12-28 23:31:44 +0000111 endif()
112
113 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
NAKAMURA Takumi14f1f442014-01-27 17:39:38 +0000114 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
Nico Weberc27118d2013-12-28 23:31:44 +0000115
116 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
117 foreach(src ${srcs})
118 get_filename_component(extension ${src} EXT)
119 if(extension STREQUAL ".cpp")
120 set(first_source_file ${src})
121 break()
122 endif()
123 endforeach()
124
125 # Force re-linking when the exports file changes. Actually, it
126 # forces recompilation of the source file. The LINK_DEPENDS target
127 # property only works for makefile-based generators.
Quentin Colombetcb2ae8d2014-01-16 06:43:55 +0000128 # FIXME: This is not safe because this will create the same target
129 # ${native_export_file} in several different file:
130 # - One where we emitted ${target_name}_exports
131 # - One where we emitted the build command for the following object.
132 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
133 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
Nico Weberc27118d2013-12-28 23:31:44 +0000134
135 set_property(DIRECTORY APPEND
136 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
137
138 add_dependencies(${target_name} ${target_name}_exports)
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000139
140 # Add dependency to *_exports later -- CMake issue 14747
141 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
142 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
Nico Weberc27118d2013-12-28 23:31:44 +0000143endfunction(add_llvm_symbol_exports)
144
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000145if(NOT WIN32 AND NOT APPLE)
146 execute_process(COMMAND ${CMAKE_C_COMPILER} -Wl,--version
147 OUTPUT_VARIABLE stdout)
148 if("${stdout}" MATCHES "GNU gold")
149 set(LLVM_LINKER_IS_GOLD ON)
150 endif()
151endif()
152
Rafael Espindola8cb78762014-11-02 12:14:22 +0000153function(add_link_opts target_name)
154 # Pass -O3 to the linker. This enabled different optimizations on different
155 # linkers.
156 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32))
157 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
158 LINK_FLAGS " -Wl,-O3")
159 endif()
160
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000161 if(LLVM_LINKER_IS_GOLD)
162 # With gold gc-sections is always safe.
163 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
Rafael Espindola9c971f92014-12-12 14:28:19 +0000164 LINK_FLAGS " -Wl,--gc-sections")
165 # Note that there is a bug with -Wl,--icf=safe so it is not safe
166 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000167 endif()
168
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000169 if(NOT LLVM_NO_DEAD_STRIP)
170 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Rafael Espindolac71e0052014-10-20 21:37:38 +0000171 # ld64's implementation of -dead_strip breaks tools that use plugins.
Nico Weber62588e12013-12-30 03:36:05 +0000172 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
173 LINK_FLAGS " -Wl,-dead_strip")
Rafael Espindola19c39ee2014-12-04 17:54:35 +0000174 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000175 # Object files are compiled with -ffunction-data-sections.
Rafael Espindolac71e0052014-10-20 21:37:38 +0000176 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
177 # tools that use plugins. Always pass --gc-sections once we require
178 # a newer linker.
NAKAMURA Takumi748627c2014-10-20 12:12:21 +0000179 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
180 LINK_FLAGS " -Wl,--gc-sections")
Nico Weber62588e12013-12-30 03:36:05 +0000181 endif()
182 endif()
Rafael Espindola8cb78762014-11-02 12:14:22 +0000183endfunction(add_link_opts)
Nico Weber62588e12013-12-30 03:36:05 +0000184
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000185# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
186# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
187# or a certain builder, for eaxample, msbuild.exe, would be confused.
188function(set_output_directory target bindir libdir)
NAKAMURA Takumi38d45a22014-07-04 04:23:15 +0000189 # Do nothing if *_OUTPUT_INTDIR is empty.
190 if("${bindir}" STREQUAL "")
191 return()
192 endif()
193
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000194 # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
195 # It affects output of add_library(MODULE).
196 if(WIN32 OR CYGWIN)
197 # DLL platform
198 set(moddir ${bindir})
199 else()
200 set(moddir ${libdir})
201 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000202 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
203 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
204 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
205 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
206 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000207 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000208 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
209 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000210 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000211 endforeach()
212 else()
213 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
214 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000215 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000216 endif()
217endfunction()
218
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000219# llvm_add_library(name sources...
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000220# SHARED;STATIC
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000221# STATIC by default w/o BUILD_SHARED_LIBS.
222# SHARED by default w/ BUILD_SHARED_LIBS.
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000223# MODULE
224# Target ${name} might not be created on unsupported platforms.
225# Check with "if(TARGET ${name})".
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000226# OUTPUT_NAME name
227# Corresponds to OUTPUT_NAME in target properties.
228# DEPENDS targets...
229# Same semantics as add_dependencies().
230# LINK_COMPONENTS components...
231# Same as the variable LLVM_LINK_COMPONENTS.
232# LINK_LIBS lib_targets...
233# Same semantics as target_link_libraries().
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000234# ADDITIONAL_HEADERS
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000235# May specify header files for IDE generators.
236# )
237function(llvm_add_library name)
238 cmake_parse_arguments(ARG
239 "MODULE;SHARED;STATIC"
240 "OUTPUT_NAME"
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000241 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000242 ${ARGN})
243 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
NAKAMURA Takumidea00df2014-02-13 01:00:52 +0000244 if(ARG_ADDITIONAL_HEADERS)
245 # Pass through ADDITIONAL_HEADERS.
246 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
247 endif()
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000248 if(ARG_OBJLIBS)
249 set(ALL_FILES ${ARG_OBJLIBS})
250 else()
251 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
252 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000253
254 if(ARG_MODULE)
255 if(ARG_SHARED OR ARG_STATIC)
256 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
257 endif()
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000258 if(NOT LLVM_ENABLE_PLUGINS)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000259 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
260 return()
261 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000262 else()
263 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
264 set(ARG_SHARED TRUE)
265 endif()
266 if(NOT ARG_SHARED)
267 set(ARG_STATIC TRUE)
268 endif()
269 endif()
270
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000271 # Generate objlib
272 if(ARG_SHARED AND ARG_STATIC)
273 # Generate an obj library for both targets.
274 set(obj_name "obj.${name}")
275 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
276 ${ALL_FILES}
277 )
278 llvm_update_compile_flags(${obj_name})
279 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
280
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000281 # Do add_dependencies(obj) later due to CMake issue 14747.
282 list(APPEND objlibs ${obj_name})
283
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000284 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
285 endif()
286
287 if(ARG_SHARED AND ARG_STATIC)
288 # static
289 set(name_static "${name}_static")
290 if(ARG_OUTPUT_NAME)
NAKAMURA Takumib47a2c02014-02-28 00:28:13 +0000291 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
NAKAMURA Takumie3408ab2014-02-21 14:17:17 +0000292 endif()
293 # DEPENDS has been appended to LLVM_COMMON_LIBS.
294 llvm_add_library(${name_static} STATIC
295 ${output_name}
296 OBJLIBS ${ALL_FILES} # objlib
297 LINK_LIBS ${ARG_LINK_LIBS}
298 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
299 )
300 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
301 set(ARG_STATIC)
302 endif()
303
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000304 if(ARG_MODULE)
305 add_library(${name} MODULE ${ALL_FILES})
306 elseif(ARG_SHARED)
307 add_library(${name} SHARED ${ALL_FILES})
308 else()
309 add_library(${name} STATIC ${ALL_FILES})
310 endif()
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000311 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000312 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000313 add_link_opts( ${name} )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000314 if(ARG_OUTPUT_NAME)
315 set_target_properties(${name}
316 PROPERTIES
317 OUTPUT_NAME ${ARG_OUTPUT_NAME}
318 )
319 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000320
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000321 if(ARG_MODULE)
NAKAMURA Takumie72e2e92014-02-13 11:19:21 +0000322 set_target_properties(${name} PROPERTIES
323 PREFIX ""
324 SUFFIX ${LLVM_PLUGIN_EXT}
325 )
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000326 endif()
327
328 if(ARG_SHARED)
NAKAMURA Takumi543105f2014-04-10 15:47:04 +0000329 if(WIN32)
330 set_target_properties(${name} PROPERTIES
331 PREFIX ""
332 )
333 endif()
NAKAMURA Takumicb065a62013-08-14 03:34:49 +0000334 if (MSVC)
335 set_target_properties(${name}
336 PROPERTIES
337 IMPORT_SUFFIX ".imp")
338 endif ()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000339 endif()
Oscar Fuentesffe32e12010-10-14 15:54:41 +0000340
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000341 if(ARG_MODULE OR ARG_SHARED)
Richard Smith2b91a7f2014-09-26 22:40:15 +0000342 # Do not add -Dname_EXPORTS to the command-line when building files in this
343 # target. Doing so is actively harmful for the modules build because it
344 # creates extra module variants, and not useful because we don't use these
345 # macros.
346 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
347
Nico Weber06473f82013-12-29 05:39:01 +0000348 if (LLVM_EXPORTED_SYMBOL_FILE)
349 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
350 endif()
Nico Weberc27118d2013-12-28 23:31:44 +0000351 endif()
352
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000353 # Add the explicit dependency information for this library.
354 #
355 # It would be nice to verify that we have the dependencies for this library
356 # name, but using get_property(... SET) doesn't suffice to determine if a
357 # property has been set to an empty value.
358 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
359
360 llvm_map_components_to_libnames(llvm_libs
361 ${ARG_LINK_COMPONENTS}
362 ${LLVM_LINK_COMPONENTS}
363 )
364
365 if(CMAKE_VERSION VERSION_LESS 2.8.12)
366 # Link libs w/o keywords, assuming PUBLIC.
367 target_link_libraries(${name}
368 ${ARG_LINK_LIBS}
369 ${lib_deps}
370 ${llvm_libs}
371 )
372 elseif(ARG_STATIC)
373 target_link_libraries(${name} INTERFACE
374 ${ARG_LINK_LIBS}
375 ${lib_deps}
376 ${llvm_libs}
377 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000378 else()
Rafael Espindolaf9dcf902014-11-07 15:33:56 +0000379 # We can use PRIVATE since SO knows its dependent libs.
NAKAMURA Takumia39b6122014-02-26 11:58:01 +0000380 target_link_libraries(${name} PRIVATE
381 ${ARG_LINK_LIBS}
382 ${lib_deps}
383 ${llvm_libs}
384 )
NAKAMURA Takumi955d27a2014-02-26 06:53:16 +0000385 endif()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000386
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000387 if(LLVM_COMMON_DEPENDS)
388 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000389 # Add dependencies also to objlibs.
390 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
391 foreach(objlib ${objlibs})
NAKAMURA Takumi679e7722014-02-21 14:17:29 +0000392 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
393 endforeach()
NAKAMURA Takumie7038df2014-02-10 09:05:11 +0000394 endif()
395endfunction()
396
397macro(add_llvm_library name)
398 if( BUILD_SHARED_LIBS )
399 llvm_add_library(${name} SHARED ${ARGN})
400 else()
401 llvm_add_library(${name} ${ARGN})
402 endif()
403 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
404
Nick Lewycky61aed872011-04-29 02:12:06 +0000405 if( EXCLUDE_FROM_ALL )
406 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
407 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000408 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
409 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000410 EXPORT LLVMExports
NAKAMURA Takumi506c0dc2014-07-11 14:36:28 +0000411 RUNTIME DESTINATION bin
Hans Wennborg16546272013-08-24 00:20:36 +0000412 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
413 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
414 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000415 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Nick Lewycky61aed872011-04-29 02:12:06 +0000416 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000417 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000418endmacro(add_llvm_library name)
419
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000420macro(add_llvm_loadable_module name)
NAKAMURA Takumi487f3872014-02-13 11:25:17 +0000421 llvm_add_library(${name} MODULE ${ARGN})
422 if(NOT TARGET ${name})
NAKAMURA Takumia8c1c3f2010-12-10 02:15:36 +0000423 # Add empty "phony" target
424 add_custom_target(${name})
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000425 else()
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000426 if( EXCLUDE_FROM_ALL )
Nick Lewycky61aed872011-04-29 02:12:06 +0000427 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000428 else()
Hans Wennborg16546272013-08-24 00:20:36 +0000429 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000430 if(WIN32 OR CYGWIN)
431 # DLL platform
432 set(dlldir "bin")
433 else()
434 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
435 endif()
Hans Wennborg16546272013-08-24 00:20:36 +0000436 install(TARGETS ${name}
NAKAMURA Takumief976072014-02-09 16:36:03 +0000437 EXPORT LLVMExports
NAKAMURA Takumi44f64ea2014-07-13 13:33:26 +0000438 LIBRARY DESTINATION ${dlldir}
Hans Wennborg16546272013-08-24 00:20:36 +0000439 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
440 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000441 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
Oscar Fuentes638aaec2011-04-26 14:55:27 +0000442 endif()
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000443 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000444
445 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesbbc10672009-11-10 02:45:37 +0000446endmacro(add_llvm_loadable_module name)
447
448
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000449macro(add_llvm_executable name)
Oscar Fuentes2c10b222008-11-15 02:08:08 +0000450 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000451 if( EXCLUDE_FROM_ALL )
452 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
453 else()
454 add_executable(${name} ${ALL_FILES})
455 endif()
NAKAMURA Takumib524c222014-01-28 11:40:04 +0000456 llvm_update_compile_flags(${name})
Rafael Espindola8cb78762014-11-02 12:14:22 +0000457 add_link_opts( ${name} )
Nico Weberc27118d2013-12-28 23:31:44 +0000458
Richard Smith2b91a7f2014-09-26 22:40:15 +0000459 # Do not add -Dname_EXPORTS to the command-line when building files in this
460 # target. Doing so is actively harmful for the modules build because it
461 # creates extra module variants, and not useful because we don't use these
462 # macros.
463 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
464
Nico Weberc27118d2013-12-28 23:31:44 +0000465 if (LLVM_EXPORTED_SYMBOL_FILE)
466 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
467 endif(LLVM_EXPORTED_SYMBOL_FILE)
468
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000469 set(EXCLUDE_FROM_ALL OFF)
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000470 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
Oscar Fuentes978e5282011-03-29 20:51:08 +0000471 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes3fca0e82009-08-14 04:38:57 +0000472 if( LLVM_COMMON_DEPENDS )
473 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
474 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000475endmacro(add_llvm_executable name)
476
477
Hans Wennborg16546272013-08-24 00:20:36 +0000478set (LLVM_TOOLCHAIN_TOOLS
479 llvm-ar
480 llvm-objdump
481 )
482
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000483macro(add_llvm_tool name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000484 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000485 set(EXCLUDE_FROM_ALL ON)
486 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000487 add_llvm_executable(${name} ${ARGN})
Hans Wennborg16546272013-08-24 00:20:36 +0000488
489 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
490 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
491 if( LLVM_BUILD_TOOLS )
NAKAMURA Takumief976072014-02-09 16:36:03 +0000492 install(TARGETS ${name}
493 EXPORT LLVMExports
494 RUNTIME DESTINATION bin)
Hans Wennborg16546272013-08-24 00:20:36 +0000495 endif()
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000496 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000497 if( LLVM_BUILD_TOOLS )
498 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
499 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000500 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000501endmacro(add_llvm_tool name)
502
503
504macro(add_llvm_example name)
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000505 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentes0c2443a2009-11-23 00:21:43 +0000506 set(EXCLUDE_FROM_ALL ON)
507 endif()
Oscar Fuentes46d8a932010-09-25 20:25:25 +0000508 add_llvm_executable(${name} ${ARGN})
Oscar Fuentesdea579f2009-11-23 00:32:42 +0000509 if( LLVM_BUILD_EXAMPLES )
510 install(TARGETS ${name} RUNTIME DESTINATION examples)
511 endif()
Oscar Fuentes3145e922011-02-20 22:06:10 +0000512 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentesa229b3c2008-09-22 01:08:49 +0000513endmacro(add_llvm_example name)
Oscar Fuentescdc95492008-09-26 04:40:32 +0000514
515
Oscar Fuentes3145e922011-02-20 22:06:10 +0000516macro(add_llvm_utility name)
517 add_llvm_executable(${name} ${ARGN})
518 set_target_properties(${name} PROPERTIES FOLDER "Utils")
519endmacro(add_llvm_utility name)
520
521
Oscar Fuentescdc95492008-09-26 04:40:32 +0000522macro(add_llvm_target target_name)
Oscar Fuentes4f0f3352011-07-25 20:17:01 +0000523 include_directories(BEFORE
524 ${CMAKE_CURRENT_BINARY_DIR}
525 ${CMAKE_CURRENT_SOURCE_DIR})
NAKAMURA Takumi45374cc2014-03-04 17:05:28 +0000526 add_llvm_library(LLVM${target_name} ${ARGN})
Oscar Fuentesba1186c2011-02-20 02:55:27 +0000527 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentescdc95492008-09-26 04:40:32 +0000528endmacro(add_llvm_target)
Michael J. Spencere734f542012-04-26 19:43:35 +0000529
530# Add external project that may want to be built as part of llvm such as Clang,
531# lld, and Polly. This adds two options. One for the source directory of the
532# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
Alp Toker171b0c32013-12-20 00:33:39 +0000533# enable or disable building it with everything else.
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000534# Additional parameter can be specified as the name of directory.
Michael J. Spencere734f542012-04-26 19:43:35 +0000535macro(add_llvm_external_project name)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000536 set(add_llvm_external_dir "${ARGN}")
537 if("${add_llvm_external_dir}" STREQUAL "")
538 set(add_llvm_external_dir ${name})
539 endif()
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000540 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000541 string(REPLACE "-" "_" nameUNDERSCORE ${name})
542 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
543 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencere734f542012-04-26 19:43:35 +0000544 CACHE PATH "Path to ${name} source directory")
545 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
546 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
547 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
548 "Whether to build ${name} as part of LLVM" ON)
549 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi700cd402012-10-05 14:10:17 +0000550 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencere734f542012-04-26 19:43:35 +0000551 endif()
552 endif()
553endmacro(add_llvm_external_project)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000554
Argyrios Kyrtzidis7eec9d02013-08-21 19:13:44 +0000555macro(add_llvm_tool_subdirectory name)
556 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
557 add_subdirectory(${name})
558endmacro(add_llvm_tool_subdirectory)
559
560macro(ignore_llvm_tool_subdirectory name)
561 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
562endmacro(ignore_llvm_tool_subdirectory)
563
564function(add_llvm_implicit_external_projects)
565 set(list_of_implicit_subdirs "")
566 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
567 foreach(dir ${sub-dirs})
568 if(IS_DIRECTORY "${dir}")
569 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
570 if( tool_subdir_ignore EQUAL -1
571 AND EXISTS "${dir}/CMakeLists.txt")
572 get_filename_component(fn "${dir}" NAME)
573 list(APPEND list_of_implicit_subdirs "${fn}")
574 endif()
575 endif()
576 endforeach()
577
578 foreach(external_proj ${list_of_implicit_subdirs})
579 add_llvm_external_project("${external_proj}")
580 endforeach()
581endfunction(add_llvm_implicit_external_projects)
582
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000583# Generic support for adding a unittest.
Chandler Carruth94d02512012-06-21 09:51:26 +0000584function(add_unittest test_suite test_name)
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000585 if( NOT LLVM_BUILD_TESTS )
586 set(EXCLUDE_FROM_ALL ON)
587 endif()
588
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000589 # Visual Studio 2012 only supports up to 8 template parameters in
590 # std::tr1::tuple by default, but gtest requires 10
591 if (MSVC AND MSVC_VERSION EQUAL 1700)
592 list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
593 endif ()
594
595 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
596 if (NOT LLVM_ENABLE_THREADS)
597 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
598 endif ()
599
600 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
NAKAMURA Takumi13961cb2014-01-30 22:55:25 +0000601 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000602 endif ()
603
NAKAMURA Takumia679f432014-01-28 09:44:06 +0000604 set(LLVM_REQUIRES_RTTI OFF)
NAKAMURA Takumie42fd0d2014-01-07 10:24:14 +0000605
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000606 add_llvm_executable(${test_name} ${ARGN})
NAKAMURA Takumibaa9f532013-12-30 06:48:30 +0000607 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
608 set_output_directory(${test_name} ${outdir} ${outdir})
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000609 target_link_libraries(${test_name}
610 gtest
611 gtest_main
612 LLVMSupport # gtest needs it for raw_ostream.
613 )
614
615 add_dependencies(${test_suite} ${test_name})
616 get_target_property(test_suite_folder ${test_suite} FOLDER)
617 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
618 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
619 endif ()
Chandler Carrutha5d42f82012-06-21 05:16:58 +0000620endfunction()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000621
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000622function(llvm_add_go_executable binary pkgpath)
623 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
624
625 if(LLVM_BINDINGS MATCHES "go")
626 # FIXME: This should depend only on the libraries Go needs.
627 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
628 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
629 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
630 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
631 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
632 add_custom_command(OUTPUT ${binpath}
633 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "cc=${cc}" "cxx=${cxx}" "ldflags=${ldflags}"
634 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
635 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
636 ${llvmlibs} ${ARG_DEPENDS}
637 COMMENT "Building Go executable ${binary}"
638 VERBATIM)
639 if (ARG_ALL)
640 add_custom_target(${binary} ALL DEPENDS ${binpath})
641 else()
642 add_custom_target(${binary} DEPENDS ${binpath})
643 endif()
644 endif()
645endfunction()
646
Chandler Carruth3511dd32012-06-28 06:36:24 +0000647# This function provides an automatic way to 'configure'-like generate a file
Alp Toker171b0c32013-12-20 00:33:39 +0000648# based on a set of common and custom variables, specifically targeting the
Chandler Carruth3511dd32012-06-28 06:36:24 +0000649# variables needed for the 'lit.site.cfg' files. This function bundles the
650# common variables that any Lit instance is likely to need, and custom
651# variables can be passed in.
652function(configure_lit_site_cfg input output)
653 foreach(c ${LLVM_TARGETS_TO_BUILD})
654 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
655 endforeach(c)
656 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
657
658 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
Chandler Carruth3511dd32012-06-28 06:36:24 +0000659
Chandler Carruth3511dd32012-06-28 06:36:24 +0000660 # Configuration-time: See Unit/lit.site.cfg.in
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000661 if (CMAKE_CFG_INTDIR STREQUAL ".")
662 set(LLVM_BUILD_MODE ".")
663 else ()
664 set(LLVM_BUILD_MODE "%(build_mode)s")
665 endif ()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000666
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000667 # They below might not be the build tree but provided binary tree.
Chandler Carruth3511dd32012-06-28 06:36:24 +0000668 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
669 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumice78b802014-01-19 12:52:10 +0000670 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
671 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000672
673 # SHLIBDIR points the build tree.
NAKAMURA Takumi6c5fbbc2014-07-04 04:23:26 +0000674 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
NAKAMURA Takumi3c6f5cd2014-01-26 06:18:56 +0000675
Chandler Carruth3511dd32012-06-28 06:36:24 +0000676 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
NAKAMURA Takumi8d7a1732014-07-04 04:45:40 +0000677 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
678 # plugins. We may rename it.
679 if(LLVM_ENABLE_PLUGINS)
680 set(ENABLE_SHARED "1")
681 else()
682 set(ENABLE_SHARED "0")
683 endif()
Chandler Carruth3511dd32012-06-28 06:36:24 +0000684
685 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
686 set(ENABLE_ASSERTIONS "1")
687 else()
688 set(ENABLE_ASSERTIONS "0")
689 endif()
690
Tim Northoverbfe84682013-02-14 16:49:32 +0000691 set(HOST_OS ${CMAKE_SYSTEM_NAME})
692 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruth3511dd32012-06-28 06:36:24 +0000693
Peter Collingbournece800842014-10-17 18:32:36 +0000694 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
695 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
Chandler Carruth97192422014-10-21 00:36:28 +0000696 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Peter Collingbourne82e3e372014-10-16 22:48:02 +0000697
Chandler Carruth3511dd32012-06-28 06:36:24 +0000698 configure_file(${input} ${output} @ONLY)
699endfunction()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000700
701# A raw function to create a lit target. This is used to implement the testuite
702# management functions.
703function(add_lit_target target comment)
NAKAMURA Takumi69a89c72013-12-19 17:11:08 +0000704 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000705 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
706 separate_arguments(LIT_ARGS)
NAKAMURA Takumi0dc10d52013-12-04 11:15:17 +0000707 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
708 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
709 endif ()
Greg Fitzgerald962a3392014-05-21 16:44:03 +0000710 if (LLVM_MAIN_SRC_DIR)
711 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
712 else()
713 find_program(LIT_COMMAND llvm-lit)
714 endif ()
715 list(APPEND LIT_COMMAND ${LIT_ARGS})
Chandler Carruth69ce6652012-06-30 10:14:14 +0000716 foreach(param ${ARG_PARAMS})
717 list(APPEND LIT_COMMAND --param ${param})
718 endforeach()
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000719 if( ARG_DEPENDS )
720 add_custom_target(${target}
721 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
722 COMMENT "${comment}"
Peter Collingbournea7fb5cf2014-11-17 22:16:15 +0000723 ${cmake_3_2_USES_TERMINAL}
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000724 )
725 add_dependencies(${target} ${ARG_DEPENDS})
726 else()
727 add_custom_target(${target}
Nico Weberb42359d2013-12-29 00:11:20 +0000728 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
NAKAMURA Takumi04a664e2012-12-24 22:43:59 +0000729 message(STATUS "${target} does nothing.")
730 endif()
NAKAMURA Takumi03fc7302013-12-02 11:31:19 +0000731
732 # Tests should be excluded from "Build Solution".
733 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
Chandler Carruth69ce6652012-06-30 10:14:14 +0000734endfunction()
735
736# A function to add a set of lit test suites to be driven through 'check-*' targets.
737function(add_lit_testsuite target comment)
738 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
739
NAKAMURA Takumi54d22742012-10-10 13:32:55 +0000740 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
741 if(NOT EXCLUDE_FROM_ALL)
742 # Register the testsuites, params and depends for the global check rule.
743 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
744 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
745 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
746 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
747 endif()
Chandler Carruth69ce6652012-06-30 10:14:14 +0000748
749 # Produce a specific suffixed check rule.
750 add_lit_target(${target} ${comment}
751 ${ARG_DEFAULT_ARGS}
752 PARAMS ${ARG_PARAMS}
753 DEPENDS ${ARG_DEPENDS}
754 ARGS ${ARG_ARGS}
755 )
756endfunction()