Skip to content

Commit

Permalink
📦 Split logic for build modes & debug symbols (#2213)
Browse files Browse the repository at this point in the history
If we want to **keep** debug symbols from `.so` files we will use:
  `--with-debug-symbols`

Also:
  - enable debug builds for all bootstraps (AndroidManifest.tmpl.xml)
  - add log message to make it clear what kind of build we will get
  • Loading branch information
opacam authored May 25, 2020
1 parent d96d71e commit 0610d20
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 12 deletions.
3 changes: 3 additions & 0 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ def parse_args_and_make_package(args=None):
const='release', default='debug',
help='Build your app as a non-debug release build. '
'(Disables gdb debugging among other things)')
ap.add_argument('--with-debug-symbols', dest='with_debug_symbols',
action='store_const', const=True, default=False,
help='Will keep debug symbols from `.so` files.')
ap.add_argument('--add-jar', dest='add_jar', action='append',
help=('Add a Java .jar to the libs, so you can access its '
'classes with pyjnius. You can specify this '
Expand Down
2 changes: 1 addition & 1 deletion pythonforandroid/bootstraps/sdl2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def assemble_distribution(self):
with open('blacklist.txt', 'a') as fileh:
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')

if not self.ctx.build_as_debuggable:
if not self.ctx.with_debug_symbols:
self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)
super().assemble_distribution()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- Android 2.3.3 -->
<uses-sdk android:minSdkVersion="{{ args.min_sdk_version }}" android:targetSdkVersion="{{ android_api }}" />

<application>
<application {% if debug %}android:debuggable="true"{% endif %} >
{% for name in service_names %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
android:process=":service_{{ name }}"
Expand Down
2 changes: 1 addition & 1 deletion pythonforandroid/bootstraps/service_only/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def assemble_distribution(self):
with open('blacklist.txt', 'a') as fileh:
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')

if not self.ctx.build_as_debuggable:
if not self.ctx.with_debug_symbols:
self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)
super().assemble_distribution()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
An example Java class can be found in README-android.txt
-->
<application android:label="@string/app_name"
{% if debug %}android:debuggable="true"{% endif %}
android:icon="@drawable/icon"
android:allowBackup="true"
android:theme="{{args.android_apptheme}}{% if not args.window %}.Fullscreen{% endif %}"
Expand Down
2 changes: 1 addition & 1 deletion pythonforandroid/bootstraps/webview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def assemble_distribution(self):
with open('blacklist.txt', 'a') as fileh:
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')

if not self.ctx.build_as_debuggable:
if not self.ctx.with_debug_symbols:
self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)
super().assemble_distribution()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
android:theme="{{args.android_apptheme}}{% if not args.window %}.Fullscreen{% endif %}"
android:hardwareAccelerated="true"
android:usesCleartextTraffic="true"
{% if debug %}android:debuggable="true"{% endif %}
>
{% for l in args.android_used_libs %}
<uses-library android:name="{{ l }}" />
Expand Down
7 changes: 5 additions & 2 deletions pythonforandroid/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ class Context:
'''A build context. If anything will be built, an instance this class
will be instantiated and used to hold all the build state.'''

# Whether to build with debugging symbols
# Whether to make a debug or release build
build_as_debuggable = False

# Whether to strip debug symbols in `.so` files
with_debug_symbols = False

env = environ.copy()
# the filepath of toolchain.py
root_dir = None
Expand Down Expand Up @@ -831,7 +834,7 @@ def run_pymodules_install(ctx, modules, project_dir=None,
)

# Strip object files after potential Cython or native code builds:
if not ctx.build_as_debuggable:
if not ctx.with_debug_symbols:
standard_recipe.strip_object_files(
ctx.archs[0], env, build_dir=ctx.build_dir
)
Expand Down
2 changes: 1 addition & 1 deletion pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def build_cython_components(self, arch):
info('First build appeared to complete correctly, skipping manual'
'cythonising.')

if not self.ctx.build_as_debuggable:
if not self.ctx.with_debug_symbols:
self.strip_object_files(arch, env)

def strip_object_files(self, arch, env, build_dir=None):
Expand Down
17 changes: 17 additions & 0 deletions pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ def build_dist_from_args(ctx, dist, args):
ctx.recipe_build_order))
info('Dist will also contain modules ({}) installed from pip'.format(
', '.join(ctx.python_modules)))
info(
'Dist will be build in mode {build_mode}{with_debug_symbols}'.format(
build_mode='debug' if ctx.build_as_debuggable else 'release',
with_debug_symbols=' (with debug symbols)'
if ctx.with_debug_symbols
else '',
)
)

ctx.distribution = dist
ctx.prepare_bootstrap(bs)
Expand Down Expand Up @@ -518,6 +526,10 @@ def add_parser(subparsers, *args, **kwargs):
const='release', default='debug',
help='Build your app as a non-debug release build. '
'(Disables gdb debugging among other things)')
parser_packaging.add_argument(
'--with-debug-symbols', dest='with_debug_symbols',
action='store_const', const=True, default=False,
help='Will keep debug symbols from `.so` files.')
parser_packaging.add_argument(
'--keystore', dest='keystore', action='store', default=None,
help=('Keystore for JAR signing key, will use jarsigner '
Expand Down Expand Up @@ -593,6 +605,8 @@ def add_parser(subparsers, *args, **kwargs):
args.unknown_args += ["--private", args.private]
if hasattr(args, "build_mode") and args.build_mode == "release":
args.unknown_args += ["--release"]
if hasattr(args, "with_debug_symbols") and args.with_debug_symbols:
args.unknown_args += ["--with-debug-symbols"]
if hasattr(args, "ignore_setup_py") and args.ignore_setup_py:
args.use_setup_py = False

Expand All @@ -612,6 +626,9 @@ def add_parser(subparsers, *args, **kwargs):
self.ctx.build_as_debuggable = getattr(
args, "build_mode", "debug"
) == "debug"
self.ctx.with_debug_symbols = getattr(
args, "with_debug_symbols", False
)

have_setup_py_or_similar = False
if getattr(args, "private", None) is not None:
Expand Down
11 changes: 6 additions & 5 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_run_pymodules_install_optional_project_dir(self):
assert m_info.call_args_list[-1] == mock.call(
'No Python modules and no setup.py to process, skipping')

def test_strip_if_debuggable(self):
def test_strip_if_with_debug_symbols(self):
ctx = mock.Mock()
ctx.python_recipe.major_minor_version_string = "python3.6"
ctx.get_site_packages_dir.return_value = "test-doesntexist"
Expand All @@ -38,12 +38,13 @@ def test_strip_if_debuggable(self):
mock.patch('pythonforandroid.build.run_setuppy_install'):
m_project_has_setup_py.return_value = False

# Make sure it is NOT called when debug build:
ctx.build_as_debuggable = True
# Make sure it is NOT called when `with_debug_symbols` is true:
ctx.with_debug_symbols = True
assert run_pymodules_install(ctx, modules, project_dir) is None
assert m_CythonRecipe().strip_object_files.called is False

# Make sure strip object files IS called when release build:
ctx.build_as_debuggable = False
# Make sure strip object files IS called when
# `with_debug_symbols` is fasle:
ctx.with_debug_symbols = False
assert run_pymodules_install(ctx, modules, project_dir) is None
assert m_CythonRecipe().strip_object_files.called is True

0 comments on commit 0610d20

Please sign in to comment.