Skip to content

Commit

Permalink
Add clang-format image
Browse files Browse the repository at this point in the history
  • Loading branch information
smola committed Oct 16, 2020
1 parent 993a209 commit fb83c9c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang-format/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore everything
**
60 changes: 60 additions & 0 deletions clang-format/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# Dockerfile for clang-format
#
# It uses a multi-stage build process. The first stage clones the llvm-project
# repository and builds a statically linked clang-format.
# The second stage gets the clang-format binary into a clean base image
# so we don't have all the overhead from build tools.
#
# - LLVM is built with GCC. Building it directly with LLVM on an Alpine system
# is a quite involved process. You don't really want to do it, but you can
# read more about it here: https://wiki.musl-libc.org/building-llvm.html
#
# - We tried to find a set of LLVM build options that avoid building unnecessary
# stuff. It can probably be optimized some more. See an introduction to CMake
# options for LLVM here: https://llvm.org/docs/CMake.html#options-and-variables
#
# This process was inspired by https://github.com/angular/clang-format
#

FROM alpine:3.12 AS builder

# number of parallel build jobs, it should usually be the number of CPUs, but
# that may overload the system significantly
ARG PARALLEL_JOBS=4

# llvm-project git tag or branch to clone
ARG LLVM_TAG=llvmorg-10.0.1

ENV LLVM_REPO=https://github.com/llvm/llvm-project.git

RUN apk add --no-cache git cmake make gcc g++ binutils python3

RUN git clone --depth 1 --branch "${LLVM_TAG}" "${LLVM_REPO}"

RUN set -ex ;\
cd llvm-project ;\
mkdir build ;\
cd build ;\
cmake \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_BUILD_STATIC=true \
-DLLVM_ENABLE_ZLIB=NO \
-DLLVM_ENABLE_FFI=NO \
-DLLVM_BUILD_DOCS=NO \
-DLLVM_BUILD_EXAMPLES=NO \
-DLLVM_ENABLE_PROJECTS=clang \
-G "Unix Makefiles" \
../llvm ; \
make -j ${PARALLEL_JOBS} clang-format

FROM alpine:3.12
LABEL io.whalebrew.name clang-format
LABEL io.whalebrew.config.volumes '["$PWD:$PWD"]'
LABEL io.whalebrew.config.working_dir '$PWD'
COPY --from=builder /llvm-project/build/bin/clang-format /usr/local/bin/clang-format
RUN mkdir /work
WORKDIR /work
ENTRYPOINT [ "clang-format" ]
CMD [ "-h" ]
62 changes: 62 additions & 0 deletions clang-format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

# docker-clang-format

Docker image for [clang-format](https://clang.llvm.org/docs/ClangFormat.html).

## Overview

- [Usage](#usage)
* [With Docker](#with-docker)
* [With Whalebrew](#with-whalebrew)
- [Build](#build)

## Usage

### With Docker

When running with Docker, you'll need to mount the current directory and change the user. Otherwise, re-formatted files will be owned by root:

```shell
docker run \
--rm \
-u "$(id -u):$(id -g)" \
-v "$(pwd):$(pwd)" \
-w "$(pwd)" \
hdivsecurity/clang-format:latest \
<clang-format arguments>
```

For example, tormat all `.c` and `.h` files in the current directory, recursively:

```shell
docker run \
--rm \
-u "$(id -u):$(id -g)" \
-v "$(pwd):$(pwd)" \
-w "$(pwd)" \
hdivsecurity/clang-format:latest \
-i --style=file $(find . -name '*.c' -o -name '*.h')
```

### With Whalebrew

This image supports [Whalebrew](https://github.com/whalebrew/whalebrew):

```
whalebrew install hdivsecurity/clang-format
clang-format -i --style=file $(find . -name '*.c' -o -name '*.h')
```

## Build

The image build process takes two optional [build arguments](https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg):

* `PARALLEL_JOBS` (default: `4`): number of parallel jobs for the build, it should not be higher than the number of CPUs. Note that setting it to the number of CPUs may result in the build process hogging the system.
* `LLVM_TAG` (default: `llvmorg-10.0.1`): Git branch or tag from the [llvm-project](https://github.com/llvm/llvm-project) to use for the build.

To build the image for clang-format 10 you would run:

```shell
docker build --build-arg PARALLEL_JOBS=4 --build-arg LLVM_TAG=llvmorg-10.0.1 -t hdivsecurity/clang-format:10 .
```

0 comments on commit fb83c9c

Please sign in to comment.