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