Skip to content

Commit

Permalink
fix(meson): fix issues with meson builds (#137)
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
* use `set_dir()` in modflow_devtools.misc
* remove `working_directory()` from conftest.py
* fix for meson mixed-language projects
* add mf6dev, zbud6dev, and libmf6dev
* remove default double precision builds for mfusg, mf2005, mflgr, and mfnwt
* limit meson and makefile builds to what currently works
  • Loading branch information
jdhughes-usgs authored Aug 8, 2023
1 parent 3bf53a2 commit 5cd534b
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ jobs:

- name: Set CXX (temporary)
if: runner.os == 'Windows'
run: echo "CXX=icl" >> $GITHUB_ENV
run: |
echo "CXX=icl" >> $GITHUB_ENV
- name: Setup Graphviz
if: runner.os == 'Linux'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/pymake-gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ jobs:
compiler: gcc
version: 11

- name: Set CXX (temporary)
if: runner.os == 'Windows'
run: |
echo "CXX=g++" >> $GITHUB_ENV
- name: Download examples for pytest runs
run: |
.github/common/download-examples.sh
Expand Down
11 changes: 0 additions & 11 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@


# misc utilities
@contextlib.contextmanager
def working_directory(path):
"""Changes working directory and returns to previous on exit."""
prev_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev_cwd)


def get_pymake_appdir():
appdir = Path.home() / ".pymake"
appdir.mkdir(parents=True, exist_ok=True)
Expand Down
113 changes: 113 additions & 0 deletions autotest/test_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os
import sys
import time

import pytest
from flaky import flaky
from modflow_devtools.misc import get_ostag, set_dir

import pymake

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")
]
test_ostag = get_ostag()
test_fc_env = os.environ.get("FC")
if "win" in test_ostag:
meson_exclude = ("mt3dms", "vs2dt", "triangle", "gridgen")
elif "win" not in test_ostag and test_fc_env in ("ifort",):
meson_exclude = ("mf2000", "mf2005", "swtv4", "mflgr")
else:
meson_exclude = []
targets_meson = [t for t in targets if t not in meson_exclude]


def build_with_makefile(target, path, fc):
success = True
with set_dir(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 set_dir(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.parametrize("target", targets_meson)
def test_meson_build(function_tmpdir, target: str) -> None:
with set_dir(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 set_dir(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
70 changes: 40 additions & 30 deletions autotest/test_cli_cmds.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import pathlib as pl
import subprocess

import pytest
from flaky import flaky
from modflow_devtools.misc import set_dir

RERUNS = 3

Expand All @@ -11,6 +13,11 @@
"crt",
)

meson_parm = (
True,
False,
)


def run_cli_cmd(cmd: list) -> None:
process = subprocess.Popen(
Expand All @@ -35,12 +42,12 @@ def run_cli_cmd(cmd: list) -> None:
@pytest.mark.dependency(name="make_program")
@pytest.mark.base
@pytest.mark.parametrize("target", targets)
def test_make_program(module_tmpdir, target: str) -> None:
def test_make_program(function_tmpdir, target: str) -> None:
cmd = [
"make-program",
target,
"--appdir",
str(module_tmpdir),
str(function_tmpdir),
"--verbose",
]
run_cli_cmd(cmd)
Expand All @@ -60,33 +67,36 @@ def test_make_program_all(module_tmpdir) -> None:
run_cli_cmd(cmd)


@flaky(max_runs=RERUNS)
@pytest.mark.dependency(name="mfpymake")
@pytest.mark.base
def test_mfpymake(module_tmpdir) -> None:
src = (
"program hello\n"
+ " ! This is a comment line; it is ignored by the compiler\n"
+ " print *, 'Hello, World!'\n"
+ "end program hello\n"
)
src_file = module_tmpdir / "mfpymake_src" / "hello.f90"
src_file.parent.mkdir(parents=True, exist_ok=True)
with open(src_file, "w") as f:
f.write(src)
cmd = [
"mfpymake",
str(src_file.parent),
"hello",
"-mc",
"--verbose",
"--appdir",
str(module_tmpdir),
"-fc",
]
if os.environ.get("FC") is None:
cmd.append("gfortran")
else:
cmd.append(os.environ.get("FC"))
run_cli_cmd(cmd)
cmd = [module_tmpdir / "hello"]
run_cli_cmd(cmd)
@pytest.mark.parametrize("meson", meson_parm)
def test_mfpymake(function_tmpdir, meson: bool) -> None:
with set_dir(function_tmpdir):
src = (
"program hello\n"
+ " ! This is a comment line; it is ignored by the compiler\n"
+ " print *, 'Hello, World!'\n"
+ "end program hello\n"
)
src_file = pl.Path("src/hello.f90")
src_file.parent.mkdir(parents=True, exist_ok=True)
with open(src_file, "w") as f:
f.write(src)
cmd = [
"mfpymake",
str(src_file.parent),
"hello",
# "-mc",
"--verbose",
"-fc",
]
if os.environ.get("FC") is None:
cmd.append("gfortran")
else:
cmd.append(os.environ.get("FC"))
if meson:
cmd.append("--meson")
run_cli_cmd(cmd)
cmd = [function_tmpdir / "hello"]
run_cli_cmd(cmd)
2 changes: 1 addition & 1 deletion autotest/test_mf6.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import sys
import time
from platform import system
from pathlib import Path
from platform import system

import flopy
import pytest
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_mfusg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from platform import system
from pathlib import Path
from platform import system

import flopy
import pytest
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_mp6.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import shutil
from platform import system
from pathlib import Path
from platform import system

import flopy
import pytest
Expand Down
3 changes: 1 addition & 2 deletions autotest/test_mp7.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import shutil
from platform import system
from pathlib import Path
from platform import system

import flopy
import pytest

import pymake


ext = ".exe" if system() == "Windows" else ""


Expand Down
2 changes: 1 addition & 1 deletion autotest/test_seawat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from platform import system
from pathlib import Path
from platform import system

import flopy
import pytest
Expand Down
11 changes: 10 additions & 1 deletion pymake/cmds/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@
"zip",
"keep",
"dryrun",
"meson",
)
COM_ARG_KEYS = (
"fc",
"cc",
"fflags",
"cflags",
"zip",
"keep",
"dryrun",
)
COM_ARG_KEYS = ("fc", "cc", "fflags", "cflags", "zip", "keep", "dryrun")


def main() -> None:
Expand Down
4 changes: 2 additions & 2 deletions pymake/cmds/mfpymakecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def main() -> None:
verbose=args.verbose,
inplace=args.inplace,
networkx=args.networkx,
meson=args.mb,
mesondir=args.mesonbuild_dir,
meson=args.meson,
mesondir=args.mesondir,
)
except (EOFError, KeyboardInterrupt):
sys.exit(f" cancelling '{sys.argv[0]}'")
Expand Down
2 changes: 1 addition & 1 deletion pymake/pymake_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def main(
inplace = True
print(
f"Using meson to build {os.path.basename(target)}, "
+ "ressetting inplace to True"
+ "resetting inplace to True"
)

if srcdir is not None and target is not None:
Expand Down
4 changes: 2 additions & 2 deletions pymake/pymake_build_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def build_apps(

# set double precision flag and whether the executable name
# can be modified
if target in ["swtv4"]:
if target in ("swtv4",):
update_target_name = False
else:
update_target_name = True
Expand All @@ -214,7 +214,7 @@ def build_apps(
pmobj.inplace = True

# set target and srcdir
pmobj.target = target
pmobj.target = target.replace("dev", "")
pmobj.srcdir = os.path.join(
download_dir, code_dict[target].dirname, code_dict[target].srcdir
)
Expand Down
6 changes: 3 additions & 3 deletions pymake/pymake_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _get_standard_arg_dict():
"action": "store_true",
},
"makefiledir": {
"tag": ("-md", "--makefile-dir"),
"tag": ("-md", "--makefiledir"),
"help": "GNU make makefile directory. (default is '.')",
"default": ".",
"choices": None,
Expand Down Expand Up @@ -256,14 +256,14 @@ def _get_standard_arg_dict():
"action": "store_true",
},
"meson": {
"tag": ("--mb", "--meson-build"),
"tag": ("--meson",),
"help": """Use meson to build executable. (default is False)""",
"default": False,
"choices": None,
"action": "store_true",
},
"mesondir": {
"tag": ("-mbd", "--mesonbuild-dir"),
"tag": ("--mesondir",),
"help": "meson directory. (default is '.')",
"default": ".",
"choices": None,
Expand Down
Loading

0 comments on commit 5cd534b

Please sign in to comment.