Skip to content

Commit 813313a

Browse files
authored
Add cmake package information (#77)
Add cmake package information generation to make it easier to use for downstream users.
1 parent 5132a32 commit 813313a

File tree

3 files changed

+144
-31
lines changed

3 files changed

+144
-31
lines changed

.github/license-check/license-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"include": [
44
"**/*.yml",
55
"**/CMakeLists.txt",
6+
"**/*.cmake.in",
67
".clang-format",
78
".gitignore"
89
],

CMakeLists.txt

Lines changed: 117 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
cmake_minimum_required(VERSION 3.22)
1616

17-
project(LibNat20 VERSION 0.0.1)
17+
project(LibNat20 VERSION 0.0.1 LANGUAGES C)
1818

1919
# Enable the libnat20 test suite by specifying
2020
# -DNAT20_WITH_TESTS=ON on the `cmake -B` command line.
@@ -55,6 +55,15 @@ set(CMAKE_C_STANDARD 11)
5555
# the benfit of clangd based IDE support.
5656
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5757

58+
if(NAT20_WITH_TESTS OR NAT20_WITH_CRYPTO_BSSL)
59+
# When building the test suite or the reference crypto implementation,
60+
# we need to enable C++ support for the test code.
61+
enable_language(CXX)
62+
set(CMAKE_CXX_STANDARD 17)
63+
endif()
64+
65+
include(GNUInstallDirs)
66+
5867
###################################################################################################
5968
# The following section defines all the groups of source files.
6069
# All files must be specified explicitly; no globbing or other generation is allowed.
@@ -145,7 +154,7 @@ set(LIBNAT20_DOC_PAGES
145154
CONTRIBUTING.md
146155
)
147156

148-
set(LIBNAT20_PUB_BSSL_HEADERS
157+
set(LIBNAT20_PUB_CRYPTO_BSSL_HEADERS
149158
include/nat20/crypto_bssl/crypto.h
150159
)
151160

@@ -178,11 +187,13 @@ set(LIBNAT20_TEST_UTILS_SOURCES
178187
# The nat20 library is the core product of this project.
179188
# It will always be compiled.
180189
add_library(nat20 STATIC)
190+
add_library(LibNat20::nat20 ALIAS nat20)
181191

182192
target_sources(nat20
183193
PRIVATE ${LIBNAT20_SOURCES}
184-
INTERFACE ${LIBNAT20_PUB_HEADERS}
194+
PRIVATE ${LIBNAT20_PUB_HEADERS}
185195
)
196+
set_target_properties(nat20 PROPERTIES PUBLIC_HEADER "${LIBNAT20_PUB_HEADERS}")
186197

187198
target_compile_options(nat20
188199
PRIVATE -pedantic
@@ -191,16 +202,23 @@ target_compile_options(nat20
191202
PRIVATE -Werror
192203
)
193204

194-
target_include_directories(nat20
195-
PUBLIC include
205+
target_include_directories(nat20 PUBLIC
206+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
207+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
208+
)
209+
210+
install(TARGETS nat20
211+
EXPORT LibNat20Targets
212+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
213+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nat20
196214
)
197215

198216
if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
199217
add_library(nat20_coverage STATIC)
200218

201219
target_sources(nat20_coverage
202220
PRIVATE ${LIBNAT20_SOURCES}
203-
INTERFACE ${LIBNAT20_PUB_HEADERS}
221+
PRIVATE ${LIBNAT20_PUB_HEADERS}
204222
)
205223

206224
target_compile_options(nat20_coverage
@@ -216,8 +234,9 @@ if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
216234
target_link_options(nat20_coverage
217235
PRIVATE --coverage
218236
)
219-
target_include_directories(nat20_coverage
220-
PUBLIC include
237+
target_include_directories(nat20_coverage PUBLIC
238+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
239+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
221240
)
222241
endif()
223242

@@ -228,11 +247,14 @@ endif()
228247
# The nat20_service library is also part of the product of this project.
229248
# It will always be compiled.
230249
add_library(nat20_service STATIC)
250+
add_library(LibNat20::nat20_service ALIAS nat20_service)
231251

232252
target_sources(nat20_service
233253
PRIVATE ${LIBNAT20_SERVICE_SOURCES}
234-
INTERFACE ${LIBNAT20_SERVICE_PUB_HEADERS}
254+
PRIVATE ${LIBNAT20_SERVICE_PUB_HEADERS}
235255
)
256+
set_target_properties(nat20_service PROPERTIES PUBLIC_HEADER "${LIBNAT20_SERVICE_PUB_HEADERS}")
257+
236258

237259
target_compile_options(nat20_service
238260
PRIVATE -pedantic
@@ -246,18 +268,26 @@ target_compile_definitions(nat20_service
246268
PUBLIC N20_STATELESS_MAX_PATH_LENGTH=${NAT20_STATELESS_MAX_PATH_LENGTH}
247269
)
248270

249-
target_include_directories(nat20_service
250-
PUBLIC include
271+
target_include_directories(nat20_service PUBLIC
272+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
273+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
251274
)
252275

253276
target_link_libraries(nat20_service nat20)
254277

278+
install(TARGETS nat20_service
279+
EXPORT LibNat20Targets
280+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
281+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nat20/service
282+
)
283+
284+
255285
if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
256286
add_library(nat20_service_coverage STATIC)
257287

258288
target_sources(nat20_service_coverage
259289
PRIVATE ${LIBNAT20_SERVICE_SOURCES}
260-
INTERFACE ${LIBNAT20_SERVICE_PUB_HEADERS}
290+
PRIVATE ${LIBNAT20_SERVICE_PUB_HEADERS}
261291
)
262292

263293
target_compile_options(nat20_service_coverage
@@ -272,8 +302,9 @@ if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
272302
PUBLIC N20_STATELESS_MAX_PATH_LENGTH=${NAT20_STATELESS_MAX_PATH_LENGTH}
273303
)
274304

275-
target_include_directories(nat20_service_coverage
276-
PUBLIC include
305+
target_include_directories(nat20_service_coverage PUBLIC
306+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
307+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
277308
)
278309

279310
target_link_libraries(nat20_service_coverage nat20)
@@ -294,11 +325,13 @@ endif()
294325
# algorithms using the native C standard library.
295326

296327
add_library(nat20_crypto_nat20 STATIC)
328+
add_library(LibNat20::nat20_crypto_nat20 ALIAS nat20_crypto_nat20)
297329

298330
target_sources(nat20_crypto_nat20
299331
PRIVATE ${LIBNAT20_CRYPTO_NAT20_SOURCES}
300-
INTERFACE ${LIBNAT20_CRYPTO_NAT20_PUB_HEADERS}
332+
PRIVATE ${LIBNAT20_CRYPTO_NAT20_PUB_HEADERS}
301333
)
334+
set_target_properties(nat20_crypto_nat20 PROPERTIES PUBLIC_HEADER "${LIBNAT20_CRYPTO_NAT20_PUB_HEADERS}")
302335

303336
target_compile_options(nat20_crypto_nat20
304337
PRIVATE -pedantic
@@ -307,16 +340,23 @@ target_compile_options(nat20_crypto_nat20
307340
PRIVATE -Werror
308341
)
309342

310-
target_include_directories(nat20_crypto_nat20
311-
PUBLIC include
343+
target_include_directories(nat20_crypto_nat20 PUBLIC
344+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
345+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
346+
)
347+
348+
install(TARGETS nat20_crypto_nat20
349+
EXPORT LibNat20Targets
350+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
351+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nat20/crypto/nat20
312352
)
313353

314354
if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
315355
add_library(nat20_crypto_nat20_coverage STATIC)
316356

317357
target_sources(nat20_crypto_nat20_coverage
318358
PRIVATE ${LIBNAT20_CRYPTO_NAT20_SOURCES}
319-
INTERFACE ${LIBNAT20_CRYPTO_NAT20_PUB_HEADERS}
359+
PRIVATE ${LIBNAT20_CRYPTO_NAT20_PUB_HEADERS}
320360
)
321361

322362
target_compile_options(nat20_crypto_nat20_coverage
@@ -326,8 +366,9 @@ if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
326366
PRIVATE -Werror
327367
)
328368

329-
target_include_directories(nat20_crypto_nat20_coverage
330-
PUBLIC include
369+
target_include_directories(nat20_crypto_nat20_coverage PUBLIC
370+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
371+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
331372
)
332373

333374
target_compile_options(nat20_crypto_nat20_coverage
@@ -358,8 +399,9 @@ if(NAT20_WITH_TESTS OR NAT20_WITH_CRYPTO_BSSL)
358399

359400
target_sources(nat20_crypto_boringssl
360401
PRIVATE ${LIBNAT20_CRYPTO_BSSL_SOURCES}
361-
INTERFACE ${LIBNAT20_PUB_CRYPTO_BSSL_HEADERS}
402+
PRIVATE ${LIBNAT20_PUB_CRYPTO_BSSL_HEADERS}
362403
)
404+
set_target_properties(nat20_crypto_boringssl PROPERTIES PUBLIC_HEADER "${LIBNAT20_PUB_CRYPTO_BSSL_HEADERS}")
363405

364406
target_compile_options(nat20_crypto_boringssl
365407
PRIVATE -pedantic
@@ -368,19 +410,27 @@ if(NAT20_WITH_TESTS OR NAT20_WITH_CRYPTO_BSSL)
368410
PRIVATE -Werror
369411
)
370412

371-
372-
target_include_directories(nat20_crypto_boringssl
373-
PUBLIC include
413+
target_include_directories(nat20_crypto_boringssl PUBLIC
414+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
415+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
374416
)
375417

376418
target_link_libraries(nat20_crypto_boringssl crypto)
377419

420+
# NOTE:
421+
# nat20_crypto_boringssl links against the BoringSSL "crypto" library
422+
# obtained via FetchContent. Since that dependency is not installed /
423+
# exported as part of this project, installing nat20_crypto_boringssl
424+
# would create a non-relocatable target for downstream consumers.
425+
# Therefore, nat20_crypto_boringssl is intentionally *not* installed
426+
# or exported as part of LibNat20. It is intended for in-tree use only.
427+
378428
if(NAT20_WITH_COVERAGE AND NAT20_WITH_TESTS)
379429
add_library(nat20_crypto_boringssl_coverage)
380430

381431
target_sources(nat20_crypto_boringssl_coverage
382432
PRIVATE ${LIBNAT20_CRYPTO_BSSL_SOURCES}
383-
INTERFACE ${LIBNAT20_PUB_CRYPTO_BSSL_HEADERS}
433+
PRIVATE ${LIBNAT20_PUB_CRYPTO_BSSL_HEADERS}
384434
)
385435

386436
target_compile_options(nat20_crypto_boringssl_coverage
@@ -390,9 +440,10 @@ if(NAT20_WITH_TESTS OR NAT20_WITH_CRYPTO_BSSL)
390440
PRIVATE -Werror
391441
)
392442

393-
394443
target_include_directories(nat20_crypto_boringssl_coverage
395-
PUBLIC include
444+
PUBLIC
445+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
446+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
396447
)
397448

398449
target_link_libraries(nat20_crypto_boringssl_coverage crypto)
@@ -446,11 +497,14 @@ if (NAT20_WITH_TESTS)
446497
add_library(nat20_test_utils STATIC)
447498
target_sources(nat20_test_utils
448499
PRIVATE ${LIBNAT20_TEST_UTILS_SOURCES}
449-
INTERFACE ${LIBNAT20_TEST_UTILS_HEADERS}
500+
PRIVATE ${LIBNAT20_TEST_UTILS_HEADERS}
450501
)
502+
set_target_properties(nat20_test_utils PROPERTIES PUBLIC_HEADER "${LIBNAT20_TEST_UTILS_HEADERS}")
503+
451504
target_link_libraries(nat20_test_utils gtest_main nat20 crypto)
452-
target_include_directories(nat20_test_utils
453-
PUBLIC include
505+
target_include_directories(nat20_test_utils PUBLIC
506+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
507+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
454508
)
455509

456510
add_executable(nat20_test_bin)
@@ -466,7 +520,6 @@ if (NAT20_WITH_TESTS)
466520
target_link_libraries(nat20_test_bin nat20)
467521
endif()
468522
target_link_libraries(nat20_test_bin gtest_main crypto pki nat20_crypto_boringssl nat20_test_utils)
469-
470523
add_test(NAME nat20_test COMMAND nat20_test_bin)
471524

472525
add_executable(nat20_service_test_bin)
@@ -577,3 +630,36 @@ if (NAT20_WITH_DOCS)
577630
)
578631

579632
endif() # NAT20_WITH_DOCS
633+
634+
include(CMakePackageConfigHelpers)
635+
636+
configure_package_config_file(
637+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/LibNat20Config.cmake.in"
638+
"${CMAKE_CURRENT_BINARY_DIR}/LibNat20Config.cmake"
639+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/LibNat20
640+
PATH_VARS
641+
CMAKE_INSTALL_INCLUDEDIR
642+
CMAKE_INSTALL_LIBDIR
643+
)
644+
645+
# Generate the version file for find_package version checking
646+
write_basic_package_version_file(
647+
"${CMAKE_CURRENT_BINARY_DIR}/LibNat20ConfigVersion.cmake"
648+
VERSION ${PROJECT_VERSION}
649+
COMPATIBILITY SameMajorVersion
650+
)
651+
652+
# Generate and install the targets file
653+
install(EXPORT LibNat20Targets
654+
FILE LibNat20Targets.cmake
655+
NAMESPACE LibNat20::
656+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/LibNat20
657+
)
658+
659+
# Install the package configuration files
660+
install(FILES
661+
"${CMAKE_CURRENT_BINARY_DIR}/LibNat20Config.cmake"
662+
"${CMAKE_CURRENT_BINARY_DIR}/LibNat20ConfigVersion.cmake"
663+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/LibNat20
664+
)
665+

cmake/LibNat20Config.cmake.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2026 Aurora Operations, Inc.
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 the targets file
18+
include("${CMAKE_CURRENT_LIST_DIR}/LibNat20Targets.cmake")
19+
20+
# Set variables for compatibility
21+
set(LIBNAT20_LIBRARIES LibNat20::nat20)
22+
set_and_check(LIBNAT20_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include")
23+
set(LIBNAT20_FOUND TRUE)
24+
25+
# Check that all required components are available
26+
check_required_components(LibNat20)

0 commit comments

Comments
 (0)