| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 1 | # |
| 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 Ramasubramani | b3fc4b2 | 2025-04-20 13:57:06 -0700 | [diff] [blame] | 19 | cmake_minimum_required(VERSION 3.16) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 20 | |
| 21 | project(thrift_compiler_tests) |
| 22 | |
| 23 | set(THRIFT_COMPILER_SOURCE_DIR |
| 24 | ${CMAKE_CURRENT_SOURCE_DIR}/.. |
| 25 | ) |
| 26 | |
| 27 | # don't generate ZERO_CHECK |
| 28 | set(CMAKE_SUPPRESS_REGENERATION true) |
| 29 | |
| Jens Geyer | 7199741 | 2019-10-19 21:22:59 +0200 | [diff] [blame] | 30 | # 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 Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 32 | if(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) |
| 39 | endif() |
| 40 | |
| 41 | find_package(FLEX REQUIRED) |
| 42 | find_package(BISON REQUIRED) |
| 43 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 44 | if(NOT TARGET parse) |
| 45 | # create directory for thrifty and thriftl |
| 46 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/thrift/) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 47 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 48 | # 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 Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 52 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 53 | 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 Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 58 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 59 | add_library(parse STATIC ${parse_SOURCES}) |
| 60 | endif() |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 61 | |
| 62 | # Thrift compiler tests |
| 63 | set(thrift_compiler_tests |
| 64 | ) |
| 65 | |
| 66 | # you can add some files manually there |
| 67 | set(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 |
| 73 | set(thrift_compiler_tests_SOURCES |
| 74 | ) |
| 75 | |
| 76 | set(thrift_compiler_SOURCES |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 77 | ${CMAKE_CURRENT_SOURCE_DIR}/thrift_test_globals.cc |
| 78 | ${CMAKE_CURRENT_SOURCE_DIR}/thrift_test_parser_support.cc |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 79 | ${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 Wang | d5927a9 | 2021-09-13 19:50:45 +0800 | [diff] [blame] | 83 | ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/validator_parser.cc |
| 84 | ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/validator_parser.h |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 85 | ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/parse/t_typedef.cc |
| 86 | ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/parse/parse.cc |
| Yawar Amin | b17672a | 2022-09-10 11:55:02 -0400 | [diff] [blame] | 87 | ${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/version.h |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 88 | ) |
| 89 | |
| 90 | # This macro adds an option THRIFT_COMPILER_${NAME} |
| 91 | # that allows enabling or disabling certain languages |
| 92 | macro(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] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 98 | file(GLOB temp_test_sources |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 99 | "${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.c*" |
| 100 | "${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.thrift" |
| 101 | ) |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 102 | list(APPEND thrift_compiler_tests_SOURCES ${temp_test_sources}) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 103 | endif() |
| 104 | endmacro() |
| 105 | |
| Simon Wang | d5927a9 | 2021-09-13 19:50:45 +0800 | [diff] [blame] | 106 | # This macro adds an option THRIFT_VALIDATOR_COMPILER_${NAME} |
| 107 | # that allows enabling or disabling certain languages |
| 108 | macro(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] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 114 | list(APPEND thrift_compiler_SOURCES ${src}) |
| Simon Wang | d5927a9 | 2021-09-13 19:50:45 +0800 | [diff] [blame] | 115 | endif() |
| 116 | endmacro() |
| 117 | |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 118 | # The following compiler with unit tests can be enabled or disabled |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 119 | THRIFT_ADD_COMPILER(c_glib "Enable compiler for C with Glib" OFF) |
| Max-Gerd Retzlaff | 04057ac | 2022-08-23 17:38:34 +0200 | [diff] [blame] | 120 | THRIFT_ADD_COMPILER(cl "Enable compiler for Common LISP" OFF) |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 121 | THRIFT_ADD_COMPILER(cpp "Enable compiler for C++" ON) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 122 | THRIFT_ADD_COMPILER(d "Enable compiler for D" OFF) |
| 123 | THRIFT_ADD_COMPILER(dart "Enable compiler for Dart" OFF) |
| 124 | THRIFT_ADD_COMPILER(delphi "Enable compiler for Delphi" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 125 | THRIFT_ADD_COMPILER(erl "Enable compiler for Erlang" OFF) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 126 | THRIFT_ADD_COMPILER(go "Enable compiler for Go" OFF) |
| 127 | THRIFT_ADD_COMPILER(gv "Enable compiler for GraphViz" OFF) |
| 128 | THRIFT_ADD_COMPILER(haxe "Enable compiler for Haxe" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 129 | THRIFT_ADD_COMPILER(html "Enable compiler for HTML Documentation" OFF) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 130 | THRIFT_ADD_COMPILER(java "Enable compiler for Java" OFF) |
| 131 | THRIFT_ADD_COMPILER(javame "Enable compiler for Java ME" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 132 | THRIFT_ADD_COMPILER(js "Enable compiler for JavaScript" OFF) |
| 133 | THRIFT_ADD_COMPILER(json "Enable compiler for JSON" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 134 | THRIFT_ADD_COMPILER(lua "Enable compiler for Lua" OFF) |
| Jens Geyer | 3f3567a | 2019-10-19 18:27:35 +0200 | [diff] [blame] | 135 | THRIFT_ADD_COMPILER(netstd "Enable compiler for .NET Standard" ON) |
| Yawar Amin | b17672a | 2022-09-10 11:55:02 -0400 | [diff] [blame] | 136 | THRIFT_ADD_COMPILER(ocaml "Enable compiler for OCaml" ON) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 137 | THRIFT_ADD_COMPILER(perl "Enable compiler for Perl" OFF) |
| 138 | THRIFT_ADD_COMPILER(php "Enable compiler for PHP" OFF) |
| 139 | THRIFT_ADD_COMPILER(py "Enable compiler for Python 2.0" OFF) |
| 140 | THRIFT_ADD_COMPILER(rb "Enable compiler for Ruby" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 141 | THRIFT_ADD_COMPILER(rs "Enable compiler for Rust" OFF) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 142 | THRIFT_ADD_COMPILER(st "Enable compiler for Smalltalk" OFF) |
| 143 | THRIFT_ADD_COMPILER(swift "Enable compiler for Swift" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 144 | THRIFT_ADD_COMPILER(xml "Enable compiler for XML" OFF) |
| James E. King III | 234fb47 | 2019-01-13 23:19:18 -0500 | [diff] [blame] | 145 | THRIFT_ADD_COMPILER(xsd "Enable compiler for XSD" OFF) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 146 | |
| Simon Wang | d5927a9 | 2021-09-13 19:50:45 +0800 | [diff] [blame] | 147 | # The following compiler can be enabled or disabled by enabling or disabling certain languages |
| 148 | THRIFT_ADD_VALIDATOR_COMPILER(go "Enable validator compiler for Go" ON) |
| 149 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 150 | # OCaml tests include the implementation .cc directly, so compiling it into the |
| 151 | # thrift_compiler lib would cause duplicate definitions (LNK2005). |
| 152 | list(REMOVE_ITEM thrift_compiler_SOURCES |
| 153 | "${THRIFT_COMPILER_SOURCE_DIR}/src/thrift/generate/t_ocaml_generator.cc" |
| 154 | ) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 155 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 156 | # 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 |
| 160 | include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR} ${THRIFT_COMPILER_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/catch) |
| 161 | |
| 162 | add_library(thrift_compiler STATIC ${thrift_compiler_SOURCES}) |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 163 | |
| 164 | #link parse lib to thrift_compiler lib |
| 165 | target_link_libraries(thrift_compiler parse) |
| 166 | |
| 167 | # add tests executable |
| 168 | add_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 |
| 171 | if(MSVC) |
| 172 | set_property(TARGET thrift_compiler_tests PROPERTY VS_STARTUP_PROJECT thrift_compiler_tests) |
| 173 | endif() |
| 174 | |
| 175 | set_target_properties(thrift_compiler_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY bin/) |
| 176 | set_target_properties(thrift_compiler_tests PROPERTIES OUTPUT_NAME thrift_compiler_tests) |
| 177 | |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 178 | # 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. |
| 181 | if(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 | ) |
| 186 | elseif(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 | ) |
| 191 | else() |
| 192 | target_link_libraries(thrift_compiler_tests PRIVATE |
| 193 | "-Wl,--whole-archive" thrift_compiler "-Wl,--no-whole-archive" |
| 194 | ) |
| 195 | endif() |
| 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). |
| 199 | if(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() |
| 262 | else() |
| 263 | message(STATUS "Skipping generated private_optional compile-check (no thrift-compiler target)") |
| 264 | endif() |
| Volodymyr Gotra | 5499329 | 2017-12-18 02:08:09 +0200 | [diff] [blame] | 265 | |
| 266 | enable_testing() |
| copilot-swe-agent[bot] | c3ced62 | 2025-11-18 18:09:06 +0000 | [diff] [blame^] | 267 | add_test(NAME ThriftCompilerTests COMMAND thrift_compiler_tests) |