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