From a274880aa68f8ff8aa3055890316d333bddf36ef Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Thu, 5 Aug 2021 09:53:07 +0300 Subject: [PATCH] aedi: move environment from target to state this completes #34 --- aedi/builder.py | 1 + aedi/state.py | 16 +++++++++++++ aedi/target/base.py | 45 ++++++++++++------------------------ aedi/target/library_tier1.py | 15 +++++++----- aedi/target/library_tier2.py | 6 ++--- aedi/target/main.py | 2 +- aedi/target/tools.py | 6 ++--- 7 files changed, 48 insertions(+), 43 deletions(-) diff --git a/aedi/builder.py b/aedi/builder.py index cd6800a1..15772e9c 100644 --- a/aedi/builder.py +++ b/aedi/builder.py @@ -132,6 +132,7 @@ class Builder(object): def _build(self, target: Target): state = self._state + state.environment = os.environ target.configure(state) target.build(state) diff --git a/aedi/state.py b/aedi/state.py index e743aab3..abd33107 100644 --- a/aedi/state.py +++ b/aedi/state.py @@ -52,6 +52,8 @@ class BuildState: self.verbose = False self.jobs = 1 + self.environment = os.environ.copy() + def architecture(self) -> str: return self.platform.architecture if self.platform else '' @@ -189,3 +191,17 @@ class BuildState: def has_source_file(self, path: typing.Union[str, Path]): return (self.source / path).exists() + + def update_environment(self, name: str, value: str): + env = self.environment + env[name] = env[name] + ' ' + value if name in env else value + + def set_sdk(self, var_name: str): + sdk_path = self.sdk_path() + if sdk_path: + self.update_environment(var_name, f'-isysroot {sdk_path}') + + def set_os_version(self, var_name: str): + os_version = self.os_version() + if os_version: + self.update_environment(var_name, f'-mmacosx-version-min={os_version}') diff --git a/aedi/target/base.py b/aedi/target/base.py index 998562fd..12fd3774 100644 --- a/aedi/target/base.py +++ b/aedi/target/base.py @@ -62,7 +62,6 @@ class BuildTarget(Target): super().__init__(name) self.src_root = '' - self.environment = os.environ.copy() self.options = CommandLineOptions() self.multi_platform = True @@ -87,7 +86,7 @@ class BuildTarget(Target): os.makedirs(state.build_path, exist_ok=True) - env = self.environment + env = state.environment env['PATH'] = os.pathsep.join([ str(state.bin_path), env['PATH'], @@ -100,31 +99,17 @@ class BuildTarget(Target): env['CXX'] = state.cxx_compiler() for prefix in ('CPP', 'C', 'CXX', 'OBJC', 'OBJCXX'): - varname = f'{prefix}FLAGS' + var_name = f'{prefix}FLAGS' - self._update_env(varname, f'-I{state.include_path}') - self._set_sdk(state, varname) - self._set_os_version(state, varname) + state.update_environment(var_name, f'-I{state.include_path}') + state.set_sdk(var_name) + state.set_os_version(var_name) ldflags = 'LDFLAGS' - self._update_env(ldflags, f'-L{state.lib_path}') - self._set_sdk(state, ldflags) - self._set_os_version(state, ldflags) - - def _update_env(self, name: str, value: str): - env = self.environment - env[name] = env[name] + ' ' + value if name in env else value - - def _set_sdk(self, state: BuildState, varname: str): - sdk_path = state.sdk_path() - if sdk_path: - self._update_env(varname, f'-isysroot {sdk_path}') - - def _set_os_version(self, state: BuildState, varname: str): - os_version = state.os_version() - if os_version: - self._update_env(varname, '-mmacosx-version-min=' + str(os_version)) + state.update_environment(ldflags, f'-L{state.lib_path}') + state.set_sdk(ldflags) + state.set_os_version(ldflags) def install(self, state: BuildState, options: CommandLineOptions = None, tool: str = 'gmake'): if state.xcode: @@ -136,7 +121,7 @@ class BuildTarget(Target): args = [tool, 'install'] args += options and options.to_list() or [] - subprocess.check_call(args, cwd=state.build_path, env=self.environment) + subprocess.check_call(args, cwd=state.build_path, env=state.environment) self.update_pc_files(state) @@ -298,7 +283,7 @@ class MakeTarget(BuildTarget): args += self.options.to_list() work_path = state.build_path / self.src_root - subprocess.check_call(args, cwd=work_path, env=self.environment) + subprocess.check_call(args, cwd=work_path, env=state.environment) class ConfigureMakeTarget(BuildTarget): @@ -328,17 +313,17 @@ class ConfigureMakeTarget(BuildTarget): try: # Try with host and disabled dependency tracking first - subprocess.check_call(args, cwd=work_path, env=self.environment) + subprocess.check_call(args, cwd=work_path, env=state.environment) except subprocess.CalledProcessError: # If it fails, try with disabled dependency tracking only args = copy.copy(common_args) args.append(disable_dependency_tracking) try: - subprocess.check_call(args, cwd=work_path, env=self.environment) + subprocess.check_call(args, cwd=work_path, env=state.environment) except subprocess.CalledProcessError: # Use only common command line arguments - subprocess.check_call(common_args, cwd=work_path, env=self.environment) + subprocess.check_call(common_args, cwd=work_path, env=state.environment) def build(self, state: BuildState): assert not state.xcode @@ -415,7 +400,7 @@ class CMakeTarget(BuildTarget): args += self.options.to_list(CommandLineOptions.CMAKE_RULES) args.append(state.source / self.src_root) - subprocess.check_call(args, cwd=state.build_path, env=self.environment) + subprocess.check_call(args, cwd=state.build_path, env=state.environment) def build(self, state: BuildState): if state.xcode: @@ -426,7 +411,7 @@ class CMakeTarget(BuildTarget): if state.verbose: args.append('VERBOSE=1') - subprocess.check_call(args, cwd=state.build_path, env=self.environment) + subprocess.check_call(args, cwd=state.build_path, env=state.environment) class ConfigureMakeDependencyTarget(ConfigureMakeTarget): diff --git a/aedi/target/library_tier1.py b/aedi/target/library_tier1.py index 350196bd..8313588f 100644 --- a/aedi/target/library_tier1.py +++ b/aedi/target/library_tier1.py @@ -41,7 +41,7 @@ class Bzip2Target(MakeTarget): opts['bzip2recover'] = None # Copy compiler flags from environment to command line argument, they would be overridden by Makefile otherwise cflags = 'CFLAGS' - opts[cflags] = self.environment[cflags] + ' -D_FILE_OFFSET_BITS=64 -O2' + opts[cflags] = state.environment[cflags] + ' -D_FILE_OFFSET_BITS=64 -O2' def post_build(self, state: BuildState): self.options['PREFIX'] = state.install_path @@ -158,7 +158,7 @@ class GlibTarget(BuildTarget): def configure(self, state: BuildState): super().configure(state) - environment = self.environment + environment = state.environment environment['LDFLAGS'] += ' -framework CoreFoundation -framework Foundation' cpu = state.architecture() @@ -194,7 +194,7 @@ endian = 'little' def build(self, state: BuildState): args = ('ninja',) - subprocess.check_call(args, cwd=state.build_path, env=self.environment) + subprocess.check_call(args, cwd=state.build_path, env=state.environment) def post_build(self, state: BuildState): self.install(state, tool='ninja') @@ -224,9 +224,6 @@ class InstPatchTarget(CMakeStaticDependencyTarget): super().__init__(name) self.options['LIB_SUFFIX'] = None - # Workaround for missing frameworks in dependencies, no clue what's wrong at the moment - self.environment['LDFLAGS'] = '-framework CoreFoundation -framework Foundation' - def prepare_source(self, state: BuildState): state.download_source( 'https://github.com/swami/libinstpatch/archive/v1.1.6.tar.gz', @@ -235,6 +232,12 @@ class InstPatchTarget(CMakeStaticDependencyTarget): def detect(self, state: BuildState) -> bool: return state.has_source_file('libinstpatch-1.0.pc.in') + def configure(self, state: BuildState): + # Workaround for missing frameworks in dependencies, no clue what's wrong at the moment + state.environment['LDFLAGS'] = '-framework CoreFoundation -framework Foundation' + + super().configure(state) + class IntlTarget(GettextTarget): def __init__(self, name='intl'): diff --git a/aedi/target/library_tier2.py b/aedi/target/library_tier2.py index 7de33780..6d4ac25d 100644 --- a/aedi/target/library_tier2.py +++ b/aedi/target/library_tier2.py @@ -126,7 +126,7 @@ class FreeImageTarget(MakeTarget): # These flags are copied from Makefile.gnu common_flags = ' -O3 -fPIC -fexceptions -fvisibility=hidden' - env = self.environment + env = state.environment env['CFLAGS'] += common_flags + ' -std=gnu89 -Wno-implicit-function-declaration' env['CXXFLAGS'] += common_flags + ' -Wno-ctor-dtor-privacy' @@ -532,7 +532,7 @@ class Sdl2MixerTarget(ConfigureMakeStaticDependencyTarget): def configure(self, state: BuildState): # Set LDFLAGS explicitly to help with FluidSynth and FLAC detection - self.environment['LDFLAGS'] = state.run_pkg_config('--libs', 'fluidsynth') + state.environment['LDFLAGS'] = state.run_pkg_config('--libs', 'fluidsynth') super().configure(state) @@ -615,7 +615,7 @@ class SfmlTarget(CMakeStaticDependencyTarget): opts['OPENAL_INCLUDE_DIR'] = state.include_path / 'AL' opts['OPENAL_LIBRARY'] = state.lib_path / 'libopenal.a' - super(SfmlTarget, self).configure(state) + super().configure(state) def detect(self, state: BuildState) -> bool: return state.has_source_file('libtiff-4.pc.in') diff --git a/aedi/target/main.py b/aedi/target/main.py index 9ef9a91c..c22d9df3 100644 --- a/aedi/target/main.py +++ b/aedi/target/main.py @@ -336,7 +336,7 @@ class EDuke32Target(MakeMainTarget): super().configure(state) # Fix missing definition when building with SDK older than 10.12 - self._update_env('CXXFLAGS', '-DCLOCK_MONOTONIC=0') + state.update_environment('CXXFLAGS', '-DCLOCK_MONOTONIC=0') class NBloodTarget(EDuke32Target): diff --git a/aedi/target/tools.py b/aedi/target/tools.py index 403d0ef4..99b8192b 100644 --- a/aedi/target/tools.py +++ b/aedi/target/tools.py @@ -53,7 +53,7 @@ class CMakeBuildTarget(CMakeTarget): assert boostrap_cmake.exists() - env = self.environment + env = state.environment env['PATH'] = os.pathsep.join([str(boostrap_cmk_path), env['PATH']]) super().configure(state) @@ -135,7 +135,7 @@ class NinjaTarget(MakeTarget): ) for args in cmdlines: - subprocess.run(args, check=True, cwd=state.build_path, env=self.environment) + subprocess.run(args, check=True, cwd=state.build_path, env=state.environment) def post_build(self, state: BuildState): self.copy_to_bin(state) @@ -229,7 +229,7 @@ class ZipTarget(MakeTarget): ] for var in ('CFLAGS', 'LDFLAGS'): - args += shlex.split(self.environment[var]) + args += shlex.split(state.environment[var]) subprocess.run(args, check=True, cwd=state.build_path)