Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build packages for Android #647

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ install_requires =
GitPython >= 3.0.8
dmgbuild >= 1.3.3; sys_platform == "darwin"
Jinja2
androidenv >= 0.4

[options.packages.find]
where = src
Expand Down
52 changes: 45 additions & 7 deletions src/briefcase/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,40 @@ def install_app_support_package(self, app: BaseConfig):
print()
raise InvalidSupportPackage(support_package_url)

def build_env(self, app: BaseConfig):
"""
Return an environment in which to build the dependencies for the app.
This should include the path to Python.h in CPPFLAGS, and libpython
and its path in LDFLAGS.

:param app: The config object for the app
"""
env = dict(os.environ)
if self.platform == "android":
# sysconfig.get_config_var("INCLUDEPY")
includepy = os.path.join(
self.bundle_path(app), "app", "include",
"python{}m".format(self.python_version_tag),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path doesn't exist, is the trailing 'm' a mistake?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "m" is a suffix added by Python when it is configured with --with-pymalloc. It seems this is only valid for 3.6 and 3.7: https://github.com/beeware/Python-Android-support/blob/master/main.sh#L84 I'll fix this.

)
assert os.path.exists(includepy), includepy
cppflags = env.get("CPPFLAGS", "").split(" ")
cppflags = [
"-I{}".format(includepy.replace(" ", "\\ ")),
] + cppflags
env.update({"CPPFLAGS": " ".join(cppflags)})
# sysconfig.get_config_var("LIBDIR")
libdir = os.path.join(
self.bundle_path(app), "app", "libs", "arm64-v8a"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it hardcoded for arm64-v8a? It should be compatible with all available targets, as the emulator can run with a x86_64 arch for example.

Copy link
Author

@mansourmoufid mansourmoufid Dec 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point. This will require changes to the beeware/Python-Android-support project to include four architecture-specific "app_packages" directories, as well as changes to the beeware/briefcase-android-gradle-template project to add the right directory to sys.path. I'll try to implement this.

Edit: We may be able to avoid architecture-specific directories by having architecture-specific filename extensions. This is set by the variable EXT_SUFFIX in Python's configure script, and available at run-time with sysconfig.get_config_var("EXT_SUFFIX"). Something like ".arm64-v8a.so" maybe. Hmmm it can't be that easy...

)
assert os.path.exists(libdir), libdir
ldflags = env.get("LDFLAGS", "").split(" ")
ldflags = [
"-L{}".format(libdir.replace(" ", "\\ ")),
"-lpython{}m".format(self.python_version_tag),
] + ldflags
env.update({"LDFLAGS": " ".join(ldflags)})
return env

def install_app_dependencies(self, app: BaseConfig):
"""
Install the dependencies for the app.
Expand All @@ -387,15 +421,19 @@ def install_app_dependencies(self, app: BaseConfig):
"""
if app.requires:
try:
pip = [sys.executable, "-m", "pip"]
options = [
"--upgrade",
"--no-user",
"--target={}".format(self.app_packages_path(app)),
]
if self.platform == "android":
pip = [sys.executable, "-m", "androidenv"] + pip
options += ["--no-binary", ":all:"]
self.subprocess.run(
[
sys.executable, "-m",
"pip", "install",
"--upgrade",
"--no-user",
"--target={}".format(self.app_packages_path(app)),
] + app.requires,
pip + ["install"] + options + app.requires,
check=True,
env=self.build_env(app),
)
except subprocess.CalledProcessError:
raise DependencyInstallError()
Expand Down