Skip to content

Commit

Permalink
Detect the command instead of hardcoding them.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Nov 8, 2023
1 parent 3b93d80 commit 10d3876
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
57 changes: 31 additions & 26 deletions kiwixbuild/buildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ def __init__(self):
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()
Expand All @@ -49,28 +53,29 @@ def detect_platform(self):
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):
Expand Down
34 changes: 18 additions & 16 deletions kiwixbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _patch(self, context):
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):
Expand Down Expand Up @@ -170,19 +170,19 @@ def git_ref(self):

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")
Expand All @@ -209,7 +209,7 @@ def _svn_export(self, context):
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)
Expand Down Expand Up @@ -415,7 +415,8 @@ def _compile(self, context):
context.try_skip(self.build_path)
command = [
*self.buildEnv.make_wrapper,
"make", "-j4",
*neutralEnv('make_command'),
"-j4",
*self.make_targets,
*self.make_options
]
Expand All @@ -426,7 +427,7 @@ def _install(self, context):
context.try_skip(self.build_path)
command = [
*self.buildEnv.make_wrapper,
"make",
*neutralEnv('make_command'),
*self.make_install_targets,
*self.make_options
]
Expand All @@ -437,7 +438,8 @@ def _make_dist(self, context):
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)
Expand All @@ -452,7 +454,7 @@ def _configure(self, context):
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}",
Expand Down Expand Up @@ -483,18 +485,18 @@ def env_options(self):
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)
run_command(command, self.build_path, context, env=env)

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"
Expand Down

0 comments on commit 10d3876

Please sign in to comment.