diff --git a/cmake/modules/FindMiniUPnPc.cmake b/cmake/modules/FindMiniUPnPc.cmake new file mode 100644 --- /dev/null +++ b/cmake/modules/FindMiniUPnPc.cmake @@ -0,0 +1,32 @@ +# Try to find libminiupnpc +# MINIUPNPC_FOUND - system has libminiupnpc +# MINIUPNPC_INCLUDE_DIR - the libminiupnpc include directory +# MINIUPNPC_LIBRARY - Library needed to use libminiupnpc + +if (MINIUPNPC_INCLUDE_DIR AND MINIUPNPC_LIBRARY) + # Already in cache, be silent + set(MINIUPNPC_FIND_QUIETLY TRUE) +endif() + +find_path(MINIUPNPC_INCLUDE_DIR miniupnpc.h + HINTS $ENV{MINIUPNPC_INCLUDE_DIR} + PATH_SUFFIXES miniupnpc +) + +find_library(MINIUPNPC_LIBRARY NAMES miniupnpc libminiupnpc + HINTS $ENV{MINIUPNPC_LIBRARY} +) + +message(STATUS "MiniUPnPc lib: " ${MINIUPNPC_LIBRARY}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + MiniUPnPc DEFAULT_MSG + MINIUPNPC_INCLUDE_DIR + MINIUPNPC_LIBRARY +) + +mark_as_advanced(MINIUPNPC_INCLUDE_DIR MINIUPNPC_LIBRARY) + +set(MiniUPnPc_LIBRARIES ${MINIUPNPC_LIBRARY}) +set(MiniUPnPc_INCLUDE_DIRS ${MINIUPNPC_INCLUDE_DIR}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,6 +17,13 @@ option(BUILD_BITCOIN_QT "Build bitcoin-qt" ON) option(ENABLE_HARDENING "Harden the executables" ON) +# ENABLE_UPNP is a tristate option: +# - AUTO (default): enable if miniupnpc is installed, disable otherwise +# - ON: force enable, make miniupnpc a requirement +# - OFF: force disable +set(ENABLE_UPNP AUTO CACHE STRING "Enable UPnP") +set_property(CACHE ENABLE_UPNP PROPERTY STRINGS AUTO ON OFF) + # Cmake uses the CMAKE_BUILD_TYPE variable to select the build configuration. # By default it supports more configurations that needed for Bitcoin ABC, and # all the releases types set NDEBUG which is unwanted as it disables the assert @@ -301,6 +308,33 @@ memenv ) +# Try to find miniupnpc +find_package(MiniUPnPc) + +if(NOT MINIUPNPC_FOUND) + # If the user explicitly enabled UPnP through -DENABLE_UPNP=ON, ensure the + # miniupnpc library is present. + if(ENABLE_UPNP AND NOT ENABLE_UPNP STREQUAL "AUTO") + message(FATAL_ERROR + "UPnP requested but cannot be built. Use -DENABLE_UPNP=OFF" + ) + endif() +else() + if(ENABLE_UPNP) + # UPnP is enabled, whether implicitly (not set) or explicitly by + # passing DENABLE_UPNP=ON on the CMake command line. + target_link_libraries(server ${MINIUPNPC_LIBRARY}) + + target_compile_definitions(server INTERFACE -DUSE_UPNP=1) + if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") + target_compile_definitions(server + INTERFACE -DSTATICLIB + INTERFACE -DMINIUPNP_STATICLIB + ) + endif() + endif() +endif() + # Test suite. add_subdirectory(test)