Skip to content

Commit eb343a5

Browse files
committed
Some progress on new prerequisites management
1 parent 4baec32 commit eb343a5

File tree

6 files changed

+431
-21
lines changed

6 files changed

+431
-21
lines changed

.github/workflows/push.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ jobs:
122122
run: |
123123
source ci/osx_ci.sh
124124
arm64_set_path_and_python_version 3.9.7
125-
brew install autoconf automake libtool openssl pkg-config
126125
make --file ci/makefiles/osx.mk
127126
- name: Build multi-arch apk Python 3 (armeabi-v7a, arm64-v8a, x86_64, x86)
128127
run: |
@@ -207,7 +206,6 @@ jobs:
207206
run: |
208207
source ci/osx_ci.sh
209208
arm64_set_path_and_python_version 3.9.7
210-
brew install autoconf automake libtool openssl pkg-config
211209
make --file ci/makefiles/osx.mk
212210
- name: Build multi-arch aab Python 3 (armeabi-v7a, arm64-v8a, x86_64, x86)
213211
run: |
@@ -293,7 +291,6 @@ jobs:
293291
run: |
294292
source ci/osx_ci.sh
295293
arm64_set_path_and_python_version 3.9.7
296-
brew install autoconf automake libtool openssl pkg-config
297294
make --file ci/makefiles/osx.mk
298295
- name: Rebuild updated recipes
299296
run: |

ci/makefiles/osx.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# installs java 1.8, android's SDK/NDK, cython and p4a
1+
# installs Android's SDK/NDK, cython
22

33
# The following variable/s can be override when running the file
44
ANDROID_HOME ?= $(HOME)/.android
@@ -10,4 +10,4 @@ upgrade_cython:
1010

1111
install_android_ndk_sdk:
1212
mkdir -p $(ANDROID_HOME)
13-
make -f ci/makefiles/android.mk JAVA_HOME=`/usr/libexec/java_home -v 13`
13+
make -f ci/makefiles/android.mk

pythonforandroid/prerequisites.py

Lines changed: 156 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import platform
55
import os
66
import subprocess
7+
import shutil
78
from pythonforandroid.logger import info, warning, error
89

910

1011
class Prerequisite(object):
1112
name = "Default"
12-
mandatory = True
13-
darwin_installer_is_supported = False
14-
linux_installer_is_supported = False
13+
mandatory = dict(linux=False, darwin=False)
14+
installer_is_supported = dict(linux=False, darwin=False)
1515

1616
def is_valid(self):
1717
if self.checker():
1818
info(f"Prerequisite {self.name} is met")
1919
return (True, "")
20-
elif not self.mandatory:
20+
elif not self.mandatory[sys.platform]:
2121
warning(
2222
f"Prerequisite {self.name} is not met, but is marked as non-mandatory"
2323
)
@@ -73,10 +73,7 @@ def show_helper(self):
7373
raise Exception("Unsupported platform")
7474

7575
def install_is_supported(self):
76-
if sys.platform == "darwin":
77-
return self.darwin_installer_is_supported
78-
elif sys.platform == "linux":
79-
return self.linux_installer_is_supported
76+
return self.installer_is_supported[sys.platform]
8077

8178
def linux_checker(self):
8279
raise Exception(f"Unsupported prerequisite check on linux for {self.name}")
@@ -96,11 +93,45 @@ def darwin_helper(self):
9693
def linux_helper(self):
9794
info(f"No helper available for prerequisite: {self.name} on linux")
9895

96+
def _darwin_get_brew_formula_location_prefix(self, formula, installed=False):
97+
opts = ["--installed"] if installed else []
98+
p = subprocess.Popen(
99+
["brew", "--prefix", formula, *opts],
100+
stdout=subprocess.PIPE,
101+
stderr=subprocess.PIPE,
102+
)
103+
_stdout_res, _stderr_res = p.communicate()
104+
105+
if p.returncode != 0:
106+
error(_stderr_res.decode("utf-8").strip())
107+
return None
108+
else:
109+
return _stdout_res.decode("utf-8").strip()
110+
111+
112+
class HomebrewPrerequisite(Prerequisite):
113+
name = "homebrew"
114+
mandatory = dict(linux=False, darwin=True)
115+
installer_is_supported = dict(linux=False, darwin=False)
116+
117+
def darwin_checker(self):
118+
if shutil.which("brew"):
119+
return True
120+
else:
121+
return False
122+
123+
def darwin_helper(self):
124+
info(
125+
"Installer for homebrew is not yet supported on macOS,"
126+
"the nice news is that the installation process is easy!"
127+
"See: https://brew.sh for further instructions."
128+
)
129+
99130

100131
class JDKPrerequisite(Prerequisite):
101132
name = "JDK"
102-
mandatory = True
103-
darwin_installer_is_supported = True
133+
mandatory = dict(linux=False, darwin=True)
134+
installer_is_supported = dict(linux=False, darwin=True)
104135
min_supported_version = 11
105136

106137
def darwin_checker(self):
@@ -216,13 +247,123 @@ def darwin_installer(self):
216247
os.environ["JAVA_HOME"] = jdk_path
217248

218249

219-
def check_and_install_default_prerequisites():
220-
DEFAULT_PREREQUISITES = dict(darwin=[JDKPrerequisite()], linux=[], all_platforms=[])
250+
class OpenSSLPrerequisite(Prerequisite):
251+
252+
mandatory = dict(linux=False, darwin=True)
253+
installer_is_supported = dict(linux=False, darwin=True)
254+
255+
def darwin_checker(self):
256+
if self._darwin_get_brew_formula_location_prefix("[email protected]", installed=True):
257+
return True
258+
else:
259+
return False
260+
261+
def darwin_installer(self):
262+
info("Installing OpenSSL ...")
263+
subprocess.check_output(["brew", "install", "[email protected]"])
264+
265+
266+
class AutoconfPrerequisite(Prerequisite):
267+
name = "autoconf"
268+
mandatory = dict(linux=False, darwin=True)
269+
installer_is_supported = dict(linux=False, darwin=True)
270+
271+
def darwin_checker(self):
272+
if self._darwin_get_brew_formula_location_prefix("autoconf", installed=True):
273+
return True
274+
else:
275+
return False
276+
277+
def darwin_installer(self):
278+
info("Installing Autoconf ...")
279+
subprocess.check_output(["brew", "install", "autoconf"])
280+
221281

222-
required_prerequisites = (
223-
DEFAULT_PREREQUISITES["all_platforms"] + DEFAULT_PREREQUISITES[sys.platform]
282+
class AutomakePrerequisite(Prerequisite):
283+
name = "automake"
284+
mandatory = dict(linux=False, darwin=True)
285+
installer_is_supported = dict(linux=False, darwin=True)
286+
287+
def darwin_checker(self):
288+
if self._darwin_get_brew_formula_location_prefix("automake", installed=True):
289+
return True
290+
else:
291+
return False
292+
293+
def darwin_installer(self):
294+
info("Installing Automake ...")
295+
subprocess.check_output(["brew", "install", "automake"])
296+
297+
298+
class LibtoolPrerequisite(Prerequisite):
299+
name = "libtool"
300+
mandatory = dict(linux=False, darwin=True)
301+
installer_is_supported = dict(linux=False, darwin=True)
302+
303+
def darwin_checker(self):
304+
if self._darwin_get_brew_formula_location_prefix("libtool", installed=True):
305+
return True
306+
else:
307+
return False
308+
309+
def darwin_installer(self):
310+
info("Installing Libtool ...")
311+
subprocess.check_output(["brew", "install", "libtool"])
312+
313+
314+
class PkgConfigPrerequisite(Prerequisite):
315+
name = "pkg-config"
316+
mandatory = dict(linux=False, darwin=True)
317+
installer_is_supported = dict(linux=False, darwin=True)
318+
319+
def darwin_checker(self):
320+
if self._darwin_get_brew_formula_location_prefix("pkg-config", installed=True):
321+
return True
322+
else:
323+
return False
324+
325+
def darwin_installer(self):
326+
info("Installing Pkg-Config ...")
327+
subprocess.check_output(["brew", "install", "pkg-config"])
328+
329+
330+
class CmakePrerequisite(Prerequisite):
331+
name = "cmake"
332+
mandatory = dict(linux=False, darwin=True)
333+
installer_is_supported = dict(linux=False, darwin=True)
334+
335+
def darwin_checker(self):
336+
if self._darwin_get_brew_formula_location_prefix("cmake", installed=True):
337+
return True
338+
else:
339+
return False
340+
341+
def darwin_installer(self):
342+
info("Installing cmake ...")
343+
subprocess.check_output(["brew", "install", "cmake"])
344+
345+
346+
def get_required_prerequisites(platform="linux"):
347+
DEFAULT_PREREQUISITES = dict(
348+
darwin=[
349+
HomebrewPrerequisite(),
350+
AutoconfPrerequisite(),
351+
AutomakePrerequisite(),
352+
LibtoolPrerequisite(),
353+
PkgConfigPrerequisite(),
354+
CmakePrerequisite(),
355+
OpenSSLPrerequisite(),
356+
JDKPrerequisite(),
357+
],
358+
linux=[],
359+
all_platforms=[],
224360
)
225361

362+
return DEFAULT_PREREQUISITES["all_platforms"] + DEFAULT_PREREQUISITES[platform]
363+
364+
365+
def check_and_install_default_prerequisites():
366+
226367
prerequisites_not_met = []
227368

228369
warning(
@@ -232,7 +373,7 @@ def check_and_install_default_prerequisites():
232373

233374
# Phase 1: Check if all prerequisites are met and add the ones
234375
# which are not to `prerequisites_not_met`
235-
for prerequisite in required_prerequisites:
376+
for prerequisite in get_required_prerequisites(sys.platform):
236377
if not prerequisite.is_valid():
237378
prerequisites_not_met.append(prerequisite)
238379

pythonforandroid/toolchain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def check_python_dependencies():
6767
exit(1)
6868

6969

70-
check_and_install_default_prerequisites()
70+
if not environ.get('SKIP_PREREQUISITES_CHECK', '0') == '1':
71+
check_and_install_default_prerequisites()
7172
check_python_dependencies()
7273

7374

0 commit comments

Comments
 (0)