Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / cmake / modules / AddClang.cmake
blob75b0080f67156463a86c77a50580b00596126b23
1 include(GNUInstallDirs)
2 include(LLVMDistributionSupport)
4 function(clang_tablegen)
5   # Syntax:
6   # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
7   # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
8   #
9   # Generates a custom command for invoking tblgen as
10   #
11   # tblgen source-file -o=output-file tablegen-arg ...
12   #
13   # and, if cmake-target-name is provided, creates a custom target for
14   # executing the custom command depending on output-file. It is
15   # possible to list more files to depend after DEPENDS.
17   cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
19   if( NOT CTG_SOURCE )
20     message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
21   endif()
23   set( CLANG_TABLEGEN_ARGUMENTS "" )
24   set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
25   tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS} ${CLANG_TABLEGEN_ARGUMENTS})
27   if(CTG_TARGET)
28     add_public_tablegen_target(${CTG_TARGET})
29     set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
30     set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
31   endif()
32 endfunction(clang_tablegen)
34 macro(set_clang_windows_version_resource_properties name)
35   if(DEFINED windows_resource_file)
36     set_windows_version_resource_properties(${name} ${windows_resource_file}
37       VERSION_MAJOR ${CLANG_VERSION_MAJOR}
38       VERSION_MINOR ${CLANG_VERSION_MINOR}
39       VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
40       VERSION_STRING "${CLANG_VERSION}"
41       PRODUCT_NAME "clang")
42   endif()
43 endmacro()
45 macro(add_clang_subdirectory name)
46   add_llvm_subdirectory(CLANG TOOL ${name})
47 endmacro()
49 macro(add_clang_library name)
50   cmake_parse_arguments(ARG
51     "SHARED;STATIC;INSTALL_WITH_TOOLCHAIN"
52     ""
53     "ADDITIONAL_HEADERS"
54     ${ARGN})
55   set(srcs)
56   if(MSVC_IDE OR XCODE)
57     # Add public headers
58     file(RELATIVE_PATH lib_path
59       ${CLANG_SOURCE_DIR}/lib/
60       ${CMAKE_CURRENT_SOURCE_DIR}
61     )
62     if(NOT lib_path MATCHES "^[.][.]")
63       file( GLOB_RECURSE headers
64         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
65         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
66       )
67       set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
69       file( GLOB_RECURSE tds
70         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
71       )
72       source_group("TableGen descriptions" FILES ${tds})
73       set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
75       if(headers OR tds)
76         set(srcs ${headers} ${tds})
77       endif()
78     endif()
79   endif(MSVC_IDE OR XCODE)
80   if(srcs OR ARG_ADDITIONAL_HEADERS)
81     set(srcs
82       ADDITIONAL_HEADERS
83       ${srcs}
84       ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
85       )
86   endif()
88   if(ARG_SHARED AND ARG_STATIC)
89     set(LIBTYPE SHARED STATIC)
90   elseif(ARG_SHARED)
91     set(LIBTYPE SHARED)
92   else()
93     # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
94     # so we need to handle it here.
95     if(BUILD_SHARED_LIBS)
96       set(LIBTYPE SHARED)
97     else()
98       set(LIBTYPE STATIC)
99     endif()
100     if(NOT XCODE)
101       # The Xcode generator doesn't handle object libraries correctly.
102       list(APPEND LIBTYPE OBJECT)
103     endif()
104     set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
105   endif()
106   llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
108   set(libs ${name})
109   if(ARG_SHARED AND ARG_STATIC)
110     list(APPEND libs ${name}_static)
111   endif()
113   foreach(lib ${libs})
114     if(TARGET ${lib})
115       target_link_libraries(${lib} INTERFACE ${LLVM_COMMON_LIBS})
117       if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
118         get_target_export_arg(${name} Clang export_to_clangtargets UMBRELLA clang-libraries)
119         install(TARGETS ${lib}
120           COMPONENT ${lib}
121           ${export_to_clangtargets}
122           LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
123           ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
124           RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
126         if (NOT LLVM_ENABLE_IDE)
127           add_llvm_install_targets(install-${lib}
128                                    DEPENDS ${lib}
129                                    COMPONENT ${lib})
130         endif()
132         set_property(GLOBAL APPEND PROPERTY CLANG_LIBS ${lib})
133       endif()
134       set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${lib})
135     else()
136       # Add empty "phony" target
137       add_custom_target(${lib})
138     endif()
139   endforeach()
141   set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
142   set_clang_windows_version_resource_properties(${name})
143 endmacro(add_clang_library)
145 macro(add_clang_executable name)
146   add_llvm_executable( ${name} ${ARGN} )
147   set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
148   set_clang_windows_version_resource_properties(${name})
149 endmacro(add_clang_executable)
151 macro(add_clang_tool name)
152   cmake_parse_arguments(ARG "DEPENDS;GENERATE_DRIVER" "" "" ${ARGN})
153   if (NOT CLANG_BUILD_TOOLS)
154     set(EXCLUDE_FROM_ALL ON)
155   endif()
156   if(ARG_GENERATE_DRIVER
157      AND LLVM_TOOL_LLVM_DRIVER_BUILD
158      AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
159     )
160     set(get_obj_args ${ARGN})
161     list(FILTER get_obj_args EXCLUDE REGEX "^SUPPORT_PLUGINS$")
162     generate_llvm_objects(${name} ${get_obj_args})
163     add_custom_target(${name} DEPENDS llvm-driver clang-resource-headers)
164   else()
165     add_clang_executable(${name} ${ARGN})
166     add_dependencies(${name} clang-resource-headers)
168     if (CLANG_BUILD_TOOLS)
169       get_target_export_arg(${name} Clang export_to_clangtargets)
170       install(TARGETS ${name}
171         ${export_to_clangtargets}
172         RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
173         COMPONENT ${name})
175       if(NOT LLVM_ENABLE_IDE)
176         add_llvm_install_targets(install-${name}
177                                  DEPENDS ${name}
178                                  COMPONENT ${name})
179       endif()
180       set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
181     endif()
182   endif()
183 endmacro()
185 macro(add_clang_symlink name dest)
186   get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
187   if(LLVM_TOOL_LLVM_DRIVER_BUILD
188      AND ${dest} IN_LIST LLVM_DRIVER_TOOLS
189      AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${dest} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
190     )
191     set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${dest} ${name})
192   else()
193     llvm_add_tool_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
194     # Always generate install targets
195     llvm_install_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
196   endif()
197 endmacro()
199 function(clang_target_link_libraries target type)
200   if (TARGET obj.${target})
201     target_link_libraries(obj.${target} ${ARGN})
202   endif()
204   get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
205   if(LLVM_TOOL_LLVM_DRIVER_BUILD AND ${target} IN_LIST LLVM_DRIVER_TOOLS)
206     set(target llvm-driver)
207   endif()
209   if (CLANG_LINK_CLANG_DYLIB)
210     target_link_libraries(${target} ${type} clang-cpp)
211   else()
212     target_link_libraries(${target} ${type} ${ARGN})
213   endif()
214 endfunction()