Home | History | Annotate | Download | only in llvm
      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 
      5 include(LLVMExternalProjectUtils)
      6 
      7 if(LLVM_MAIN_INCLUDE_DIR)
      8   set(LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_INCLUDE_DIR})
      9 endif()
     10 
     11 function(tablegen project ofn)
     12   # Validate calling context.
     13   if(NOT ${project}_TABLEGEN_EXE)
     14     message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
     15   endif()
     16 
     17   file(GLOB local_tds "*.td")
     18   file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
     19 
     20   if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
     21     set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
     22   else()
     23     set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
     24       ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
     25   endif()
     26   if (LLVM_ENABLE_DAGISEL_COV)
     27     list(FIND ARGN "-gen-dag-isel" idx)
     28     if( NOT idx EQUAL -1 )
     29       list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
     30     endif()
     31   endif()
     32 
     33   # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the  DEPENDS list
     34   # (both the target and the file) to have .inc files rebuilt on
     35   # a tablegen change, as cmake does not propagate file-level dependencies
     36   # of custom targets. See the following ticket for more information:
     37   # https://cmake.org/Bug/view.php?id=15858
     38   # The dependency on both, the target and the file, produces the same
     39   # dependency twice in the result file when
     40   # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
     41   # but lets us having smaller and cleaner code here.
     42   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     43     # Generate tablegen output in a temporary file.
     44     COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
     45     ${LLVM_TABLEGEN_FLAGS}
     46     ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
     47     -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     48     # The file in LLVM_TARGET_DEFINITIONS may be not in the current
     49     # directory and local_tds may not contain it, so we must
     50     # explicitly list it here:
     51     DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
     52       ${local_tds} ${global_tds}
     53     ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
     54     COMMENT "Building ${ofn}..."
     55     )
     56   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
     57     # Only update the real output file if there are any differences.
     58     # This prevents recompilation of all the files depending on it if there
     59     # aren't any.
     60     COMMAND ${CMAKE_COMMAND} -E copy_if_different
     61         ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     62         ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
     63     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     64     COMMENT "Updating ${ofn}..."
     65     )
     66 
     67   # `make clean' must remove all those generated files:
     68   set_property(DIRECTORY APPEND
     69     PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}.tmp ${ofn})
     70 
     71   set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
     72   set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
     73     GENERATED 1)
     74 endfunction()
     75 
     76 # Creates a target for publicly exporting tablegen dependencies.
     77 function(add_public_tablegen_target target)
     78   if(NOT TABLEGEN_OUTPUT)
     79     message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
     80   endif()
     81   add_custom_target(${target}
     82     DEPENDS ${TABLEGEN_OUTPUT})
     83   if(LLVM_COMMON_DEPENDS)
     84     add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
     85   endif()
     86   set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
     87   set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
     88 endfunction()
     89 
     90 if(LLVM_USE_HOST_TOOLS)
     91   llvm_ExternalProject_BuildCmd(tblgen_build_cmd LLVMSupport
     92     ${LLVM_NATIVE_BUILD}
     93     CONFIGURATION Release)
     94   add_custom_command(OUTPUT LIB_LLVMTABLEGEN
     95       COMMAND ${tblgen_build_cmd}
     96       DEPENDS CONFIGURE_LLVM_NATIVE
     97       WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
     98       COMMENT "Building libLLVMTableGen for native TableGen..."
     99       USES_TERMINAL)
    100   add_custom_target(NATIVE_LIB_LLVMTABLEGEN DEPENDS LIB_LLVMTABLEGEN)
    101 endif(LLVM_USE_HOST_TOOLS)
    102 
    103 macro(add_tablegen target project)
    104   set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
    105   set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
    106 
    107   if(NOT XCODE)
    108     # FIXME: It leaks to user, callee of add_tablegen.
    109     set(LLVM_ENABLE_OBJLIB ON)
    110   endif()
    111 
    112   add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
    113   set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
    114 
    115   set(${project}_TABLEGEN "${target}" CACHE
    116       STRING "Native TableGen executable. Saves building one when cross-compiling.")
    117 
    118   # Upgrade existing LLVM_TABLEGEN setting.
    119   if(${project} STREQUAL LLVM)
    120     if(${LLVM_TABLEGEN} STREQUAL tblgen)
    121       set(LLVM_TABLEGEN "${target}" CACHE
    122           STRING "Native TableGen executable. Saves building one when cross-compiling."
    123           FORCE)
    124     endif()
    125   endif()
    126 
    127   # Effective tblgen executable to be used:
    128   set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
    129   set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
    130 
    131   if(LLVM_USE_HOST_TOOLS)
    132     if( ${${project}_TABLEGEN} STREQUAL "${target}" )
    133       if (NOT CMAKE_CONFIGURATION_TYPES)
    134         set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/bin/${target}")
    135       else()
    136         set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/Release/bin/${target}")
    137       endif()
    138       set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
    139 
    140       llvm_ExternalProject_BuildCmd(tblgen_build_cmd ${target}
    141                                     ${LLVM_NATIVE_BUILD}
    142                                     CONFIGURATION Release)
    143       add_custom_command(OUTPUT ${${project}_TABLEGEN_EXE}
    144         COMMAND ${tblgen_build_cmd}
    145         DEPENDS ${target} NATIVE_LIB_LLVMTABLEGEN
    146         WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
    147         COMMENT "Building native TableGen..."
    148         USES_TERMINAL)
    149       add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
    150       set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
    151     endif()
    152   endif()
    153 
    154   if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
    155     if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
    156         NOT LLVM_DISTRIBUTION_COMPONENTS)
    157       set(export_to_llvmexports EXPORT LLVMExports)
    158     endif()
    159 
    160     install(TARGETS ${target}
    161             ${export_to_llvmexports}
    162             RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
    163   endif()
    164   set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
    165 endmacro()
    166