From 09764d141e839688bd138c5c4109b5b640103186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Bac=C5=82awski?= Date: Sun, 23 Aug 2015 22:27:22 +0200 Subject: [PATCH] bootstrap.sh partially converted to Python. --- build-ppc.py | 20 +++---- common.py | 152 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 119 insertions(+), 53 deletions(-) diff --git a/build-ppc.py b/build-ppc.py index 1c0bb13..441388b 100755 --- a/build-ppc.py +++ b/build-ppc.py @@ -50,7 +50,7 @@ def prepare_sdk(): execute('lha', '-xf', clib2, 'clib2/*') execute('lha', '-xf', newlib, 'newlib/*') execute('lha', '-xf', base, 'Include/*') - rename('Include', 'include') + move('Include', 'include') def doit(): @@ -87,27 +87,27 @@ def doit(): name = path.basename(url) fetch(name, url) - source('{lha}', copy=path.join('{build}', '{lha}')) + unpack('{lha}', copy=path.join('{build}', '{lha}')) configure('{lha}', '--disable-shared', '--prefix={host}') build('{lha}') install('{lha}') - source('{texinfo}') + unpack('{texinfo}') configure('{texinfo}', '--prefix={host}') build('{texinfo}') install('{texinfo}') - source('{gmp}') + unpack('{gmp}') configure('{gmp}', '--disable-shared', '--prefix={host}') build('{gmp}') install('{gmp}') - source('{mpfr}') + unpack('{mpfr}') configure('{mpfr}', '--disable-shared', '--prefix={host}', @@ -115,7 +115,7 @@ def doit(): build('{mpfr}') install('{mpfr}') - source('{mpc}') + unpack('{mpc}') configure('{mpc}', '--disable-shared', '--prefix={host}', @@ -124,7 +124,7 @@ def doit(): build('{mpc}') install('{mpc}') - source('{isl}') + unpack('{isl}') configure('{isl}', '--disable-shared', '--prefix={host}', @@ -132,7 +132,7 @@ def doit(): build('{isl}') install('{isl}') - source('{cloog}') + unpack('{cloog}') configure('{cloog}', '--disable-shared', '--prefix={host}', @@ -148,7 +148,7 @@ def doit(): elif cmpver('eq', '{binutils_ver}', '2.23.2'): binutils_env.update(CFLAGS='-Wno-error') - source('{binutils}') + unpack('{binutils}') with env(**binutils_env): configure('{binutils}', '--prefix={target}', @@ -165,7 +165,7 @@ def doit(): cflags.append('-m32') gcc_env.update(CFLAGS=' '.join(cflags)) - source('{gcc}') + unpack('{gcc}') with env(**gcc_env): configure('{gcc}', '--with-bugurl="http://sf.net/p/adtools"', diff --git a/common.py b/common.py index 36457f1..1ac9de7 100644 --- a/common.py +++ b/common.py @@ -53,6 +53,7 @@ def flatten(*args): chdir = fill_in_args(os.chdir) path.exists = fill_in_args(path.exists) path.join = fill_in_args(path.join) +path.relpath = fill_in_args(path.relpath) @fill_in_args @@ -91,10 +92,10 @@ def cmpver(op, v1, v2): @fill_in_args -def relpath(name): +def topdir(name): if not path.isabs(name): name = path.abspath(name) - return path.relpath(name, fill_in('{top}')) + return path.relpath(name, '{top}') @fill_in_args @@ -104,13 +105,18 @@ def find_executable(name): @fill_in_args -def find_files(pattern): - found = [] - for root, dirs, files in os.walk('.', topdown=True): - for name in files: - if fnmatch(name, pattern): - found.append(path.join(root, name)) - return found +def find(root, include=None, exclude=None): + lst = [] + for name in sorted(os.listdir(root)): + if exclude and any(fnmatch(name, pat) for pat in exclude): + continue + if include and not any(fnmatch(name, pat) for pat in include): + continue + fullname = path.join(root, name) + lst.append(fullname) + if path.isdir(fullname): + lst.extend(find(fullname, include, exclude)) + return lst @fill_in_args @@ -125,7 +131,7 @@ def touch(name): def rmtree(*names): for name in flatten(names): if path.isdir(name): - debug('rmtree "%s"', relpath(name)) + debug('rmtree "%s"', topdir(name)) shutil.rmtree(name) @@ -133,33 +139,48 @@ def rmtree(*names): def remove(*names): for name in flatten(names): if path.isfile(name): - debug('remove "%s"', relpath(name)) + debug('remove "%s"', topdir(name)) os.remove(name) @fill_in_args def mkdir(*names): for name in flatten(names): - debug('makedir "%s"', relpath(name)) - os.makedirs(name) + if not path.isdir(name): + debug('makedir "%s"', topdir(name)) + os.makedirs(name) + + +@fill_in_args +def copy(src, dst): + debug('copy "%s" to "%s"', topdir(src), topdir(dst)) + shutil.copy(src, dst) @fill_in_args def copytree(src, dst, **kwargs): - debug('copytree "%s" to "%s"', relpath(src), relpath(dst)) - shutil.copytree(src, dst, **kwargs) + debug('copytree "%s" to "%s"', topdir(src), topdir(dst)) + + mkdir(dst) + + for name in find(src, **kwargs): + if path.isdir(name): + mkdir(path.join(dst, path.relpath(name, src))) + else: + copy(name, path.join(dst, path.relpath(name, src))) @fill_in_args -def rename(src, dst): - debug('rename "%s" to "%s"', relpath(src), relpath(dst)) - os.rename(src, dst) +def move(src, dst): + debug('move "%s" to "%s"', topdir(src), topdir(dst)) + shutil.move(src, dst) @fill_in_args def symlink(src, name): - debug('symlink "%s" from "%s"', src, relpath(name)) - os.symlink(src, name) + if not path.islink(name): + debug('symlink "%s" points at "%s"', topdir(name), src) + os.symlink(src, name) @fill_in_args @@ -196,10 +217,10 @@ def download(url, name): @fill_in_args def unarc(name): - info('extract files from "%s"' % relpath(name)) + info('extract files from "%s"' % topdir(name)) if name.endswith('.lha'): - execute('lha', '-xq', name) + execute('lha', '-x', name) else: if name.endswith('.tar.gz') or name.endswith('.tar.bz2'): module = tarfile @@ -221,7 +242,7 @@ def cwd(name): if not path.exists(name): mkdir(name) try: - debug('enter directory "%s"', relpath(name)) + debug('enter directory "%s"', topdir(name)) chdir(name) yield finally: @@ -276,58 +297,103 @@ def fetch(name, url): if not path.exists(name): execute('svn', 'checkout', url, name) else: - execute('svn', 'update', name) + with cwd(name): + execute('svn', 'update') + elif url.startswith('git'): + if not path.exists(name): + execute('git', 'clone', url, name) + else: + with cwd(name): + execute('git', 'pull') + else: + panic('URL "%s" not recognized!', url) @check_stamp -def source(name, copy=None): +def unpack(name, top_dir=None, in_dir=None): try: src = glob(path.join('{archives}', name) + '*')[0] except IndexError: - panic('Missing source for "%s".', src) + panic('Missing files for "%s".', src) dst = path.join('{sources}', name) rmtree(dst) - info('preparing source "%s"', name) + info('preparing files for "%s"', name) - with cwd('{sources}'): + if in_dir is not None: + workdir = path.join('{sources}', in_dir) + else: + workdir = '{sources}' + + with cwd(workdir): if path.isdir(src): - copytree(src, dst, ignore=shutil.ignore_patterns('.svn')) + copytree(src, dst, exclude=['*.svn']) else: unarc(src) - - if copy is not None: - rmtree(copy) - copytree(dst, copy) + if top_dir is not None: + move(top_dir, name) + if path.dirname(top_dir): + rmtree(path.split(top_dir)) @check_stamp -def configure(name, *confopts): +def patch(name): + with cwd('{sources}'): + found = [] + + for root, _, files in os.walk(path.join('{patches}', name), topdown=True): + for name in files: + if not fnmatch(name, '*~'): + found.append(path.join(root, name)) + + for name in sorted(found): + if fnmatch(name, '*.diff'): + execute('patch', '-t', '-p0', '-i', name) + else: + dst = path.relpath(name, '{patches}') + mkdir(path.dirname(dst)) + copy(path.join(root, name), dst) + + +@check_stamp +def configure(name, *confopts, **kwargs): info('configuring "%s"', name) + if kwargs.get('copy_source', False): + rmtree(path.join('{build}', name)) + copytree(path.join('{sources}', name), path.join('{build}', name)) + from_dir = '.' + else: + from_dir = path.join('{sources}', name) + with cwd(path.join('{build}', name)): - remove(find_files('config.cache')) - execute(path.join('{sources}', name, 'configure'), *confopts) + remove(find('.', include=['config.cache'])) + execute(path.join(from_dir, 'configure'), *confopts) @check_stamp -def build(name, *confopts): +def build(name, *targets, **makevars): info('building "%s"', name) with cwd(path.join('{build}', name)): - execute('make') + args = list(targets) + ['%s=%s' % item for item in makevars.items()] + execute('make', *args) @check_stamp -def install(name, *confopts): +def install(name, *targets, **makevars): info('installing "%s"', name) + if not targets: + targets = ['install'] + with cwd(path.join('{build}', name)): - execute('make', 'install') + args = list(targets) + ['%s=%s' % item for item in makevars.items()] + execute('make', *args) __all__ = ['setvar', 'panic', 'cmpver', 'find_executable', 'execute', - 'rmtree', 'mkdir', 'copytree', 'unarc', 'fetch', 'cwd', - 'remove', 'rename', 'find_files', 'env', 'path', 'check_stamp', - 'source', 'configure', 'build', 'install'] + 'rmtree', 'mkdir', 'copy', 'copytree', 'unarc', 'fetch', 'cwd', + 'symlink', 'remove', 'move', 'find', 'env', 'path', 'check_stamp', + 'unpack', 'patch', 'configure', 'build', 'install']