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