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