Skip to content

Commit

Permalink
Add clang-format-12 source format placeholder
Browse files Browse the repository at this point in the history
* Add duktape-clang-format Docker image with clang-format-12.

* Add Makefile target docker-image-clang-format to build the image,
  and clang-format-source to format src-input sources in-place.

* No CI integration or automation yet, .clang-format is also a
  placeholder.
  • Loading branch information
svaarala committed Sep 4, 2021
1 parent ad84a4d commit 192cc89
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html

BasedOnStyle: LLVM
IndentWidth: 8
TabWidth: 8
ColumnLimit: 100
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,13 @@ codepolicycheck:
codepolicycheckvim:
-$(PYTHON2) util/check_code_policy.py --dump-vim-commands src-input/*.c src-input/*.h src-input/*.h.in tests/api/*.c

.PHONY: clang-format-source
clang-format-source: | tmp
-rm -f tmp/docker-clang-format-input.zip tmp/docker-clang-format-output.zip
zip -1 -q -r tmp/docker-clang-format-input.zip .clang-format src-input
$(DOCKER) run --rm -i duktape-clang-format < tmp/docker-clang-format-input.zip > tmp/docker-clang-format-output.zip
unzip -q -o tmp/docker-clang-format-output.zip ; true # avoid failure due to leading garbage

# Simple heap graph and peak usage using valgrind --tool=massif, for quick
# and dirty baseline comparison. Say e.g. 'make massif-test-dev-hello-world'.
# The target name is intentionally not 'massif-%.out' so that the rule is never
Expand Down Expand Up @@ -1275,11 +1282,16 @@ docker-images-s390x: docker-prepare
$(DOCKER) build -t duktape-shell-ubuntu-18.04-s390x docker/duktape-shell-ubuntu-18.04-s390x

# Build Docker image for fuzzilli fuzz testing, tag as 'fuzzilli'.
.PHONY: docker-image-fuzzilli
docker-image-fuzzilli: build/duk-fuzzilli deps/fuzzilli
mkdir -p deps/fuzzilli/Cloud/Docker/DuktapeBuilder/out
cp build/duk-fuzzilli deps/fuzzilli/Cloud/Docker/DuktapeBuilder/out/
cd deps/fuzzilli/Cloud/Docker; ./build.sh fuzzilli # Don't use duktape build option here, as duk-fuzzilli is already present

.PHONY: docker-image-clang-format
docker-image-clang-format:
$(DOCKER) build --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) -t duktape-clang-format docker/duktape-clang-format

.PHONY: docker-images
docker-images: docker-images-x64

Expand Down
33 changes: 33 additions & 0 deletions docker/duktape-clang-format/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Docker image to run clang-format on input source files.

FROM ubuntu:20.04

# Username is assumed to be 'duktape' for now, change only UID/GID if required.
ARG USERNAME=duktape
ARG UID=1000
ARG GID=1000

RUN echo "=== Package installation ===" && \
apt-get update && apt-get install -qqy clang-format-12 zip unzip

# Add non-root uid/gid to image, replicating host uid/gid if possible.
RUN echo "=== User setup, /work directory creation ===" && \
groupadd -g $GID -o $USERNAME && \
useradd -m -u $UID -g $GID -o -s /bin/bash $USERNAME && \
mkdir /work && chown $UID:$GID /work && chmod 755 /work && \
echo "PS1='\033[40;37mDOCKER\033[0;34m \u@\h [\w] >>>\033[0m '" > /root/.profile && \
echo "PS1='\033[40;37mDOCKER\033[0;34m \u@\h [\w] >>>\033[0m '" > /home/$USERNAME/.profile && \
chown $UID:$GID /home/$USERNAME/.profile

# Switch to non-root user. (Note that COPY will still copy files as root,
# so use COPY --chown for files copied.)
USER $USERNAME

# Use /work for builds, temporaries, etc.
WORKDIR /work

COPY --chown=duktape:duktape run.sh .
RUN chmod 755 run.sh

# ZIP in, ZIP out.
ENTRYPOINT ["/work/run.sh"]
12 changes: 12 additions & 0 deletions docker/duktape-clang-format/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -e

cat >/tmp/stdin.zip
mkdir reformat && cd reformat
unzip -q /tmp/stdin.zip

clang-format-12 -i `find src-input -type f | egrep '(.c|.h|.c.in|.c.in)'`

zip -1 -q -r /tmp/stdout.zip *
cat /tmp/stdout.zip

0 comments on commit 192cc89

Please sign in to comment.