+ if(NOT LLVM_NO_DEAD_STRIP)
+ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+ # ld64's implementation of -dead_strip breaks tools that use plugins.
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,-dead_strip")
+ elseif(NOT WIN32)
+ # Object files are compiled with -ffunction-data-sections.
+ # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
+ # tools that use plugins. Always pass --gc-sections once we require
+ # a newer linker.
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,--gc-sections")
+ endif()
+ endif()
+endfunction(add_link_opts)
+
+# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
+# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
+# or a certain builder, for eaxample, msbuild.exe, would be confused.
+function(set_output_directory target bindir libdir)
+ # Do nothing if *_OUTPUT_INTDIR is empty.
+ if("${bindir}" STREQUAL "")
+ return()
+ endif()
+
+ # moddir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
+ # It affects output of add_library(MODULE).
+ if(WIN32 OR CYGWIN)
+ # DLL platform
+ set(moddir ${bindir})
+ else()
+ set(moddir ${libdir})
+ endif()
+ if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
+ foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
+ string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
+ string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
+ string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
+ string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
+ set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
+ set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
+ set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
+ endforeach()
+ else()
+ set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
+ set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
+ set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
+ endif()
+endfunction()
+
+# llvm_add_library(name sources...
+# SHARED;STATIC
+# STATIC by default w/o BUILD_SHARED_LIBS.
+# SHARED by default w/ BUILD_SHARED_LIBS.
+# MODULE
+# Target ${name} might not be created on unsupported platforms.
+# Check with "if(TARGET ${name})".
+# OUTPUT_NAME name
+# Corresponds to OUTPUT_NAME in target properties.
+# DEPENDS targets...
+# Same semantics as add_dependencies().
+# LINK_COMPONENTS components...
+# Same as the variable LLVM_LINK_COMPONENTS.
+# LINK_LIBS lib_targets...
+# Same semantics as target_link_libraries().
+# ADDITIONAL_HEADERS
+# May specify header files for IDE generators.
+# )
+function(llvm_add_library name)
+ cmake_parse_arguments(ARG
+ "MODULE;SHARED;STATIC"
+ "OUTPUT_NAME"
+ "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
+ ${ARGN})
+ list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
+ if(ARG_ADDITIONAL_HEADERS)
+ # Pass through ADDITIONAL_HEADERS.
+ set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
+ endif()
+ if(ARG_OBJLIBS)
+ set(ALL_FILES ${ARG_OBJLIBS})
+ else()
+ llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
+ endif()
+
+ if(ARG_MODULE)
+ if(ARG_SHARED OR ARG_STATIC)
+ message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
+ endif()
+ if(NOT LLVM_ENABLE_PLUGINS)
+ message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
+ return()
+ endif()
+ else()
+ if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
+ set(ARG_SHARED TRUE)
+ endif()
+ if(NOT ARG_SHARED)
+ set(ARG_STATIC TRUE)
+ endif()
+ endif()
+
+ # Generate objlib
+ if(ARG_SHARED AND ARG_STATIC)
+ # Generate an obj library for both targets.
+ set(obj_name "obj.${name}")
+ add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
+ ${ALL_FILES}
+ )
+ llvm_update_compile_flags(${obj_name})
+ set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
+
+ # Do add_dependencies(obj) later due to CMake issue 14747.
+ list(APPEND objlibs ${obj_name})
+
+ set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
+ endif()
+
+ if(ARG_SHARED AND ARG_STATIC)
+ # static
+ set(name_static "${name}_static")
+ if(ARG_OUTPUT_NAME)
+ set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
+ endif()
+ # DEPENDS has been appended to LLVM_COMMON_LIBS.
+ llvm_add_library(${name_static} STATIC
+ ${output_name}
+ OBJLIBS ${ALL_FILES} # objlib
+ LINK_LIBS ${ARG_LINK_LIBS}
+ LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
+ )
+ # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
+ set(ARG_STATIC)
+ endif()
+
+ if(ARG_MODULE)
+ add_library(${name} MODULE ${ALL_FILES})
+ elseif(ARG_SHARED)
+ add_library(${name} SHARED ${ALL_FILES})
+ else()
+ add_library(${name} STATIC ${ALL_FILES})
+ endif()
+ set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
+ llvm_update_compile_flags(${name})
+ add_link_opts( ${name} )
+ if(ARG_OUTPUT_NAME)
+ set_target_properties(${name}
+ PROPERTIES
+ OUTPUT_NAME ${ARG_OUTPUT_NAME}
+ )
+ endif()
+
+ if(ARG_MODULE)
+ set_target_properties(${name} PROPERTIES
+ PREFIX ""
+ SUFFIX ${LLVM_PLUGIN_EXT}
+ )
+ endif()
+
+ if(ARG_SHARED)
+ if(WIN32)
+ set_target_properties(${name} PROPERTIES
+ PREFIX ""
+ )
+ endif()