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