From 59bc2c5b655b56d9da7d23cab2386d8187c384c9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 11 Jan 2022 10:37:55 +0800 Subject: [PATCH] add promise-cpp (#837) * add promise-cpp * Update xmake.lua * Update xmake.lua * Update xmake.lua * Update xmake.lua * Update xmake.lua * improve promise-cpp * improve promise-cpp --- .../p/promise-cpp/patches/2.1.3/cmake.patch | 72 +++++++++++++++ packages/p/promise-cpp/xmake.lua | 89 +++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 packages/p/promise-cpp/patches/2.1.3/cmake.patch create mode 100644 packages/p/promise-cpp/xmake.lua diff --git a/packages/p/promise-cpp/patches/2.1.3/cmake.patch b/packages/p/promise-cpp/patches/2.1.3/cmake.patch new file mode 100644 index 000000000..7d552edc6 --- /dev/null +++ b/packages/p/promise-cpp/patches/2.1.3/cmake.patch @@ -0,0 +1,72 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index fd43e91..5ddef71 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -39,26 +39,6 @@ else() + add_library(promise STATIC ${my_sources} ${my_headers}) + endif() + +- +-add_executable(test0 ${my_headers} example/test0.cpp) +-target_link_libraries(test0 PRIVATE promise) +- +-add_executable(simple_timer ${my_headers} example/simple_timer.cpp) +-target_link_libraries(simple_timer PRIVATE promise) +- +-add_executable(simple_benchmark_test ${my_headers} example/simple_benchmark_test.cpp) +-target_link_libraries(simple_benchmark_test PRIVATE promise) +- +-find_package(Threads) +-if(Threads_FOUND) +- add_executable(multithread_test ${my_headers} example/multithread_test.cpp) +- target_link_libraries(multithread_test PRIVATE promise Threads::Threads) +-endif() +- +-add_executable(chain_defer_test ${my_headers} example/chain_defer_test.cpp) +-target_link_libraries(chain_defer_test PRIVATE promise) +- +- + find_package(Boost) + if(NOT Boost_FOUND) + message(WARNING "Boost not found, so asio projects will not be compiled") +@@ -70,40 +50,4 @@ else() + endif (UNIX) + + include_directories(. ./add_ons/asio) +- +- add_executable(asio_benchmark_test ${my_headers} example/asio_benchmark_test.cpp) +- target_compile_definitions(asio_benchmark_test PRIVATE BOOST_ALL_NO_LIB) +- target_link_libraries(asio_benchmark_test PRIVATE promise) +- +- add_executable(asio_timer ${my_headers} example/asio_timer.cpp) +- target_compile_definitions(asio_timer PRIVATE BOOST_ALL_NO_LIB) +- target_link_libraries(asio_timer PRIVATE promise) +- +- add_executable(asio_http_client ${my_headers} example/asio_http_client.cpp) +- target_compile_definitions(asio_http_client PRIVATE BOOST_ALL_NO_LIB) +- target_link_libraries(asio_http_client PRIVATE promise) +- +- add_executable(asio_http_server ${my_headers} example/asio_http_server.cpp) +- target_compile_definitions(asio_http_server PRIVATE BOOST_ALL_NO_LIB) +- target_link_libraries(asio_http_server PRIVATE promise) +-endif() +- +- +-find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets) +-find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets) +-if(NOT QT_DIR) +- message(WARNING "QT not found, so project qt_timer will not be compiled") +-else() +- if(PROMISE_BUILD_SHARED OR BUILD_SHARED_LIBS) +- add_library(promise_qt SHARED ./add_ons/qt/promise_qt.cpp ${my_headers}) +- else() +- add_library(promise_qt STATIC ./add_ons/qt/promise_qt.cpp ${my_headers}) +- endif() +- target_link_libraries(promise_qt PRIVATE promise Qt${QT_VERSION_MAJOR}::Widgets) +- +- add_subdirectory(./example/qt_timer) +-endif() +- +-if(MSVC) +- add_subdirectory(./example/mfc_timer) + endif() diff --git a/packages/p/promise-cpp/xmake.lua b/packages/p/promise-cpp/xmake.lua new file mode 100644 index 000000000..055e655f6 --- /dev/null +++ b/packages/p/promise-cpp/xmake.lua @@ -0,0 +1,89 @@ +package("promise-cpp") + set_homepage("https://github.com/xhawk18/promise-cpp") + set_description("C++ promise/A+ library in Javascript style.") + set_license("MIT") + + add_urls("https://github.com/xhawk18/promise-cpp/archive/refs/tags/$(version).tar.gz", + "https://github.com/xhawk18/promise-cpp.git") + add_versions("2.1.3", "831f5c7fb36a1f0adda408898038b428d4afe96e7028947be0f755c6851eec26") + + add_patches("2.1.3", path.join(os.scriptdir(), "patches", "2.1.3", "cmake.patch"), "3ea274743d852c685e8a9fb4f609f768c2e5461061b87473092ee98c68e1ab88") + + add_configs("boost_asio", { description = "Enable boost asio.", default = false, type = "boolean"}) + + add_deps("cmake") + add_includedirs("include", "include/promise-cpp") + + on_load(function (package) + if package:config("boost_asio") then + package:add("deps", "boost") + end + end) + + on_install("linux", "macosx", "windows", function (package) + local configs = {} + table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) + table.insert(configs, "-DPROMISE_BUILD_SHARED=" .. (package:config("shared") and "ON" or "OFF")) + import("package.tools.cmake").install(package, configs, {buildir = "build"}) + os.cp("include", package:installdir()) + os.cp("add_ons", package:installdir("include")) + if package:is_plat("linux") then + if package:config("shared") then + os.cp("build/*.so", package:installdir("lib")) + else + os.cp("build/*.a", package:installdir("lib")) + end + elseif package:is_plat("macosx") then + if package:config("shared") then + os.cp("build/*.dylib", package:installdir("lib")) + else + os.cp("build/*.a", package:installdir("lib")) + end + end + end) + + on_test(function (package) + if package:config("boost_asio") then + assert(package:check_cxxsnippets({test = [[ + #include + #include + #include "add_ons/asio/timer.hpp" + + using namespace promise; + using namespace boost::asio; + + Promise myDelay(boost::asio::io_service &io, uint64_t time_ms) { + return newPromise([&io, time_ms](Defer &d) { + setTimeout(io, [d](bool cancelled) { + if (cancelled) d.reject(); + else d.resolve(); + }, time_ms); + }); + } + + Promise testTimer(io_service &io) { + return myDelay(io, 3000).then([&] { + return myDelay(io, 1000); + }).then([&] { + return myDelay(io, 2000); + }).then([] { + printf("timer after 2000 ms!\n"); + }).fail([] { + printf("timer cancelled!\n"); + }); + } + + void test() { + io_service io; + Promise timer = testTimer(io); + delay(io, 4500).then([=] { + clearTimeout(timer); + }); + io.run(); + } + ]]}, {configs = {languages = "c++14"}})) + else + assert(package:has_cxxtypes("promise::Promise", {configs = {languages = "c++14"}, includes = "promise-cpp/promise.hpp"})) + end + end) +