blob: c9cfb37f8d2c8f6954a49b733e74b9a65781b51e [file] [log] [blame]
Oscar Fuentescdc95492008-09-26 04:40:32 +00001# 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
Chris Bieneman76864f92016-09-06 20:27:07 +00005include(LLVMExternalProjectUtils)
6
NAKAMURA Takumi93d4cda2014-01-26 12:41:33 +00007function(tablegen project ofn)
NAKAMURA Takumi04817e42014-02-12 11:50:11 +00008 # Validate calling context.
9 foreach(v
10 ${project}_TABLEGEN_EXE
11 LLVM_MAIN_SRC_DIR
12 LLVM_MAIN_INCLUDE_DIR
13 )
14 if(NOT ${v})
15 message(FATAL_ERROR "${v} not set")
16 endif()
17 endforeach()
18
NAKAMURA Takumie6bf06b2014-02-04 01:17:12 +000019 file(GLOB local_tds "*.td")
20 file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
Douglas Gregord36792c2009-03-16 21:35:18 +000021
Douglas Gregor934ced72010-06-17 15:17:07 +000022 if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
23 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
24 else()
NAKAMURA Takumidba47bf2014-02-09 16:38:31 +000025 set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
Douglas Gregor934ced72010-06-17 15:17:07 +000026 ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
27 endif()
Oscar Fuentesa5221752010-10-22 17:37:42 +000028 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
Oscar Fuentes861e51a2010-10-22 16:20:10 +000029 # Generate tablegen output in a temporary file.
NAKAMURA Takumie6bf06b2014-02-04 01:17:12 +000030 COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
31 -I ${LLVM_MAIN_SRC_DIR}/lib/Target -I ${LLVM_MAIN_INCLUDE_DIR}
NAKAMURA Takumidba47bf2014-02-09 16:38:31 +000032 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
Oscar Fuentes861e51a2010-10-22 16:20:10 +000033 -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
Oscar Fuentes0db031d2011-01-03 20:01:32 +000034 # The file in LLVM_TARGET_DEFINITIONS may be not in the current
35 # directory and local_tds may not contain it, so we must
36 # explicitly list it here:
Chris Bieneman3eddcae2015-04-24 17:09:20 +000037 DEPENDS ${${project}_TABLEGEN_TARGET} ${local_tds} ${global_tds}
Oscar Fuentesf4202ba2011-02-03 20:57:36 +000038 ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
Oscar Fuentesa5221752010-10-22 17:37:42 +000039 COMMENT "Building ${ofn}..."
40 )
41 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
Oscar Fuentes861e51a2010-10-22 16:20:10 +000042 # Only update the real output file if there are any differences.
43 # This prevents recompilation of all the files depending on it if there
44 # aren't any.
45 COMMAND ${CMAKE_COMMAND} -E copy_if_different
46 ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
47 ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
Oscar Fuentesa5221752010-10-22 17:37:42 +000048 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
NAKAMURA Takumi96688902014-01-12 17:42:43 +000049 COMMENT "Updating ${ofn}..."
Oscar Fuentescdc95492008-09-26 04:40:32 +000050 )
Oscar Fuentesa6aa8782011-02-04 03:47:50 +000051
52 # `make clean' must remove all those generated files:
53 set_property(DIRECTORY APPEND
54 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn}.tmp ${ofn})
55
NAKAMURA Takumi93d4cda2014-01-26 12:41:33 +000056 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
NAKAMURA Takumid93593f2014-02-23 12:54:15 +000057 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
58 GENERATED 1)
59endfunction()
Oscar Fuentesa2e62742011-07-25 14:11:55 +000060
NAKAMURA Takumid93593f2014-02-23 12:54:15 +000061# Creates a target for publicly exporting tablegen dependencies.
62function(add_public_tablegen_target target)
63 if(NOT TABLEGEN_OUTPUT)
64 message(FATAL_ERROR "Requires tablegen() definitions as TABLEGEN_OUTPUT.")
65 endif()
66 add_custom_target(${target}
67 DEPENDS ${TABLEGEN_OUTPUT})
68 if(LLVM_COMMON_DEPENDS)
69 add_dependencies(${target} ${LLVM_COMMON_DEPENDS})
70 endif()
71 set_target_properties(${target} PROPERTIES FOLDER "Tablegenning")
72 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
73endfunction()
Peter Collingbournefb3d9352011-10-06 01:51:51 +000074
Chris Bieneman8a3e6e192015-10-09 22:26:04 +000075if(LLVM_USE_HOST_TOOLS)
Chris Bieneman76864f92016-09-06 20:27:07 +000076 llvm_ExternalProject_BuildCmd(tblgen_build_cmd LLVMSupport
77 ${LLVM_NATIVE_BUILD}
78 CONFIGURATION Release)
Chris Bieneman9b058a62015-10-23 19:48:17 +000079 add_custom_command(OUTPUT LIB_LLVMTABLEGEN
Chris Bieneman76864f92016-09-06 20:27:07 +000080 COMMAND ${tblgen_build_cmd}
Chris Bieneman9b058a62015-10-23 19:48:17 +000081 DEPENDS CONFIGURE_LLVM_NATIVE
82 WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
Chris Bieneman76864f92016-09-06 20:27:07 +000083 COMMENT "Building libLLVMTableGen for native TableGen..."
84 USES_TERMINAL)
Chris Bieneman9b058a62015-10-23 19:48:17 +000085 add_custom_target(NATIVE_LIB_LLVMTABLEGEN DEPENDS LIB_LLVMTABLEGEN)
Chris Bieneman8a3e6e192015-10-09 22:26:04 +000086endif(LLVM_USE_HOST_TOOLS)
87
Peter Collingbournefb3d9352011-10-06 01:51:51 +000088macro(add_tablegen target project)
Chandler Carruth39bf89b2011-11-02 05:03:06 +000089 set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
90 set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +000091
NAKAMURA Takumid4b21932015-09-02 10:11:26 +000092 if(NOT XCODE)
93 # FIXME: It leaks to user, callee of add_tablegen.
94 set(LLVM_ENABLE_OBJLIB ON)
95 endif()
NAKAMURA Takumie6bc0932015-08-27 16:10:47 +000096
Andrew Wilkinsbb6d95f2015-09-05 08:27:33 +000097 add_llvm_utility(${target} ${ARGN})
Chandler Carruth3198cc72011-11-02 16:55:57 +000098 set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
Peter Collingbournefb3d9352011-10-06 01:51:51 +000099
100 set(${project}_TABLEGEN "${target}" CACHE
101 STRING "Native TableGen executable. Saves building one when cross-compiling.")
102
103 # Upgrade existing LLVM_TABLEGEN setting.
104 if(${project} STREQUAL LLVM)
105 if(${LLVM_TABLEGEN} STREQUAL tblgen)
106 set(LLVM_TABLEGEN "${target}" CACHE
107 STRING "Native TableGen executable. Saves building one when cross-compiling."
108 FORCE)
109 endif()
110 endif()
NAKAMURA Takumidba47bf2014-02-09 16:38:31 +0000111
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000112 # Effective tblgen executable to be used:
113 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
Chris Bieneman3eddcae2015-04-24 17:09:20 +0000114 set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000115
Chris Bienemanda91ceb2015-03-10 20:48:02 +0000116 if(LLVM_USE_HOST_TOOLS)
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000117 if( ${${project}_TABLEGEN} STREQUAL "${target}" )
Chris Bieneman7c445dd2015-05-29 18:34:41 +0000118 if (NOT CMAKE_CONFIGURATION_TYPES)
119 set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/bin/${target}")
120 else()
121 set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/Release/bin/${target}")
122 endif()
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000123 set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
124
Chris Bieneman76864f92016-09-06 20:27:07 +0000125 llvm_ExternalProject_BuildCmd(tblgen_build_cmd ${target}
126 ${LLVM_NATIVE_BUILD}
127 CONFIGURATION Release)
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000128 add_custom_command(OUTPUT ${${project}_TABLEGEN_EXE}
Chris Bieneman76864f92016-09-06 20:27:07 +0000129 COMMAND ${tblgen_build_cmd}
130 DEPENDS ${target} NATIVE_LIB_LLVMTABLEGEN
Chris Bieneman50077412014-09-03 23:21:18 +0000131 WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
Chris Bieneman76864f92016-09-06 20:27:07 +0000132 COMMENT "Building native TableGen..."
133 USES_TERMINAL)
Chris Bieneman3eddcae2015-04-24 17:09:20 +0000134 add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
135 set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000136 endif()
137 endif()
138
Hans Wennborgbac9d132013-09-10 18:35:14 +0000139 if (${project} STREQUAL LLVM AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
NAKAMURA Takumief976072014-02-09 16:36:03 +0000140 install(TARGETS ${target}
141 EXPORT LLVMExports
Chris Bieneman2724f142016-06-08 21:19:26 +0000142 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
Hans Wennborgbac9d132013-09-10 18:35:14 +0000143 endif()
NAKAMURA Takumi8faf6602014-02-09 16:36:16 +0000144 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
Peter Collingbournefb3d9352011-10-06 01:51:51 +0000145endmacro()