Squashed 'external/toxcore/c-toxcore/' changes from c9cdae001..9ed2fa80d

9ed2fa80d fix(toxav): remove extra copy of video frame on encode
de30cf3ad docs: Add new file kinds, that should be useful to all clients.
d5b5e879d fix(DHT): Correct node skipping logic timed out nodes.
30e71fe97 refactor: Generate event dispatch functions and add tox_events_dispatch.
8fdbb0b50 style: Format parameter lists in event handlers.
d00dee12b refactor: Add warning logs when losing chat invites.
b144e8db1 feat: Add a way to look up a file number by ID.
849281ea0 feat: Add a way to fetch groups by chat ID.
a2c177396 refactor: Harden event system and improve type safety.
8f5caa656 refactor: Add MessagePack string support to bin_pack.
34e8d5ad5 chore: Add GitHub CodeQL workflow and local Docker runner.
f7b068010 refactor: Add nullability annotations to event headers.
788abe651 refactor(toxav): Use system allocator for mutexes.
2e4b423eb refactor: Use specific typedefs for public API arrays.
2baf34775 docs(toxav): update idle iteration interval see 679444751876fa3882a717772918ebdc8f083354
2f87ac67b feat: Add Event Loop abstraction (Ev).
f8dfc38d8 test: Fix data race in ToxScenario virtual_clock.
38313921e test(TCP): Add regression test for TCP priority queue integrity.
f94a50d9a refactor(toxav): Replace mutable_mutex with dynamically allocated mutex.
ad054511e refactor: Internalize DHT structs and add debug helpers.
8b467cc96 fix: Prevent potential integer overflow in group chat handshake.
4962bdbb8 test: Improve TCP simulation and add tests
5f0227093 refactor: Allow nullable data in group chat handlers.
e97b18ea9 chore: Improve Windows Docker support.
b14943bbd refactor: Move Logger out of Messenger into Tox.
dd3136250 cleanup: Apply nullability qualifiers to C++ codebase.
1849f70fc refactor: Extract low-level networking code to net and os_network.
8fec75421 refactor: Delete tox_random, align on rng and os_random.
a03ae8051 refactor: Delete tox_memory, align on mem and os_memory.
4c88fed2c refactor: Use `std::` prefixes more consistently in C++ code.
72452f2ae test: Add some more tests for onion and shared key cache.
d5a51b09a cleanup: Use tox_attributes.h in tox_private.h and install it.
b6f5b9fc5 test: Add some benchmarks for various high level things.
8a8d02785 test(support): Introduce threaded Tox runner and simulation barrier
d68d1d095 perf(toxav): optimize audio and video intermediate buffers by keeping them around
REVERT: c9cdae001 fix(toxav): remove extra copy of video frame on encode

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 9ed2fa80d582c714d6bdde6a7648220a92cddff8
This commit is contained in:
Green Sky
2026-02-01 14:26:52 +01:00
parent 565efa4f39
commit 9b36dd9d99
274 changed files with 11891 additions and 4292 deletions

View File

@@ -31,6 +31,8 @@ cc_binary(
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
],
)

View File

@@ -10,7 +10,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++17 \
-std=c++20 \
-Werror \
-Weverything \
-Wno-alloca \

View File

@@ -11,7 +11,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++17 \
-std=c++20 \
-fdiagnostics-color=always \
-Wall \
-Wextra \
@@ -20,6 +20,7 @@ run() {
-Wno-aggressive-loop-optimizations \
-Wno-float-conversion \
-Wno-format-signedness \
-Wno-inline \
-Wno-missing-field-initializers \
-Wno-nonnull-compare \
-Wno-padded \
@@ -46,7 +47,6 @@ run() {
-Wignored-attributes \
-Wignored-qualifiers \
-Winit-self \
-Winline \
-Wlarger-than=530000 \
-Wmaybe-uninitialized \
-Wmemset-transposed-args \

View File

@@ -25,6 +25,7 @@ cc_binary(
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
"@libconfig",

View File

@@ -290,8 +290,8 @@ int main(int argc, char *argv[])
IP ip;
ip_init(&ip, enable_ipv6);
const Tox_Memory *mem = os_memory();
const Tox_Random *rng = os_random();
const Memory *mem = os_memory();
const Random *rng = os_random();
const Network *ns = os_network();
Logger *logger = logger_new(mem);

View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -e
cmake -GNinja -B build -S .
cmake --build build --parallel "$(nproc)"

View File

@@ -0,0 +1,53 @@
# other/docker/codeql/codeql.Dockerfile
FROM toxchat/c-toxcore:sources AS sources
FROM ubuntu:22.04
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
cmake \
curl \
git \
libconfig-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
ninja-build \
pkg-config \
unzip \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install CodeQL
ARG CODEQL_VERSION=v2.23.9
RUN curl -L -o /tmp/codeql.zip https://github.com/github/codeql-cli-binaries/releases/download/${CODEQL_VERSION}/codeql-linux64.zip && \
unzip -q /tmp/codeql.zip -d /opt && \
rm /tmp/codeql.zip
ENV PATH="/opt/codeql:$PATH"
RUN groupadd -r -g 1000 builder \
&& useradd -m --no-log-init -r -g builder -u 1000 builder
WORKDIR /home/builder/c-toxcore
# Copy sources
COPY --chown=builder:builder --from=sources /src/ /home/builder/c-toxcore/
# Pre-create build directory
RUN mkdir -p build codeql-db && chown builder:builder codeql-db build
# Copy scripts
COPY --chown=builder:builder other/docker/codeql/build.sh .
COPY --chown=builder:builder other/docker/codeql/run-analysis.sh .
RUN chmod +x build.sh run-analysis.sh
USER builder
# Download standard queries as builder
RUN codeql pack download codeql/cpp-queries
CMD ["./run-analysis.sh"]

15
other/docker/codeql/run Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -eux
BUILD=codeql
# Ensure the sources image is built
other/docker/sources/build.sh
# Build the codeql image
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .
# Run the container
echo "Running CodeQL analysis..."
docker run --rm "toxchat/c-toxcore:$BUILD"

View File

@@ -0,0 +1,8 @@
#!/bin/bash
set -e
echo "Creating CodeQL Database..."
codeql database create codeql-db --language=cpp --overwrite --command="./build.sh"
echo "Analyzing..."
codeql database analyze codeql-db codeql/cpp-queries:codeql-suites/cpp-security-and-quality.qls --format=csv --output=codeql-db/results.csv
echo "Analysis complete. Results in codeql-db/results.csv"
cat codeql-db/results.csv

View File

@@ -1,8 +0,0 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "sodium",
testonly = True,
srcs = ["sodium.c"],
deps = ["@libsodium"],
)

View File

@@ -1,43 +0,0 @@
{
"ana": {
"activated": [
"base","mallocWrapper","escape","mutex","mutexEvents","access","assert","expRelation"
],
"arrayoob": true,
"wp": true,
"apron": {
"strengthening": true
},
"base": {
"structs" : {
"domain" : "combined-sk"
},
"arrays": {
"domain": "partitioned"
}
},
"malloc": {
"wrappers": [
"mem_balloc",
"mem_alloc",
"mem_valloc",
"mem_vrealloc"
]
}
},
"warn": {
"behavior": false,
"call": false,
"integer": true,
"float": false,
"race": false,
"deadcode": false,
"unsound": false,
"imprecise": false,
"success": false,
"unknown": false
},
"exp": {
"earlyglobs": true
}
}

View File

@@ -1,21 +0,0 @@
FROM toxchat/c-toxcore:sources AS sources
FROM ghcr.io/goblint/analyzer:2.5.0
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
libsodium-dev \
tcc \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /work
COPY --from=sources /src/ /work/
COPY other/deploy/single-file/make_single_file /work/
RUN ./make_single_file -core auto_tests/tox_new_test.c other/docker/goblint/sodium.c > analysis.c
# Try compiling+linking just to make sure we have all the fake functions.
RUN tcc analysis.c
COPY other/docker/goblint/analysis.json /work/other/docker/goblint/
RUN /opt/goblint/analyzer/bin/goblint --conf /work/other/docker/goblint/analysis.json analysis.c

View File

@@ -1,6 +0,0 @@
#!/bin/sh
set -eux
BUILD=goblint
other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -1,123 +0,0 @@
#include <sodium.h>
#include <string.h>
int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, const unsigned char *seed)
{
memset(pk, 0, 32);
memset(sk, 0, 32);
return 0;
}
int crypto_sign_ed25519_pk_to_curve25519(unsigned char *curve25519_pk,
const unsigned char *ed25519_pk)
{
memset(curve25519_pk, 0, 32);
return 0;
}
int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk,
const unsigned char *ed25519_sk)
{
memset(curve25519_sk, 0, 32);
return 0;
}
void sodium_memzero(void *const pnt, const size_t len)
{
memset(pnt, 0, len);
}
int sodium_mlock(void *const addr, const size_t len)
{
return 0;
}
int sodium_munlock(void *const addr, const size_t len)
{
return 0;
}
int crypto_verify_32(const unsigned char *x, const unsigned char *y)
{
return memcmp(x, y, 32);
}
int crypto_verify_64(const unsigned char *x, const unsigned char *y)
{
return memcmp(x, y, 64);
}
int crypto_sign_detached(unsigned char *sig, unsigned long long *siglen_p,
const unsigned char *m, unsigned long long mlen,
const unsigned char *sk)
{
return 0;
}
int crypto_sign_verify_detached(const unsigned char *sig,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk)
{
return 0;
}
int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
const unsigned char *sk)
{
memset(k, 0, 32);
return 0;
}
int crypto_box_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k)
{
memset(c, 0, 32);
return 0;
}
int crypto_box_open_afternm(unsigned char *m, const unsigned char *c,
unsigned long long clen, const unsigned char *n,
const unsigned char *k)
{
return 0;
}
int crypto_scalarmult_curve25519_base(unsigned char *q,
const unsigned char *n)
{
memset(q, 0, 32);
return 0;
}
int crypto_auth(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
return 0;
}
int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
return 0;
}
int crypto_hash_sha256(unsigned char *out, const unsigned char *in,
unsigned long long inlen)
{
return 0;
}
int crypto_hash_sha512(unsigned char *out, const unsigned char *in,
unsigned long long inlen)
{
return 0;
}
int crypto_pwhash_scryptsalsa208sha256(unsigned char *const out,
unsigned long long outlen,
const char *const passwd,
unsigned long long passwdlen,
const unsigned char *const salt,
unsigned long long opslimit,
size_t memlimit)
{
memset(out, 0, outlen);
return 0;
}
void randombytes(unsigned char *const buf, const unsigned long long buf_len)
{
memset(buf, 0, buf_len);
}
uint32_t randombytes_uniform(const uint32_t upper_bound)
{
return upper_bound;
}
int sodium_init(void)
{
return 0;
}

View File

@@ -6,92 +6,79 @@ import sys
from dataclasses import dataclass
from dataclasses import field
from typing import Any
from typing import Dict
from typing import List
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"
textual header "/usr/include/errno.h"
textual header "/usr/include/fortify/stdio.h"
textual header "/usr/include/fortify/string.h"
textual header "/usr/include/fortify/unistd.h"
textual header "/usr/include/limits.h"
textual header "/usr/include/stdarg.h"
textual header "/usr/include/stdbool.h"
textual header "/usr/include/stddef.h"
textual header "/usr/include/stdint.h"
textual header "/usr/include/sys/time.h"
textual header "/usr/include/sys/types.h"
textual header "/usr/include/time.h"
# We no longer define 'std' or 'musl' manually.
# We rely on -fimplicit-module-maps to find the system-provided ones.
# We only define modules for our project and third-party libraries.
EXTRA_MODULES = """
module "_system" [system] {
header "/usr/include/stdint.h"
header "/usr/include/stdbool.h"
header "/usr/include/stddef.h"
header "/usr/include/stdio.h"
header "/usr/include/stdlib.h"
header "/usr/include/string.h"
header "/usr/include/inttypes.h"
header "/usr/include/limits.h"
header "/usr/include/errno.h"
header "/usr/include/unistd.h"
header "/usr/include/fcntl.h"
header "/usr/include/time.h"
header "/usr/include/assert.h"
header "/usr/include/pthread.h"
header "/usr/include/arpa/inet.h"
header "/usr/include/netdb.h"
header "/usr/include/netinet/in.h"
header "/usr/include/sys/types.h"
header "/usr/include/sys/stat.h"
header "/usr/include/sys/time.h"
header "/usr/include/sys/socket.h"
header "/usr/include/sys/epoll.h"
header "/usr/include/sys/ioctl.h"
header "/usr/include/sys/resource.h"
header "/usr/include/sys/uio.h"
header "/usr/include/sys/mman.h"
header "/usr/include/sys/wait.h"
header "/usr/include/sys/select.h"
header "/usr/include/poll.h"
header "/usr/include/sched.h"
header "/usr/include/signal.h"
header "/usr/include/ctype.h"
header "/usr/include/alloca.h"
header "/usr/include/malloc.h"
header "/usr/include/dirent.h"
export *
}
module "c_toxcore_third_party_cmp" {
header "third_party/cmp/cmp.h"
use std
module "_libsodium" [system] {
header "/usr/include/sodium.h"
use _system
export *
}
module "c_toxcore_toxencryptsave_defines" {
header "toxencryptsave/defines.h"
module "_benchmark" [system] {
header "/usr/include/benchmark/benchmark.h"
use _system
export *
}
module "_benchmark" {
textual header "/usr/include/benchmark/benchmark.h"
use std
module "_com_google_googletest___gtest" [system] {
header "/usr/include/gtest/gtest.h"
header "/usr/include/gmock/gmock.h"
use _system
export *
}
module "_com_google_googletest___gtest" {
textual header "/usr/include/gmock/gmock.h"
textual header "/usr/include/gtest/gtest.h"
use std
module "_com_google_googletest___gtest_main" [system] {
use _com_google_googletest___gtest
export *
}
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 "_opus" [system] {
header "/usr/include/opus/opus.h"
use _system
export *
}
module "_libsodium" {
textual header "/usr/include/sodium.h"
}
module "_pthread" {
textual header "/usr/include/pthread.h"
}
module "_psocket" {
textual header "/usr/include/arpa/inet.h"
textual header "/usr/include/fcntl.h"
textual header "/usr/include/fortify/sys/socket.h"
textual header "/usr/include/linux/if.h"
textual header "/usr/include/netdb.h"
textual header "/usr/include/netinet/in.h"
textual header "/usr/include/sys/epoll.h"
textual header "/usr/include/sys/ioctl.h"
module "_libvpx" [system] {
header "/usr/include/vpx/vpx_encoder.h"
header "/usr/include/vpx/vpx_decoder.h"
use _system
export *
}
"""
@@ -100,24 +87,16 @@ module "_psocket" {
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)
srcs: list[str] = field(default_factory=list)
hdrs: list[str] = field(default_factory=list)
deps: list[str] = field(default_factory=list)
@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
return f"c-toxcore/{self.package}:{self.name}"
TARGETS: List[Target] = []
TARGETS: list[Target] = []
class BuildContext:
@@ -140,16 +119,29 @@ class BuildContext:
def bzl_cc_fuzz_test(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_select(self, selector: Dict[str, List[str]]) -> List[str]:
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]:
def bzl_glob(self,
include: list[str],
exclude: list[str] | None = None,
**kwargs: Any) -> list[str]:
results = []
for pattern in include:
full_pattern = os.path.join(self.package, pattern)
files = glob.glob(full_pattern)
files = glob.glob(full_pattern, recursive=True)
results.extend([os.path.relpath(f, self.package) for f in files])
if exclude:
excluded_files = set()
for pattern in exclude:
full_pattern = os.path.join(self.package, pattern)
files = glob.glob(full_pattern, recursive=True)
excluded_files.update(
[os.path.relpath(f, self.package) for f in files])
results = [f for f in results if f not in excluded_files]
return results
def _add_target(self, name: str, srcs: Any, hdrs: Any, deps: Any) -> None:
@@ -174,6 +166,9 @@ class BuildContext:
**kwargs: Any) -> None:
self._add_target(name, srcs, hdrs, deps)
def bzl_project(self, *args: Any, **kwargs: Any) -> None:
pass
def bzl_cc_test(self,
name: str,
srcs: Any = (),
@@ -184,29 +179,44 @@ class BuildContext:
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}"
if dep in ["@psocket", "@pthread"]:
return "_system"
if dep == "@libsodium":
return "_libsodium"
if dep == "@benchmark":
return "_benchmark"
if dep == "@com_google_googletest//:gtest":
return "_com_google_googletest___gtest"
if dep == "@com_google_googletest//:gtest_main":
return "_com_google_googletest___gtest_main"
if dep == "@opus":
return "_opus"
if dep == "@libvpx":
return "_libvpx"
# Sanitize
sanitized = (label.replace("/", "_").replace(":", "_").replace(
".", "_").replace("-", "_").replace("@", "_"))
if sanitized.startswith("__"):
sanitized = sanitized[2:]
return sanitized
# Resolve to canonical label first
if dep.startswith("@"):
return dep
if dep.startswith("//"):
label = dep[2:]
if ":" in label:
return label
pkg_name = os.path.basename(label)
return f"{label}:{pkg_name}"
if dep.startswith(":"):
return f"c-toxcore/{current_pkg}{dep}"
return dep
def main() -> None:
packages = ["toxcore", "testing/support"]
packages = []
for root, dirs, files in os.walk("."):
if "BUILD.bazel" in files:
pkg = os.path.relpath(root, ".")
if pkg == ".":
pkg = ""
packages.append(pkg)
for pkg in packages:
ctx = BuildContext(pkg)
@@ -214,39 +224,53 @@ def main() -> None:
if not os.path.exists(build_file):
continue
# Use a defaultdict to handle any unknown functions as no-ops
from collections import defaultdict
env: dict[str, Any] = defaultdict(lambda: lambda *args, **kwargs: None)
env.update({
"load": ctx.bzl_load,
"exports_files": ctx.bzl_exports_files,
"cc_library": ctx.bzl_cc_library,
"no_undefined_cc_library": ctx.bzl_cc_library,
"cc_binary": 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,
"alias": ctx.bzl_alias,
"sh_library": ctx.bzl_sh_library,
"project": ctx.bzl_project,
})
with open(build_file, "r") as f:
exec(
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_test,
"cc_fuzz_test": ctx.bzl_cc_fuzz_test,
"select": ctx.bzl_select,
"glob": ctx.bzl_glob,
"alias": ctx.bzl_alias,
"sh_library": ctx.bzl_sh_library,
},
)
exec(f.read(), env)
with open("module.modulemap", "w") as f:
f.write(STD_MODULE)
f.write(EXTRA_MODULES)
for t in TARGETS:
f.write(f'module "{t.label}" {{\n')
for hdr in t.hdrs:
# Proper modular header
f.write(f' header "{os.path.join(t.package, hdr)}"\n')
f.write(" use std\n")
# Use all dependencies
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(f' use "{mod_name}"\n')
# Re-export everything we use to match standard C++ transitive include behavior.
f.write(" export *\n")
# Basic system modules used everywhere
f.write(' use "_system"\n')
f.write("}\n")
# with open("module.modulemap", "r") as f:
# print(f.read(), file=sys.stderr)
if "--print-modulemap" in sys.argv:
with open("module.modulemap", "r") as f:
print(f.read())
return
src_to_module = {}
for t in TARGETS:
@@ -254,29 +278,44 @@ def main() -> None:
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())
all_srcs = [src for src in all_srcs if src.endswith(".c")]
os.makedirs("/tmp/clang-modules", exist_ok=True)
for src in all_srcs:
print(f"Validating {src}", file=sys.stderr)
module_name = src_to_module[src]
lang = "-xc" if src.endswith(".c") else "-xc++"
std = "-std=c11" if src.endswith(".c") else "-std=c++23"
subprocess.run(
[
"clang",
"-fsyntax-only",
"-xc++",
lang,
"-stdlib=libc++" if lang == "-xc++" else "-nostdinc++",
"-Wall",
"-Werror",
"-Wno-missing-braces",
"-DTCP_SERVER_USE_EPOLL",
"-std=c++23",
"-D_XOPEN_SOURCE=600",
"-D_GNU_SOURCE",
std,
"-fdiagnostics-color=always",
"-fmodules",
"-fmodules-strict-decluse",
"-Xclang",
"-fmodules-local-submodule-visibility",
"-fmodules-decluse",
"-Xclang",
"-fno-modules-error-recovery",
"-fno-implicit-module-maps",
"-fno-builtin-module-map",
"-fmodules-cache-path=/tmp/clang-modules",
"-fmodule-map-file=module.modulemap",
f"-fmodule-name={module_name}",
"-I.",
"-I/usr/include/opus",
src,
],
check=True,

View File

@@ -1,10 +1,13 @@
FROM alpine:3.21.0
FROM alpine:3.23.0
RUN ["apk", "add", "--no-cache", \
"bash", \
"benchmark-dev", \
"clang", \
"gmock", \
"gtest-dev", \
"libc++-dev", \
"libc++-static", \
"libconfig-dev", \
"libsodium-dev", \
"libvpx-dev", \

View File

@@ -114,6 +114,21 @@ build() {
make install
cd ..
echo
echo "=== Building GTest $VERSION_GTEST $ARCH ==="
curl "${CURL_OPTIONS[@]}" "https://github.com/google/googletest/archive/refs/tags/v$VERSION_GTEST.tar.gz" -o "googletest-$VERSION_GTEST.tar.gz"
check_sha256 "65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c" "googletest-$VERSION_GTEST.tar.gz"
tar -xf "googletest-$VERSION_GTEST.tar.gz"
cd "googletest-$VERSION_GTEST"
cmake \
-DCMAKE_TOOLCHAIN_FILE=../windows_toolchain.cmake \
-DCMAKE_INSTALL_PREFIX="$PREFIX_DIR" \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-S . -B build
cmake --build build --target install --parallel "$(nproc)"
cd ..
rm -rf /tmp/*
}

View File

@@ -0,0 +1 @@
!other/docker/windows/*.sh

26
other/docker/windows/run Executable file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
DOCKERFLAGS=(--build-arg SUPPORT_TEST=true)
. "$(cd "$(dirname "${BASH_SOURCE[0]}")/../sources" && pwd)/run.sh"
# Create a temporary directory for the source to workaround potential bind mount issues
# (e.g. FUSE/SSHFS filesystems or Docker daemon visibility issues).
TEMP_SRC=$(mktemp -d -t toxcore-src-XXXXXX)
cleanup() {
rm -rf "$TEMP_SRC"
}
trap cleanup EXIT
echo "Copying source to temporary directory $TEMP_SRC..."
# Exclude .git and build artifacts to speed up copy
rsync -a --exclude .git --exclude _build . "$TEMP_SRC/"
docker run \
-e ENABLE_TEST=true \
-e EXTRA_CMAKE_FLAGS=-DUNITTEST=ON \
-v "$TEMP_SRC:/toxcore" \
-v /tmp/c-toxcore-build:/prefix \
-t \
--rm \
toxchat/c-toxcore:windows

View File

@@ -7,6 +7,7 @@ FROM debian:trixie-slim
ARG VERSION_OPUS=1.4 \
VERSION_SODIUM=1.0.19 \
VERSION_VPX=1.14.0 \
VERSION_GTEST=1.17.0 \
ENABLE_HASH_VERIFICATION=true \
\
SUPPORT_TEST=false \
@@ -21,14 +22,14 @@ ENV SUPPORT_TEST=${SUPPORT_TEST} \
CROSS_COMPILE=${CROSS_COMPILE}
WORKDIR /work
COPY check_sha256.sh .
COPY get_packages.sh .
COPY other/docker/windows/check_sha256.sh .
COPY other/docker/windows/get_packages.sh .
RUN ./get_packages.sh
COPY build_dependencies.sh .
COPY other/docker/windows/build_dependencies.sh .
RUN ./build_dependencies.sh
COPY build_toxcore.sh .
COPY other/docker/windows/build_toxcore.sh .
ENV ENABLE_TEST=false \
ALLOW_TEST_FAILURE=false \

View File

@@ -0,0 +1,24 @@
# ===== common =====
# Ignore everything ...
**/*
# ... except sources
!**/*.[ch]
!**/*.cc
!**/*.hh
!CHANGELOG.md
!LICENSE
!README.md
!auto_tests/data/*
!other/bootstrap_daemon/bash-completion/**
!other/bootstrap_daemon/tox-bootstrapd.*
!other/proxy/*.mod
!other/proxy/*.sum
!other/proxy/*.go
# ... and CMake build files (used by most builds).
!**/CMakeLists.txt
!.github/scripts/flags*.sh
!cmake/*.cmake
!other/pkgconfig/*
!other/rpm/*
!so.version
!other/docker/windows/*.sh

View File

@@ -4,13 +4,14 @@
// this file can be used to generate event.c files
// requires c++17
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <variant>
#include <map>
#include <string>
#include <variant>
#include <vector>
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
@@ -168,6 +169,7 @@ std::string zero_initializer_for_type(const std::string& type) {
void generate_event_impl(const std::string& event_name, const std::vector<EventType>& event_types) {
const std::string event_name_l = str_tolower(event_name);
const std::string event_name_u = str_toupper(event_name);
std::string file_name = output_folder + "/" + event_name_l + ".c";
std::ofstream f(file_name);
@@ -218,11 +220,20 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
#include "../tox.h"
#include "../tox_event.h"
#include "../tox_events.h")";
if (need_tox_unpack_h) {
f << R"(
#include "../tox_pack.h")";
}
f << R"(
#include "../tox_struct.h")";
if (need_tox_unpack_h) {
f << R"(
#include "../tox_pack.h"
#include "../tox_unpack.h")";
}
f << R"(
/*****************************************************
@@ -242,7 +253,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " " << t.type << " " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
f << " " << t.type_c_arg << " *_Nullable " << t.name_data << ";\n";
f << " " << "uint32_t" << " " << t.name_length << ";\n";
},
[&](const EventTypeByteArray& t) {
@@ -279,7 +290,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " " << t.type << " " << t.name << ")\n";
},
[&](const EventTypeByteRange& t) {
f << "\n const Memory *_Nonnull mem, const uint8_t *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
f << "\n const Memory *_Nonnull mem, const " << t.type_c_arg << " *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
},
[&](const EventTypeByteArray& t) {
f << " const uint8_t " << t.name << "[" << t.length_constant << "])\n";
@@ -294,28 +305,41 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " " << event_name_l << "->" << t.name << " = " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << " if (" << event_name_l << "->" << t.name_data << " != nullptr) {\n";
f << " mem_delete(mem, " << event_name_l << "->" << t.name_data << ");\n";
f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n";
f << " " << event_name_l << "->" << t.name_length << " = 0;\n";
f << " }\n\n";
f << " if (" << t.name_data << " == nullptr) {\n";
f << " assert(" << t.name_length << " == 0);\n";
f << " return true;\n }\n\n";
f << " if (" << event_name_l << "->" << t.name_data << " != nullptr) {\n"
" mem_delete(mem, " << event_name_l << "->" << t.name_data << ");\n"
" " << event_name_l << "->" << t.name_data << " = nullptr;\n"
" " << event_name_l << "->" << t.name_length << " = 0;\n"
" }\n\n"
" if (" << t.name_data << " == nullptr) {\n"
" assert(" << t.name_length << " == 0);\n"
" return true;\n"
" }\n\n";
if (t.null_terminated) {
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << " + 1);\n\n";
f << " if (" << t.name_length << " == UINT32_MAX) {\n"
" return false;\n"
" }\n\n";
} else {
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << ");\n\n";
f << " if (" << t.name_length << " == 0) {\n"
" " << event_name_l << "->" << t.name_data << " = nullptr;\n"
" " << event_name_l << "->" << t.name_length << " = 0;\n"
" return true;\n"
" }\n\n";
}
f << " if (" << t.name_data << "_copy == nullptr) {\n";
f << " return false;\n }\n\n";
f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n";
f << " " << t.type_c_arg << " *" << t.name_data << "_copy = (" << t.type_c_arg << " *)mem_balloc(mem, " << t.name_length << (t.null_terminated ? " + 1" : "") << ");\n\n"
" if (" << t.name_data << "_copy == nullptr) {\n"
" return false;\n"
" }\n\n"
" memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n";
if (t.null_terminated) {
f << " " << t.name_data << "_copy[" << t.name_length << "] = 0;\n";
}
f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n";
f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n";
f << " return true;\n";
f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n"
" " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n"
" return true;\n";
},
[&](const EventTypeByteArray& t) {
f << " memcpy(" << event_name_l << "->" << t.name << ", " << t.name << ", " << t.length_constant << ");\n";
@@ -342,7 +366,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
f << "const " << t.type_c_arg << " *tox_event_" << event_name_l << "_get_" << t.name_data;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
@@ -435,7 +459,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
}
},
[&](const EventTypeByteRange& t) {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
if (t.type_c_arg == "char") {
f << "bin_pack_str(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
} else {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
}
},
[&](const EventTypeByteArray& t) {
f << "bin_pack_bin(bp, event->" << t.name << ", " << t.length_constant << ")";
@@ -471,7 +499,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
}
},
[&](const EventTypeByteRange& t) {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
if (t.type_c_arg == "char") {
f << "bin_unpack_str(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
} else {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
}
},
[&](const EventTypeByteArray& t) {
f << "bin_unpack_bin_fixed(bu, event->" << t.name << ", " << t.length_constant << ")";
@@ -493,7 +525,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
)";
f << "const Tox_Event_" << event_name << " *tox_event_get_" << event_name_l << "(const Tox_Event *event)\n{\n";
f << " return event->type == TOX_EVENT_" << str_toupper(event_name) << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
f << " return event->type == TOX_EVENT_" << event_name_u << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
// new
f << "Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_new(const Memory *mem)\n{\n";
@@ -506,7 +538,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
// free
f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
f << " if (" << event_name_l << " != nullptr) {\n";
f << " tox_event_" << event_name_l << "_destruct((Tox_Event_" << event_name << " * _Nonnull)" << event_name_l << ", mem);\n }\n";
f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n";
f << " mem_delete(mem, " << event_name_l << ");\n}\n\n";
// add
@@ -515,7 +547,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " if (" << event_name_l << " == nullptr) {\n";
f << " return nullptr;\n }\n\n";
f << " Tox_Event event;\n";
f << " event.type = TOX_EVENT_" << str_toupper(event_name) << ";\n";
f << " event.type = TOX_EVENT_" << event_name_u << ";\n";
f << " event.data." << event_name_l << " = " << event_name_l << ";\n\n";
f << " if (!tox_events_add(events, &event)) {\n";
f << " tox_event_" << event_name_l << "_free(" << event_name_l << ", mem);\n";
@@ -553,16 +585,17 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " Tox *tox";
for (const auto& t : event_types) {
f << ",\n ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << ", " << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
f << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
},
[&](const EventTypeByteRange& t) {
f << ", const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
f << "const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
},
[&](const EventTypeByteArray& t) {
f << ", const uint8_t *" << t.name;
f << "const uint8_t *" << t.name;
}
},
t
@@ -581,11 +614,10 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " tox_event_" << event_name_l << "_set_" << t.name << "(" << event_name_l << ", " << t.name << ");\n";
},
[&](const EventTypeByteRange& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", state->mem, ";
if (t.type_c_arg != "uint8_t") {
f << "(const uint8_t *)";
}
f << t.name_data << ", " << t.name_length_cb << ");\n";
f << " if (!tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", state->mem, ";
f << t.name_data << ", " << t.name_length_cb << ")) {\n";
f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n";
f << " }\n";
},
[&](const EventTypeByteArray& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name << "(" << event_name_l << ", " << t.name << ");\n";
@@ -595,6 +627,45 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
);
}
f << "}\n";
f << "\nvoid tox_events_handle_" << event_name_l << "_dispatch(Tox *tox, const Tox_Event_" << event_name << " *event, void *user_data)\n{\n";
if (event_name_l == "friend_lossy_packet" || event_name_l == "friend_lossless_packet") {
f << " if (event->data_length == 0 || tox->" << event_name_l << "_callback_per_pktid[event->data[0]] == nullptr) {\n";
f << " return;\n";
f << " }\n\n";
f << " tox_unlock(tox);\n";
f << " tox->" << event_name_l << "_callback_per_pktid[event->data[0]](tox, event->friend_number, event->data, event->data_length, user_data);\n";
f << " tox_lock(tox);\n";
} else {
f << " if (tox->" << event_name_l << "_callback == nullptr) {\n return;\n }\n\n";
f << " tox_unlock(tox);\n";
f << " tox->" << event_name_l << "_callback(tox, ";
bool first_arg = true;
for (const auto& t : event_types) {
if (!first_arg) f << ", ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << "event->" << t.name;
},
[&](const EventTypeByteRange& t) {
if (t.type_c_arg != "uint8_t") {
f << "(const " << t.type_c_arg << " *)";
}
f << "event->" << t.name_data << ", event->" << t.name_length;
},
[&](const EventTypeByteArray& t) {
f << "event->" << t.name;
}
},
t
);
first_arg = false;
}
f << ", user_data);\n";
f << " tox_lock(tox);\n";
}
f << "}\n";
}
// c++ generate_event_c.cpp -std=c++17 && ./a.out Friend_Lossy_Packet && diff --color ../../toxcore/events/friend_lossy_packet.c out/friend_lossy_packet.c

View File

@@ -1,6 +1,6 @@
#!/bin/sh
# When editing, make sure to update /other/docker/windows/Dockerfile and
# When editing, make sure to update /other/docker/windows/windows.Dockerfile and
# INSTALL.md to match.
export VERSION_OPUS="1.4"