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 57 # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's 58 # a possibility of generated tables being consumed by MSVC, generate arrays of 59 # char literals, instead. If we're cross-compiling, then conservatively assume 60 # that the source might be consumed by MSVC. 61 # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017 62 if (MSVC AND project STREQUAL LLVM) 63 list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0") 64 endif() 65 if (CMAKE_GENERATOR MATCHES "Visual Studio") 66 # Visual Studio has problems with llvm-tblgen's native --write-if-changed 67 # behavior. Since it doesn't do restat optimizations anyway, just don't 68 # pass --write-if-changed there. 69 set(tblgen_change_flag) 70 else() 71 set(tblgen_change_flag "--write-if-changed") 72 endif() 73 74 # With CMake 3.12 this can be reduced to: 75 # get_directory_property(tblgen_includes "INCLUDE_DIRECTORIES") 76 # list(TRANSFORM tblgen_includes PREPEND -I) 77 set(tblgen_includes) 78 get_directory_property(includes "INCLUDE_DIRECTORIES") 79 foreach(include ${includes}) 80 list(APPEND tblgen_includes -I ${include}) 81 endforeach() 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. 91 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn} 92 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR} 93 ${tblgen_includes} 94 ${LLVM_TABLEGEN_FLAGS} 95 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 96 ${tblgen_change_flag} 97 ${additional_cmdline} 98 # The file in LLVM_TARGET_DEFINITIONS may be not in the current 99 # directory and local_tds may not contain it, so we must 100 # explicitly list it here: 101 DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE} 102 ${local_tds} ${global_tds} 103 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 104 COMMENT "Building ${ofn}..." 105 ) 106 107 # `make clean' must remove all those generated files: 108 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}) 109 110 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE) 111 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES 112 GENERATED 1) 113endfunction() 114 115# Creates a target for publicly exporting tablegen dependencies. 116function(add_public_tablegen_target target) 117 if(NOT TABLEGEN_OUTPUT) 118 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.") 119 endif() 120 add_custom_target(${target} 121 DEPENDS ${TABLEGEN_OUTPUT}) 122 if(LLVM_COMMON_DEPENDS) 123 add_dependencies(${target} ${LLVM_COMMON_DEPENDS}) 124 endif() 125 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning") 126 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE) 127endfunction() 128 129macro(add_tablegen target project) 130 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) 131 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen) 132 133 # CMake-3.9 doesn't let compilation units depend on their dependent libraries. 134 if(NOT (CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.9) AND NOT XCODE) 135 # FIXME: It leaks to user, callee of add_tablegen. 136 set(LLVM_ENABLE_OBJLIB ON) 137 endif() 138 139 add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) 140 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) 141 142 set(${project}_TABLEGEN "${target}" CACHE 143 STRING "Native TableGen executable. Saves building one when cross-compiling.") 144 145 # Effective tblgen executable to be used: 146 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE) 147 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE) 148 149 if(LLVM_USE_HOST_TOOLS) 150 if( ${${project}_TABLEGEN} STREQUAL "${target}" ) 151 # The NATIVE tablegen executable *must* depend on the current target one 152 # otherwise the native one won't get rebuilt when the tablgen sources 153 # change, and we end up with incorrect builds. 154 build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target}) 155 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE) 156 157 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE}) 158 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE) 159 160 # Create an artificial dependency between tablegen projects, because they 161 # compile the same dependencies, thus using the same build folders. 162 # FIXME: A proper fix requires sequentially chaining tablegens. 163 if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND 164 TARGET LLVM-tablegen-host) 165 add_dependencies(${project}-tablegen-host LLVM-tablegen-host) 166 endif() 167 168 # If we're using the host tablegen, and utils were not requested, we have no 169 # need to build this tablegen. 170 if ( NOT LLVM_BUILD_UTILS ) 171 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON) 172 endif() 173 endif() 174 endif() 175 176 if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS) 177 set(export_to_llvmexports) 178 if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 179 NOT LLVM_DISTRIBUTION_COMPONENTS) 180 set(export_to_llvmexports EXPORT LLVMExports) 181 endif() 182 183 install(TARGETS ${target} 184 ${export_to_llvmexports} 185 COMPONENT ${target} 186 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}) 187 if(NOT LLVM_ENABLE_IDE) 188 add_llvm_install_targets("install-${target}" 189 DEPENDS ${target} 190 COMPONENT ${target}) 191 endif() 192 endif() 193 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target}) 194endmacro() 195