1 include(GNUInstallDirs)
2 include(LLVMDistributionSupport)
4 function(clang_tablegen)
6 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
7 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
9 # Generates a custom command for invoking tblgen as
11 # tblgen source-file -o=output-file tablegen-arg ...
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})
20 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
23 set( CLANG_TABLEGEN_ARGUMENTS "" )
24 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
25 tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS} ${CLANG_TABLEGEN_ARGUMENTS})
28 add_public_tablegen_target(${CTG_TARGET})
29 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
31 endfunction(clang_tablegen)
33 macro(set_clang_windows_version_resource_properties name)
34 if(DEFINED windows_resource_file)
35 set_windows_version_resource_properties(${name} ${windows_resource_file}
36 VERSION_MAJOR ${CLANG_VERSION_MAJOR}
37 VERSION_MINOR ${CLANG_VERSION_MINOR}
38 VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
39 VERSION_STRING "${CLANG_VERSION}"
44 macro(add_clang_subdirectory name)
45 add_llvm_subdirectory(CLANG TOOL ${name})
48 macro(add_clang_library name)
49 cmake_parse_arguments(ARG
50 "SHARED;STATIC;INSTALL_WITH_TOOLCHAIN"
57 file(RELATIVE_PATH lib_path
58 ${CLANG_SOURCE_DIR}/lib/
59 ${CMAKE_CURRENT_SOURCE_DIR}
61 if(NOT lib_path MATCHES "^[.][.]")
62 file( GLOB_RECURSE headers
63 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
64 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
66 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
68 file( GLOB_RECURSE tds
69 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
71 source_group("TableGen descriptions" FILES ${tds})
72 set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
75 set(srcs ${headers} ${tds})
78 endif(MSVC_IDE OR XCODE)
79 if(srcs OR ARG_ADDITIONAL_HEADERS)
83 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
87 if(ARG_SHARED AND ARG_STATIC)
88 set(LIBTYPE SHARED STATIC)
92 # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
93 # so we need to handle it here.
99 if(NOT XCODE AND NOT MSVC_IDE)
100 # The Xcode generator doesn't handle object libraries correctly.
101 # The Visual Studio CMake generator does handle object libraries
102 # correctly, but it is preferable to list the libraries with their
103 # source files (instead of the object files and the source files in
104 # a separate target in the "Object Libraries" folder)
105 list(APPEND LIBTYPE OBJECT)
107 set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
109 llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
111 if(MSVC AND NOT CLANG_LINK_CLANG_DYLIB)
112 # Make sure all consumers also turn off visibility macros so there not trying to dllimport symbols.
113 target_compile_definitions(${name} PUBLIC CLANG_BUILD_STATIC)
114 if(TARGET "obj.${name}")
115 target_compile_definitions("obj.${name}" PUBLIC CLANG_BUILD_STATIC)
117 elseif(NOT ARG_SHARED AND NOT ARG_STATIC)
118 # Clang component libraries linked in to clang-cpp are declared without SHARED or STATIC
119 target_compile_definitions("obj.${name}" PUBLIC CLANG_EXPORTS)
123 if(ARG_SHARED AND ARG_STATIC)
124 list(APPEND libs ${name}_static)
129 target_link_libraries(${lib} INTERFACE ${LLVM_COMMON_LIBS})
131 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
132 get_target_export_arg(${name} Clang export_to_clangtargets UMBRELLA clang-libraries)
133 install(TARGETS ${lib}
135 ${export_to_clangtargets}
136 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
137 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
138 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
140 if (NOT LLVM_ENABLE_IDE)
141 add_llvm_install_targets(install-${lib}
146 set_property(GLOBAL APPEND PROPERTY CLANG_LIBS ${lib})
148 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${lib})
150 # Add empty "phony" target
151 add_custom_target(${lib})
155 set_clang_windows_version_resource_properties(${name})
156 endmacro(add_clang_library)
158 macro(add_clang_executable name)
159 add_llvm_executable( ${name} ${ARGN} )
160 set_clang_windows_version_resource_properties(${name})
161 set_target_properties(${name} PROPERTIES XCODE_GENERATE_SCHEME ON)
162 endmacro(add_clang_executable)
164 macro(add_clang_tool name)
165 cmake_parse_arguments(ARG "DEPENDS;GENERATE_DRIVER" "" "" ${ARGN})
166 if (NOT CLANG_BUILD_TOOLS)
167 set(EXCLUDE_FROM_ALL ON)
169 if(ARG_GENERATE_DRIVER
170 AND LLVM_TOOL_LLVM_DRIVER_BUILD
171 AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
173 set(get_obj_args ${ARGN})
174 list(FILTER get_obj_args EXCLUDE REGEX "^SUPPORT_PLUGINS$")
175 generate_llvm_objects(${name} ${get_obj_args})
176 add_custom_target(${name} DEPENDS llvm-driver clang-resource-headers)
178 add_clang_executable(${name} ${ARGN})
179 add_dependencies(${name} clang-resource-headers)
181 if (CLANG_BUILD_TOOLS)
182 get_target_export_arg(${name} Clang export_to_clangtargets)
183 install(TARGETS ${name}
184 ${export_to_clangtargets}
185 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
188 if(NOT LLVM_ENABLE_IDE)
189 add_llvm_install_targets(install-${name}
193 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
196 set_target_properties(${name} PROPERTIES XCODE_GENERATE_SCHEME ON)
199 macro(add_clang_symlink name dest)
200 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
201 if(LLVM_TOOL_LLVM_DRIVER_BUILD
202 AND ${dest} IN_LIST LLVM_DRIVER_TOOLS
203 AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${dest} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
205 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${dest} ${name})
207 llvm_add_tool_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
208 # Always generate install targets
209 llvm_install_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
213 function(clang_target_link_libraries target type)
214 if (TARGET obj.${target})
215 target_link_libraries(obj.${target} ${ARGN})
218 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
219 if(LLVM_TOOL_LLVM_DRIVER_BUILD AND ${target} IN_LIST LLVM_DRIVER_TOOLS)
220 set(target llvm-driver)
223 if (CLANG_LINK_CLANG_DYLIB)
224 target_link_libraries(${target} ${type} clang-cpp)
226 target_link_libraries(${target} ${type} ${ARGN})