-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-linux.sh
executable file
·151 lines (126 loc) · 5.07 KB
/
prepare-linux.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# This is the main linux script for preparing the build of the JNI
# native part for the clang bindings. This is intended to be run on a
# fresh Ubunutu installation (last tested with 18.04), such as a cloud
# machine. Please read the lines/instructions carefully, before
# executing the script. The script should be executed from the main
# directory of the git and will clone LLVM besides it.
# The tag used to compile from
LLVM_TAG=llvmorg-9.0.1
echo "Installing required tools"
sudo apt update
sudo apt install -y g++ cmake openjdk-8-jdk-headless swig
echo "Cloning LLVM into sibling directory"
(
cd ..
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout $LLVM_TAG
)
echo "Preparing JNI code generated via SWIG"
(
GENERATED_DIR=generated/eu/cqse/clang
mkdir -p $GENERATED_DIR
cp native/clang.i ../llvm-project/clang/include/clang-c
cd ../llvm-project/clang/include/clang-c
# run swig to generate the JNI binding
swig -c++ -java -package eu.cqse.clang -outdir ../../../../clang-jni/$GENERATED_DIR -o clang-jni.cpp -v -Wall clang.i
# make generated JNI methods available in list of exported functions
grep -o 'Java.*ClangJNI[^\(]*' clang-jni.cpp >> ../../tools/libclang/libclang.exports
)
echo "Integrating own Java JNI code"
(
# generate headers for JNI code
(cd src && javac -h ../../llvm-project/clang/tools/libclang -cp .:../generated eu/cqse/clang/ClangBinding.java)
# copy our own native code
cp native/eu_cqse_clang_ClangBinding.cpp ../llvm-project/clang/tools/libclang
mv ../llvm-project/clang/include/clang-c/clang-jni.cpp ../llvm-project/clang/tools/libclang
# patch generated JNI to fix resource leak
cat <<EOF | (cd ../llvm-project && patch -p1)
diff --git a/clang/tools/libclang/clang-jni.cpp b/clang/tools/libclang/clang-jni.cpp
index 279f9e62b56..1d668f0c201 100644
--- a/clang/tools/libclang/clang-jni.cpp
+++ b/clang/tools/libclang/clang-jni.cpp
@@ -1266,7 +1266,11 @@ SWIGEXPORT void JNICALL Java_eu_cqse_clang_ClangJNI_delete_1CXUnsavedFile(JNIEnv
(void)jenv;
(void)jcls;
- arg1 = *(CXUnsavedFile **)&jarg1;
+ arg1 = *(CXUnsavedFile **)&jarg1;
+ if (arg1 != 0) {
+ if (arg1->Contents != 0) delete arg1->Contents;
+ if (arg1->Filename != 0) delete arg1->Filename;
+ }
delete arg1;
}
EOF
# make generated JNI methods available in list of exported functions
cd ../llvm-project/clang/tools/libclang
grep -o 'Java_eu_cqse_clang_ClangBinding[^\(]*' eu_cqse_clang_ClangBinding.h >> libclang.exports
# Monkey patching our build steps into existing cmake files
sed -i -e '/Indexing.cpp/a eu_cqse_clang_ClangBinding.cpp' CMakeLists.txt
sed -i -e '/Indexing.cpp/a clang-jni.cpp' CMakeLists.txt
sed -i -e '/Index_Internal.h/a eu_cqse_clang_ClangBinding.h' CMakeLists.txt
sed -i -e '/set.LIBS/i include_directories(../../include/clang-c)' CMakeLists.txt
sed -i -e '/if.ENABLE_SHARED/i target_compile_options (libclang PUBLIC "-fexceptions")' CMakeLists.txt
)
echo "Patching raw_ostream to suppress unwanted output"
(
cd ../llvm-project
cat <<EOF | patch -p1
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 2baccaa0cbd..c43ce425fe0 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -853,7 +853,7 @@ raw_ostream &llvm::outs() {
raw_ostream &llvm::errs() {
// Set standard error to be unbuffered by default.
static raw_fd_ostream S(STDERR_FILENO, false, true);
- return S;
+ return llvm::nulls();
}
/// nulls() - This returns a reference to a raw_ostream which discards output.
EOF
)
echo "Patching check to get rid of locale dependency of option parsing"
(
cd ../llvm-project
cat <<EOF | patch -p1
index 09409d87020..71c406ec286 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp
@@ -10,6 +10,9 @@
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include <sstream>
+#include <locale>
+
using namespace clang::ast_matchers;
namespace clang {
@@ -67,11 +70,20 @@ AST_MATCHER_P(StringLiteral, isConcatenatedLiteral, unsigned,
} // namespace
+// a more robust stod that is indepdent of the locale
+double robust_stod(const std::string& str) {
+ double result = 0;
+ std::istringstream istr(str);
+ istr.imbue(std::locale("C"));
+ istr >> result;
+ return result;
+}
+
SuspiciousMissingCommaCheck::SuspiciousMissingCommaCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
SizeThreshold(Options.get("SizeThreshold", 5U)),
- RatioThreshold(std::stod(Options.get("RatioThreshold", ".2"))),
+ RatioThreshold(robust_stod(Options.get("RatioThreshold", "0.2"))),
MaxConcatenatedTokens(Options.get("MaxConcatenatedTokens", 5U)) {}
void SuspiciousMissingCommaCheck::storeOptions(
EOF
)
PACKAGE=clang-build-package.tar.gz
echo "Packing for usage on other build machines"
(
cd ..
tar czf $PACKAGE clang-jni llvm-project
)
echo "Package ready at $PACKAGE"