TableGen.cmake 4.8 KB

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