Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add astyle and enforce in CI #8

Merged
merged 17 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .astylerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--style=google
--indent=spaces
--indent-preproc-define
--indent-preproc-cond
--pad-oper
--pad-comma
--pad-header
--align-pointer=name
--add-braces
--convert-tabs
--mode=c
--suffix=none
5 changes: 5 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[codespell]
skip = .direnv,.git,libopencm3
ignore-words = ./dev-support/codespell-ignore-words
check-filenames =
check-hidden =
5 changes: 5 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if ! has nix_direnv_version || ! nix_direnv_version 3.0.3; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.3/direnvrc" "sha256-0EVQVNSRQWsln+rgPW3mXVmnF5sfcmKEYOmOSfLYxHg="
fi

use flake
48 changes: 48 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build
on:
push:
branches: [ '*' ]
pull_request:
branches: [ "main" ]
jobs:
build_test:
runs-on: ubuntu-latest
steps:
- name: install jq
shell: bash
run: |
if ! (command -v jq) &> /dev/null
then
sudo apt install -y --no-install-recommends jq
fi
- uses: actions/checkout@v4
- id: nixpkgs
shell: bash
run: |
if [ -f flake.lock ]; then
nixpkgs="flake:$(cat flake.lock | jq -r '.nodes.nixpkgs.locked // empty | .type + ":" + .owner + "/" + .repo + "/" + .rev')"
else
nixpkgs=channel:nixos-unstable
fi
echo "nixpkgs=$nixpkgs" >> "$GITHUB_OUTPUT"
- uses: cachix/install-nix-action@v25
with:
nix_path: nixpkgs=${{ steps.nixpkgs.outputs.nixpkgs }}
- name: Prepare nix dev shell
shell: nix develop .#ci -c bash -e {0}
run: |
astyle --version
- name: Astyle
shell: nix develop .#ci -c bash -e {0}
run: |
err=$(astyle $(git ls-files "*.c" "*.h") --options=.astylerc --dry-run --formatted)
if [[ ${#err} != 0 ]]; then
echo "$err" | awk '{split($0,a);print a[2]}' | while IFS= read -r file; do
echo "::error file={"$file"},title={checking}::Formatted $file"
done
exit 1
fi
- name: Build targets
shell: nix develop .#ci -c bash -e {0}
run: |
make
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

.vscode
.idea
.direnv
obj
elf
bin
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ If you are a potential consumer of **MLKEM-C-EMBEDDED**, please reach out to us.
We're interested in hearing the way you are considering using **MLKEM-C-EMBEDDED** and could benefit from additional features.
If you have specific feature requests, please open an issue.

## Environment Setup

All the develop and build dependencies are specified in [flake.nix](flake.nix).

- Setup with nix,
- Running `nix develop` will execute a bash shell with the development environment specified in [flake.nix](flake.nix).
- Alternatively, you can enable `direnv` by using `direnv allow`, allowing it to handle the environment setup for you.

- If your're not using nix, please ensure you have installed the same version as specified in [flake.nix](flake.nix).

For further details, please refer to [DevSupport](dev-support/)

## Running tests and benchmarks

The build system compiles tests and benchmarks for each mlkem parameter set on specified platform (currently only `stm32f4discovery` is supported).
Expand Down
22 changes: 22 additions & 0 deletions dev-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[//]: # (SPDX-License-Identifier: CC-BY-4.0)

# Dev Support

## Recommended Prerequisites

### [direnv](https://direnv.net/)

Enabling `direnv` would greatly enhance the development experience. Once enabled, all dependencies will be installed, and the shell will be augmented according to the specifications in [flake.nix](../flake.nix) whenever you enter the project directory from your shell. The initial setup might take a bit longer when entering the project directory for the first time.

`direnv` can be enabled or disabled using `direnv allow` and `direnv deny` respectively. If you're new to `direnv`, it is also recommended to [integrate it with your shell](https://direnv.net/docs/hook.html) first.

### Formatter and Linter
- `nixpkgs-fmt` and `deadnix` for `Nix`
- `shfmt` and `shellcheck` for shell script
- `astyle` for .c, .h files

### Tools

When used together with direnv, the tools should be automatically discoverable by the shell.

- [style](bin/style): Formatting, linting and spell checking all files
49 changes: 49 additions & 0 deletions dev-support/bin/log
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0

RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW3="$(tput setaf 148)"
BLUE="$(tput setaf 4)"
GREY54="$(tput setaf 249)"
NORMAL="$(tput sgr0)"

export RED
export GREEN
export YELLOW3
export BLUE
export GREY54
export NORMAL
# UNDERLINE=$(tput smul)

# export to avoid lsp warning
export DEBUG="${BLUE}DEBUG"
export INFO="${GREEN}INFO"
export WARN="${YELLOW3}WARN"
export ERROR="${RED}ERROR"

log()
{
local level="${FUNCNAME[1]^^}"
printf "%s\t%s\t%b\n" "${GREY54}$(date "+%T")" "${!level}" "${*}"
}

debug()
{
log "${NORMAL}${*}"
}

info()
{
log "${NORMAL}${*}"
}

warn()
{
log "${NORMAL}${*}" 1>&2
}

error()
{
log "${NORMAL}${*}" 1>&2
}
26 changes: 26 additions & 0 deletions dev-support/bin/style
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o errtrace
set -o nounset
set -o pipefail

ROOT="$(realpath "$(dirname "$0")"/../../)"
DEVSUPPORT="$(realpath "$(dirname "$0")"/../)"
CURDIR="$(realpath "$(dirname "$0")")"
. "$CURDIR/log"

info "Formatting nix files"
nixpkgs-fmt "$ROOT"

info "Formatting shell scripts"
# shellcheck disable=SC2046
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))

info "Chekcking misspellings"
codespell --skip "$ROOT/.direnv,$ROOT/.git,$ROOT/libopencm3" --ignore-words "$DEVSUPPORT/codespell-ignore-words" "$ROOT"

info "Formatting c files"
# shellcheck disable=SC2046
astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --formatted
3 changes: 3 additions & 0 deletions dev-support/codespell-ignore-words
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ake
ege
hsi
Loading