newapkbuild.in 6.98 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__
}

Isaac Dunham's avatar
Isaac Dunham committed
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
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.

	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_COMPILER="\${CXX:-g++}" \\
		-DCMAKE_C_COMPILER="\${CC:-gcc}" \\
		-DCMAKE_CXX_FLAGS="\$CXXFLAGS" \\
		-DCMAKE_CXX_FLAGS="\$CFLAGS" \\
		${CMAKE_CROSSOPTS} \\
		|| return 1
	make || return 1
__EOF__
}

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

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

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

102 103 104 105
package_autotools() {
	package_make
}

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

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

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

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

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

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

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

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

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

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

__EOF__

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

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


230
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
231 232 233 234 235 236 237 238 239 240 241 242 243
	cat >>APKBUILD<<__EOF__
prepare() {
	local i
	cd "\$_builddir"
	for i in \$source; do
		case \$i in
		*.patch) msg \$i; patch -p1 -i "\$srcdir"/\$i || return 1;;
		esac
	done
}

__EOF__

244
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
245 246 247 248
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
249 250

	case "$buildtype" in
251 252
	make)
		build_make;;
Isaac Dunham's avatar
Isaac Dunham committed
253 254
	cmake)
		build_cmake;;
255
	autotools)
256
		build_autotools;;
257
	perl)
258
		build_perl;;
259
	python)
260
		build_python;;
261 262
	esac

Natanael Copa's avatar
Natanael Copa committed
263 264 265 266 267
	cat >>APKBUILD<<__EOF__
}

__EOF__

268
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
269 270
	cat >>APKBUILD<<__EOF__
package() {
271
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
272
__EOF__
273 274

	case "$buildtype" in
275 276
	make)
		package_make;;
277 278 279 280 281 282
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
283 284
	esac

285 286 287 288 289 290 291 292 293
	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
294 295 296 297
	cat >>APKBUILD<<__EOF__
}

__EOF__
298
	abuild -f checksum
299 300 301
}

usage() {
302
	cat >&2 <<__EOF__
303
$program $program_version - generate a new APKBUILD
304 305
Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		PKGNAME[-PKGVER]|SRCURL
306 307 308 309 310 311
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 ...)
Isaac Dunham's avatar
Isaac Dunham committed
312
  -C  Create CMake pakckage (Assume cmake/ is there)
313 314 315 316 317 318 319 320
  -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__
321 322
}

323
while getopts "acd:fhl:n:pyu:s" opt; do
324
	case $opt in
325
		'a') buildtype="autotools";;
326
		'c') cpinitd=1;;
Isaac Dunham's avatar
Isaac Dunham committed
327
		'C') buildtype="cmake";;
Natanael Copa's avatar
Natanael Copa committed
328
		'd') pkgdesc="$OPTARG";;
329
		'f') force=1;;
330
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
331
		'l') license="$OPTARG";;
332
		'n') pkgname="$OPTARG";;
333
		'p') buildtype="perl";;
334
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
335
		'u') url="$OPTARG";;
336
		's') sourceforge=1;;
337 338 339 340
	esac
done
shift $(( $OPTIND - 1 ))

341
while [ $# -gt 0 ]; do
342 343 344 345
	newaport $1 || exit 1
	shift
done