1 # LLVM_TARGET_DEFINITIONS must contain the name of the .td file to process,
2 # while LLVM_TARGET_DEPENDS may contain additional file dependencies.
3 # Extra parameters for `tblgen' may come after `ofn' parameter.
4 # Adds the name of the generated file to TABLEGEN_OUTPUT.
5 include(LLVMDistributionSupport)
7 function(tablegen project ofn)
8 cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
10 # Override ${project} with ${project}_TABLEGEN_PROJECT
11 if(NOT "${${project}_TABLEGEN_PROJECT}" STREQUAL "")
12 set(project ${${project}_TABLEGEN_PROJECT})
15 # Validate calling context.
16 if(NOT ${project}_TABLEGEN_EXE)
17 message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
20 # Use depfile instead of globbing arbitrary *.td(s) for Ninja.
21 if(CMAKE_GENERATOR MATCHES "Ninja")
22 # Make output path relative to build.ninja, assuming located on
23 # ${CMAKE_BINARY_DIR}.
24 # CMake emits build targets as relative paths but Ninja doesn't identify
25 # absolute path (in *.d) as relative path (in build.ninja)
26 # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
27 file(RELATIVE_PATH ofn_rel
28 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
29 set(additional_cmdline
32 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
33 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
38 file(GLOB local_tds "*.td")
39 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
40 set(additional_cmdline
41 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
45 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
46 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
48 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
49 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
51 if (LLVM_ENABLE_DAGISEL_COV AND "-gen-dag-isel" IN_LIST ARGN)
52 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
54 if (LLVM_ENABLE_GISEL_COV AND "-gen-global-isel" IN_LIST ARGN)
55 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
56 list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
58 if (LLVM_OMIT_DAGISEL_COMMENTS AND "-gen-dag-isel" IN_LIST ARGN)
59 list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
62 # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
63 # a possibility of generated tables being consumed by MSVC, generate arrays of
64 # char literals, instead. If we're cross-compiling, then conservatively assume
65 # that the source might be consumed by MSVC.
66 # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
67 if (MSVC AND project STREQUAL LLVM)
68 list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
70 if (CMAKE_GENERATOR MATCHES "Visual Studio")
71 # Visual Studio has problems with llvm-tblgen's native --write-if-changed
72 # behavior. Since it doesn't do restat optimizations anyway, just don't
73 # pass --write-if-changed there.
74 set(tblgen_change_flag)
76 set(tblgen_change_flag "--write-if-changed")
79 if (NOT LLVM_ENABLE_WARNINGS)
80 list(APPEND LLVM_TABLEGEN_FLAGS "-no-warn-on-unused-template-args")
83 # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
84 # (both the target and the file) to have .inc files rebuilt on
85 # a tablegen change, as cmake does not propagate file-level dependencies
86 # of custom targets. See the following ticket for more information:
87 # https://cmake.org/Bug/view.php?id=15858
88 # The dependency on both, the target and the file, produces the same
89 # dependency twice in the result file when
90 # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
91 # but lets us having smaller and cleaner code here.
92 get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
93 list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
94 # Filter out empty items before prepending each entry with -I
95 list(REMOVE_ITEM tblgen_includes "")
96 list(TRANSFORM tblgen_includes PREPEND -I)
98 set(tablegen_exe ${${project}_TABLEGEN_EXE})
99 set(tablegen_depends ${${project}_TABLEGEN_TARGET} ${tablegen_exe})
101 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
102 COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -I ${CMAKE_CURRENT_SOURCE_DIR}
104 ${LLVM_TABLEGEN_FLAGS}
105 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
106 ${tblgen_change_flag}
107 ${additional_cmdline}
108 # The file in LLVM_TARGET_DEFINITIONS may be not in the current
109 # directory and local_tds may not contain it, so we must
110 # explicitly list it here:
111 DEPENDS ${ARG_DEPENDS} ${tablegen_depends}
112 ${local_tds} ${global_tds}
113 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
114 ${LLVM_TARGET_DEPENDS}
115 COMMENT "Building ${ofn}..."
118 # `make clean' must remove all those generated files:
119 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
121 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
122 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
126 # Creates a target for publicly exporting tablegen dependencies.
127 function(add_public_tablegen_target target)
128 if(NOT TABLEGEN_OUTPUT)
129 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
131 add_custom_target(${target}
132 DEPENDS ${TABLEGEN_OUTPUT})
133 if(LLVM_COMMON_DEPENDS)
134 add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
136 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
137 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
140 macro(add_tablegen target project)
141 cmake_parse_arguments(ADD_TABLEGEN "" "DESTINATION;EXPORT" "" ${ARGN})
143 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
144 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
146 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB
147 ${ADD_TABLEGEN_UNPARSED_ARGUMENTS})
148 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
150 set(${project}_TABLEGEN_DEFAULT "${target}")
151 if (LLVM_NATIVE_TOOL_DIR)
152 if (EXISTS "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
153 set(${project}_TABLEGEN_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
157 # FIXME: Quick fix to reflect LLVM_TABLEGEN to llvm-min-tblgen
158 if("${target}" STREQUAL "llvm-min-tblgen"
159 AND NOT "${LLVM_TABLEGEN}" STREQUAL ""
160 AND NOT "${LLVM_TABLEGEN}" STREQUAL "llvm-tblgen")
161 set(${project}_TABLEGEN_DEFAULT "${LLVM_TABLEGEN}")
164 if(ADD_TABLEGEN_EXPORT)
165 set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}" CACHE
166 STRING "Native TableGen executable. Saves building one when cross-compiling.")
169 set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}")
170 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
173 # Effective tblgen executable to be used:
174 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
175 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
177 if(LLVM_USE_HOST_TOOLS)
178 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
179 # The NATIVE tablegen executable *must* depend on the current target one
180 # otherwise the native one won't get rebuilt when the tablgen sources
181 # change, and we end up with incorrect builds.
182 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
183 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
185 add_custom_target(${target}-host DEPENDS ${${project}_TABLEGEN_EXE})
186 set(${project}_TABLEGEN_TARGET ${target}-host PARENT_SCOPE)
188 # If we're using the host tablegen, and utils were not requested, we have no
189 # need to build this tablegen.
190 if ( NOT LLVM_BUILD_UTILS )
191 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
196 if (ADD_TABLEGEN_DESTINATION AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND
197 (LLVM_BUILD_UTILS OR ${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS))
199 if(ADD_TABLEGEN_EXPORT)
200 get_target_export_arg(${target} ${ADD_TABLEGEN_EXPORT} export_arg)
202 install(TARGETS ${target}
205 RUNTIME DESTINATION "${ADD_TABLEGEN_DESTINATION}")
206 if(NOT LLVM_ENABLE_IDE)
207 add_llvm_install_targets("install-${target}"
212 if(ADD_TABLEGEN_EXPORT)
213 string(TOUPPER ${ADD_TABLEGEN_EXPORT} export_upper)
214 set_property(GLOBAL APPEND PROPERTY ${export_upper}_EXPORTS ${target})