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 5function(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 11 # Use depfile instead of globbing arbitrary *.td(s) 12 # DEPFILE is available for Ninja Generator with CMake>=3.7. 13 if(CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.7) 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 22 -o ${ofn_rel} 23 -d ${ofn_rel}.d 24 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 25 DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d 26 ) 27 set(local_tds) 28 set(global_tds) 29 else() 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} 34 ) 35 endif() 36 37 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) 38 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) 39 else() 40 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE 41 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS}) 42 endif() 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") 47 endif() 48 endif() 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") 54 endif() 55 endif() 56 # Comments are only useful for Debug builds. Omit them if the backend 57 # supports it. 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) 61 if (NOT idx EQUAL -1) 62 list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments") 63 endif() 64 endif() 65 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") 73 endif() 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) 79 else() 80 set(tblgen_change_flag "--write-if-changed") 81 endif() 82 83 # With CMake 3.12 this can be reduced to: 84 # get_directory_property(tblgen_includes "INCLUDE_DIRECTORIES") 85 # list(TRANSFORM tblgen_includes PREPEND -I) 86 set(tblgen_includes) 87 get_directory_property(includes "INCLUDE_DIRECTORIES") 88 foreach(include ${includes}) 89 list(APPEND tblgen_includes -I ${include}) 90 endforeach() 91 # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list 92 # (both the target and the file) to have .inc files rebuilt on 93 # a tablegen change, as cmake does not propagate file-level dependencies 94 # of custom targets. See the following ticket for more information: 95 # https://cmake.org/Bug/view.php?id=15858 96 # The dependency on both, the target and the file, produces the same 97 # dependency twice in the result file when 98 # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}") 99 # but lets us having smaller and cleaner code here. 100 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn} 101 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR} 102 ${tblgen_includes} 103 ${LLVM_TABLEGEN_FLAGS} 104 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 105 ${tblgen_change_flag} 106 ${additional_cmdline} 107 # The file in LLVM_TARGET_DEFINITIONS may be not in the current 108 # directory and local_tds may not contain it, so we must 109 # explicitly list it here: 110 DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE} 111 ${local_tds} ${global_tds} 112 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 113 COMMENT "Building ${ofn}..." 114 ) 115 116 # `make clean' must remove all those generated files: 117 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}) 118 119 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE) 120 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES 121 GENERATED 1) 122endfunction() 123 124# Creates a target for publicly exporting tablegen dependencies. 125function(add_public_tablegen_target target) 126 if(NOT TABLEGEN_OUTPUT) 127 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.") 128 endif() 129 add_custom_target(${target} 130 DEPENDS ${TABLEGEN_OUTPUT}) 131 if(LLVM_COMMON_DEPENDS) 132 add_dependencies(${target} ${LLVM_COMMON_DEPENDS}) 133 endif() 134 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning") 135 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE) 136endfunction() 137 138macro(add_tablegen target project) 139 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) 140 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen) 141 142 # CMake-3.9 doesn't let compilation units depend on their dependent libraries. 143 if(NOT (CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.9) AND NOT XCODE) 144 # FIXME: It leaks to user, callee of add_tablegen. 145 set(LLVM_ENABLE_OBJLIB ON) 146 endif() 147 148 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) 149 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) 150 151 set(${project}_TABLEGEN "${target}" CACHE 152 STRING "Native TableGen executable. Saves building one when cross-compiling.") 153 154 # Effective tblgen executable to be used: 155 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE) 156 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE) 157 158 if(LLVM_USE_HOST_TOOLS) 159 if( ${${project}_TABLEGEN} STREQUAL "${target}" ) 160 # The NATIVE tablegen executable *must* depend on the current target one 161 # otherwise the native one won't get rebuilt when the tablgen sources 162 # change, and we end up with incorrect builds. 163 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target}) 164 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE) 165 166 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE}) 167 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE) 168 169 # Create an artificial dependency between tablegen projects, because they 170 # compile the same dependencies, thus using the same build folders. 171 # FIXME: A proper fix requires sequentially chaining tablegens. 172 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND 173 TARGET LLVM-tablegen-host) 174 add_dependencies(${project}-tablegen-host LLVM-tablegen-host) 175 endif() 176 177 # If we're using the host tablegen, and utils were not requested, we have no 178 # need to build this tablegen. 179 if ( NOT LLVM_BUILD_UTILS ) 180 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON) 181 endif() 182 endif() 183 endif() 184 185 if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS) 186 set(export_to_llvmexports) 187 if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 188 NOT LLVM_DISTRIBUTION_COMPONENTS) 189 set(export_to_llvmexports EXPORT LLVMExports) 190 endif() 191 192 install(TARGETS ${target} 193 ${export_to_llvmexports} 194 COMPONENT ${target} 195 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}) 196 if(NOT LLVM_ENABLE_IDE) 197 add_llvm_install_targets("install-${target}" 198 DEPENDS ${target} 199 COMPONENT ${target}) 200 endif() 201 endif() 202 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target}) 203endmacro() 204