From 6974d61e23daedc73398a1729dc8da76c2fd43ef Mon Sep 17 00:00:00 2001 From: Magnus Larsen Date: Thu, 28 Nov 2024 01:34:13 -0800 Subject: [PATCH 1/4] CMake: Install natpmp_declspec.h --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5df1be2..16af4aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,7 @@ install(TARGETS natpmp natpmpc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) -install(FILES natpmp.h +install(FILES natpmp.h natpmp_declspec.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/natpmp.pc From 6864a24293bceeb65bc38952f5f098bf260724a6 Mon Sep 17 00:00:00 2001 From: Magnus Larsen Date: Thu, 28 Nov 2024 01:50:32 -0800 Subject: [PATCH 2/4] CMake: Add option to not build natpmpc.exe --- CMakeLists.txt | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16af4aa..09c6572 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ if (WIN32) endif (WIN32) option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) +option(BUILD_EXECUTABLES "Build executables" ON) # Library itself add_library(natpmp ${NATPMP_SOURCES}) @@ -38,25 +39,31 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Haiku") target_link_libraries(natpmp PUBLIC network) endif () -# Executables -add_executable(natpmpc natpmpc.c) -target_link_libraries(natpmpc natpmp) +if(BUILD_EXECUTABLES) + # Executables + add_executable(natpmpc natpmpc.c) + target_link_libraries(natpmpc natpmp) -add_executable(testgetgateway - testgetgateway.c - getgateway.c) -target_link_libraries(testgetgateway natpmp) + add_executable(testgetgateway + testgetgateway.c + getgateway.c) + target_link_libraries(testgetgateway natpmp) +endif() # natpmp.pc include(GNUInstallDirs) configure_file(natpmp.pc.in natpmp.pc @ONLY) # install -install(TARGETS natpmp natpmpc +install(TARGETS natpmp RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +if(BUILD_EXECUTABLES) + install(TARGETS natpmpc DESTINATION ${CMAKE_INSTALL_BINDIR}) +endif() + install(FILES natpmp.h natpmp_declspec.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) From b1121b5eaaa9a6eade322ea4a565e91aec233f40 Mon Sep 17 00:00:00 2001 From: Magnus Larsen Date: Thu, 28 Nov 2024 02:07:25 -0800 Subject: [PATCH 3/4] Install CMake targets for find_package config mode --- CMakeLists.txt | 23 +++++++++++++++++++++-- libnatpmp-config.cmake.in | 5 +++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 libnatpmp-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 09c6572..129322a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ option(BUILD_EXECUTABLES "Build executables" ON) # Library itself add_library(natpmp ${NATPMP_SOURCES}) -target_include_directories(natpmp PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_include_directories(natpmp PUBLIC $ $) target_compile_definitions(natpmp PRIVATE -DENABLE_STRNATPMPERR) if (WIN32) @@ -56,12 +56,15 @@ configure_file(natpmp.pc.in natpmp.pc @ONLY) # install install(TARGETS natpmp + EXPORT libnatpmp-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(BUILD_EXECUTABLES) - install(TARGETS natpmpc DESTINATION ${CMAKE_INSTALL_BINDIR}) + install(TARGETS natpmpc + EXPORT libnatpmp-targets + DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() install(FILES natpmp.h natpmp_declspec.h @@ -70,3 +73,19 @@ install(FILES natpmp.h natpmp_declspec.h install(FILES ${CMAKE_CURRENT_BINARY_DIR}/natpmp.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +include(CMakePackageConfigHelpers) +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/libnatpmp-config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/libnatpmp-config.cmake" + INSTALL_DESTINATION "share/libnatpmp" +) +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/libnatpmp-config.cmake" + DESTINATION "share/libnatpmp" +) + +install( + EXPORT libnatpmp-targets + DESTINATION share/libnatpmp + NAMESPACE libnatpmp:: +) diff --git a/libnatpmp-config.cmake.in b/libnatpmp-config.cmake.in new file mode 100644 index 0000000..350f53b --- /dev/null +++ b/libnatpmp-config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/libnatpmp-targets.cmake") + +check_required_components(libnatpmp) From ed721327df24e9b1473c777da36c660fcd76f9c1 Mon Sep 17 00:00:00 2001 From: Magnus Larsen Date: Thu, 28 Nov 2024 02:19:22 -0800 Subject: [PATCH 4/4] CMake: Add -DNATPMP_STATICLIB to natpmp.pc Cflags --- CMakeLists.txt | 2 ++ natpmp.pc.in | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 129322a..ffc2008 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,10 @@ if (WIN32) target_link_libraries(natpmp PUBLIC ws2_32 iphlpapi) if (BUILD_SHARED_LIBS) target_compile_definitions(natpmp PUBLIC -DNATPMP_EXPORTS) + set(EXTRA_PKGCONFIG_CFLAGS "") else() # win static lib target_compile_definitions(natpmp PUBLIC -DNATPMP_STATICLIB) + set(EXTRA_PKGCONFIG_CFLAGS "-DNATPMP_STATICLIB") endif (BUILD_SHARED_LIBS) endif (WIN32) diff --git a/natpmp.pc.in b/natpmp.pc.in index a4bcd0a..1fee979 100644 --- a/natpmp.pc.in +++ b/natpmp.pc.in @@ -7,4 +7,4 @@ Name: libnatpmp Description: NAT-PMP client library Version: @PROJECT_VERSION@ Libs: -L${libdir} -lnatpmp -Cflags: -I${includedir} +Cflags: -I${includedir} @EXTRA_PKGCONFIG_CFLAGS@