Skip to content

Commit 5bc2dc8

Browse files
authored
chore: add shared libs and pkg-config support (#21)
1 parent 737833c commit 5bc2dc8

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

CMakeLists.txt

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,44 @@ project(ncrypto VERSION 1.0.1) # x-release-please-version
33

44
include(CTest)
55
include(GNUInstallDirs)
6-
include(FetchContent)
76
include(cmake/ncrypto-flags.cmake)
87

98
if (NOT CMAKE_BUILD_TYPE)
109
message(STATUS "No build type selected, default to Release")
1110
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
1211
endif()
1312

14-
include(cmake/CPM.cmake)
13+
if (NCRYPTO_SHARED_LIBS)
14+
find_package(OpenSSL REQUIRED)
15+
else()
16+
include(FetchContent)
17+
include(cmake/CPM.cmake)
1518

16-
CPMAddPackage(
19+
CPMAddPackage(
1720
NAME boringssl
1821
VERSION 0.20250818.0
1922
GITHUB_REPOSITORY google/boringssl
2023
GIT_TAG 0.20250818.0
2124
OPTIONS "BUILD_SHARED_LIBS OFF" "BUILD_TESTING OFF"
22-
)
25+
)
26+
endif()
27+
2328
add_subdirectory(src)
2429
add_library(ncrypto::ncrypto ALIAS ncrypto)
2530

26-
include_directories(${boringssl_SOURCE_DIR}/include)
27-
2831
if (NCRYPTO_TESTING)
29-
CPMAddPackage(
32+
if (NCRYPTO_SHARED_LIBS)
33+
find_package(GTest REQUIRED)
34+
else()
35+
CPMAddPackage(
3036
NAME GTest
3137
GITHUB_REPOSITORY google/googletest
3238
VERSION 1.15.2
3339
OPTIONS "BUILD_GMOCK OFF" "INSTALL_GTEST OFF"
34-
)
35-
# For Windows: Prevent overriding the parent project's compiler/linker settings
36-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
40+
)
41+
# For Windows: Prevent overriding the parent project's compiler/linker settings
42+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
43+
endif()
3744
enable_testing()
3845
add_subdirectory(tests)
3946
endif()
@@ -53,3 +60,15 @@ install(
5360
ARCHIVE COMPONENT ncrypto_development
5461
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
5562
)
63+
64+
# Generate pkg-config file
65+
configure_file(
66+
${CMAKE_CURRENT_SOURCE_DIR}/ncrypto.pc.in
67+
${CMAKE_CURRENT_BINARY_DIR}/ncrypto.pc
68+
@ONLY
69+
)
70+
71+
install(
72+
FILES ${CMAKE_CURRENT_BINARY_DIR}/ncrypto.pc
73+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
74+
)

cmake/ncrypto-flags.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
option(NCRYPTO_DEVELOPMENT_CHECKS "development checks (useful for debugging)" OFF)
22
option(NCRYPTO_TESTING "Build tests" ON)
3+
option(NCRYPTO_SHARED_LIBS "Build (and test) using shared library" OFF)
34
option(NCRYPTO_BSSL_LIBDECREPIT_MISSING "enable if boringssl is built without libdecrepit" OFF)
45

56
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

ncrypto.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
exec_prefix=${prefix}
3+
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
4+
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
5+
6+
Name: ncrypto
7+
Description: crypto functions for node:crypto
8+
Version: @PROJECT_VERSION@
9+
Libs: -L${libdir} -lncrypto
10+
Cflags: -I${includedir}

src/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
add_library(ncrypto ncrypto.cpp engine.cpp aead.cpp)
2-
target_link_libraries(ncrypto PUBLIC ssl crypto)
3-
4-
if (NCRYPTO_BSSL_LIBDECREPIT_MISSING)
5-
target_compile_definitions(ncrypto PUBLIC NCRYPTO_BSSL_LIBDECREPIT_MISSING=1)
2+
if (NCRYPTO_SHARED_LIBS)
3+
target_link_libraries(ncrypto PUBLIC OpenSSL::SSL OpenSSL::Crypto)
64
else()
7-
target_link_libraries(ncrypto PUBLIC decrepit)
8-
endif()
5+
target_link_libraries(ncrypto PUBLIC ssl crypto)
96

7+
if (NCRYPTO_BSSL_LIBDECREPIT_MISSING)
8+
target_compile_definitions(ncrypto PUBLIC NCRYPTO_BSSL_LIBDECREPIT_MISSING=1)
9+
else()
10+
target_link_libraries(ncrypto PUBLIC decrepit)
11+
endif()
12+
endif()
1013
target_include_directories(ncrypto
1114
PUBLIC
1215
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>

0 commit comments

Comments
 (0)