Skip to content

Commit

Permalink
Move all environment related stuff from build_arch to get_recipe_env
Browse files Browse the repository at this point in the history
In order to keep the recipe more maintainable and readable, all the environment variables has been moved into method "get_recipe_env", overwriting the base class. To achieve this goal some other changes has been made:
    - Remove unused variables (platform_dir)
    - Move some "toolchain bin variables" directly into the env dictionary (because they are only used to set the corresponding env variables)
    - Add new env variable HOSTARCH (we use that in build_arch, cause this commit moves the needed variable that was used before this commit...and moreover...this new variable targets 'android_host' which still is not set as it should, so introducing this variable we avoid future errors when the target variable 'android_host'  will get fixed)
    - Shorted long lines to avoid pep8 errors and to be more readable
    - Add some todo comments

References: kivy#1412
  • Loading branch information
opacam committed Oct 19, 2018
1 parent a529d77 commit ff8e45b
Showing 1 changed file with 62 additions and 52 deletions.
114 changes: 62 additions & 52 deletions pythonforandroid/recipes/python3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,60 @@ class Python3Recipe(TargetPythonRecipe):
patches = ['patches/python-{version}-libs.patch'.format(version=version),
'patches/fix-termios.patch']

def get_recipe_env(self, arch=None, with_flags_in_cc=True):
env = environ.copy()

# TODO: Get this information from p4a's arch system
env['HOSTARCH'] = android_host = 'arm-linux-androideabi'
toolchain = '{android_host}-4.9'.format(android_host=android_host)
toolchain = join(self.ctx.ndk_dir, 'toolchains',
toolchain, 'prebuilt', 'linux-x86_64')

env['CC'] = \
'{clang} -target {target} -gcc-toolchain {toolchain}'.format(
clang=join(
self.ctx.ndk_dir, 'toolchains', 'llvm',
'prebuilt', 'linux-x86_64', 'bin', 'clang'),
target='armv7-none-linux-androideabi',
toolchain=toolchain)
env['AR'] = join(toolchain, 'bin', android_host) + '-ar'
env['LD'] = join(toolchain, 'bin', android_host) + '-ld'
env['RANLIB'] = join(toolchain, 'bin', android_host) + '-ranlib'
env['READELF'] = join(toolchain, 'bin', android_host) + '-readelf'
env['STRIP'] = \
join(toolchain, 'bin', android_host) + \
'-strip --strip-debug --strip-unneeded'

ndk_flags = \
'--sysroot={ndk_sysroot} -D__ANDROID_API__=21 ' \
'-isystem {ndk_android_host}'.format(
ndk_sysroot=join(self.ctx.ndk_dir, 'sysroot'),
ndk_android_host=join(self.ctx.ndk_dir, 'sysroot',
'usr', 'include', android_host))
sysroot = join(self.ctx.ndk_dir, 'platforms', 'android-21', 'arch-arm')
env['CFLAGS'] = env.get('CFLAGS', '') + ' ' + ndk_flags
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + ' ' + ndk_flags
env['LDFLAGS'] = \
env.get('LDFLAGS', '') + ' --sysroot={} -L{}'.format(
sysroot, join(sysroot, 'usr', 'lib'))
# Manually add the libs directory, and copy some object
# files to the current directory otherwise they aren't
# picked up. This seems necessary because the --sysroot
# setting in LDFLAGS is overridden by the other flags.
# TODO: Work out why this doesn't happen in the original
# bpo-30386 Makefile system.
logger.warning('Doing some hacky stuff to link properly')
lib_dir = join(sysroot, 'usr', 'lib')
env['LDFLAGS'] += ' -L{}'.format(lib_dir)
# TODO: Maybe this should be moved somewhere else?
shprint(sh.cp, join(lib_dir, 'crtbegin_so.o'), './')
shprint(sh.cp, join(lib_dir, 'crtend_so.o'), './')

env['SYSROOT'] = sysroot

env = self.set_libs_flags(env, arch)
return env

def set_libs_flags(self, env, arch=None):
# Takes an env as argument and adds cflags/ldflags
# based on libs_info and versioned_libs.
Expand Down Expand Up @@ -144,60 +198,16 @@ def build_arch(self, arch):
sys_prefix = '/usr/local'
sys_exec_prefix = '/usr/local'

# Skipping "Ensure that nl_langinfo is broken" from the original bpo-30386
# TODO: Why we put the below comment? It's still needed?
# Skipping "Ensure that nl_langinfo is broken"
# from the original bpo-30386

with current_directory(build_dir):
env = environ.copy()

# TODO: Get this information from p4a's arch system
android_host = 'arm-linux-androideabi'
android_build = sh.Command(join(recipe_build_dir, 'config.guess'))().stdout.strip().decode('utf-8')
platform_dir = join(self.ctx.ndk_dir, 'platforms', 'android-21', 'arch-arm')
toolchain = '{android_host}-4.9'.format(android_host=android_host)
toolchain = join(self.ctx.ndk_dir, 'toolchains', toolchain, 'prebuilt', 'linux-x86_64')
CC = '{clang} -target {target} -gcc-toolchain {toolchain}'.format(
clang=join(self.ctx.ndk_dir, 'toolchains', 'llvm', 'prebuilt', 'linux-x86_64', 'bin', 'clang'),
target='armv7-none-linux-androideabi',
toolchain=toolchain)

AR = join(toolchain, 'bin', android_host) + '-ar'
LD = join(toolchain, 'bin', android_host) + '-ld'
RANLIB = join(toolchain, 'bin', android_host) + '-ranlib'
READELF = join(toolchain, 'bin', android_host) + '-readelf'
STRIP = join(toolchain, 'bin', android_host) + '-strip --strip-debug --strip-unneeded'
env = self.get_recipe_env(arch)

env['CC'] = CC
env['AR'] = AR
env['LD'] = LD
env['RANLIB'] = RANLIB
env['READELF'] = READELF
env['STRIP'] = STRIP

ndk_flags = '--sysroot={ndk_sysroot} -D__ANDROID_API__=21 -isystem {ndk_android_host}'.format(
ndk_sysroot=join(self.ctx.ndk_dir, 'sysroot'),
ndk_android_host=join(self.ctx.ndk_dir, 'sysroot', 'usr', 'include', android_host))
sysroot = join(self.ctx.ndk_dir, 'platforms', 'android-21', 'arch-arm')
env['CFLAGS'] = env.get('CFLAGS', '') + ' ' + ndk_flags
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + ' ' + ndk_flags
env['LDFLAGS'] = env.get('LDFLAGS', '') + ' --sysroot={} -L{}'.format(sysroot, join(sysroot, 'usr', 'lib'))

# Manually add the libs directory, and copy some object
# files to the current directory otherwise they aren't
# picked up. This seems necessary because the --sysroot
# setting in LDFLAGS is overridden by the other flags.
# TODO: Work out why this doesn't happen in the original
# bpo-30386 Makefile system.
logger.warning('Doing some hacky stuff to link properly')
lib_dir = join(sysroot, 'usr', 'lib')
env['LDFLAGS'] += ' -L{}'.format(lib_dir)
shprint(sh.cp, join(lib_dir, 'crtbegin_so.o'), './')
shprint(sh.cp, join(lib_dir, 'crtend_so.o'), './')

env['SYSROOT'] = sysroot

# TODO: All the env variables should be moved
# into method: get_recipe_env (all above included)
env = self.set_libs_flags(env, arch)
android_build = sh.Command(
join(self.get_build_dir(arch.arch),
'config.guess'))().stdout.strip().decode('utf-8')

# Arguments for python configure
# TODO: move ac_xx_ arguments to config.site
Expand All @@ -221,7 +231,7 @@ def build_arch(self, arch):
if not exists('config.status'):
shprint(sh.Command(join(recipe_build_dir, 'configure')),
*(' '.join(configure_args).format(
android_host=android_host,
android_host=env['HOSTARCH'],
android_build=android_build,
prefix=sys_prefix,
exec_prefix=sys_exec_prefix)).split(' '), _env=env)
Expand Down

0 comments on commit ff8e45b

Please sign in to comment.