newapkbuild.in 6.99 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 36 37 38 39
build_make() {
	cat >>APKBUILD<<__EOF__
	make || return 1
__EOF__
}

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

54 55 56
build_cmake() {
# References:
# http://www.cmake.org/Wiki/CMake_Useful_Variables
57 58
# http://www.vtk.org/Wiki/CMake_Cross_Compiling
# This is incomplete: CMAKE_{HOST_,}SYSTEM_PROCESSOR needs to be set,
59 60 61 62 63 64 65 66 67 68 69 70 71 72
# and likewise CMAKE_FIND_ROOT_PATH and a few other details.

	sed -i -e 's/^\(makedepends="\)/\1cmake /' APKBUILD
	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" \\
73
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
74 75 76 77 78 79
		${CMAKE_CROSSOPTS} \\
		|| return 1
	make || return 1
__EOF__
}

80
build_perl() {
81 82
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
83 84 85 86
	make || return 1
__EOF__
}

87
build_python() {
88 89
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
90 91 92
__EOF__
}

93
# Package sections
94
package_make() {
95 96 97 98 99
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install || return 1
__EOF__
}

100 101 102 103
package_autotools() {
	package_make
}

104
package_perl() {
105
	cat >>APKBUILD<<__EOF__
106
	make DESTDIR="\$pkgdir" install || return 1
107 108 109 110
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

111 112 113 114 115 116 117
package_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py install --prefix=/usr --root="\$pkgdir" || return 1
__EOF__
}

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

125 126 127 128 129 130 131 132 133
	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
134
		pv=${newname#$pn-}
135
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
136
	fi
137 138 139 140 141
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
142 143
		return 1
	fi
144 145
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
146

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

151
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
152 153 154
		depends="python"
	fi

155
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
156 157
		makedepends="python-dev"
	else
158
		makedepends="\$depends_dev"
159 160 161
	fi

	# Replace pkgver in $source
162
	if [ -n "$source" ]; then
163
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
164
	fi
Natanael Copa's avatar
Natanael Copa committed
165

166
	# Copy init.d scripts if requested
167
	if [ -n "$cpinitd" ]; then
168 169 170 171
		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
172 173
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
174 175
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
176 177 178
	"
	fi

179
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
180 181 182
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
183
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
184 185 186 187 188 189
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
190 191 192
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
193 194 195 196 197 198
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"
__EOF__

	abuild -f fetch unpack
199
	# Figure out the builddir
Natanael Copa's avatar
Natanael Copa committed
200 201 202
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
203
			builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
Natanael Copa's avatar
Natanael Copa committed
204 205
		fi
	done
206
	printf 'builddir="$srcdir/%s"\n\n' "$builddir" >> APKBUILD
Natanael Copa's avatar
Natanael Copa committed
207

208 209
	# 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.
210
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
211 212 213
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

214
	# Check if its autotools
215 216 217
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
218
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
219 220 221 222 223 224 225
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -d "$sdir"/cmake ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
226 227
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
228 229 230
		fi
	fi

231
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
232 233
	cat >>APKBUILD<<__EOF__
build() {
234
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
235
__EOF__
236 237

	case "$buildtype" in
238 239
	make)
		build_make;;
240 241
	cmake)
		build_cmake;;
242
	autotools)
243
		build_autotools;;
244
	perl)
245
		build_perl;;
246
	python)
247
		build_python;;
248 249
	esac

Natanael Copa's avatar
Natanael Copa committed
250 251 252 253 254
	cat >>APKBUILD<<__EOF__
}

__EOF__

255
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
256 257
	cat >>APKBUILD<<__EOF__
package() {
258
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
259
__EOF__
260 261

	case "$buildtype" in
262 263
	make)
		package_make;;
264 265 266 267 268 269
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
270 271
	esac

272 273 274 275 276 277 278 279 280
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
		"\$pkgdir"/etc/init.d/\$pkgname || return 1
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
		"\$pkgdir"/etc/conf.d/\$pkgname || return 1
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
281 282 283 284
	cat >>APKBUILD<<__EOF__
}

__EOF__
285
	abuild -f checksum
286 287 288
}

usage() {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
		Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		       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
		  -a  Create autotools (use ./configure ...)
		  -C  Create CMake pakckage (Assume cmake/ 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 to new directory
		  -f  Force even if directory already exist
		  -h  Show this help

	__EOF__
308 309
}

310
while getopts "acd:fhl:n:pyu:s" opt; do
311
	case $opt in
312
		'a') buildtype="autotools";;
313
		'c') cpinitd=1;;
314
		'C') buildtype="cmake";;
Natanael Copa's avatar
Natanael Copa committed
315
		'd') pkgdesc="$OPTARG";;
316
		'f') force=1;;
317
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
318
		'l') license="$OPTARG";;
319
		'n') pkgname="$OPTARG";;
320
		'p') buildtype="perl";;
321
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
322
		'u') url="$OPTARG";;
323
		's') sourceforge=1;;
324 325 326 327
	esac
done
shift $(( $OPTIND - 1 ))

328
while [ $# -gt 0 ]; do
329 330 331
	newaport $1 || exit 1
	shift
done