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
5if(LLVM_MAIN_INCLUDE_DIR)
6  set(LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_INCLUDE_DIR})
7endif()
8
9function(tablegen project ofn)
10  # Validate calling context.
11  if(NOT ${project}_TABLEGEN_EXE)
12    message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
13  endif()
14
15  # Use depfile instead of globbing arbitrary *.td(s)
16  # DEPFILE is available for Ninja Generator with CMake>=3.7.
17  if(CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.7)
18    # Make output path relative to build.ninja, assuming located on
19    # ${CMAKE_BINARY_DIR}.
20    # CMake emits build targets as relative paths but Ninja doesn't identify
21    # absolute path (in *.d) as relative path (in build.ninja)
22    # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
23    file(RELATIVE_PATH ofn_rel
24      ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
25    set(additional_cmdline
26      -o ${ofn_rel}
27      -d ${ofn_rel}.d
28      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
29      DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
30      )
31    set(local_tds)
32    set(global_tds)
33  else()
34    file(GLOB local_tds "*.td")
35    file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
36    set(additional_cmdline
37      -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
38      )
39  endif()
40
41  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
42    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
43  else()
44    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
45      ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
46  endif()
47  if (LLVM_ENABLE_DAGISEL_COV)
48    list(FIND ARGN "-gen-dag-isel" idx)
49    if( NOT idx EQUAL -1 )
50      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
51    endif()
52  endif()
53  if (LLVM_ENABLE_GISEL_COV)
54    list(FIND ARGN "-gen-global-isel" idx)
55    if( NOT idx EQUAL -1 )
56      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
57      list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
58    endif()
59  endif()
60
61  if (CMAKE_GENERATOR MATCHES "Visual Studio")
62    # Visual Studio has problems with llvm-tblgen's native --write-if-changed
63    # behavior. Since it doesn't do restat optimizations anyway, just don't
64    # pass --write-if-changed there.
65    set(tblgen_change_flag)
66  else()
67    set(tblgen_change_flag "--write-if-changed")
68  endif()
69
70  # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the  DEPENDS list
71  # (both the target and the file) to have .inc files rebuilt on
72  # a tablegen change, as cmake does not propagate file-level dependencies
73  # of custom targets. See the following ticket for more information:
74  # https://cmake.org/Bug/view.php?id=15858
75  # The dependency on both, the target and the file, produces the same
76  # dependency twice in the result file when
77  # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
78  # but lets us having smaller and cleaner code here.
79  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
80    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
81    ${LLVM_TABLEGEN_FLAGS}
82    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
83    ${tblgen_change_flag}
84    ${additional_cmdline}
85    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
86    # directory and local_tds may not contain it, so we must
87    # explicitly list it here:
88    DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
89      ${local_tds} ${global_tds}
90    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
91    COMMENT "Building ${ofn}..."
92    )
93
94  # `make clean' must remove all those generated files:
95  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
96
97  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
98  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
99    GENERATED 1)
100endfunction()
101
102# Creates a target for publicly exporting tablegen dependencies.
103function(add_public_tablegen_target target)
104  if(NOT TABLEGEN_OUTPUT)
105    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
106  endif()
107  add_custom_target(${target}
108    DEPENDS ${TABLEGEN_OUTPUT})
109  if(LLVM_COMMON_DEPENDS)
110    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
111  endif()
112  set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
113  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
114endfunction()
115
116macro(add_tablegen target project)
117  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
118  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
119
120  # CMake-3.9 doesn't let compilation units depend on their dependent libraries.
121  if(NOT (CMAKE_GENERATOR STREQUAL "Ninja" AND NOT CMAKE_VERSION VERSION_LESS 3.9) AND NOT XCODE)
122    # FIXME: It leaks to user, callee of add_tablegen.
123    set(LLVM_ENABLE_OBJLIB ON)
124  endif()
125
126  add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
127  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
128
129  set(${project}_TABLEGEN "${target}" CACHE
130      STRING "Native TableGen executable. Saves building one when cross-compiling.")
131
132  # Effective tblgen executable to be used:
133  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
134  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
135
136  if(LLVM_USE_HOST_TOOLS)
137    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
138      # The NATIVE tablegen executable *must* depend on the current target one
139      # otherwise the native one won't get rebuilt when the tablgen sources
140      # change, and we end up with incorrect builds.
141      build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
142      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
143
144      add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
145      set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
146
147      # Create an artificial dependency between tablegen projects, because they
148      # compile the same dependencies, thus using the same build folders.
149      # FIXME: A proper fix requires sequentially chaining tablegens.
150      if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
151          TARGET LLVM-tablegen-host)
152        add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
153      endif()
154
155      # If we're using the host tablegen, and utils were not requested, we have no
156      # need to build this tablegen.
157      if ( NOT LLVM_BUILD_UTILS )
158        set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
159      endif()
160    endif()
161  endif()
162
163  if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
164    set(export_to_llvmexports)
165    if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
166        NOT LLVM_DISTRIBUTION_COMPONENTS)
167      set(export_to_llvmexports EXPORT LLVMExports)
168    endif()
169
170    install(TARGETS ${target}
171            ${export_to_llvmexports}
172            COMPONENT ${target}
173            RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
174    if(NOT LLVM_ENABLE_IDE)
175      add_llvm_install_targets("install-${target}"
176                               DEPENDS ${target}
177                               COMPONENT ${target})
178    endif()
179  endif()
180  set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
181endmacro()
182