From 816e06a512122788e77a2b6c12920ed0da2bb952 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 28 May 2018 11:16:47 +0200 Subject: [PATCH] Make the neutralEnv global. No need to pass the neutralEnv from instance to instance. --- kiwixbuild/__init__.py | 3 ++ kiwixbuild/_global.py | 8 +++ kiwixbuild/buildenv.py | 13 ++--- kiwixbuild/builder.py | 5 +- kiwixbuild/dependencies/all_dependencies.py | 4 +- kiwixbuild/dependencies/base.py | 55 +++++++++------------ kiwixbuild/dependencies/kiwix_lib.py | 3 +- kiwixbuild/dependencies/libaria2.py | 3 +- kiwixbuild/dependencies/xapian.py | 3 +- kiwixbuild/toolchains/base_toolchain.py | 9 ++-- kiwixbuild/toolchains/mingw32.py | 3 +- 11 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 kiwixbuild/_global.py diff --git a/kiwixbuild/__init__.py b/kiwixbuild/__init__.py index d84be62..73bb7c2 100644 --- a/kiwixbuild/__init__.py +++ b/kiwixbuild/__init__.py @@ -7,6 +7,7 @@ from .dependencies import Dependency from .platforms import PlatformInfo from .builder import Builder from .utils import setup_print_progress +from . import _global def parse_args(): parser = argparse.ArgumentParser() @@ -65,6 +66,8 @@ def main(): options = parse_args() options.working_dir = os.path.abspath(options.working_dir) setup_print_progress(options.show_progress) + neutralEnv = buildenv.PlatformNeutralEnv(options) + _global.set_neutralEnv(neutralEnv) builder = Builder(options) builder.run() diff --git a/kiwixbuild/_global.py b/kiwixbuild/_global.py new file mode 100644 index 0000000..0b5aa35 --- /dev/null +++ b/kiwixbuild/_global.py @@ -0,0 +1,8 @@ +_neutralEnv = None + +def set_neutralEnv(env): + global _neutralEnv + _neutralEnv = env + +def neutralEnv(what): + return getattr(_neutralEnv, what) diff --git a/kiwixbuild/buildenv.py b/kiwixbuild/buildenv.py index c742519..c103a69 100644 --- a/kiwixbuild/buildenv.py +++ b/kiwixbuild/buildenv.py @@ -6,10 +6,8 @@ import platform from .platforms import PlatformInfo from .toolchains import Toolchain from .packages import PACKAGE_NAME_MAPPERS -from .utils import ( - pj, - download_remote, - Defaultdict) +from .utils import pj, download_remote, Defaultdict +from . import _global SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) @@ -115,9 +113,8 @@ class PlatformNeutralEnv: class BuildEnv: - def __init__(self, options, neutralEnv, targetsDict): + def __init__(self, options, targetsDict): build_dir = "BUILD_{}".format(options.target_platform) - self.neutralEnv = neutralEnv self.build_dir = pj(options.working_dir, build_dir) self.install_dir = pj(self.build_dir, "INSTALL") for d in (self.build_dir, @@ -156,7 +153,7 @@ class BuildEnv: ToolchainClass = Toolchain.all_toolchains[toolchain_name] if ToolchainClass.neutral: self.toolchains.append( - self.neutralEnv.add_toolchain(toolchain_name) + _global._neutralEnv.add_toolchain(toolchain_name) ) else: self.toolchains.append(ToolchainClass(self)) @@ -202,7 +199,7 @@ class BuildEnv: self.meson_crossfile = self._gen_crossfile('meson_cross_file.txt') def __getattr__(self, name): - return getattr(self.neutralEnv, name) + return _global.neutralEnv(name) def _is_debianlike(self): return os.path.isfile('/etc/debian_version') diff --git a/kiwixbuild/builder.py b/kiwixbuild/builder.py index 92aab5a..52b9525 100644 --- a/kiwixbuild/builder.py +++ b/kiwixbuild/builder.py @@ -10,8 +10,7 @@ class Builder: def __init__(self, options): self.options = options self.targets = OrderedDict() - self.neutralEnv = PlatformNeutralEnv(options) - self.buildEnv = BuildEnv(options, self.neutralEnv, self.targets) + self.buildEnv = BuildEnv(options, self.targets) _targets = {} targetDef = options.targets @@ -31,7 +30,7 @@ class Builder: if targetName in targets: return targetClass = Dependency.all_deps[targetName] - target = targetClass(self.neutralEnv, self.buildEnv) + target = targetClass(self.buildEnv) targets[targetName] = target for dep in target.dependencies: self.add_targets(dep, targets) diff --git a/kiwixbuild/dependencies/all_dependencies.py b/kiwixbuild/dependencies/all_dependencies.py index 34845fd..9eb6837 100644 --- a/kiwixbuild/dependencies/all_dependencies.py +++ b/kiwixbuild/dependencies/all_dependencies.py @@ -3,6 +3,8 @@ from .base import ( NoopSource, NoopBuilder) +from kiwixbuild._global import neutralEnv + class AllBaseDependencies(Dependency): name = "alldependencies" @@ -16,7 +18,7 @@ class AllBaseDependencies(Dependency): else: base_deps += ["icu4c", "libmagic"] if ( self.buildEnv.platform_info.build != 'android' - and self.buildEnv.distname != 'Darwin'): + and neutralEnv('distname') != 'Darwin'): base_deps += ['ctpp2c', 'ctpp2'] if self.buildEnv.platform_info.build == 'android': base_deps += ['Gradle'] diff --git a/kiwixbuild/dependencies/base.py b/kiwixbuild/dependencies/base.py index a1410bf..ee9e66d 100644 --- a/kiwixbuild/dependencies/base.py +++ b/kiwixbuild/dependencies/base.py @@ -4,6 +4,7 @@ import shutil from kiwixbuild.utils import pj, Context, SkipCommand, extract_archive, Defaultdict, StopBuild from kiwixbuild.versions import main_project_versions, base_deps_versions +from kiwixbuild._global import neutralEnv SCRIPT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) @@ -22,8 +23,7 @@ class Dependency(metaclass=_MetaDependency): dependencies = [] force_native_build = False - def __init__(self, neutralEnv, buildEnv): - self.neutralEnv = neutralEnv + def __init__(self, buildEnv): self.buildEnv = buildEnv self.source = self.Source(self) self.builder = self.Builder(self) @@ -41,7 +41,7 @@ class Dependency(metaclass=_MetaDependency): @property def source_path(self): - return pj(self.neutralEnv.source_dir, self.source.source_dir) + return pj(neutralEnv('source_dir'), self.source.source_dir) @property def _log_dir(self): @@ -77,7 +77,6 @@ class Source: inside the neutralEnv.source_dir.""" def __init__(self, target): self.target = target - self.neutralEnv = target.neutralEnv @property def name(self): @@ -88,12 +87,12 @@ class Source: return self.target.full_name def _patch(self, context): - source_path = pj(self.neutralEnv.source_dir, self.source_dir) + source_path = pj(neutralEnv('source_dir'), self.source_dir) context.try_skip(source_path) context.force_native_build = True for p in self.patches: with open(pj(SCRIPT_DIR, 'patches', p), 'r') as patch_input: - self.neutralEnv.run_command("patch -p1", source_path, context, input=patch_input.read()) + neutralEnv('run_command')("patch -p1", source_path, context, input=patch_input.read()) def command(self, *args, **kwargs): return self.target.command(*args, **kwargs) @@ -109,18 +108,18 @@ class ReleaseDownload(Source): @property def extract_path(self): - return pj(self.neutralEnv.source_dir, self.source_dir) + return pj(neutralEnv('source_dir'), self.source_dir) def _download(self, context): - context.try_skip(self.neutralEnv.archive_dir, self.name) - self.neutralEnv.download(self.archive) + context.try_skip(neutralEnv('archive_dir'), self.name) + neutralEnv('download')(self.archive) def _extract(self, context): context.try_skip(self.extract_path) if os.path.exists(self.extract_path): shutil.rmtree(self.extract_path) - extract_archive(pj(self.neutralEnv.archive_dir, self.archive.name), - self.neutralEnv.source_dir, + extract_archive(pj(neutralEnv('archive_dir'), self.archive.name), + neutralEnv('source_dir'), topdir=self.archive_top_dir, name=self.source_dir) @@ -142,18 +141,18 @@ class GitClone(Source): @property def source_dir(self): - if self.neutralEnv.make_release: + if neutralEnv('make_release'): return "{}_release".format(self.git_dir) else: return self.git_dir @property def git_path(self): - return pj(self.neutralEnv.source_dir, self.source_dir) + return pj(neutralEnv('source_dir'), self.source_dir) @property def git_ref(self): - if self.neutralEnv.make_release: + if neutralEnv('make_release'): return self.release_git_ref else: return self.base_git_ref @@ -163,13 +162,13 @@ class GitClone(Source): raise SkipCommand() command = "git clone --depth=1 --branch {} {} {}".format( self.git_ref, self.git_remote, self.source_dir) - self.neutralEnv.run_command(command, self.neutralEnv.source_dir, context) + neutralEnv('run_command')(command, neutralEnv('source_dir'), context) def _git_update(self, context): command = "git fetch origin {}".format( self.git_ref) - self.neutralEnv.run_command(command, self.git_path, context) - self.neutralEnv.run_command("git checkout "+self.git_ref, self.git_path, context) + neutralEnv('run_command')(command, self.git_path, context) + neutralEnv('run_command')("git checkout "+self.git_ref, self.git_path, context) def prepare(self): self.command('gitclone', self._git_clone) @@ -185,17 +184,17 @@ class SvnClone(Source): @property def svn_path(self): - return pj(self.neutralEnv.source_dir, self.svn_dir) + return pj(neutralEnv('source_dir'), self.svn_dir) def _svn_checkout(self, context): if os.path.exists(self.svn_path): raise SkipCommand() command = "svn checkout {} {}".format(self.svn_remote, self.svn_dir) - self.neutralEnv.run_command(command, self.neutralEnv.source_dir, context) + neutralEnv('run_command')(command, neutralEnv('source_dir'), context) def _svn_update(self, context): context.try_skip(self.svn_path) - self.neutralEnv.run_command("svn update", self.svn_path, context) + neutralEnv('run_command')("svn update", self.svn_path, context) def prepare(self): self.command('svncheckout', self._svn_checkout) @@ -355,12 +354,6 @@ class MesonBuilder(Builder): configure_option = "" test_option = "" - def __init__(self, target): - super().__init__(target) - self.meson_command = self.buildEnv.neutralEnv.meson_command - self.mesontest_command = self.buildEnv.neutralEnv.mesontest_command - self.ninja_command = self.buildEnv.neutralEnv.ninja_command - @property def library_type(self): return 'static' if self.buildEnv.platform_info.static else 'shared' @@ -382,7 +375,7 @@ class MesonBuilder(Builder): " --libdir={buildEnv.libprefix}" " {cross_option}") command = command.format( - command=self.meson_command, + command=neutralEnv('meson_command'), library_type=self.library_type, configure_option=configure_option, build_path=self.build_path, @@ -392,7 +385,7 @@ class MesonBuilder(Builder): self.buildEnv.run_command(command, self.source_path, context, cross_env_only=True) def _compile(self, context): - command = "{} -v".format(self.ninja_command) + command = "{} -v".format(neutralEnv('ninja_command')) self.buildEnv.run_command(command, self.build_path, context) def _test(self, context): @@ -401,15 +394,15 @@ class MesonBuilder(Builder): and not self.buildEnv.platform_info.static) ): raise SkipCommand() - command = "{} --verbose {}".format(self.mesontest_command, self.test_option) + command = "{} --verbose {}".format(neutralEnv('mesontest_command'), self.test_option) self.buildEnv.run_command(command, self.build_path, context) def _install(self, context): - command = "{} -v install".format(self.ninja_command) + command = "{} -v install".format(neutralEnv('ninja_command')) self.buildEnv.run_command(command, self.build_path, context) def _make_dist(self, context): - command = "{} -v dist".format(self.ninja_command) + command = "{} -v dist".format(neutralEnv('ninja_command')) self.buildEnv.run_command(command, self.build_path, context) diff --git a/kiwixbuild/dependencies/kiwix_lib.py b/kiwixbuild/dependencies/kiwix_lib.py index 6d18a34..46e8bc3 100644 --- a/kiwixbuild/dependencies/kiwix_lib.py +++ b/kiwixbuild/dependencies/kiwix_lib.py @@ -2,6 +2,7 @@ from .base import ( Dependency, GitClone, MesonBuilder) +from kiwixbuild._global import neutralEnv class Kiwixlib(Dependency): name = "kiwix-lib" @@ -10,7 +11,7 @@ class Kiwixlib(Dependency): def dependencies(self): base_dependencies = ["pugixml", "libzim", "zlib", "lzma", "libaria2"] if ( self.buildEnv.platform_info.build != 'android' - and self.buildEnv.distname != 'Darwin'): + and neutralEnv('distname') != 'Darwin'): base_dependencies += ['ctpp2c', 'ctpp2'] if self.buildEnv.platform_info.build != 'native': return base_dependencies + ["icu4c_cross-compile"] diff --git a/kiwixbuild/dependencies/libaria2.py b/kiwixbuild/dependencies/libaria2.py index 90f1567..c64686d 100644 --- a/kiwixbuild/dependencies/libaria2.py +++ b/kiwixbuild/dependencies/libaria2.py @@ -5,6 +5,7 @@ from .base import ( ) from kiwixbuild.utils import Remotefile +from kiwixbuild._global import neutralEnv class Aria2(Dependency): name = "libaria2" @@ -20,7 +21,7 @@ class Aria2(Dependency): def _post_prepare_script(self, context): context.try_skip(self.extract_path) command = "autoreconf -i" - self.neutralEnv.run_command(command, self.extract_path, context) + neutralEnv('run_command')(command, self.extract_path, context) class Builder(MakeBuilder): configure_option = "--enable-libaria2 --disable-ssl --disable-bittorent --disable-metalink --without-sqlite3 --without-libxml2 --without-libexpat" diff --git a/kiwixbuild/dependencies/xapian.py b/kiwixbuild/dependencies/xapian.py index 13f9b4b..0236e8f 100644 --- a/kiwixbuild/dependencies/xapian.py +++ b/kiwixbuild/dependencies/xapian.py @@ -5,6 +5,7 @@ from .base import ( ) from kiwixbuild.utils import Remotefile +from kiwixbuild._global import neutralEnv class Xapian(Dependency): @@ -23,6 +24,6 @@ class Xapian(Dependency): def dependencies(self): deps = ['zlib', 'lzma'] if (self.buildEnv.platform_info.build == 'win32' - or self.buildEnv.distname == 'Darwin'): + or neutralEnv('distname') == 'Darwin'): return deps return deps + ['uuid'] diff --git a/kiwixbuild/toolchains/base_toolchain.py b/kiwixbuild/toolchains/base_toolchain.py index e5040cd..3a459ac 100644 --- a/kiwixbuild/toolchains/base_toolchain.py +++ b/kiwixbuild/toolchains/base_toolchain.py @@ -4,6 +4,7 @@ import subprocess pj = os.path.join from kiwixbuild.utils import Context, SkipCommand, StopBuild +from kiwixbuild._global import neutralEnv class _MetaToolchain(type): def __new__(cls, name, bases, dct): @@ -22,9 +23,7 @@ class Toolchain(metaclass=_MetaToolchain): Builder = None Source = None - def __init__(self, neutralEnv): - self.neutralEnv = neutralEnv - self.buildEnv = neutralEnv + def __init__(self): self.source = self.Source(self) if self.Source else None self.builder = self.Builder(self) if self.Builder else None @@ -36,11 +35,11 @@ class Toolchain(metaclass=_MetaToolchain): @property def source_path(self): - return pj(self.neutralEnv.source_dir, self.source.source_dir) + return pj(neutralEnv('source_dir'), self.source.source_dir) @property def _log_dir(self): - return self.neutralEnv.log_dir + return neutralEnv('log_dir') def set_env(self, env): pass diff --git a/kiwixbuild/toolchains/mingw32.py b/kiwixbuild/toolchains/mingw32.py index 7da1283..8b29b1a 100644 --- a/kiwixbuild/toolchains/mingw32.py +++ b/kiwixbuild/toolchains/mingw32.py @@ -3,6 +3,7 @@ import subprocess from .base_toolchain import Toolchain from kiwixbuild.utils import which +from kiwixbuild._global import neutralEnv pj = os.path.join @@ -17,7 +18,7 @@ class mingw32_toolchain(Toolchain): 'fedora': '/usr/i686-w64-mingw32/sys-root/mingw', 'debian': '/usr/i686-w64-mingw32' } - return root_paths[self.neutralEnv.distname] + return root_paths[neutralEnv('distname')] @property def binaries(self):