-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·134 lines (117 loc) · 5.02 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·134 lines (117 loc) · 5.02 KB
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
#!/usr/bin/env bash
#
# build.sh - Assemble the World Bank Open Data Java Calc add-in into
# build/WorldBank.oxt
#
# Pipeline:
# 1. unoidl-write : idl/**.idl -> build/types/XWorldBank.rdb (UNO type library)
# 2. javamaker : build/types/XWorldBank.rdb -> build/gen/**.class (Java bindings)
# 3. javac : src/**.java + bindings -> build/classes/**.class
# 4. jar : classes + bindings -> build/oxt/worldbank.jar (+ RegistrationClassName)
# 5. zip : staging tree -> build/WorldBank.oxt
#
# Requirements:
# - LibreOffice with the SDK installed. Resolved from -l/--libreoffice, else
# $LO_HOME, else /opt/libreoffice*, else ~/libreoffice*.
# - A JDK (any 8+) to compile with. Resolved from -j/--jdk, else JAVA_HOME,
# else PATH. Output targets Java 8 bytecode (--release 8) so it runs on
# the JRE 8 that LibreOffice accepts; see docs/INSTALL.md.
#
# Usage:
# ./build.sh
# ./build.sh --libreoffice ~/libreoffice26.2 --jdk ~/jdks/jdk8u492-b09
set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
libreoffice="${LO_HOME:-}"
jdk="${JAVA_HOME:-}"
while [[ $# -gt 0 ]]; do
case "$1" in
-l|--libreoffice) libreoffice="$2"; shift 2 ;;
-j|--jdk) jdk="$2"; shift 2 ;;
*) echo "Unknown argument: $1" >&2; exit 1 ;;
esac
done
if [[ -z "$libreoffice" ]]; then
for candidate in "$HOME"/libreoffice[0-9]*.[0-9]* /opt/libreoffice[0-9]*.[0-9]*; do
[[ -d "$candidate" ]] && libreoffice="$candidate" && break
done
fi
if [[ -z "$libreoffice" ]]; then
echo "LibreOffice install not found; pass --libreoffice <dir> or set LO_HOME" >&2
exit 1
fi
# --- Tool locations --------------------------------------------------------
sdk_bin="$libreoffice/sdk/bin"
program="$libreoffice/program"
types="$program/types.rdb"
uw="$sdk_bin/unoidl-write"
jmaker="$sdk_bin/javamaker"
if [[ -n "$jdk" ]]; then
javac="$jdk/bin/javac"
jar="$jdk/bin/jar"
[[ -x "$javac" ]] || { echo "javac not found under --jdk/JAVA_HOME: $javac" >&2; exit 1; }
else
javac=javac
jar=jar
fi
for t in "$uw" "$jmaker"; do
[[ -x "$t" ]] || { echo "SDK tool not found: $t (check --libreoffice)" >&2; exit 1; }
done
[[ -f "$types" ]] || { echo "types.rdb not found: $types (check --libreoffice)" >&2; exit 1; }
# unoidl-write and javamaker need the LibreOffice program dir on PATH for .so libs.
export PATH="$program:$PATH"
export LD_LIBRARY_PATH="$program${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
# --- Clean staging -----------------------------------------------------------
build="$root/build"
gen_dir="$build/gen"
cls_dir="$build/classes"
type_dir="$build/types"
stage="$build/oxt"
rm -rf "$gen_dir" "$cls_dir" "$type_dir" "$stage"
mkdir -p "$gen_dir" "$cls_dir" "$type_dir" "$stage/types" "$stage/config" "$stage/META-INF"
# --- 1. Compile IDL -> type library -----------------------------------------
echo '[1/5] unoidl-write : idl -> build/types/XWorldBank.rdb'
rdb="$type_dir/XWorldBank.rdb"
"$uw" "$types" "$root/idl" "$rdb"
# --- 2. Generate Java bindings from the type library ------------------------
echo '[2/5] javamaker : build/types/XWorldBank.rdb -> build/gen'
"$jmaker" -nD -Gc -O "$gen_dir" -X "$types" "$rdb"
# --- 3. Compile Java sources -------------------------------------------------
echo '[3/5] javac : src -> build/classes'
sdk_classes="$program/classes"
cp="$gen_dir:$sdk_classes/*"
mapfile -t sources < <(find "$root/src" -name '*.java')
# JDK 8's javac has no --release flag (added in JDK 9); it only ever targets 8
# anyway, so fall back to -source/-target on it.
javac_version="$("$javac" -version 2>&1 | grep -oE '[0-9]+' | head -1)"
if [[ "$javac_version" != "1" && "$javac_version" -ge 9 ]]; then
release_flags=(--release 8)
else
release_flags=(-source 8 -target 8)
fi
"$javac" "${release_flags[@]}" -cp "$cp" -d "$cls_dir" "${sources[@]}"
# --- 4. Package the jar (with RegistrationClassName manifest) ---------------
echo '[4/5] jar : classes + bindings -> build/oxt/worldbank.jar'
wb_jar="$stage/worldbank.jar"
mf="$root/registration/MANIFEST.MF"
# Merge classes + bindings into one tree first: this JDK's `jar` tool rejects
# duplicate directory entries (e.g. com/example/worldbank/) when given as two
# separate -C trees, even though the class files themselves don't collide.
jar_dir="$build/jarstage"
rm -rf "$jar_dir"
mkdir -p "$jar_dir"
cp -r "$cls_dir"/. "$jar_dir"/
cp -r "$gen_dir"/. "$jar_dir"/
"$jar" cfm "$wb_jar" "$mf" -C "$jar_dir" .
# --- 5. Stage remaining package files and zip the .oxt ----------------------
echo '[5/5] zip : staging -> build/WorldBank.oxt'
cp "$rdb" "$stage/types/XWorldBank.rdb"
cp "$root/registration/CalcAddIns.xcu" "$stage/config/CalcAddIns.xcu"
cp "$root/registration/description.xml" "$stage/description.xml"
cp "$root/registration/manifest.xml" "$stage/META-INF/manifest.xml"
oxt="$build/WorldBank.oxt"
rm -f "$oxt"
( cd "$stage" && zip -q -r "$oxt" . )
echo
echo "Built $oxt"
echo 'Install with: unopkg add --force build/WorldBank.oxt (see docs/INSTALL.md)'