xref: /aosp_15_r20/external/llvm/cmake/modules/TableGen.cmake (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker# LLVM_TARGET_DEFINITIONS must contain the name of the .td file to process.
2*9880d681SAndroid Build Coastguard Worker# Extra parameters for `tblgen' may come after `ofn' parameter.
3*9880d681SAndroid Build Coastguard Worker# Adds the name of the generated file to TABLEGEN_OUTPUT.
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Workerfunction(tablegen project ofn)
6*9880d681SAndroid Build Coastguard Worker  # Validate calling context.
7*9880d681SAndroid Build Coastguard Worker  foreach(v
8*9880d681SAndroid Build Coastguard Worker      ${project}_TABLEGEN_EXE
9*9880d681SAndroid Build Coastguard Worker      LLVM_MAIN_SRC_DIR
10*9880d681SAndroid Build Coastguard Worker      LLVM_MAIN_INCLUDE_DIR
11*9880d681SAndroid Build Coastguard Worker      )
12*9880d681SAndroid Build Coastguard Worker    if(NOT ${v})
13*9880d681SAndroid Build Coastguard Worker      message(FATAL_ERROR "${v} not set")
14*9880d681SAndroid Build Coastguard Worker    endif()
15*9880d681SAndroid Build Coastguard Worker  endforeach()
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker  file(GLOB local_tds "*.td")
18*9880d681SAndroid Build Coastguard Worker  file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
21*9880d681SAndroid Build Coastguard Worker    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
22*9880d681SAndroid Build Coastguard Worker  else()
23*9880d681SAndroid Build Coastguard Worker    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
24*9880d681SAndroid Build Coastguard Worker      ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
25*9880d681SAndroid Build Coastguard Worker  endif()
26*9880d681SAndroid Build Coastguard Worker  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
27*9880d681SAndroid Build Coastguard Worker    # Generate tablegen output in a temporary file.
28*9880d681SAndroid Build Coastguard Worker    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
29*9880d681SAndroid Build Coastguard Worker    -I ${LLVM_MAIN_SRC_DIR}/lib/Target -I ${LLVM_MAIN_INCLUDE_DIR}
30*9880d681SAndroid Build Coastguard Worker    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
31*9880d681SAndroid Build Coastguard Worker    -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
32*9880d681SAndroid Build Coastguard Worker    # The file in LLVM_TARGET_DEFINITIONS may be not in the current
33*9880d681SAndroid Build Coastguard Worker    # directory and local_tds may not contain it, so we must
34*9880d681SAndroid Build Coastguard Worker    # explicitly list it here:
35*9880d681SAndroid Build Coastguard Worker    DEPENDS ${${project}_TABLEGEN_TARGET} ${local_tds} ${global_tds}
36*9880d681SAndroid Build Coastguard Worker    ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
37*9880d681SAndroid Build Coastguard Worker    COMMENT "Building ${ofn}..."
38*9880d681SAndroid Build Coastguard Worker    )
39*9880d681SAndroid Build Coastguard Worker  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
40*9880d681SAndroid Build Coastguard Worker    # Only update the real output file if there are any differences.
41*9880d681SAndroid Build Coastguard Worker    # This prevents recompilation of all the files depending on it if there
42*9880d681SAndroid Build Coastguard Worker    # aren't any.
43*9880d681SAndroid Build Coastguard Worker    COMMAND ${CMAKE_COMMAND} -E copy_if_different
44*9880d681SAndroid Build Coastguard Worker        ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
45*9880d681SAndroid Build Coastguard Worker        ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
46*9880d681SAndroid Build Coastguard Worker    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
47*9880d681SAndroid Build Coastguard Worker    COMMENT "Updating ${ofn}..."
48*9880d681SAndroid Build Coastguard Worker    )
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker  # `make clean' must remove all those generated files:
51*9880d681SAndroid Build Coastguard Worker  set_property(DIRECTORY APPEND
52*9880d681SAndroid Build Coastguard Worker    PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}.tmp ${ofn})
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
55*9880d681SAndroid Build Coastguard Worker  set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
56*9880d681SAndroid Build Coastguard Worker    GENERATED 1)
57*9880d681SAndroid Build Coastguard Workerendfunction()
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker# Creates a target for publicly exporting tablegen dependencies.
60*9880d681SAndroid Build Coastguard Workerfunction(add_public_tablegen_target target)
61*9880d681SAndroid Build Coastguard Worker  if(NOT TABLEGEN_OUTPUT)
62*9880d681SAndroid Build Coastguard Worker    message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
63*9880d681SAndroid Build Coastguard Worker  endif()
64*9880d681SAndroid Build Coastguard Worker  add_custom_target(${target}
65*9880d681SAndroid Build Coastguard Worker    DEPENDS ${TABLEGEN_OUTPUT})
66*9880d681SAndroid Build Coastguard Worker  if(LLVM_COMMON_DEPENDS)
67*9880d681SAndroid Build Coastguard Worker    add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
68*9880d681SAndroid Build Coastguard Worker  endif()
69*9880d681SAndroid Build Coastguard Worker  set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
70*9880d681SAndroid Build Coastguard Worker  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
71*9880d681SAndroid Build Coastguard Workerendfunction()
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Workerif(LLVM_USE_HOST_TOOLS)
74*9880d681SAndroid Build Coastguard Worker  add_custom_command(OUTPUT LIB_LLVMSUPPORT
75*9880d681SAndroid Build Coastguard Worker      COMMAND ${CMAKE_COMMAND} --build . --target LLVMSupport --config Release
76*9880d681SAndroid Build Coastguard Worker      DEPENDS CONFIGURE_LLVM_NATIVE
77*9880d681SAndroid Build Coastguard Worker      WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
78*9880d681SAndroid Build Coastguard Worker      COMMENT "Building libLLVMSupport for native TableGen...")
79*9880d681SAndroid Build Coastguard Worker  add_custom_target(NATIVE_LIB_LLVMSUPPORT DEPENDS LIB_LLVMSUPPORT)
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker  add_custom_command(OUTPUT LIB_LLVMTABLEGEN
82*9880d681SAndroid Build Coastguard Worker      COMMAND ${CMAKE_COMMAND} --build . --target LLVMTableGen --config Release
83*9880d681SAndroid Build Coastguard Worker      DEPENDS CONFIGURE_LLVM_NATIVE
84*9880d681SAndroid Build Coastguard Worker      WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
85*9880d681SAndroid Build Coastguard Worker      COMMENT "Building libLLVMTableGen for native TableGen...")
86*9880d681SAndroid Build Coastguard Worker  add_custom_target(NATIVE_LIB_LLVMTABLEGEN DEPENDS LIB_LLVMTABLEGEN)
87*9880d681SAndroid Build Coastguard Workerendif(LLVM_USE_HOST_TOOLS)
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Workermacro(add_tablegen target project)
90*9880d681SAndroid Build Coastguard Worker  set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
91*9880d681SAndroid Build Coastguard Worker  set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker  if(NOT XCODE)
94*9880d681SAndroid Build Coastguard Worker    # FIXME: It leaks to user, callee of add_tablegen.
95*9880d681SAndroid Build Coastguard Worker    set(LLVM_ENABLE_OBJLIB ON)
96*9880d681SAndroid Build Coastguard Worker  endif()
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker  add_llvm_utility(${target} ${ARGN})
99*9880d681SAndroid Build Coastguard Worker  set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker  set(${project}_TABLEGEN "${target}" CACHE
102*9880d681SAndroid Build Coastguard Worker      STRING "Native TableGen executable. Saves building one when cross-compiling.")
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker  # Upgrade existing LLVM_TABLEGEN setting.
105*9880d681SAndroid Build Coastguard Worker  if(${project} STREQUAL LLVM)
106*9880d681SAndroid Build Coastguard Worker    if(${LLVM_TABLEGEN} STREQUAL tblgen)
107*9880d681SAndroid Build Coastguard Worker      set(LLVM_TABLEGEN "${target}" CACHE
108*9880d681SAndroid Build Coastguard Worker          STRING "Native TableGen executable. Saves building one when cross-compiling."
109*9880d681SAndroid Build Coastguard Worker          FORCE)
110*9880d681SAndroid Build Coastguard Worker    endif()
111*9880d681SAndroid Build Coastguard Worker  endif()
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker  # Effective tblgen executable to be used:
114*9880d681SAndroid Build Coastguard Worker  set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
115*9880d681SAndroid Build Coastguard Worker  set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker  if(LLVM_USE_HOST_TOOLS)
118*9880d681SAndroid Build Coastguard Worker    if( ${${project}_TABLEGEN} STREQUAL "${target}" )
119*9880d681SAndroid Build Coastguard Worker      if (NOT CMAKE_CONFIGURATION_TYPES)
120*9880d681SAndroid Build Coastguard Worker        set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/bin/${target}")
121*9880d681SAndroid Build Coastguard Worker      else()
122*9880d681SAndroid Build Coastguard Worker        set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/Release/bin/${target}")
123*9880d681SAndroid Build Coastguard Worker      endif()
124*9880d681SAndroid Build Coastguard Worker      set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker      add_custom_command(OUTPUT ${${project}_TABLEGEN_EXE}
127*9880d681SAndroid Build Coastguard Worker        COMMAND ${CMAKE_COMMAND} --build . --target ${target} --config Release
128*9880d681SAndroid Build Coastguard Worker        DEPENDS ${target} NATIVE_LIB_LLVMSUPPORT NATIVE_LIB_LLVMTABLEGEN
129*9880d681SAndroid Build Coastguard Worker        WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
130*9880d681SAndroid Build Coastguard Worker        COMMENT "Building native TableGen...")
131*9880d681SAndroid Build Coastguard Worker      add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
132*9880d681SAndroid Build Coastguard Worker      set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
133*9880d681SAndroid Build Coastguard Worker    endif()
134*9880d681SAndroid Build Coastguard Worker  endif()
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker  if( MINGW )
137*9880d681SAndroid Build Coastguard Worker    if(CMAKE_SIZEOF_VOID_P MATCHES "8")
138*9880d681SAndroid Build Coastguard Worker      set_target_properties(${target} PROPERTIES LINK_FLAGS -Wl,--stack,16777216)
139*9880d681SAndroid Build Coastguard Worker    endif(CMAKE_SIZEOF_VOID_P MATCHES "8")
140*9880d681SAndroid Build Coastguard Worker  endif( MINGW )
141*9880d681SAndroid Build Coastguard Worker  if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
142*9880d681SAndroid Build Coastguard Worker    install(TARGETS ${target}
143*9880d681SAndroid Build Coastguard Worker            EXPORT LLVMExports
144*9880d681SAndroid Build Coastguard Worker            RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
145*9880d681SAndroid Build Coastguard Worker  endif()
146*9880d681SAndroid Build Coastguard Worker  set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
147*9880d681SAndroid Build Coastguard Workerendmacro()
148