@@ -30,6 +30,171 @@ endif()
3030
3131include (GNUInstallDirs)
3232
33+ ################
34+ ## C++ Test ##
35+ ################
36+ # add_cxx_test()
37+ # CMake function to generate and build C++ test.
38+ # 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
46+ # 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 ()
104+
105+ if (BUILD_TESTING)
106+ add_test (
107+ NAME cxx_${TEST_NAME}
108+ COMMAND ${TEST_NAME}
109+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
110+ )
111+ endif ()
112+ message (STATUS "Configuring test ${TEST_NAME} ...DONE" )
113+ endfunction ()
114+
115+ ###################
116+ ## C++ Library ##
117+ ###################
118+ # add_cxx_library()
119+ # CMake function to generate and build C++ library.
120+ # Parameters:
121+ # NAME: CMake target name
122+ # SOURCES: List of source files
123+ # [TYPE]: SHARED, STATIC or INTERFACE
124+ # [COMPILE_DEFINITIONS]: List of private compile definitions
125+ # [COMPILE_OPTIONS]: List of private compile options
126+ # [LINK_LIBRARIES]: List of **public** libraries to use when linking
127+ # note: ortools::ortools is always linked to the target
128+ # [LINK_OPTIONS]: List of private link options
129+ # e.g.:
130+ # add_cxx_library(
131+ # NAME
132+ # foo
133+ # SOURCES
134+ # foo.cc
135+ # ${PROJECT_SOURCE_DIR}/Foo/foo.cc
136+ # TYPE
137+ # SHARED
138+ # LINK_LIBRARIES
139+ # GTest::gmock
140+ # GTest::gtest_main
141+ # TESTING
142+ # )
143+ function (add_cxx_library)
144+ set (options "TESTING" )
145+ set (oneValueArgs "NAME;TYPE" )
146+ set (multiValueArgs
147+ "SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS" )
148+ cmake_parse_arguments (LIBRARY
149+ "${options} "
150+ "${oneValueArgs} "
151+ "${multiValueArgs} "
152+ ${ARGN}
153+ )
154+ if (LIBRARY_TESTING AND NOT BUILD_TESTING)
155+ return ()
156+ endif ()
157+
158+ if (NOT LIBRARY_NAME)
159+ message (FATAL_ERROR "no NAME provided" )
160+ endif ()
161+ if (NOT LIBRARY_SOURCES)
162+ message (FATAL_ERROR "no SOURCES provided" )
163+ endif ()
164+ message (STATUS "Configuring library ${LIBRARY_NAME} ..." )
165+
166+ add_library (${LIBRARY_NAME} ${LIBRARY_TYPE} "" )
167+ if (LIBRARY_TYPE STREQUAL "INTERFACE" )
168+ target_include_directories (${LIBRARY_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} )
169+ target_link_libraries (${LIBRARY_NAME} INTERFACE ${LIBRARY_LINK_LIBRARIES} )
170+ else ()
171+ target_sources (${LIBRARY_NAME} PRIVATE ${LIBRARY_SOURCES} )
172+ target_include_directories (${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
173+ target_compile_definitions (${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_DEFINITIONS} )
174+ target_compile_features (${LIBRARY_NAME} PRIVATE cxx_std_20)
175+ target_compile_options (${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_OPTIONS} )
176+ target_link_libraries (${LIBRARY_NAME} PUBLIC ${LIBRARY_LINK_LIBRARIES} )
177+ target_link_options (${LIBRARY_NAME} PRIVATE ${LIBRARY_LINK_OPTIONS} )
178+ endif ()
179+
180+ include (GNUInstallDirs)
181+ if (APPLE )
182+ set_target_properties (${LIBRARY_NAME} PROPERTIES
183+ INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR} ;@loader_path" )
184+ elseif (UNIX )
185+ cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
186+ BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
187+ OUTPUT_VARIABLE libdir_relative_path)
188+ set_target_properties (${LIBRARY_NAME} PROPERTIES
189+ INSTALL_RPATH "$ORIGIN/${libdir_relative_path} :$ORIGIN" )
190+ endif ()
191+ add_library (${PROJECT_NAMESPACE} ::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME} )
192+ message (STATUS "Configuring library ${LIBRARY_NAME} ...DONE" )
193+ endfunction ()
194+
195+ ##################
196+ ## PROTO FILE ##
197+ ##################
33198# get_cpp_proto()
34199# CMake macro to generate Protobuf cpp sources
35200# Parameters:
@@ -75,6 +240,12 @@ endmacro()
75240###################
76241## CMake Install ##
77242###################
243+ include (GNUInstallDirs)
244+ #include(GenerateExportHeader)
245+ #GENERATE_EXPORT_HEADER(${PROJECT_NAME})
246+ #install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h
247+ # DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
248+
78249install (EXPORT ${PROJECT_NAME} Targets
79250 NAMESPACE ${PROJECT_NAMESPACE} ::
80251 DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/${PROJECT_NAME}
0 commit comments