blob: f15a23b624555665ca389bb68b6491c93374e96b [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001# 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.
4
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01005if(LLVM_MAIN_INCLUDE_DIR)
6 set(LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_INCLUDE_DIR})
7endif()
8
9function(tablegen project ofn)
10 # Validate calling context.
11 if(NOT ${project}_TABLEGEN_EXE)
12 message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
13 endif()
14
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
Andrew Walbran16937d02019-10-22 13:54:20 +010026 -o ${ofn_rel}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027 -d ${ofn_rel}.d
28 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
29 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
30 )
31 set(local_tds)
32 set(global_tds)
33 else()
34 file(GLOB local_tds "*.td")
35 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
36 set(additional_cmdline
Andrew Walbran16937d02019-10-22 13:54:20 +010037 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 )
39 endif()
40
41 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
42 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
43 else()
44 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
45 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
46 endif()
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")
51 endif()
52 endif()
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")
58 endif()
59 endif()
60
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.
Andrew Walbran16937d02019-10-22 13:54:20 +010070 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
72 ${LLVM_TABLEGEN_FLAGS}
73 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
74 ${additional_cmdline}
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}..."
82 )
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083
84 # `make clean' must remove all those generated files:
Andrew Walbran16937d02019-10-22 13:54:20 +010085 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086
87 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
88 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
89 GENERATED 1)
90endfunction()
91
92# Creates a target for publicly exporting tablegen dependencies.
93function(add_public_tablegen_target target)
94 if(NOT TABLEGEN_OUTPUT)
95 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
96 endif()
97 add_custom_target(${target}
98 DEPENDS ${TABLEGEN_OUTPUT})
99 if(LLVM_COMMON_DEPENDS)
100 add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
101 endif()
102 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
103 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
104endfunction()
105
106macro(add_tablegen target project)
107 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
108 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
109
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)
114 endif()
115
116 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
117 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
118
119 set(${project}_TABLEGEN "${target}" CACHE
120 STRING "Native TableGen executable. Saves building one when cross-compiling.")
121
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122 # Effective tblgen executable to be used:
123 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
124 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
125
126 if(LLVM_USE_HOST_TOOLS)
127 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100128 build_native_tool(${target} ${project}_TABLEGEN_EXE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100129 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
130
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100131 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
132 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
133
Andrew Walbran16937d02019-10-22 13:54:20 +0100134 # Create an artificial dependency between tablegen projects, because they
135 # compile the same dependencies, thus using the same build folders.
136 # FIXME: A proper fix requires sequentially chaining tablegens.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100137 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
138 TARGET LLVM-tablegen-host)
Andrew Walbran16937d02019-10-22 13:54:20 +0100139 add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
140 endif()
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100141
142 # If we're using the host tablegen, and utils were not requested, we have no
143 # need to build this tablegen.
144 if ( NOT LLVM_BUILD_UTILS )
145 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
146 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 endif()
148 endif()
149
150 if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100151 set(export_to_llvmexports)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152 if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
153 NOT LLVM_DISTRIBUTION_COMPONENTS)
154 set(export_to_llvmexports EXPORT LLVMExports)
155 endif()
156
157 install(TARGETS ${target}
158 ${export_to_llvmexports}
159 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
160 endif()
161 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
162endmacro()