@cryptotaxi247 / netdata-1 / commits / 9861bb5e7

Fix handling of OpenSSL linking on macOS (#17535)

* Pull in all dependencies for macOS CI jobs. Instead of only pulling in a basic set. * Switch to using imported target for OpenSSL in most cases. * Use imported libraries for OpenSSL in all cases. * Work around broken behavior in FindPkgConfig with static builds. It happened to be working before without this because we weren’t hitting any edge cases, but the use of IMPORTED targets with PkgConfig requires these fixes to behave correctly for transitive dependencies in static builds. * Correctly detect static builds. * Fix H2O linking. * Fix typo. * Always check for libcrypto if we found openssl. If we fail to find libcrypto when we found openssl with pkg_check_modules, then the openssl install is horribly broken and we will see failures either at link time or at runtime, so there is no point in not checking for it on macOS. This also more clearly delineates that we _do_ require libcrypto irrespective of the platform.

Austin S. Hemmelgarn committed May 21, 2024 at 06:55 UTC 9861bb5e7963c8375043570abffe406e383bedbe
2 files changed +33 -19
.github/workflows/build-macos.yml
+1 -1
@@ -105,7 +105,7 @@ jobs:
105 id: install-nd-dep
106 if: needs.file-check.outputs.run == 'true'
107 run: |
108 - bash ./packaging/installer/install-required-packages.sh --dont-wait --non-interactive netdata
108 + bash ./packaging/installer/install-required-packages.sh --dont-wait --non-interactive netdata-all
109 - name: Build from source
110 id: build-source
111 if: needs.file-check.outputs.run == 'true'
CMakeLists.txt
+32 -18
@@ -66,8 +66,23 @@ project(netdata
66 list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/packaging/cmake/Modules")
67 include(CMakeDependentOption)
68
69 +if(DEFINED BUILD_SHARED_LIBS)
70 + if(NOT BUILD_SHARED_LIBS)
71 + set(STATIC_BUILD TRUE)
72 + endif()
73 +endif()
74 +
75 +if(STATIC_BUILD)
76 + set(CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_STATIC_LIBRARY_PREFIX}")
77 + set(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_STATIC_LIBRARY_SUFFIX}")
78 +endif()
79 +
80 find_package(PkgConfig REQUIRED)
81
82 +if(STATIC_BUILD)
83 + list(APPEND PKG_CONFIG_EXECUTABLE "--static")
84 +endif()
85 +
86 set(CMAKE_C_STANDARD 11)
87 set(CMAKE_CXX_STANDARD 14)
88 set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "netdata")
@@ -547,9 +562,9 @@ endif()
562
563 # openssl/crypto
564 set(ENABLE_OPENSSL True)
550 -pkg_check_modules(OPENSSL openssl)
565 +pkg_check_modules(TLS IMPORTED_TARGET openssl)
566
552 -if(NOT OPENSSL_FOUND)
567 +if(NOT TARGET PkgConfig::TLS)
568 if(MACOS)
569 execute_process(COMMAND
570 brew --prefix --installed openssl
@@ -561,17 +576,23 @@ if(NOT OPENSSL_FOUND)
576 message(FATAL_ERROR "OpenSSL (or LibreSSL) is required for building Netdata, but could not be found.")
577 endif()
578
564 - set(OPENSSL_INCLUDE_DIRS "${BREW_OPENSSL_PREFIX}/include")
565 - set(OPENSSL_CFLAGS_OTHER "")
566 - set(OPENSSL_LDFLAGS "-L${BREW_OPENSSL_PREFIX}/lib;-lssl;-lcrypto")
579 + add_library(PkgConfig::CRYPTO IMPORTED)
580 + set_target_properties(PkgConfig::CRYPTO
581 + IMPORTED_LOCATION ${BREW_OPENSSL_PREFIX}/lib/libcrypto.dylib
582 + INTERFACE_INCLUDE_DIRECTORIES ${BREW_OPENSSL_PREFIX}/include)
583 +
584 + add_library(PkgConfig::TLS IMPORTED)
585 + set_target_properties(PkgConfig::TLS
586 + IMPORTED_LOCATION ${BREW_OPENSSL_PREFIX}/lib/libssl.dylib
587 + INTERFACE_LINK_LIBRARIES PkgConfig::CRYPTO
588 + INTERFACE_INCLUDE_DIRECTORIES ${BREW_OPENSSL_PREFIX}/include)
589 else()
590 message(FATAL_ERROR "OpenSSL (or LibreSSL) is required for building Netdata, but could not be found.")
591 endif()
592 +else()
593 + pkg_check_modules(CRYPTO IMPORTED_TARGET REQUIRED libcrypto)
594 endif()
595
572 -if(NOT MACOS)
573 - pkg_check_modules(CRYPTO libcrypto)
574 -endif()
596
597 #
598 # figure out if we need protoc/protobuf
@@ -1576,10 +1597,7 @@ if(ENABLE_H2O)
1597 )
1598
1599 target_compile_options(h2o PUBLIC -DH2O_USE_LIBUV=0)
1579 -
1580 - target_include_directories(h2o BEFORE PRIVATE ${OPENSSL_INCLUDE_DIRS})
1581 - target_compile_options(h2o PRIVATE ${OPENSSL_CFLAGS_OTHER})
1582 - target_link_libraries(h2o PRIVATE ${OPENSSL_LIBRARIES})
1600 + target_link_libraries(h2o PRIVATE PkgConfig::TLS)
1601 endif()
1602
1603 #
@@ -1713,14 +1731,10 @@ target_compile_options(libnetdata PUBLIC ${LIBUV_CFLAGS_OTHER})
1731 target_link_libraries(libnetdata PUBLIC ${LIBUV_LDFLAGS})
1732
1733 # crypto
1716 -target_include_directories(libnetdata BEFORE PUBLIC ${CRYPTO_INCLUDE_DIRS})
1717 -target_compile_options(libnetdata PUBLIC ${CRYPTO_CFLAGS_OTHER})
1718 -target_link_libraries(libnetdata PUBLIC ${CRYPTO_LDFLAGS})
1734 +target_link_libraries(libnetdata PUBLIC PkgConfig::CRYPTO)
1735
1736 # openssl
1721 -target_include_directories(libnetdata BEFORE PUBLIC ${OPENSSL_INCLUDE_DIRS})
1722 -target_compile_options(libnetdata PUBLIC ${OPENSSL_CFLAGS_OTHER})
1723 -target_link_libraries(libnetdata PUBLIC ${OPENSSL_LDFLAGS})
1737 +target_link_libraries(libnetdata PUBLIC PkgConfig::TLS)
1738
1739 # mnl
1740 if(NOT MACOS)