add_custom_target(clang-cir-doc)

# This replicates part of the add_mlir_doc cmake function from MLIR that cannot
# be used here. This happens because it expects to be run inside MLIR directory
# which is not the case for CIR (and also FIR, both have similar workarounds).
function(add_clang_mlir_doc doc_filename output_file output_directory command)
  set(LLVM_TARGET_DEFINITIONS ${doc_filename}.td)
  # NB: MLIR_*_INCLUDE_DIR variables cannot be used here: MLIR configures
  # after Clang, so they are still empty at this point. Derive the same
  # roots from LLVM dirs instead (as CIR/CMakeLists.txt already does).
  mlir_tablegen(${output_file}.md ${command} ${ARGN}
                EXTRA_INCLUDES ${LLVM_MAIN_SRC_DIR}/../mlir/include
                               ${LLVM_BINARY_DIR}/tools/mlir/include)
  set(GEN_DOC_FILE ${CLANG_BINARY_DIR}/docs/CIR/_raw/${output_directory}${output_file}.md)
  add_custom_command(
          OUTPUT ${GEN_DOC_FILE}
          COMMAND ${CMAKE_COMMAND} -E copy
                  ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md
                  ${GEN_DOC_FILE}
          DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md)
  add_custom_target(${output_file}DocGen DEPENDS ${GEN_DOC_FILE})
  add_dependencies(clang-cir-doc ${output_file}DocGen)
endfunction()

add_subdirectory(IR)

set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name CIR)
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix CIR)
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix CIR)
add_public_tablegen_target(MLIRCIRPassIncGen)

add_clang_mlir_doc(Passes CIRPasses ./ -gen-pass-doc)
