From 688f5da3e383de01e8422071b2e32f655d16cd6f Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Thu, 23 Jan 2025 10:29:45 +0800 Subject: [PATCH] adapt for macos. --- CMakeLists.txt | 15 +++++++------- Fluent/QHotkey/CMakeLists.txt | 8 +++++++- HttpProxy/NetworkUtility.cpp | 2 ++ HttpProxy/NetworkUtility.h | 2 ++ Nng/Message.h | 1 + UnitTest/Nng.cpp | 6 ++++-- UnitTest/Universal/MessageManagerTest.cpp | 4 ++++ Universal/CMakeLists.txt | 6 ++++++ Universal/ProcessUtility.cpp | 4 ++-- Universal/ProcessUtility.inl | 14 ++++++++----- resource/deploy.sh | 24 ++++++++++++++++------- 11 files changed, 62 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0abbae6..8954a3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,12 @@ if(INDEPENDENT_BUILD) set(OPENSSL_INCLUDE_DIR ${OpenSSL_ROOT}/include) set(OpenSSL_LIBRARY_DIRS ${OpenSSL_ROOT}/lib) set(OpenSSL_LIBRARIES libssl libcrypto) + elseif(APPLE) + set(Libraries_ROOT /opt/Libraries) + set(MbedTLS_DIR ${Libraries_ROOT}/mbedtls-3.6.2/lib/cmake/MbedTLS) + set(nng_DIR ${Libraries_ROOT}/nng-1.10/lib/cmake/nng) + set(OpenSSL_ROOT /opt/homebrew/Cellar/openssl@3/3.4.0) + set(OPENSSL_INCLUDE_DIR ${OpenSSL_ROOT}/include) else() execute_process( COMMAND sh -c "echo $HOME" @@ -36,13 +42,8 @@ if(INDEPENDENT_BUILD) set(BOOST_ROOT ${Libraries_ROOT}/boost_1_86_0) set(Boost_INCLUDE_DIR ${BOOST_ROOT}/include) - set(MBEDTLS_ROOT ${Libraries_ROOT}/mbedtls-3.6.2) - set(MBEDTLS_INCLUDE_DIR ${MBEDTLS_ROOT}/include) - set(MBEDTLS_LIBRARY_DIRS ${MBEDTLS_ROOT}/lib) - - set(NNG_ROOT ${Libraries_ROOT}/nng-1.9.0) - set(NNG_INCLUDE_DIR ${NNG_ROOT}/include) - set(NNG_LIBRARY_DIRS ${NNG_ROOT}/lib) + set(MbedTLS_DIR ${Libraries_ROOT}/mbedtls-3.6.2/lib/cmake/MbedTLS) + set(nng_DIR ${Libraries_ROOT}/nng-1.10/lib/cmake/nng) endif() option(Boost_USE_STATIC_LIBS OFF) diff --git a/Fluent/QHotkey/CMakeLists.txt b/Fluent/QHotkey/CMakeLists.txt index 4687119..c856e2a 100644 --- a/Fluent/QHotkey/CMakeLists.txt +++ b/Fluent/QHotkey/CMakeLists.txt @@ -1,9 +1,14 @@ +if(APPLE) + find_library(CARBON_LIBRARY Carbon) + mark_as_advanced(CARBON_LIBRARY) +endif() add_library(QHotkey qhotkey.h qhotkey.cpp qhotkey_p.h $<$:qhotkey_win.cpp> $<$:qhotkey_x11.cpp> + $<$:qhotkey_mac.cpp> ) target_include_directories(QHotkey @@ -13,4 +18,5 @@ target_include_directories(QHotkey target_link_libraries(QHotkey PUBLIC Qt${QT_VERSION_MAJOR}::Gui $<$:X11> -) + $<$:${CARBON_LIBRARY}> +) \ No newline at end of file diff --git a/HttpProxy/NetworkUtility.cpp b/HttpProxy/NetworkUtility.cpp index d73d228..44de166 100644 --- a/HttpProxy/NetworkUtility.cpp +++ b/HttpProxy/NetworkUtility.cpp @@ -29,6 +29,7 @@ std::string Https::put(boost::asio::io_context &ioContext, const std::string &ho return client.put(host, port, url, body, error); } +#ifndef __APPLE__ uint64_t Network::htonll(uint64_t val) { return (static_cast(htonl(static_cast(val))) << 32) + htonl(val >> 32); } @@ -36,6 +37,7 @@ uint64_t Network::htonll(uint64_t val) { uint64_t Network::ntohll(uint64_t val) { return (static_cast(ntohl(static_cast(val))) << 32) + ntohl(val >> 32); } +#endif bool Network::isConnected(const std::string_view &host, size_t size) { std::ostringstream oss; diff --git a/HttpProxy/NetworkUtility.h b/HttpProxy/NetworkUtility.h index 51c8c30..1f0a9a5 100644 --- a/HttpProxy/NetworkUtility.h +++ b/HttpProxy/NetworkUtility.h @@ -21,8 +21,10 @@ class Network { public: static bool isConnected(const std::string_view &host, size_t size); +#ifndef __APPLE__ static uint64_t htonll(uint64_t val); static uint64_t ntohll(uint64_t val); +#endif protected: Network(int argc, char *argv[]); diff --git a/Nng/Message.h b/Nng/Message.h index 39273d3..8544e83 100644 --- a/Nng/Message.h +++ b/Nng/Message.h @@ -3,6 +3,7 @@ #include #include +#include namespace Nng { diff --git a/UnitTest/Nng.cpp b/UnitTest/Nng.cpp index c6de1dc..97e622a 100644 --- a/UnitTest/Nng.cpp +++ b/UnitTest/Nng.cpp @@ -10,7 +10,8 @@ BOOST_AUTO_TEST_CASE(HelloWorld) { reply.listen("tcp://localhost:8000"); Socket request(Request); - request.dial("tcp://localhost:8000"); + std::error_code error; + request.dial("tcp://localhost:8000", error); request.send((void *)"hello", strlen("hello") + 1); auto buffer = reply.recv(); @@ -35,7 +36,8 @@ BOOST_AUTO_TEST_CASE(AisoHelloWorld) { request.asyncReceive([&request](const boost::system::error_code &error, const Buffer &buffer) { BOOST_CHECK_EQUAL(buffer.data(), std::string_view("world")); }); - request.dial("tcp://localhost:8000"); + std::error_code error; + request.dial("tcp://localhost:8000", error); request.send((void *)"hello", strlen("hello") + 1); ioContext.run(); } \ No newline at end of file diff --git a/UnitTest/Universal/MessageManagerTest.cpp b/UnitTest/Universal/MessageManagerTest.cpp index b32c94c..89d832e 100644 --- a/UnitTest/Universal/MessageManagerTest.cpp +++ b/UnitTest/Universal/MessageManagerTest.cpp @@ -30,11 +30,13 @@ BOOST_AUTO_TEST_CASE(FunctionTraitsNormalFunctionTest) { m.registerTopic("123", [&t](int a, int b) { return t.test(a, b); }); BOOST_CHECK_EQUAL(m.topicCount("123"), 1); +#ifndef __APPLE__ m.registerTopic("123", std::bind(&Test::test, &t, _1, _2)); BOOST_CHECK_EQUAL(m.topicCount("123"), 2); m.registerTopic("123", &test); BOOST_CHECK_EQUAL(m.topicCount("123"), 3); +#endif m.removeTopic("123"); BOOST_CHECK_EQUAL(m.topicCount("123"), 0); @@ -70,10 +72,12 @@ BOOST_AUTO_TEST_CASE(SyncMessage) { return 0; }); Test t; +#ifndef __APPLE__ manager.registerTopic("123", std::bind(&Test::test, &t, _1, _2)); manager.sendMessage("123", 12, 13); BOOST_CHECK_EQUAL(result3, 25); BOOST_CHECK_EQUAL(t.result, 28); +#endif int result4 = 0; manager.registerTopic("123", [&result4]() { diff --git a/Universal/CMakeLists.txt b/Universal/CMakeLists.txt index c8d9e2c..db1cc17 100644 --- a/Universal/CMakeLists.txt +++ b/Universal/CMakeLists.txt @@ -30,6 +30,12 @@ else() cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH KYLIN_CORE_INCLUDE_PATH) endif() +if(APPLE) +target_compile_definitions(Universal + PUBLIC BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED +) +endif() + if(DISABLE_LOG) target_compile_definitions(Universal PUBLIC DISABLE_LOG diff --git a/Universal/ProcessUtility.cpp b/Universal/ProcessUtility.cpp index 71f9f19..05d8c7a 100644 --- a/Universal/ProcessUtility.cpp +++ b/Universal/ProcessUtility.cpp @@ -56,7 +56,7 @@ bool DockerUtility::running(std::string_view container, std::error_code &error) std::string NativeUtility::currentExecutable() { // win32 GetModuleFileNameA -#ifdef __linux__ +#if defined(__linux__) || defined(__APPLE__) char buffer[512] = {0}; auto status = readlink("/proc/self/exe", buffer, sizeof(buffer)); return status == -1 ? std::string() : std::string(buffer); @@ -69,7 +69,7 @@ std::string NativeUtility::currentExecutable() { std::string NativeUtility::executableDirectory() { auto path = currentExecutable(); -#ifdef __linux__ +#if defined(__linux__) || defined(__APPLE__) auto slashPos = path.find_last_of("/"); #else auto slashPos = path.find_last_of("\\"); diff --git a/Universal/ProcessUtility.inl b/Universal/ProcessUtility.inl index 329080d..ab83ab2 100644 --- a/Universal/ProcessUtility.inl +++ b/Universal/ProcessUtility.inl @@ -5,10 +5,12 @@ #include "ProcessUtility.h" #include #include -#if (defined __arm__) || (defined __aarch64__) +#if __cplusplus >= 201703L +#include +#elif __cplusplus >= 201402L && defined(__GNUC__) #include #else -#include +#error "C++14 or later is required" #endif template @@ -23,10 +25,12 @@ void DockerUtility::runScript(std::string_view container, const std::string &scr template void NativeUtility::runScript(const std::string &script, Args &&...scriptArgs) { -#if (defined __arm__) || (defined __aarch64__) - using namespace std::experimental; +#if __cplusplus >= 201703L +using namespace std; +#elif __cplusplus >= 201402L && defined(__GNUC__) +using namespace std::experimental; #else - using namespace std; +#error "C++14 or later is required" #endif #if defined(WIN32) || defined(ANDROID) diff --git a/resource/deploy.sh b/resource/deploy.sh index b615625..98028e0 100755 --- a/resource/deploy.sh +++ b/resource/deploy.sh @@ -1,15 +1,25 @@ #!/bin/bash base_path=$(pwd) -qt_prefix_path="/opt/Qt/6.8.0/gcc_64" -libraries_root="/opt/Libraries" -if [ $base_path == /home/* ]; then - build_path=${base_path}/build +build_path=${base_path}/build +qt_prefix_path="/opt/Qt/6.8.1/gcc_64" +if [[ $(uname) == "Darwin" ]]; then + qt_prefix_path="/Users/amass/Qt/6.8.1/macos" else - build_path=/tmp/build + if [[ "$base_path" != /home/* ]]; then + build_path=/tmp/build + fi fi + +libraries_root="/opt/Libraries" echo "build directory: $build_path" +if command -v cmake >/dev/null 2>&1; then + cmake_exe=cmake +else + cmake_exe=/opt/Qt/Tools/CMake/bin/cmake +fi + if [ -d ${qt_prefix_path} ]; then # 先找Qt6 cmake_qt_parameters="-DCMAKE_PREFIX_PATH=${qt_prefix_path} \ -DQT_QMAKE_EXECUTABLE=${qt_prefix_path}/bin/qmake \ @@ -42,7 +52,7 @@ function cmake_scan() { if [ ! -d ${build_path} ]; then mkdir ${build_path} fi - /opt/Qt/Tools/CMake/bin/cmake \ + ${cmake_exe} \ -G Ninja \ -S ${base_path} \ -B ${build_path} \ @@ -62,7 +72,7 @@ function build() { if [ $? -ne 0 ]; then exit 1 fi - /opt/Qt/Tools/CMake/bin/cmake \ + ${cmake_exe} \ --build ${build_path} \ --target all if [ $? -ne 0 ]; then