Skip to content

Commit 47e3094

Browse files
收录 Linux 公共库 compat.libsystemd 262.2 (#975)
* 收录 Linux 公共库 compat.libsystemd 262.2 * 固定 libsystemd Meson 构建使用系统 Python * 使用 Meson 原生配置的 python 键 * 明确 libsystemd 公共头文件的安装目录
1 parent 51441c5 commit 47e3094

7 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 收录 libsystemd 262.2
2+
3+
`compat.libsystemd` 封装 systemd 发布的公共 C 接口。Linux 安装钩子使用上游 Meson 的 `libsystemd` 目标构建共享库,并通过 Meson 的安装清单复制公开头文件。包面向原生 glibc Linux。Meson 使用系统 Python 和它安装的 Jinja2 模块。XZ、LZ4 和 Zstandard 支持按名在构建时启用,对应共享库由上游在读取压缩内容时加载。
4+
5+
源码来自 GitHub 的 v262.2 标签归档,以 SHA-256 校验。自动更新跟踪同一标签和归档格式。打包时保留 LGPL 和上游附带的许可文件。
6+
7+
Debian 或 Ubuntu 的构建先决条件如下。其他发行版提供对应组件即可。
8+
9+
```sh
10+
sudo apt-get install build-essential pkg-config meson ninja-build gperf rsync python3-jinja2 liblzma-dev liblz4-dev libzstd-dev
11+
```
12+
13+
消费工程通过本地 Unix 数据报套接字验证 `sd_notify` 实际发送 `READY` 和 `STATUS`,无需运行 systemd 服务。索引工作区使用 `bash tests/run_members.sh libsystemd` 运行该用例。

‎.github/workflows/validate.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,12 @@ jobs:
20142014
if: runner.os == 'macOS' && contains(format(' {0} ', env.MEMBERS), ' nanodbc ')
20152015
run: brew install libiodbc
20162016

2017+
- name: Install libsystemd build dependencies
2018+
if: runner.os == 'Linux' && (env.MEMBERS == '__ALL__' || contains(format(' {0} ', env.MEMBERS), ' libsystemd '))
2019+
run: |
2020+
sudo apt-get update
2021+
sudo apt-get install -y --no-install-recommends meson ninja-build gperf rsync python3-jinja2 liblzma-dev liblz4-dev libzstd-dev
2022+
20172023
- name: mcpp test (workspace or affected members)
20182024
shell: bash
20192025
env:

‎.github/workflows/xpkgindex-update.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ jobs:
225225
- name: Install macOS ODBC development headers
226226
if: runner.os == 'macOS' && matrix.id == 'compat:nanodbc'
227227
run: brew install libiodbc
228+
- name: Install libsystemd build dependencies
229+
if: runner.os == 'Linux' && matrix.id == 'compat:libsystemd'
230+
run: |
231+
sudo apt-get update
232+
sudo apt-get install -y --no-install-recommends meson ninja-build gperf rsync python3-jinja2 liblzma-dev liblz4-dev libzstd-dev
233+
228234
- name: Resolve, build and run the actual candidate
229235
shell: bash
230236
env:

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ members = [
242242
"tests/examples/libinput",
243243
"tests/examples/libmaxminddb",
244244
"tests/examples/libseat",
245+
"tests/examples/libsystemd",
245246
"tests/examples/libudev",
246247
"tests/examples/bluez",
247248
"tests/examples/libxkbcommon",

‎pkgs/c/compat.libsystemd.lua‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package = {
2+
spec = "1",
3+
namespace = "compat",
4+
name = "libsystemd",
5+
description = "Public systemd client library for Linux",
6+
licenses = { "LGPL-2.1-or-later" },
7+
repo = "https://github.com/systemd/systemd",
8+
type = "package",
9+
extensions = {
10+
autoupdate = {
11+
tag = "v${version}",
12+
url = "https://github.com/systemd/systemd/archive/refs/tags/${tag}.tar.gz",
13+
},
14+
},
15+
xpm = {
16+
linux = {
17+
["262.2"] = {
18+
url = "https://github.com/systemd/systemd/archive/refs/tags/v262.2.tar.gz",
19+
sha256 = "116432ee9dce9bfef4c7da7963c6de41899ab44197f2876ee99545707a5c7c1f",
20+
},
21+
},
22+
},
23+
mcpp = {
24+
language = "c++23",
25+
import_std = false,
26+
c_standard = "c11",
27+
include_dirs = { "include" },
28+
license_files = { "LICENSE.LGPL2.1", "LICENSES/*.txt" },
29+
generated_files = {
30+
["mcpp_generated/libsystemd_anchor.c"] =
31+
"int mcpp_compat_libsystemd_anchor(void) { return 0; }\n",
32+
},
33+
sources = { "mcpp_generated/libsystemd_anchor.c" },
34+
targets = { ["systemd_binding"] = { kind = "lib" } },
35+
provides = { "platform-sdk" },
36+
ldflags = { "-l:libsystemd.so.0" },
37+
runtime = {
38+
link_library_dirs = { "lib" },
39+
runtime_search_dirs = { "lib" },
40+
deploy = {
41+
{ from = "lib/libsystemd.so.0", to = "lib" },
42+
{ from = "LICENSE.LGPL2.1", to = "licenses/libsystemd" },
43+
{ from = "LICENSES/*", to = "licenses/libsystemd/LICENSES" },
44+
},
45+
},
46+
},
47+
}
48+
49+
import("xim.libxpkg.pkginfo")
50+
import("xim.libxpkg.log")
51+
52+
local function quote(value)
53+
return "'" .. tostring(value):gsub("'", "'\\''") .. "'"
54+
end
55+
56+
local function run(command, logfile)
57+
local ok, success = pcall(os.exec, command .. " >> " .. quote(logfile) .. " 2>&1")
58+
if ok and success then return true end
59+
log.error("compat.libsystemd build failed. Install Meson, Ninja, gperf, rsync, Python 3 with Jinja2, and the XZ, LZ4 and Zstandard development packages.\n%s",
60+
io.readfile(logfile) or logfile)
61+
return false
62+
end
63+
64+
function install()
65+
local source = "systemd-" .. pkginfo.version()
66+
if not os.isfile(path.join(source, "meson.build")) then
67+
log.error("compat.libsystemd: source archive is missing meson.build")
68+
return false
69+
end
70+
71+
local prefix = pkginfo.install_dir()
72+
local build = path.join(prefix, "build")
73+
os.mkdir(prefix)
74+
local logfile = path.join(prefix, "mcpp_libsystemd_build.log")
75+
io.writefile(logfile, "")
76+
77+
-- Meson must use the interpreter that owns the system Jinja2 package.
78+
local native = path.join(prefix, "meson-native.ini")
79+
io.writefile(native, "[binaries]\npython = '/usr/bin/python3'\n")
80+
81+
local options = " --prefix=/usr --libdir=lib --includedir=include --buildtype=release --auto-features=disabled"
82+
.. " -Dmode=release -Dlibc=glibc -Dtests=false -Dtranslations=false"
83+
.. " -Dstatic-libsystemd=false -Dman=disabled -Db_lto=false"
84+
.. " -Dxz=enabled -Dlz4=enabled -Dzstd=enabled -Ddefault-compression=zstd"
85+
local setup = "CC=/usr/bin/cc CXX=/usr/bin/c++ meson setup --native-file " .. quote(native) .. " "
86+
.. quote(build) .. " " .. quote(source) .. options
87+
if not run(setup, logfile) then return false end
88+
local jobs = math.max(1, math.min(tonumber(os.getenv("MCPP_JOBS")) or 4, 4))
89+
if not run("meson compile -C " .. quote(build) .. " -j " .. jobs .. " libsystemd", logfile) then
90+
return false
91+
end
92+
93+
-- Meson's install metadata excludes systemd's internal headers.
94+
local stage = [==[
95+
import json
96+
import shutil
97+
import sys
98+
from pathlib import Path
99+
100+
build, prefix = map(Path, sys.argv[1:])
101+
installed = json.loads((build / "meson-info/intro-installed.json").read_text())
102+
headers = prefix / "include/systemd"
103+
headers.mkdir(parents=True, exist_ok=True)
104+
for src, dest in installed.items():
105+
if isinstance(dest, str) and Path(dest).parent == Path("/usr/include/systemd"):
106+
shutil.copy2(src, headers / Path(dest).name)
107+
if not (headers / "sd-daemon.h").is_file():
108+
raise RuntimeError("Missing public systemd headers in Meson install metadata")
109+
lib = prefix / "lib"
110+
lib.mkdir(exist_ok=True)
111+
shutil.copy2(build / "libsystemd.so.0", lib / "libsystemd.so.0")
112+
]==]
113+
if not run("/usr/bin/python3 -c " .. quote(stage) .. " " .. quote(build) .. " " .. quote(prefix), logfile) then
114+
return false
115+
end
116+
os.cp(path.join(source, "LICENSE.LGPL2.1"), prefix)
117+
os.cp(path.join(source, "LICENSES"), prefix)
118+
os.tryrm(build)
119+
return true
120+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "libsystemd-tests"
3+
version = "0.1.0"
4+
5+
[target.'cfg(linux)'.dependencies.compat]
6+
libsystemd = "262.2"
7+
8+
[indices]
9+
compat = { path = "../../.." }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifdef __linux__
2+
3+
#include <dlfcn.h>
4+
#include <systemd/sd-daemon.h>
5+
#include <sys/socket.h>
6+
#include <sys/un.h>
7+
#include <unistd.h>
8+
9+
#include <cstddef>
10+
#include <cstdio>
11+
#include <cstdlib>
12+
#include <cstring>
13+
#include <filesystem>
14+
#include <string_view>
15+
16+
int main() {
17+
namespace fs = std::filesystem;
18+
const auto deployed = fs::read_symlink("/proc/self/exe").parent_path() / "lib/libsystemd.so.0";
19+
Dl_info library{};
20+
std::error_code error;
21+
if (dladdr(dlsym(RTLD_DEFAULT, "sd_notify"), &library) == 0 ||
22+
!fs::equivalent(library.dli_fname, deployed, error)) {
23+
std::fputs("Expected the packaged libsystemd.so.0\n", stderr);
24+
return 1;
25+
}
26+
27+
const int receiver = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
28+
if (receiver < 0) return 2;
29+
30+
sockaddr_un address{};
31+
address.sun_family = AF_UNIX;
32+
char notify_socket[sizeof(address.sun_path)]{};
33+
std::snprintf(notify_socket, sizeof(notify_socket), "@mcpp-libsystemd-%ld", static_cast<long>(getpid()));
34+
const auto name_size = std::strlen(notify_socket);
35+
std::memcpy(address.sun_path + 1, notify_socket + 1, name_size - 1);
36+
const auto address_size = static_cast<socklen_t>(offsetof(sockaddr_un, sun_path) + name_size);
37+
if (bind(receiver, reinterpret_cast<const sockaddr*>(&address), address_size) < 0 ||
38+
setenv("NOTIFY_SOCKET", notify_socket, 1) < 0) {
39+
close(receiver);
40+
return 3;
41+
}
42+
43+
constexpr std::string_view expected{"READY=1\nSTATUS=mcpp consumer ready"};
44+
const int sent = sd_notify(1, expected.data());
45+
char message[128]{};
46+
const auto received = recv(receiver, message, sizeof(message), 0);
47+
close(receiver);
48+
if (sent <= 0 || received < 0 || std::string_view(message, received) != expected) {
49+
std::fputs("sd_notify did not deliver the expected status\n", stderr);
50+
return 4;
51+
}
52+
std::puts("Packaged libsystemd delivered READY and STATUS");
53+
return 0;
54+
}
55+
56+
#else
57+
58+
int main() { return 0; }
59+
60+
#endif

0 commit comments

Comments
 (0)