Skip to content

Commit

Permalink
cleanup: Use Bazel modules to enforce proper #include hygiene.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 30, 2024
1 parent 1710a0d commit 511bfe3
Show file tree
Hide file tree
Showing 128 changed files with 432 additions and 21 deletions.
2 changes: 0 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@ CheckOptions:
value: true
- key: concurrency-mt-unsafe.FunctionSet
value: posix
- key: misc-include-cleaner.IgnoreHeaders
value: "pthread.h;stdbool.h;stddef.h;stdint.;stdint.h;stdint...;cstdint;sodium.*;sys/.*;unistd.h;opus.*;vpx.*;attributes.h;tox_struct.h"
- key: readability-function-cognitive-complexity.Threshold
value: 153 # TODO(iphydf): Decrease. tox_new is the highest at the moment.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
analysis:
strategy:
matrix:
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, rpm, tcc, tokstyle]
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, modules, rpm, tcc, tokstyle]
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
Expand Down
8 changes: 6 additions & 2 deletions other/analysis/run-clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ CHECKS="$CHECKS,-cert-dcl03-c"
CHECKS="$CHECKS,-hicpp-static-assert"
CHECKS="$CHECKS,-misc-static-assert"

# Doesn't consider use of preprocessor macros as needing a header, breaking
# struct definitions that depend on size macros from e.g. crypto_core.h.
CHECKS="$CHECKS,-misc-include-cleaner"

set -eux

# TODO(iphydf): Add toxav.
Expand All @@ -154,7 +158,7 @@ copy_files() {
find "${DIRS[@]}" \
-maxdepth 1 -type d -exec mkdir -p "$1/{}" \;
find "${DIRS[@]}" \
-maxdepth 1 -name "*.c" -exec cp "{}" "$1/{}" \;
-maxdepth 1 -name "*.[ch]" -exec cp "{}" "$1/{}" \;
}

run() {
Expand All @@ -166,7 +170,7 @@ run() {
ls .clang-tidy
copy_files a
if ! find "${DIRS[@]}" \
-maxdepth 1 -name "*.c" -print0 \
-maxdepth 1 -name "*.[ch]" -print0 \
| xargs -0 -n15 -P"$(nproc)" clang-tidy \
-p="$PWD/_build" \
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e9e6e7baafe3cfe9b212ffb4f1ea8a8b48e2dc1ee4c07c6b1b0b04893b3b6464 /usr/local/bin/tox-bootstrapd
af58a125e5c80d7a19bc7f32868c1edfdf80f366e3bf778728961a50ce63ee26 /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <stdbool.h>

#include "../../../toxcore/ccompat.h"
#include "../../../toxcore/attributes.h"

typedef enum LOG_BACKEND {
LOG_BACKEND_STDOUT,
Expand Down
7 changes: 4 additions & 3 deletions other/bootstrap_daemon/src/log_backend_stdout.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H

#include "log.h"

#include <stdarg.h>

#include "../../../toxcore/attributes.h"
#include "log.h"

void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0);

#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H
7 changes: 4 additions & 3 deletions other/bootstrap_daemon/src/log_backend_syslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H

#include "log.h"

#include <stdarg.h>

#include "../../../toxcore/attributes.h"
#include "log.h"

void log_backend_syslog_open(void);
void log_backend_syslog_close(void);
void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0);

#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H
93 changes: 93 additions & 0 deletions other/docker/modules/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python3
import glob as py_glob
import os
import subprocess
import sys
from typing import Optional

LIBS = {}


def load(bzl: str, *syms: str) -> None:
pass


def exports_files(srcs: list[str],
visibility: Optional[list[str]] = None) -> None:
pass


def cc_library(name: str, **kwargs: list[str]) -> None:
LIBS[name] = kwargs


def cc_test(name: str, **kwargs: list[str]) -> None:
pass


def cc_fuzz_test(name: str, **kwargs: list[str]) -> None:
pass


def select(selector: dict[str, list[str]]) -> list[str]:
return selector["//tools/config:linux"]


def glob(include: list[str]) -> list[str]:
return [
f[len("toxcore/"):] for p in include
for f in py_glob.glob(os.path.join("toxcore", p))
]


def alias(name: str, actual: str, visibility: list[str]) -> None:
pass


def sh_library(name: str, **kwargs: list[str]) -> None:
pass


def main() -> None:
with open("toxcore/BUILD.bazel", "r") as fh:
exec(fh.read())

with open("module.modulemap", "w") as fh:
for name, lib in LIBS.items():
fh.write("module " + name + " {\n")
for hdr in lib["hdrs"]:
fh.write(f' header "toxcore/{hdr}"\n')
for dep in lib.get("deps", []):
if dep[0] == ":":
fh.write(f" use {dep[1:]}\n")
fh.write("}\n")

srcs = sorted(
set(
os.path.join("toxcore", src) for lib in LIBS.values()
for src in lib.get("srcs", [])))
for src in srcs:
print(f"Validating {src}", file=sys.stderr)
subprocess.run(
[
"clang",
"-xc++",
"-fsyntax-only",
"-Wall",
"-Werror",
"-Wno-missing-braces",
"-std=c++23",
"-fdiagnostics-color=always",
"-fmodules",
# TODO(iphydf): Fix all the other errors.
# "-fmodules-strict-decluse",
"-fmodules-decluse",
"-fmodule-map-file=module.modulemap",
src,
],
check=True,
)


if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions other/docker/modules/modules.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM toxchat/c-toxcore:sources AS sources
FROM alpine:3.19.0

RUN ["apk", "add", "--no-cache", \
"bash", \
"clang", \
"libconfig-dev", \
"libsodium-dev", \
"libvpx-dev", \
"linux-headers", \
"opus-dev", \
"pkgconfig", \
"python3"]

WORKDIR /work
COPY --from=sources /src/ /work/

COPY toxcore/BUILD.bazel /work/toxcore/
COPY other/docker/modules/check /work/other/docker/modules/
RUN ["other/docker/modules/check"]
6 changes: 6 additions & 0 deletions other/docker/modules/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -eux
BUILD=modules
other/docker/sources/build
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .
1 change: 1 addition & 0 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
}
f << R"(
#include "../attributes.h"
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
Expand Down
Loading

0 comments on commit 511bfe3

Please sign in to comment.