Skip to content

Commit

Permalink
Fixes building in venv
Browse files Browse the repository at this point in the history
Borrowed from p4a:
kivy/python-for-android#2159
Also refs upstream issue:
https://bugs.python.org/issue40261
Note that the fix used upstream cannot really be used directly here
because the actual system Python is impacted.
Adds venv build to CI to check for regressions.
  • Loading branch information
renabriseno68 committed May 1, 2020
1 parent 10e3a0c commit ab2b3ce
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 34 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/kivy_ios.yml
Expand Up @@ -48,6 +48,28 @@ jobs:
run: |
.ci/test_project.sh
build_python3_kivy_venv:
runs-on: macos-latest
steps:
- name: Checkout kivy-ios
uses: actions/checkout@v2
- name: Set up Python 3.7.x
uses: actions/setup-python@v1
with:
python-version: 3.7.x
- name: Install requirements
run: |
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
pip install sh
brew install autoconf automake libtool pkg-config
brew link libtool
pip install Cython==0.28.1
- name: Build Python & Kivy
run: |
python toolchain.py build python3 kivy
build_updated_recipes:
runs-on: macos-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions README.md
@@ -1,5 +1,6 @@
# Kivy for iOS

[![kivy-ios](https://github.com/kivy/kivy-ios/workflows/kivy-ios/badge.svg)](https://github.com/kivy/kivy-ios/actions?query=workflow%3Akivy-ios)
[![Backers on Open Collective](https://opencollective.com/kivy/backers/badge.svg)](#backers)
[![Sponsors on Open Collective](https://opencollective.com/kivy/sponsors/badge.svg)](#sponsors)

Expand Down
33 changes: 9 additions & 24 deletions recipes/host_setuptools3/__init__.py
@@ -1,35 +1,20 @@
from toolchain import Recipe, shprint
from os.path import join
from toolchain import Recipe, shprint, cd, cache_execution
import sh
import os
import shutil


class HostSetuptools3(Recipe):
depends = ["openssl", "hostpython3"]
archs = ["x86_64"]
url = "setuptools"
version = '40.9.0'
url = 'https://pypi.python.org/packages/source/s/setuptools/setuptools-{version}.zip'

def prebuild_arch(self, arch):
@cache_execution
def install(self):
arch = self.filtered_archs[0]
build_dir = self.get_build_dir(arch.arch)
hostpython = sh.Command(self.ctx.hostpython)
sh.curl("-O", "https://bootstrap.pypa.io/ez_setup.py")
shprint(hostpython, "./ez_setup.py")
# Extract setuptools egg and remove .pth files. Otherwise subsequent
# python package installations using setuptools will raise exceptions.
# Setuptools version 28.3.0
site_packages_path = join(
self.ctx.dist_dir, 'hostpython3',
'lib', 'python3.8', 'site-packages')
os.chdir(site_packages_path)
with open('setuptools.pth', 'r') as f:
setuptools_egg_path = f.read().strip('./').strip('\n')
print("setuptools_egg_path=", setuptools_egg_path)
unzip = sh.Command('unzip')
shprint(unzip, "-o", setuptools_egg_path)
os.remove(setuptools_egg_path)
os.remove('setuptools.pth')
os.remove('easy-install.pth')
shutil.rmtree('EGG-INFO')
with cd(build_dir):
shprint(hostpython, "setup.py", "install")


recipe = HostSetuptools3()
25 changes: 17 additions & 8 deletions recipes/hostpython3/__init__.py
@@ -1,4 +1,4 @@
from toolchain import Recipe, shprint
from toolchain import Recipe, cd, shprint
from os.path import join
import os
import sh
Expand All @@ -14,6 +14,7 @@ class Hostpython3Recipe(Recipe):
depends = ["hostlibffi", "hostopenssl"]
optional_depends = []
archs = ["x86_64"]
build_subdir = 'native-build'

def init_with_ctx(self, ctx):
super(Hostpython3Recipe, self).init_with_ctx(ctx)
Expand All @@ -24,9 +25,13 @@ def init_with_ctx(self, ctx):
logger.info("Global: hostpython located at {}".format(self.ctx.hostpython))
logger.info("Global: hostpgen located at {}".format(self.ctx.hostpgen))

def get_build_subdir(self, arch):
return join(self.get_build_dir(arch), self.build_subdir)

def prebuild_arch(self, arch):
if self.has_marker("patched"):
return
self.apply_patch("pyconfig_detection.patch")
self.copy_file("ModulesSetup", "Modules/Setup.local")
self.set_marker("patched")

Expand Down Expand Up @@ -55,20 +60,24 @@ def get_build_env(self):
def build_x86_64(self):
build_env = self.get_build_env()
configure = sh.Command(join(self.build_dir, "configure"))
shprint(configure,
"--prefix={}".format(join(self.ctx.dist_dir, "hostpython3")),
"--with-openssl={}".format(join(self.ctx.dist_dir, 'hostopenssl')),
_env=build_env)
shprint(sh.make, "-C", self.build_dir, self.ctx.concurrent_make,
arch = self.filtered_archs[0]
build_subdir = self.get_build_subdir(arch.arch)
os.makedirs(build_subdir, exist_ok=True)
with cd(build_subdir):
shprint(configure,
"--prefix={}".format(join(self.ctx.dist_dir, "hostpython3")),
"--with-openssl={}".format(join(self.ctx.dist_dir, 'hostopenssl')),
_env=build_env)
shprint(sh.make, "-C", build_subdir, self.ctx.concurrent_make,
_env=build_env)

def install(self):
arch = list(self.filtered_archs)[0]
build_env = self.get_build_env()
build_dir = self.get_build_dir(arch.arch)
build_subdir = self.get_build_subdir(arch.arch)
build_env["PATH"] = os.environ["PATH"]
shprint(sh.make, self.ctx.concurrent_make,
"-C", build_dir,
"-C", build_subdir,
"install",
_env=build_env)
shutil.copy(
Expand Down
25 changes: 25 additions & 0 deletions recipes/hostpython3/pyconfig_detection.patch
@@ -0,0 +1,25 @@
diff -Nru Python-3.8.2/Lib/site.py Python-3.8.2-new/Lib/site.py
--- Python-3.8.2/Lib/site.py 2020-02-24 22:36:25.000000000 +0100
+++ Python-3.8.2-new/Lib/site.py 2020-05-01 17:10:43.000000000 +0200
@@ -458,9 +458,8 @@

env = os.environ
if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env:
- executable = sys._base_executable = os.environ['__PYVENV_LAUNCHER__']
- else:
- executable = sys.executable
+ print("Ignoring __PYVENV_LAUNCHER__")
+ executable = sys.executable
exe_dir, _ = os.path.split(os.path.abspath(executable))
site_prefix = os.path.dirname(exe_dir)
sys._home = None
@@ -487,7 +486,8 @@
if key == 'include-system-site-packages':
system_site = value.lower()
elif key == 'home':
- sys._home = value
+ # this is breaking pyconfig.h path detection with venv
+ print('Ignoring "sys._home = value" override')

sys.prefix = sys.exec_prefix = site_prefix

2 changes: 1 addition & 1 deletion recipes/kivy/__init__.py
Expand Up @@ -17,7 +17,7 @@ class KivyRecipe(CythonRecipe):
url = "https://github.com/kivy/kivy/archive/{version}.zip"
library = "libkivy.a"
depends = ["sdl2", "sdl2_image", "sdl2_mixer", "sdl2_ttf", "ios",
"pyobjus", "python"]
"pyobjus", "python", "host_setuptools3"]
pbx_frameworks = ["OpenGLES", "Accelerate", "CoreMedia", "CoreVideo"]
pre_build_ext = True

Expand Down
1 change: 1 addition & 0 deletions recipes/python3/__init__.py
Expand Up @@ -32,6 +32,7 @@ def prebuild_arch(self, arch):
self.apply_patch("posixmodule.patch")
self.apply_patch("dynload_shlib.patch")
self.apply_patch("disable_explicit_blake2.patch")
self.apply_patch("pyconfig_detection.patch")
self.copy_file("ModulesSetup", "Modules/Setup.local")
self.append_file("ModulesSetup.mobile", "Modules/Setup.local")
self.set_marker("patched")
Expand Down
25 changes: 25 additions & 0 deletions recipes/python3/pyconfig_detection.patch
@@ -0,0 +1,25 @@
diff -Nru Python-3.8.2/Lib/site.py Python-3.8.2-new/Lib/site.py
--- Python-3.8.2/Lib/site.py 2020-02-24 22:36:25.000000000 +0100
+++ Python-3.8.2-new/Lib/site.py 2020-05-01 17:10:43.000000000 +0200
@@ -458,9 +458,8 @@

env = os.environ
if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env:
- executable = sys._base_executable = os.environ['__PYVENV_LAUNCHER__']
- else:
- executable = sys.executable
+ print("Ignoring __PYVENV_LAUNCHER__")
+ executable = sys.executable
exe_dir, _ = os.path.split(os.path.abspath(executable))
site_prefix = os.path.dirname(exe_dir)
sys._home = None
@@ -487,7 +486,8 @@
if key == 'include-system-site-packages':
system_site = value.lower()
elif key == 'home':
- sys._home = value
+ # this is breaking pyconfig.h path detection with venv
+ print('Ignoring "sys._home = value" override')

sys.prefix = sys.exec_prefix = site_prefix

13 changes: 12 additions & 1 deletion toolchain.py
Expand Up @@ -8,7 +8,7 @@

import sys
from sys import stdout
from os.path import join, dirname, realpath, exists, isdir, basename
from os.path import join, dirname, realpath, exists, isdir, basename, expanduser
from os import listdir, unlink, makedirs, environ, chdir, getcwd, walk
import zipfile
import tarfile
Expand All @@ -19,6 +19,7 @@
import fnmatch
import tempfile
import time
from contextlib import contextmanager
from datetime import datetime
from pprint import pformat
import logging
Expand Down Expand Up @@ -58,6 +59,16 @@
IS_PY2 = sys.version_info[0] == 2


@contextmanager
def cd(newdir):
prevdir = getcwd()
chdir(expanduser(newdir))
try:
yield
finally:
chdir(prevdir)


def shprint(command, *args, **kwargs):
kwargs["_iter"] = True
kwargs["_out_bufsize"] = 1
Expand Down

0 comments on commit ab2b3ce

Please sign in to comment.