@@ -110,6 +110,106 @@ function(add_cxx_test)
110110 message (STATUS "Configuring test ${TEST_NAME} ...DONE" )
111111endfunction ()
112112
113+ ###################
114+ ## C++ Library ##
115+ ###################
116+ # add_cxx_library()
117+ # CMake function to generate and build C++ library.
118+ # Parameters:
119+ # NAME: CMake target name
120+ # [HEADERS]: List of headers files
121+ # SOURCES: List of source files
122+ # [TYPE]: SHARED, STATIC or INTERFACE
123+ # [COMPILE_DEFINITIONS]: List of private compile definitions
124+ # [COMPILE_OPTIONS]: List of private compile options
125+ # [LINK_LIBRARIES]: List of **public** libraries to use when linking
126+ # note: ortools::ortools is always linked to the target
127+ # [LINK_OPTIONS]: List of private link options
128+ # e.g.:
129+ # add_cxx_library(
130+ # NAME
131+ # foo
132+ # HEADERS
133+ # foo.h
134+ # SOURCES
135+ # foo.cc
136+ # ${PROJECT_SOURCE_DIR}/Foo/foo.cc
137+ # TYPE
138+ # SHARED
139+ # LINK_LIBRARIES
140+ # GTest::gmock
141+ # GTest::gtest_main
142+ # TESTING
143+ # )
144+ function (add_cxx_library)
145+ set (options "TESTING" )
146+ set (oneValueArgs "NAME;TYPE;INSTALL_DIR" )
147+ set (multiValueArgs
148+ "HEADERS;SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS" )
149+ cmake_parse_arguments (LIBRARY
150+ "${options} "
151+ "${oneValueArgs} "
152+ "${multiValueArgs} "
153+ ${ARGN}
154+ )
155+ if (LIBRARY_TESTING AND NOT BUILD_TESTING)
156+ return ()
157+ endif ()
158+
159+ if (NOT LIBRARY_NAME)
160+ message (FATAL_ERROR "no NAME provided" )
161+ endif ()
162+ if (NOT LIBRARY_SOURCES)
163+ message (FATAL_ERROR "no SOURCES provided" )
164+ endif ()
165+ message (STATUS "Configuring library ${LIBRARY_NAME} ..." )
166+
167+ add_library (${LIBRARY_NAME} ${LIBRARY_TYPE} "" )
168+ if (LIBRARY_TYPE STREQUAL "INTERFACE" )
169+ target_include_directories (${LIBRARY_NAME} INTERFACE
170+ ${CMAKE_CURRENT_SOURCE_DIR} /include )
171+ target_link_libraries (${LIBRARY_NAME} INTERFACE ${LIBRARY_LINK_LIBRARIES} )
172+ target_link_options (${LIBRARY_NAME} INTERFACE ${LIBRARY_LINK_OPTIONS} )
173+ else ()
174+ target_include_directories (${LIBRARY_NAME} PUBLIC
175+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
176+ $<INSTALL_INTERFACE:include >
177+ )
178+ target_sources (${LIBRARY_NAME} PRIVATE
179+ ${LIBRARY_HEADERS}
180+ ${LIBRARY_SOURCES}
181+ )
182+ target_compile_definitions (${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_DEFINITIONS} )
183+ target_compile_features (${LIBRARY_NAME} PRIVATE cxx_std_20)
184+ target_compile_options (${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_OPTIONS} )
185+ target_link_libraries (${LIBRARY_NAME} PUBLIC ${LIBRARY_LINK_LIBRARIES} )
186+ target_link_options (${LIBRARY_NAME} PRIVATE ${LIBRARY_LINK_OPTIONS} )
187+ endif ()
188+ set_target_properties (${LIBRARY_NAME} PROPERTIES
189+ VERSION ${PROJECT_VERSION}
190+ POSITION_INDEPENDENT_CODE ON
191+ PUBLIC_HEADER ${LIBRARY_HEADERS}
192+ )
193+
194+ if (APPLE )
195+ set_target_properties (${LIBRARY_NAME} PROPERTIES INSTALL_RPATH "@loader_path" )
196+ elseif (UNIX )
197+ set_target_properties (${LIBRARY_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN" )
198+ endif ()
199+
200+ # Install
201+ include (GNUInstallDirs)
202+ install (TARGETS ${LIBRARY_NAME}
203+ EXPORT ${PROJECT_NAME} Targets
204+ PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /${LIBRARY_INSTALL_DIR}
205+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
206+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
207+ #RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
208+ )
209+ add_library (${PROJECT_NAMESPACE} ::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME} )
210+ message (STATUS "Configuring library ${LIBRARY_NAME} ...DONE" )
211+ endfunction ()
212+
113213###################
114214## CMake Install ##
115215###################
0 commit comments