Squashed 'external/toxcore/c-toxcore/' changes from 1828c5356..c9cdae001

c9cdae001 fix(toxav): remove extra copy of video frame on encode
4f6d4546b test: Improve the fake network library.
a2581e700 refactor(toxcore): generate `Friend_Request` and `Dht_Nodes_Response`
2aaa11770 refactor(toxcore): use Tox_Memory in generated events
5c367452b test(toxcore): fix incorrect mutex in tox_scenario_get_time
8f92e710f perf: Add a timed limit of number of cookie requests.
695b6417a test: Add some more simulated network support.
815ae9ce9 test(toxcore): fix thread-safety in scenario framework
6d85c754e test(toxcore): add unit tests for net_crypto
9c22e79cc test(support): add SimulatedEnvironment for deterministic testing
f34fcb195 chore: Update windows Dockerfile to debian stable (trixie).
ece0e8980 fix(group_moderation): allow validating unsorted sanction list signatures
a4fa754d7 refactor: rename struct Packet to struct Net_Packet
d6f330f85 cleanup: Fix some warnings from coverity.
e206bffa2 fix(group_chats): fix sync packets reverting topics
0e4715598 test: Add new scenario testing framework.
668291f44 refactor(toxcore): decouple Network_Funcs from sockaddr via IP_Port
fc4396cef fix: potential division by zero in toxav and unsafe hex parsing
8e8b352ab refactor: Add nullable annotations to struct members.
7740bb421 refactor: decouple net_crypto from DHT
1936d4296 test: add benchmark for toxav audio and video
46bfdc2df fix: correct printf format specifiers for unsigned integers
REVERT: 1828c5356 fix(toxav): remove extra copy of video frame on encode

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: c9cdae001341e701fca980c9bb9febfeb95d2902
This commit is contained in:
Green Sky
2026-01-11 14:42:31 +01:00
parent e95f2cbb1c
commit 565efa4f39
328 changed files with 19057 additions and 13982 deletions

View File

@@ -4,29 +4,43 @@ import os
import subprocess
import sys
from dataclasses import dataclass
from typing import Iterable
from typing import Optional
from dataclasses import field
from typing import Any
from typing import Dict
from typing import List
LIBS = {}
MODS = {}
STD_MODULE = """module std [system] {
textual header "/usr/include/alloca.h"
textual header "/usr/include/assert.h"
textual header "/usr/include/c++/14.2.0/algorithm"
textual header "/usr/include/c++/14.2.0/array"
textual header "/usr/include/c++/14.2.0/atomic"
textual header "/usr/include/c++/14.2.0/cassert"
textual header "/usr/include/c++/14.2.0/cerrno"
textual header "/usr/include/c++/14.2.0/chrono"
textual header "/usr/include/c++/14.2.0/climits"
textual header "/usr/include/c++/14.2.0/compare"
textual header "/usr/include/c++/14.2.0/cstddef"
textual header "/usr/include/c++/14.2.0/cstdint"
textual header "/usr/include/c++/14.2.0/cstdio"
textual header "/usr/include/c++/14.2.0/cstdlib"
textual header "/usr/include/c++/14.2.0/cstring"
textual header "/usr/include/c++/14.2.0/deque"
textual header "/usr/include/c++/14.2.0/functional"
textual header "/usr/include/c++/14.2.0/iomanip"
textual header "/usr/include/c++/14.2.0/iosfwd"
textual header "/usr/include/c++/14.2.0/iostream"
textual header "/usr/include/c++/14.2.0/limits"
textual header "/usr/include/c++/14.2.0/map"
textual header "/usr/include/c++/14.2.0/memory"
textual header "/usr/include/c++/14.2.0/mutex"
textual header "/usr/include/c++/14.2.0/new"
textual header "/usr/include/c++/14.2.0/optional"
textual header "/usr/include/c++/14.2.0/ostream"
textual header "/usr/include/c++/14.2.0/queue"
textual header "/usr/include/c++/14.2.0/random"
textual header "/usr/include/c++/14.2.0/stdlib.h"
textual header "/usr/include/c++/14.2.0/string"
textual header "/usr/include/c++/14.2.0/thread"
textual header "/usr/include/c++/14.2.0/type_traits"
textual header "/usr/include/c++/14.2.0/vector"
@@ -43,29 +57,33 @@ STD_MODULE = """module std [system] {
textual header "/usr/include/sys/types.h"
textual header "/usr/include/time.h"
}
module "//c-toxcore/third_party:cmp" {
module "c_toxcore_third_party_cmp" {
header "third_party/cmp/cmp.h"
use std
}
module "//c-toxcore/toxencryptsave:defines" {
module "c_toxcore_toxencryptsave_defines" {
header "toxencryptsave/defines.h"
}
module "@benchmark" {
module "_benchmark" {
textual header "/usr/include/benchmark/benchmark.h"
use std
}
module "@com_google_googletest//:gtest" {
module "_com_google_googletest___gtest" {
textual header "/usr/include/gmock/gmock.h"
textual header "/usr/include/gtest/gtest.h"
use std
}
module "@libsodium" {
module "_com_google_googletest___gtest_main" {
// Dummy module for gtest_main, assuming it links but headers are in gtest
use "_com_google_googletest___gtest"
}
module "_libsodium" {
textual header "/usr/include/sodium.h"
}
module "@pthread" {
module "_pthread" {
textual header "/usr/include/pthread.h"
}
module "@psocket" {
module "_psocket" {
textual header "/usr/include/arpa/inet.h"
textual header "/usr/include/fcntl.h"
textual header "/usr/include/fortify/sys/socket.h"
@@ -79,94 +97,132 @@ module "@psocket" {
@dataclass
class Context:
pkg: str
pkg_prefix: str
class Target:
name: str
package: str
srcs: List[str] = field(default_factory=list)
hdrs: List[str] = field(default_factory=list)
deps: List[str] = field(default_factory=list)
def bzl_load(self, bzl: str, *syms: str) -> None:
@property
def label(self) -> str:
# Sanitize label for module name
sanitized = (f"//c-toxcore/{self.package}:{self.name}".replace(
"/", "_").replace(":",
"_").replace(".",
"_").replace("-",
"_").replace("@", "_"))
if sanitized.startswith("__"):
sanitized = sanitized[2:]
return sanitized
TARGETS: List[Target] = []
class BuildContext:
def __init__(self, package: str):
self.package = package
def bzl_load(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_exports_files(
self,
srcs: list[str],
visibility: Optional[list[str]] = None,
) -> None:
def bzl_exports_files(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_cc_library(
self,
name: str,
srcs: Iterable[str] = tuple(),
hdrs: Iterable[str] = tuple(),
deps: Iterable[str] = tuple(),
visibility: Iterable[str] = tuple(),
testonly: bool = False,
copts: Iterable[str] = tuple(),
) -> None:
LIBS[name] = {
"srcs":
srcs,
"deps": [
f"{self.pkg_prefix}{dep}" if dep[0] == ":" else dep
for dep in deps
],
"hdrs":
hdrs,
}
def bzl_cc_binary(
self,
name: str,
srcs: Iterable[str] = tuple(),
hdrs: Iterable[str] = tuple(),
deps: Iterable[str] = tuple(),
**kwargs: list[str],
) -> None:
LIBS[name] = {
"srcs":
srcs,
"deps": [
f"{self.pkg_prefix}{dep}" if dep[0] == ":" else dep
for dep in deps
],
"hdrs":
hdrs,
}
def bzl_cc_fuzz_test(self, name: str, **kwargs: list[str]) -> None:
def bzl_alias(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_select(self, selector: dict[str, list[str]]) -> list[str]:
return selector["//tools/config:linux"]
def bzl_glob(self, include: list[str]) -> list[str]:
return [
f[len(self.pkg) + 1:] for p in include
for f in glob.glob(os.path.join(self.pkg, p))
]
def bzl_alias(self, name: str, actual: str, visibility: list[str]) -> None:
def bzl_sh_library(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_sh_library(self, name: str, **kwargs: list[str]) -> None:
def bzl_cc_fuzz_test(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_select(self, selector: Dict[str, List[str]]) -> List[str]:
return selector.get("//tools/config:linux",
selector.get("//conditions:default", []))
def bzl_glob(self, include: List[str]) -> List[str]:
results = []
for pattern in include:
full_pattern = os.path.join(self.package, pattern)
files = glob.glob(full_pattern)
results.extend([os.path.relpath(f, self.package) for f in files])
return results
def _add_target(self, name: str, srcs: Any, hdrs: Any, deps: Any) -> None:
srcs = list(srcs) if srcs else []
hdrs = list(hdrs) if hdrs else []
deps = list(deps) if deps else []
TARGETS.append(Target(name, self.package, srcs, hdrs, deps))
def bzl_cc_library(self,
name: str,
srcs: Any = (),
hdrs: Any = (),
deps: Any = (),
**kwargs: Any) -> None:
self._add_target(name, srcs, hdrs, deps)
def bzl_cc_binary(self,
name: str,
srcs: Any = (),
hdrs: Any = (),
deps: Any = (),
**kwargs: Any) -> None:
self._add_target(name, srcs, hdrs, deps)
def bzl_cc_test(self,
name: str,
srcs: Any = (),
hdrs: Any = (),
deps: Any = (),
**kwargs: Any) -> None:
self._add_target(name, srcs, hdrs, deps)
def resolve_module_name(dep: str, current_pkg: str) -> str:
# Resolve to canonical label first
label = dep
if dep.startswith("@"):
label = dep
elif dep.startswith("//"):
if ":" in dep:
label = dep
else:
pkg_name = os.path.basename(dep)
label = f"{dep}:{pkg_name}"
elif dep.startswith(":"):
label = f"//c-toxcore/{current_pkg}{dep}"
# Sanitize
sanitized = (label.replace("/", "_").replace(":", "_").replace(
".", "_").replace("-", "_").replace("@", "_"))
if sanitized.startswith("__"):
sanitized = sanitized[2:]
return sanitized
def main() -> None:
srcs: list[str] = []
for pkg in ("toxcore", ):
# TODO(iphydf): Why does this break everything?
# ctx = Context(pkg, "//c-toxcore/{pkg}")
ctx = Context(pkg, "")
with open(os.path.join(pkg, "BUILD.bazel"), "r") as fh:
packages = ["toxcore", "testing/support"]
for pkg in packages:
ctx = BuildContext(pkg)
build_file = os.path.join(pkg, "BUILD.bazel")
if not os.path.exists(build_file):
continue
with open(build_file, "r") as f:
exec(
fh.read(),
f.read(),
{
"load": ctx.bzl_load,
"exports_files": ctx.bzl_exports_files,
"cc_library": ctx.bzl_cc_library,
"cc_binary": ctx.bzl_cc_binary,
"cc_test": ctx.bzl_cc_binary,
"cc_test": ctx.bzl_cc_test,
"cc_fuzz_test": ctx.bzl_cc_fuzz_test,
"select": ctx.bzl_select,
"glob": ctx.bzl_glob,
@@ -175,36 +231,36 @@ def main() -> None:
},
)
with open("module.modulemap", "w") as fh:
fh.write(STD_MODULE)
for name, lib in LIBS.items():
fh.write(f'module "{ctx.pkg_prefix}:{name}"' + " {\n")
for hdr in lib["hdrs"]:
fh.write(f' header "{pkg}/{hdr}"\n')
fh.write(f" use std\n")
for dep in lib.get("deps", []):
fh.write(f' use "{dep}"\n')
fh.write("}\n")
with open("module.modulemap", "w") as f:
f.write(STD_MODULE)
for t in TARGETS:
f.write(f'module "{t.label}" {{\n')
for hdr in t.hdrs:
f.write(f' header "{os.path.join(t.package, hdr)}"\n')
f.write(" use std\n")
for dep in t.deps:
mod_name = resolve_module_name(dep, t.package)
# Export all dependencies to ensure visibility of types used in headers
f.write(f" use {mod_name}\n")
f.write(f" export {mod_name}\n")
f.write("}\n")
for name, lib in LIBS.items():
for src in lib.get("srcs", []):
MODS[os.path.join(pkg, src)] = name
srcs.extend(
os.path.join(pkg, src) # just within a package for now
for lib in LIBS.values() for src in lib.get("srcs", []))
# subprocess.run(["cat", "module.modulemap"], check=True)
for src in sorted(
set(srcs) - set([
# TODO(iphydf): Figure out what's wrong here.
"toxcore/crypto_core_test.cc",
"toxcore/group_announce_test.cc",
"toxcore/group_moderation_test.cc",
"toxcore/mono_time_test.cc",
"toxcore/network_test.cc",
"toxcore/ping_array_test.cc",
"toxcore/util_test.cc",
])):
# with open("module.modulemap", "r") as f:
# print(f.read(), file=sys.stderr)
src_to_module = {}
for t in TARGETS:
for src in t.srcs:
full_src = os.path.join(t.package, src)
src_to_module[full_src] = t.label
# Sort for deterministic output
all_srcs = sorted(src_to_module.keys())
for src in all_srcs:
print(f"Validating {src}", file=sys.stderr)
module_name = src_to_module[src]
subprocess.run(
[
"clang",
@@ -219,7 +275,8 @@ def main() -> None:
"-fmodules",
"-fmodules-strict-decluse",
"-fmodule-map-file=module.modulemap",
f"-fmodule-name={ctx.pkg_prefix}:{MODS[src]}",
f"-fmodule-name={module_name}",
"-I.",
src,
],
check=True,