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 5include(LLVMExternalProjectUtils) 6 7function(tablegen project ofn) 8 # Validate calling context. 9 foreach(v 10 ${project}_TABLEGEN_EXE 11 LLVM_MAIN_SRC_DIR 12 LLVM_MAIN_INCLUDE_DIR 13 ) 14 if(NOT ${v}) 15 message(FATAL_ERROR "${v} not set") 16 endif() 17 endforeach() 18 19 file(GLOB local_tds "*.td") 20 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td") 21 22 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) 23 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS}) 24 else() 25 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE 26 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS}) 27 endif() 28 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp 29 # Generate tablegen output in a temporary file. 30 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR} 31 -I ${LLVM_MAIN_SRC_DIR}/lib/Target -I ${LLVM_MAIN_INCLUDE_DIR} 32 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 33 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp 34 # The file in LLVM_TARGET_DEFINITIONS may be not in the current 35 # directory and local_tds may not contain it, so we must 36 # explicitly list it here: 37 DEPENDS ${${project}_TABLEGEN_TARGET} ${local_tds} ${global_tds} 38 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE} 39 COMMENT "Building ${ofn}..." 40 ) 41 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn} 42 # Only update the real output file if there are any differences. 43 # This prevents recompilation of all the files depending on it if there 44 # aren't any. 45 COMMAND ${CMAKE_COMMAND} -E copy_if_different 46 ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp 47 ${CMAKE_CURRENT_BINARY_DIR}/${ofn} 48 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp 49 COMMENT "Updating ${ofn}..." 50 ) 51 52 # `make clean' must remove all those generated files: 53 set_property(DIRECTORY APPEND 54 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}.tmp ${ofn}) 55 56 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE) 57 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES 58 GENERATED 1) 59endfunction() 60 61# Creates a target for publicly exporting tablegen dependencies. 62function(add_public_tablegen_target target) 63 if(NOT TABLEGEN_OUTPUT) 64 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.") 65 endif() 66 add_custom_target(${target} 67 DEPENDS ${TABLEGEN_OUTPUT}) 68 if(LLVM_COMMON_DEPENDS) 69 add_dependencies(${target} ${LLVM_COMMON_DEPENDS}) 70 endif() 71 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning") 72 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE) 73endfunction() 74 75if(LLVM_USE_HOST_TOOLS) 76 llvm_ExternalProject_BuildCmd(tblgen_build_cmd LLVMSupport 77 ${LLVM_NATIVE_BUILD} 78 CONFIGURATION Release) 79 add_custom_command(OUTPUT LIB_LLVMTABLEGEN 80 COMMAND ${tblgen_build_cmd} 81 DEPENDS CONFIGURE_LLVM_NATIVE 82 WORKING_DIRECTORY ${LLVM_NATIVE_BUILD} 83 COMMENT "Building libLLVMTableGen for native TableGen..." 84 USES_TERMINAL) 85 add_custom_target(NATIVE_LIB_LLVMTABLEGEN DEPENDS LIB_LLVMTABLEGEN) 86endif(LLVM_USE_HOST_TOOLS) 87 88macro(add_tablegen target project) 89 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) 90 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen) 91 92 if(NOT XCODE) 93 # FIXME: It leaks to user, callee of add_tablegen. 94 set(LLVM_ENABLE_OBJLIB ON) 95 endif() 96 97 add_llvm_utility(${target} ${ARGN}) 98 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) 99 100 set(${project}_TABLEGEN "${target}" CACHE 101 STRING "Native TableGen executable. Saves building one when cross-compiling.") 102 103 # Upgrade existing LLVM_TABLEGEN setting. 104 if(${project} STREQUAL LLVM) 105 if(${LLVM_TABLEGEN} STREQUAL tblgen) 106 set(LLVM_TABLEGEN "${target}" CACHE 107 STRING "Native TableGen executable. Saves building one when cross-compiling." 108 FORCE) 109 endif() 110 endif() 111 112 # Effective tblgen executable to be used: 113 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE) 114 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE) 115 116 if(LLVM_USE_HOST_TOOLS) 117 if( ${${project}_TABLEGEN} STREQUAL "${target}" ) 118 if (NOT CMAKE_CONFIGURATION_TYPES) 119 set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/bin/${target}") 120 else() 121 set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/Release/bin/${target}") 122 endif() 123 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE) 124 125 llvm_ExternalProject_BuildCmd(tblgen_build_cmd ${target} 126 ${LLVM_NATIVE_BUILD} 127 CONFIGURATION Release) 128 add_custom_command(OUTPUT ${${project}_TABLEGEN_EXE} 129 COMMAND ${tblgen_build_cmd} 130 DEPENDS ${target} NATIVE_LIB_LLVMTABLEGEN 131 WORKING_DIRECTORY ${LLVM_NATIVE_BUILD} 132 COMMENT "Building native TableGen..." 133 USES_TERMINAL) 134 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE}) 135 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE) 136 endif() 137 endif() 138 139 if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 140 install(TARGETS ${target} 141 EXPORT LLVMExports 142 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}) 143 endif() 144 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target}) 145endmacro() 146