newapkbuild.in 7.91 KB
Newer Older
1
#!/bin/sh
2
#
3
# newapkbuild - generate a new APKBUILD
4 5 6 7 8
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

9
program_version=@VERSION@
10 11
datadir=@datadir@

12 13 14 15 16
if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"
17 18


19 20
is_url() {
	case "$1" in
21
	http://*|https://*|ftp://*) return 0;;
22
	esac
23
	return 1
24 25
}

26 27 28 29 30 31 32
is_github_url() {
	case $1 in
	https://github.com/*/*/archive/*.tar.gz) return 0;;
	esac
	return 1
}

33
# Build sections
34 35
build_make() {
	cat >>APKBUILD<<__EOF__
36
	make
37 38 39
__EOF__
}

40
build_autotools() {
41
	cat >>APKBUILD<<__EOF__
42 43 44 45
	./configure \\
		--build=\$CBUILD \\
		--host=\$CHOST \\
		--prefix=/usr \\
46 47
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
48 49
		--localstatedir=/var
	make
50 51 52
__EOF__
}

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

	cat >>APKBUILD<<__EOF__
61
	if [ "\$CBUILD" != "\$CHOST" ]; then
62 63 64 65 66 67 68 69
		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" \\
70
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
71
		\${CMAKE_CROSSOPTS}
72
	make
73 74 75
__EOF__
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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__
}

94
build_perl() {
95
	cat >>APKBUILD<<__EOF__
96 97
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor
	make
98 99 100
__EOF__
}

101
build_python() {
102
	cat >>APKBUILD<<__EOF__
103
	python3 setup.py build
104 105 106
__EOF__
}

107 108 109 110 111 112 113 114 115 116 117 118
check_make() {
	cat >>APKBUILD<<__EOF__
	make check
__EOF__
}

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

119
# Package sections
120
package_make() {
121
	cat >>APKBUILD<<__EOF__
122
	make DESTDIR="\$pkgdir" install
123 124 125
__EOF__
}

126 127 128 129
package_autotools() {
	package_make
}

130 131
package_meson() {
	cat >>APKBUILD<<__EOF__
132
	DESTDIR="\$pkgdir" ninja -C output install
133 134 135
__EOF__
}

136
package_perl() {
137
	cat >>APKBUILD<<__EOF__
138
	make DESTDIR="\$pkgdir" install
139 140 141 142
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

143 144
package_python() {
	cat >>APKBUILD<<__EOF__
145
	python3 setup.py install --prefix=/usr --root="\$pkgdir"
146 147 148 149
__EOF__
}

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

157 158 159 160 161 162 163 164 165
	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
166
		pv=${newname#$pn-}
167
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
168
	fi
169 170 171 172 173
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
174 175
		return 1
	fi
176 177
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
178

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

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

187
	case "$buildtype" in
188
	python) makedepends="python3-dev";;
189 190 191 192
	cmake)  makedepends="cmake";;
	meson)  makedepends="meson";;
	*)      makedepends="\$depends_dev";;
	esac
193 194

	# Replace pkgver in $source
195
	if [ -n "$source" ]; then
196
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
197
	fi
Natanael Copa's avatar
Natanael Copa committed
198

199
	# Copy init.d scripts if requested
200
	if [ -n "$cpinitd" ]; then
201 202 203 204
		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
Natanael Copa's avatar
Natanael Copa committed
205 206
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
207 208
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
209 210 211
	"
	fi

212
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
213 214 215
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
216
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
217 218 219 220 221 222
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
223 224 225
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
226 227 228 229 230 231
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"
__EOF__

	abuild -f fetch unpack
232
	# Figure out the builddir
Natanael Copa's avatar
Natanael Copa committed
233 234 235
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
236
			builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
Natanael Copa's avatar
Natanael Copa committed
237 238
		fi
	done
239
	printf 'builddir="$srcdir/%s"\n\n' "$builddir" >> APKBUILD
Natanael Copa's avatar
Natanael Copa committed
240

241 242
	# 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.
243
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
244 245 246
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

247
	# Try to autodetect the buildtype
248 249 250
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
251
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
252 253 254
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
255 256
		elif [ -r "$sdir"/meson.build ]; then
			buildtype="meson"
257
		elif [ -d "$sdir"/cmake ] || [ -r "$sdir/CMakeLists.txt" ]; then
258 259 260
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
261 262
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
263 264 265
		fi
	fi

266
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
267 268
	cat >>APKBUILD<<__EOF__
build() {
269
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
270
__EOF__
271 272

	case "$buildtype" in
273 274
	make)
		build_make;;
275 276
	cmake)
		build_cmake;;
277 278
	meson)
		build_meson;;
279
	autotools)
280
		build_autotools;;
281
	perl)
282
		build_perl;;
283
	python)
284
		build_python;;
285 286
	esac

Natanael Copa's avatar
Natanael Copa committed
287 288 289
	cat >>APKBUILD<<__EOF__
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
__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__
}

Natanael Copa's avatar
Natanael Copa committed
308 309
__EOF__

310
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
311 312
	cat >>APKBUILD<<__EOF__
package() {
313
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
314
__EOF__
315 316

	case "$buildtype" in
317
	make|cmake)
318
		package_make;;
319 320
	autotools)
		package_autotools;;
321 322
	meson)
		package_meson;;
323 324 325 326
	perl)
		package_perl;;
	python)
		package_python;;
327 328
	esac

329 330 331 332
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
333
		"\$pkgdir"/etc/init.d/\$pkgname
334
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
335
		"\$pkgdir"/etc/conf.d/\$pkgname
336 337
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
338 339 340 341
	cat >>APKBUILD<<__EOF__
}

__EOF__
342
	abuild -f checksum
343 344 345
}

usage() {
346 347
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
A. Wilcox's avatar
A. Wilcox committed
348 349
		Usage: $program [-n NAME] [-d DESC] [-l LICENSE] [-u URL]
		       [-aCpy] [-s] [-cfh]
350 351 352 353 354 355
		       PKGNAME[-PKGVER]|SRCURL
		Options:
		  -n  Set package name to NAME
		  -d  Set package description (pkgdesc) to DESC
		  -l  Set package license to LICENSE
		  -u  Set package URL
356
		  -a  Create autotools package (use ./configure ...)
Oliver Smith's avatar
Oliver Smith committed
357
		  -C  Create CMake package (Assume cmake/ is there)
358
		  -m  Create meson package (Assume meson.build is there)
359 360 361 362 363 364 365 366
		  -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 to new directory
		  -f  Force even if directory already exist
		  -h  Show this help

	__EOF__
367 368
}

369
while getopts "acCmd:fhl:n:pyu:s" opt; do
370
	case $opt in
371
		'a') buildtype="autotools";;
372
		'c') cpinitd=1;;
373
		'C') buildtype="cmake";;
374
		'm') buildtype="meson";;
Natanael Copa's avatar
Natanael Copa committed
375
		'd') pkgdesc="$OPTARG";;
376
		'f') force=1;;
377
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
378
		'l') license="$OPTARG";;
379
		'n') pkgname="$OPTARG";;
380
		'p') buildtype="perl";;
381
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
382
		'u') url="$OPTARG";;
383
		's') sourceforge=1;;
384 385 386 387
	esac
done
shift $(( $OPTIND - 1 ))

388
while [ $# -gt 0 ]; do
389 390 391
	newaport $1 || exit 1
	shift
done