blob: 468de6ee846355004d5f32845908e22ad439e546 [file] [log] [blame]
Volodymyr Gotra54993292017-12-18 02:08:09 +02001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
Vyas Ramasubramanib3fc4b22025-04-20 13:57:06 -070019cmake_minimum_required(VERSION 3.16)
Volodymyr Gotra54993292017-12-18 02:08:09 +020020
21project(thrift_compiler_tests)
22
23set(THRIFT_COMPILER_SOURCE_DIR
24 ${CMAKE_CURRENT_SOURCE_DIR}/..
25)
26
27# don't generate ZERO_CHECK
28set(CMAKE_SUPPRESS_REGENERATION true)
29
Jens Geyer71997412019-10-19 21:22:59 +020030# version.h now handled via veralign.sh
31#configure_file(${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/thrift/version.h)
Volodymyr Gotra54993292017-12-18 02:08:09 +020032if(MSVC)
33 # The winflexbison generator outputs some macros that conflict with the Visual Studio 2010 copy of stdint.h
34 # This might be fixed in later versions of Visual Studio, but an easy solution is to include stdint.h first
35 if(HAVE_STDINT_H)
36 add_definitions(-D__STDC_LIMIT_MACROS)
37 add_definitions(/FI"stdint.h")
38 endif(HAVE_STDINT_H)
39endif()
40
41find_package(FLEX REQUIRED)
42find_package(BISON REQUIRED)
43
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000044if(NOT TARGET parse)
45 # create directory for thrifty and thriftl
46 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/thrift/)
Volodymyr Gotra54993292017-12-18 02:08:09 +020047
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000048 # Create flex and bison files and build the lib parse static library
49 BISON_TARGET(thrifty ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/thrifty.yy ${CMAKE_CURRENT_BINARY_DIR}/thrift/thrifty.cc COMPILE_FLAGS "--file-prefix-map=${CMAKE_BINARY_DIR}='' --file-prefix-map=${CMAKE_SOURCE_DIR}=''")
50 FLEX_TARGET(thriftl ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/thriftl.ll ${CMAKE_CURRENT_BINARY_DIR}/thrift/thriftl.cc)
51 ADD_FLEX_BISON_DEPENDENCY(thriftl thrifty)
Volodymyr Gotra54993292017-12-18 02:08:09 +020052
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000053 set(parse_SOURCES
54 ${CMAKE_CURRENT_BINARY_DIR}/thrift/thrifty.cc
55 ${CMAKE_CURRENT_BINARY_DIR}/thrift/thriftl.cc
56 ${CMAKE_CURRENT_BINARY_DIR}/thrift/thrifty.hh
57 )
Volodymyr Gotra54993292017-12-18 02:08:09 +020058
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000059 add_library(parse STATIC ${parse_SOURCES})
60endif()
Volodymyr Gotra54993292017-12-18 02:08:09 +020061
62# Thrift compiler tests
63set(thrift_compiler_tests
64)
65
66# you can add some files manually there
67set(thrift_compiler_tests_manual_SOURCES
68 # tests file to avoid main in every test file
69 ${CMAKE_CURRENT_SOURCE_DIR}/tests_main.cc
70)
71
72# set variable for tests sources - will be filled later
73set(thrift_compiler_tests_SOURCES
74)
75
76set(thrift_compiler_SOURCES
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000077 ${CMAKE_CURRENT_SOURCE_DIR}/thrift_test_globals.cc
78 ${CMAKE_CURRENT_SOURCE_DIR}/thrift_test_parser_support.cc
Volodymyr Gotra54993292017-12-18 02:08:09 +020079 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/logging.cc # we use logging instead of main to avoid breaking compillation (2 main v)
80 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/audit/t_audit.cpp
81 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/common.cc
82 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/t_generator.cc
Simon Wangd5927a92021-09-13 19:50:45 +080083 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/validator_parser.cc
84 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/validator_parser.h
Volodymyr Gotra54993292017-12-18 02:08:09 +020085 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/parse/t_typedef.cc
86 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/parse/parse.cc
Yawar Aminb17672a2022-09-10 11:55:02 -040087 ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/version.h
Volodymyr Gotra54993292017-12-18 02:08:09 +020088)
89
90# This macro adds an option THRIFT_COMPILER_${NAME}
91# that allows enabling or disabling certain languages
92macro(THRIFT_ADD_COMPILER name description initial)
93 string(TOUPPER "THRIFT_COMPILER_${name}" enabler)
94 set(src "${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/t_${name}_generator.cc")
95 option(${enabler} ${description} ${initial})
96 if(${enabler})
97 list(APPEND thrift_compiler_SOURCES ${src})
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +000098 file(GLOB temp_test_sources
Volodymyr Gotra54993292017-12-18 02:08:09 +020099 "${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.c*"
100 "${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.thrift"
101 )
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000102 list(APPEND thrift_compiler_tests_SOURCES ${temp_test_sources})
Volodymyr Gotra54993292017-12-18 02:08:09 +0200103 endif()
104endmacro()
105
Simon Wangd5927a92021-09-13 19:50:45 +0800106# This macro adds an option THRIFT_VALIDATOR_COMPILER_${NAME}
107# that allows enabling or disabling certain languages
108macro(THRIFT_ADD_VALIDATOR_COMPILER name description initial)
109 string(TOUPPER "THRIFT_VALIDATOR_COMPILER_${name}" enabler)
110 set(src "${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/${name}_validator_generator.cc")
111 list(APPEND "${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/${name}_validator_generator.h")
112 option(${enabler} ${description} ${initial})
113 if(${enabler})
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000114 list(APPEND thrift_compiler_SOURCES ${src})
Simon Wangd5927a92021-09-13 19:50:45 +0800115 endif()
116endmacro()
117
Volodymyr Gotra54993292017-12-18 02:08:09 +0200118# The following compiler with unit tests can be enabled or disabled
James E. King III234fb472019-01-13 23:19:18 -0500119THRIFT_ADD_COMPILER(c_glib "Enable compiler for C with Glib" OFF)
Max-Gerd Retzlaff04057ac2022-08-23 17:38:34 +0200120THRIFT_ADD_COMPILER(cl "Enable compiler for Common LISP" OFF)
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000121THRIFT_ADD_COMPILER(cpp "Enable compiler for C++" ON)
James E. King III234fb472019-01-13 23:19:18 -0500122THRIFT_ADD_COMPILER(d "Enable compiler for D" OFF)
123THRIFT_ADD_COMPILER(dart "Enable compiler for Dart" OFF)
124THRIFT_ADD_COMPILER(delphi "Enable compiler for Delphi" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200125THRIFT_ADD_COMPILER(erl "Enable compiler for Erlang" OFF)
James E. King III234fb472019-01-13 23:19:18 -0500126THRIFT_ADD_COMPILER(go "Enable compiler for Go" OFF)
127THRIFT_ADD_COMPILER(gv "Enable compiler for GraphViz" OFF)
128THRIFT_ADD_COMPILER(haxe "Enable compiler for Haxe" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200129THRIFT_ADD_COMPILER(html "Enable compiler for HTML Documentation" OFF)
James E. King III234fb472019-01-13 23:19:18 -0500130THRIFT_ADD_COMPILER(java "Enable compiler for Java" OFF)
131THRIFT_ADD_COMPILER(javame "Enable compiler for Java ME" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200132THRIFT_ADD_COMPILER(js "Enable compiler for JavaScript" OFF)
133THRIFT_ADD_COMPILER(json "Enable compiler for JSON" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200134THRIFT_ADD_COMPILER(lua "Enable compiler for Lua" OFF)
Jens Geyer3f3567a2019-10-19 18:27:35 +0200135THRIFT_ADD_COMPILER(netstd "Enable compiler for .NET Standard" ON)
Yawar Aminb17672a2022-09-10 11:55:02 -0400136THRIFT_ADD_COMPILER(ocaml "Enable compiler for OCaml" ON)
James E. King III234fb472019-01-13 23:19:18 -0500137THRIFT_ADD_COMPILER(perl "Enable compiler for Perl" OFF)
138THRIFT_ADD_COMPILER(php "Enable compiler for PHP" OFF)
139THRIFT_ADD_COMPILER(py "Enable compiler for Python 2.0" OFF)
140THRIFT_ADD_COMPILER(rb "Enable compiler for Ruby" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200141THRIFT_ADD_COMPILER(rs "Enable compiler for Rust" OFF)
James E. King III234fb472019-01-13 23:19:18 -0500142THRIFT_ADD_COMPILER(st "Enable compiler for Smalltalk" OFF)
143THRIFT_ADD_COMPILER(swift "Enable compiler for Swift" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200144THRIFT_ADD_COMPILER(xml "Enable compiler for XML" OFF)
James E. King III234fb472019-01-13 23:19:18 -0500145THRIFT_ADD_COMPILER(xsd "Enable compiler for XSD" OFF)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200146
Simon Wangd5927a92021-09-13 19:50:45 +0800147# The following compiler can be enabled or disabled by enabling or disabling certain languages
148THRIFT_ADD_VALIDATOR_COMPILER(go "Enable validator compiler for Go" ON)
149
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000150# OCaml tests include the implementation .cc directly, so compiling it into the
151# thrift_compiler lib would cause duplicate definitions (LNK2005).
152list(REMOVE_ITEM thrift_compiler_SOURCES
153 "${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/t_ocaml_generator.cc"
154)
Volodymyr Gotra54993292017-12-18 02:08:09 +0200155
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000156# Thrift is looking for include files in the src directory
157# We also add:
158# - the current binary directory for locally generated files (standalone tests build)
159# - the top-level binary directory for generated files when reusing targets from the parent build
160include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR} ${THRIFT_COMPILER_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/catch)
161
162add_library(thrift_compiler STATIC ${thrift_compiler_SOURCES})
Volodymyr Gotra54993292017-12-18 02:08:09 +0200163
164#link parse lib to thrift_compiler lib
165target_link_libraries(thrift_compiler parse)
166
167# add tests executable
168add_executable(thrift_compiler_tests ${thrift_compiler_tests_manual_SOURCES} ${thrift_compiler_tests_SOURCES})
169
170# if generates for Visual Studio set thrift_compiler_tests as default project
171if(MSVC)
172 set_property(TARGET thrift_compiler_tests PROPERTY VS_STARTUP_PROJECT thrift_compiler_tests)
173endif()
174
175set_target_properties(thrift_compiler_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY bin/)
176set_target_properties(thrift_compiler_tests PROPERTIES OUTPUT_NAME thrift_compiler_tests)
177
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000178# Ensure generator registration translation units are linked in.
179# Many generators register themselves via static initialization; linkers may
180# otherwise discard those objects from static libraries.
181if(MSVC)
182 target_link_libraries(thrift_compiler_tests PRIVATE thrift_compiler)
183 target_link_options(thrift_compiler_tests PRIVATE
184 "/WHOLEARCHIVE:$<TARGET_LINKER_FILE:thrift_compiler>"
185 )
186elseif(APPLE)
187 target_link_libraries(thrift_compiler_tests PRIVATE thrift_compiler)
188 target_link_options(thrift_compiler_tests PRIVATE
189 "-Wl,-force_load,$<TARGET_LINKER_FILE:thrift_compiler>"
190 )
191else()
192 target_link_libraries(thrift_compiler_tests PRIVATE
193 "-Wl,--whole-archive" thrift_compiler "-Wl,--no-whole-archive"
194 )
195endif()
196
Copilotab178fb2026-01-27 09:46:59 -0800197# Compile-check generated C++ output for fixture thrift files.
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000198# This ensures the generator output is compileable (no link step required).
Copilotab178fb2026-01-27 09:46:59 -0800199# Note: These checks require Boost headers and are optional.
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000200if(TARGET thrift-compiler)
Copilotab178fb2026-01-27 09:46:59 -0800201 # Try to find Boost for the compile checks
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000202 find_package(Boost QUIET)
Copilotab178fb2026-01-27 09:46:59 -0800203 set(_compile_check_boost_include_dirs "")
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000204 if(Boost_FOUND)
Copilotab178fb2026-01-27 09:46:59 -0800205 set(_compile_check_boost_include_dirs ${Boost_INCLUDE_DIRS})
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000206 elseif(DEFINED BOOST_ROOT)
207 if(EXISTS "${BOOST_ROOT}/include/boost")
Copilotab178fb2026-01-27 09:46:59 -0800208 set(_compile_check_boost_include_dirs "${BOOST_ROOT}/include")
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000209 elseif(EXISTS "${BOOST_ROOT}/boost")
Copilotab178fb2026-01-27 09:46:59 -0800210 set(_compile_check_boost_include_dirs "${BOOST_ROOT}")
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000211 endif()
212 endif()
213
Copilotab178fb2026-01-27 09:46:59 -0800214 # Only create compile-check targets if Boost is available
215 if(NOT _compile_check_boost_include_dirs STREQUAL "")
216 set(_private_optional_thrift
217 "${CMAKE_CURRENT_SOURCE_DIR}/cpp/test_private_optional.thrift"
218 )
219 set(_private_optional_gen_out_dir
220 "${CMAKE_CURRENT_BINARY_DIR}/generated-private-optional"
221 )
222 set(_private_optional_gen_cpp_dir
223 "${_private_optional_gen_out_dir}/gen-cpp"
224 )
225 set(_private_optional_types_cpp
226 "${_private_optional_gen_cpp_dir}/test_private_optional_types.cpp"
227 )
228
229 add_custom_command(
230 OUTPUT "${_private_optional_types_cpp}"
231 COMMAND ${CMAKE_COMMAND} -E make_directory "${_private_optional_gen_out_dir}"
232 COMMAND ${CMAKE_COMMAND} -E chdir "${_private_optional_gen_out_dir}"
233 $<TARGET_FILE:thrift-compiler>
234 --gen cpp:private_optional
235 -o "${_private_optional_gen_out_dir}"
236 "${_private_optional_thrift}"
237 DEPENDS thrift-compiler "${_private_optional_thrift}"
238 VERBATIM
239 )
240
241 set_source_files_properties(
242 "${_private_optional_types_cpp}"
243 PROPERTIES GENERATED TRUE
244 )
245
246 add_library(thrift_compiler_generated_private_optional STATIC
247 "${_private_optional_types_cpp}"
248 )
249
250 target_include_directories(thrift_compiler_generated_private_optional PRIVATE
251 "${_private_optional_gen_cpp_dir}"
252 "${THRIFT_COMPILER_SOURCE_DIR}/../../lib/cpp/src"
253 "${CMAKE_CURRENT_BINARY_DIR}"
254 "${CMAKE_BINARY_DIR}"
255 ${_compile_check_boost_include_dirs}
256 )
257
258 set(_enum_class_thrift
259 "${CMAKE_CURRENT_SOURCE_DIR}/cpp/test_enum_class.thrift"
260 )
261 set(_enum_class_gen_out_dir
262 "${CMAKE_CURRENT_BINARY_DIR}/generated-enum-class"
263 )
264 set(_enum_class_gen_cpp_dir
265 "${_enum_class_gen_out_dir}/gen-cpp"
266 )
267 set(_enum_class_types_cpp
268 "${_enum_class_gen_cpp_dir}/test_enum_class_types.cpp"
269 )
270
271 add_custom_command(
272 OUTPUT "${_enum_class_types_cpp}"
273 COMMAND ${CMAKE_COMMAND} -E make_directory "${_enum_class_gen_out_dir}"
274 COMMAND ${CMAKE_COMMAND} -E chdir "${_enum_class_gen_out_dir}"
275 $<TARGET_FILE:thrift-compiler>
276 --gen cpp:pure_enums=enum_class
277 -o "${_enum_class_gen_out_dir}"
278 "${_enum_class_thrift}"
279 DEPENDS thrift-compiler "${_enum_class_thrift}"
280 VERBATIM
281 )
282
283 set_source_files_properties(
284 "${_enum_class_types_cpp}"
285 PROPERTIES GENERATED TRUE
286 )
287
288 add_library(thrift_compiler_generated_enum_class STATIC
289 "${_enum_class_types_cpp}"
290 )
291
292 target_include_directories(thrift_compiler_generated_enum_class PRIVATE
293 "${_enum_class_gen_cpp_dir}"
294 "${THRIFT_COMPILER_SOURCE_DIR}/../../lib/cpp/src"
295 "${CMAKE_CURRENT_BINARY_DIR}"
296 "${CMAKE_BINARY_DIR}"
297 ${_compile_check_boost_include_dirs}
298 )
299
300 # Build the compile-check as part of the standard test build.
301 add_dependencies(thrift_compiler_tests thrift_compiler_generated_private_optional)
302 add_dependencies(thrift_compiler_tests thrift_compiler_generated_enum_class)
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000303 else()
Copilotab178fb2026-01-27 09:46:59 -0800304 message(STATUS "Skipping generated code compile-checks (Boost headers not found)")
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000305 endif()
306else()
Copilotab178fb2026-01-27 09:46:59 -0800307 message(STATUS "Skipping generated code compile-checks (no thrift-compiler target)")
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000308endif()
Volodymyr Gotra54993292017-12-18 02:08:09 +0200309
310enable_testing()
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000311add_test(NAME ThriftCompilerTests COMMAND thrift_compiler_tests)