2222SCRIPT_NAME = Path (__file__ ).name
2323CHECKOUT = Path (__file__ ).resolve ().parent .parent
2424ANDROID_DIR = CHECKOUT / "Android"
25+ BUILD_DIR = ANDROID_DIR / "build"
26+ DIST_DIR = ANDROID_DIR / "dist"
27+ PREFIX_DIR = ANDROID_DIR / "prefix"
2528TESTBED_DIR = ANDROID_DIR / "testbed"
26- CROSS_BUILD_DIR = CHECKOUT / "cross-build"
2729
30+ HOSTS = ["aarch64-linux-android" , "x86_64-linux-android" ]
2831APP_ID = "org.python.testbed"
2932DECODE_ARGS = ("UTF-8" , "backslashreplace" )
3033
@@ -58,12 +61,10 @@ def delete_glob(pattern):
5861 path .unlink ()
5962
6063
61- def subdir (name , * , clean = None ):
62- path = CROSS_BUILD_DIR / name
63- if clean :
64- delete_glob (path )
64+ def subdir (parent , host , * , create = False ):
65+ path = parent / host
6566 if not path .exists ():
66- if clean is None :
67+ if not create :
6768 sys .exit (
6869 f"{ path } does not exist. Create it by running the appropriate "
6970 f"`configure` subcommand of { SCRIPT_NAME } ." )
@@ -83,7 +84,7 @@ def run(command, *, host=None, env=None, log=True, **kwargs):
8384 env_output = subprocess .run (
8485 f"set -eu; "
8586 f"HOST={ host } ; "
86- f"PREFIX={ subdir (host )} /prefix ; "
87+ f"PREFIX={ subdir (PREFIX_DIR , host )} ; "
8788 f". { env_script } ; "
8889 f"export" ,
8990 check = True , shell = True , text = True , stdout = subprocess .PIPE
@@ -111,19 +112,21 @@ def run(command, *, host=None, env=None, log=True, **kwargs):
111112
112113def build_python_path ():
113114 """The path to the build Python binary."""
114- build_dir = subdir ("build" )
115- binary = build_dir / "python"
115+ build_subdir = subdir (BUILD_DIR , "build" )
116+ binary = build_subdir / "python"
116117 if not binary .is_file ():
117118 binary = binary .with_suffix (".exe" )
118119 if not binary .is_file ():
119120 raise FileNotFoundError ("Unable to find `python(.exe)` in "
120- f"{ build_dir } " )
121+ f"{ build_subdir } " )
121122
122123 return binary
123124
124125
125126def configure_build_python (context ):
126- os .chdir (subdir ("build" , clean = context .clean ))
127+ if context .clean :
128+ clean ("build" )
129+ os .chdir (subdir (BUILD_DIR , "build" , create = True ))
127130
128131 command = [relpath (CHECKOUT / "configure" )]
129132 if context .args :
@@ -132,7 +135,7 @@ def configure_build_python(context):
132135
133136
134137def make_build_python (context ):
135- os .chdir (subdir ("build" ))
138+ os .chdir (subdir (BUILD_DIR , "build" ))
136139 run (["make" , "-j" , str (os .cpu_count ())])
137140
138141
@@ -153,17 +156,16 @@ def download(url, target_dir="."):
153156
154157
155158def configure_host_python (context ):
156- host_dir = subdir (context .host , clean = context .clean )
159+ if context .clean :
160+ clean (context .host )
157161
158- prefix_dir = host_dir / "prefix"
159- if not prefix_dir .exists ():
160- prefix_dir .mkdir ()
161- os .chdir (prefix_dir )
162+ prefix_subdir = subdir (PREFIX_DIR , context .host , create = True )
163+ if not (prefix_subdir / "include" ).exists ():
164+ os .chdir (prefix_subdir )
162165 unpack_deps (context .host )
163166
164- build_dir = host_dir / "build"
165- build_dir .mkdir (exist_ok = True )
166- os .chdir (build_dir )
167+ build_subdir = subdir (BUILD_DIR , context .host , create = True )
168+ os .chdir (build_subdir )
167169
168170 command = [
169171 # Basic cross-compiling configuration
@@ -179,7 +181,7 @@ def configure_host_python(context):
179181
180182 # Dependent libraries. The others are found using pkg-config: see
181183 # android-env.sh.
182- f"--with-openssl={ prefix_dir } " ,
184+ f"--with-openssl={ prefix_subdir } " ,
183185 ]
184186
185187 if context .args :
@@ -191,15 +193,13 @@ def make_host_python(context):
191193 # The CFLAGS and LDFLAGS set in android-env include the prefix dir, so
192194 # delete any previous Python installation to prevent it being used during
193195 # the build.
194- host_dir = subdir (context .host )
195- prefix_dir = host_dir / "prefix"
196- delete_glob (f"{ prefix_dir } /include/python*" )
197- delete_glob (f"{ prefix_dir } /lib/libpython*" )
198- delete_glob (f"{ prefix_dir } /lib/python*" )
196+ prefix_subdir = subdir (PREFIX_DIR , context .host )
197+ for pattern in ("include/python*" , "lib/libpython*" , "lib/python*" ):
198+ delete_glob (f"{ prefix_subdir } /{ pattern } " )
199199
200- os .chdir (host_dir / "build" )
200+ os .chdir (subdir ( BUILD_DIR , context . host ) )
201201 run (["make" , "-j" , str (os .cpu_count ())], host = context .host )
202- run (["make" , "install" , f"prefix={ prefix_dir } " ], host = context .host )
202+ run (["make" , "install" , f"prefix={ prefix_subdir } " ], host = context .host )
203203
204204
205205def build_all (context ):
@@ -209,8 +209,14 @@ def build_all(context):
209209 step (context )
210210
211211
212+ def clean (host ):
213+ for parent in (BUILD_DIR , PREFIX_DIR ):
214+ delete_glob (f"{ parent } /{ host } " )
215+
216+
212217def clean_all (context ):
213- delete_glob (CROSS_BUILD_DIR )
218+ for parent in (BUILD_DIR , PREFIX_DIR ):
219+ delete_glob (parent )
214220
215221
216222def setup_sdk ():
0 commit comments