From e7e9d3b8c71aaa81271cc68f1e9970b8bdf0ec2e Mon Sep 17 00:00:00 2001 From: Tom Clesius <48072820+tclesius@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:37:16 +0200 Subject: [PATCH] Adapt Docker script and Dockerfile (#1347) Adapt Docker script and Dockerfile --- .github/workflows/main.yml | 111 +++++++++++++++++++++++++++++++++++++ build-with-docker.sh | 65 +++++++++++----------- docker/Dockerfile | 51 ++++++++++++++--- 3 files changed, 186 insertions(+), 41 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 83df02d81..90db0c4a2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -486,6 +486,117 @@ jobs: with: name: c3-ubuntu-20-${{matrix.build_type}} path: c3-ubuntu-20-${{matrix.build_type}}.tar.gz + + build-with-docker: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + ubuntu_version: [20.04, 22.04] + build_type: [Release, Debug] + llvm_version: [17, 18, 19, 20] + + steps: + - uses: actions/checkout@v4 + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Make script executable + run: chmod +x ./build-with-docker.sh + + - name: Run build + run: | + LLVM_VERSION=${{ matrix.llvm_version }} UBUNTU_VERSION=${{ matrix.ubuntu_version }} CMAKE_BUILD_TYPE=${{ matrix.build_type }} ./build-with-docker.sh + + - name: Compile and run some examples + run: | + cd resources + ../build/c3c compile examples/base64.c3 + ../build/c3c compile examples/binarydigits.c3 + ../build/c3c compile examples/brainfk.c3 + ../build/c3c compile examples/factorial_macro.c3 + ../build/c3c compile examples/fasta.c3 + ../build/c3c compile examples/gameoflife.c3 + ../build/c3c compile examples/hash.c3 + ../build/c3c compile-only examples/levenshtein.c3 + ../build/c3c compile examples/load_world.c3 + ../build/c3c compile-only examples/map.c3 + ../build/c3c compile examples/mandelbrot.c3 + ../build/c3c compile examples/plus_minus.c3 + ../build/c3c compile examples/nbodies.c3 + ../build/c3c compile examples/spectralnorm.c3 + ../build/c3c compile examples/swap.c3 + ../build/c3c compile examples/contextfree/boolerr.c3 + ../build/c3c compile examples/contextfree/dynscope.c3 + ../build/c3c compile examples/contextfree/guess_number.c3 + ../build/c3c compile examples/contextfree/multi.c3 + ../build/c3c compile examples/contextfree/cleanup.c3 + ../build/c3c compile-run examples/hello_world_many.c3 + ../build/c3c compile-run examples/time.c3 + ../build/c3c compile-run examples/fannkuch-redux.c3 + ../build/c3c compile-run examples/contextfree/boolerr.c3 + ../build/c3c compile-run examples/load_world.c3 + ../build/c3c compile-run examples/process.c3 + ../build/c3c compile-run examples/ls.c3 + ../build/c3c compile-run --linker=builtin linux_stack.c3 + ../build/c3c compile-run linux_stack.c3 + ../build/c3c compile-run examples/args.c3 -- foo -bar "baz baz" + + - name: Compile run unit tests + run: | + cd test + ../build/c3c compile-test unit + + - name: Build testproject + run: | + cd resources/testproject + ../../build/c3c run --debug-log + + - name: Test WASM + run: | + cd resources/testfragments + ../../build/c3c compile --reloc=none --target wasm32 -g0 --link-libc=no --no-entry -Os wasm4.c3 + + - name: Install QEMU and Risc-V toolchain + run: | + sudo apt-get install opensbi qemu-system-misc u-boot-qemu gcc-riscv64-unknown-elf + + - name: Compile and run Baremetal Risc-V Example + run: | + cd resources/examples/embedded/riscv-qemu + make C3C_PATH=../../../../build/ run + + - name: Build testproject direct linker + run: | + cd resources/testproject + ../../build/c3c run --debug-log --linker=builtin + + - name: Init a library & a project + run: | + ./build/c3c init-lib mylib + ls mylib.c3l + ./build/c3c init myproject + ls myproject + + - name: run compiler tests + run: | + cd test + python3 src/tester.py ../build/c3c test_suite/ + + - name: bundle_output + run: | + mkdir c3 + cp -r lib c3 + cp msvc_build_libraries.py c3 + cp build/c3c c3 + tar czf c3-ubuntu-${{matrix.build_type}}.tar.gz c3 + + - name: upload artifacts + uses: actions/upload-artifact@v3 + with: + name: c3-ubuntu-${{ matrix.ubuntu_version }}-${{matrix.build_type}} + path: c3-ubuntu-${{matrix.build_type}}.tar.gz build-mac: runs-on: macos-latest diff --git a/build-with-docker.sh b/build-with-docker.sh index b969df97f..dc45d39ca 100755 --- a/build-with-docker.sh +++ b/build-with-docker.sh @@ -1,43 +1,44 @@ #!/bin/bash -## build-with-docker.sh -## @author gdm85 -## @modified by Kenta -## -## Script to build c3c for Ubuntu 22 -## -# -read -p "Select Build Type: Debug/Release: " config +: ${DOCKER:=docker} +: ${IMAGE:="c3c-builder"} +: ${CMAKE_BUILD_TYPE:=Release} +: ${LLVM_VERSION:=18} +: ${UBUNTU_VERSION:="22.04"} +: ${CMAKE_VERSION:="3.20.0"} -set -e +cd docker || exit 1 # Exit if the 'docker' directory doesn't exist -DOCKER=docker -DOCKER_RUN="" -IMAGE="c3c-builder" -if type podman 2>/dev/null >/dev/null; then - DOCKER=podman - DOCKER_RUN="--userns=keep-id" - IMAGE="localhost/$IMAGE" +$DOCKER build \ + --build-arg LLVM_VERSION=$LLVM_VERSION \ + --build-arg CMAKE_VERSION=$CMAKE_VERSION \ + --build-arg UBUNTU_VERSION=$UBUNTU_VERSION \ + -t $IMAGE . + +if [ $? -ne 0 ]; then + echo "Docker image build failed. Exiting." + exit 1 fi -if [ $config == "Debug" ]; then - CMAKE_BUILD_TYPE=Debug -else - CMAKE_BUILD_TYPE="$config" -fi - -UBUNTU_VERSION="22.10" -LLVM_VERSION="15" - -IMAGE="$IMAGE:22" - -cd docker && $DOCKER build -t $IMAGE\ - --build-arg DEPS="llvm-$LLVM_VERSION-dev liblld-$LLVM_VERSION-dev clang-$LLVM_VERSION libllvm$LLVM_VERSION llvm-$LLVM_VERSION-runtime" \ - --build-arg UBUNTU_VERSION="$UBUNTU_VERSION" . cd .. rm -rf build bin mkdir -p build bin -exec $DOCKER run -ti --rm --tmpfs=/tmp $DOCKER_RUN -v "$PWD":/home/c3c/source -w /home/c3c/source $IMAGE bash -c \ - "cd build && cmake -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DC3_LLVM_VERSION=$LLVM_VERSION .. && cmake --build . && mv c3c lib ../bin/" +chmod -R 777 build bin + +exec $DOCKER run -i --rm \ + -v "$PWD":/home/c3c/source \ + -w /home/c3c/source $IMAGE bash -c \ + "cmake -S . -B build \ + -G Ninja \ + -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \ + -DCMAKE_C_COMPILER=clang-$LLVM_VERSION \ + -DCMAKE_CXX_COMPILER=clang++-$LLVM_VERSION \ + -DCMAKE_LINKER=lld-$LLVM_VERSION \ + -DCMAKE_OBJCOPY=llvm-objcopy-$LLVM_VERSION \ + -DCMAKE_STRIP=llvm-strip-$LLVM_VERSION \ + -DCMAKE_DLLTOOL=llvm-dlltool-$LLVM_VERSION \ + -DC3_LLVM_VERSION=auto && \ + cmake --build build && \ + cp -r build/c3c build/lib bin" \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 59a4924e9..1aeb1e02a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,16 +1,49 @@ +ARG UBUNTU_VERSION=22.04 +FROM ubuntu:${UBUNTU_VERSION} -ARG UBUNTU_VERSION -FROM ubuntu:$UBUNTU_VERSION +ARG LLVM_VERSION=18 +ENV LLVM_DEV_VERSION=20 -ARG DEPS +ARG CMAKE_VERSION=3.20 -RUN export DEBIAN_FRONTEND=noninteractive && export TERM=xterm && apt-get update && apt-get install -y build-essential cmake zlib1g zlib1g-dev \ - $DEPS && \ - rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt-get install -y wget gnupg software-properties-common zlib1g zlib1g-dev python3 ninja-build curl g++ && \ + wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-$CMAKE_VERSION-linux-x86_64.sh && \ + mkdir -p /opt/cmake && \ + sh cmake-${CMAKE_VERSION}-linux-x86_64.sh --prefix=/opt/cmake --skip-license && \ + rm cmake-${CMAKE_VERSION}-linux-x86_64.sh && \ + ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake -ARG GID=1000 -ARG UID=1000 +RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \ + if [ "${LLVM_VERSION}" -lt 18 ]; then \ + add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${LLVM_VERSION} main" && \ + apt-get update && \ + apt-get install -y -t llvm-toolchain-focal-${LLVM_VERSION} \ + libpolly-${LLVM_VERSION}-dev \ + clang-${LLVM_VERSION} llvm-${LLVM_VERSION} llvm-${LLVM_VERSION}-dev \ + lld-${LLVM_VERSION} liblld-${LLVM_VERSION}-dev libmlir-${LLVM_VERSION} \ + libmlir-${LLVM_VERSION}-dev mlir-${LLVM_VERSION}-tools; \ + elif [ "${LLVM_VERSION}" -lt "${LLVM_DEV_VERSION}" ]; then \ + add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${LLVM_VERSION} main" && \ + apt-get update && \ + apt-get install -y -t llvm-toolchain-focal-${LLVM_VERSION} \ + libpolly-${LLVM_VERSION}-dev \ + clang-${LLVM_VERSION} clang++-${LLVM_VERSION} llvm-${LLVM_VERSION} llvm-${LLVM_VERSION}-dev \ + lld-${LLVM_VERSION} liblld-${LLVM_VERSION}-dev; \ + else \ + add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main" && \ + apt-get update && \ + apt-get install -y -t llvm-toolchain-focal \ + libpolly-${LLVM_VERSION}-dev \ + clang-${LLVM_VERSION} llvm-${LLVM_VERSION} llvm-${LLVM_VERSION}-dev \ + lld-${LLVM_VERSION} liblld-${LLVM_VERSION}-dev; \ + fi && \ + rm -rf /var/lib/apt/lists/* -RUN groupadd -o --gid=$GID c3c && useradd --gid=$GID --uid=$GID --create-home --shell /bin/bash c3c +RUN groupadd -g 1337 c3c && \ + useradd -m -u 1337 -g c3c c3c +# Add cmake to PATH for user c3c USER c3c +ENV PATH="/opt/cmake/bin:${PATH}" + +WORKDIR /home/c3c \ No newline at end of file