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_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
30 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
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}"
45 macro(add_clang_subdirectory name)
46 add_llvm_subdirectory(CLANG TOOL ${name})
49 macro(add_clang_library name)
50 cmake_parse_arguments(ARG
51 "SHARED;STATIC;INSTALL_WITH_TOOLCHAIN"
58 file(RELATIVE_PATH lib_path
59 ${CLANG_SOURCE_DIR}/lib/
60 ${CMAKE_CURRENT_SOURCE_DIR}
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
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
72 source_group("TableGen descriptions" FILES ${tds})
73 set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
76 set(srcs ${headers} ${tds})
79 endif(MSVC_IDE OR XCODE)
80 if(srcs OR ARG_ADDITIONAL_HEADERS)
84 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
88 if(ARG_SHARED AND ARG_STATIC)
89 set(LIBTYPE SHARED STATIC)
93 # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
94 # so we need to handle it here.
101 # The Xcode generator doesn't handle object libraries correctly.
102 list(APPEND LIBTYPE OBJECT)
104 set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
106 llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
109 if(ARG_SHARED AND ARG_STATIC)
110 list(APPEND libs ${name}_static)
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}
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}
132 set_property(GLOBAL APPEND PROPERTY CLANG_LIBS ${lib})
134 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${lib})
136 # Add empty "phony" target
137 add_custom_target(${lib})
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)
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)
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)
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}"
175 if(NOT LLVM_ENABLE_IDE)
176 add_llvm_install_targets(install-${name}
180 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
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)
191 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${dest} ${name})
193 llvm_add_tool_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
194 # Always generate install targets
195 llvm_install_symlink(CLANG ${name} ${dest} ALWAYS_GENERATE)
199 function(clang_target_link_libraries target type)
200 if (TARGET obj.${target})
201 target_link_libraries(obj.${target} ${ARGN})
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)
209 if (CLANG_LINK_CLANG_DYLIB)
210 target_link_libraries(${target} ${type} clang-cpp)
212 target_link_libraries(${target} ${type} ${ARGN})