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  # Comments are only useful for Debug builds. Omit them if the backend
57  # supports it.
58  if (NOT (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR
59           uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))
60    list(FIND ARGN "-gen-dag-isel" idx)
61    if (NOT idx EQUAL -1)
62      list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
63    endif()
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  # We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the  DEPENDS list
84  # (both the target and the file) to have .inc files rebuilt on
85  # a tablegen change, as cmake does not propagate file-level dependencies
86  # of custom targets. See the following ticket for more information:
87  # https://cmake.org/Bug/view.php?id=15858
88  # The dependency on both, the target and the file, produces the same
89  # dependency twice in the result file when
90  # ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
91  # but lets us having smaller and cleaner code here.
92  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
93  list(TRANSFORM tblgen_includes PREPEND -I)
94
95  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
96    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
97    ${tblgen_includes}
98    ${LLVM_TABLEGEN_FLAGS}
99    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
100    ${tblgen_change_flag}
101    ${additional_cmdline}
102    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
103    # directory and local_tds may not contain it, so we must
104    # explicitly list it here:
105    DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
106      ${local_tds} ${global_tds}
107    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
108    ${LLVM_TARGET_DEPENDS}
109    COMMENT "Building ${ofn}..."
110    )
111
112  # `make clean' must remove all those generated files:
113  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
114
115  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
116  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
117    GENERATED 1)
118endfunction()
119
120# Creates a target for publicly exporting tablegen dependencies.
121function(add_public_tablegen_target target)
122  if(NOT TABLEGEN_OUTPUT)
123    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
124  endif()
125  add_custom_target(${target}
126    DEPENDS ${TABLEGEN_OUTPUT})
127  if(LLVM_COMMON_DEPENDS)
128    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
129  endif()
130  set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
131  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
132endfunction()
133
134macro(add_tablegen target project)
135  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
136  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
137
138  # CMake doesn't let compilation units depend on their dependent libraries on some generators.
139  if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE)
140    # FIXME: It leaks to user, callee of add_tablegen.
141    set(LLVM_ENABLE_OBJLIB ON)
142  endif()
143
144  add_llvm_executable(${target} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
145  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
146
147  set(${project}_TABLEGEN "${target}" CACHE
148      STRING "Native TableGen executable. Saves building one when cross-compiling.")
149
150  # Effective tblgen executable to be used:
151  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
152  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
153
154  if(LLVM_USE_HOST_TOOLS)
155    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
156      # The NATIVE tablegen executable *must* depend on the current target one
157      # otherwise the native one won't get rebuilt when the tablgen sources
158      # change, and we end up with incorrect builds.
159      build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
160      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
161
162      add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
163      set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
164
165      # Create an artificial dependency between tablegen projects, because they
166      # compile the same dependencies, thus using the same build folders.
167      # FIXME: A proper fix requires sequentially chaining tablegens.
168      if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
169          TARGET LLVM-tablegen-host)
170        add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
171      endif()
172
173      # If we're using the host tablegen, and utils were not requested, we have no
174      # need to build this tablegen.
175      if ( NOT LLVM_BUILD_UTILS )
176        set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
177      endif()
178    endif()
179  endif()
180
181  if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
182    set(export_to_llvmexports)
183    if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
184        NOT LLVM_DISTRIBUTION_COMPONENTS)
185      set(export_to_llvmexports EXPORT LLVMExports)
186    endif()
187
188    install(TARGETS ${target}
189            ${export_to_llvmexports}
190            COMPONENT ${target}
191            RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
192    if(NOT LLVM_ENABLE_IDE)
193      add_llvm_install_targets("install-${target}"
194                               DEPENDS ${target}
195                               COMPONENT ${target})
196    endif()
197  endif()
198  set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
199endmacro()
200