Skip to content

Commit

Permalink
Add ability to compile recipes with clang
Browse files Browse the repository at this point in the history
The android ndk has been migrated his default compiler from gcc to clang and soon will be removed from the ndk. Here we prepare p4a to compile recipes with clang, which will allow us to successfully compile an updated openssl libs. Without this openssl upgrade we will not be able to grant support to python3's _ssl.so module.

Note: The default p4a compiler still is gcc...so...no changes on the compile behaviour unless explicitly requested

References: kivy#1478
  • Loading branch information
tito authored and opacam committed Dec 16, 2018
1 parent f7d8fda commit a159a8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
68 changes: 45 additions & 23 deletions pythonforandroid/archs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@ def include_dirs(self):
d.format(arch=self))
for d in self.ctx.include_dirs]

def get_env(self, with_flags_in_cc=True):
def get_env(self, with_flags_in_cc=True, clang=False):
env = {}

env['CFLAGS'] = ' '.join([
'-DANDROID', '-mandroid', '-fomit-frame-pointer'
' -D__ANDROID_API__={}'.format(self.ctx.ndk_api),
])
cflags = [
'-DANDROID',
'-fomit-frame-pointer',
'-D__ANDROID_API__={}'.format(self.ctx.ndk_api)]
if not clang:
cflags += ['-mandroid']
else:
cflags += ['-target armv7-none-linux-androideabi']
android_host = 'arm-linux-androideabi'
platform_dir = join(self.ctx.ndk_dir, 'platforms', 'android-{}'.format(self.ctx.ndk_api), 'arch-arm')
toolchain = '{android_host}-4.9'.format(android_host=android_host)
toolchain = join(self.ctx.ndk_dir, 'toolchains', toolchain, 'prebuilt', 'linux-x86_64')
cflags += ['-gcc-toolchain {}'.format(toolchain)]

env['CFLAGS'] = ' '.join(cflags)
env['LDFLAGS'] = ' '

sysroot = join(self.ctx._ndk_dir, 'sysroot')
Expand Down Expand Up @@ -82,8 +93,19 @@ def get_env(self, with_flags_in_cc=True):
env['NDK_CCACHE'] = self.ctx.ccache
env.update({k: v for k, v in environ.items() if k.startswith('CCACHE_')})

cc = find_executable('{command_prefix}-gcc'.format(
command_prefix=command_prefix), path=environ['PATH'])
if clang:
clang_path = join(
self.ctx.ndk_dir, 'toolchains', 'llvm', 'prebuilt',
'linux-x86_64', 'bin')
environ['PATH'] = '{clang_path}:{path}'.format(
clang_path=clang_path, path=environ['PATH'])
exe = join(clang_path, 'clang')
execxx = join(clang_path, 'clang++')
else:
exe = '{command_prefix}-gcc'.format(command_prefix=command_prefix)
execxx = '{command_prefix}-g++'.format(command_prefix=command_prefix)

cc = find_executable(exe, path=environ['PATH'])
if cc is None:
print('Searching path are: {!r}'.format(environ['PATH']))
raise BuildInterruptingException(
Expand All @@ -93,20 +115,20 @@ def get_env(self, with_flags_in_cc=True):
'installed. Exiting.')

if with_flags_in_cc:
env['CC'] = '{ccache}{command_prefix}-gcc {cflags}'.format(
command_prefix=command_prefix,
env['CC'] = '{ccache}{exe} {cflags}'.format(
exe=exe,
ccache=ccache,
cflags=env['CFLAGS'])
env['CXX'] = '{ccache}{command_prefix}-g++ {cxxflags}'.format(
command_prefix=command_prefix,
env['CXX'] = '{ccache}{execxx} {cxxflags}'.format(
execxx=execxx,
ccache=ccache,
cxxflags=env['CXXFLAGS'])
else:
env['CC'] = '{ccache}{command_prefix}-gcc'.format(
command_prefix=command_prefix,
env['CC'] = '{ccache}{exe}'.format(
exe=exe,
ccache=ccache)
env['CXX'] = '{ccache}{command_prefix}-g++'.format(
command_prefix=command_prefix,
env['CXX'] = '{ccache}{execxx}'.format(
execxx=execxx,
ccache=ccache)

env['AR'] = '{}-ar'.format(command_prefix)
Expand Down Expand Up @@ -151,8 +173,8 @@ class ArchARM(Arch):
class ArchARMv7_a(ArchARM):
arch = 'armeabi-v7a'

def get_env(self, with_flags_in_cc=True):
env = super(ArchARMv7_a, self).get_env(with_flags_in_cc)
def get_env(self, with_flags_in_cc=True, clang=False):
env = super(ArchARMv7_a, self).get_env(with_flags_in_cc, clang=clang)
env['CFLAGS'] = (env['CFLAGS'] +
(' -march=armv7-a -mfloat-abi=softfp '
'-mfpu=vfp -mthumb'))
Expand All @@ -166,8 +188,8 @@ class Archx86(Arch):
command_prefix = 'i686-linux-android'
platform_dir = 'arch-x86'

def get_env(self, with_flags_in_cc=True):
env = super(Archx86, self).get_env(with_flags_in_cc)
def get_env(self, with_flags_in_cc=True, clang=False):
env = super(Archx86, self).get_env(with_flags_in_cc, clang=clang)
env['CFLAGS'] = (env['CFLAGS'] +
' -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32')
env['CXXFLAGS'] = env['CFLAGS']
Expand All @@ -180,8 +202,8 @@ class Archx86_64(Arch):
command_prefix = 'x86_64-linux-android'
platform_dir = 'arch-x86_64'

def get_env(self, with_flags_in_cc=True):
env = super(Archx86_64, self).get_env(with_flags_in_cc)
def get_env(self, with_flags_in_cc=True, clang=False):
env = super(Archx86_64, self).get_env(with_flags_in_cc, clang=clang)
env['CFLAGS'] = (env['CFLAGS'] +
' -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel')
env['CXXFLAGS'] = env['CFLAGS']
Expand All @@ -194,8 +216,8 @@ class ArchAarch_64(Arch):
command_prefix = 'aarch64-linux-android'
platform_dir = 'arch-arm64'

def get_env(self, with_flags_in_cc=True):
env = super(ArchAarch_64, self).get_env(with_flags_in_cc)
def get_env(self, with_flags_in_cc=True, clang=False):
env = super(ArchAarch_64, self).get_env(with_flags_in_cc, clang=clang)
incpath = ' -I' + join(dirname(__file__), 'includes', 'arm64-v8a')
env['EXTRA_CFLAGS'] = incpath
env['CFLAGS'] += incpath
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ def unpack(self, arch):
else:
info('{} is already unpacked, skipping'.format(self.name))

def get_recipe_env(self, arch=None, with_flags_in_cc=True):
def get_recipe_env(self, arch=None, with_flags_in_cc=True, clang=False):
"""Return the env specialized for the recipe
"""
if arch is None:
arch = self.filtered_archs[0]
return arch.get_env(with_flags_in_cc=with_flags_in_cc)
return arch.get_env(with_flags_in_cc=with_flags_in_cc, clang=clang)

def prebuild_arch(self, arch):
'''Run any pre-build tasks for the Recipe. By default, this checks if
Expand Down

0 comments on commit a159a8d

Please sign in to comment.