summaryrefslogtreecommitdiff
path: root/ibps-src
blob: c7e38a83c59a7ebe40dca937e29b625f3bd90cb3 (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
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
#!/bin/bash

## This project is heavily influenced by xbps, which I just really like as
## a package manager. So if you are seeing similar patterns, they are very
## similar.

#set -e

MIRROR_GNU_FTP="https://ftp.gnu.org/gnu"

if [ -z "${CACHE}" ]
then
	CACHE="$(pwd)/cache/"
fi

mkdir -p "${CACHE}"

if [ -z "${MASTERDIR}" ]
then
	MASTERDIR="$(pwd)/env"
fi

if [ -z "${DISTRO_TC}" ]
then
	DISTRO_TC="x86_64-x221e-linux-gnu"
fi

PY_ROOT="/usr/lib/python3.14/site-packages/"

COMPILED_DIR="$(pwd)/compiled"

function usage {
	echo -e "You must supply a valid argument!"
	echo -e "Available arguments: "
	echo -e "    pkg - installs and provides the specified package."
}

if [ $# -eq 0 ]
then
    usage
    exit 1
fi

## Commands
## ---------
##      ibps-src bootstrap-rootfs
##	ibps-src pkg <package_name>
##

function bootstrap-rootfs {
	pkg "perl" "true"
	pkg "libxcrypt" "true"
	pkg "openssl3" "true"
	pkg "python" "true"
	pkg "pip" "true"
	pkg "bison" "true"
	pkg "libidn2" "true"
	pkg "cmake" "true"
	pkg "ninja" "true"	
	pkg "meson" "true"
	pkg "gmp" "true"
	pkg "mpc" "true"
	pkg "mpfr" "true"
	pkg "gcc" "true"
}

function msg_err {
	msg=$1
	echo -e "$msg"
	exit 1
}

function env_run {
	command="$1"

	doas mount --bind /dev "${MASTERDIR}/dev"
	doas mount --make-rslave   "${MASTERDIR}/dev"
	doas mount --bind /dev/pts "${MASTERDIR}/dev/pts"
	doas mount --bind /proc "${MASTERDIR}/proc"
	doas mount --bind /sys "${MASTERDIR}/sys"
	doas mount --bind /tmp "${MASTERDIR}/tmp"
	doas mount --bind "$(pwd)/compiled" "${MASTERDIR}/compiled"

	unshare --pid -r --fork --user --mount chroot ${MASTERDIR} /bin/bash -c "${command}" 

	doas umount "${MASTERDIR}/compiled"
	doas umount "${MASTERDIR}/tmp"
	doas umount "${MASTERDIR}/sys"
	doas umount "${MASTERDIR}/proc"
	doas umount "${MASTERDIR}/dev/pts"
	doas umount "${MASTERDIR}/dev"
}


function chroot {
	doas mount --bind /dev "${MASTERDIR}/dev"
	doas mount --make-rslave   "${MASTERDIR}/dev"
	doas mount --bind /dev/pts "${MASTERDIR}/dev/pts"
	doas mount --bind /proc "${MASTERDIR}/proc"
	doas mount --bind /sys "${MASTERDIR}/sys"
	doas mount --bind /tmp "${MASTERDIR}/tmp"
	doas mount --bind "$(pwd)/compiled" "${MASTERDIR}/compiled"

	unshare --pid -r --fork --user --mount chroot ${MASTERDIR} /bin/bash 

	doas umount "${MASTERDIR}/compiled"
	doas umount "${MASTERDIR}/tmp"
	doas umount "${MASTERDIR}/sys"
	doas umount "${MASTERDIR}/proc"
	doas umount "${MASTERDIR}/dev/pts"
	doas umount "${MASTERDIR}/dev"
}

function download_if_uncached {
	pkgname="$1"
	version="$2"
	upstream="$3"
	
	if [[ ! -e "${CACHE}/upstream/${pkgname}-${version}.tar" || ! -f "${CACHE}/upstream/${pkgname}-${version}.tar" ]]
	then
		mkdir -p "${CACHE}/upstream"
		curl --output-dir="${CACHE}/upstream" -L -o "${pkgname}-${version}.tar" $upstream 
	fi
}

function extract_archive {
	currdir=$(pwd)
		
	cd "${MASTERDIR}/build/"
	tar -xf "${MASTERDIR}/build/${pkgname}-${version}.tar"

	cd "${currdir}"
}

function package_app {
	pkgname="$1"
	version="$2"
	build_no="$3"

	pkgdir="${pkgname}-${version}-${build_no}"

	if [ ! -e "${COMPILED_DIR}/${pkgdir}" ]
	then
		echo -e "error: cannot package '${pkgdir}', is not located under "${COMPILED_DIR}". Build problem?"
		exit 1
	fi

	for i in $(find "${COMPILED_DIR}/${pkgdir}" -type f);
	do
		echo $(ldd "$i" 2>/dev/null)	
	done

	echo "name=${pkgname}" > "${COMPILED_DIR}/${pkgdir}/.desc" 
	echo "description=${description}" >> "${COMPILED_DIR}/${pkgdir}/.desc" 
	echo "version=${version}" >> "${COMPILED_DIR}/${pkgdir}/.desc" 
	echo "build_no=${build_no}" >> "${COMPILED_DIR}/${pkgdir}/.desc" 
	echo "depends=${depends[@]}" >> "${COMPILED_DIR}/${pkgdir}/.desc"

	if [ -e "./templates/${pkgname}/hooks" ] && [ -f "./templates/${pkgname}/hooks" ]; then
	    echo $(cat "./templates/${pkgname}/hooks") >> "${COMPILED_DIR}/${pkgdir}/.desc"
	fi

	currdir="$(pwd)"

	cd "${COMPILED_DIR}"

	tar --zstd -cf "${pkgdir}.ibps" "${pkgdir}" 

	cd "${currdir}"

	mv "$(pwd)/compiled/${pkgdir}.ibps" "$(pwd)/ready/"
}

function apply_patches {
	cp -r "./templates/${pkgname}/patches" "${MASTERDIR}/build/"

	if [[ -e "./templates/${pkgname}/patches" && -d "./templates/${pkgname}/patches" ]]
	then
		currdir="$(pwd)"
		
		if [ -n "${cddir}" ]
		then
			cd "${cddir}"
		else
			cd "${MASTERDIR}/build/${pkgname}-${version}"
		fi

		for i in $(ls ../patches);
		do
			patch -p1 < "../patches/$i" 
		done

		cd "${currdir}"
	fi
}

function pkg {

	local pkg="$1"
	local install_sys="$2"
    
	if [ -z "$pkg" ]
	then
		msg_err "You must specify a package name!"
	fi

	echo -e "Searching for '$pkg' in local repository."

	template_loc="./templates/$pkg/template"
    
	if [ ! -e "$template_loc" ]
	then
		msg_err "Template does not exist!"
	fi
    
	local makedepends pkgname version build_no cddir upstream extract depends extras
	source "${template_loc}"
	echo -e "Found '$pkgname' with version '$version'."

	if [[ bootstrap_only == "true" && install_sys == "false" ]]; then
	    echo -e "This package is only meant to be bootstrapped!"
	    exit 1
	fi
	
	if [ ${#makedepends[@]} -gt 0 ]
	then
		for i in "${makedepends[@]}";
		do
			pkg_exists=$(grep "^${i}=" "${MASTERDIR}/packages")
			if [ -z "${pkg_exists}" ]
			then
				echo -e "Error on: ${i}"
				echo "Please first install the dependencies of '${pkgname}': ${makedepends[@]}"
				exit 1
			fi
		done
		echo "All requirements appear satisfied"
	fi

	download_if_uncached "${pkgname}" "${version}" "${upstream}"

	mkdir -p "${MASTERDIR}/build/"

	for extra in ${extras[@]};
	do
	    curl -OL "$extra" --output-dir="${MASTERDIR}/build/"
	    echo -e "Downloaded ${extra}"
	done

	cp "${CACHE}/upstream/${pkgname}-${version}.tar" "${MASTERDIR}/build/"

	if [[ -z "${extract}" || "${extract}" == "true" ]]
	then
	    extract_archive
	else
	    mkdir -p "${MASTERDIR}/build/${pkgname}-${version}"
	fi
		    
	cp "${template_loc}" "${MASTERDIR}/build/template"
	apply_patches

	if [[ "$(type -t 'do_configure')" == "function" ]]; then
	    cp "stubs/configure" "${MASTERDIR}/build/"
	    env_run "DISTRO_TC=${DISTRO_TC} ./build/configure"       
	fi

	if [[ "$(type -t 'do_build')" == "function" ]]; then
	    cp "stubs/build" "${MASTERDIR}/build/buildstub"
	    env_run "DISTRO_TC=${DISTRO_TC} ./build/buildstub"
	fi

	if [[ "$(type -t 'do_install')" == "function" ]]; then
	    cp "stubs/install" "${MASTERDIR}/build/install"
	fi

	if [[ "${install_sys}" == "true" ]]
	then
		env_run "DISTRO_TC=${DISTRO_TC} DESTDIR=/ PY_SITE_PACKAGES=${PY_ROOT} ./build/install"
		echo "${pkgname}=${version}" >> "${MASTERDIR}/packages"
	else
		env_run "DISTRO_TC=${DISTRO_TC} PY_SITE_PACKAGES=${PY_ROOT} ./build/install"
		package_app "${pkgname}" "${version}" "${build_no}"
	fi
	
	rm -rf "${MASTERDIR}/build"
}

function pkg_all {
	all_templates=$(ls templates/)
	for tmpl in $all_templates[@];
	do
		pkg "${tmpl}"
	done
}

case "$1" in
	"bootstrap-rootfs" ) bootstrap-rootfs ;;
	"pkg" ) pkg "$2" ;;
	"chroot") chroot ;;
	"install" ) pkg "$2" "true" ;;
	"pkg-all" ) pkg_all ;;
	* )  echo -e "Command '$1' not found!" && usage && exit 1 ;;
esac