forked from yan12125/python3-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.py
More file actions
153 lines (118 loc) · 4.53 KB
/
Copy pathpackage.py
File metadata and controls
153 lines (118 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import importlib
import itertools
import os.path
import pathlib
from typing import Dict, Iterator, List, Optional, Sequence, Union
from .patch import Patch
from .source import Source, URLSource, VCSSource
from .util import (
_PathType,
android_api_level,
BASE,
target_arch,
)
class Package:
BUILDDIR = BASE / 'build'
SYSROOT = BUILDDIR / 'sysroot'
version: Optional[str] = None
source: Optional[Source] = None
patches: List[Patch] = []
dependencies: List[str] = []
skip_uploading: bool = False
def __init__(self):
self.name = type(self).__name__.lower()
self.arch = target_arch().__class__.__name__
self.env: Dict[str, Union[_PathType, Sequence[_PathType]]] = {}
for f in itertools.chain(self.sources, self.patches):
f.package = self
for directory in (self.SYSROOT,):
directory.mkdir(exist_ok=True, parents=True)
def get_version(self):
return self.version or self.source.get_version()
@property
def sources(self) -> List[Source]:
ret = []
for source in itertools.chain([self.source], self.patches):
if source and isinstance(source, Source):
ret.append(source)
if source.sig_suffix:
ret.append(URLSource(
source.source_url + source.sig_suffix))
return ret
@classmethod
def destdir(cls) -> pathlib.Path:
return cls.SYSROOT
def init_build_env(self) -> bool:
if self.env:
return False
self._check_ndk()
CLANG_PREFIX = (self.unified_toolchain /
f'{target_arch().ANDROID_TARGET}{android_api_level()}')
cflags = ['-fPIC']
self.env.update({
# Compilers
'CC': f'{CLANG_PREFIX}-clang',
'CXX': f'{CLANG_PREFIX}-clang++',
'CPP': f'{CLANG_PREFIX}-clang -E',
# Compiler flags
'CPPFLAGS': [
f'-I{self.SYSROOT}/usr/include',
],
'CFLAGS': cflags,
'CXXFLAGS': cflags,
'LDFLAGS': [
f'-L{self.SYSROOT}/usr/lib',
'-pie',
'-fuse-ld=lld',
],
# pkg-config settings
'PKG_CONFIG_SYSROOT_DIR': self.SYSROOT,
'PKG_CONFIG_LIBDIR': self.SYSROOT / 'usr' / 'lib' / 'pkgconfig',
})
for prog in ('ar', 'as', 'ld', 'objcopy', 'objdump', 'ranlib', 'strip', 'readelf'):
self.env[prog.upper()] = self.unified_toolchain / f'{target_arch().binutils_prefix}-{prog}'
return True
@property
def filesdir(self) -> pathlib.Path:
return BASE / 'mk' / self.name
def need_download(self) -> bool:
if not self.source:
return False
if isinstance(self.source, VCSSource):
return True
return not (self.source.source_dir / 'Makefile').exists()
def run(self, cmd: List[str], *args, **kwargs) -> None:
assert isinstance(self.source, Source)
self.source.run_in_source_dir(cmd, *args, **kwargs)
def run_with_env(self, cmd: List[str]) -> None:
assert isinstance(self.source, Source)
self.init_build_env()
self.source.run_in_source_dir(cmd, env=self.env)
def _check_ndk(self) -> None:
ndk_path = os.getenv('ANDROID_NDK')
if not ndk_path:
raise Exception('Requires environment variable $ANDROID_NDK')
ndk = pathlib.Path(ndk_path)
HOST_OS = os.uname().sysname.lower()
if HOST_OS not in ('linux', 'darwin'):
raise Exception(f'Unsupported system {HOST_OS}')
self.unified_toolchain = ndk / 'toolchains' / 'llvm' / 'prebuilt' / f'{HOST_OS}-x86_64' / 'bin'
if not self.unified_toolchain.exists():
raise Exception('Requires Android NDK r19 or above')
def prepare(self):
raise NotImplementedError
def build(self):
raise NotImplementedError
def import_package(pkgname: str) -> Package:
pkgmod = importlib.import_module(f'pybuild.packages.{pkgname}')
for symbol_name in dir(pkgmod):
symbol = getattr(pkgmod, symbol_name)
if type(symbol) == type and symbol_name.lower() == pkgname:
return symbol()
raise Exception(f'Package {pkgname} not found')
def enumerate_packages() -> Iterator[str]:
for child in (pathlib.Path(__file__).parent / 'packages').iterdir():
pkgname, ext = os.path.splitext(os.path.basename(child))
if ext != '.py':
continue
yield pkgname