-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·495 lines (431 loc) · 11.9 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/bin/bash
# Copyright (C) 2009 all rights reserved
# GNU GENERAL PUBLIC LICENSE VERSION 3.0
# Author [email protected]
COMPILER=""
LINKY=""
D=`dirname "$0"`
B=`basename "$0"`
FULL="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
HERE=$(dirname "$FULL")
IDIR=$HERE/src
CPROOT=`date +"tmp-pkgroot-%s"`
SRCROOT="$GOROOT/src/pkg"
UP_ONE=""
ONLYGCC=""
# array to store packages which are pure go
declare -a package;
# this is done statically for now, no grepping
# to figure out which packges are actually pure go..
package=(
'archive'
'compress'
'container'
'flag'
'go'
'html'
'http'
'image'
'mime'
'patch'
'rpc'
'strconv'
'tabwriter'
'template'
'io'
## packages above this line cannot be tested without modification
'asn1'
'bufio'
'cmath'
'ebnf'
'encoding'
'expvar'
'fmt'
'gob'
'hash'
'index'
'json'
'log'
'netchan'
'rand'
'reflect'
'regexp'
'scanner'
'smtp'
'sort'
'strings'
'syslog'
'testing'
'try'
'unicode'
'unsafe'
'utf16'
'utf8'
'websocket'
'xml'
)
function build(){
gcsanity
echo -n "build "
cd src/utilz && $COMPILER walker.go || exit 1
$COMPILER handy.go || exit 1
$COMPILER stringset.go || exit 1
$COMPILER stringbuffer.go || exit 1
$COMPILER global.go || exit 1
$COMPILER timer.go || exit 1
$COMPILER say.go || exit 1
cd $HERE/src/parse && $COMPILER -o gopt.$OBJ option.go gopt.go || exit 1
cd $HERE/src/cmplr && $COMPILER -I $IDIR dag.go || exit 1
$COMPILER -I $IDIR compiler.go || exit 1
cd $HERE/src/start && $COMPILER -I $IDIR main.go || exit 1
cd $HERE && $LINKY -o mgd -L src src/start/main.? || exit 1
echo "...done"
}
function gccbuild(){
echo -n "gccgo "
gccgo -I src -c -o src/utilz/stringbuffer.o src/utilz/stringbuffer.go || exit 1
gccgo -I src -c -o src/utilz/walker.o src/utilz/walker.go || exit 1
gccgo -I src -c -o src/utilz/stringset.o src/utilz/stringset.go || exit 1
gccgo -I src -c -o src/utilz/timer.o src/utilz/timer.go || exit 1
gccgo -I src -c -o src/parse/gopt.o src/parse/gopt.go src/parse/option.go || exit 1
gccgo -I src -c -o src/utilz/global.o src/utilz/global.go || exit 1
gccgo -I src -c -o src/utilz/handy.o src/utilz/handy.go || exit 1
gccgo -I src -c -o src/utilz/say.o src/utilz/say.go || exit 1
gccgo -I src -c -o src/cmplr/dag.o src/cmplr/dag.go || exit 1
gccgo -I src -c -o src/cmplr/compiler.o src/cmplr/compiler.go || exit 1
gccgo -I src -c -o src/start/main.o src/start/main.go || exit 1
gccgo -o mgd -static src/start/main.o src/parse/gopt.o\
src/utilz/stringset.o src/utilz/handy.o\
src/utilz/stringbuffer.o src/utilz/walker.o\
src/cmplr/dag.o src/utilz/say.o\
src/utilz/global.o src/cmplr/compiler.o\
src/utilz/timer.o || exit 1
echo "...done"
}
function clean(){
echo -n "clean"
cd $HERE
rm -rf src/utilz/walker.?
rm -rf src/utilz/stringset.?
rm -rf src/utilz/stringbuffer.?
rm -rf src/utilz/global.?
rm -rf src/utilz/utilz_test.?
rm -rf src/utilz/handy.?
rm -rf src/utilz/timer.?
rm -rf src/utilz/say.?
rm -rf src/cmplr/dag.?
rm -rf src/cmplr/compiler.?
rm -rf src/parse/gopt.?
rm -rf src/parse/gopt_test.?
rm -rf src/parse/option.?
rm -rf src/start/main.?
rm -rf mgd
rm -rf "$HOME/bin/mgd"
rm -rf "$GOBIN/mgd"
echo " ...done"
}
function move(){
cd "$HERE"
if [ -f "mgd" ]; then
echo -n "move"
if [ -d "${HOME}/bin" ]; then
cd "$HERE"
mv mgd "$HOME/bin"
else
if [ -d "$GOBIN" ]; then
cd "$HERE"
mv mgd "$GOBIN"
else
echo -e "\n[ERROR] \$HOME/bin: not a directory"
echo -e "[ERROR] \$GOBIN : not set\n"
fi
fi
echo " ...done"
else
echo "'mgd' not found, nothing to move"
exit 1
fi
}
function phelp(){
cat <<EOH
build.sh - utility script for godag
targets:
help : print this menu and exit
clean : rm *.[865a] from src + rm mgd \$HOME/bin/mgd \$GOBIN/mgd
build : compile source code in ./src
gbuild : compile source code in ./src using gccgo
move : move 'mgd' to \$HOME/bin (\$GOBIN fallback)
install : clean + build + move (DEFAULT)
ginstall: clean + gbuild + move
cproot : copy modified (pure go) part of \$GOROOT/src/pkg
stdlib : copy original (pure go) part of \$GOROOT/src/pkg
testok : copy partial stdlib that can be tested without modification
debian : build a debian package (godag_0.2-0_${GOARCH}.deb)
gdebian : build a debian package (godag_0.2-0_${GOARCH}.deb) using gccgo
EOH
}
function die(){
echo "$1"
exit 1
}
# recursively copy all the $GOROOT/src/pkg to $CPROOT,
# with a *.go filter, any test that includes testdata will fail.
# NOTE main packages are also removed, these are used for testing
# and since too many of these end up in the same name-space, they
# are all removed here..
function recursive_copy(){
mkdir "$2"
for i in $(ls "$1");
do
if [ -f "$1/$i" ]; then
case "$i" in *.go)
grep "^package main$" -q "$1/$i" || cp "$1/$i" "$2/$i"
esac
fi
if [ -d "$1/$i" ]; then
if [ ! "$i" == "testdata" ];then
recursive_copy "$1/$i" "$2/$i"
fi
fi
done
return 1
}
# move all go packages up one level, and give them
# a fitting header based on directory..
function up_one_level(){
for element in $(ls $1);
do
if [ -f "$1/$element" ]; then
mv "$1/$element" "${1}/${2}_${element}"
mv "${1}/${2}_${element}" "${1}/.."
fi
if [ -d "$1/$element" ]; then
up_one_level "$1/$element" "$element"
fi
done
return 1
}
function cproot(){
gcsanity
mkdir "$CPROOT";
echo "cp *.go: \$GOROOT/src/pkg -> $CPROOT"
echo "this may take some time..."
for p in "${package[@]}";
do
recursive_copy "$SRCROOT/$p" "$CPROOT/$p"
done
if [ "$UP_ONE" ];then
up_one_level "$CPROOT" "$CPROOT"
fi
# delete empty directories from $CPROOT
find -depth -type d -empty -exec rmdir {} \;
exit 0
}
function testok(){
cnt=15
for((i = 0; i < cnt; i++))
do
unset package[$i]
done
cproot
}
# this target does not show up in help message :-)
function rmstdlib(){
for p in "${package[@]}";
do
echo "rm -rf ${GOROOT}/pkg/${GOOS}_${GOARCH}/${p}.*"
rm -rf "${GOROOT}/pkg/${GOOS}_${GOARCH}/${p}".*
done
}
# default target clean + build + move
function triple(){
clean
build
move
}
# same as triple but build with gccgo
function gtriple(){
clean
gccbuild
move
}
# Make sure we have all binaries needed in order to build debian package
function sanity(){
if [ -d ".hg" ];then
pathfind 'hg' || die "[ERROR] missing 'hg' (mercurial)"
else # git
pathfind 'git' || die "[ERROR] missing 'hg' (mercurial)"
fi
if [ "$ONLYGCC" ];then
pathfind 'gccgo' || die "[ERROR] missing 'gccgo'"
fi
pathfind 'gzip' || die "[ERROR] missing 'gzip'"
pathfind 'md5sum' || die "[ERROR] missing 'md5sum'"
pathfind 'dpkg-deb' || die "[ERROR] missing 'dpkg-deb'"
pathfind 'fakeroot' || die "[ERROR] missing 'fakeroot'"
# Not too many systems lacking these (coreutils) but still
pathfind 'cp' || die "[ERROR] missing 'cp'"
pathfind 'mkdir' || die "[ERROR] missing 'mkdir'"
pathfind 'mv' || die "[ERROR] missing 'mv'"
pathfind 'du' || die "[ERROR] missing 'du'"
pathfind 'chmod' || die "[ERROR] missing 'chmod'"
pathfind 'printf' || die "[ERROR] missing 'printf'"
}
# Taken from Debian Developers Reference Chapter 6
function pathfind(){
OLDIFS="$IFS"
IFS=:
for p in $PATH; do
if [ -x "$p/$*" ]; then
IFS="$OLDIFS"
return 0
fi
done
IFS="$OLDIFS"
return 1
}
# create a debian package of godag, not the prettiest function..
function debian() {
# do you have what it takes to build a deb?
sanity
# NOTE: does not depend on anything yet :-)
DEBCONTROL="Package: godag
Version: 0.2
Section: devel
Priority: optional
Architecture: %s
Depends:
Suggests: gccgo,golang
Conflicts:
Replaces:
Installed-Size: %d
Maintainer: Bjarne Holen <[email protected]>
Description: Golang/Go compiler front-end.
Godag automatically builds projects written in golang,
by inspecting source-code imports to calculate compile order.
Unit-testing, formatting and installation of external
libraries are also automated. The default back-end is gc,
other back-ends have partial support: gccgo, express.
"
DEBCOPYRIGHT="Name: godag
Maintainer: bjarne holen <[email protected]>
Source: https://godag.googlecode.com/hg
Files: *
Copyright: 2011, bjarne holen <[email protected]>
License: GPL-3
License: GPL-3
On Debian systems, the complete text of the GNU General Public License
version 3 can be found in '/usr/share/common-licenses/GPL-3'.
"
DEBCHANGELOG="godag (0.2.0) devel; urgency=low
* The actual changelog can be found in changelog...
-- Bjarne Holen <[email protected]> Thu, 05 May 2011 14:07:28 -0400
"
if [ "$GOARCH" = "386" ]; then
DEBARCH="i386"
else
DEBARCH="$GOARCH"
fi
if [ "$ONLYGCC" ];then
gccbuild
else
build
fi
mkdir -p ./debian/DEBIAN
mkdir -p ./debian/usr/bin
mkdir -p ./debian/usr/share/man/man1
mkdir -p ./debian/usr/share/doc/godag
mkdir -p ./debian/etc/bash_completion.d
chmod -R 755 ./debian
mv mgd ./debian/usr/bin
cp ./util/gd-completion.sh ./debian/etc/bash_completion.d/gd
cat ./util/gd.1 | gzip --best - > ./debian/usr/share/man/man1/gd.1.gz
cat ./util/gd.1 | gzip --best - > ./debian/usr/share/man/man1/godag.1.gz
echo "$DEBCOPYRIGHT" > ./debian/usr/share/doc/godag/copyright
if [ -d ".hg" ];then
hg log | gzip --best - > ./debian/usr/share/doc/godag/changelog.gz
else # git
git log | gzip --best - > ./debian/usr/share/doc/godag/changelog.gz
fi
echo "$DEBCHANGELOG" | gzip --best - > ./debian/usr/share/doc/godag/changelog.Debian.gz
arr=($(du -s ./debian))
printf "$DEBCONTROL" "$DEBARCH" "${arr[0]}" > ./debian/DEBIAN/control
echo "/etc/bash_completion.d/gd" > ./debian/DEBIAN/conffiles
fakeroot dpkg-deb --build ./debian
mv debian.deb "godag_0.2-0_$DEBARCH.deb"
rm -rf ./debian
}
function gcsanity(){
[ "$GOROOT" ] || die "[ERROR] missing \$GOROOT"
[ "$GOARCH" ] || die "[ERROR] missing \$GOARCH, You must export GOARCH=\"`ls $GOROOT/pkg/ | cut -d '_' -f 2`\""
[ "$GOOS" ] || die "[ERROR] missing \$GOOS, You must export GOOS=\"`ls $GOROOT/pkg/ | cut -d '_' -f 1`\""
[ "$GOBIN" ] || die "[ERROR] missing \$GOBIN, You must export GOBIN="$GOROOT/bin""
case "$GOARCH" in
'386')
COMPILER="8g"
LINKY="8l"
OBJ="8"
;;
'arm')
COMPILER="5g"
LINKY="5l"
OBJ="5"
;;
'amd64')
COMPILER="6g"
LINKY="6l"
OBJ="6"
;;
*)
echo "architecture not: 'amd64' '386' 'arm'"
echo "architecture was: ${GOARCH}"
exit 1
;;
esac
}
# main
{
case "$1" in
'help' | '-h' | '--help' | '-help')
phelp
;;
'cproot' | '--cproot' | '-cproot')
UP_ONE="yes"
time cproot
;;
'stdlib' | '-stdlib' | '--stdlib')
time cproot
;;
'testok' | '-testok' | '--testok')
time testok
;;
'clean' | 'c' | '-c' | '--clean' | '-clean')
time clean
;;
'build' | 'b' | '-b' | '--build' | '-build')
time build
;;
'gbuild' | 'g' | '-g' | '--gbuild' | '-gbuild')
time gccbuild
;;
'move' | 'm' | '-m' | '--move' | '-move')
time move
;;
'del' | '--del' | '-del')
rmstdlib
;;
'debian' | '--debian' | '-debian')
debian
;;
'gdebian' | '--gdebian' | '-gdebian')
ONLYGCC="yes"
debian
;;
'ginstall' | '--ginstall' | '-ginstall')
time gtriple
;;
*)
time triple
;;
esac
}