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.
6 function(tablegen project ofn)
7 # Validate calling context.
8 if(NOT ${project}_TABLEGEN_EXE)
9 message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
12 # Use depfile instead of globbing arbitrary *.td(s) for Ninja.
13 if(CMAKE_GENERATOR STREQUAL "Ninja")
14 # Make output path relative to build.ninja, assuming located on
15 # ${CMAKE_BINARY_DIR}.
16 # CMake emits build targets as relative paths but Ninja doesn't identify
17 # absolute path (in *.d) as relative path (in build.ninja)
18 # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
19 file(RELATIVE_PATH ofn_rel
20 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
21 set(additional_cmdline
24 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
25 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
30 file(GLOB local_tds "*.td")
31 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
32 set(additional_cmdline
33 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
37 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
38 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
40 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
41 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
43 if (LLVM_ENABLE_DAGISEL_COV)
44 list(FIND ARGN "-gen-dag-isel" idx)
45 if( NOT idx EQUAL -1 )
46 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
49 if (LLVM_ENABLE_GISEL_COV)
50 list(FIND ARGN "-gen-global-isel" idx)
51 if( NOT idx EQUAL -1 )
52 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
53 list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
56 # Comments are only useful for Debug builds. Omit them if the backend
58 if (NOT (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR
59 uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))
60 list(FIND ARGN "-gen-dag-isel" idx)
62 list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
66 # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
67 # a possibility of generated tables being consumed by MSVC, generate arrays of
68 # char literals, instead. If we're cross-compiling, then conservatively assume
69 # that the source might be consumed by MSVC.
70 # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
71 if (MSVC AND project STREQUAL LLVM)
72 list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
74 if (CMAKE_GENERATOR MATCHES "Visual Studio")
75 # Visual Studio has problems with llvm-tblgen's native --write-if-changed
76 # behavior. Since it doesn't do restat optimizations anyway, just don't
77 # pass --write-if-changed there.
78 set(tblgen_change_flag)
80 set(tblgen_change_flag "--write-if-changed")
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(TRANSFORM tblgen_includes PREPEND -I)
95 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
96 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
98 ${LLVM_TABLEGEN_FLAGS}
99 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
100 ${tblgen_change_flag}
101 ${additional_cmdline}
102 # The file in LLVM_TARGET_DEFINITIONS may be not in the current
103 # directory and local_tds may not contain it, so we must
104 # explicitly list it here:
105 DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
106 ${local_tds} ${global_tds}
107 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
108 ${LLVM_TARGET_DEPENDS}
109 COMMENT "Building ${ofn}..."
112 # `make clean' must remove all those generated files:
113 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
115 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
116 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
120 # Creates a target for publicly exporting tablegen dependencies.
121 function(add_public_tablegen_target target)
122 if(NOT TABLEGEN_OUTPUT)
123 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
125 add_custom_target(${target}
126 DEPENDS ${TABLEGEN_OUTPUT})
127 if(LLVM_COMMON_DEPENDS)
128 add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
130 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
131 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
134 macro(add_tablegen target project)
135 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
136 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
138 # CMake doesn't let compilation units depend on their dependent libraries on some generators.
139 if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE)
140 # FIXME: It leaks to user, callee of add_tablegen.
141 set(LLVM_ENABLE_OBJLIB ON)
144 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
145 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
147 set(${project}_TABLEGEN "${target}" CACHE
148 STRING "Native TableGen executable. Saves building one when cross-compiling.")
150 # Effective tblgen executable to be used:
151 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
152 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
154 if(LLVM_USE_HOST_TOOLS)
155 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
156 # The NATIVE tablegen executable *must* depend on the current target one
157 # otherwise the native one won't get rebuilt when the tablgen sources
158 # change, and we end up with incorrect builds.
159 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
160 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
162 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
163 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
165 # Create an artificial dependency between tablegen projects, because they
166 # compile the same dependencies, thus using the same build folders.
167 # FIXME: A proper fix requires sequentially chaining tablegens.
168 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
169 TARGET LLVM-tablegen-host)
170 add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
173 # If we're using the host tablegen, and utils were not requested, we have no
174 # need to build this tablegen.
175 if ( NOT LLVM_BUILD_UTILS )
176 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
181 if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
182 set(export_to_llvmexports)
183 if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
184 NOT LLVM_DISTRIBUTION_COMPONENTS)
185 set(export_to_llvmexports EXPORT LLVMExports)
188 install(TARGETS ${target}
189 ${export_to_llvmexports}
191 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
192 if(NOT LLVM_ENABLE_IDE)
193 add_llvm_install_targets("install-${target}"
198 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})