Skip to content

Commit 30b39e9

Browse files
authored
add two cmake options to make it easier to integrate avro c++ (#3268)
AVRO_BUILD_EXECUTABLES controls whether to build precompile and avrogencpp AVRO_BUILD_TESTS controls whether to build unittest The two options default to ON conforming the original behaviour. This is needed because for libraries like iceberg-cpp, we don't need to build the unittest and executable. Signed-off-by: Junwang Zhao <zhjwpku@gmail.com>
1 parent 6e4a568 commit 30b39e9

File tree

1 file changed

+89
-77
lines changed

1 file changed

+89
-77
lines changed

lang/c++/CMakeLists.txt

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ list(GET AVRO_VERSION 2 AVRO_VERSION_PATCH)
5555
project (Avro-cpp)
5656
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
5757

58+
option(AVRO_BUILD_EXECUTABLES "Build executables" ON)
59+
option(AVRO_BUILD_TESTS "Build tests" ON)
60+
5861
if (WIN32 AND NOT CYGWIN AND NOT MSYS)
5962
add_definitions (/EHa)
6063
add_definitions (
@@ -78,9 +81,12 @@ if (AVRO_ADD_PROTECTOR_FLAGS)
7881
endif ()
7982
endif ()
8083

81-
82-
find_package (Boost 1.38 REQUIRED
83-
COMPONENTS filesystem iostreams program_options regex system)
84+
if (AVRO_BUILD_TESTS OR AVRO_BUILD_EXECUTABLES)
85+
find_package (Boost 1.38 REQUIRED
86+
COMPONENTS filesystem iostreams program_options system)
87+
else ()
88+
find_package (Boost 1.38 REQUIRED COMPONENTS iostreams)
89+
endif ()
8490

8591
include(FetchContent)
8692
FetchContent_Declare(
@@ -149,86 +155,90 @@ set_target_properties (avrocpp_s PROPERTIES
149155
target_link_libraries (avrocpp ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES} fmt::fmt-header-only)
150156
target_include_directories(avrocpp PRIVATE ${SNAPPY_INCLUDE_DIR})
151157

152-
add_executable (precompile test/precompile.cc)
153-
154-
target_link_libraries (precompile avrocpp_s)
155-
156-
macro (gen file ns)
157-
add_custom_command (OUTPUT ${file}.hh
158-
COMMAND avrogencpp
159-
-p -
160-
-i ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file}
161-
-o ${file}.hh -n ${ns}
162-
DEPENDS avrogencpp ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file})
163-
add_custom_target (${file}_hh DEPENDS ${file}.hh)
164-
endmacro (gen)
165-
166-
gen (empty_record empty)
167-
gen (bigrecord testgen)
168-
gen (bigrecord_r testgen_r)
169-
gen (bigrecord2 testgen2)
170-
gen (tweet testgen3)
171-
gen (union_array_union uau)
172-
gen (union_map_union umu)
173-
gen (union_conflict uc)
174-
gen (union_empty_record uer)
175-
gen (recursive rec)
176-
gen (reuse ru)
177-
gen (circulardep cd)
178-
gen (tree1 tr1)
179-
gen (tree2 tr2)
180-
gen (crossref cr)
181-
gen (primitivetypes pt)
182-
gen (cpp_reserved_words cppres)
183-
gen (cpp_reserved_words_union_typedef cppres_union)
184-
gen (big_union big_union)
185-
gen (union_redundant_types redundant_types)
186-
187-
add_executable (avrogencpp impl/avrogencpp.cc)
188-
target_link_libraries (avrogencpp avrocpp_s)
189-
190-
target_include_directories(avrocpp_s PUBLIC
158+
target_include_directories(avrocpp PUBLIC
191159
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
192160
$<INSTALL_INTERFACE:include>
193161
)
194-
target_include_directories(avrocpp PUBLIC
162+
target_include_directories(avrocpp_s PUBLIC
195163
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
196164
$<INSTALL_INTERFACE:include>
197165
)
198166

199-
enable_testing()
200-
201-
macro (unittest name)
202-
add_executable (${name} test/${name}.cc)
203-
target_link_libraries (${name} avrocpp ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES})
204-
add_test (NAME ${name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
205-
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${name})
206-
endmacro (unittest)
207-
208-
unittest (buffertest)
209-
unittest (unittest)
210-
unittest (SchemaTests)
211-
unittest (LargeSchemaTests)
212-
unittest (CodecTests)
213-
unittest (StreamTests)
214-
unittest (SpecificTests)
215-
unittest (DataFileTests)
216-
unittest (JsonTests)
217-
unittest (AvrogencppTests)
218-
unittest (CompilerTests)
219-
unittest (AvrogencppTestReservedWords)
220-
unittest (CommonsSchemasTests)
221-
222-
add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh)
223-
add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh
224-
cpp_reserved_words_union_typedef_hh)
225-
226-
add_dependencies (AvrogencppTests bigrecord_hh bigrecord_r_hh bigrecord2_hh
227-
tweet_hh
228-
union_array_union_hh union_map_union_hh union_conflict_hh
229-
recursive_hh reuse_hh circulardep_hh tree1_hh tree2_hh crossref_hh
230-
primitivetypes_hh empty_record_hh cpp_reserved_words_union_typedef_hh
231-
union_empty_record_hh big_union_hh union_redundant_types_hh)
167+
if (AVRO_BUILD_EXECUTABLES)
168+
add_executable (precompile test/precompile.cc)
169+
170+
target_link_libraries (precompile avrocpp_s)
171+
172+
macro (gen file ns)
173+
add_custom_command (OUTPUT ${file}.hh
174+
COMMAND avrogencpp
175+
-p -
176+
-i ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file}
177+
-o ${file}.hh -n ${ns}
178+
DEPENDS avrogencpp ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file})
179+
add_custom_target (${file}_hh DEPENDS ${file}.hh)
180+
endmacro (gen)
181+
182+
gen (empty_record empty)
183+
gen (bigrecord testgen)
184+
gen (bigrecord_r testgen_r)
185+
gen (bigrecord2 testgen2)
186+
gen (tweet testgen3)
187+
gen (union_array_union uau)
188+
gen (union_map_union umu)
189+
gen (union_conflict uc)
190+
gen (union_empty_record uer)
191+
gen (recursive rec)
192+
gen (reuse ru)
193+
gen (circulardep cd)
194+
gen (tree1 tr1)
195+
gen (tree2 tr2)
196+
gen (crossref cr)
197+
gen (primitivetypes pt)
198+
gen (cpp_reserved_words cppres)
199+
gen (cpp_reserved_words_union_typedef cppres_union)
200+
gen (big_union big_union)
201+
gen (union_redundant_types redundant_types)
202+
203+
add_executable (avrogencpp impl/avrogencpp.cc)
204+
target_link_libraries (avrogencpp avrocpp_s ${Boost_LIBRARIES})
205+
endif ()
206+
207+
if (AVRO_BUILD_TESTS)
208+
enable_testing()
209+
210+
macro (unittest name)
211+
add_executable (${name} test/${name}.cc)
212+
target_link_libraries (${name} avrocpp_s ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES})
213+
add_test (NAME ${name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
214+
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${name})
215+
endmacro (unittest)
216+
217+
unittest (buffertest)
218+
unittest (unittest)
219+
unittest (SchemaTests)
220+
unittest (LargeSchemaTests)
221+
unittest (CodecTests)
222+
unittest (StreamTests)
223+
unittest (SpecificTests)
224+
unittest (DataFileTests)
225+
unittest (JsonTests)
226+
unittest (AvrogencppTests)
227+
unittest (CompilerTests)
228+
unittest (AvrogencppTestReservedWords)
229+
unittest (CommonsSchemasTests)
230+
231+
add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh)
232+
add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh
233+
cpp_reserved_words_union_typedef_hh)
234+
235+
add_dependencies (AvrogencppTests bigrecord_hh bigrecord_r_hh bigrecord2_hh
236+
tweet_hh
237+
union_array_union_hh union_map_union_hh union_conflict_hh
238+
recursive_hh reuse_hh circulardep_hh tree1_hh tree2_hh crossref_hh
239+
primitivetypes_hh empty_record_hh cpp_reserved_words_union_typedef_hh
240+
union_empty_record_hh big_union_hh union_redundant_types_hh)
241+
endif ()
232242

233243
include (InstallRequiredSystemLibraries)
234244

@@ -241,7 +251,9 @@ install (TARGETS avrocpp avrocpp_s
241251
ARCHIVE DESTINATION lib
242252
RUNTIME DESTINATION lib)
243253

244-
install (TARGETS avrogencpp RUNTIME DESTINATION bin)
254+
if (AVRO_BUILD_EXECUTABLES)
255+
install (TARGETS avrogencpp RUNTIME DESTINATION bin)
256+
endif ()
245257

246258
install (DIRECTORY include/avro DESTINATION include
247259
FILES_MATCHING PATTERN *.hh)

0 commit comments

Comments
 (0)