@@ -28,87 +28,50 @@ if(CHECK_TYPE)
2828 cmake_pop_check_state ()
2929endif ()
3030
31- include (GNUInstallDirs )
32-
33- ################
34- ## C++ Test ##
35- ################
36- # add_cxx_test()
37- # CMake function to generate and build C++ test.
31+ ##################
32+ ## PROTO FILE ##
33+ ##################
34+ # get_cpp_proto()
35+ # CMake macro to generate Protobuf cpp sources
3836# Parameters:
39- # NAME: CMake target name
40- # SOURCES: List of source files
41- # [COMPILE_DEFINITIONS]: List of private compile definitions
42- # [COMPILE_OPTIONS]: List of private compile options
43- # [LINK_LIBRARIES]: List of private libraries to use when linking
44- # note: ortools::ortools is always linked to the target
45- # [LINK_OPTIONS]: List of private link options
37+ # the proto c++ headers list
38+ # the proto c++ sources list
4639# e.g.:
47- # add_cxx_test(
48- # NAME
49- # foo_test
50- # SOURCES
51- # foo_test.cc
52- # ${PROJECT_SOURCE_DIR}/Foo/foo_test.cc
53- # LINK_LIBRARIES
54- # GTest::gmock
55- # GTest::gtest_main
56- # )
57- function (add_cxx_test )
58- set (options "" )
59- set (oneValueArgs "NAME" )
60- set (multiValueArgs
61- "SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS" )
62- cmake_parse_arguments (TEST
63- "${options} "
64- "${oneValueArgs} "
65- "${multiValueArgs} "
66- ${ARGN}
67- )
68- if (NOT BUILD_TESTING)
69- return ()
70- endif ()
71-
72- if (NOT TEST_NAME)
73- message (FATAL_ERROR "no NAME provided" )
74- endif ()
75- if (NOT TEST_SOURCES)
76- message (FATAL_ERROR "no SOURCES provided" )
77- endif ()
78- message (STATUS "Configuring test ${TEST_NAME} ..." )
79-
80- add_executable (${TEST_NAME} "" )
81- target_sources (${TEST_NAME} PRIVATE ${TEST_SOURCES} )
82- target_include_directories (${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
83- target_compile_definitions (${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS} )
84- target_compile_features (${TEST_NAME} PRIVATE cxx_std_20 )
85- target_compile_options (${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS} )
86- target_link_libraries (${TEST_NAME} PRIVATE
87- GTest::gtest
88- GTest::gtest_main
89- ${TEST_LINK_LIBRARIES}
90- )
91- target_link_options (${TEST_NAME} PRIVATE ${TEST_LINK_OPTIONS} )
92-
93- include (GNUInstallDirs )
94- if (APPLE )
95- set_target_properties (${TEST_NAME} PROPERTIES
96- INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR} ;@loader_path" )
97- elseif (UNIX )
98- cmake_path (RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
99- BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
100- OUTPUT_VARIABLE libdir_relative_path )
101- set_target_properties (${TEST_NAME} PROPERTIES
102- INSTALL_RPATH "$ORIGIN/${libdir_relative_path} :$ORIGIN" )
103- endif ()
40+ # get_cpp_proto(PROTO_HDRS PROTO_SRCS)
41+ macro (get_cpp_proto PROTO_HDRS PROTO_SRCS )
42+ file (GLOB_RECURSE PROTO_FILES RELATIVE ${PROJECT_SOURCE_DIR} "*.proto" )
43+ ## Get Protobuf include dir
44+ get_target_property (protobuf_dirs protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES )
45+ foreach (dir IN LISTS protobuf_dirs)
46+ if (NOT "${dir} " MATCHES "INSTALL_INTERFACE|-NOTFOUND" )
47+ message (STATUS "protoc(cc) Adding proto path: ${dir} " )
48+ list (APPEND PROTO_DIRS "--proto_path=${dir} " )
49+ endif ()
50+ endforeach ()
10451
105- add_test (
106- NAME cxx_${TEST_NAME}
107- COMMAND ${TEST_NAME}
108- WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
109- )
110- message (STATUS "Configuring test ${TEST_NAME} ...DONE" )
111- endfunction ()
52+ foreach (PROTO_FILE IN LISTS PROTO_FILES)
53+ message (STATUS "protoc(cc) .proto: ${PROTO_FILE} " )
54+ get_filename_component (PROTO_DIR ${PROTO_FILE} DIRECTORY )
55+ get_filename_component (PROTO_NAME ${PROTO_FILE} NAME_WE )
56+ set (PROTO_HDR ${PROJECT_BINARY_DIR} /${PROTO_DIR} /${PROTO_NAME} .pb.h)
57+ set (PROTO_SRC ${PROJECT_BINARY_DIR} /${PROTO_DIR} /${PROTO_NAME} .pb.cc)
58+ message (STATUS "protoc(cc) hdr: ${PROTO_HDR} " )
59+ message (STATUS "protoc(cc) src: ${PROTO_SRC} " )
60+ add_custom_command (
61+ OUTPUT ${PROTO_SRC} ${PROTO_HDR}
62+ COMMAND ${PROTOC_PRG}
63+ "--proto_path=${PROJECT_SOURCE_DIR} "
64+ ${PROTO_DIRS}
65+ "--cpp_out=dllexport_decl=PROTO_DLL:${PROJECT_BINARY_DIR} "
66+ ${PROTO_FILE}
67+ DEPENDS ${PROTO_NAME} .proto ${PROTOC_PRG}
68+ COMMENT "Generate C++ protocol buffer for ${PROTO_FILE} "
69+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
70+ VERBATIM )
71+ list (APPEND ${PROTO_HDRS} ${PROTO_HDR} )
72+ list (APPEND ${PROTO_SRCS} ${PROTO_SRC} )
73+ endforeach ()
74+ endmacro ()
11275
11376###################
11477## C++ Library ##
@@ -117,6 +80,7 @@ endfunction()
11780# CMake function to generate and build C++ library.
11881# Parameters:
11982# NAME: CMake target name
83+ # [HEADERS]: List of headers files
12084# SOURCES: List of source files
12185# [TYPE]: SHARED, STATIC or INTERFACE
12286# [COMPILE_DEFINITIONS]: List of private compile definitions
@@ -209,50 +173,85 @@ function(add_cxx_library)
209173 message (STATUS "Configuring library ${LIBRARY_NAME} ...DONE" )
210174endfunction ()
211175
212- ##################
213- ## PROTO FILE ##
214- ##################
215- # get_cpp_proto ()
216- # CMake macro to generate Protobuf cpp sources
176+ ################
177+ ## C++ Test ##
178+ ################
179+ # add_cxx_test ()
180+ # CMake function to generate and build C++ test.
217181# Parameters:
218- # the proto c++ headers list
219- # the proto c++ sources list
182+ # NAME: CMake target name
183+ # SOURCES: List of source files
184+ # [COMPILE_DEFINITIONS]: List of private compile definitions
185+ # [COMPILE_OPTIONS]: List of private compile options
186+ # [LINK_LIBRARIES]: List of private libraries to use when linking
187+ # note: ortools::ortools is always linked to the target
188+ # [LINK_OPTIONS]: List of private link options
220189# e.g.:
221- # get_cpp_proto(PROTO_HDRS PROTO_SRCS)
222- macro (get_cpp_proto PROTO_HDRS PROTO_SRCS )
223- file (GLOB_RECURSE PROTO_FILES RELATIVE ${PROJECT_SOURCE_DIR} "*.proto" )
224- ## Get Protobuf include dir
225- get_target_property (protobuf_dirs protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES )
226- foreach (dir IN LISTS protobuf_dirs)
227- if (NOT "${dir} " MATCHES "INSTALL_INTERFACE|-NOTFOUND" )
228- message (STATUS "protoc(cc) Adding proto path: ${dir} " )
229- list (APPEND PROTO_DIRS "--proto_path=${dir} " )
230- endif ()
231- endforeach ()
190+ # add_cxx_test(
191+ # NAME
192+ # foo_test
193+ # SOURCES
194+ # foo_test.cc
195+ # ${PROJECT_SOURCE_DIR}/Foo/foo_test.cc
196+ # LINK_LIBRARIES
197+ # GTest::gmock
198+ # GTest::gtest_main
199+ # )
200+ function (add_cxx_test )
201+ set (options "" )
202+ set (oneValueArgs "NAME" )
203+ set (multiValueArgs
204+ "SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS" )
205+ cmake_parse_arguments (TEST
206+ "${options} "
207+ "${oneValueArgs} "
208+ "${multiValueArgs} "
209+ ${ARGN}
210+ )
211+ if (NOT BUILD_TESTING)
212+ return ()
213+ endif ()
232214
233- foreach (PROTO_FILE IN LISTS PROTO_FILES)
234- message (STATUS "protoc(cc) .proto: ${PROTO_FILE} " )
235- get_filename_component (PROTO_DIR ${PROTO_FILE} DIRECTORY )
236- get_filename_component (PROTO_NAME ${PROTO_FILE} NAME_WE )
237- set (PROTO_HDR ${PROJECT_BINARY_DIR} /${PROTO_DIR} /${PROTO_NAME} .pb.h)
238- set (PROTO_SRC ${PROJECT_BINARY_DIR} /${PROTO_DIR} /${PROTO_NAME} .pb.cc)
239- message (STATUS "protoc(cc) hdr: ${PROTO_HDR} " )
240- message (STATUS "protoc(cc) src: ${PROTO_SRC} " )
241- add_custom_command (
242- OUTPUT ${PROTO_SRC} ${PROTO_HDR}
243- COMMAND ${PROTOC_PRG}
244- "--proto_path=${PROJECT_SOURCE_DIR} "
245- ${PROTO_DIRS}
246- "--cpp_out=dllexport_decl=PROTO_DLL:${PROJECT_BINARY_DIR} "
247- ${PROTO_FILE}
248- DEPENDS ${PROTO_NAME} .proto ${PROTOC_PRG}
249- COMMENT "Generate C++ protocol buffer for ${PROTO_FILE} "
250- WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
251- VERBATIM )
252- list (APPEND ${PROTO_HDRS} ${PROTO_HDR} )
253- list (APPEND ${PROTO_SRCS} ${PROTO_SRC} )
254- endforeach ()
255- endmacro ()
215+ if (NOT TEST_NAME)
216+ message (FATAL_ERROR "no NAME provided" )
217+ endif ()
218+ if (NOT TEST_SOURCES)
219+ message (FATAL_ERROR "no SOURCES provided" )
220+ endif ()
221+ message (STATUS "Configuring test ${TEST_NAME} ..." )
222+
223+ add_executable (${TEST_NAME} "" )
224+ target_sources (${TEST_NAME} PRIVATE ${TEST_SOURCES} )
225+ target_include_directories (${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
226+ target_compile_definitions (${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS} )
227+ target_compile_features (${TEST_NAME} PRIVATE cxx_std_20 )
228+ target_compile_options (${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS} )
229+ target_link_libraries (${TEST_NAME} PRIVATE
230+ GTest::gtest
231+ GTest::gtest_main
232+ ${TEST_LINK_LIBRARIES}
233+ )
234+ target_link_options (${TEST_NAME} PRIVATE ${TEST_LINK_OPTIONS} )
235+
236+ include (GNUInstallDirs )
237+ if (APPLE )
238+ set_target_properties (${TEST_NAME} PROPERTIES
239+ INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR} ;@loader_path" )
240+ elseif (UNIX )
241+ cmake_path (RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
242+ BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
243+ OUTPUT_VARIABLE libdir_relative_path )
244+ set_target_properties (${TEST_NAME} PROPERTIES
245+ INSTALL_RPATH "$ORIGIN/${libdir_relative_path} :$ORIGIN" )
246+ endif ()
247+
248+ add_test (
249+ NAME cxx_${TEST_NAME}
250+ COMMAND ${TEST_NAME}
251+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
252+ )
253+ message (STATUS "Configuring test ${TEST_NAME} ...DONE" )
254+ endfunction ()
256255
257256###################
258257## CMake Install ##
@@ -262,7 +261,6 @@ include(GNUInstallDirs)
262261#GENERATE_EXPORT_HEADER(${PROJECT_NAME})
263262#install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h
264263# DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
265-
266264install (EXPORT ${PROJECT_NAME} Targets
267265 NAMESPACE ${PROJECT_NAMESPACE} ::
268266 DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/${PROJECT_NAME}
0 commit comments