blob: 77c15244a55c99a5fd582d52f27b2bf5609d5df7 [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
197# Compile-check generated C++ output for the fixture thrift file with private_optional enabled.
198# This ensures the generator output is compileable (no link step required).
199if(TARGET thrift-compiler)
200 # Generated C++ includes Thrift runtime headers which may require Boost.
201 # Only enable the compile-check when Boost headers are available.
202 set(_private_optional_boost_include_dirs "")
203 find_package(Boost QUIET)
204 if(Boost_FOUND)
205 set(_private_optional_boost_include_dirs ${Boost_INCLUDE_DIRS})
206 elseif(DEFINED BOOST_ROOT)
207 if(EXISTS "${BOOST_ROOT}/include/boost")
208 set(_private_optional_boost_include_dirs "${BOOST_ROOT}/include")
209 elseif(EXISTS "${BOOST_ROOT}/boost")
210 set(_private_optional_boost_include_dirs "${BOOST_ROOT}")
211 endif()
212 endif()
213
214 if(_private_optional_boost_include_dirs STREQUAL "")
215 message(STATUS "Skipping generated private_optional compile-check (Boost headers not found)")
216 else()
217 set(_private_optional_thrift
218 "${CMAKE_CURRENT_SOURCE_DIR}/cpp/test_private_optional.thrift"
219 )
220 set(_private_optional_gen_out_dir
221 "${CMAKE_CURRENT_BINARY_DIR}/generated-private-optional"
222 )
223 set(_private_optional_gen_cpp_dir
224 "${_private_optional_gen_out_dir}/gen-cpp"
225 )
226 set(_private_optional_types_cpp
227 "${_private_optional_gen_cpp_dir}/test_private_optional_types.cpp"
228 )
229
230 add_custom_command(
231 OUTPUT "${_private_optional_types_cpp}"
232 COMMAND ${CMAKE_COMMAND} -E make_directory "${_private_optional_gen_out_dir}"
233 COMMAND ${CMAKE_COMMAND} -E chdir "${_private_optional_gen_out_dir}"
234 $<TARGET_FILE:thrift-compiler>
235 --gen cpp:private_optional
236 -o "${_private_optional_gen_out_dir}"
237 "${_private_optional_thrift}"
238 DEPENDS thrift-compiler "${_private_optional_thrift}"
239 VERBATIM
240 )
241
242 set_source_files_properties(
243 "${_private_optional_types_cpp}"
244 PROPERTIES GENERATED TRUE
245 )
246
247 add_library(thrift_compiler_generated_private_optional STATIC
248 "${_private_optional_types_cpp}"
249 )
250
251 target_include_directories(thrift_compiler_generated_private_optional PRIVATE
252 "${_private_optional_gen_cpp_dir}"
253 "${THRIFT_COMPILER_SOURCE_DIR}/../../lib/cpp/src"
254 "${CMAKE_CURRENT_BINARY_DIR}"
255 "${CMAKE_BINARY_DIR}"
256 ${_private_optional_boost_include_dirs}
257 )
258
259 # Build the compile-check as part of the standard test build.
260 add_dependencies(thrift_compiler_tests thrift_compiler_generated_private_optional)
261 endif()
262else()
263 message(STATUS "Skipping generated private_optional compile-check (no thrift-compiler target)")
264endif()
Volodymyr Gotra54993292017-12-18 02:08:09 +0200265
266enable_testing()
copilot-swe-agent[bot]c3ced622025-11-18 18:09:06 +0000267add_test(NAME ThriftCompilerTests COMMAND thrift_compiler_tests)