1 # LLVM_TARGET_DEFINITIONS must contain the name of the .td file to process.
2 # Extra parameters for `tblgen' may come after `ofn' parameter.
3 # Adds the name of the generated file to TABLEGEN_OUTPUT.
5 if(LLVM_MAIN_INCLUDE_DIR)
6 set(LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_INCLUDE_DIR})
9 function(tablegen project ofn)
10 # Validate calling context.
11 if(NOT ${project}_TABLEGEN_EXE)
12 message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
15 # Use depfile instead of globbing arbitrary *.td(s)
16 # DEPFILE is available for Ninja Generator with CMake>=3.7.
17 if(CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.7)
18 # Make output path relative to build.ninja, assuming located on
19 # ${CMAKE_BINARY_DIR}.
20 # CMake emits build targets as relative paths but Ninja doesn't identify
21 # absolute path (in *.d) as relative path (in build.ninja)
22 # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
23 file(RELATIVE_PATH ofn_rel
24 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
25 set(additional_cmdline
28 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
29 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
34 file(GLOB local_tds "*.td")
35 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
36 set(additional_cmdline
37 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
41 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
42 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
44 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
45 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
47 if (LLVM_ENABLE_DAGISEL_COV)
48 list(FIND ARGN "-gen-dag-isel" idx)
49 if( NOT idx EQUAL -1 )
50 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
53 if (LLVM_ENABLE_GISEL_COV)
54 list(FIND ARGN "-gen-global-isel" idx)
55 if( NOT idx EQUAL -1 )
56 list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
57 list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
61 # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
62 # (both the target and the file) to have .inc files rebuilt on
63 # a tablegen change, as cmake does not propagate file-level dependencies
64 # of custom targets. See the following ticket for more information:
65 # https://cmake.org/Bug/view.php?id=15858
66 # The dependency on both, the target and the file, produces the same
67 # dependency twice in the result file when
68 # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
69 # but lets us having smaller and cleaner code here.
70 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
71 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
72 ${LLVM_TABLEGEN_FLAGS}
73 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
75 # The file in LLVM_TARGET_DEFINITIONS may be not in the current
76 # directory and local_tds may not contain it, so we must
77 # explicitly list it here:
78 DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
79 ${local_tds} ${global_tds}
80 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
81 COMMENT "Building ${ofn}..."
84 # `make clean' must remove all those generated files:
85 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
87 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
88 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
92 # Creates a target for publicly exporting tablegen dependencies.
93 function(add_public_tablegen_target target)
94 if(NOT TABLEGEN_OUTPUT)
95 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
97 add_custom_target(${target}
98 DEPENDS ${TABLEGEN_OUTPUT})
99 if(LLVM_COMMON_DEPENDS)
100 add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
102 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
103 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
106 macro(add_tablegen target project)
107 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
108 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
110 # CMake-3.9 doesn't let compilation units depend on their dependent libraries.
111 if(NOT (CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.9) AND NOT XCODE)
112 # FIXME: It leaks to user, callee of add_tablegen.
113 set(LLVM_ENABLE_OBJLIB ON)
116 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
117 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
119 set(${project}_TABLEGEN "${target}" CACHE
120 STRING "Native TableGen executable. Saves building one when cross-compiling.")
122 # Effective tblgen executable to be used:
123 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
124 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
126 if(LLVM_USE_HOST_TOOLS)
127 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
128 # The NATIVE tablegen executable *must* depend on the current target one
129 # otherwise the native one won't get rebuilt when the tablgen sources
130 # change, and we end up with incorrect builds.
131 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
132 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
134 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
135 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
137 # Create an artificial dependency between tablegen projects, because they
138 # compile the same dependencies, thus using the same build folders.
139 # FIXME: A proper fix requires sequentially chaining tablegens.
140 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
141 TARGET LLVM-tablegen-host)
142 add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
145 # If we're using the host tablegen, and utils were not requested, we have no
146 # need to build this tablegen.
147 if ( NOT LLVM_BUILD_UTILS )
148 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
153 if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
154 set(export_to_llvmexports)
155 if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
156 NOT LLVM_DISTRIBUTION_COMPONENTS)
157 set(export_to_llvmexports EXPORT LLVMExports)
160 install(TARGETS ${target}
161 ${export_to_llvmexports}
162 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
164 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})