Prune debug message in AddLLVM.cmake.
[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(sources TARGET ${name} PROPERTY SOURCES)
7   if("${sources}" MATCHES "\\.c(;|$)")
8     set(update_src_props ON)
9   endif()
10
11   if(LLVM_REQUIRES_EH)
12     set(LLVM_REQUIRES_RTTI ON)
13   else()
14     if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
15       list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
16     elseif(MSVC)
17       list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
18       list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
19     endif()
20   endif()
21
22   if(NOT LLVM_REQUIRES_RTTI)
23     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
24     if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
25       list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
26     elseif (MSVC)
27       list(APPEND LLVM_COMPILE_FLAGS "/GR-")
28     endif ()
29   endif()
30
31   # Assume that;
32   #   - LLVM_COMPILE_FLAGS is list.
33   #   - PROPERTY COMPILE_FLAGS is string.
34   string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
35
36   if(update_src_props)
37     foreach(fn ${sources})
38       get_filename_component(suf ${fn} EXT)
39       if("${suf}" STREQUAL ".cpp")
40         set_property(SOURCE ${fn} APPEND_STRING PROPERTY
41           COMPILE_FLAGS "${target_compile_flags}")
42       endif()
43     endforeach()
44   else()
45     # Update target props, since all sources are C++.
46     set_property(TARGET ${name} APPEND_STRING PROPERTY
47       COMPILE_FLAGS "${target_compile_flags}")
48   endif()
49
50   set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
51 endfunction()
52
53 function(add_llvm_symbol_exports target_name export_file)
54   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
55     set(native_export_file "${target_name}.exports")
56     add_custom_command(OUTPUT ${native_export_file}
57       COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
58       DEPENDS ${export_file}
59       VERBATIM
60       COMMENT "Creating export file for ${target_name}")
61     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
62                  LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
63   elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
64     # Gold and BFD ld require a version script rather than a plain list.
65     set(native_export_file "${target_name}.exports")
66     # FIXME: Don't write the "local:" line on OpenBSD.
67     add_custom_command(OUTPUT ${native_export_file}
68       COMMAND echo "{" > ${native_export_file}
69       COMMAND grep -q "[[:alnum:]]" ${export_file} && echo "  global:" >> ${native_export_file} || :
70       COMMAND sed -e "s/$/;/" -e "s/^/    /" < ${export_file} >> ${native_export_file}
71       COMMAND echo "  local: *;" >> ${native_export_file}
72       COMMAND echo "};" >> ${native_export_file}
73       DEPENDS ${export_file}
74       VERBATIM
75       COMMENT "Creating export file for ${target_name}")
76     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
77                  LINK_FLAGS "  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
78   else()
79     set(native_export_file "${target_name}.def")
80
81     set(CAT "type")
82     if(CYGWIN)
83       set(CAT "cat")
84     endif()
85
86     # Using ${export_file} in add_custom_command directly confuses cmd.exe.
87     file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
88
89     add_custom_command(OUTPUT ${native_export_file}
90       COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
91       COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
92       DEPENDS ${export_file}
93       VERBATIM
94       COMMENT "Creating export file for ${target_name}")
95     if(CYGWIN OR MINGW)
96       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
97                    LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
98     else()
99       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
100                    LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
101     endif()
102   endif()
103
104   add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
105   set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
106
107   get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
108   foreach(src ${srcs})
109     get_filename_component(extension ${src} EXT)
110     if(extension STREQUAL ".cpp")
111       set(first_source_file ${src})
112       break()
113     endif()
114   endforeach()
115
116   # Force re-linking when the exports file changes. Actually, it
117   # forces recompilation of the source file. The LINK_DEPENDS target
118   # property only works for makefile-based generators.
119   # FIXME: This is not safe because this will create the same target
120   # ${native_export_file} in several different file:
121   # - One where we emitted ${target_name}_exports
122   # - One where we emitted the build command for the following object.
123   # set_property(SOURCE ${first_source_file} APPEND PROPERTY
124   #   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
125
126   set_property(DIRECTORY APPEND
127     PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
128
129   add_dependencies(${target_name} ${target_name}_exports)
130
131   # Add dependency to *_exports later -- CMake issue 14747
132   list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
133   set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
134 endfunction(add_llvm_symbol_exports)
135
136 function(add_dead_strip target_name)
137   if(NOT LLVM_NO_DEAD_STRIP)
138     if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
139       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
140                    LINK_FLAGS " -Wl,-dead_strip")
141     elseif(NOT WIN32)
142       # Object files are compiled with -ffunction-data-sections.
143       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
144                    LINK_FLAGS " -Wl,--gc-sections")
145     endif()
146   endif()
147 endfunction(add_dead_strip)
148
149 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
150 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
151 # or a certain builder, for eaxample, msbuild.exe, would be confused.
152 function(set_output_directory target bindir libdir)
153   if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
154     foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
155       string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
156       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
157       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
158       set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
159       set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
160       set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
161     endforeach()
162   else()
163     set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
164     set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
165     set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
166   endif()
167 endfunction()
168
169 # llvm_add_library(name sources...
170 #   SHARED;STATIC
171 #     STATIC by default w/o BUILD_SHARED_LIBS.
172 #     SHARED by default w/  BUILD_SHARED_LIBS.
173 #   MODULE
174 #     Target ${name} might not be created on unsupported platforms.
175 #     Check with "if(TARGET ${name})".
176 #   OUTPUT_NAME name
177 #     Corresponds to OUTPUT_NAME in target properties.
178 #   DEPENDS targets...
179 #     Same semantics as add_dependencies().
180 #   LINK_COMPONENTS components...
181 #     Same as the variable LLVM_LINK_COMPONENTS.
182 #   LINK_LIBS lib_targets...
183 #     Same semantics as target_link_libraries().
184 #   ADDITIONAL_HEADERS
185 #     May specify header files for IDE generators.
186 #   )
187 function(llvm_add_library name)
188   cmake_parse_arguments(ARG
189     "MODULE;SHARED;STATIC"
190     "OUTPUT_NAME"
191     "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
192     ${ARGN})
193   list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
194   if(ARG_ADDITIONAL_HEADERS)
195     # Pass through ADDITIONAL_HEADERS.
196     set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
197   endif()
198   if(ARG_OBJLIBS)
199     set(ALL_FILES ${ARG_OBJLIBS})
200   else()
201     llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
202   endif()
203
204   if(ARG_MODULE)
205     if(ARG_SHARED OR ARG_STATIC)
206       message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
207     endif()
208     if(NOT LLVM_ON_UNIX OR CYGWIN)
209       message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
210       return()
211     endif()
212   else()
213     if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
214       set(ARG_SHARED TRUE)
215     endif()
216     if(NOT ARG_SHARED)
217       set(ARG_STATIC TRUE)
218     endif()
219   endif()
220
221   # Generate objlib
222   if(ARG_SHARED AND ARG_STATIC)
223     # Generate an obj library for both targets.
224     set(obj_name "obj.${name}")
225     add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
226       ${ALL_FILES}
227       )
228     llvm_update_compile_flags(${obj_name})
229     set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
230
231     # Do add_dependencies(obj) later due to CMake issue 14747.
232     list(APPEND objlibs ${obj_name})
233
234     set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
235   endif()
236
237   if(ARG_SHARED AND ARG_STATIC)
238     # static
239     set(name_static "${name}_static")
240     if(ARG_OUTPUT_NAME)
241       set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}_static")
242     endif()
243     # DEPENDS has been appended to LLVM_COMMON_LIBS.
244     llvm_add_library(${name_static} STATIC
245       ${output_name}
246       OBJLIBS ${ALL_FILES} # objlib
247       LINK_LIBS ${ARG_LINK_LIBS}
248       LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
249       )
250     # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
251     set(ARG_STATIC)
252   endif()
253
254   if(ARG_MODULE)
255     add_library(${name} MODULE ${ALL_FILES})
256   elseif(ARG_SHARED)
257     add_library(${name} SHARED ${ALL_FILES})
258   else()
259     add_library(${name} STATIC ${ALL_FILES})
260   endif()
261   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
262   llvm_update_compile_flags(${name})
263   add_dead_strip( ${name} )
264   if(ARG_OUTPUT_NAME)
265     set_target_properties(${name}
266       PROPERTIES
267       OUTPUT_NAME ${ARG_OUTPUT_NAME}
268       )
269   endif()
270
271   if(ARG_MODULE)
272     set_target_properties(${name} PROPERTIES
273       PREFIX ""
274       SUFFIX ${LLVM_PLUGIN_EXT}
275       )
276   endif()
277
278   if(ARG_SHARED)
279     if (MSVC)
280       set_target_properties(${name}
281         PROPERTIES
282         IMPORT_SUFFIX ".imp")
283     endif ()
284   endif()
285
286   if(ARG_MODULE OR ARG_SHARED)
287     if (LLVM_EXPORTED_SYMBOL_FILE)
288       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
289     endif()
290   endif()
291
292   target_link_libraries(${name} ${ARG_LINK_LIBS})
293
294   llvm_config(${name} ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
295
296   if(LLVM_COMMON_DEPENDS)
297     add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
298     # Add dependencies also to objlibs.
299     # CMake issue 14747 --  add_dependencies() might be ignored to objlib's user.
300     foreach(objlib ${objlibs})
301       add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
302     endforeach()
303   endif()
304 endfunction()
305
306 macro(add_llvm_library name)
307   if( BUILD_SHARED_LIBS )
308     llvm_add_library(${name} SHARED ${ARGN})
309   else()
310     llvm_add_library(${name} ${ARGN})
311   endif()
312   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
313
314   if( EXCLUDE_FROM_ALL )
315     set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
316   else()
317     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
318       install(TARGETS ${name}
319         EXPORT LLVMExports
320         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
321         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
322     endif()
323     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
324   endif()
325   set_target_properties(${name} PROPERTIES FOLDER "Libraries")
326
327   # Add the explicit dependency information for this library.
328   #
329   # It would be nice to verify that we have the dependencies for this library
330   # name, but using get_property(... SET) doesn't suffice to determine if a
331   # property has been set to an empty value.
332   get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
333   target_link_libraries(${name} ${lib_deps})
334 endmacro(add_llvm_library name)
335
336 macro(add_llvm_loadable_module name)
337   llvm_add_library(${name} MODULE ${ARGN})
338   if(NOT TARGET ${name})
339     # Add empty "phony" target
340     add_custom_target(${name})
341   else()
342     if( EXCLUDE_FROM_ALL )
343       set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
344     else()
345       if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
346         install(TARGETS ${name}
347           EXPORT LLVMExports
348           LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
349           ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
350       endif()
351       set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
352     endif()
353   endif()
354
355   set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
356 endmacro(add_llvm_loadable_module name)
357
358
359 macro(add_llvm_executable name)
360   llvm_process_sources( ALL_FILES ${ARGN} )
361   if( EXCLUDE_FROM_ALL )
362     add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
363   else()
364     add_executable(${name} ${ALL_FILES})
365   endif()
366   llvm_update_compile_flags(${name})
367   add_dead_strip( ${name} )
368
369   if (LLVM_EXPORTED_SYMBOL_FILE)
370     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
371   endif(LLVM_EXPORTED_SYMBOL_FILE)
372
373   set(EXCLUDE_FROM_ALL OFF)
374   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
375   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
376   if( LLVM_COMMON_DEPENDS )
377     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
378   endif( LLVM_COMMON_DEPENDS )
379 endmacro(add_llvm_executable name)
380
381
382 set (LLVM_TOOLCHAIN_TOOLS
383   llvm-ar
384   llvm-objdump
385   )
386
387 macro(add_llvm_tool name)
388   if( NOT LLVM_BUILD_TOOLS )
389     set(EXCLUDE_FROM_ALL ON)
390   endif()
391   add_llvm_executable(${name} ${ARGN})
392
393   list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
394   if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
395     if( LLVM_BUILD_TOOLS )
396       install(TARGETS ${name}
397               EXPORT LLVMExports
398               RUNTIME DESTINATION bin)
399     endif()
400   endif()
401   if( LLVM_BUILD_TOOLS )
402     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
403   endif()
404   set_target_properties(${name} PROPERTIES FOLDER "Tools")
405 endmacro(add_llvm_tool name)
406
407
408 macro(add_llvm_example name)
409   if( NOT LLVM_BUILD_EXAMPLES )
410     set(EXCLUDE_FROM_ALL ON)
411   endif()
412   add_llvm_executable(${name} ${ARGN})
413   if( LLVM_BUILD_EXAMPLES )
414     install(TARGETS ${name} RUNTIME DESTINATION examples)
415   endif()
416   set_target_properties(${name} PROPERTIES FOLDER "Examples")
417 endmacro(add_llvm_example name)
418
419
420 macro(add_llvm_utility name)
421   add_llvm_executable(${name} ${ARGN})
422   set_target_properties(${name} PROPERTIES FOLDER "Utils")
423 endmacro(add_llvm_utility name)
424
425
426 macro(add_llvm_target target_name)
427   include_directories(BEFORE
428     ${CMAKE_CURRENT_BINARY_DIR}
429     ${CMAKE_CURRENT_SOURCE_DIR})
430   add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
431   set( CURRENT_LLVM_TARGET LLVM${target_name} )
432 endmacro(add_llvm_target)
433
434 # Add external project that may want to be built as part of llvm such as Clang,
435 # lld, and Polly. This adds two options. One for the source directory of the
436 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
437 # enable or disable building it with everything else.
438 # Additional parameter can be specified as the name of directory.
439 macro(add_llvm_external_project name)
440   set(add_llvm_external_dir "${ARGN}")
441   if("${add_llvm_external_dir}" STREQUAL "")
442     set(add_llvm_external_dir ${name})
443   endif()
444   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
445   string(REPLACE "-" "_" nameUNDERSCORE ${name})
446   string(TOUPPER ${nameUNDERSCORE} nameUPPER)
447   set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
448       CACHE PATH "Path to ${name} source directory")
449   if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
450       AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
451     option(LLVM_EXTERNAL_${nameUPPER}_BUILD
452            "Whether to build ${name} as part of LLVM" ON)
453     if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
454       add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
455     endif()
456   endif()
457 endmacro(add_llvm_external_project)
458
459 macro(add_llvm_tool_subdirectory name)
460   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
461   add_subdirectory(${name})
462 endmacro(add_llvm_tool_subdirectory)
463
464 macro(ignore_llvm_tool_subdirectory name)
465   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
466 endmacro(ignore_llvm_tool_subdirectory)
467
468 function(add_llvm_implicit_external_projects)
469   set(list_of_implicit_subdirs "")
470   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
471   foreach(dir ${sub-dirs})
472     if(IS_DIRECTORY "${dir}")
473       list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
474       if( tool_subdir_ignore EQUAL -1
475           AND EXISTS "${dir}/CMakeLists.txt")
476         get_filename_component(fn "${dir}" NAME)
477         list(APPEND list_of_implicit_subdirs "${fn}")
478       endif()
479     endif()
480   endforeach()
481
482   foreach(external_proj ${list_of_implicit_subdirs})
483     add_llvm_external_project("${external_proj}")
484   endforeach()
485 endfunction(add_llvm_implicit_external_projects)
486
487 # Generic support for adding a unittest.
488 function(add_unittest test_suite test_name)
489   if( NOT LLVM_BUILD_TESTS )
490     set(EXCLUDE_FROM_ALL ON)
491   endif()
492
493   # Visual Studio 2012 only supports up to 8 template parameters in
494   # std::tr1::tuple by default, but gtest requires 10
495   if (MSVC AND MSVC_VERSION EQUAL 1700)
496     list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
497   endif ()
498
499   include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
500   if (NOT LLVM_ENABLE_THREADS)
501     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
502   endif ()
503
504   if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
505     list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
506   endif ()
507
508   set(LLVM_REQUIRES_RTTI OFF)
509
510   add_llvm_executable(${test_name} ${ARGN})
511   set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
512   set_output_directory(${test_name} ${outdir} ${outdir})
513   target_link_libraries(${test_name}
514     gtest
515     gtest_main
516     LLVMSupport # gtest needs it for raw_ostream.
517     )
518
519   add_dependencies(${test_suite} ${test_name})
520   get_target_property(test_suite_folder ${test_suite} FOLDER)
521   if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
522     set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
523   endif ()
524 endfunction()
525
526 # This function provides an automatic way to 'configure'-like generate a file
527 # based on a set of common and custom variables, specifically targeting the
528 # variables needed for the 'lit.site.cfg' files. This function bundles the
529 # common variables that any Lit instance is likely to need, and custom
530 # variables can be passed in.
531 function(configure_lit_site_cfg input output)
532   foreach(c ${LLVM_TARGETS_TO_BUILD})
533     set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
534   endforeach(c)
535   set(TARGETS_TO_BUILD ${TARGETS_BUILT})
536
537   set(SHLIBEXT "${LTDL_SHLIB_EXT}")
538
539   if(BUILD_SHARED_LIBS)
540     set(LLVM_SHARED_LIBS_ENABLED "1")
541   else()
542     set(LLVM_SHARED_LIBS_ENABLED "0")
543   endif(BUILD_SHARED_LIBS)
544
545   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
546     set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
547   else() # Default for all other unix like systems.
548     # CMake hardcodes the library locaction using rpath.
549     # Therefore LD_LIBRARY_PATH is not required to run binaries in the
550     # build dir. We pass it anyways.
551     set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
552   endif()
553
554   # Configuration-time: See Unit/lit.site.cfg.in
555   if (CMAKE_CFG_INTDIR STREQUAL ".")
556     set(LLVM_BUILD_MODE ".")
557   else ()
558     set(LLVM_BUILD_MODE "%(build_mode)s")
559   endif ()
560
561   # They below might not be the build tree but provided binary tree.
562   set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
563   set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
564   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
565   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
566
567   # SHLIBDIR points the build tree.
568   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
569
570   set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
571   set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
572   set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
573
574   if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
575     set(ENABLE_ASSERTIONS "1")
576   else()
577     set(ENABLE_ASSERTIONS "0")
578   endif()
579
580   set(HOST_OS ${CMAKE_SYSTEM_NAME})
581   set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
582
583   if (CLANG_ENABLE_ARCMT)
584     set(ENABLE_CLANG_ARCMT "1")
585   else()
586     set(ENABLE_CLANG_ARCMT "0")
587   endif()
588   if (CLANG_ENABLE_REWRITER)
589     set(ENABLE_CLANG_REWRITER "1")
590   else()
591     set(ENABLE_CLANG_REWRITER "0")
592   endif()
593   if (CLANG_ENABLE_STATIC_ANALYZER)
594     set(ENABLE_CLANG_STATIC_ANALYZER "1")
595   else()
596     set(ENABLE_CLANG_STATIC_ANALYZER "0")
597   endif()
598
599   configure_file(${input} ${output} @ONLY)
600 endfunction()
601
602 # A raw function to create a lit target. This is used to implement the testuite
603 # management functions.
604 function(add_lit_target target comment)
605   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
606   set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
607   separate_arguments(LIT_ARGS)
608   if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
609     list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
610   endif ()
611   set(LIT_COMMAND
612     ${PYTHON_EXECUTABLE}
613     ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
614     ${LIT_ARGS}
615     )
616   foreach(param ${ARG_PARAMS})
617     list(APPEND LIT_COMMAND --param ${param})
618   endforeach()
619   if( ARG_DEPENDS )
620     add_custom_target(${target}
621       COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
622       COMMENT "${comment}"
623       )
624     add_dependencies(${target} ${ARG_DEPENDS})
625   else()
626     add_custom_target(${target}
627       COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
628     message(STATUS "${target} does nothing.")
629   endif()
630
631   # Tests should be excluded from "Build Solution".
632   set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
633 endfunction()
634
635 # A function to add a set of lit test suites to be driven through 'check-*' targets.
636 function(add_lit_testsuite target comment)
637   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
638
639   # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
640   if(NOT EXCLUDE_FROM_ALL)
641     # Register the testsuites, params and depends for the global check rule.
642     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
643     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
644     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
645     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
646   endif()
647
648   # Produce a specific suffixed check rule.
649   add_lit_target(${target} ${comment}
650     ${ARG_DEFAULT_ARGS}
651     PARAMS ${ARG_PARAMS}
652     DEPENDS ${ARG_DEPENDS}
653     ARGS ${ARG_ARGS}
654     )
655 endfunction()