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