Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
| 4 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 5 | function(tablegen project ofn) |
| 6 | # Validate calling context. |
| 7 | if(NOT ${project}_TABLEGEN_EXE) |
| 8 | message(FATAL_ERROR "${project}_TABLEGEN_EXE not set") |
| 9 | endif() |
| 10 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 11 | # Use depfile instead of globbing arbitrary *.td(s) for Ninja. |
| 12 | if(CMAKE_GENERATOR STREQUAL "Ninja") |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 13 | # Make output path relative to build.ninja, assuming located on |
| 14 | # ${CMAKE_BINARY_DIR}. |
| 15 | # CMake emits build targets as relative paths but Ninja doesn't identify |
| 16 | # absolute path (in *.d) as relative path (in build.ninja) |
| 17 | # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory. |
| 18 | file(RELATIVE_PATH ofn_rel |
| 19 | ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}) |
| 20 | set(additional_cmdline |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 21 | -o ${ofn_rel} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | -d ${ofn_rel}.d |
| 23 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
| 24 | DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d |
| 25 | ) |
| 26 | set(local_tds) |
| 27 | set(global_tds) |
| 28 | else() |
| 29 | file(GLOB local_tds "*.td") |
| 30 | file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td") |
| 31 | set(additional_cmdline |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 32 | -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 33 | ) |
| 34 | endif() |
| 35 | |
| 36 | if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) |
| 37 | set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) |
| 38 | else() |
| 39 | set(LLVM_TARGET_DEFINITIONS_ABSOLUTE |
| 40 | ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS}) |
| 41 | endif() |
| 42 | if (LLVM_ENABLE_DAGISEL_COV) |
| 43 | list(FIND ARGN "-gen-dag-isel" idx) |
| 44 | if( NOT idx EQUAL -1 ) |
| 45 | list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage") |
| 46 | endif() |
| 47 | endif() |
| 48 | if (LLVM_ENABLE_GISEL_COV) |
| 49 | list(FIND ARGN "-gen-global-isel" idx) |
| 50 | if( NOT idx EQUAL -1 ) |
| 51 | list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage") |
| 52 | list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all") |
| 53 | endif() |
| 54 | endif() |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 55 | # Comments are only useful for Debug builds. Omit them if the backend |
| 56 | # supports it. |
| 57 | if (NOT (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR |
| 58 | uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")) |
| 59 | list(FIND ARGN "-gen-dag-isel" idx) |
| 60 | if (NOT idx EQUAL -1) |
| 61 | list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments") |
| 62 | endif() |
| 63 | endif() |
| 64 | |
| 65 | # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's |
| 66 | # a possibility of generated tables being consumed by MSVC, generate arrays of |
| 67 | # char literals, instead. If we're cross-compiling, then conservatively assume |
| 68 | # that the source might be consumed by MSVC. |
| 69 | # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017 |
| 70 | if (MSVC AND project STREQUAL LLVM) |
| 71 | list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0") |
| 72 | endif() |
| 73 | if (CMAKE_GENERATOR MATCHES "Visual Studio") |
| 74 | # Visual Studio has problems with llvm-tblgen's native --write-if-changed |
| 75 | # behavior. Since it doesn't do restat optimizations anyway, just don't |
| 76 | # pass --write-if-changed there. |
| 77 | set(tblgen_change_flag) |
| 78 | else() |
| 79 | set(tblgen_change_flag "--write-if-changed") |
| 80 | endif() |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | |
| 82 | # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list |
| 83 | # (both the target and the file) to have .inc files rebuilt on |
| 84 | # a tablegen change, as cmake does not propagate file-level dependencies |
| 85 | # of custom targets. See the following ticket for more information: |
| 86 | # https://cmake.org/Bug/view.php?id=15858 |
| 87 | # The dependency on both, the target and the file, produces the same |
| 88 | # dependency twice in the result file when |
| 89 | # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}") |
| 90 | # but lets us having smaller and cleaner code here. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 91 | get_directory_property(tblgen_includes INCLUDE_DIRECTORIES) |
| 92 | list(TRANSFORM tblgen_includes PREPEND -I) |
| 93 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 94 | add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR} |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 96 | ${tblgen_includes} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | ${LLVM_TABLEGEN_FLAGS} |
| 98 | ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 99 | ${tblgen_change_flag} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 100 | ${additional_cmdline} |
| 101 | # The file in LLVM_TARGET_DEFINITIONS may be not in the current |
| 102 | # directory and local_tds may not contain it, so we must |
| 103 | # explicitly list it here: |
| 104 | DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE} |
| 105 | ${local_tds} ${global_tds} |
| 106 | ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} |
| 107 | COMMENT "Building ${ofn}..." |
| 108 | ) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | |
| 110 | # `make clean' must remove all those generated files: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 111 | set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | |
| 113 | set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE) |
| 114 | set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES |
| 115 | GENERATED 1) |
| 116 | endfunction() |
| 117 | |
| 118 | # Creates a target for publicly exporting tablegen dependencies. |
| 119 | function(add_public_tablegen_target target) |
| 120 | if(NOT TABLEGEN_OUTPUT) |
| 121 | message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.") |
| 122 | endif() |
| 123 | add_custom_target(${target} |
| 124 | DEPENDS ${TABLEGEN_OUTPUT}) |
| 125 | if(LLVM_COMMON_DEPENDS) |
| 126 | add_dependencies(${target} ${LLVM_COMMON_DEPENDS}) |
| 127 | endif() |
| 128 | set_target_properties(${target} PROPERTIES FOLDER "Tablegenning") |
| 129 | set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE) |
| 130 | endfunction() |
| 131 | |
| 132 | macro(add_tablegen target project) |
| 133 | set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) |
| 134 | set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen) |
| 135 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 136 | # CMake doesn't let compilation units depend on their dependent libraries on some generators. |
| 137 | if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 138 | # FIXME: It leaks to user, callee of add_tablegen. |
| 139 | set(LLVM_ENABLE_OBJLIB ON) |
| 140 | endif() |
| 141 | |
| 142 | add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) |
| 143 | set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) |
| 144 | |
| 145 | set(${project}_TABLEGEN "${target}" CACHE |
| 146 | STRING "Native TableGen executable. Saves building one when cross-compiling.") |
| 147 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 148 | # Effective tblgen executable to be used: |
| 149 | set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE) |
| 150 | set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE) |
| 151 | |
| 152 | if(LLVM_USE_HOST_TOOLS) |
| 153 | if( ${${project}_TABLEGEN} STREQUAL "${target}" ) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 154 | # The NATIVE tablegen executable *must* depend on the current target one |
| 155 | # otherwise the native one won't get rebuilt when the tablgen sources |
| 156 | # change, and we end up with incorrect builds. |
| 157 | build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target}) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 158 | set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE) |
| 159 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 160 | add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE}) |
| 161 | set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE) |
| 162 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 163 | # Create an artificial dependency between tablegen projects, because they |
| 164 | # compile the same dependencies, thus using the same build folders. |
| 165 | # FIXME: A proper fix requires sequentially chaining tablegens. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 166 | if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND |
| 167 | TARGET LLVM-tablegen-host) |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 168 | add_dependencies(${project}-tablegen-host LLVM-tablegen-host) |
| 169 | endif() |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 170 | |
| 171 | # If we're using the host tablegen, and utils were not requested, we have no |
| 172 | # need to build this tablegen. |
| 173 | if ( NOT LLVM_BUILD_UTILS ) |
| 174 | set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON) |
| 175 | endif() |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 176 | endif() |
| 177 | endif() |
| 178 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 179 | if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 180 | set(export_to_llvmexports) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 181 | if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR |
| 182 | NOT LLVM_DISTRIBUTION_COMPONENTS) |
| 183 | set(export_to_llvmexports EXPORT LLVMExports) |
| 184 | endif() |
| 185 | |
| 186 | install(TARGETS ${target} |
| 187 | ${export_to_llvmexports} |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 188 | COMPONENT ${target} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 189 | RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 190 | if(NOT LLVM_ENABLE_IDE) |
| 191 | add_llvm_install_targets("install-${target}" |
| 192 | DEPENDS ${target} |
| 193 | COMPONENT ${target}) |
| 194 | endif() |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 195 | endif() |
| 196 | set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target}) |
| 197 | endmacro() |