From becdf236f4a8fde98b767f9862ad3c3c56d257a7 Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Sun, 21 Apr 2024 19:32:20 +0200 Subject: [PATCH 1/2] [#116]: Update tools --- Dockerfile | 2 +- docker/static_analysis.dockerfile | 8 +++++--- entrypoint_cpp.sh | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index afe3093..e4f47ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM jdomagala/static_analysis:python +FROM jdomagala/static_analysis:update_tools WORKDIR /src diff --git a/docker/static_analysis.dockerfile b/docker/static_analysis.dockerfile index f8e931b..5e42d03 100644 --- a/docker/static_analysis.dockerfile +++ b/docker/static_analysis.dockerfile @@ -1,8 +1,8 @@ FROM ubuntu:23.04 as base # Define versions as environment variables -ENV CLANG_VERSION=16 -ENV CPPCHECK_VERSION=2.12.0 +ENV CLANG_VERSION=18 +ENV CPPCHECK_VERSION=2.14.0 # Other environment variables ENV CXX=clang++ @@ -11,10 +11,12 @@ ENV DEBIAN_FRONTEND=noninteractive # Install dependencies RUN apt-get update && apt-get install -y \ - build-essential python3 python3-pip git clang-$CLANG_VERSION clang-tidy-$CLANG_VERSION wget libssl-dev ninja-build \ + build-essential python3 python3-pip git wget libssl-dev ninja-build \ + lsb-release software-properties-common gnupg \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && pip3 install PyGithub pylint --break-system-packages \ + && bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" \ && ln -s "$(which clang++-$CLANG_VERSION)" /usr/bin/clang++ \ && ln -s "$(which clang-$CLANG_VERSION)" /usr/bin/clang \ && ln -s /usr/bin/python3 /usr/bin/python diff --git a/entrypoint_cpp.sh b/entrypoint_cpp.sh index df4f9b3..d8851d8 100644 --- a/entrypoint_cpp.sh +++ b/entrypoint_cpp.sh @@ -67,16 +67,16 @@ else cat cppcheck_*.txt > cppcheck.txt # Excludes for clang-tidy are handled in python script - debug_print "Running run-clang-tidy-16 $CLANG_TIDY_ARGS -p $(pwd) $files_to_check >>clang_tidy.txt 2>&1" - eval run-clang-tidy-16 "$CLANG_TIDY_ARGS" -p "$(pwd)" "$files_to_check" >clang_tidy.txt 2>&1 || true + debug_print "Running run-clang-tidy-18 $CLANG_TIDY_ARGS -p $(pwd) $files_to_check >>clang_tidy.txt 2>&1" + eval run-clang-tidy-18 "$CLANG_TIDY_ARGS" -p "$(pwd)" "$files_to_check" >clang_tidy.txt 2>&1 || true else # Excludes for clang-tidy are handled in python script debug_print "Running cppcheck -j $num_proc $files_to_check $CPPCHECK_ARGS --output-file=cppcheck.txt ..." eval cppcheck -j "$num_proc" "$files_to_check" "$CPPCHECK_ARGS" --output-file=cppcheck.txt || true - debug_print "Running run-clang-tidy-16 $CLANG_TIDY_ARGS $files_to_check >>clang_tidy.txt 2>&1" - eval run-clang-tidy-16 "$CLANG_TIDY_ARGS" "$files_to_check" >clang_tidy.txt 2>&1 || true + debug_print "Running run-clang-tidy-18 $CLANG_TIDY_ARGS $files_to_check >>clang_tidy.txt 2>&1" + eval run-clang-tidy-18 "$CLANG_TIDY_ARGS" "$files_to_check" >clang_tidy.txt 2>&1 || true fi cd / From 532f15bd71c3eb690ec76c5d7f565c88cd0f0ae7 Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Mon, 22 Apr 2024 11:30:38 +0200 Subject: [PATCH 2/2] [#116]: Add llvm.sh script that will be used for future clang-tidy releases --- Dockerfile | 2 +- docker/static_analysis.dockerfile | 28 +++-- llvm.sh | 167 ++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+), 11 deletions(-) create mode 100644 llvm.sh diff --git a/Dockerfile b/Dockerfile index e4f47ec..ad59e55 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM jdomagala/static_analysis:update_tools +FROM jdomagala/static_analysis:latest WORKDIR /src diff --git a/docker/static_analysis.dockerfile b/docker/static_analysis.dockerfile index 5e42d03..1f380e9 100644 --- a/docker/static_analysis.dockerfile +++ b/docker/static_analysis.dockerfile @@ -1,22 +1,26 @@ FROM ubuntu:23.04 as base # Define versions as environment variables -ENV CLANG_VERSION=18 -ENV CPPCHECK_VERSION=2.14.0 +ENV CLANG_VERSION=18 \ + CPPCHECK_VERSION=2.14.0 \ + CXX=clang++ \ + CC=clang \ + DEBIAN_FRONTEND=noninteractive -# Other environment variables -ENV CXX=clang++ -ENV CC=clang -ENV DEBIAN_FRONTEND=noninteractive +# Copy the llvm.sh installation script +COPY llvm.sh /llvm.sh # Install dependencies RUN apt-get update && apt-get install -y \ build-essential python3 python3-pip git wget libssl-dev ninja-build \ lsb-release software-properties-common gnupg \ + # Execute the LLVM install script with the version number + && chmod +x /llvm.sh && /llvm.sh $CLANG_VERSION \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ - && pip3 install PyGithub pylint --break-system-packages \ - && bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" \ + # Install Python packages + && pip3 install --break-system-packages PyGithub pylint \ + # Create symlinks for clang and clang++ && ln -s "$(which clang++-$CLANG_VERSION)" /usr/bin/clang++ \ && ln -s "$(which clang-$CLANG_VERSION)" /usr/bin/clang \ && ln -s /usr/bin/python3 /usr/bin/python @@ -25,8 +29,12 @@ WORKDIR /opt # Build CMake from source RUN git clone https://github.com/Kitware/CMake.git \ - && cd CMake && ./bootstrap && make -j4 && make install + && cd CMake \ + && ./bootstrap && make -j$(nproc) && make install # Install cppcheck RUN git clone https://github.com/danmar/cppcheck.git \ - && cd cppcheck && git checkout tags/$CPPCHECK_VERSION && mkdir build && cd build && cmake -G Ninja .. && ninja all && ninja install + && cd cppcheck \ + && git checkout tags/$CPPCHECK_VERSION \ + && mkdir build && cd build \ + && cmake -G Ninja .. && ninja all && ninja install diff --git a/llvm.sh b/llvm.sh new file mode 100644 index 0000000..491c785 --- /dev/null +++ b/llvm.sh @@ -0,0 +1,167 @@ +#!/bin/bash +################################################################################ +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +################################################################################ +# +# This script will install the llvm toolchain on the different +# Debian and Ubuntu versions + +set -eux + +usage() { + set +x + echo "Usage: $0 [llvm_major_version] [all] [OPTIONS]" 1>&2 + echo -e "all\t\t\tInstall all packages." 1>&2 + echo -e "-n=code_name\t\tSpecifies the distro codename, for example bionic" 1>&2 + echo -e "-h\t\t\tPrints this help." 1>&2 + echo -e "-m=repo_base_url\tSpecifies the base URL from which to download." 1>&2 + exit 1; +} + +CURRENT_LLVM_STABLE=18 +BASE_URL="http://apt.llvm.org" + +# Check for required tools +needed_binaries=(lsb_release wget add-apt-repository gpg) +missing_binaries=() +for binary in "${needed_binaries[@]}"; do + if ! which "$binary" &>/dev/null ; then + missing_binaries+=("$binary") + fi +done +if [[ ${#missing_binaries[@]} -gt 0 ]] ; then + echo "You are missing some tools this script requires: " "${missing_binaries[@]}" + echo "(hint: apt install lsb-release wget software-properties-common gnupg)" + exit 4 +fi + +# Set default values for commandline arguments +# We default to the current stable branch of LLVM +LLVM_VERSION=$CURRENT_LLVM_STABLE +DISTRO=$(lsb_release -is) +VERSION=$(lsb_release -sr) +UBUNTU_CODENAME="" +CODENAME_FROM_ARGUMENTS="" +# Obtain VERSION_CODENAME and UBUNTU_CODENAME (for Ubuntu and its derivatives) +# shellcheck disable=SC1091 +source /etc/os-release +DISTRO=${DISTRO,,} +case ${DISTRO} in + debian) + # Debian Trixie has a workaround because of + # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1038383 + if [[ "${VERSION}" == "unstable" ]] || [[ "${VERSION}" == "testing" ]] || [[ "${VERSION_CODENAME}" == "trixie" ]]; then + CODENAME=unstable + LINKNAME= + else + # "stable" Debian release + CODENAME=${VERSION_CODENAME} + LINKNAME=-${CODENAME} + fi + ;; + *) + # ubuntu and its derivatives + if [[ -n "${UBUNTU_CODENAME}" ]]; then + CODENAME=${UBUNTU_CODENAME} + if [[ -n "${CODENAME}" ]]; then + LINKNAME=-${CODENAME} + fi + fi + ;; +esac + +# read optional command line arguments +if [ "$#" -ge 1 ] && [ "${1::1}" != "-" ]; then + if [ "$1" != "all" ]; then + LLVM_VERSION=$1 + fi + OPTIND=2 + if [ "$#" -ge 2 ]; then + if [ "$2" == "all" ]; then + # Install all packages + OPTIND=3 + fi + fi +fi + +while getopts ":hm:n:" arg; do + # shellcheck disable=SC2220 + case $arg in + h) + usage + ;; + m) + BASE_URL=${OPTARG} + ;; + n) + CODENAME=${OPTARG} + if [[ "${CODENAME}" == "unstable" ]]; then + # link name does not apply to unstable repository + LINKNAME= + else + LINKNAME=-${CODENAME} + fi + CODENAME_FROM_ARGUMENTS="true" + ;; + esac +done + +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root!" + exit 1 +fi + +declare -A LLVM_VERSION_PATTERNS +LLVM_VERSION_PATTERNS[9]="-9" +LLVM_VERSION_PATTERNS[10]="-10" +LLVM_VERSION_PATTERNS[11]="-11" +LLVM_VERSION_PATTERNS[12]="-12" +LLVM_VERSION_PATTERNS[13]="-13" +LLVM_VERSION_PATTERNS[14]="-14" +LLVM_VERSION_PATTERNS[15]="-15" +LLVM_VERSION_PATTERNS[16]="-16" +LLVM_VERSION_PATTERNS[17]="-17" +LLVM_VERSION_PATTERNS[18]="-18" +LLVM_VERSION_PATTERNS[19]="" + +if [ ! ${LLVM_VERSION_PATTERNS[$LLVM_VERSION]+_} ]; then + echo "This script does not support LLVM version $LLVM_VERSION" + exit 3 +fi + +LLVM_VERSION_STRING=${LLVM_VERSION_PATTERNS[$LLVM_VERSION]} + +# join the repository name +if [[ -n "${CODENAME}" ]]; then + REPO_NAME="deb ${BASE_URL}/${CODENAME}/ llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING} main" + + # check if the repository exists for the distro and version + if ! wget -q --method=HEAD "${BASE_URL}/${CODENAME}" &> /dev/null; then + if [[ -n "${CODENAME_FROM_ARGUMENTS}" ]]; then + echo "Specified codename '${CODENAME}' is not supported by this script." + else + echo "Distribution '${DISTRO}' in version '${VERSION}' is not supported by this script." + fi + exit 2 + fi +fi + + +# install everything + +if [[ ! -f /etc/apt/trusted.gpg.d/apt.llvm.org.asc ]]; then + # download GPG key once + wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc +fi + +# shellcheck disable=SC2006 +if [[ -z "`apt-key list 2> /dev/null | grep -i llvm`" ]]; then + # Delete the key in the old format + apt-key del AF4F7421 +fi +add-apt-repository -y "${REPO_NAME}" +apt-get update +PKG="clang-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION clang-tidy-$LLVM_VERSION" +apt-get install -y "$PKG"