2
0

xmake.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package("cppcoro")
  2. set_homepage("https://github.com/lewissbaker/cppcoro")
  3. set_description("A library of C++ coroutine abstractions for the coroutines TS")
  4. set_urls("https://github.com/lewissbaker/cppcoro.git")
  5. add_versions("2020.10.13", "a87e97fe5b6091ca9f6de4637736b8e0d8b109cf")
  6. if is_plat("windows") then
  7. add_syslinks("synchronization", "ws2_32", "mswsock")
  8. end
  9. on_install("windows", function (package)
  10. io.writefile("xmake.lua", [[
  11. add_rules("mode.debug", "mode.release")
  12. target("cppcoro")
  13. set_kind("$(kind)")
  14. add_files("lib/*.cpp|win32.cpp")
  15. add_includedirs("include")
  16. add_headerfiles("include/(**.hpp)")
  17. set_languages("c++17")
  18. if is_plat("windows") then
  19. add_files("lib/win32.cpp")
  20. add_defines("_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING")
  21. add_cxxflags("/await")
  22. if is_kind("shared") then
  23. add_rules("utils.symbols.export_all", {export_classes = true})
  24. add_syslinks("synchronization", "ws2_32", "mswsock")
  25. end
  26. else
  27. add_cxxflags("-fcoroutines-ts")
  28. end
  29. ]])
  30. local configs = {}
  31. if package:config("shared") then
  32. configs.kind = "shared"
  33. elseif not package:is_plat("windows", "mingw") and package:config("pic") ~= false then
  34. configs.cxflags = "-fPIC"
  35. end
  36. for _, filepath in ipairs(os.files("lib/*.cpp")) do
  37. io.replace(filepath, "cppcoro\\", "cppcoro/", {plain = true})
  38. end
  39. import("package.tools.xmake").install(package, configs)
  40. end)
  41. on_test(function (package)
  42. local cxxflags = package:is_plat("windows") and "/await" or "-fcoroutines-ts"
  43. assert(package:check_cxxsnippets({test = [[
  44. #include <cppcoro/task.hpp>
  45. #include <cppcoro/task.hpp>
  46. #include <cppcoro/sync_wait.hpp>
  47. #include <cppcoro/io_service.hpp>
  48. #include <cppcoro/when_all_ready.hpp>
  49. #include <cppcoro/read_only_file.hpp>
  50. #include <experimental/filesystem>
  51. #include <memory>
  52. #include <algorithm>
  53. #include <iostream>
  54. namespace fs = std::experimental::filesystem;
  55. cppcoro::task<std::uint64_t> count_lines(cppcoro::io_service& ioService, fs::path path) {
  56. auto file = cppcoro::read_only_file::open(ioService, path);
  57. constexpr size_t bufferSize = 4096;
  58. auto buffer = std::make_unique<std::uint8_t[]>(bufferSize);
  59. std::uint64_t newlineCount = 0;
  60. for (std::uint64_t offset = 0, fileSize = file.size(); offset < fileSize;) {
  61. const auto bytesToRead = static_cast<size_t>(
  62. std::min<std::uint64_t>(bufferSize, fileSize - offset));
  63. const auto bytesRead = co_await file.read(offset, buffer.get(), bytesToRead);
  64. newlineCount += std::count(buffer.get(), buffer.get() + bytesRead, '\n');
  65. offset += bytesRead;
  66. }
  67. co_return newlineCount;
  68. }
  69. cppcoro::task<> run(cppcoro::io_service& ioService) {
  70. cppcoro::io_work_scope ioScope(ioService);
  71. auto lineCount = co_await count_lines(ioService, fs::path{"foo.txt"});
  72. std::cout << "foo.txt has " << lineCount << " lines." << std::endl;;
  73. }
  74. cppcoro::task<> process_events(cppcoro::io_service& ioService) {
  75. co_return;
  76. }
  77. int test() {
  78. cppcoro::io_service ioService;
  79. cppcoro::sync_wait(cppcoro::when_all_ready(
  80. run(ioService),
  81. process_events(ioService)));
  82. return 0;
  83. }
  84. ]]}, {configs = {cxxflags = cxxflags, languages = "c++17", defines = "_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING"}}))
  85. end)