diff --git a/snapcraft/plugins/v1/autotools.py b/snapcraft/plugins/v1/autotools.py index e12f9da8df..cd7f322682 100644 --- a/snapcraft/plugins/v1/autotools.py +++ b/snapcraft/plugins/v1/autotools.py @@ -40,7 +40,7 @@ """ import os -import stat +from pathlib import Path from snapcraft.plugins.v1 import make @@ -88,32 +88,25 @@ def enable_cross_compilation(self): pass def build(self): - if not os.path.exists(os.path.join(self.builddir, "configure")): - generated = False - scripts = ["autogen.sh", "bootstrap"] - for script in scripts: - path = os.path.join(self.builddir, script) - if not os.path.exists(path) or os.path.isdir(path): - continue - # Make sure it's executable - if not os.access(path, os.X_OK): - os.chmod( - path, - stat.S_IRUSR - | stat.S_IWUSR - | stat.S_IXUSR - | stat.S_IRGRP - | stat.S_IWGRP - | stat.S_IXGRP - | stat.S_IROTH - | stat.S_IWOTH - | stat.S_IXOTH, - ) - self.run(["env", "NOCONFIGURE=1", "./{}".format(script)]) - generated = True - break - if not generated: + configure_script = Path(self.builddir) / "configure" + if not configure_script.exists(): + potential_scripts = [ + Path(self.builddir) / s for s in ["autogen.sh", "bootstrap"] + ] + existing_scripts = [ + s for s in potential_scripts if s.exists() and s.is_file() + ] + + # If there are not alternative configure scripts, run autoreconf. + if not existing_scripts: self.run(["autoreconf", "-i"]) + else: + # Pick the first one. + script = existing_scripts[0] + # Make sure it's executable. + if not os.access(script, os.X_OK): + script.chmod(0o755) + self.run(["env", "NOCONFIGURE=1", script]) configure_command = ["./configure"] diff --git a/tests/unit/plugins/v1/test_autotools.py b/tests/unit/plugins/v1/test_autotools.py index 2615e13d78..1300454244 100644 --- a/tests/unit/plugins/v1/test_autotools.py +++ b/tests/unit/plugins/v1/test_autotools.py @@ -16,8 +16,8 @@ # along with this program. If not, see . import os -import pathlib import stat +from pathlib import Path from unittest import mock import pytest @@ -258,12 +258,13 @@ def test_build_autogen_with_destdir(self, run_mock): self.assertThat(run_mock.call_count, Equals(4)) run_mock.assert_has_calls( [ - mock.call(["env", "NOCONFIGURE=1", "./autogen.sh"]), + mock.call( + ["env", "NOCONFIGURE=1", Path(plugin.builddir) / "autogen.sh"] + ), mock.call(["./configure", "--prefix="]), mock.call(["make", "-j2"], env=None), mock.call( - ["make", "install", "DESTDIR={}".format(plugin.installdir)], - env=None, + ["make", "install", f"DESTDIR={plugin.installdir}"], env=None, ), ] ) @@ -275,7 +276,9 @@ def test_build_bootstrap_with_destdir(self, run_mock): self.assertThat(run_mock.call_count, Equals(4)) run_mock.assert_has_calls( [ - mock.call(["env", "NOCONFIGURE=1", "./bootstrap"]), + mock.call( + ["env", "NOCONFIGURE=1", Path(plugin.builddir) / "bootstrap"] + ), mock.call(["./configure", "--prefix="]), mock.call(["make", "-j2"], env=None), mock.call( @@ -292,7 +295,9 @@ def test_build_bootstrap_and_autogen_with_destdir(self, run_mock): self.assertThat(run_mock.call_count, Equals(4)) run_mock.assert_has_calls( [ - mock.call(["env", "NOCONFIGURE=1", "./autogen.sh"]), + mock.call( + ["env", "NOCONFIGURE=1", Path(plugin.builddir) / "autogen.sh"] + ), mock.call(["./configure", "--prefix="]), mock.call(["make", "-j2"], env=None), mock.call( @@ -310,7 +315,9 @@ def test_build_autogen_with_prefix(self, run_mock): self.assertThat(run_mock.call_count, Equals(4)) run_mock.assert_has_calls( [ - mock.call(["env", "NOCONFIGURE=1", "./autogen.sh"]), + mock.call( + ["env", "NOCONFIGURE=1", Path(plugin.builddir) / "autogen.sh"] + ), mock.call(["./configure", "--prefix={}".format(plugin.installdir)]), mock.call(["make", "-j2"], env=None), mock.call(["make", "install"], env=None), @@ -443,7 +450,7 @@ def test_cross_compile(mock_run, monkeypatch, project, options, deb_arch, triple plugin = autotools.AutotoolsPlugin("test-part", options, project) plugin.enable_cross_compilation() - configure_file = pathlib.Path(plugin.builddir) / "configure" + configure_file = Path(plugin.builddir) / "configure" configure_file.parent.mkdir(parents=True) configure_file.touch()