[CMake] Put *_exports into "Misc" folder.
[oota-llvm.git] / cmake / modules / AddLLVM.cmake
1 include(LLVMParseArguments)
2 include(LLVMProcessSources)
3 include(LLVM-Config)
4
5 function(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})
21 endfunction()
22
23 function(add_llvm_symbol_exports target_name export_file)
24   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
25     set(native_export_file "${target_name}.exports")
26     add_custom_command(OUTPUT ${native_export_file}
27       COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
28       DEPENDS ${export_file}
29       VERBATIM
30       COMMENT "Creating export file for ${target_name}")
31     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
32                  LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
33   elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
34     # Gold and BFD ld require a version script rather than a plain list.
35     set(native_export_file "${target_name}.exports")
36     # FIXME: Don't write the "local:" line on OpenBSD.
37     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}
43       DEPENDS ${export_file}
44       VERBATIM
45       COMMENT "Creating export file for ${target_name}")
46     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
47                  LINK_FLAGS "  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
48   else()
49     set(native_export_file "${target_name}.def")
50
51     set(CAT "type")
52     if(CYGWIN)
53       set(CAT "cat")
54     endif()
55
56     # Using ${export_file} in add_custom_command directly confuses cmd.exe.
57     file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
58
59     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}
62       DEPENDS ${export_file}
63       VERBATIM
64       COMMENT "Creating export file for ${target_name}")
65     if(CYGWIN OR MINGW)
66       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
67                    LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
68     else()
69       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
70                    LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
71     endif()
72   endif()
73
74   add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
75   set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
76
77   get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
78   foreach(src ${srcs})
79     get_filename_component(extension ${src} EXT)
80     if(extension STREQUAL ".cpp")
81       set(first_source_file ${src})
82       break()
83     endif()
84   endforeach()
85
86   # Force re-linking when the exports file changes. Actually, it
87   # forces recompilation of the source file. The LINK_DEPENDS target
88   # property only works for makefile-based generators.
89   # FIXME: This is not safe because this will create the same target
90   # ${native_export_file} in several different file:
91   # - One where we emitted ${target_name}_exports
92   # - One where we emitted the build command for the following object.
93   # set_property(SOURCE ${first_source_file} APPEND PROPERTY
94   #   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
95
96   set_property(DIRECTORY APPEND
97     PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
98
99   add_dependencies(${target_name} ${target_name}_exports)
100 endfunction(add_llvm_symbol_exports)
101
102 function(add_dead_strip target_name)
103   # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
104   # But MinSizeRel seems to add that automatically, so maybe disable these
105   # flags instead if LLVM_NO_DEAD_STRIP is set.
106   if(NOT CYGWIN AND NOT MINGW AND NOT MSVC)
107     if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
108        SET(CMAKE_CXX_FLAGS
109            "${CMAKE_CXX_FLAGS}  -ffunction-sections -fdata-sections"
110            PARENT_SCOPE)
111     endif()
112   endif()
113   if(NOT LLVM_NO_DEAD_STRIP)
114     if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
115       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
116                    LINK_FLAGS " -Wl,-dead_strip")
117     elseif(NOT WIN32)
118       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
119                    LINK_FLAGS " -Wl,--gc-sections")
120     endif()
121   endif()
122 endfunction(add_dead_strip)
123
124 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
125 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
126 # or a certain builder, for eaxample, msbuild.exe, would be confused.
127 function(set_output_directory target bindir libdir)
128   if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
129     foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
130       string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
131       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
132       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
133       set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
134       set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
135       set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
136     endforeach()
137   else()
138     set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
139     set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
140     set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
141   endif()
142 endfunction()
143
144 macro(add_llvm_library name)
145   llvm_process_sources( ALL_FILES ${ARGN} )
146   add_library( ${name} ${ALL_FILES} )
147   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
148   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
149   add_dead_strip( ${name} )
150   if( LLVM_COMMON_DEPENDS )
151     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
152   endif( LLVM_COMMON_DEPENDS )
153
154   if( BUILD_SHARED_LIBS )
155     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
156     if (MSVC)
157       set_target_properties(${name}
158         PROPERTIES
159         IMPORT_SUFFIX ".imp")
160     endif ()
161
162     if (LLVM_EXPORTED_SYMBOL_FILE)
163       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
164     endif()
165   endif()
166
167   # Ensure that the system libraries always comes last on the
168   # list. Without this, linking the unit tests on MinGW fails.
169   link_system_libs( ${name} )
170
171   if( EXCLUDE_FROM_ALL )
172     set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
173   else()
174     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
175       install(TARGETS ${name}
176         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
177         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
178     endif()
179   endif()
180   set_target_properties(${name} PROPERTIES FOLDER "Libraries")
181
182   # Add the explicit dependency information for this library.
183   #
184   # It would be nice to verify that we have the dependencies for this library
185   # name, but using get_property(... SET) doesn't suffice to determine if a
186   # property has been set to an empty value.
187   get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
188   target_link_libraries(${name} ${lib_deps})
189 endmacro(add_llvm_library name)
190
191 macro(add_llvm_loadable_module name)
192   if( NOT LLVM_ON_UNIX OR CYGWIN )
193     message(STATUS "Loadable modules not supported on this platform.
194 ${name} ignored.")
195     # Add empty "phony" target
196     add_custom_target(${name})
197   else()
198     llvm_process_sources( ALL_FILES ${ARGN} )
199     if (MODULE)
200       set(libkind MODULE)
201     else()
202       set(libkind SHARED)
203     endif()
204
205     add_library( ${name} ${libkind} ${ALL_FILES} )
206     set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
207     set_target_properties( ${name} PROPERTIES PREFIX "" )
208     add_dead_strip( ${name} )
209
210     if (LLVM_EXPORTED_SYMBOL_FILE)
211       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
212     endif(LLVM_EXPORTED_SYMBOL_FILE)
213
214     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
215     link_system_libs( ${name} )
216
217     if (APPLE)
218       # Darwin-specific linker flags for loadable modules.
219       set_property(TARGET ${name} APPEND_STRING PROPERTY
220         LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
221     endif()
222
223     if (MODULE)
224       set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
225     endif ()
226
227     if( EXCLUDE_FROM_ALL )
228       set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
229     else()
230       if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
231         install(TARGETS ${name}
232           LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
233           ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
234       endif()
235     endif()
236   endif()
237
238   set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
239 endmacro(add_llvm_loadable_module name)
240
241
242 macro(add_llvm_executable name)
243   llvm_process_sources( ALL_FILES ${ARGN} )
244   if( EXCLUDE_FROM_ALL )
245     add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
246   else()
247     add_executable(${name} ${ALL_FILES})
248   endif()
249   add_dead_strip( ${name} )
250
251   if (LLVM_EXPORTED_SYMBOL_FILE)
252     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
253   endif(LLVM_EXPORTED_SYMBOL_FILE)
254
255   set(EXCLUDE_FROM_ALL OFF)
256   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
257   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
258   if( LLVM_COMMON_DEPENDS )
259     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
260   endif( LLVM_COMMON_DEPENDS )
261   link_system_libs( ${name} )
262 endmacro(add_llvm_executable name)
263
264
265 set (LLVM_TOOLCHAIN_TOOLS
266   llvm-ar
267   llvm-objdump
268   )
269
270 macro(add_llvm_tool name)
271   if( NOT LLVM_BUILD_TOOLS )
272     set(EXCLUDE_FROM_ALL ON)
273   endif()
274   add_llvm_executable(${name} ${ARGN})
275
276   list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
277   if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
278     if( LLVM_BUILD_TOOLS )
279       install(TARGETS ${name} RUNTIME DESTINATION bin)
280     endif()
281   endif()
282   set_target_properties(${name} PROPERTIES FOLDER "Tools")
283 endmacro(add_llvm_tool name)
284
285
286 macro(add_llvm_example name)
287   if( NOT LLVM_BUILD_EXAMPLES )
288     set(EXCLUDE_FROM_ALL ON)
289   endif()
290   add_llvm_executable(${name} ${ARGN})
291   if( LLVM_BUILD_EXAMPLES )
292     install(TARGETS ${name} RUNTIME DESTINATION examples)
293   endif()
294   set_target_properties(${name} PROPERTIES FOLDER "Examples")
295 endmacro(add_llvm_example name)
296
297
298 macro(add_llvm_utility name)
299   add_llvm_executable(${name} ${ARGN})
300   set_target_properties(${name} PROPERTIES FOLDER "Utils")
301 endmacro(add_llvm_utility name)
302
303
304 macro(add_llvm_target target_name)
305   include_directories(BEFORE
306     ${CMAKE_CURRENT_BINARY_DIR}
307     ${CMAKE_CURRENT_SOURCE_DIR})
308   add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
309   set( CURRENT_LLVM_TARGET LLVM${target_name} )
310 endmacro(add_llvm_target)
311
312 # Add external project that may want to be built as part of llvm such as Clang,
313 # lld, and Polly. This adds two options. One for the source directory of the
314 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
315 # enable or disable building it with everything else.
316 # Additional parameter can be specified as the name of directory.
317 macro(add_llvm_external_project name)
318   set(add_llvm_external_dir "${ARGN}")
319   if("${add_llvm_external_dir}" STREQUAL "")
320     set(add_llvm_external_dir ${name})
321   endif()
322   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
323   string(REPLACE "-" "_" nameUNDERSCORE ${name})
324   string(TOUPPER ${nameUNDERSCORE} nameUPPER)
325   set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
326       CACHE PATH "Path to ${name} source directory")
327   if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
328       AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
329     option(LLVM_EXTERNAL_${nameUPPER}_BUILD
330            "Whether to build ${name} as part of LLVM" ON)
331     if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
332       add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
333     endif()
334   endif()
335 endmacro(add_llvm_external_project)
336
337 macro(add_llvm_tool_subdirectory name)
338   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
339   add_subdirectory(${name})
340 endmacro(add_llvm_tool_subdirectory)
341
342 macro(ignore_llvm_tool_subdirectory name)
343   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
344 endmacro(ignore_llvm_tool_subdirectory)
345
346 function(add_llvm_implicit_external_projects)
347   set(list_of_implicit_subdirs "")
348   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
349   foreach(dir ${sub-dirs})
350     if(IS_DIRECTORY "${dir}")
351       list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
352       if( tool_subdir_ignore EQUAL -1
353           AND EXISTS "${dir}/CMakeLists.txt")
354         get_filename_component(fn "${dir}" NAME)
355         list(APPEND list_of_implicit_subdirs "${fn}")
356       endif()
357     endif()
358   endforeach()
359
360   foreach(external_proj ${list_of_implicit_subdirs})
361     add_llvm_external_project("${external_proj}")
362   endforeach()
363 endfunction(add_llvm_implicit_external_projects)
364
365 # Generic support for adding a unittest.
366 function(add_unittest test_suite test_name)
367   if( NOT LLVM_BUILD_TESTS )
368     set(EXCLUDE_FROM_ALL ON)
369   endif()
370
371   # Visual Studio 2012 only supports up to 8 template parameters in
372   # std::tr1::tuple by default, but gtest requires 10
373   if (MSVC AND MSVC_VERSION EQUAL 1700)
374     list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
375   endif ()
376
377   include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
378   if (NOT LLVM_ENABLE_THREADS)
379     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
380   endif ()
381
382   if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
383     set(LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
384   endif ()
385
386   set(LLVM_NO_RTTI ON)
387
388   add_llvm_executable(${test_name} ${ARGN})
389   set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
390   set_output_directory(${test_name} ${outdir} ${outdir})
391   target_link_libraries(${test_name}
392     gtest
393     gtest_main
394     LLVMSupport # gtest needs it for raw_ostream.
395     )
396
397   add_dependencies(${test_suite} ${test_name})
398   get_target_property(test_suite_folder ${test_suite} FOLDER)
399   if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
400     set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
401   endif ()
402   llvm_update_compile_flags(${test_name})
403 endfunction()
404
405 # This function provides an automatic way to 'configure'-like generate a file
406 # based on a set of common and custom variables, specifically targeting the
407 # variables needed for the 'lit.site.cfg' files. This function bundles the
408 # common variables that any Lit instance is likely to need, and custom
409 # variables can be passed in.
410 function(configure_lit_site_cfg input output)
411   foreach(c ${LLVM_TARGETS_TO_BUILD})
412     set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
413   endforeach(c)
414   set(TARGETS_TO_BUILD ${TARGETS_BUILT})
415
416   set(SHLIBEXT "${LTDL_SHLIB_EXT}")
417
418   if(BUILD_SHARED_LIBS)
419     set(LLVM_SHARED_LIBS_ENABLED "1")
420   else()
421     set(LLVM_SHARED_LIBS_ENABLED "0")
422   endif(BUILD_SHARED_LIBS)
423
424   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
425     set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
426   else() # Default for all other unix like systems.
427     # CMake hardcodes the library locaction using rpath.
428     # Therefore LD_LIBRARY_PATH is not required to run binaries in the
429     # build dir. We pass it anyways.
430     set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
431   endif()
432
433   # Configuration-time: See Unit/lit.site.cfg.in
434   if (CMAKE_CFG_INTDIR STREQUAL ".")
435     set(LLVM_BUILD_MODE ".")
436   else ()
437     set(LLVM_BUILD_MODE "%(build_mode)s")
438   endif ()
439
440   # They below might not be the build tree but provided binary tree.
441   set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
442   set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
443   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
444   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
445
446   # SHLIBDIR points the build tree.
447   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
448
449   set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
450   set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
451   set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
452
453   if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
454     set(ENABLE_ASSERTIONS "1")
455   else()
456     set(ENABLE_ASSERTIONS "0")
457   endif()
458
459   set(HOST_OS ${CMAKE_SYSTEM_NAME})
460   set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
461
462   if (CLANG_ENABLE_ARCMT)
463     set(ENABLE_CLANG_ARCMT "1")
464   else()
465     set(ENABLE_CLANG_ARCMT "0")
466   endif()
467   if (CLANG_ENABLE_REWRITER)
468     set(ENABLE_CLANG_REWRITER "1")
469   else()
470     set(ENABLE_CLANG_REWRITER "0")
471   endif()
472   if (CLANG_ENABLE_STATIC_ANALYZER)
473     set(ENABLE_CLANG_STATIC_ANALYZER "1")
474   else()
475     set(ENABLE_CLANG_STATIC_ANALYZER "0")
476   endif()
477
478   configure_file(${input} ${output} @ONLY)
479 endfunction()
480
481 # A raw function to create a lit target. This is used to implement the testuite
482 # management functions.
483 function(add_lit_target target comment)
484   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
485   set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
486   separate_arguments(LIT_ARGS)
487   if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
488     list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
489   endif ()
490   set(LIT_COMMAND
491     ${PYTHON_EXECUTABLE}
492     ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
493     ${LIT_ARGS}
494     )
495   foreach(param ${ARG_PARAMS})
496     list(APPEND LIT_COMMAND --param ${param})
497   endforeach()
498   if( ARG_DEPENDS )
499     add_custom_target(${target}
500       COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
501       COMMENT "${comment}"
502       )
503     add_dependencies(${target} ${ARG_DEPENDS})
504   else()
505     add_custom_target(${target}
506       COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
507     message(STATUS "${target} does nothing.")
508   endif()
509
510   # Tests should be excluded from "Build Solution".
511   set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
512 endfunction()
513
514 # A function to add a set of lit test suites to be driven through 'check-*' targets.
515 function(add_lit_testsuite target comment)
516   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
517
518   # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
519   if(NOT EXCLUDE_FROM_ALL)
520     # Register the testsuites, params and depends for the global check rule.
521     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
522     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
523     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
524     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
525   endif()
526
527   # Produce a specific suffixed check rule.
528   add_lit_target(${target} ${comment}
529     ${ARG_DEFAULT_ARGS}
530     PARAMS ${ARG_PARAMS}
531     DEPENDS ${ARG_DEPENDS}
532     ARGS ${ARG_ARGS}
533     )
534 endfunction()