Skip to content

Commit

Permalink
fix(meson): fix issues with meson builds
Browse files Browse the repository at this point in the history
* add test that tests meson builds for all targets
* add test that tests makefile build except for targets with c++ and libmf6
  • Loading branch information
jdhughes-usgs committed Aug 6, 2023
1 parent 3bf53a2 commit 91ec074
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 11 deletions.
105 changes: 105 additions & 0 deletions autotest/test_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import os
import sys
import time

import pytest
from flaky import flaky

import pymake
from autotest.conftest import working_directory

RERUNS = 3

targets = pymake.usgs_program_data.get_keys(current=True)
targets_make = [
t
for t in targets
if t not in ("libmf6", "gridgen", "mf2000", "swtv4", "mflgr")
]


def build_with_makefile(target, path, fc):
success = True
with working_directory(path):
if os.path.isfile("makefile"):
# wait to delete on windows
if sys.platform.lower() == "win32":
time.sleep(6)

# clean prior to make
print(f"clean {target} with makefile")
os.system("make clean")

# build MODFLOW-NWT with makefile
print(f"build {target} with makefile")
return_code = os.system("make")

# test if running on Windows with ifort, if True the makefile
# should fail
errmsg = f"{target} created by makefile does not exist."
if sys.platform.lower() == "win32" and fc == "ifort":
if return_code != 0:
success = True
else:
success = False
# verify that MODFLOW-NWT was made
else:
success = os.path.isfile(target)
else:
errmsg = "makefile does not exist"

return success, errmsg


@pytest.mark.base
@flaky(max_runs=RERUNS)
@pytest.mark.parametrize("target", targets)
def test_build(function_tmpdir, target: str) -> None:
with working_directory(function_tmpdir):
assert (
pymake.build_apps(
target,
verbose=True,
clean=False,
)
== 0
), f"could not compile {target}"


@pytest.mark.base
@flaky(max_runs=RERUNS)
#@pytest.mark.skipif(sys.platform == "win32", reason="do not run on Windows")
@pytest.mark.parametrize("target", targets)
def test_meson_build(function_tmpdir, target: str) -> None:
with working_directory(function_tmpdir):
assert (
pymake.build_apps(
target,
verbose=True,
clean=False,
meson=True,
)
== 0
), f"could not compile {target}"


@pytest.mark.base
@flaky(max_runs=RERUNS)
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on Windows")
@pytest.mark.parametrize("target", targets_make)
def test_makefile_build(function_tmpdir, target: str) -> None:
pm = pymake.Pymake(verbose=True)
pm.target = target
pm.makefile = True
pm.makefiledir = "."
pm.inplace = True
pm.dryrun = True
pm.makeclean = False

with working_directory(function_tmpdir):
pm.download_target(target)
assert pm.download, f"could not download {target} distribution"
assert pm.build() == 0, f"could not compile {target}"

success, errmsg = build_with_makefile(target, function_tmpdir, pm.fc)
assert success, errmsg
23 changes: 12 additions & 11 deletions pymake/utils/_meson_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
_get_osname,
_get_prepend,
)
from ._file_utils import _get_extra_exclude_files, _get_extrafiles_common_path
from ._Popen_wrapper import (
_process_Popen_command,
_process_Popen_communicate,
_process_Popen_initialize,
)
from ._file_utils import _get_extrafiles_common_path


@contextmanager
Expand Down Expand Up @@ -197,6 +192,7 @@ def meson_setup(
os.path.abspath(appdir), os.path.abspath(mesondir)
)
command_list.append(f"--libdir={libdir}")
command_list.append(f"--bindir={libdir}")

if os.path.isdir(build_dir):
command_list.append("--wipe")
Expand All @@ -208,7 +204,7 @@ def meson_setup(

# evaluate return code
if returncode != 0:
print(f"meson install failed on '{' '.join(command)}'")
print(f"meson setup failed on '{command}'")

return returncode

Expand Down Expand Up @@ -243,7 +239,7 @@ def meson_install(

# evaluate return code
if returncode != 0:
print(f"meson setup failed on '{' '.join(command)}'")
print(f"meson install failed on '{command}'")

return returncode

Expand Down Expand Up @@ -529,9 +525,14 @@ def _create_main_meson_build(
line = f"project(\n\t'{target}',\n"
for language in languages:
line += f"\t'{language}',\n"
line += "\tmeson_version: '>= 0.59.0',\n"
line += "\tmeson_version: '>= 1.1.0',\n"
line += "\tdefault_options: [\n\t\t'b_vscrt=static_from_buildtype',\n"
line += f"\t\t'optimization={optlevel_int}'\n"
line += f"\t\t'optimization={optlevel_int}',\n"
line += "\t\t'debug="
if debug:
line += "true',\n"
else:
line += "false',\n"
line += "\t])\n\n"
f.write(line)

Expand Down Expand Up @@ -666,4 +667,4 @@ def _create_source_meson_build(source_path_dict, srcfiles):
for temp in pop_list:
srcfiles_copy.remove(temp)

return
return

0 comments on commit 91ec074

Please sign in to comment.