106f32e7eSjoerg# LLVM_TARGET_DEFINITIONS must contain the name of the .td file to process.
206f32e7eSjoerg# Extra parameters for `tblgen' may come after `ofn' parameter.
306f32e7eSjoerg# Adds the name of the generated file to TABLEGEN_OUTPUT.
406f32e7eSjoerg
506f32e7eSjoergfunction(tablegen project ofn)
606f32e7eSjoerg  # Validate calling context.
706f32e7eSjoerg  if(NOT ${project}_TABLEGEN_EXE)
806f32e7eSjoerg    message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
906f32e7eSjoerg  endif()
1006f32e7eSjoerg
11*da58b97aSjoerg  # Use depfile instead of globbing arbitrary *.td(s) for Ninja.
12*da58b97aSjoerg  if(CMAKE_GENERATOR STREQUAL "Ninja")
1306f32e7eSjoerg    # Make output path relative to build.ninja, assuming located on
1406f32e7eSjoerg    # ${CMAKE_BINARY_DIR}.
1506f32e7eSjoerg    # CMake emits build targets as relative paths but Ninja doesn't identify
1606f32e7eSjoerg    # absolute path (in *.d) as relative path (in build.ninja)
1706f32e7eSjoerg    # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
1806f32e7eSjoerg    file(RELATIVE_PATH ofn_rel
1906f32e7eSjoerg      ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
2006f32e7eSjoerg    set(additional_cmdline
2106f32e7eSjoerg      -o ${ofn_rel}
2206f32e7eSjoerg      -d ${ofn_rel}.d
2306f32e7eSjoerg      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
2406f32e7eSjoerg      DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
2506f32e7eSjoerg      )
2606f32e7eSjoerg    set(local_tds)
2706f32e7eSjoerg    set(global_tds)
2806f32e7eSjoerg  else()
2906f32e7eSjoerg    file(GLOB local_tds "*.td")
3006f32e7eSjoerg    file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
3106f32e7eSjoerg    set(additional_cmdline
3206f32e7eSjoerg      -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
3306f32e7eSjoerg      )
3406f32e7eSjoerg  endif()
3506f32e7eSjoerg
3606f32e7eSjoerg  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
3706f32e7eSjoerg    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
3806f32e7eSjoerg  else()
3906f32e7eSjoerg    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
4006f32e7eSjoerg      ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
4106f32e7eSjoerg  endif()
4206f32e7eSjoerg  if (LLVM_ENABLE_DAGISEL_COV)
4306f32e7eSjoerg    list(FIND ARGN "-gen-dag-isel" idx)
4406f32e7eSjoerg    if( NOT idx EQUAL -1 )
4506f32e7eSjoerg      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
4606f32e7eSjoerg    endif()
4706f32e7eSjoerg  endif()
4806f32e7eSjoerg  if (LLVM_ENABLE_GISEL_COV)
4906f32e7eSjoerg    list(FIND ARGN "-gen-global-isel" idx)
5006f32e7eSjoerg    if( NOT idx EQUAL -1 )
5106f32e7eSjoerg      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
5206f32e7eSjoerg      list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
5306f32e7eSjoerg    endif()
5406f32e7eSjoerg  endif()
55*da58b97aSjoerg  # Comments are only useful for Debug builds. Omit them if the backend
56*da58b97aSjoerg  # supports it.
57*da58b97aSjoerg  if (NOT (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR
58*da58b97aSjoerg           uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))
59*da58b97aSjoerg    list(FIND ARGN "-gen-dag-isel" idx)
60*da58b97aSjoerg    if (NOT idx EQUAL -1)
61*da58b97aSjoerg      list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
62*da58b97aSjoerg    endif()
63*da58b97aSjoerg  endif()
6406f32e7eSjoerg
65*da58b97aSjoerg  # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
66*da58b97aSjoerg  # a possibility of generated tables being consumed by MSVC, generate arrays of
67*da58b97aSjoerg  # char literals, instead. If we're cross-compiling, then conservatively assume
68*da58b97aSjoerg  # that the source might be consumed by MSVC.
69*da58b97aSjoerg  # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
70*da58b97aSjoerg  if (MSVC AND project STREQUAL LLVM)
71*da58b97aSjoerg    list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
72*da58b97aSjoerg  endif()
7306f32e7eSjoerg  if (CMAKE_GENERATOR MATCHES "Visual Studio")
7406f32e7eSjoerg    # Visual Studio has problems with llvm-tblgen's native --write-if-changed
7506f32e7eSjoerg    # behavior. Since it doesn't do restat optimizations anyway, just don't
7606f32e7eSjoerg    # pass --write-if-changed there.
7706f32e7eSjoerg    set(tblgen_change_flag)
7806f32e7eSjoerg  else()
7906f32e7eSjoerg    set(tblgen_change_flag "--write-if-changed")
8006f32e7eSjoerg  endif()
8106f32e7eSjoerg
8206f32e7eSjoerg  # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the  DEPENDS list
8306f32e7eSjoerg  # (both the target and the file) to have .inc files rebuilt on
8406f32e7eSjoerg  # a tablegen change, as cmake does not propagate file-level dependencies
8506f32e7eSjoerg  # of custom targets. See the following ticket for more information:
8606f32e7eSjoerg  # https://cmake.org/Bug/view.php?id=15858
8706f32e7eSjoerg  # The dependency on both, the target and the file, produces the same
8806f32e7eSjoerg  # dependency twice in the result file when
8906f32e7eSjoerg  # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
9006f32e7eSjoerg  # but lets us having smaller and cleaner code here.
91*da58b97aSjoerg  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
92*da58b97aSjoerg  list(TRANSFORM tblgen_includes PREPEND -I)
93*da58b97aSjoerg
9406f32e7eSjoerg  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
9506f32e7eSjoerg    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
96*da58b97aSjoerg    ${tblgen_includes}
9706f32e7eSjoerg    ${LLVM_TABLEGEN_FLAGS}
9806f32e7eSjoerg    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
9906f32e7eSjoerg    ${tblgen_change_flag}
10006f32e7eSjoerg    ${additional_cmdline}
10106f32e7eSjoerg    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
10206f32e7eSjoerg    # directory and local_tds may not contain it, so we must
10306f32e7eSjoerg    # explicitly list it here:
10406f32e7eSjoerg    DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
10506f32e7eSjoerg      ${local_tds} ${global_tds}
10606f32e7eSjoerg    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
10706f32e7eSjoerg    COMMENT "Building ${ofn}..."
10806f32e7eSjoerg    )
10906f32e7eSjoerg
11006f32e7eSjoerg  # `make clean' must remove all those generated files:
11106f32e7eSjoerg  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
11206f32e7eSjoerg
11306f32e7eSjoerg  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
11406f32e7eSjoerg  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
11506f32e7eSjoerg    GENERATED 1)
11606f32e7eSjoergendfunction()
11706f32e7eSjoerg
11806f32e7eSjoerg# Creates a target for publicly exporting tablegen dependencies.
11906f32e7eSjoergfunction(add_public_tablegen_target target)
12006f32e7eSjoerg  if(NOT TABLEGEN_OUTPUT)
12106f32e7eSjoerg    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
12206f32e7eSjoerg  endif()
12306f32e7eSjoerg  add_custom_target(${target}
12406f32e7eSjoerg    DEPENDS ${TABLEGEN_OUTPUT})
12506f32e7eSjoerg  if(LLVM_COMMON_DEPENDS)
12606f32e7eSjoerg    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
12706f32e7eSjoerg  endif()
12806f32e7eSjoerg  set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
12906f32e7eSjoerg  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
13006f32e7eSjoergendfunction()
13106f32e7eSjoerg
13206f32e7eSjoergmacro(add_tablegen target project)
13306f32e7eSjoerg  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
13406f32e7eSjoerg  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
13506f32e7eSjoerg
136*da58b97aSjoerg  # CMake doesn't let compilation units depend on their dependent libraries on some generators.
137*da58b97aSjoerg  if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE)
13806f32e7eSjoerg    # FIXME: It leaks to user, callee of add_tablegen.
13906f32e7eSjoerg    set(LLVM_ENABLE_OBJLIB ON)
14006f32e7eSjoerg  endif()
14106f32e7eSjoerg
14206f32e7eSjoerg  add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
14306f32e7eSjoerg  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
14406f32e7eSjoerg
14506f32e7eSjoerg  set(${project}_TABLEGEN "${target}" CACHE
14606f32e7eSjoerg      STRING "Native TableGen executable. Saves building one when cross-compiling.")
14706f32e7eSjoerg
14806f32e7eSjoerg  # Effective tblgen executable to be used:
14906f32e7eSjoerg  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
15006f32e7eSjoerg  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
15106f32e7eSjoerg
15206f32e7eSjoerg  if(LLVM_USE_HOST_TOOLS)
15306f32e7eSjoerg    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
15406f32e7eSjoerg      # The NATIVE tablegen executable *must* depend on the current target one
15506f32e7eSjoerg      # otherwise the native one won't get rebuilt when the tablgen sources
15606f32e7eSjoerg      # change, and we end up with incorrect builds.
15706f32e7eSjoerg      build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
15806f32e7eSjoerg      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
15906f32e7eSjoerg
16006f32e7eSjoerg      add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
16106f32e7eSjoerg      set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
16206f32e7eSjoerg
16306f32e7eSjoerg      # Create an artificial dependency between tablegen projects, because they
16406f32e7eSjoerg      # compile the same dependencies, thus using the same build folders.
16506f32e7eSjoerg      # FIXME: A proper fix requires sequentially chaining tablegens.
16606f32e7eSjoerg      if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
16706f32e7eSjoerg          TARGET LLVM-tablegen-host)
16806f32e7eSjoerg        add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
16906f32e7eSjoerg      endif()
17006f32e7eSjoerg
17106f32e7eSjoerg      # If we're using the host tablegen, and utils were not requested, we have no
17206f32e7eSjoerg      # need to build this tablegen.
17306f32e7eSjoerg      if ( NOT LLVM_BUILD_UTILS )
17406f32e7eSjoerg        set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
17506f32e7eSjoerg      endif()
17606f32e7eSjoerg    endif()
17706f32e7eSjoerg  endif()
17806f32e7eSjoerg
179*da58b97aSjoerg  if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
18006f32e7eSjoerg    set(export_to_llvmexports)
18106f32e7eSjoerg    if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
18206f32e7eSjoerg        NOT LLVM_DISTRIBUTION_COMPONENTS)
18306f32e7eSjoerg      set(export_to_llvmexports EXPORT LLVMExports)
18406f32e7eSjoerg    endif()
18506f32e7eSjoerg
18606f32e7eSjoerg    install(TARGETS ${target}
18706f32e7eSjoerg            ${export_to_llvmexports}
18806f32e7eSjoerg            COMPONENT ${target}
18906f32e7eSjoerg            RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
19006f32e7eSjoerg    if(NOT LLVM_ENABLE_IDE)
19106f32e7eSjoerg      add_llvm_install_targets("install-${target}"
19206f32e7eSjoerg                               DEPENDS ${target}
19306f32e7eSjoerg                               COMPONENT ${target})
19406f32e7eSjoerg    endif()
19506f32e7eSjoerg  endif()
19606f32e7eSjoerg  set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
19706f32e7eSjoergendmacro()
198