Skip to content

Commit 08a121a

Browse files
Copilotnomeguy
andcommitted
Add vcpkg support with proper CMake installation and package config
- Add proper install(TARGETS) command for the casbin library - Add install(EXPORT) to generate and install CMake config files - Create casbinConfig.cmake.in for proper package discovery - Use GNUInstallDirs for standard installation paths - Change include directories and link libraries from PRIVATE to PUBLIC - Add generator expressions for BUILD_INTERFACE and INSTALL_INTERFACE - Create casbinConfigVersion.cmake with version compatibility checking - All tests pass successfully Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent 542391b commit 08a121a

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

CMakeLists.txt

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,61 @@ endif()
170170
# Install casbin
171171

172172
if(CASBIN_INSTALL)
173+
include(GNUInstallDirs)
174+
include(CMakePackageConfigHelpers)
175+
173176
message(CHECK_START "[casbin]: Installing casbin ...")
174-
export(
177+
178+
# Install the library
179+
install(
175180
TARGETS casbin
176-
NAMESPACE casbin::
177-
FILE casbinConfig.cmake
181+
EXPORT casbinTargets
182+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
183+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
184+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
178185
)
179-
186+
180187
# Installing headers
181188
install(
182189
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/casbin
183-
DESTINATION include
190+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
191+
)
192+
193+
# Install the export set
194+
install(
195+
EXPORT casbinTargets
196+
FILE casbinTargets.cmake
197+
NAMESPACE casbin::
198+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/casbin
199+
)
200+
201+
# Create and install the config file
202+
configure_package_config_file(
203+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/casbinConfig.cmake.in
204+
${CMAKE_CURRENT_BINARY_DIR}/casbinConfig.cmake
205+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/casbin
206+
)
207+
208+
# Create and install the version file
209+
write_basic_package_version_file(
210+
${CMAKE_CURRENT_BINARY_DIR}/casbinConfigVersion.cmake
211+
VERSION ${PROJECT_VERSION}
212+
COMPATIBILITY SameMajorVersion
213+
)
214+
215+
install(
216+
FILES
217+
${CMAKE_CURRENT_BINARY_DIR}/casbinConfig.cmake
218+
${CMAKE_CURRENT_BINARY_DIR}/casbinConfigVersion.cmake
219+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/casbin
220+
)
221+
222+
# Export for build tree (optional, for developers)
223+
export(
224+
EXPORT casbinTargets
225+
FILE ${CMAKE_CURRENT_BINARY_DIR}/casbinTargets.cmake
226+
NAMESPACE casbin::
184227
)
185-
186-
set(CMAKE_EXPORT_PACKAGE_REGISTRY ON)
187-
export(PACKAGE casbin)
188228

189229
message(CHECK_PASS " The targets can now be imported with find_package(casbin)")
190230
message(STATUS "[casbin]: Build the \"install\" target and add \"${CMAKE_INSTALL_PREFIX}/include\" to you PATH for casbin to work")

casbin/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ set(CMAKE_CXX_STANDARD 17)
7676
add_library(casbin STATIC ${CASBIN_SOURCE_FILES})
7777

7878
target_precompile_headers(casbin PRIVATE ${CASBIN_INCLUDE_DIR}/casbin/pch.h)
79-
target_include_directories(casbin PRIVATE ${CASBIN_INCLUDE_DIR})
80-
target_link_libraries(casbin PRIVATE nlohmann_json::nlohmann_json)
79+
target_include_directories(casbin
80+
PUBLIC
81+
$<BUILD_INTERFACE:${CASBIN_INCLUDE_DIR}>
82+
$<INSTALL_INTERFACE:include>
83+
)
84+
target_link_libraries(casbin PUBLIC nlohmann_json::nlohmann_json)
8185

8286
set_target_properties(casbin PROPERTIES
8387
PREFIX ""

cmake/casbinConfig.cmake.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
@PACKAGE_INIT@
16+
17+
include(CMakeFindDependencyMacro)
18+
19+
# Find required dependencies
20+
find_dependency(nlohmann_json 3.10.1 REQUIRED)
21+
22+
# Include the targets file
23+
include("${CMAKE_CURRENT_LIST_DIR}/casbinTargets.cmake")
24+
25+
check_required_components(casbin)

0 commit comments

Comments
 (0)