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