diff --git a/tools/build/helpers.py b/tools/build/helpers.py new file mode 100644 index 000000000..d73363f57 --- /dev/null +++ b/tools/build/helpers.py @@ -0,0 +1,36 @@ +import os + + +def is_executable(path) -> bool: + return os.path.isfile(path) and os.access(path, os.X_OK) + + +def get_bin(binary): + """Checks whether the given binary is present and returns the path. + + Args: + binary: binary name (without .exe, etc). + + Returns: + Full path to the binary or None if not found. + """ + for path in os.environ['PATH'].split(os.pathsep): + path = path.strip('"') + exe_file = os.path.join(path, binary) + if is_executable(exe_file): + return exe_file + if is_executable(exe_file + '.exe'): + return exe_file + '.exe' + return None + + +def has_bin(binary): + """Checks whether the given binary is present in the PATH. + + Args: + binary: binary name (without .exe, etc). + + Returns: + True if the binary exists. + """ + return get_bin(binary) is not None diff --git a/tools/build/premake b/tools/build/premake index b9f423dee..2b20c732f 100644 --- a/tools/build/premake +++ b/tools/build/premake @@ -12,12 +12,14 @@ import json import os import shutil import subprocess +import pathlib import sys import re +from helpers import is_executable self_path = os.path.dirname(os.path.abspath(__file__)) -root_path = os.path.join(self_path, '..', '..') +root_path = pathlib.Path(self_path).parent.parent.absolute() premake_submodule_path = os.path.join(root_path, 'third_party', 'premake-core') premake_path = premake_submodule_path @@ -54,15 +56,15 @@ setup_premake_path_override() def main(): # First try the freshly-built premake. premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5') - if not has_bin(premake5_bin): + if not is_executable(premake5_bin): # No fresh build, so fallback to checked in copy (which we may not have). premake5_bin = os.path.join(self_path, 'bin', 'premake5') - if not has_bin(premake5_bin): + if not is_executable(premake5_bin): # Still no valid binary, so build it. print('premake5 executable not found, attempting build...') build_premake() premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5') - if not has_bin(premake5_bin): + if not is_executable(premake5_bin): # Nope, boned. print('ERROR: cannot build premake5 executable.') sys.exit(1) @@ -152,22 +154,6 @@ def clone_premake_to_internal_storage(): ]) -def has_bin(bin): - """Checks whether the given binary is present. - """ - for path in os.environ["PATH"].split(os.pathsep): - if sys.platform == 'win32': - exe_file = os.path.join(path, bin + '.exe') - if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): - return True - else: - path = path.strip('"') - exe_file = os.path.join(path, bin) - if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): - return True - return None - - def shell_call(command, throw_on_error=True, stdout_path=None, stderr_path=None, shell=False): """Executes a shell command. diff --git a/xenia-build b/xenia-build index 2f02b4d5e..3f4bc6d6f 100755 --- a/xenia-build +++ b/xenia-build @@ -18,6 +18,7 @@ import shutil import subprocess import sys import stat +from tools.build.helpers import has_bin, get_bin __author__ = 'ben.vanik@gmail.com (Ben Vanik)' @@ -194,41 +195,6 @@ def print_box(msg): .format('', msg, len(msg) + 2)) -def has_bin(binary): - """Checks whether the given binary is present. - - Args: - binary: binary name (without .exe, etc). - - Returns: - True if the binary exists. - """ - bin_path = get_bin(binary) - if not bin_path: - return False - return True - - -def get_bin(binary): - """Checks whether the given binary is present and returns the path. - - Args: - binary: binary name (without .exe, etc). - - Returns: - Full path to the binary or None if not found. - """ - for path in os.environ['PATH'].split(os.pathsep): - path = path.strip('"') - exe_file = os.path.join(path, binary) - if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): - return exe_file - exe_file += '.exe' - if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): - return exe_file - return None - - def shell_call(command, throw_on_error=True, stdout_path=None, stderr_path=None, shell=False): """Executes a shell command.