[CMake] Fix set_output_directory to do nothing if *_OUTPUT_INTDIR was not given.
[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   # Do nothing if *_OUTPUT_INTDIR is empty.
154   if("${bindir}" STREQUAL "")
155     return()
156   endif()
157
158   if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
159     foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
160       string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
161       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
162       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
163       set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
164       set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
165       set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
166     endforeach()
167   else()
168     set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
169     set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
170     set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
171   endif()
172 endfunction()
173
174 # llvm_add_library(name sources...
175 #   SHARED;STATIC
176 #     STATIC by default w/o BUILD_SHARED_LIBS.
177 #     SHARED by default w/  BUILD_SHARED_LIBS.
178 #   MODULE
179 #     Target ${name} might not be created on unsupported platforms.
180 #     Check with "if(TARGET ${name})".
181 #   OUTPUT_NAME name
182 #     Corresponds to OUTPUT_NAME in target properties.
183 #   DEPENDS targets...
184 #     Same semantics as add_dependencies().
185 #   LINK_COMPONENTS components...
186 #     Same as the variable LLVM_LINK_COMPONENTS.
187 #   LINK_LIBS lib_targets...
188 #     Same semantics as target_link_libraries().
189 #   ADDITIONAL_HEADERS
190 #     May specify header files for IDE generators.
191 #   )
192 function(llvm_add_library name)
193   cmake_parse_arguments(ARG
194     "MODULE;SHARED;STATIC"
195     "OUTPUT_NAME"
196     "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
197     ${ARGN})
198   list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
199   if(ARG_ADDITIONAL_HEADERS)
200     # Pass through ADDITIONAL_HEADERS.
201     set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
202   endif()
203   if(ARG_OBJLIBS)
204     set(ALL_FILES ${ARG_OBJLIBS})
205   else()
206     llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
207   endif()
208
209   if(ARG_MODULE)
210     if(ARG_SHARED OR ARG_STATIC)
211       message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
212     endif()
213     if(NOT LLVM_ON_UNIX OR CYGWIN)
214       message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
215       return()
216     endif()
217   else()
218     if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
219       set(ARG_SHARED TRUE)
220     endif()
221     if(NOT ARG_SHARED)
222       set(ARG_STATIC TRUE)
223     endif()
224   endif()
225
226   # Generate objlib
227   if(ARG_SHARED AND ARG_STATIC)
228     # Generate an obj library for both targets.
229     set(obj_name "obj.${name}")
230     add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
231       ${ALL_FILES}
232       )
233     llvm_update_compile_flags(${obj_name})
234     set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
235
236     # Do add_dependencies(obj) later due to CMake issue 14747.
237     list(APPEND objlibs ${obj_name})
238
239     set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
240   endif()
241
242   if(ARG_SHARED AND ARG_STATIC)
243     # static
244     set(name_static "${name}_static")
245     if(ARG_OUTPUT_NAME)
246       set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
247     endif()
248     # DEPENDS has been appended to LLVM_COMMON_LIBS.
249     llvm_add_library(${name_static} STATIC
250       ${output_name}
251       OBJLIBS ${ALL_FILES} # objlib
252       LINK_LIBS ${ARG_LINK_LIBS}
253       LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
254       )
255     # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
256     set(ARG_STATIC)
257   endif()
258
259   if(ARG_MODULE)
260     add_library(${name} MODULE ${ALL_FILES})
261   elseif(ARG_SHARED)
262     add_library(${name} SHARED ${ALL_FILES})
263   else()
264     add_library(${name} STATIC ${ALL_FILES})
265   endif()
266   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
267   llvm_update_compile_flags(${name})
268   add_dead_strip( ${name} )
269   if(ARG_OUTPUT_NAME)
270     set_target_properties(${name}
271       PROPERTIES
272       OUTPUT_NAME ${ARG_OUTPUT_NAME}
273       )
274   endif()
275
276   if(ARG_MODULE)
277     set_target_properties(${name} PROPERTIES
278       PREFIX ""
279       SUFFIX ${LLVM_PLUGIN_EXT}
280       )
281   endif()
282
283   if(ARG_SHARED)
284     if(WIN32)
285       set_target_properties(${name} PROPERTIES
286         PREFIX ""
287         )
288     endif()
289     if (MSVC)
290       set_target_properties(${name}
291         PROPERTIES
292         IMPORT_SUFFIX ".imp")
293     endif ()
294   endif()
295
296   if(ARG_MODULE OR ARG_SHARED)
297     if (LLVM_EXPORTED_SYMBOL_FILE)
298       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
299     endif()
300   endif()
301
302   # Add the explicit dependency information for this library.
303   #
304   # It would be nice to verify that we have the dependencies for this library
305   # name, but using get_property(... SET) doesn't suffice to determine if a
306   # property has been set to an empty value.
307   get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
308
309   llvm_map_components_to_libnames(llvm_libs
310     ${ARG_LINK_COMPONENTS}
311     ${LLVM_LINK_COMPONENTS}
312     )
313
314   if(CMAKE_VERSION VERSION_LESS 2.8.12)
315     # Link libs w/o keywords, assuming PUBLIC.
316     target_link_libraries(${name}
317       ${ARG_LINK_LIBS}
318       ${lib_deps}
319       ${llvm_libs}
320       )
321   elseif(ARG_STATIC)
322     target_link_libraries(${name} INTERFACE
323       ${ARG_LINK_LIBS}
324       ${lib_deps}
325       ${llvm_libs}
326       )
327   elseif(ARG_SHARED AND BUILD_SHARED_LIBS)
328     # FIXME: It may be PRIVATE since SO knows its dependent libs.
329     target_link_libraries(${name} PUBLIC
330       ${ARG_LINK_LIBS}
331       ${lib_deps}
332       ${llvm_libs}
333       )
334   else()
335     # MODULE|SHARED
336     target_link_libraries(${name} PRIVATE
337       ${ARG_LINK_LIBS}
338       ${lib_deps}
339       ${llvm_libs}
340       )
341   endif()
342
343   if(LLVM_COMMON_DEPENDS)
344     add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
345     # Add dependencies also to objlibs.
346     # CMake issue 14747 --  add_dependencies() might be ignored to objlib's user.
347     foreach(objlib ${objlibs})
348       add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
349     endforeach()
350   endif()
351 endfunction()
352
353 macro(add_llvm_library name)
354   if( BUILD_SHARED_LIBS )
355     llvm_add_library(${name} SHARED ${ARGN})
356   else()
357     llvm_add_library(${name} ${ARGN})
358   endif()
359   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
360
361   if( EXCLUDE_FROM_ALL )
362     set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
363   else()
364     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
365       install(TARGETS ${name}
366         EXPORT LLVMExports
367         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
368         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
369     endif()
370     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
371   endif()
372   set_target_properties(${name} PROPERTIES FOLDER "Libraries")
373 endmacro(add_llvm_library name)
374
375 macro(add_llvm_loadable_module name)
376   llvm_add_library(${name} MODULE ${ARGN})
377   if(NOT TARGET ${name})
378     # Add empty "phony" target
379     add_custom_target(${name})
380   else()
381     if( EXCLUDE_FROM_ALL )
382       set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
383     else()
384       if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
385         install(TARGETS ${name}
386           EXPORT LLVMExports
387           LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
388           ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
389       endif()
390       set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
391     endif()
392   endif()
393
394   set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
395 endmacro(add_llvm_loadable_module name)
396
397
398 macro(add_llvm_executable name)
399   llvm_process_sources( ALL_FILES ${ARGN} )
400   if( EXCLUDE_FROM_ALL )
401     add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
402   else()
403     add_executable(${name} ${ALL_FILES})
404   endif()
405   llvm_update_compile_flags(${name})
406   add_dead_strip( ${name} )
407
408   if (LLVM_EXPORTED_SYMBOL_FILE)
409     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
410   endif(LLVM_EXPORTED_SYMBOL_FILE)
411
412   set(EXCLUDE_FROM_ALL OFF)
413   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
414   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
415   if( LLVM_COMMON_DEPENDS )
416     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
417   endif( LLVM_COMMON_DEPENDS )
418 endmacro(add_llvm_executable name)
419
420
421 set (LLVM_TOOLCHAIN_TOOLS
422   llvm-ar
423   llvm-objdump
424   )
425
426 macro(add_llvm_tool name)
427   if( NOT LLVM_BUILD_TOOLS )
428     set(EXCLUDE_FROM_ALL ON)
429   endif()
430   add_llvm_executable(${name} ${ARGN})
431
432   list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
433   if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
434     if( LLVM_BUILD_TOOLS )
435       install(TARGETS ${name}
436               EXPORT LLVMExports
437               RUNTIME DESTINATION bin)
438     endif()
439   endif()
440   if( LLVM_BUILD_TOOLS )
441     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
442   endif()
443   set_target_properties(${name} PROPERTIES FOLDER "Tools")
444 endmacro(add_llvm_tool name)
445
446
447 macro(add_llvm_example name)
448   if( NOT LLVM_BUILD_EXAMPLES )
449     set(EXCLUDE_FROM_ALL ON)
450   endif()
451   add_llvm_executable(${name} ${ARGN})
452   if( LLVM_BUILD_EXAMPLES )
453     install(TARGETS ${name} RUNTIME DESTINATION examples)
454   endif()
455   set_target_properties(${name} PROPERTIES FOLDER "Examples")
456 endmacro(add_llvm_example name)
457
458
459 macro(add_llvm_utility name)
460   add_llvm_executable(${name} ${ARGN})
461   set_target_properties(${name} PROPERTIES FOLDER "Utils")
462 endmacro(add_llvm_utility name)
463
464
465 macro(add_llvm_target target_name)
466   include_directories(BEFORE
467     ${CMAKE_CURRENT_BINARY_DIR}
468     ${CMAKE_CURRENT_SOURCE_DIR})
469   add_llvm_library(LLVM${target_name} ${ARGN})
470   set( CURRENT_LLVM_TARGET LLVM${target_name} )
471 endmacro(add_llvm_target)
472
473 # Add external project that may want to be built as part of llvm such as Clang,
474 # lld, and Polly. This adds two options. One for the source directory of the
475 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
476 # enable or disable building it with everything else.
477 # Additional parameter can be specified as the name of directory.
478 macro(add_llvm_external_project name)
479   set(add_llvm_external_dir "${ARGN}")
480   if("${add_llvm_external_dir}" STREQUAL "")
481     set(add_llvm_external_dir ${name})
482   endif()
483   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
484   string(REPLACE "-" "_" nameUNDERSCORE ${name})
485   string(TOUPPER ${nameUNDERSCORE} nameUPPER)
486   set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
487       CACHE PATH "Path to ${name} source directory")
488   if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
489       AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
490     option(LLVM_EXTERNAL_${nameUPPER}_BUILD
491            "Whether to build ${name} as part of LLVM" ON)
492     if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
493       add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
494     endif()
495   endif()
496 endmacro(add_llvm_external_project)
497
498 macro(add_llvm_tool_subdirectory name)
499   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
500   add_subdirectory(${name})
501 endmacro(add_llvm_tool_subdirectory)
502
503 macro(ignore_llvm_tool_subdirectory name)
504   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
505 endmacro(ignore_llvm_tool_subdirectory)
506
507 function(add_llvm_implicit_external_projects)
508   set(list_of_implicit_subdirs "")
509   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
510   foreach(dir ${sub-dirs})
511     if(IS_DIRECTORY "${dir}")
512       list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
513       if( tool_subdir_ignore EQUAL -1
514           AND EXISTS "${dir}/CMakeLists.txt")
515         get_filename_component(fn "${dir}" NAME)
516         list(APPEND list_of_implicit_subdirs "${fn}")
517       endif()
518     endif()
519   endforeach()
520
521   foreach(external_proj ${list_of_implicit_subdirs})
522     add_llvm_external_project("${external_proj}")
523   endforeach()
524 endfunction(add_llvm_implicit_external_projects)
525
526 # Generic support for adding a unittest.
527 function(add_unittest test_suite test_name)
528   if( NOT LLVM_BUILD_TESTS )
529     set(EXCLUDE_FROM_ALL ON)
530   endif()
531
532   # Visual Studio 2012 only supports up to 8 template parameters in
533   # std::tr1::tuple by default, but gtest requires 10
534   if (MSVC AND MSVC_VERSION EQUAL 1700)
535     list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
536   endif ()
537
538   include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
539   if (NOT LLVM_ENABLE_THREADS)
540     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
541   endif ()
542
543   if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
544     list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
545   endif ()
546
547   set(LLVM_REQUIRES_RTTI OFF)
548
549   add_llvm_executable(${test_name} ${ARGN})
550   set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
551   set_output_directory(${test_name} ${outdir} ${outdir})
552   target_link_libraries(${test_name}
553     gtest
554     gtest_main
555     LLVMSupport # gtest needs it for raw_ostream.
556     )
557
558   add_dependencies(${test_suite} ${test_name})
559   get_target_property(test_suite_folder ${test_suite} FOLDER)
560   if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
561     set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
562   endif ()
563 endfunction()
564
565 # This function provides an automatic way to 'configure'-like generate a file
566 # based on a set of common and custom variables, specifically targeting the
567 # variables needed for the 'lit.site.cfg' files. This function bundles the
568 # common variables that any Lit instance is likely to need, and custom
569 # variables can be passed in.
570 function(configure_lit_site_cfg input output)
571   foreach(c ${LLVM_TARGETS_TO_BUILD})
572     set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
573   endforeach(c)
574   set(TARGETS_TO_BUILD ${TARGETS_BUILT})
575
576   set(SHLIBEXT "${LTDL_SHLIB_EXT}")
577
578   if(BUILD_SHARED_LIBS)
579     set(LLVM_SHARED_LIBS_ENABLED "1")
580   else()
581     set(LLVM_SHARED_LIBS_ENABLED "0")
582   endif(BUILD_SHARED_LIBS)
583
584   # Configuration-time: See Unit/lit.site.cfg.in
585   if (CMAKE_CFG_INTDIR STREQUAL ".")
586     set(LLVM_BUILD_MODE ".")
587   else ()
588     set(LLVM_BUILD_MODE "%(build_mode)s")
589   endif ()
590
591   # They below might not be the build tree but provided binary tree.
592   set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
593   set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
594   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
595   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
596
597   # SHLIBDIR points the build tree.
598   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
599
600   set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
601   set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
602
603   if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
604     set(ENABLE_ASSERTIONS "1")
605   else()
606     set(ENABLE_ASSERTIONS "0")
607   endif()
608
609   set(HOST_OS ${CMAKE_SYSTEM_NAME})
610   set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
611
612   if (CLANG_ENABLE_ARCMT)
613     set(ENABLE_CLANG_ARCMT "1")
614   else()
615     set(ENABLE_CLANG_ARCMT "0")
616   endif()
617   if (CLANG_ENABLE_REWRITER)
618     set(ENABLE_CLANG_REWRITER "1")
619   else()
620     set(ENABLE_CLANG_REWRITER "0")
621   endif()
622   if (CLANG_ENABLE_STATIC_ANALYZER)
623     set(ENABLE_CLANG_STATIC_ANALYZER "1")
624   else()
625     set(ENABLE_CLANG_STATIC_ANALYZER "0")
626   endif()
627
628   configure_file(${input} ${output} @ONLY)
629 endfunction()
630
631 # A raw function to create a lit target. This is used to implement the testuite
632 # management functions.
633 function(add_lit_target target comment)
634   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
635   set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
636   separate_arguments(LIT_ARGS)
637   if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
638     list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
639   endif ()
640   if (LLVM_MAIN_SRC_DIR)
641     set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
642   else()
643     find_program(LIT_COMMAND llvm-lit)
644   endif ()
645   list(APPEND LIT_COMMAND ${LIT_ARGS})
646   foreach(param ${ARG_PARAMS})
647     list(APPEND LIT_COMMAND --param ${param})
648   endforeach()
649   if( ARG_DEPENDS )
650     add_custom_target(${target}
651       COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
652       COMMENT "${comment}"
653       )
654     add_dependencies(${target} ${ARG_DEPENDS})
655   else()
656     add_custom_target(${target}
657       COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
658     message(STATUS "${target} does nothing.")
659   endif()
660
661   # Tests should be excluded from "Build Solution".
662   set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
663 endfunction()
664
665 # A function to add a set of lit test suites to be driven through 'check-*' targets.
666 function(add_lit_testsuite target comment)
667   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
668
669   # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
670   if(NOT EXCLUDE_FROM_ALL)
671     # Register the testsuites, params and depends for the global check rule.
672     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
673     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
674     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
675     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
676   endif()
677
678   # Produce a specific suffixed check rule.
679   add_lit_target(${target} ${comment}
680     ${ARG_DEFAULT_ARGS}
681     PARAMS ${ARG_PARAMS}
682     DEPENDS ${ARG_DEPENDS}
683     ARGS ${ARG_ARGS}
684     )
685 endfunction()