Detect the command instead of hardcoding them.

This commit is contained in:
Matthieu Gautier 2018-06-22 18:14:35 +02:00
parent c99a9bd91f
commit 985643089a
2 changed files with 48 additions and 41 deletions

View File

@ -21,16 +21,20 @@ class PlatformNeutralEnv:
self.log_dir):
os.makedirs(d, exist_ok=True)
self.detect_platform()
self.ninja_command = self._detect_ninja()
if not self.ninja_command:
sys.exit("ERROR: ninja command not found.")
self.meson_command = self._detect_meson()
if not self.meson_command:
sys.exit("ERROR: meson command not found.")
self.qmake_command = self._detect_qmake()
if not self.qmake_command:
print("WARNING: qmake command not found.", file=sys.stderr)
self.ninja_command = self._detect_command(
'ninja',
default=[['ninja'], ['ninja-build']])
self.meson_command = self._detect_command(
'meson',
default= [['meson.py'], ['meson']])
self.mesontest_command = [*self.meson_command, "test"]
self.patch_command = self._detect_command('patch')
self.git_command = self._detect_command('git')
self.svn_command = self._detect_command('svn')
self.make_command = self._detect_command('make')
self.cmake_command = self._detect_command('cmake')
self.qmake_command = self._detect_command('qmake', required=False)
def detect_platform(self):
_platform = platform.system()
@ -49,28 +53,29 @@ class PlatformNeutralEnv:
def download(self, what, where=None):
where = where or self.archive_dir
download_remote(what, where)
def _detect_binary(self, *bin_variants):
for n in bin_variants:
def _detect_command(self, name, default=None, options=['--version'], required=True):
if default is None:
default = [[name]]
env_key = 'KBUILD_{}_COMMAND'.format(name.upper())
if env_key in os.environ:
default = [os.environ[env_key].split()] + default
for command in default:
try:
retcode = subprocess.check_call([n, '--version'],
retcode = subprocess.check_call(command + options,
stdout=subprocess.DEVNULL)
except (FileNotFoundError, PermissionError):
except (FileNotFoundError, PermissionError, OSError):
# Doesn't exist in PATH or isn't executable
continue
if retcode == 0:
return [n]
def _detect_ninja(self):
return self._detect_binary('ninja', 'ninja-build')
def _detect_meson(self):
return self._detect_binary('meson.py', 'meson')
def _detect_qmake(self):
return self._detect_binary('qmake-qt5', 'qmake')
return command
else:
if required:
sys.exit("ERROR: {} command not found".format(name))
else:
print("WARNING: {} command not found".format(name))
return ["{}_NOT_FOUND".format(name.upper())]
class BuildEnv:
def __init__(self, platformInfo):

View File

@ -71,7 +71,7 @@ class Source:
context.try_skip(self.source_path)
for p in self.patches:
patch_file_path = pj(SCRIPT_DIR, 'patches', p)
patch_command = ["patch", "-p1", "-i", patch_file_path]
patch_command = [*neutralEnv('patch_command'), "-p1", "-i", patch_file_path]
run_command(patch_command, self.source_path, context)
def command(self, name, function, *args):
@ -170,19 +170,19 @@ class GitClone(Source):
def _git_init(self, context):
if option('fast_clone') and self.force_full_clone == False:
command = ["git", "clone" , "--depth=1", "--branch", self.git_ref, self.git_remote, self.source_dir]
command = [*neutralEnv('git_command'), "clone" , "--depth=1", "--branch", self.git_ref, self.git_remote, self.source_dir]
run_command(command, neutralEnv('source_dir'), context)
else:
command = ["git", "clone", self.git_remote, self.source_dir]
command = [*neutralEnv('git_command'), "clone", self.git_remote, self.source_dir]
run_command(command, neutralEnv('source_dir'), context)
command = ["git", "checkout", self.git_ref]
command = [*neutralEnv('git_command'), "checkout", self.git_ref]
run_command(command, self.git_path, context)
def _git_update(self, context):
command = ["git", "fetch", "origin", self.git_ref]
command = [*neutralEnv('git_command'), "fetch", "origin", self.git_ref]
run_command(command, self.git_path, context)
try:
command = ["git", "merge", "--ff-only", f"origin/{self.git_ref}"]
command = [*neutralEnv('git_command'), "merge", "--ff-only", f"origin/{self.git_ref}"]
run_command(command, self.git_path, context)
except subprocess.CalledProcessError:
raise WarningMessage("Cannot update, please check log for information")
@ -209,7 +209,7 @@ class SvnClone(Source):
if os.path.exists(self.svn_path):
raise SkipCommand()
command = [
"svn", "export",
*neutralEnv('svn_command'), "export",
self.svn_remote, self.svn_dir
]
run_command(command, neutralEnv('source_dir'), context)
@ -415,7 +415,8 @@ class MakeBuilder(Builder):
context.try_skip(self.build_path)
command = [
*self.buildEnv.make_wrapper,
"make", "-j4",
*neutralEnv('make_command'),
"-j4",
*self.make_targets,
*self.make_options
]
@ -426,7 +427,7 @@ class MakeBuilder(Builder):
context.try_skip(self.build_path)
command = [
*self.buildEnv.make_wrapper,
"make",
*neutralEnv('make_command'),
*self.make_install_targets,
*self.make_options
]
@ -437,7 +438,8 @@ class MakeBuilder(Builder):
context.try_skip(self.build_path)
command = [
*self.buildEnv.make_wrapper,
"make", "dist"
*neutralEnv('make_command'),
"dist"
]
env = self.get_env(cross_comp_flags=True, cross_compilers=True, cross_path=True)
run_command(command, self.build_path, context, env=env)
@ -452,7 +454,7 @@ class CMakeBuilder(MakeBuilder):
if not self.target.force_native_build and self.buildEnv.cmake_crossfile:
cross_options += [f"-DCMAKE_TOOLCHAIN_FILE={self.buildEnv.cmake_crossfile}"]
command = [
"cmake",
*neutralEnv('cmake_command'),
*self.configure_options,
"-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON",
f"-DCMAKE_INSTALL_PREFIX={self.buildEnv.install_dir}",
@ -483,10 +485,10 @@ class QMakeBuilder(MakeBuilder):
def _configure(self, context):
context.try_skip(self.build_path)
command = [
*neutralEnv('qmake_command'),
*self.configure_options,
*self.env_options,
self.source_path,
"qmake",
*self.configure_options,
*self.env_options,
self.source_path
]
env = self.get_env(cross_comp_flags=True, cross_compilers=False, cross_path=True)
self.set_configure_env(env)
@ -494,7 +496,7 @@ class QMakeBuilder(MakeBuilder):
def _make_dist(self, context):
command = [
"git", "archive",
*neutralEnv('git_command'), "archive",
"-o", f"{self.build_path}/{self.target_full_name()}.tar.gz",
f"--prefix={self.target_full_name()}/",
"HEAD"