restbed icon indicating copy to clipboard operation
restbed copied to clipboard

Fails to use the correct OpenSSL to link librestbed.dylib

Open mouse07410 opened this issue 5 years ago • 1 comments

MacOS Catalina 10.15.7, Xcode-12.2. Problem: macOS comes with LibreSSL that is not link-able. System apps somehow use it, but you cannot link your own app with those libraries /usr/lib/libssl.dylib and /usr/lib/libcrypto.dylib.

I cloned this repo with submodules, like your README describes. So far so good. Then CMake produced:

$ cmake ..
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/local/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
                                                              
##############################################################
#                                                            #
#  Copyright 2013-2020, Corvusoft Ltd, All Rights Reserved.  #
#                                                            #
#  Restbed is dual-licensed requiring commerical entities    #
#  to acquire a Corvusoft Permissive License, Contact        #
#  [email protected] for details.                        #
#                                                            #
##############################################################
                                                              
-- Found ASIO include at: /Users/ur20980/src/restbed/dependency/asio/asio/include
-- Found OpenSSL include at: /Users/ur20980/src/restbed/dependency/openssl/include
-- Found OpenSSL library at: /opt/local/lib/libssl.a
-- Found OpenSSL library at: /usr/lib/libssl.dylib
-- Found Crypto library at: /opt/local/lib/libcrypto.a
-- Found Crypto library at: /usr/lib/libcrypto.dylib
-- Found catch: /Users/ur20980/src/restbed/dependency/catch/single_include/catch2  
-- The C compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /opt/local/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ur20980/src/restbed/build

You can see two sets of OpenSSL libraries - the "good" one in /opt/local (/opt/local/lib/libssl.dylib and /opt/local/lib/libcrypto.dylib), and the unusable one provided by the system in /usr/lib.

make apparently uses the "bad" library, and naturally fails to link:

$ make install VERBOSE=1 2>&1 | tee make-out.txt
/opt/local/bin/cmake -S/Users/ur20980/src/restbed -B/Users/ur20980/src/restbed/build --check-build-system CMakeFiles/Makefile.cmake 0
/opt/local/bin/cmake -E cmake_progress_start /Users/ur20980/src/restbed/build/CMakeFiles /Users/ur20980/src/restbed/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/Users/ur20980/src/restbed/build'
make  -f CMakeFiles/restbed-shared.dir/build.make CMakeFiles/restbed-shared.dir/depend
make[2]: Entering directory '/Users/ur20980/src/restbed/build'
cd /Users/ur20980/src/restbed/build && /opt/local/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ur20980/src/restbed /Users/ur20980/src/restbed /Users/ur20980/src/restbed/build /Users/ur20980/src/restbed/build /Users/ur20980/src/restbed/build/CMakeFiles/restbed-shared.dir/DependInfo.cmake --color=
Dependee "/Users/ur20980/src/restbed/build/CMakeFiles/restbed-shared.dir/DependInfo.cmake" is newer than depender "/Users/ur20980/src/restbed/build/CMakeFiles/restbed-shared.dir/depend.internal".
Dependee "/Users/ur20980/src/restbed/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/ur20980/src/restbed/build/CMakeFiles/restbed-shared.dir/depend.internal".
Scanning dependencies of target restbed-shared
make[2]: Leaving directory '/Users/ur20980/src/restbed/build'
make  -f CMakeFiles/restbed-shared.dir/build.make CMakeFiles/restbed-shared.dir/build
make[2]: Entering directory '/Users/ur20980/src/restbed/build'
[  1%] Building CXX object CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/http_impl.cpp.o
/opt/local/bin/clang++ -Drestbed_shared_EXPORTS -I/Users/ur20980/src/restbed/source -isystem /Users/ur20980/src/restbed/dependency/asio/asio/include -isystem /Users/ur20980/src/restbed/dependency/openssl/include -std=gnu++17 -O3 -march=native -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -Wall -Wextra -Weffc++ -pedantic -Wno-unknown-pragmas -Wno-deprecated-declarations -DASIO_STANDALONE=YES -Wno-deprecated-declarations -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -mmacosx-version-min=10.15 -fPIC -std=gnu++14 -o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/http_impl.cpp.o -c /Users/ur20980/src/restbed/source/corvusoft/restbed/detail/http_impl.cpp
/Users/ur20980/src/restbed/source/corvusoft/restbed/detail/http_impl.cpp:77:34: warning: loop variable 'parameter' of type 'const std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> >' creates a copy from type 'const std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> >' [-Wrange-loop-analysis]
                for ( const auto parameter : parameters )
                                 ^
/Users/ur20980/src/restbed/source/corvusoft/restbed/detail/http_impl.cpp:77:23: note: use reference type 'const std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> > &' to prevent copying
                for ( const auto parameter : parameters )
                      ^~~~~~~~~~~~~~~~~~~~~~
                                 &
1 warning generated.
.  .  .  .  .
[  8%] Linking CXX shared library librestbed.dylib
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/restbed-shared.dir/link.txt --verbose=1
/opt/local/bin/clang++ -std=gnu++17 -O3 -march=native -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -Wall -Wextra -Weffc++ -pedantic -Wno-unknown-pragmas -Wno-deprecated-declarations -DASIO_STANDALONE=YES -Wno-deprecated-declarations -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -mmacosx-version-min=10.15 -dynamiclib -Wl,-headerpad_max_install_names -compatibility_version 4.0.0 -current_version 4.7.0 -o librestbed.4.7.dylib -install_name @rpath/librestbed.4.dylib CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/http_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/service_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/session_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/socket_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/web_socket_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/detail/web_socket_manager_impl.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/http.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/request.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/resource.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/response.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/rule.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/service.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/session.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/session_manager.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/settings.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/ssl_settings.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/string.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/uri.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/web_socket.cpp.o CMakeFiles/restbed-shared.dir/source/corvusoft/restbed/web_socket_message.cpp.o  /usr/lib/libssl.dylib /usr/lib/libcrypto.dylib 
ld: cannot link directly with dylib/framework, your binary is not an allowed client of /usr/lib/libcrypto.dylib for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/restbed-shared.dir/build.make:390: librestbed.4.7.dylib] Error 1
make[2]: Leaving directory '/Users/ur20980/src/restbed/build'
make[1]: *** [CMakeFiles/Makefile2:402: CMakeFiles/restbed-shared.dir/all] Error 2
make[1]: Leaving directory '/Users/ur20980/src/restbed/build'
make: *** [Makefile:160: all] Error 2

What I need is to tell the build process (and/or CMake) to use OpenSSL installed in /opt/local, and build other submodules as appropriate.

Please help! Thanks

mouse07410 avatar Nov 24 '20 02:11 mouse07410

And here's the proposed (and tested) fix:

diff --git a/cmake/Findopenssl.cmake b/cmake/Findopenssl.cmake
index 1d635f3..0c18e5f 100644
--- a/cmake/Findopenssl.cmake
+++ b/cmake/Findopenssl.cmake
@@ -1,9 +1,24 @@
+if( ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" )
+
+# On MacOS, user applications cannot link to system-provided LibreSSL.
+# Therefore, finding /usr/lib/libcrypto.dylib on MacOS guarantees that the linking will fail
+
+find_library( ssl_LIBRARY_STATIC libssl.a libssl_static.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/local/lib" "/opt/local/lib" )
+find_library( crypto_LIBRARY_STATIC libcrypto.a libcrypto_static.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/local/lib" "/opt/local/lib" )
+
+find_library( ssl_LIBRARY_SHARED libssl.so libssl.dylib libssl.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/local/lib" "/opt/local/lib" )
+find_library( crypto_LIBRARY_SHARED libcrypto.so libcrypto.dylib libcrypto.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/local/lib" "/opt/local/lib" )
+
+else( ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" )
+
 find_library( ssl_LIBRARY_STATIC libssl.a libssl_static.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
 find_library( crypto_LIBRARY_STATIC libcrypto.a libcrypto_static.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
 
 find_library( ssl_LIBRARY_SHARED libssl.so libssl.dylib libssl.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
 find_library( crypto_LIBRARY_SHARED libcrypto.so libcrypto.dylib libcrypto.lib HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
 
+endif( ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" )
+
 find_path( ssl_INCLUDE openssl/ssl.h HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/include" "/usr/local/opt/openssl/include" "/usr/include" "/usr/local/include" "/opt/local/include" )
 
 if ( ssl_LIBRARY_STATIC AND ssl_LIBRARY_SHARED AND crypto_LIBRARY_STATIC AND crypto_LIBRARY_SHARED )

mouse07410 avatar Nov 24 '20 02:11 mouse07410