@@ -23,10 +23,6 @@ if(APPLE AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
2323 set (CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Minimum OS X deployment version" )
2424endif ()
2525
26- if (WIN32 )
27- add_compile_options ("/bigobj" )
28- endif ()
29-
3026###############################################################################
3127# Project definition.
3228
@@ -43,6 +39,15 @@ set(CMAKE_MODULE_PATH
4339 ${PROJECT_SOURCE_DIR } /cmake/modules
4440)
4541
42+ # Some translation units exceed the default COFF section limit. This has to come
43+ # after project(), which is what defines WIN32 and the compiler id.
44+ if (MSVC )
45+ add_compile_options ("/bigobj" )
46+ elseif (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
47+ # MinGW spells the same thing differently, and rejects /bigobj outright.
48+ add_compile_options ("-Wa,-mbig-obj" )
49+ endif ()
50+
4651###############################################################################
4752# Forbid in-source build.
4853
@@ -78,6 +83,10 @@ endif()
7883# Setting to C++ standard to C++17
7984set (CMAKE_CXX_STANDARD 17)
8085
86+ # Standard bin/lib/include/share layout, needed by the install rules below and
87+ # by the INSTALL_INTERFACE of the casbin target.
88+ include (GNUInstallDirs )
89+
8190###############################################################################
8291# Install external dependencies
8392# Some required targets may be created by third-party CMake configs, which
@@ -170,22 +179,66 @@ endif()
170179# Install casbin
171180
172181if (CASBIN_INSTALL)
173- message (CHECK_START "[casbin]: Installing casbin ..." )
174- export (
182+ message (CHECK_START "[casbin]: Configuring the casbin installation ..." )
183+
184+ include (CMakePackageConfigHelpers )
185+
186+ set (CASBIN_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR} /cmake/casbin"
187+ CACHE STRING "Path to the installed casbin CMake package files" )
188+
189+ # Installing headers
190+ install (
191+ DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR } /include/casbin
192+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
193+ )
194+
195+ # Installing the library itself. Without this the installed tree only ever
196+ # contained headers, so downstream projects had nothing to link against.
197+ install (
175198 TARGETS casbin
199+ EXPORT casbinTargets
200+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
201+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
202+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
203+ )
204+
205+ # Installing the CMake package files so that find_package(casbin) works
206+ # against the install prefix (and hence for package managers such as vcpkg)
207+ # instead of only against the CMake User Package Registry.
208+ install (
209+ EXPORT casbinTargets
210+ FILE casbinTargets.cmake
176211 NAMESPACE casbin::
177- FILE casbinConfig.cmake
212+ DESTINATION ${CASBIN_INSTALL_CMAKEDIR}
213+ )
214+
215+ configure_package_config_file (
216+ ${CMAKE_CURRENT_SOURCE_DIR } /cmake/casbinConfig.cmake.in
217+ ${CMAKE_CURRENT_BINARY_DIR } /casbinConfig.cmake
218+ INSTALL_DESTINATION ${CASBIN_INSTALL_CMAKEDIR}
219+ )
220+
221+ write_basic_package_version_file (
222+ ${CMAKE_CURRENT_BINARY_DIR } /casbinConfigVersion.cmake
223+ VERSION ${PROJECT_VERSION }
224+ COMPATIBILITY SameMajorVersion
178225 )
179226
180- # Installing headers
181227 install (
182- DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR } /include/casbin
183- DESTINATION include
228+ FILES
229+ ${CMAKE_CURRENT_BINARY_DIR } /casbinConfig.cmake
230+ ${CMAKE_CURRENT_BINARY_DIR } /casbinConfigVersion.cmake
231+ DESTINATION ${CASBIN_INSTALL_CMAKEDIR}
184232 )
185233
186- set (CMAKE_EXPORT_PACKAGE_REGISTRY ON )
187- export (PACKAGE casbin )
234+ # Also export the targets for the build tree, so that a project using
235+ # FetchContent/add_subdirectory can consume casbin:: without installing.
236+ export (
237+ EXPORT casbinTargets
238+ NAMESPACE casbin::
239+ FILE ${CMAKE_CURRENT_BINARY_DIR } /casbinTargets.cmake
240+ )
188241
189- message (CHECK_PASS " The targets can now be imported with find_package(casbin) " )
190- message (STATUS "[casbin]: Build the \" install\" target and add \" ${ CMAKE_INSTALL_PREFIX } /include \" to you PATH for casbin to work " )
242+ message (CHECK_PASS " done " )
243+ message (STATUS "[casbin]: Build the \" install\" target, then use find_package(casbin) and link against casbin::casbin " )
191244endif ()
0 commit comments