Skip to content

Commit 239531f

Browse files
committed
refactor(asio): make module package canonical
1 parent 9630d85 commit 239531f

14 files changed

Lines changed: 277 additions & 910 deletions

File tree

mcpp.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
[workspace]
1010
members = [
1111
"tests/examples/archive",
12-
"tests/examples/asio",
1312
"tests/examples/asio-module",
1413
"tests/examples/build-mcpp",
1514
"tests/examples/cjson",

pkgs/a/asio.lua

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
-- asio -- 将独立版 Asio 1.38.1 暴露为 C++23 模块 `asio`
2+
-- (Form B inline descriptor, separate-compilation mode)。
3+
--
4+
-- 注意事项
5+
-- * 使用 `mcpp add asio@1.38.1` 引入;消费者需显式写
6+
-- `import std; import asio;`,因为本包设置 import_std = false。
7+
-- * 本包只支持模块方式消费。同一 translation unit 不要混用
8+
-- `#include <asio.hpp>` 和 `import asio;`,避免 inline 定义与模块 BMI
9+
-- 的 separate-compilation 定义产生 ODR 差异。
10+
-- * 默认 feature 显式传播 ASIO_STANDALONE、ASIO_SEPARATE_COMPILATION、
11+
-- ASIO_DISABLE_BOOST_CONTEXT_FIBER 和 ASIO_HAS_THREADS。Asio 头文件内部
12+
-- 自动检测的其他 ASIO_HAS_* 宏不会由 `import asio;` 导出。
13+
--
14+
-- 与 header-only Asio 的区别/限制
15+
-- * 上游 1.38.x 没有模块接口单元。本描述生成 `asio.cppm`,并只编译一次
16+
-- `*/src/asio.cpp` 中的非模板实现;首次构建需生成 BMI,增量构建可避免
17+
-- 每个消费者 translation unit 重复解析整组 Asio 头文件。
18+
-- * 模块只暴露 wrapper 中明确 export 的声明,不等同于
19+
-- `#include <asio.hpp>` 的完整 API 表面。
20+
-- * asio::error_code 是 std::error_code 的别名;wrapper 导出
21+
-- asio::use_future 变量,但未导出 asio::use_future_t<Alloc> 类模板。
22+
-- * 依赖未导出 ASIO_HAS_* 宏、平台专用头文件或 Boost 扩展的代码,需要
23+
-- 改用标准/操作系统能力检测或另行扩展模块 wrapper。
24+
--
25+
-- 未导出的组件
26+
-- * SSL/TLS (`asio/ssl/*.hpp`):需要 OpenSSL/wolfSSL 等外部依赖。
27+
-- * Unix 域套接字、POSIX 描述符和 Windows 句柄:
28+
-- `asio/local/*.hpp`、`asio/posix/*.hpp`、`asio/windows/*.hpp`。
29+
-- * 串口、pipe 和文件 I/O:`asio/serial_port.hpp`、
30+
-- `asio/*able_pipe.hpp`、`asio/stream_file.hpp`、
31+
-- `asio/random_access_file.hpp`。
32+
-- * spawn()/yield_context 有栈协程:需要 Boost.Context;本包禁用其自动
33+
-- 检测,应改用 co_spawn + awaitable + use_awaitable。
34+
-- * deadline_timer、generic protocol、execution、traits、遗留宏式协程和
35+
-- streambuf:对应 `asio/deadline_timer.hpp`、`asio/generic/*.hpp`、
36+
-- `asio/execution/*.hpp`、`asio/traits/*.hpp`、`asio/yield.hpp`、
37+
-- `asio/coroutine.hpp`、`asio/streambuf.hpp`。
38+
package = {
39+
spec = "1",
40+
namespace = "",
41+
name = "asio",
42+
description = "Standalone asio exposed as the C++23 module `asio` (separate compilation)",
43+
licenses = {"BSL-1.0"},
44+
repo = "https://github.com/chriskohlhoff/asio",
45+
type = "package",
46+
47+
xpm = {
48+
linux = {
49+
["1.38.1"] = {
50+
url = {
51+
GLOBAL = "https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-38-1.tar.gz",
52+
CN = "https://gitcode.com/mcpp-res/asio/releases/download/1.38.1/asio-1.38.1.tar.gz",
53+
},
54+
sha256 = "2827b229972be80cdb14e5497962fa393d1adf036b5869e2b9c99f644daadacc",
55+
},
56+
},
57+
macosx = {
58+
["1.38.1"] = {
59+
url = {
60+
GLOBAL = "https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-38-1.tar.gz",
61+
CN = "https://gitcode.com/mcpp-res/asio/releases/download/1.38.1/asio-1.38.1.tar.gz",
62+
},
63+
sha256 = "2827b229972be80cdb14e5497962fa393d1adf036b5869e2b9c99f644daadacc",
64+
},
65+
},
66+
windows = {
67+
-- Upstream tag archives carry two POSIX symlinks
68+
-- (asio/include -> ../include, asio/src -> ../src) that tar.exe
69+
-- cannot materialize on the Windows runner. This uses the existing
70+
-- symlink-free repack documented by xlings-res/asio.
71+
["1.38.1"] = {
72+
url = {
73+
GLOBAL = "https://github.com/xlings-res/asio/releases/download/1.38.1/asio-1.38.1-nosymlinks.tar.gz",
74+
CN = "https://gitcode.com/mcpp-res/asio/releases/download/1.38.1/asio-1.38.1-nosymlinks.tar.gz",
75+
},
76+
sha256 = "77f74094bb12cd867a6edbf5736bbed816c6ce0906e880de8573097a81714d89",
77+
},
78+
},
79+
},
80+
81+
mcpp = {
82+
schema = "0.1",
83+
language = "c++23",
84+
import_std = false,
85+
modules = { "asio" },
86+
-- GitHub wraps the tag as asio-asio-1-38-1/; expose its include root
87+
-- so the wrapper's `#include <asio/*.hpp>` resolve.
88+
include_dirs = { "*/include" },
89+
generated_files = {
90+
["mcpp_generated/asio.cppm"] = [==[
91+
module;
92+
#include <asio/io_context.hpp>
93+
#include <asio/post.hpp>
94+
#include <asio/executor_work_guard.hpp>
95+
#include <asio/dispatch.hpp>
96+
#include <asio/defer.hpp>
97+
#include <asio/steady_timer.hpp>
98+
#include <asio/thread_pool.hpp>
99+
#include <asio/strand.hpp>
100+
#include <asio/ip/tcp.hpp>
101+
#include <asio/ip/address_v4.hpp>
102+
#include <asio/buffer.hpp>
103+
#include <asio/awaitable.hpp>
104+
#include <asio/this_coro.hpp>
105+
#include <asio/use_awaitable.hpp>
106+
#include <asio/co_spawn.hpp>
107+
#include <asio/cancellation_signal.hpp>
108+
#include <asio/cancellation_type.hpp>
109+
#include <asio/bind_cancellation_slot.hpp>
110+
#include <asio/execution_context.hpp>
111+
#include <asio/any_io_executor.hpp>
112+
#include <asio/system_executor.hpp>
113+
#include <asio/system_context.hpp>
114+
#include <asio/associated_executor.hpp>
115+
#include <asio/associated_allocator.hpp>
116+
#include <asio/associated_cancellation_slot.hpp>
117+
#include <asio/error_code.hpp>
118+
#include <asio/detached.hpp>
119+
#include <asio/use_future.hpp>
120+
#include <asio/deferred.hpp>
121+
#include <asio/redirect_error.hpp>
122+
#include <asio/bind_executor.hpp>
123+
#include <asio/signal_set.hpp>
124+
#include <asio/system_timer.hpp>
125+
#include <asio/bind_allocator.hpp>
126+
#include <asio/append.hpp>
127+
#include <asio/prepend.hpp>
128+
#include <asio/consign.hpp>
129+
#include <asio/as_tuple.hpp>
130+
#include <asio/socket_base.hpp>
131+
#include <asio/connect.hpp>
132+
#include <asio/read.hpp>
133+
#include <asio/write.hpp>
134+
#include <asio/read_until.hpp>
135+
#include <asio/ip/udp.hpp>
136+
#include <asio/ip/address.hpp>
137+
#include <asio/ip/address_v6.hpp>
138+
#include <asio/experimental/promise.hpp>
139+
#include <asio/experimental/channel_error.hpp>
140+
#include <asio/experimental/channel.hpp>
141+
#include <asio/experimental/concurrent_channel.hpp>
142+
#include <asio/experimental/use_promise.hpp>
143+
#include <asio/experimental/parallel_group.hpp>
144+
#include <asio/experimental/awaitable_operators.hpp>
145+
146+
export module asio;
147+
148+
export namespace asio::detail {
149+
using ::std::chrono::operator==;
150+
using ::std::chrono::operator<;
151+
using ::std::chrono::operator>=;
152+
using ::std::chrono::operator+;
153+
using ::std::chrono::operator-;
154+
using ::std::coroutine_traits;
155+
}
156+
157+
export namespace asio::error {
158+
using ::asio::error::make_error_code;
159+
using ::asio::error::operation_aborted;
160+
}
161+
162+
export namespace asio {
163+
using ::asio::io_context;
164+
using ::asio::post;
165+
using ::asio::make_work_guard;
166+
using ::asio::dispatch;
167+
using ::asio::defer;
168+
using ::asio::steady_timer;
169+
using ::asio::thread_pool;
170+
using ::asio::make_strand;
171+
using ::asio::mutable_buffer;
172+
using ::asio::const_buffer;
173+
using ::asio::buffer;
174+
using ::asio::awaitable;
175+
using ::asio::use_awaitable;
176+
using ::asio::co_spawn;
177+
using ::asio::cancellation_signal;
178+
using ::asio::cancellation_type;
179+
using ::asio::bind_cancellation_slot;
180+
using ::asio::execution_context;
181+
using ::asio::any_io_executor;
182+
using ::asio::system_executor;
183+
using ::asio::system_context;
184+
using ::asio::associated_executor;
185+
using ::asio::associated_allocator;
186+
using ::asio::associated_cancellation_slot;
187+
using ::asio::error_code;
188+
using ::asio::detached;
189+
using ::asio::detached_t;
190+
using ::asio::use_future;
191+
using ::asio::deferred;
192+
using ::asio::deferred_t;
193+
using ::asio::redirect_error;
194+
using ::asio::bind_executor;
195+
using ::asio::signal_set;
196+
using ::asio::system_timer;
197+
using ::asio::bind_allocator;
198+
using ::asio::append;
199+
using ::asio::prepend;
200+
using ::asio::consign;
201+
using ::asio::as_tuple;
202+
using ::asio::socket_base;
203+
using ::asio::connect;
204+
using ::asio::async_read;
205+
using ::asio::async_write;
206+
using ::asio::read;
207+
using ::asio::write;
208+
using ::asio::read_until;
209+
}
210+
211+
export namespace asio::experimental {
212+
using ::asio::experimental::channel;
213+
using ::asio::experimental::concurrent_channel;
214+
using ::asio::experimental::use_promise;
215+
}
216+
217+
export namespace asio::experimental::error {
218+
using ::asio::experimental::error::make_error_code;
219+
}
220+
221+
export namespace asio::ip {
222+
using ::asio::ip::tcp;
223+
using ::asio::ip::udp;
224+
using ::asio::ip::address;
225+
using ::asio::ip::address_v4;
226+
using ::asio::ip::address_v6;
227+
}
228+
229+
export namespace asio::this_coro {
230+
using ::asio::this_coro::executor;
231+
using ::asio::this_coro::cancellation_state;
232+
using ::asio::this_coro::throw_if_cancelled;
233+
using ::asio::this_coro::reset_cancellation_state;
234+
}
235+
236+
]==],
237+
},
238+
sources = {
239+
"mcpp_generated/asio.cppm",
240+
"*/src/asio.cpp",
241+
},
242+
targets = { ["asio"] = { kind = "lib" } },
243+
-- `separate-compilation` is a default feature so its defines propagate
244+
-- to every consumer TU (the module BMI and the consumer must agree on
245+
-- ASIO_SEPARATE_COMPILATION or the inline/extern split miscompiles).
246+
--
247+
-- ASIO_HAS_THREADS: asio's detection keys off CRT macros
248+
-- (_MT/_REENTRANT/_POSIX_THREADS) that the workspace's llvm-on-Windows
249+
-- toolchain does not define, otherwise silently selecting null_thread.
250+
-- Pin the known multithreaded package contract; POSIX pthread selection
251+
-- still runs beneath this define where applicable.
252+
features = {
253+
["default"] = { implies = { "separate-compilation" } },
254+
["separate-compilation"] = {
255+
defines = {
256+
"ASIO_STANDALONE",
257+
"ASIO_SEPARATE_COMPILATION",
258+
"ASIO_DISABLE_BOOST_CONTEXT_FIBER",
259+
"ASIO_HAS_THREADS",
260+
},
261+
},
262+
},
263+
deps = {},
264+
-- POSIX threading is detected by asio from unistd.h feature macros;
265+
-- retain the portable driver-level thread link contract on Linux.
266+
linux = {
267+
ldflags = { "-pthread" },
268+
},
269+
-- On the supported desktop MSVC-ABI route, asio autolinks ws2_32.lib
270+
-- and mswsock.lib. Do not inject GNU -l flags into native link.exe.
271+
},
272+
}

0 commit comments

Comments
 (0)