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  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
129    COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -I ${CMAKE_CURRENT_SOURCE_DIR}
130    ${tblgen_includes}
131    ${LLVM_TABLEGEN_FLAGS}
132    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
133    ${tblgen_change_flag}
134    ${additional_cmdline}
135    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
136    # directory and local_tds may not contain it, so we must
137    # explicitly list it here:
138    DEPENDS ${ARG_DEPENDS} ${tablegen_depends}
139      ${local_tds} ${global_tds}
140    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
141    ${LLVM_TARGET_DEPENDS}
142    COMMENT "Building ${ofn}..."
143    )
144
145  # `make clean' must remove all those generated files:
146  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
147
148  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
149  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
150    GENERATED 1)
151endfunction()
152
153# Creates a target for publicly exporting tablegen dependencies.
154function(add_public_tablegen_target target)
155  if(NOT TABLEGEN_OUTPUT)
156    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
157  endif()
158  add_custom_target(${target}
159    DEPENDS ${TABLEGEN_OUTPUT})
160  if(LLVM_COMMON_DEPENDS)
161    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
162  endif()
163  set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
164  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
165endfunction()
166
167macro(add_tablegen target project)
168  cmake_parse_arguments(ADD_TABLEGEN "" "DESTINATION;EXPORT" "" ${ARGN})
169
170  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
171  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
172
173  add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB
174    ${ADD_TABLEGEN_UNPARSED_ARGUMENTS})
175  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
176
177  set(${project}_TABLEGEN_DEFAULT "${target}")
178  if (LLVM_NATIVE_TOOL_DIR)
179    if (EXISTS "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
180      set(${project}_TABLEGEN_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}")
181    endif()
182  endif()
183
184  # FIXME: Quick fix to reflect LLVM_TABLEGEN to llvm-min-tblgen
185  if("${target}" STREQUAL "llvm-min-tblgen"
186      AND NOT "${LLVM_TABLEGEN}" STREQUAL ""
187      AND NOT "${LLVM_TABLEGEN}" STREQUAL "llvm-tblgen")
188    set(${project}_TABLEGEN_DEFAULT "${LLVM_TABLEGEN}")
189  endif()
190
191  if(ADD_TABLEGEN_EXPORT)
192    set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}" CACHE
193      STRING "Native TableGen executable. Saves building one when cross-compiling.")
194  else()
195    # Internal tablegen
196    set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}")
197    set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
198  endif()
199
200  # Effective tblgen executable to be used:
201  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
202  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
203
204  if(LLVM_USE_HOST_TOOLS)
205    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
206      # The NATIVE tablegen executable *must* depend on the current target one
207      # otherwise the native one won't get rebuilt when the tablgen sources
208      # change, and we end up with incorrect builds.
209      build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
210      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
211
212      add_custom_target(${target}-host DEPENDS ${${project}_TABLEGEN_EXE})
213      set(${project}_TABLEGEN_TARGET ${target}-host PARENT_SCOPE)
214
215      # If we're using the host tablegen, and utils were not requested, we have no
216      # need to build this tablegen.
217      if ( NOT LLVM_BUILD_UTILS )
218        set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
219      endif()
220    endif()
221  endif()
222
223  if (ADD_TABLEGEN_DESTINATION AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND
224      (LLVM_BUILD_UTILS OR ${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS))
225    set(export_arg)
226    if(ADD_TABLEGEN_EXPORT)
227      get_target_export_arg(${target} ${ADD_TABLEGEN_EXPORT} export_arg)
228    endif()
229    install(TARGETS ${target}
230            ${export_arg}
231            COMPONENT ${target}
232            RUNTIME DESTINATION "${ADD_TABLEGEN_DESTINATION}")
233    if(NOT LLVM_ENABLE_IDE)
234      add_llvm_install_targets("install-${target}"
235                               DEPENDS ${target}
236                               COMPONENT ${target})
237    endif()
238  endif()
239  if(ADD_TABLEGEN_EXPORT)
240    string(TOUPPER ${ADD_TABLEGEN_EXPORT} export_upper)
241    set_property(GLOBAL APPEND PROPERTY ${export_upper}_EXPORTS ${target})
242  endif()
243endmacro()
244