summaryrefslogtreecommitdiff
path: root/newapkbuild.in
blob: b2712d563be750ac3a78753b012ab67032786df6 (plain) (blame)
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
#!/bin/sh
#
# newapkbuild - generate a new APKBUILD
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

program_version=@VERSION@
datadir=@datadir@

if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"


is_url() {
	case "$1" in
	http://*|https://*|ftp://*) return 0;;
	esac
	return 1
}

is_github_url() {
	case $1 in
	https://github.com/*/*/archive/*.tar.gz) return 0;;
	esac
	return 1
}

# Build sections
build_make() {
	cat >>APKBUILD<<__EOF__
	make
__EOF__
}

build_autotools() {
	cat >>APKBUILD<<__EOF__
	./configure \\
		--build=\$CBUILD \\
		--host=\$CHOST \\
		--prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--localstatedir=/var
	make
__EOF__
}

build_cmake() {
# References:
# http://www.cmake.org/Wiki/CMake_Useful_Variables
# http://www.vtk.org/Wiki/CMake_Cross_Compiling
# This is incomplete: CMAKE_{HOST_,}SYSTEM_PROCESSOR needs to be set,
# and likewise CMAKE_FIND_ROOT_PATH and a few other details.

	cat >>APKBUILD<<__EOF__
	if [ "\$CBUILD" != "\$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake \\
		-DCMAKE_INSTALL_PREFIX=/usr \\
		-DCMAKE_INSTALL_LIBDIR=lib \\
		-DBUILD_SHARED_LIBS=True \\
		-DCMAKE_BUILD_TYPE=Release \\
		-DCMAKE_CXX_FLAGS="\$CXXFLAGS" \\
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
		\${CMAKE_CROSSOPTS}
	make
__EOF__
}

build_meson() {
# References:
# http://mesonbuild.com/Reference-manual.html
# http://mesonbuild.com/Cross-compilation.html
# TODO For cross compilation a cross_file needs to be created.

	cat >>APKBUILD<<__EOF__
	meson \\
		--prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--localstatedir=/var \\
		--buildtype=release \\
		. output
	ninja -C output
__EOF__
}

build_perl() {
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor
	make
__EOF__
}

build_python() {
	cat >>APKBUILD<<__EOF__
	python3 setup.py build
__EOF__
}

check_make() {
	cat >>APKBUILD<<__EOF__
	make check
__EOF__
}

check_python() {
	cat >>APKBUILD<<__EOF__
	python3 setup.py test
__EOF__
}

# Package sections
package_make() {
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install
__EOF__
}

package_autotools() {
	package_make
}

package_meson() {
	cat >>APKBUILD<<__EOF__
	DESTDIR="\$pkgdir" ninja -C output install
__EOF__
}

package_perl() {
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

package_python() {
	cat >>APKBUILD<<__EOF__
	python3 setup.py install --prefix=/usr --root="\$pkgdir"
__EOF__
}

# Create new aport from templates
newaport() {
	local newname="${1##*/}"
	local pn=${newname%-[0-9]*}
	local pv
	local source=
	is_url "$1" && source="$1"

	if is_github_url $source; then
		if [ -z "$pkgname" ]; then
			pkgname=${source%/archive/*}
			pkgname=${pkgname##*/}
		fi
		pv=${newname%.t*} #strip .tar.gz .tgz .tar.bz2 etc
		pv=${pv#*[a-z]}
		source="$pkgname-$pv.tar.gz::$source"
	elif [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
	fi
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exists"
		return 1
	fi
	mkdir -p "$pkgname"
	cd "$pkgname"

	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
		depends="python3"
	fi

	case "$buildtype" in
	python) makedepends="python3-dev";;
	cmake)  makedepends="cmake";;
	meson)  makedepends="meson";;
	*)      makedepends="\$depends_dev";;
	esac

	# Replace pkgver in $source
	if [ -n "$source" ]; then
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
	fi

	# Copy init.d scripts if requested
	if [ -n "$cpinitd" ]; then
		cp "$datadir"/sample.initd $pkgname.initd
		cp "$datadir"/sample.confd $pkgname.confd
		cp "$datadir"/sample.pre-install $pkgname.pre-install
		cp "$datadir"/sample.post-install $pkgname.post-install
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
	$pkgname.initd
	$pkgname.confd
	"
	fi

	# Generate header with standard variables
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
pkgname=$pkgname
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
depends="$depends"
depends_dev=""
makedepends="$makedepends"
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"
__EOF__

	abuild -f fetch checksum unpack
	# Figure out the builddir
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
			builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
		fi
	done
	printf 'builddir="$srcdir/%s"\n\n' "$builddir" >> APKBUILD

	# Subpackage -dev is usually required only for C/C++. Since depends_dev
	# confuses a lot people, remove it if there's no .h or .hpp file.
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

	# Try to autodetect the buildtype
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -r "$sdir"/meson.build ]; then
			buildtype="meson"
		elif [ -d "$sdir"/cmake ] || [ -r "$sdir/CMakeLists.txt" ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
		fi
	fi

	# Create build() function
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$builddir"
__EOF__

	case "$buildtype" in
	make)
		build_make;;
	cmake)
		build_cmake;;
	meson)
		build_meson;;
	autotools)
		build_autotools;;
	perl)
		build_perl;;
	python)
		build_python;;
	esac

	cat >>APKBUILD<<__EOF__
}

__EOF__

	# Create check() function
	cat >>APKBUILD<<__EOF__
check() {
	cd "\$builddir"
__EOF__

	case "$buildtype" in
	make|cmake|autotools|perl)
		check_make;;
	python)
		check_python;;
	esac

	cat >>APKBUILD<<__EOF__
}

__EOF__

	# Create package() function
	cat >>APKBUILD<<__EOF__
package() {
	cd "\$builddir"
__EOF__

	case "$buildtype" in
	make|cmake)
		package_make;;
	autotools)
		package_autotools;;
	meson)
		package_meson;;
	perl)
		package_perl;;
	python)
		package_python;;
	esac

	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
		"\$pkgdir"/etc/init.d/\$pkgname
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
		"\$pkgdir"/etc/conf.d/\$pkgname
__EOF__
	fi
	cat >>APKBUILD<<__EOF__
}

__EOF__
}

usage() {
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
		Usage: $program [-n PKGNAME] [-d PKGDESC] [-l LICENSE] [-u URL]
		       [-a | -C | -m | -p | -y] [-s] [-c] [-f] [-h]
		       PKGNAME[-PKGVER] | SRCURL
		Options:
		  -n  Set package name to PKGNAME (only use with SRCURL)
		  -d  Set package description to PKGDESC
		  -l  Set package license to LICENSE, use identifiers from:
		      <https://spdx.org/licenses/>
		  -u  Set package URL
		  -a  Create autotools package (use ./configure ...)
		  -C  Create CMake package (Assume cmake/ is there)
		  -m  Create meson package (Assume meson.build is there)
		  -p  Create perl package (Assume Makefile.PL is there)
		  -y  Create python package (Assume setup.py is there)
		  -s  Use sourceforge source URL
		  -c  Copy a sample init.d, conf.d, and install script
		  -f  Force even if directory already exists
		  -h  Show this help
	__EOF__
}

set_buildtype() {
	if [ -n "$buildtype" ]; then
		error "More than one buildtype flag specified ($buildtype and $1)"
		exit 1
	fi
	buildtype="$1"
}

check_arguments() {
	if [ $# -eq 0 ]; then
		error "Missing required argument: PKGNAME[-PKGVER] | SRCURL"
		exit 1
	fi
	if [ $# -gt 1 ]; then
		shift
		error "Unrecognized arguments: $*"
		exit 1
	fi
	if ! is_url "$1" && [ -n "$pkgname" ]; then
		error "-n is only allowed when using SRCURL as last argument"
		exit 1
	fi
	if is_url "$1" && [ -n "$sourceforge" ]; then
		error "-s is only allowed when using PKGNAME as last argument"
		exit 1
	fi
}

while getopts "acCmd:fhl:n:pyu:s" opt; do
	case $opt in
		'a') set_buildtype "autotools";;
		'c') cpinitd=1;;
		'C') set_buildtype "cmake";;
		'm') set_buildtype "meson";;
		'd') pkgdesc="$OPTARG";;
		'f') force=1;;
		'h') usage; exit;;
		'l') license="$OPTARG";;
		'n') pkgname="$OPTARG";;
		'p') set_buildtype "perl";;
		'y') set_buildtype "python";;
		'u') url="$OPTARG";;
		's') sourceforge=1;;
	esac
done
shift $(( $OPTIND - 1 ))

check_arguments "$@"
newaport $1