diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..daf13da --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Chromium +IndentWidth: 4 +ColumnLimit: 100 diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..1d1d234 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,12 @@ +Checks: > + -*, + clang-analyzer-*, + bugprone-*, + performance-*, + readability-*, + modernize-*, + -modernize-use-trailing-return-type, + -readability-magic-numbers, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling +HeaderFilterRegex: 'src/.*' +WarningsAsErrors: '' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7b89c6f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: CI + +env: + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true + +on: + push: + branches: [dev, main] + pull_request: + branches: ["**"] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v4 + + - name: Build + run: make + + - name: Run + run: ./hello diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..5c18b29 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,46 @@ +name: Lint + +env: + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true + +on: + push: + branches: [dev, main] + pull_request: + branches: ["**"] + +jobs: + lint: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Install clang tools + run: | + sudo apt-get update + sudo apt-get install -y clang-format clang-tidy + + - name: Static analysis (clang-tidy) + run: | + find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \ + | xargs -I {} clang-tidy {} -- -Isrc + + - name: Check formatting (clang-format) + run: | + find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \ + | xargs clang-format -i + + - name: Commit if changes + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add -A + if ! git diff --cached --quiet; then + git commit -m "chore: auto-format code [skip ci]" + git push + fi diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml new file mode 100644 index 0000000..0773e39 --- /dev/null +++ b/.github/workflows/pre-commit.yaml @@ -0,0 +1,50 @@ +name: pre-commit + +on: + push: + branches: [dev, main] + pull_request: + branches: [dev, main] + workflow_call: + inputs: + working-directory: + type: string + required: false + default: '.' + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + id: setup-python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + cache: 'pip' + + - uses: actions/cache@v4 + with: + path: ~/.cache/pre-commit + key: > + ${{ format('pre-commit-{0}-{1}', + steps.setup-python.outputs.python-version, + hashFiles('.pre-commit-config.yaml') + ) }} + + - name: Install pre-commit + run: | + pip install --upgrade pip + pip install pre-commit + pre-commit install + + - name: Run pre-commit hooks + working-directory: ${{ inputs.working-directory }} + run: | + git ls-files | xargs pre-commit run \ + --show-diff-on-failure \ + --color=always \ + --files diff --git a/.gitignore b/.gitignore index 845cda6..18deb2c 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ dkms.conf # debug information files *.dwo +hello diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..0690d83 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,22 @@ +repos: +- repo: https://github.com/google/yapf.git + rev: v0.43.0 + hooks: + - id: yapf + additional_dependencies: [toml] +- repo: https://github.com/pre-commit/pre-commit-hooks.git + rev: v6.0.0 + hooks: + - id: trailing-whitespace + exclude: \.fits + - id: end-of-file-fixer + exclude: \.fits + - id: check-yaml + - id: debug-statements + - id: name-tests-test + exclude: conftestaux/ + - id: requirements-txt-fixer +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: v22.1.1 + hooks: + - id: clang-format diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e35a5a5 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -Wall -Wextra -Isrc +SRCS = src/main.c src/mylib.c +TARGET = hello + +.PHONY: all clean + +all: $(TARGET) + +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) -o $@ $^ + +clean: + rm -f $(TARGET) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..f2399e7 --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ +#include +#include "mylib.h" + +int main(void) { + int x = 3; + printf("%s\n", greet("World")); + return 0; +} diff --git a/src/mylib.c b/src/mylib.c new file mode 100644 index 0000000..cd01d47 --- /dev/null +++ b/src/mylib.c @@ -0,0 +1,11 @@ +#include "mylib.h" +#include + +#define BUF_SIZE 256 + +static _Thread_local char buf[BUF_SIZE]; + +const char* greet(const char* name) { + snprintf(buf, BUF_SIZE, "Hello, %s!", name); + return buf; +} diff --git a/src/mylib.h b/src/mylib.h new file mode 100644 index 0000000..a197d9d --- /dev/null +++ b/src/mylib.h @@ -0,0 +1,7 @@ +#ifndef MYLIB_H +#define MYLIB_H + +/* Returns a greeting string for the given name. */ +const char* greet(const char* name); + +#endif /* MYLIB_H */