1 include(LLVMParseArguments)
2 include(LLVMProcessSources)
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)
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)
19 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
20 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
22 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
23 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
26 # FIXME: Remove this once clang-cl supports SEH
27 list(APPEND LLVM_COMPILE_DEFINITIONS "GTEST_HAS_SEH=0")
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")
38 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
43 # - LLVM_COMPILE_FLAGS is list.
44 # - PROPERTY COMPILE_FLAGS is string.
45 string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
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}")
56 # Update target props, since all sources are C++.
57 set_property(TARGET ${name} APPEND_STRING PROPERTY
58 COMPILE_FLAGS "${target_compile_flags}")
61 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
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}
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}
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}")
90 set(native_export_file "${target_name}.def")
93 set(export_file_nativeslashes ${export_file})
94 if(WIN32 AND NOT CYGWIN)
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})
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}
108 COMMENT "Creating export file for ${target_name}")
109 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
111 set(export_file_linker_flag "/DEF:${export_file_linker_flag}")
113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " ${export_file_linker_flag}")
117 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
118 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
120 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
122 get_filename_component(extension ${src} EXT)
123 if(extension STREQUAL ".cpp")
124 set(first_source_file ${src})
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})
139 set_property(DIRECTORY APPEND
140 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
142 add_dependencies(${target_name} ${target_name}_exports)
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)
149 if(NOT WIN32 AND NOT APPLE)
151 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
152 OUTPUT_VARIABLE stdout
155 if("${stdout}" MATCHES "GNU gold")
156 set(LLVM_LINKER_IS_GOLD ON)
160 function(add_link_opts target_name)
161 # Pass -O3 to the linker. This enabled different optimizations on different
163 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32))
164 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
165 LINK_FLAGS " -Wl,-O3")
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.
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
186 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
187 LINK_FLAGS " -Wl,--gc-sections")
190 endfunction(add_link_opts)
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 "")
201 # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
202 # It affects output of add_library(MODULE).
205 set(moddir ${bindir})
207 set(moddir ${libdir})
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})
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})
226 # llvm_add_library(name sources...
228 # STATIC by default w/o BUILD_SHARED_LIBS.
229 # SHARED by default w/ BUILD_SHARED_LIBS.
231 # Target ${name} might not be created on unsupported platforms.
232 # Check with "if(TARGET ${name})".
234 # Corresponds to OUTPUT_NAME in target properties.
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().
242 # May specify header files for IDE generators.
244 function(llvm_add_library name)
245 cmake_parse_arguments(ARG
246 "MODULE;SHARED;STATIC"
248 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
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})
256 set(ALL_FILES ${ARG_OBJLIBS})
258 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
262 if(ARG_SHARED OR ARG_STATIC)
263 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
265 if(NOT LLVM_ENABLE_PLUGINS)
266 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
270 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
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
285 llvm_update_compile_flags(${obj_name})
286 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
288 # Do add_dependencies(obj) later due to CMake issue 14747.
289 list(APPEND objlibs ${obj_name})
291 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
294 if(ARG_SHARED AND ARG_STATIC)
296 set(name_static "${name}_static")
298 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
300 # DEPENDS has been appended to LLVM_COMMON_LIBS.
301 llvm_add_library(${name_static} STATIC
303 OBJLIBS ${ALL_FILES} # objlib
304 LINK_LIBS ${ARG_LINK_LIBS}
305 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
307 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
312 add_library(${name} MODULE ${ALL_FILES})
314 add_library(${name} SHARED ${ALL_FILES})
316 add_library(${name} STATIC ${ALL_FILES})
318 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
319 llvm_update_compile_flags(${name})
320 add_link_opts( ${name} )
322 set_target_properties(${name}
324 OUTPUT_NAME ${ARG_OUTPUT_NAME}
329 set_target_properties(${name} PROPERTIES
331 SUFFIX ${LLVM_PLUGIN_EXT}
337 set_target_properties(${name} PROPERTIES
342 set_target_properties(${name}
344 SOVERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}
345 VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
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
353 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
355 if (LLVM_EXPORTED_SYMBOL_FILE)
356 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
360 # Add the explicit dependency information for this library.
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})
367 llvm_map_components_to_libnames(llvm_libs
368 ${ARG_LINK_COMPONENTS}
369 ${LLVM_LINK_COMPONENTS}
372 if(CMAKE_VERSION VERSION_LESS 2.8.12)
373 # Link libs w/o keywords, assuming PUBLIC.
374 target_link_libraries(${name}
380 target_link_libraries(${name} INTERFACE
386 # We can use PRIVATE since SO knows its dependent libs.
387 target_link_libraries(${name} PRIVATE
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})
404 macro(add_llvm_library name)
405 if( BUILD_SHARED_LIBS )
406 llvm_add_library(${name} SHARED ${ARGN})
408 llvm_add_library(${name} ${ARGN})
410 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
412 if( EXCLUDE_FROM_ALL )
413 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
415 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
416 install(TARGETS ${name}
418 RUNTIME DESTINATION bin
419 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
420 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
423 if (NOT CMAKE_CONFIGURATION_TYPES)
424 add_custom_target(install-${name}
426 COMMAND "${CMAKE_COMMAND}"
427 -DCMAKE_INSTALL_COMPONENT=${name}
428 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
431 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
433 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
434 endmacro(add_llvm_library name)
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})
442 if( EXCLUDE_FROM_ALL )
443 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
445 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
450 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
452 install(TARGETS ${name}
454 LIBRARY DESTINATION ${dlldir}
455 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
457 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
461 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
462 endmacro(add_llvm_loadable_module name)
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})
470 add_executable(${name} ${ALL_FILES})
472 llvm_update_compile_flags(${name})
473 add_link_opts( ${name} )
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
479 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
481 if (LLVM_EXPORTED_SYMBOL_FILE)
482 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
483 endif(LLVM_EXPORTED_SYMBOL_FILE)
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)
494 set (LLVM_TOOLCHAIN_TOOLS
499 macro(add_llvm_tool name)
500 if( NOT LLVM_BUILD_TOOLS )
501 set(EXCLUDE_FROM_ALL ON)
503 add_llvm_executable(${name} ${ARGN})
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}
510 RUNTIME DESTINATION bin
513 if (NOT CMAKE_CONFIGURATION_TYPES)
514 add_custom_target(install-${name}
516 COMMAND "${CMAKE_COMMAND}"
517 -DCMAKE_INSTALL_COMPONENT=${name}
518 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
522 if( LLVM_BUILD_TOOLS )
523 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
525 set_target_properties(${name} PROPERTIES FOLDER "Tools")
526 endmacro(add_llvm_tool name)
529 macro(add_llvm_example name)
530 if( NOT LLVM_BUILD_EXAMPLES )
531 set(EXCLUDE_FROM_ALL ON)
533 add_llvm_executable(${name} ${ARGN})
534 if( LLVM_BUILD_EXAMPLES )
535 install(TARGETS ${name} RUNTIME DESTINATION examples)
537 set_target_properties(${name} PROPERTIES FOLDER "Examples")
538 endmacro(add_llvm_example name)
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)
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)
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})
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})
578 endmacro(add_llvm_external_project)
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)
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)
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}")
603 foreach(external_proj ${list_of_implicit_subdirs})
604 add_llvm_external_project("${external_proj}")
606 endfunction(add_llvm_implicit_external_projects)
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)
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)
619 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
620 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
623 set(LLVM_REQUIRES_RTTI OFF)
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}
631 LLVMSupport # gtest needs it for raw_ostream.
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}")
641 function(llvm_add_go_executable binary pkgpath)
642 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
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}")
651 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
652 foreach(d ${include_dirs})
653 set(cppflags "${cppflags} -I${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}"
664 add_custom_target(${binary} ALL DEPENDS ${binpath})
666 add_custom_target(${binary} DEPENDS ${binpath})
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}")
680 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
682 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
684 # Configuration-time: See Unit/lit.site.cfg.in
685 if (CMAKE_CFG_INTDIR STREQUAL ".")
686 set(LLVM_BUILD_MODE ".")
688 set(LLVM_BUILD_MODE "%(build_mode)s")
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})
697 # SHLIBDIR points the build tree.
698 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
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")
706 set(ENABLE_SHARED "0")
709 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
710 set(ENABLE_ASSERTIONS "1")
712 set(ENABLE_ASSERTIONS "0")
715 set(HOST_OS ${CMAKE_SYSTEM_NAME})
716 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
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}")
722 configure_file(${input} ${output} @ONLY)
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})
734 if (LLVM_MAIN_SRC_DIR)
735 set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
737 find_program(LIT_COMMAND llvm-lit)
739 list(APPEND LIT_COMMAND ${LIT_ARGS})
740 foreach(param ${ARG_PARAMS})
741 list(APPEND LIT_COMMAND --param ${param})
743 if (ARG_DEFAULT_ARGS)
744 add_custom_target(${target}
745 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
747 ${cmake_3_2_USES_TERMINAL}
750 add_custom_target(${target}
751 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
752 message(STATUS "${target} does nothing.")
755 add_dependencies(${target} ${ARG_DEPENDS})
758 # Tests should be excluded from "Build Solution".
759 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
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})
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})
775 # Produce a specific suffixed check rule.
776 add_lit_target(${target} ${comment}
779 DEPENDS ${ARG_DEPENDS}