11#!/usr/bin/env python3
22
33import argparse
4+ from glob import glob
45import os
56import re
67import shutil
1617CROSS_BUILD_DIR = CHECKOUT / "cross-build"
1718
1819
19- def delete_if_exists (path ):
20- if path .exists ():
20+ def delete_glob (pattern ):
21+ # Path.glob doesn't accept non-relative patterns.
22+ for path in glob (str (pattern )):
23+ path = Path (path )
2124 print (f"Deleting { path } ..." )
22- shutil .rmtree (path )
25+ if path .is_dir () and not path .is_symlink ():
26+ shutil .rmtree (path )
27+ else :
28+ path .unlink ()
2329
2430
2531def subdir (name , * , clean = None ):
2632 path = CROSS_BUILD_DIR / name
2733 if clean :
28- delete_if_exists (path )
34+ delete_glob (path )
2935 if not path .exists ():
3036 if clean is None :
3137 sys .exit (
@@ -150,10 +156,17 @@ def configure_host_python(context):
150156
151157
152158def make_host_python (context ):
159+ # The CFLAGS and LDFLAGS set in android-env include the prefix dir, so
160+ # delete any previously-installed Python libs and include files to prevent
161+ # them being used during the build.
153162 host_dir = subdir (context .host )
163+ prefix_dir = host_dir / "prefix"
164+ delete_glob (f"{ prefix_dir } /include/python*" )
165+ delete_glob (f"{ prefix_dir } /lib/libpython*" )
166+
154167 os .chdir (host_dir / "build" )
155168 run (["make" , "-j" , str (os .cpu_count ())], host = context .host )
156- run (["make" , "install" , f"prefix={ host_dir } /prefix " ], host = context .host )
169+ run (["make" , "install" , f"prefix={ prefix_dir } " ], host = context .host )
157170
158171
159172def build_all (context ):
@@ -164,7 +177,7 @@ def build_all(context):
164177
165178
166179def clean_all (context ):
167- delete_if_exists (CROSS_BUILD_DIR )
180+ delete_glob (CROSS_BUILD_DIR )
168181
169182
170183# To avoid distributing compiled artifacts without corresponding source code,
0 commit comments