Skip to content

Commit 75df47c

Browse files
committed
chore: add shared libs and pkg-config support
1 parent 50b68a6 commit 75df47c

4 files changed

Lines changed: 50 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
cmake_minimum_required(VERSION 3.28)
2-
project(ncrypto)
2+
project(ncrypto VERSION 1.0.1)
33

44
include(CTest)
55
include(GNUInstallDirs)
6-
include(FetchContent)
7-
include(cmake/ncrypto-flags.cmake)
86

97
if (NOT CMAKE_BUILD_TYPE)
108
message(STATUS "No build type selected, default to Release")
119
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
1210
endif()
1311

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

16-
CPMAddPackage(
18+
CPMAddPackage(
1719
NAME boringssl
1820
VERSION 0.20250818.0
1921
GITHUB_REPOSITORY google/boringssl
2022
GIT_TAG 0.20250818.0
2123
OPTIONS "BUILD_SHARED_LIBS OFF" "BUILD_TESTING OFF"
22-
)
24+
)
25+
endif()
26+
2327
add_subdirectory(src)
2428
add_library(ncrypto::ncrypto ALIAS ncrypto)
2529

26-
include_directories(${boringssl_SOURCE_DIR}/include)
27-
2830
if (NCRYPTO_TESTING)
29-
CPMAddPackage(
31+
if (NCRYPTO_SHARED_LIBS)
32+
find_package(GTest REQUIRED)
33+
else()
34+
CPMAddPackage(
3035
NAME GTest
3136
GITHUB_REPOSITORY google/googletest
3237
VERSION 1.15.2
3338
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)
39+
)
40+
# For Windows: Prevent overriding the parent project's compiler/linker settings
41+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
42+
endif()
3743
enable_testing()
3844
add_subdirectory(tests)
3945
endif()
@@ -53,3 +59,15 @@ install(
5359
ARCHIVE COMPONENT ncrypto_development
5460
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
5561
)
62+
63+
# Generate pkg-config file
64+
configure_file(
65+
${CMAKE_CURRENT_SOURCE_DIR}/ncrypto.pc.in
66+
${CMAKE_CURRENT_BINARY_DIR}/ncrypto.pc
67+
@ONLY
68+
)
69+
70+
install(
71+
FILES ${CMAKE_CURRENT_BINARY_DIR}/ncrypto.pc
72+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
73+
)

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)
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)