Debian Codegraph @

contrib/utilities/compile_clang_format

raw
1 #!/bin/bash
2 ## ------------------------------------------------------------------------
3 ##
4 ## SPDX-License-Identifier: LGPL-2.1-or-later
5 ## Copyright (C) 2018 - 2023 by the deal.II authors
6 ##
7 ## This file is part of the deal.II library.
8 ##
9 ## Part of the source code is dual licensed under Apache-2.0 WITH
10 ## LLVM-exception OR LGPL-2.1-or-later. Detailed license information
11 ## governing the source code and code contributions can be found in
12 ## LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
13 ##
14 ## ------------------------------------------------------------------------
15
16 #
17 # This script downloads, compiles and installs the clang-format binary. The
18 # destination directory is
19 # [contrib/utilities]/programs/clang-<VERSION>/bin.
20 #
21 # Compiling clang-format and all necessary parts of LLVM/CLANG might
22 # require a significant amount of resources. Alternatively, you can use
23 # download_clang_format to install a statically-linked binary.
24 #
25
26 set -e
27 set -u
28
29 PRG="$(cd "$(dirname "$0")" && pwd)/programs"
30
31 VERSION="16"
32 RELEASE_DATE="2023-06-10"
33 LLVM_COMMIT="7cbf1a2591520c2491aa35339f227775f4d3adf6"
34
35 CLANG_PATH="${PRG}/clang-${VERSION}"
36 RELEASE_BRANCH="release/${VERSION}.x"
37 LLVM_REPOSITORY="https://github.com/llvm/llvm-project"
38
39 if [ ! -d "${PRG}" ]
40 then
41 echo "create folder ${PRG}"
42 mkdir "${PRG}"
43 fi
44
45 if [ -d "${CLANG_PATH}" ]
46 then
47 echo "${CLANG_PATH} exists. Exiting."
48 exit 1
49 fi
50
51 echo "Downloading and compiling clang-format-${VERSION}."
52 mkdir -p "${CLANG_PATH}/bin"
53
54 tmpdir="${TMPDIR:-/tmp}/dealiiclang${RANDOM}${RANDOM}"
55 mkdir -p "${tmpdir}"
56 cd "${tmpdir}"
57
58 GIT_VERSION=$(git version)
59 GIT_MAJOR_VERSION=$(echo "${GIT_VERSION}" | sed 's/^[^0-9]*\([0-9]*\).*$/\1/g')
60 GIT_MINOR_VERSION=$(echo "${GIT_VERSION}" | sed 's/^[^0-9]*[0-9]*\.\([0-9]*\).*$/\1/g')
61
62 if [ "$GIT_MAJOR_VERSION" -ge 2 ] && [ "$GIT_MINOR_VERSION" -ge 11 ]; then
63 GIT_SHALLOW_SINCE_AVAILABLE=true
64 else
65 GIT_SHALLOW_SINCE_AVAILABLE=false
66 fi
67
68 git init
69 git remote add origin "${LLVM_REPOSITORY}"
70 if [ "$GIT_SHALLOW_SINCE_AVAILABLE" = true ]; then
71 git fetch --shallow-since="${RELEASE_DATE}" origin "${RELEASE_BRANCH}"
72 else
73 git fetch --depth=1 origin "${RELEASE_BRANCH}"
74 i=1;
75 while ! git cat-file -e ${LLVM_COMMIT} 2> /dev/null; do
76 git fetch --depth=$((i+=10)) origin "${RELEASE_BRANCH}";
77 done
78 fi
79 git reset --hard "${LLVM_COMMIT}"
80
81 # move clang directory into right place for the build system
82 mv clang llvm/tools
83
84 mkdir llvm/build
85 cd llvm/build
86
87 case "${OSTYPE}" in
88 darwin*)
89 cmake -DCMAKE_BUILD_TYPE=MinSizeRel ..
90 ;;
91 *)
92 cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_BUILD_STATIC=true ..
93 ;;
94 esac
95
96 make -j4 clang-format
97 cp bin/clang-format "${CLANG_PATH}"/bin
98 cp ../{CODE_OWNERS,CREDITS,LICENSE}.TXT "${CLANG_PATH}"
99 rm -rf "${tmpdir}"
100
101 echo "All done. clang-format successfully installed into"
102 echo " ${CLANG_PATH}/bin"