forked from openjdk/jmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·329 lines (283 loc) · 10.5 KB
/
build.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/bash
set -e # immediately exit if any command has a non-zero exit status
set -u # a reference to any variable you have not previously defined causes the program to immediately exit
set -o pipefail # If any command in a pipeline fails, that return code will be used as the return code of the whole pipeline
PROGNAME=$(basename "$0")
JETTY_PID=""
BASEDIR=""
JMC_DIR=""
function err_report() {
err_log "$(date +%T) ${PROGNAME}: Error on line $1"
err_log "$(date +%T) current working directory: $PWD"
}
function exitTrap() {
if [ -n "${JETTY_PID}" ]; then
echo "$(date +%T) terminating jetty server"
kill "${JETTY_PID}"
fi
}
function installCore() {
local timestamp="$1"
local installLog="${BASEDIR}/build_${timestamp}.3.install.log"
pushd core 1> /dev/null || {
err_log "directory core not found"
exit 1
}
echo "$(date +%T) installing core artifacts - logging output to ${installLog}"
mvn clean install --log-file "${installLog}"
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
}
function startJetty() {
local timestamp=$1
local p2SiteLog="${BASEDIR}/build_${timestamp}.1.p2_site.log"
local jettyLog="${BASEDIR}/build_${timestamp}.2.jetty.log"
pushd releng/third-party 1> /dev/null || {
err_log "directory releng/third-party not found"
exit 1
}
echo "$(date +%T) building p2:site - logging output to ${p2SiteLog}"
mvn p2:site --log-file "${p2SiteLog}"
echo "$(date +%T) run jetty - logging output to ${jettyLog}"
touch "${jettyLog}" # create file so that it exists already for tail below
mvn jetty:run --log-file "${jettyLog}" &
JETTY_PID=$!
while ! grep -q "^\[INFO\] Started Jetty Server$" "${jettyLog}"; do
echo "$(date +%T) waiting for jetty server to start"
sleep 1
done
echo "$(date +%T) jetty server up and running on pid ${JETTY_PID}"
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
}
function err_log() {
echo "$@" >&2
}
trap 'err_report $LINENO' ERR
trap 'exitTrap' EXIT
function printHelp() {
echo "usage: call ./$(basename "$0") with the following options:"
{
printf " \t%s\t%s\n" "--test" "to run the tests"
printf " \t%s\t%s\n" "--testUi" "to run the tests including UI tests"
printf " \t%s\t%s\n" "--installCore" "to install JMC core"
printf " \t%s\t%s\n" "--packageJmc" "to package JMC"
printf " \t%s\t%s\n" "--packageAgent" "to package Agent"
printf " \t%s\t%s\n" "--clean" "to run maven clean"
printf " \t%s\t%s\n" "--run" "to run JMC, once it is packaged"
printf " \t%s\t%s\n" "--runAgentExample" "to run Agent 'InstrumentMe' example, once it is packaged"
printf " \t%s\t%s\n" "--runAgentConverterExample" "to run Agent 'InstrumentMeConverter' example, once it is packaged"
printf " \t%s\t%s\n" "--help" "to show this help dialog"
} | column -ts $'\t'
}
if [ $# -eq 0 ]; then
printHelp
exit 0
fi
function runTests() {
local timestamp=$1
startJetty $timestamp
echo "${timestamp} running tests"
mvn verify
}
function runUiTests() {
local timestamp=$1
startJetty $timestamp
installCore $timestamp
echo "$(date +%T) running UI tests"
mvn verify -P uitests
}
function packageJmc() {
local timestamp=$1
startJetty $timestamp
installCore $timestamp
local packageLog="${BASEDIR}/build_${timestamp}.4.package.log"
echo "$(date +%T) packaging jmc - logging output to ${packageLog}"
mvn package --log-file "${packageLog}"
if [[ "${OSTYPE}" =~ "linux"* ]]; then
echo "You can now run jmc by calling \"${PROGNAME} --run\" or \"${BASEDIR}/products/org.openjdk.jmc/linux/gtk/x86_64/JDK\ Mission\ Control/jmc\""
elif [[ "${OSTYPE}" =~ "darwin"* ]]; then
echo "You can now run jmc by calling \"${PROGNAME} --run\" or \"${BASEDIR}/products/org.openjdk.jmc/macosx/cocoa/x86_64/JDK\ Mission\ Control.app/Contents/MacOS/jmc\""
else
err_log "unknown OS type: \"${OSTYPE}\". Please check your package in \"${BASEDIR}/products/org.openjdk.jmc/\""
fi
}
function packageAgent() {
local timestamp=$1
local packageLog="${BASEDIR}/build_${timestamp}.5.package.log"
pushd agent 1> /dev/null || {
err_log "directory agent not found"
exit 1
}
echo "$(date +%T) packaging jmc agent - logging output to ${packageLog}"
mvn package --log-file "${packageLog}"
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
if [[ "${OSTYPE}" =~ "linux"* ]] || [[ "${OSTYPE}" =~ "darwin"* ]]; then
printf "%s\n" "Agent library build complete. You can now run an example with the agent using \"${PROGNAME} --runAgentExample or --runAgentConverterExample\""
printf "%s\n" "See agent/README.md for more information"
else
err_log "unknown OS type: \"${OSTYPE}\". Please check package in \"${JMC_DIR}/agent/target\""
exit 1
fi
}
function clean() {
echo "$(date +%T) running clean up"
mvn clean
pushd core 1> /dev/null || {
err_log "directory core not found"
exit 1
}
mvn clean
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
pushd agent 1> /dev/null || {
err_log "directory agent not found"
exit 1
}
mvn clean
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
pushd releng/third-party 1> /dev/null || {
err_log "directory releng/third-party not found"
exit 1
}
mvn clean
popd 1> /dev/null || {
err_log "could not go to project root directory"
exit 1
}
}
function run() {
local path
if [[ "${OSTYPE}" =~ "linux"* ]]; then
path="${BASEDIR}/products/org.openjdk.jmc/linux/gtk/x86_64/JDK Mission Control/jmc"
elif [[ "${OSTYPE}" =~ "darwin"* ]]; then
path="${BASEDIR}/products/org.openjdk.jmc/macosx/cocoa/x86_64/JDK Mission Control.app/Contents/MacOS/jmc"
else
err_log "unknown OS type: ${OSTYPE}"
exit 1
fi
if [ -f "${path}" ]; then
exec "${path}"
else
err_log "JMC not found in \"${path}\". Did you call --packageJmc before?"
exit 1
fi
}
function runAgentByClass() {
local agentExampleClass=$1
if [[ -z "${agentExampleClass}" ]]; then
err_log "error: try to run undefined agent class, empty class"
printHelp
exit 1
else
printf "example agent class: %s\n" "${agentExampleClass}"
fi
checkJava
if [[ "${OSTYPE}" =~ "linux"* ]] || [[ "${OSTYPE}" =~ "darwin"* ]]; then
printf "%s\n" "try to execute an agent example"
else
err_log "unknown OS type: \"${OSTYPE}\". Please check package ${JMC_DIR}/agent README.MD"
exit 1
fi
local javaVersion=`java -version 2>&1 | head -1 | cut -d '"' -f 2 | sed 's/^1\.//' | cut -d '.' -f 1`
if ! [ "$javaVersion" -eq "$javaVersion" ] 2> /dev/null; then
printf "%s\n" "WARNING: java version not recognized"
javaVersion=15
fi
printf "Java Version:%s\n" "${javaVersion}"
local pathToAgentTargetDir="${JMC_DIR}/agent/target"
local pathToAgentJar="${pathToAgentTargetDir}/org.openjdk.jmc.agent-1.0.0-SNAPSHOT.jar"
printf "Agent path:%s\n" "${pathToAgentJar}"
if [ -f "${pathToAgentJar}" ]; then
if [ "$javaVersion" -lt "8" ]; then
printf "min. required java version is 8"
exit 1
elif [ "$javaVersion" -eq "8" ]; then
java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -javaagent:${pathToAgentJar}=${pathToAgentTargetDir}/test-classes/org/openjdk/jmc/agent/test/jfrprobes_template.xml -cp ${pathToAgentJar}:${pathToAgentTargetDir}/test-classes/ ${agentExampleClass}
elif [ "$javaVersion" -lt "13" ]; then
java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -XX:+FlightRecorder -javaagent:${pathToAgentJar}=${pathToAgentTargetDir}/test-classes/org/openjdk/jmc/agent/test/jfrprobes_template.xml -cp ${pathToAgentJar}:${pathToAgentTargetDir}/test-classes/ ${agentExampleClass}
else
java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -javaagent:${pathToAgentJar}=${pathToAgentTargetDir}/test-classes/org/openjdk/jmc/agent/test/jfrprobes_template.xml -cp ${pathToAgentJar}:${pathToAgentTargetDir}/test-classes/ ${agentExampleClass}
fi
else
err_log "Agent not found in \"${pathToAgentJar}\". Did you call --packageAgent before?"
exit 1
fi
}
function parseArgs() {
local timestamp="$(date +%Y%m%d%H%M%S)"
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
printHelp
exit 0
;;
--test)
runTests $timestamp
;;
--testUi)
runUiTests $timestamp
;;
--installCore)
installCore $timestamp
;;
--packageJmc)
packageJmc $timestamp
;;
--packageAgent)
packageAgent $timestamp
;;
--clean)
clean
;;
--run)
run
;;
--runAgentExample)
runAgentByClass "org.openjdk.jmc.agent.test.InstrumentMe"
;;
--runAgentConverterExample)
runAgentByClass "org.openjdk.jmc.agent.converters.test.InstrumentMeConverter"
;;
*)
err_log "unknown arguments: $@"
printHelp
exit 1
;;
esac
shift
done
}
function checkJava() {
if [[ -z "$JAVA_HOME" ]]; then
echo "JAVA_HOME is not defined"
fi
if ! command -v java &> /dev/null ; then
err_log "It seems you do not have java installed. Please ensure you have it installed and executable as \"java\"."
exit 1
fi
}
function checkPreconditions() {
checkJava
if ! command -v mvn &> /dev/null ; then
err_log "It seems you do not have maven installed. Please ensure you have it installed and executable as \"mvn\"."
exit 1
fi
BASEDIR=$(mvn help:evaluate -Dexpression=project.build.directory --non-recursive -q -DforceStdout)
JMC_DIR=$(pwd)
mkdir -p "${BASEDIR}" # just in case clean was called before
}
checkPreconditions
parseArgs "$@"