Skip to content

Commit c874271

Browse files
authored
0.27.0: rules-qml, QML applications on rules-qt (#66)
rules-qml declares the chain qt_add_qml_module runs (moc --output-json, qmltyperegistrar, qmlcachegen, rcc, qsb, qmllint) as actions, places the Qt QML modules the program imports beside it and writes a qt.conf. rules-qt gains moc_json, moc_includes, i18n.extensions and compile_ex, off by default. The pack closure of the plugins' libraries is mcpp 2026.11.23.1's (mcpp#1011); the package floor is unchanged.
1 parent ee86a0b commit c874271

59 files changed

Lines changed: 3832 additions & 32 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/docs/2026-11-24-rules-qml-design.md‎

Lines changed: 634 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# rules-qml spikes (2026-11-24)
2+
3+
Throwaway prototypes behind `../2026-11-24-rules-qml-design.md`. They are evidence, not the implementation:
4+
the implementation is C++23 modules under `rules/` and `src/`. Section numbers refer to the design document.
5+
6+
| file | what it proves | how it was run |
7+
|---|---|---|
8+
| `closure.py` | the runtime-module closure (design Q1, §8) equals `qmlimportscanner`'s on 4 samples, including all 40 top-level Qt modules (24/27/15/49 modules, both difference sets empty) | `python3 -I closure.py <dir of .qml>` |
9+
| `binary.hpp`, `needed_main.cpp` | `needed(path)` for ELF/PE/Mach-O (design Q2, §7): 180/180 SDK ELF files identical to `readelf -d`; mingw-built PE and lld-built Mach-O (thin and fat) identical to `objdump -p` / `llvm-otool -L`; 1500 truncated or corrupted inputs, no crash under ASan+UBSan | `g++ -std=c++23 -fsanitize=address,undefined needed_main.cpp -o needed` |
10+
| `module-pipeline.build.mcpp`, `module-pipeline.Main.qml` | the per-module action chain (design §6 steps 1-6) for one dotted uri with a C++ type, a singleton, a `.js`, a nested file and a resource: `cpp=84 singleton=red js=42 nested=7 resource=hello-resource`; with a C++ `QML_SINGLETON` added: `theme=9` | a package whose `build.mcpp` is this file, `src/backend.h` with `QML_ELEMENT`, `qml/ui/*`, `xim:qt` 6.11.1, `mcpp run` |
11+
| `multi-module.build.mcpp`, `multi-module.Main.qml` | two modules (`app.core` with C++ types and `Card.qml`, `app.ui` importing it); `--foreign-types` across modules gives `prototype: "Base"` for a class derived across modules; `sum=19`; the qmldir needs no `depends` line | same as above, ASCII path without spaces |
12+
| `llvm-check.sh` | the generated sources compile with clang (20.1.7, 22.1.8, 23.1.3, 23.1.6) against the real libstdc++, link with lld and run with the same output as gcc | see the pitfall in the script header |
13+
14+
Not covered by any spike: Windows, macOS, `qsb` (not runnable from `xim:qt-addons`), `qmllint`, `qt.conf` under a macOS bundle.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Prototype of mcpp.plugins.binary::needed -- std only. Returns the library names a
2+
// shared library or program names as dependencies, or an error string.
3+
#pragma once
4+
#include <algorithm>
5+
#include <cstdint>
6+
#include <cstring>
7+
#include <fstream>
8+
#include <iterator>
9+
#include <string>
10+
#include <string_view>
11+
#include <vector>
12+
namespace binary {
13+
struct result { std::vector<std::string> names; std::string error; bool ok() const { return error.empty(); } };
14+
namespace detail {
15+
struct view {
16+
const unsigned char* p; std::size_t n;
17+
bool in(std::uint64_t off, std::uint64_t len) const { return off <= n && len <= n - off; }
18+
template <class T> bool get(std::uint64_t off, T& out, bool big = false) const {
19+
if (!in(off, sizeof(T))) return false;
20+
unsigned char b[sizeof(T)]; std::memcpy(b, p + off, sizeof(T));
21+
if (big) std::reverse(b, b + sizeof(T));
22+
std::memcpy(&out, b, sizeof(T)); return true; // host is little-endian (x86-64, arm64)
23+
}
24+
bool cstr(std::uint64_t off, std::string& out, std::size_t max = 4096) const {
25+
out.clear();
26+
for (std::size_t i = 0; i < max; ++i) { if (!in(off + i, 1)) return false; char c = char(p[off + i]); if (!c) return true; out += c; }
27+
return false;
28+
}
29+
};
30+
inline void add(result& r, std::string s) { if (!s.empty() && std::ranges::find(r.names, s) == r.names.end()) r.names.push_back(std::move(s)); }
31+
32+
inline result elf(const view& v) {
33+
result r; const bool is64 = v.p[4] == 2;
34+
if (v.p[5] != 1) { r.error = "big-endian ELF is not read"; return r; }
35+
std::uint64_t phoff; std::uint16_t phentsize, phnum;
36+
if (is64) { std::uint64_t o; if (!v.get(32, o)) { r.error = "truncated ELF header"; return r; } phoff = o; v.get(54, phentsize); v.get(56, phnum); }
37+
else { std::uint32_t o; if (!v.get(28, o)) { r.error = "truncated ELF header"; return r; } phoff = o; v.get(42, phentsize); v.get(44, phnum); }
38+
struct seg { std::uint64_t vaddr, off, filesz; };
39+
std::vector<seg> loads; std::uint64_t dynoff = 0, dynsz = 0; bool haveDyn = false;
40+
for (unsigned i = 0; i < phnum; ++i) {
41+
const std::uint64_t at = phoff + std::uint64_t(i) * phentsize; std::uint32_t type;
42+
if (!v.get(at, type)) { r.error = "truncated program headers"; return r; }
43+
std::uint64_t off, vaddr, filesz;
44+
if (is64) { std::uint64_t a, b, c; if (!v.get(at + 8, a) || !v.get(at + 16, b) || !v.get(at + 32, c)) { r.error = "truncated program headers"; return r; } off = a; vaddr = b; filesz = c; }
45+
else { std::uint32_t a, b, c; if (!v.get(at + 4, a) || !v.get(at + 8, b) || !v.get(at + 16, c)) { r.error = "truncated program headers"; return r; } off = a; vaddr = b; filesz = c; }
46+
if (type == 1) loads.push_back({vaddr, off, filesz});
47+
if (type == 2) { dynoff = off; dynsz = filesz; haveDyn = true; }
48+
}
49+
if (!haveDyn) return r; // statically linked: no dependencies
50+
auto to_off = [&](std::uint64_t va, std::uint64_t& out) { for (auto& s : loads) if (va >= s.vaddr && va < s.vaddr + s.filesz) { out = va - s.vaddr + s.off; return true; } return false; };
51+
std::uint64_t strtab = 0; std::vector<std::uint64_t> needed;
52+
const std::size_t esz = is64 ? 16 : 8;
53+
for (std::uint64_t o = 0; o + esz <= dynsz; o += esz) {
54+
std::uint64_t tag, val;
55+
if (is64) { if (!v.get(dynoff + o, tag) || !v.get(dynoff + o + 8, val)) { r.error = "truncated dynamic section"; return r; } }
56+
else { std::uint32_t a, b; if (!v.get(dynoff + o, a) || !v.get(dynoff + o + 4, b)) { r.error = "truncated dynamic section"; return r; } tag = a; val = b; }
57+
if (tag == 0) break; if (tag == 1) needed.push_back(val); if (tag == 5) strtab = val;
58+
}
59+
std::uint64_t soff; if (needed.empty()) return r;
60+
if (!to_off(strtab, soff)) { r.error = "dynamic string table is outside the file"; return r; }
61+
for (auto n : needed) { std::string s; if (!v.cstr(soff + n, s)) { r.error = "bad dependency name"; return r; } add(r, s); }
62+
return r;
63+
}
64+
inline result pe(const view& v) {
65+
result r; std::uint32_t lfanew, sig;
66+
if (!v.get(0x3c, lfanew) || !v.get(lfanew, sig) || sig != 0x00004550) { r.error = "not a PE file"; return r; }
67+
std::uint16_t nsec, optsz, magic;
68+
if (!v.get(lfanew + 6, nsec) || !v.get(lfanew + 20, optsz) || !v.get(lfanew + 24, magic)) { r.error = "truncated PE header"; return r; }
69+
const std::uint64_t opt = lfanew + 24, dirs = opt + (magic == 0x20b ? 112 : 96);
70+
if (magic != 0x20b && magic != 0x10b) { r.error = "unknown PE optional header"; return r; }
71+
struct sec { std::uint32_t va, vsize, raw, rawsz; }; std::vector<sec> secs; const std::uint64_t st = opt + optsz;
72+
for (unsigned i = 0; i < nsec; ++i) { sec s; const std::uint64_t a = st + std::uint64_t(i) * 40;
73+
if (!v.get(a + 8, s.vsize) || !v.get(a + 12, s.va) || !v.get(a + 16, s.rawsz) || !v.get(a + 20, s.raw)) { r.error = "truncated section table"; return r; } secs.push_back(s); }
74+
auto rva = [&](std::uint32_t x, std::uint64_t& out) { for (auto& s : secs) { const std::uint32_t len = std::max(s.vsize, s.rawsz); if (x >= s.va && x - s.va < len) { out = std::uint64_t(x - s.va) + s.raw; return true; } } return false; };
75+
auto walk = [&](unsigned index, std::size_t entry, std::size_t nameAt, bool delay) -> bool {
76+
std::uint32_t dva, dsz; if (!v.get(dirs + std::uint64_t(index) * 8, dva) || !v.get(dirs + std::uint64_t(index) * 8 + 4, dsz)) return true; // no such directory
77+
if (!dva) return true; std::uint64_t base; if (!rva(dva, base)) return false;
78+
for (std::uint64_t o = 0;; o += entry) {
79+
std::uint32_t nm = 0, probe = 0; if (!v.get(base + o + nameAt, nm) || !v.get(base + o, probe)) return false;
80+
if (!nm && (!delay ? true : !probe)) break; if (!nm) break;
81+
std::uint64_t so; std::string s; if (!rva(nm, so) || !v.cstr(so, s, 256)) return false; add(r, s);
82+
} return true; };
83+
if (!walk(1, 20, 12, false) || !walk(13, 32, 4, true)) r.error = "import table is outside the file";
84+
return r;
85+
}
86+
inline result macho_one(const view& v, std::uint64_t base) {
87+
result r; std::uint32_t magic; if (!v.get(base, magic)) { r.error = "truncated Mach-O header"; return r; }
88+
const bool is64 = magic == 0xfeedfacf; if (!is64 && magic != 0xfeedface) { r.error = "not a little-endian Mach-O"; return r; }
89+
std::uint32_t ncmds; if (!v.get(base + 16, ncmds)) { r.error = "truncated Mach-O header"; return r; }
90+
std::uint64_t at = base + (is64 ? 32 : 28);
91+
for (std::uint32_t i = 0; i < ncmds; ++i) {
92+
std::uint32_t cmd, size; if (!v.get(at, cmd) || !v.get(at + 4, size) || size < 8) { r.error = "truncated load commands"; return r; }
93+
if (cmd == 0xc || cmd == 0x80000018 || cmd == 0x8000001f || cmd == 0x80000023 || cmd == 0x20) { // not LC_ID_DYLIB (0xd): that is the file's own name
94+
std::uint32_t nameoff; std::string s;
95+
if (!v.get(at + 8, nameoff) || nameoff >= size || !v.cstr(at + nameoff, s)) { r.error = "bad dylib command"; return r; } add(r, s); }
96+
at += size;
97+
}
98+
return r;
99+
}
100+
inline result macho(const view& v) {
101+
std::uint32_t magic; v.get(0, magic, true);
102+
if (magic != 0xcafebabe && magic != 0xcafebabf) return macho_one(v, 0);
103+
const bool f64 = magic == 0xcafebabf; std::uint32_t n; if (!v.get(4, n, true) || n > 64) { result r; r.error = "bad fat header"; return r; }
104+
result all;
105+
for (std::uint32_t i = 0; i < n; ++i) { const std::uint64_t a = 8 + std::uint64_t(i) * (f64 ? 32 : 20); std::uint64_t off;
106+
if (f64) { if (!v.get(a + 8, off, true)) { all.error = "truncated fat header"; return all; } } else { std::uint32_t o; if (!v.get(a + 8, o, true)) { all.error = "truncated fat header"; return all; } off = o; }
107+
auto one = macho_one(v, off); if (!one.ok()) { all.error = one.error; return all; } for (auto& s : one.names) add(all, s); }
108+
return all;
109+
}
110+
} // namespace detail
111+
inline result needed(const std::string& path) {
112+
std::ifstream in(path, std::ios::binary); if (!in) return {{}, "cannot open " + path};
113+
std::string data((std::istreambuf_iterator<char>(in)), {}); detail::view v{reinterpret_cast<const unsigned char*>(data.data()), data.size()};
114+
if (v.n >= 20 && std::memcmp(v.p, "\x7f""ELF", 4) == 0) return detail::elf(v);
115+
if (v.n >= 2 && v.p[0] == 'M' && v.p[1] == 'Z') return detail::pe(v);
116+
if (v.n >= 8) { std::uint32_t m; v.get(0, m); std::uint32_t mb; v.get(0, mb, true);
117+
if (m == 0xfeedface || m == 0xfeedfacf || mb == 0xcafebabe || mb == 0xcafebabf) return detail::macho(v); }
118+
return {{}, "unrecognised binary format"};
119+
}
120+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import re, sys, json, subprocess, os
2+
Q = os.path.expanduser("~/.xlings/data/xpkgs/xim-x-qt/6.11.1")
3+
IMP = re.compile(r'^\s*import\s+([A-Za-z_][\w.]*)(?:\s+[\d.]+)?(?:\s+as\s+\w+)?\s*;?\s*$', re.M)
4+
def strip(text):
5+
out=[];i=0
6+
while i<len(text):
7+
if text.startswith('//',i):
8+
while i<len(text) and text[i]!='\n': i+=1
9+
elif text.startswith('/*',i):
10+
j=text.find('*/',i+2); i=len(text) if j<0 else j+2; out.append(' ')
11+
elif text[i]=='"':
12+
j=i+1
13+
while j<len(text) and text[j]!='"': j+= 2 if text[j]=='\\' else 1
14+
out.append('""'); i=j+1
15+
else: out.append(text[i]); i+=1
16+
return ''.join(out)
17+
def file_imports(path):
18+
return IMP.findall(strip(open(path,encoding='utf-8').read()))
19+
def qmldir_follow(uri, own):
20+
rel = uri.replace('.','/')
21+
d = os.path.join(Q,'qml',rel)
22+
f = os.path.join(d,'qmldir')
23+
if not os.path.isfile(f): return None, []
24+
deps=[]
25+
for line in open(f,encoding='utf-8'):
26+
t=line.split()
27+
if not t: continue
28+
if t[0] in ('optional','default') and len(t)>=3 and t[1]=='import': deps.append(t[2]); continue
29+
if t[0]=='import' and len(t)>=2: deps.append(t[1]); continue
30+
if t[0]=='depends' and len(t)>=2: deps.append(t[1])
31+
# the module's own .qml files import further modules (the scanner reads them)
32+
for dp,dns,fns in os.walk(d):
33+
dns[:]=sorted(x for x in dns if not os.path.isfile(os.path.join(dp,x,'qmldir'))) # a nested module is its own module
34+
for fn in sorted(fns):
35+
if fn.endswith('.qml'):
36+
deps.extend(file_imports(os.path.join(dp,fn)))
37+
return rel, deps
38+
def closure(roots, own=()):
39+
seen={}; todo=list(roots)
40+
while todo:
41+
u=todo.pop()
42+
if u in seen or u in own: continue
43+
rel,deps=qmldir_follow(u,own)
44+
if rel is None: seen[u]=None; continue
45+
seen[u]=rel; todo.extend(deps)
46+
return seen
47+
def scanner(qmldir_root):
48+
j=subprocess.run([Q+'/libexec/qmlimportscanner','-rootPath',qmldir_root,'-importPath',Q+'/qml'],capture_output=True,text=True,env={'LD_LIBRARY_PATH':Q+'/lib','LC_ALL':'C.UTF-8'}).stdout
49+
return {e['name']:e.get('relativePath') for e in json.loads(j) if e.get('type')=='module'}
50+
if __name__=='__main__':
51+
root=sys.argv[1]
52+
roots=set()
53+
for dp,_,fs in os.walk(root):
54+
for f in fs:
55+
if f.endswith('.qml'): roots.update(file_imports(os.path.join(dp,f)))
56+
mine=closure(roots)
57+
sc=scanner(root)
58+
mine_set={k for k,v in mine.items() if v is not None}
59+
print("imports:",sorted(roots))
60+
print("mine=%d scanner=%d"%(len(mine_set),len(sc)))
61+
print("scanner-only:",sorted(set(sc)-mine_set))
62+
print("mine-only :",sorted(mine_set-set(sc)))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
# Compile, link and run the generated QML sources with clang and the REAL libstdc++ (Linux).
3+
# usage: llvm-check.sh <dir of generated .cpp> <dir of moc/other .cpp> <main.cpp> <include dir of src/>
4+
# PITFALL: mcpp's `clang++.cfg` appends libc++ headers unconditionally, so `clang++ -stdlib=libstdc++`
5+
# silently uses libc++. Use the C driver `clang -x c++ --no-default-config` and prove the runtime first.
6+
set -euo pipefail
7+
GEN=$1; GEN2=$2; MAIN=$3; SRC=$4
8+
LLVM=${LLVM:-$HOME/.mcpp/registry/data/xpkgs/xim-x-llvm/23.1.3}; GCC=$HOME/.mcpp/registry/data/xpkgs/xim-x-gcc/16.1.0
9+
GLD=$HOME/.mcpp/registry/data/xpkgs/xim-x-glibc/2.44.3; LH=$HOME/.mcpp/registry/data/xpkgs/xim-x-linux-headers/5.11.1/include
10+
Q=${QT_ROOT_DIR:-$HOME/.xlings/data/xpkgs/xim-x-qt/6.11.1}
11+
X="--no-default-config -x c++ -std=c++23 -nostdlibinc -nostdinc++ -isystem $GCC/include/c++/16.1.0 -isystem $GCC/include/c++/16.1.0/x86_64-linux-gnu -isystem $GLD/include -isystem $LH"
12+
printf '#include <string>\n#if defined(_LIBCPP_VERSION)\n#error RUNTIME=libc++\n#elif defined(__GLIBCXX__)\n#error RUNTIME=libstdc++\n#endif\n' > /tmp/rt.cpp
13+
$LLVM/bin/clang $X -fsyntax-only /tmp/rt.cpp 2>&1 | grep -q 'RUNTIME=libstdc++' || { echo "not libstdc++"; exit 1; }
14+
INC="-I$Q/include -I$Q/include/QtCore -I$Q/include/QtGui -I$Q/include/QtQml -I$Q/include/QtQuick -I$SRC -fPIC"
15+
D=$(mktemp -d); cd "$D"
16+
for f in $GEN/*.cpp $GEN2/*.cpp $MAIN; do $LLVM/bin/clang $X $INC -O1 -c -Wno-everything "$f" -o "$(basename "$f").o"; done
17+
$LLVM/bin/clang --no-default-config -fuse-ld=lld --gcc-install-dir=$GCC/lib/gcc/x86_64-linux-gnu/16.1.0 -B$GLD/lib64 -L$GLD/lib64 \
18+
-Wl,--dynamic-linker=$GLD/lib64/ld-linux-x86-64.so.2 -Wl,-rpath,$GLD/lib64 *.o -o prog -L$Q/lib -lQt6Quick -lQt6Qml -lQt6Gui -lQt6Core \
19+
-L$GCC/lib64 -lstdc++ -lm -Wl,-rpath,$GCC/lib64 -Wl,-rpath,$Q/lib -Wl,-rpath-link,$Q/lib -Wl,--allow-shlib-undefined
20+
QT_QPA_PLATFORM=offscreen LC_ALL=C.UTF-8 ./prog
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import QtQuick
2+
import "Util.js" as U
3+
import com.acme.ui
4+
QtObject {
5+
property Backend b: Backend {}
6+
property QtObject inner: Inner { }
7+
readonly property int viaCpp: b.twice(b.n)
8+
readonly property string viaSingleton: Style.accent
9+
readonly property int viaJs: U.add(40, 2)
10+
readonly property int viaNested: inner.value
11+
}

0 commit comments

Comments
 (0)