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})
9 # Validate calling context.
10 if(NOT ${project}_TABLEGEN_EXE)
11 message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
14 # Use depfile instead of globbing arbitrary *.td(s) for Ninja.
15 if(CMAKE_GENERATOR MATCHES "Ninja")
16 # Make output path relative to build.ninja, assuming located on
17 # ${CMAKE_BINARY_DIR}.
18 # CMake emits build targets as relative paths but Ninja doesn't identify
19 # absolute path (in *.d) as relative path (in build.ninja)
20 # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
21 file(RELATIVE_PATH ofn_rel
22 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
23 set(additional_cmdline
26 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
27 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
32 file(GLOB local_tds "*.td")
33 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
34 set(additional_cmdline
35 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
39 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
40 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
42 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
43 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
45 if (LLVM_ENABLE_DAGISEL_COV AND "-gen-dag-isel" IN_LIST ARGN)
46 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
48 if (LLVM_ENABLE_GISEL_COV AND "-gen-global-isel" IN_LIST ARGN)
49 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
50 list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
52 if (LLVM_OMIT_DAGISEL_COMMENTS AND "-gen-dag-isel" IN_LIST ARGN)
53 list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
56 # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
57 # a possibility of generated tables being consumed by MSVC, generate arrays of
58 # char literals, instead. If we're cross-compiling, then conservatively assume
59 # that the source might be consumed by MSVC.
60 # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
61 if (MSVC AND project STREQUAL LLVM)
62 list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
64 if (CMAKE_GENERATOR MATCHES "Visual Studio")
65 # Visual Studio has problems with llvm-tblgen's native --write-if-changed
66 # behavior. Since it doesn't do restat optimizations anyway, just don't
67 # pass --write-if-changed there.
68 set(tblgen_change_flag)
70 set(tblgen_change_flag "--write-if-changed")
73 if (NOT LLVM_ENABLE_WARNINGS)
74 list(APPEND LLVM_TABLEGEN_FLAGS "-no-warn-on-unused-template-args")
77 # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
78 # (both the target and the file) to have .inc files rebuilt on
79 # a tablegen change, as cmake does not propagate file-level dependencies
80 # of custom targets. See the following ticket for more information:
81 # https://cmake.org/Bug/view.php?id=15858
82 # The dependency on both, the target and the file, produces the same
83 # dependency twice in the result file when
84 # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
85 # but lets us having smaller and cleaner code here.
86 get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
87 list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
88 # Filter out empty items before prepending each entry with -I
89 list(REMOVE_ITEM tblgen_includes "")
90 list(TRANSFORM tblgen_includes PREPEND -I)
92 set(tablegen_exe ${${project}_TABLEGEN_EXE})
93 set(tablegen_depends ${${project}_TABLEGEN_TARGET} ${tablegen_exe})
95 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
96 COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -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 ${ARG_DEPENDS} ${tablegen_depends}
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 cmake_parse_arguments(ADD_TABLEGEN "" "DESTINATION;EXPORT" "" ${ARGN})
137 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
138 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
140 # CMake doesn't let compilation units depend on their dependent libraries on some generators.
141 if(NOT CMAKE_GENERATOR MATCHES "Ninja" AND NOT XCODE)
142 # FIXME: It leaks to user, callee of add_tablegen.
143 set(LLVM_ENABLE_OBJLIB ON)
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}")
156 set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}" CACHE
157 STRING "Native TableGen executable. Saves building one when cross-compiling.")
159 # Effective tblgen executable to be used:
160 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
161 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
163 if(LLVM_USE_HOST_TOOLS)
164 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
165 # The NATIVE tablegen executable *must* depend on the current target one
166 # otherwise the native one won't get rebuilt when the tablgen sources
167 # change, and we end up with incorrect builds.
168 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
169 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
171 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
172 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
174 # Create an artificial dependency between tablegen projects, because they
175 # compile the same dependencies, thus using the same build folders.
176 # FIXME: A proper fix requires sequentially chaining tablegens.
177 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
178 TARGET LLVM-tablegen-host)
179 add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
182 # If we're using the host tablegen, and utils were not requested, we have no
183 # need to build this tablegen.
184 if ( NOT LLVM_BUILD_UTILS )
185 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
190 if (ADD_TABLEGEN_DESTINATION AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND
191 (LLVM_BUILD_UTILS OR ${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS))
193 if(ADD_TABLEGEN_EXPORT)
194 get_target_export_arg(${target} ${ADD_TABLEGEN_EXPORT} export_arg)
196 install(TARGETS ${target}
199 RUNTIME DESTINATION "${ADD_TABLEGEN_DESTINATION}")
200 if(NOT LLVM_ENABLE_IDE)
201 add_llvm_install_targets("install-${target}"
206 if(ADD_TABLEGEN_EXPORT)
207 string(TOUPPER ${ADD_TABLEGEN_EXPORT} export_upper)
208 set_property(GLOBAL APPEND PROPERTY ${export_upper}_EXPORTS ${target})