summaryrefslogtreecommitdiff
path: root/build_stage3_package.sh
blob: c2652123c4284d62cc0e1f024d90e195326a3cf1 (plain)
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
#!/bin/sh

# shellcheck source=./default.conf
. "./default.conf"

# builds and installs one package for stage 3

if test "$(id -u)" = 0; then
	sudo -u cross "$0" "$1"
	exit 0
fi

PACKAGE=$1

# draw in default values for build variables

. "$SCRIPT_DIR/$TARGET_CPU-stage3/template/DESCR"

if test "$(find "$STAGE3_PACKAGES" -regex ".*/$PACKAGE-.*pkg\\.tar\\.xz" | wc -l)" = 0; then
	echo "Building package $PACKAGE."

	cd $STAGE3_BUILD || exit 1
	
	# clean up old build
	
	sudo rm -rf "$PACKAGE"
	rm -f "$STAGE3_PACKAGES/$PACKAGE"-*pkg.tar.xz
	ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP rm -rf "/build/$PACKAGE"

	# check out the package build information from the upstream git rep
	# using asp (or from the AUR using yaourt)
	
	PACKAGE_DIR="$SCRIPT_DIR/$TARGET_CPU-stage3/$PACKAGE"
	PACKAGE_CONF="$PACKAGE_DIR/DESCR"	
	if test -f "$PACKAGE_CONF"; then
		if test "$(grep -c FETCH_METHOD "$PACKAGE_CONF")" = 1; then
			FETCH_METHOD=$(grep FETCH_METHOD "$PACKAGE_CONF" | cut -f 2 -d = | tr -d '"')
		fi
	fi
	case $FETCH_METHOD in
		"asp")
			$ASP export "$PACKAGE"
			;;
		"yaourt")
			yaourt -G "$PACKAGE"
			;;
		"packages32")
			# (we assume, we only take core packages)
			cp -a "$ARCHLINUX32_PACKAGES/core/$PACKAGE" .
			;;
		*)
			echo "ERROR: unknown FETCH_METHOD '$FETCH_METHOD'.." >&2
			exit 1
	esac
			
	cd "$PACKAGE" || exit 1

	# attach our destination platform to be a supported architecture
	sed -i "/^arch=[^#]*any/!{/^arch=(/s/(/($TARGET_CPU /}" PKGBUILD

	# if there is a packages32 diff-PKGBUILD, attach it at the end
	# (we assume, we build only 'core' packages during stage1)
	DIFF_PKGBUILD="$ARCHLINUX32_PACKAGES/core/$PACKAGE/PKGBUILD"
	if test -f "$DIFF_PKGBUILD"; then
		cat "$DIFF_PKGBUILD" >> PKGBUILD
	fi

	# copy all other files from Archlinux32, if they exist
	# (we assume, we only take core packages during stage1)
	if test -f "$DIFF_PKGBUILD"; then
		find "$ARCHLINUX32_PACKAGES/core/$PACKAGE"/* ! -name PKGBUILD \
			-exec cp {} . \;
	fi
		
	# source package descriptions, sets variables for this script
	# and executes whatever is needed to build the package

	if test -f "$PACKAGE_CONF"; then
		. "$PACKAGE_CONF"
	fi

	# copy all files into the build area on the target machine 
	# (but the package DESCR file)
	
	if test -d "$PACKAGE_DIR"; then
		find "$PACKAGE_DIR"/* ! -name DESCR \
			-exec cp {} . \;
	fi
	
	# execute makepkg on the host
	# we would actually like to have a mode like 'download, and noextract' but
	# makepkg is not doing that (see -e and -o options)
	
	makepkg --nobuild
	rm -rf "$STAGE3_BUILD/$PACKAGE/src"

	# copy everything to the stage 1 machine
	
	scp -i $CROSS_HOME/.ssh/id_rsa -rC "$STAGE3_BUILD/$PACKAGE" build@$STAGE1_MACHINE_IP:/build/.

	# building the actual package

	echo "Building $PACKAGE on target.."
	
	ssh -i $CROSS_HOME/.ssh/id_rsa build@$STAGE1_MACHINE_IP bash -c "'cd $PACKAGE && makepkg --skipchecksums --skippgpcheck --nocheck'" > $PACKAGE.log 2>&1
	RES=$?
	
	tail "$PACKAGE.log"

	if test $RES = 0; then
	
		echo "Package $PACKAGE built sucessfully, installing on target.."

		# copy to our package folder in the first stage chroot
		
		ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP bash -c "'
			cd /build/$PACKAGE
			rm -f ./*debug*.pkg.tar.xz
			cp -v ./*.pkg.tar.xz /packages/$TARGET_CPU/.
		'"

		# redo the whole pacman cache and repo (always running into trouble
		# there, packages seem to reappear in old versions)

		ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP bash -c "'
#			rm -rf /var/cache/pacman/pkg/*
#			rm -rf /packages/$TARGET_CPU/temp.db*
#			rm -rf /packages/$TARGET_CPU/temp.files*
#			repo-add /packages/$TARGET_CPU/temp.db.tar.gz /packages/$TARGET_CPU/*pkg.tar.xz
		'"
		
		# install onto stage 1 system via pacman

		if test "$FORCE_INSTALL"; then
			FORCE="--force"
		fi
		
		ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP bash -c "'		
			# TODO: broken [temp] repo
			if test \"$ADDITIONAL_INSTALL_PACKAGE\" != \"\"; then
				#pacman $FORCE --noconfirm -Syy $PACKAGE $ADDITIONAL_INSTALL_PACKAGE
				pacman $FORCE --noconfirm -U /packages/$TARGET_CPU/$PACKAGE-*.pkg.tar.xz /packages/$TARGET_CPU/$ADDITIONAL_INSTALL_PACKAGE-*.pkg.tar.xz
			else
				#pacman $FORCE --noconfirm -Syy $PACKAGE
				pacman $FORCE --noconfirm -U /packages/$TARGET_CPU/$PACKAGE-*.pkg.tar.xz
			fi
		'"
		
		# copy packages from target machine and replace our local version with it

		echo "Salvaging build environment of $PACKAGE from target back to host.."

		tmp_dir=$(mktemp -d 'tmp.compute-dependencies.0.XXXXXXXXXX' --tmpdir)
		trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT
		
		cd $STAGE3_BUILD || exit 1
		mv "$STAGE3_BUILD/$PACKAGE/$PACKAGE.log" "$tmp_dir"
		cd "$STAGE3_BUILD" || exit 1
		rm -rf "$PACKAGE"
		ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP bash -c "'cd /build && tar zcf $PACKAGE.tar.gz $PACKAGE/'"		
		scp -i $CROSS_HOME/.ssh/id_rsa -rC build@$STAGE1_MACHINE_IP:/build/"$PACKAGE.tar.gz" "$STAGE3_BUILD/."
		ssh -i $CROSS_HOME/.ssh/id_rsa root@$STAGE1_MACHINE_IP bash -c "'cd /build && rm -f $PACKAGE.tar.gz'"		
		tar zxf "$PACKAGE.tar.gz"
		rm -f "$PACKAGE.tar.gz"
		mv "$tmp_dir/$PACKAGE.log" "$STAGE3_BUILD/$PACKAGE/."
		mv -vf "$STAGE3_BUILD/$PACKAGE/"*.pkg.tar.xz "$STAGE3_PACKAGES/."

		echo "Built package $PACKAGE."
		
	else	
		echo "ERROR building package $PACKAGE"
		exit 1
	fi

	cd $STAGE3_BUILD || exit 1

else
	echo "$PACKAGE exists."
fi