Skip to content

Commit

Permalink
python/build/toolchain.py: add AnyToolchain for type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 26, 2023
1 parent 4669f7e commit 5f253e6
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 27 deletions.
5 changes: 3 additions & 2 deletions python/build/autotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Collection, Iterable, Optional

from build.makeproject import MakeProject
from .toolchain import AnyToolchain

class AutotoolsProject(MakeProject):
def __init__(self, url: str, md5: str, installed: str,
Expand All @@ -22,7 +23,7 @@ def __init__(self, url: str, md5: str, installed: str,
self.libs = libs
self.subdirs = subdirs

def configure(self, toolchain) -> str:
def configure(self, toolchain: AnyToolchain) -> str:
src = self.unpack(toolchain)
if self.autogen:
if sys.platform == 'darwin':
Expand Down Expand Up @@ -70,7 +71,7 @@ def configure(self, toolchain) -> str:

return build

def _build(self, toolchain) -> None:
def _build(self, toolchain: AnyToolchain) -> None:
build = self.configure(toolchain)
if self.subdirs is not None:
for subdir in self.subdirs:
Expand Down
9 changes: 5 additions & 4 deletions python/build/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Mapping

from build.project import Project
from .toolchain import AnyToolchain

def __write_cmake_compiler(f: TextIO, language: str, compiler: str) -> None:
s = compiler.split(' ', 1)
Expand All @@ -13,7 +14,7 @@ def __write_cmake_compiler(f: TextIO, language: str, compiler: str) -> None:
compiler = s[1]
print(f'set(CMAKE_{language}_COMPILER {compiler})', file=f)

def __write_cmake_toolchain_file(f: TextIO, toolchain) -> None:
def __write_cmake_toolchain_file(f: TextIO, toolchain: AnyToolchain) -> None:
if '-darwin' in toolchain.actual_arch:
cmake_system_name = 'Darwin'
elif toolchain.is_windows:
Expand Down Expand Up @@ -54,7 +55,7 @@ def __write_cmake_toolchain_file(f: TextIO, toolchain) -> None:
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
""")

def configure(toolchain, src: str, build: str, args: list[str]=[], env: Optional[Mapping[str, str]]=None) -> None:
def configure(toolchain: AnyToolchain, src: str, build: str, args: list[str]=[], env: Optional[Mapping[str, str]]=None) -> None:
cross_args = []

if toolchain.is_windows:
Expand Down Expand Up @@ -103,7 +104,7 @@ def __init__(self, url: str, md5: str, installed: str,
self.windows_configure_args = windows_configure_args
self.env = env

def configure(self, toolchain) -> str:
def configure(self, toolchain: AnyToolchain) -> str:
src = self.unpack(toolchain)
build = self.make_build_path(toolchain)
configure_args = self.configure_args
Expand All @@ -112,7 +113,7 @@ def configure(self, toolchain) -> str:
configure(toolchain, src, build, configure_args, self.env)
return build

def _build(self, toolchain) -> None:
def _build(self, toolchain: AnyToolchain) -> None:
build = self.configure(toolchain)
subprocess.check_call(['ninja', '-v', 'install'],
cwd=build, env=toolchain.env)
9 changes: 5 additions & 4 deletions python/build/makeproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional

from build.project import Project
from .toolchain import AnyToolchain

class MakeProject(Project):
def __init__(self, url: str, md5: str, installed: str,
Expand All @@ -18,17 +19,17 @@ def get_simultaneous_jobs(self) -> int:
# default to 12, if multiprocessing.cpu_count() is not implemented
return 12

def get_make_args(self, toolchain) -> list[str]:
def get_make_args(self, toolchain: AnyToolchain) -> list[str]:
return ['--quiet', '-j' + str(self.get_simultaneous_jobs())]

def get_make_install_args(self, toolchain) -> list[str]:
def get_make_install_args(self, toolchain: AnyToolchain) -> list[str]:
return ['--quiet', self.install_target]

def make(self, toolchain, wd: str, args: list[str]) -> None:
def make(self, toolchain: AnyToolchain, wd: str, args: list[str]) -> None:
subprocess.check_call(['make'] + args,
cwd=wd, env=toolchain.env)

def build_make(self, toolchain, wd: str, install: bool=True) -> None:
def build_make(self, toolchain: AnyToolchain, wd: str, install: bool=True) -> None:
self.make(toolchain, wd, self.get_make_args(toolchain))
if install:
self.make(toolchain, wd, self.get_make_install_args(toolchain))
9 changes: 5 additions & 4 deletions python/build/meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from typing import Optional

from build.project import Project
from .toolchain import AnyToolchain

def make_cross_file(toolchain) -> str:
def make_cross_file(toolchain: AnyToolchain) -> str:
if toolchain.is_windows:
system = 'windows'
windres = "windres = '%s'" % toolchain.windres
Expand Down Expand Up @@ -81,7 +82,7 @@ def make_cross_file(toolchain) -> str:
""")
return path

def configure(toolchain, src: str, build: str, args: list[str]=[]) -> None:
def configure(toolchain: AnyToolchain, src: str, build: str, args: list[str]=[]) -> None:
cross_file = make_cross_file(toolchain)
configure = [
'meson', 'setup',
Expand Down Expand Up @@ -110,13 +111,13 @@ def __init__(self, url: str, md5: str, installed: str,
Project.__init__(self, url, md5, installed, **kwargs)
self.configure_args = configure_args

def configure(self, toolchain) -> str:
def configure(self, toolchain: AnyToolchain) -> str:
src = self.unpack(toolchain)
build = self.make_build_path(toolchain)
configure(toolchain, src, build, self.configure_args)
return build

def _build(self, toolchain) -> None:
def _build(self, toolchain: AnyToolchain) -> None:
build = self.configure(toolchain)
subprocess.check_call(['ninja', '-v', 'install'],
cwd=build, env=toolchain.env)
7 changes: 4 additions & 3 deletions python/build/openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Optional

from build.makeproject import MakeProject
from .toolchain import AnyToolchain

class OpenSSLProject(MakeProject):
def __init__(self, url: str, md5: str, installed: str,
**kwargs):
MakeProject.__init__(self, url, md5, installed, install_target='install_dev', **kwargs)

def get_make_args(self, toolchain) -> list[str]:
def get_make_args(self, toolchain: AnyToolchain) -> list[str]:
return MakeProject.get_make_args(self, toolchain) + [
'CC=' + toolchain.cc,
'CFLAGS=' + toolchain.cflags,
Expand All @@ -18,13 +19,13 @@ def get_make_args(self, toolchain) -> list[str]:
'build_libs',
]

def get_make_install_args(self, toolchain) -> list[str]:
def get_make_install_args(self, toolchain: AnyToolchain) -> list[str]:
# OpenSSL's Makefile runs "ranlib" during installation
return MakeProject.get_make_install_args(self, toolchain) + [
'RANLIB=' + toolchain.ranlib,
]

def _build(self, toolchain) -> None:
def _build(self, toolchain: AnyToolchain) -> None:
src = self.unpack(toolchain, out_of_tree=False)

# OpenSSL has a weird target architecture scheme with lots of
Expand Down
11 changes: 6 additions & 5 deletions python/build/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from build.download import download_and_verify
from build.tar import untar
from build.quilt import push_all
from .toolchain import AnyToolchain

class Project:
def __init__(self, url: str, md5: str, installed: str,
Expand Down Expand Up @@ -41,10 +42,10 @@ def __init__(self, url: str, md5: str, installed: str,
self.edits = edits
self.use_cxx = use_cxx

def download(self, toolchain) -> str:
def download(self, toolchain: AnyToolchain) -> str:
return download_and_verify(self.url, self.md5, toolchain.tarball_path)

def is_installed(self, toolchain) -> bool:
def is_installed(self, toolchain: AnyToolchain) -> bool:
tarball = self.download(toolchain)
installed = os.path.join(toolchain.install_prefix, self.installed)
tarball_mtime = os.path.getmtime(tarball)
Expand All @@ -53,7 +54,7 @@ def is_installed(self, toolchain) -> bool:
except FileNotFoundError:
return False

def unpack(self, toolchain, out_of_tree: bool=True) -> str:
def unpack(self, toolchain: AnyToolchain, out_of_tree: bool=True) -> str:
if out_of_tree:
parent_path = toolchain.src_path
else:
Expand All @@ -74,7 +75,7 @@ def unpack(self, toolchain, out_of_tree: bool=True) -> str:

return path

def make_build_path(self, toolchain, lazy: bool=False) -> str:
def make_build_path(self, toolchain: AnyToolchain, lazy: bool=False) -> str:
path = os.path.join(toolchain.build_path, self.base)
if lazy and os.path.isdir(path):
return path
Expand All @@ -85,5 +86,5 @@ def make_build_path(self, toolchain, lazy: bool=False) -> str:
os.makedirs(path, exist_ok=True)
return path

def build(self, toolchain) -> None:
def build(self, toolchain: AnyToolchain) -> None:
self._build(toolchain)
6 changes: 4 additions & 2 deletions python/build/quilt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import subprocess
from typing import Union

def run_quilt(toolchain, cwd: str, patches_path: str, *args: str) -> None:
from .toolchain import AnyToolchain

def run_quilt(toolchain: AnyToolchain, cwd: str, patches_path: str, *args: str) -> None:
env = dict(toolchain.env)
env['QUILT_PATCHES'] = patches_path
subprocess.check_call(['quilt'] + list(args), cwd=cwd, env=env)

def push_all(toolchain, src_path: str, patches_path: str) -> None:
def push_all(toolchain: AnyToolchain, src_path: str, patches_path: str) -> None:
run_quilt(toolchain, src_path, patches_path, 'push', '-a')
3 changes: 3 additions & 0 deletions python/build/toolchain.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os.path
import shutil
from typing import Union

android_abis = {
'armeabi-v7a': {
Expand Down Expand Up @@ -172,3 +173,5 @@ def __init__(self, top_path: str,
self.pkg_config = shutil.copy(os.path.join(top_path, 'build', 'pkg-config.sh'),
os.path.join(bin_dir, 'pkg-config'))
self.env['PKG_CONFIG'] = self.pkg_config

AnyToolchain = Union[AndroidNdkToolchain, MingwToolchain]
7 changes: 4 additions & 3 deletions python/build/zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Optional

from build.makeproject import MakeProject
from .toolchain import AnyToolchain

class ZlibProject(MakeProject):
def __init__(self, url: str, md5: str, installed: str,
**kwargs):
MakeProject.__init__(self, url, md5, installed, **kwargs)

def get_make_args(self, toolchain) -> list[str]:
def get_make_args(self, toolchain: AnyToolchain) -> list[str]:
return MakeProject.get_make_args(self, toolchain) + [
'CC=' + toolchain.cc + ' ' + toolchain.cppflags + ' ' + toolchain.cflags,
'CPP=' + toolchain.cc + ' -E ' + toolchain.cppflags,
Expand All @@ -19,13 +20,13 @@ def get_make_args(self, toolchain) -> list[str]:
'libz.a'
]

def get_make_install_args(self, toolchain) -> list[str]:
def get_make_install_args(self, toolchain: AnyToolchain) -> list[str]:
return [
'RANLIB=' + toolchain.ranlib,
self.install_target
]

def _build(self, toolchain) -> None:
def _build(self, toolchain: AnyToolchain) -> None:
src = self.unpack(toolchain, out_of_tree=False)

subprocess.check_call(['./configure', '--prefix=' + toolchain.install_prefix, '--static'],
Expand Down

0 comments on commit 5f253e6

Please sign in to comment.