xref: /llvm-project/llvm/cmake/modules/TableGen.cmake (revision 4ecbfacf9ecdc5bd9bf699d400c5058071b9500c)
1# LLVM_TARGET_DEFINITIONS must contain the name of the .td file to process,
2# while LLVM_TARGET_DEPENDS may contain additional file dependencies.
3# Extra parameters for `tblgen' may come after `ofn' parameter.
4# Adds the name of the generated file to TABLEGEN_OUTPUT.
5include(LLVMDistributionSupport)
6
7# Clear out any pre-existing compile_commands file before processing. This
8# allows for generating a clean compile_commands on each configure.
9file(REMOVE ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml)
10
11function(tablegen project ofn)
12  cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
13
14  # Override ${project} with ${project}_TABLEGEN_PROJECT
15  if(NOT "${${project}_TABLEGEN_PROJECT}" STREQUAL "")
16    set(project ${${project}_TABLEGEN_PROJECT})
17  endif()
18
19  # Validate calling context.
20  if(NOT ${project}_TABLEGEN_EXE)
21    message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
22  endif()
23
24  # Use depfile instead of globbing arbitrary *.td(s) for Ninja.
25  if(CMAKE_GENERATOR MATCHES "Ninja")
26    # Make output path relative to build.ninja, assuming located on
27    # ${CMAKE_BINARY_DIR}.
28    # CMake emits build targets as relative paths but Ninja doesn't identify
29    # absolute path (in *.d) as relative path (in build.ninja)
30    # Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
31    file(RELATIVE_PATH ofn_rel
32      ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
33    set(additional_cmdline
34      -o ${ofn_rel}
35      -d ${ofn_rel}.d
36      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
37      DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
38      )
39    set(local_tds)
40    set(global_tds)
41  else()
42    file(GLOB local_tds "*.td")
43    file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
44    set(additional_cmdline
45      -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
46      )
47  endif()
48
49  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
50    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
51  else()
52    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
53      ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
54  endif()
55  if (LLVM_ENABLE_DAGISEL_COV AND "-gen-dag-isel" IN_LIST ARGN)
56    list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
57  endif()
58  if (LLVM_ENABLE_GISEL_COV AND "-gen-global-isel" IN_LIST ARGN)
59    list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
60    list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
61  endif()
62  if (LLVM_OMIT_DAGISEL_COMMENTS AND "-gen-dag-isel" IN_LIST ARGN)
63    list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
64  endif()
65
66  # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
67  # a possibility of generated tables being consumed by MSVC, generate arrays of
68  # char literals, instead. If we're cross-compiling, then conservatively assume
69  # that the source might be consumed by MSVC.
70  # [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
71  if (MSVC AND project STREQUAL LLVM)
72    list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
73  endif()
74  if (CMAKE_GENERATOR MATCHES "Visual Studio")
75    # Visual Studio has problems with llvm-tblgen's native --write-if-changed
76    # behavior. Since it doesn't do restat optimizations anyway, just don't
77    # pass --write-if-changed there.
78    set(tblgen_change_flag)
79  else()
80    set(tblgen_change_flag "--write-if-changed")
81  endif()
82
83  if (NOT LLVM_ENABLE_WARNINGS)
84    list(APPEND LLVM_TABLEGEN_FLAGS "-no-warn-on-unused-template-args")
85  endif()
86
87  # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the  DEPENDS list
88  # (both the target and the file) to have .inc files rebuilt on
89  # a tablegen change, as cmake does not propagate file-level dependencies
90  # of custom targets. See the following ticket for more information:
91  # https://cmake.org/Bug/view.php?id=15858
92  # The dependency on both, the target and the file, produces the same
93  # dependency twice in the result file when
94  # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
95  # but lets us having smaller and cleaner code here.
96  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
97  list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
98
99  # Get the current set of include paths for this td file.
100  cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
101  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
102  list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
103  # Filter out any empty include items.
104  list(REMOVE_ITEM tblgen_includes "")
105
106  # Build the absolute path for the current input file.
107  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
108    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
109  else()
110    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
111  endif()
112
113  # Append this file and its includes to the compile commands file.
114  # This file is used by the TableGen LSP Language Server (tblgen-lsp-server).
115  file(APPEND ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml
116      "--- !FileInfo:\n"
117      "  filepath: \"${LLVM_TARGET_DEFINITIONS_ABSOLUTE}\"\n"
118      "  includes: \"${CMAKE_CURRENT_SOURCE_DIR};${tblgen_includes}\"\n"
119  )
120
121  # Filter out empty items before prepending each entry with -I
122  list(REMOVE_ITEM tblgen_includes "")
123  list(TRANSFORM tblgen_includes PREPEND -I)
124
125  set(tablegen_exe ${${project}_TABLEGEN_EXE})
126  set(tablegen_depends ${${project}_TABLEGEN_TARGET} ${tablegen_exe})
127
128  if(LLVM_PARALLEL_TABLEGEN_JOBS)
129    set(LLVM_TABLEGEN_JOB_POOL JOB_POOL tablegen_job_pool)
130  else()
131    set(LLVM_TABLEGEN_JOB_POOL "")
132  endif()
133
134  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
135    COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -I ${CMAKE_CURRENT_SOURCE_DIR}
136    ${tblgen_includes}
137    ${LLVM_TABLEGEN_FLAGS}
138    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
139    ${tblgen_change_flag}
140    ${additional_cmdline}
141    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
142    # directory and local_tds may not contain it, so we must
143    # explicitly list it here:
144    DEPENDS ${ARG_DEPENDS} ${tablegen_depends}
145      ${local_tds} ${global_tds}
146    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
147    ${LLVM_TARGET_DEPENDS}
148    ${LLVM_TABLEGEN_JOB_POOL}
149    COMMENT "Building ${ofn}..."
150    )
151
152  # `make clean' must remove all those generated files:
153  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
154
155  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
156  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
157    GENERATED 1)
158endfunction()
159
160# Creates a target for publicly exporting tablegen dependencies.
161function(add_public_tablegen_target target)
162  if(NOT TABLEGEN_OUTPUT)
163    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
164  endif()
165  add_custom_target(${target}
166    DEPENDS ${TABLEGEN_OUTPUT})
167  if(LLVM_COMMON_DEPENDS)
168    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
169  endif()
170  get_subproject_title(subproject_title)
171  set_target_properties(${target} PROPERTIES FOLDER "${subproject_title}/Tablegenning")
172  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
173endfunction()
174
175macro(add_tablegen target project)
176  cmake_parse_arguments(ADD_TABLEGEN "" "DESTINATION;EXPORT" "" ${ARGN})
177
178  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
179  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
180
181  add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB
182    ${ADD_TABLEGEN_UNPARSED_ARGUMENTS})
183  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
184
185  set(${project}_TABLEGEN_DEFAULT "${target}")
186  if (LLVM_NATIVE_TOOL_DIR)
187    if (EXISTS "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
188      set(${project}_TABLEGEN_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
189    endif()
190  endif()
191
192  # FIXME: Quick fix to reflect LLVM_TABLEGEN to llvm-min-tblgen
193  if("${target}" STREQUAL "llvm-min-tblgen"
194      AND NOT "${LLVM_TABLEGEN}" STREQUAL ""
195      AND NOT "${LLVM_TABLEGEN}" STREQUAL "llvm-tblgen")
196    set(${project}_TABLEGEN_DEFAULT "${LLVM_TABLEGEN}")
197  endif()
198
199  if(ADD_TABLEGEN_EXPORT)
200    set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}" CACHE
201      STRING "Native TableGen executable. Saves building one when cross-compiling.")
202  else()
203    # Internal tablegen
204    set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}")
205    set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
206  endif()
207
208  # Effective tblgen executable to be used:
209  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
210  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
211
212  if(LLVM_USE_HOST_TOOLS)
213    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
214      # The NATIVE tablegen executable *must* depend on the current target one
215      # otherwise the native one won't get rebuilt when the tablgen sources
216      # change, and we end up with incorrect builds.
217      build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
218      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
219
220      add_custom_target(${target}-host DEPENDS ${${project}_TABLEGEN_EXE})
221      get_subproject_title(subproject_title)
222      set_target_properties(${target}-host PROPERTIES FOLDER "${subproject_title}/Native")
223      set(${project}_TABLEGEN_TARGET ${target}-host PARENT_SCOPE)
224
225      # If we're using the host tablegen, and utils were not requested, we have no
226      # need to build this tablegen.
227      if ( NOT LLVM_BUILD_UTILS )
228        set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
229      endif()
230    endif()
231  endif()
232
233  if (ADD_TABLEGEN_DESTINATION AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND
234      (LLVM_BUILD_UTILS OR ${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS))
235    set(export_arg)
236    if(ADD_TABLEGEN_EXPORT)
237      get_target_export_arg(${target} ${ADD_TABLEGEN_EXPORT} export_arg)
238    endif()
239    install(TARGETS ${target}
240            ${export_arg}
241            COMPONENT ${target}
242            RUNTIME DESTINATION "${ADD_TABLEGEN_DESTINATION}")
243    if(NOT LLVM_ENABLE_IDE)
244      add_llvm_install_targets("install-${target}"
245                               DEPENDS ${target}
246                               COMPONENT ${target})
247    endif()
248  endif()
249  if(ADD_TABLEGEN_EXPORT)
250    string(TOUPPER ${ADD_TABLEGEN_EXPORT} export_upper)
251    set_property(GLOBAL APPEND PROPERTY ${export_upper}_EXPORTS ${target})
252  endif()
253endmacro()
254