Skip to content

Commit 5cd534b

Browse files
authored
fix(meson): fix issues with meson builds (#137)
* 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
1 parent 3bf53a2 commit 5cd534b

18 files changed

+270
-82
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ jobs:
3737

3838
- name: Set CXX (temporary)
3939
if: runner.os == 'Windows'
40-
run: echo "CXX=icl" >> $GITHUB_ENV
40+
run: |
41+
echo "CXX=icl" >> $GITHUB_ENV
4142
4243
- name: Setup Graphviz
4344
if: runner.os == 'Linux'

.github/workflows/pymake-gcc.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ jobs:
4646
compiler: gcc
4747
version: 11
4848

49+
- name: Set CXX (temporary)
50+
if: runner.os == 'Windows'
51+
run: |
52+
echo "CXX=g++" >> $GITHUB_ENV
53+
4954
- name: Download examples for pytest runs
5055
run: |
5156
.github/common/download-examples.sh

autotest/conftest.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@
1515

1616

1717
# misc utilities
18-
@contextlib.contextmanager
19-
def working_directory(path):
20-
"""Changes working directory and returns to previous on exit."""
21-
prev_cwd = os.getcwd()
22-
os.chdir(path)
23-
try:
24-
yield
25-
finally:
26-
os.chdir(prev_cwd)
27-
28-
2918
def get_pymake_appdir():
3019
appdir = Path.home() / ".pymake"
3120
appdir.mkdir(parents=True, exist_ok=True)

autotest/test_build.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
import sys
3+
import time
4+
5+
import pytest
6+
from flaky import flaky
7+
from modflow_devtools.misc import get_ostag, set_dir
8+
9+
import pymake
10+
11+
RERUNS = 3
12+
13+
targets = pymake.usgs_program_data.get_keys(current=True)
14+
targets_make = [
15+
t
16+
for t in targets
17+
if t not in ("libmf6", "gridgen", "mf2000", "swtv4", "mflgr")
18+
]
19+
test_ostag = get_ostag()
20+
test_fc_env = os.environ.get("FC")
21+
if "win" in test_ostag:
22+
meson_exclude = ("mt3dms", "vs2dt", "triangle", "gridgen")
23+
elif "win" not in test_ostag and test_fc_env in ("ifort",):
24+
meson_exclude = ("mf2000", "mf2005", "swtv4", "mflgr")
25+
else:
26+
meson_exclude = []
27+
targets_meson = [t for t in targets if t not in meson_exclude]
28+
29+
30+
def build_with_makefile(target, path, fc):
31+
success = True
32+
with set_dir(path):
33+
if os.path.isfile("makefile"):
34+
# wait to delete on windows
35+
if sys.platform.lower() == "win32":
36+
time.sleep(6)
37+
38+
# clean prior to make
39+
print(f"clean {target} with makefile")
40+
os.system("make clean")
41+
42+
# build MODFLOW-NWT with makefile
43+
print(f"build {target} with makefile")
44+
return_code = os.system("make")
45+
46+
# test if running on Windows with ifort, if True the makefile
47+
# should fail
48+
errmsg = f"{target} created by makefile does not exist."
49+
if sys.platform.lower() == "win32" and fc == "ifort":
50+
if return_code != 0:
51+
success = True
52+
else:
53+
success = False
54+
# verify that MODFLOW-NWT was made
55+
else:
56+
success = os.path.isfile(target)
57+
else:
58+
errmsg = "makefile does not exist"
59+
60+
return success, errmsg
61+
62+
63+
@pytest.mark.base
64+
@flaky(max_runs=RERUNS)
65+
@pytest.mark.parametrize("target", targets)
66+
def test_build(function_tmpdir, target: str) -> None:
67+
with set_dir(function_tmpdir):
68+
assert (
69+
pymake.build_apps(
70+
target,
71+
verbose=True,
72+
clean=False,
73+
)
74+
== 0
75+
), f"could not compile {target}"
76+
77+
78+
@pytest.mark.base
79+
@flaky(max_runs=RERUNS)
80+
@pytest.mark.parametrize("target", targets_meson)
81+
def test_meson_build(function_tmpdir, target: str) -> None:
82+
with set_dir(function_tmpdir):
83+
assert (
84+
pymake.build_apps(
85+
target,
86+
verbose=True,
87+
clean=False,
88+
meson=True,
89+
)
90+
== 0
91+
), f"could not compile {target}"
92+
93+
94+
@pytest.mark.base
95+
@flaky(max_runs=RERUNS)
96+
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on Windows")
97+
@pytest.mark.parametrize("target", targets_make)
98+
def test_makefile_build(function_tmpdir, target: str) -> None:
99+
pm = pymake.Pymake(verbose=True)
100+
pm.target = target
101+
pm.makefile = True
102+
pm.makefiledir = "."
103+
pm.inplace = True
104+
pm.dryrun = True
105+
pm.makeclean = False
106+
107+
with set_dir(function_tmpdir):
108+
pm.download_target(target)
109+
assert pm.download, f"could not download {target} distribution"
110+
assert pm.build() == 0, f"could not compile {target}"
111+
112+
success, errmsg = build_with_makefile(target, function_tmpdir, pm.fc)
113+
assert success, errmsg

autotest/test_cli_cmds.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
2+
import pathlib as pl
23
import subprocess
34

45
import pytest
56
from flaky import flaky
7+
from modflow_devtools.misc import set_dir
68

79
RERUNS = 3
810

@@ -11,6 +13,11 @@
1113
"crt",
1214
)
1315

16+
meson_parm = (
17+
True,
18+
False,
19+
)
20+
1421

1522
def run_cli_cmd(cmd: list) -> None:
1623
process = subprocess.Popen(
@@ -35,12 +42,12 @@ def run_cli_cmd(cmd: list) -> None:
3542
@pytest.mark.dependency(name="make_program")
3643
@pytest.mark.base
3744
@pytest.mark.parametrize("target", targets)
38-
def test_make_program(module_tmpdir, target: str) -> None:
45+
def test_make_program(function_tmpdir, target: str) -> None:
3946
cmd = [
4047
"make-program",
4148
target,
4249
"--appdir",
43-
str(module_tmpdir),
50+
str(function_tmpdir),
4451
"--verbose",
4552
]
4653
run_cli_cmd(cmd)
@@ -60,33 +67,36 @@ def test_make_program_all(module_tmpdir) -> None:
6067
run_cli_cmd(cmd)
6168

6269

70+
@flaky(max_runs=RERUNS)
6371
@pytest.mark.dependency(name="mfpymake")
6472
@pytest.mark.base
65-
def test_mfpymake(module_tmpdir) -> None:
66-
src = (
67-
"program hello\n"
68-
+ " ! This is a comment line; it is ignored by the compiler\n"
69-
+ " print *, 'Hello, World!'\n"
70-
+ "end program hello\n"
71-
)
72-
src_file = module_tmpdir / "mfpymake_src" / "hello.f90"
73-
src_file.parent.mkdir(parents=True, exist_ok=True)
74-
with open(src_file, "w") as f:
75-
f.write(src)
76-
cmd = [
77-
"mfpymake",
78-
str(src_file.parent),
79-
"hello",
80-
"-mc",
81-
"--verbose",
82-
"--appdir",
83-
str(module_tmpdir),
84-
"-fc",
85-
]
86-
if os.environ.get("FC") is None:
87-
cmd.append("gfortran")
88-
else:
89-
cmd.append(os.environ.get("FC"))
90-
run_cli_cmd(cmd)
91-
cmd = [module_tmpdir / "hello"]
92-
run_cli_cmd(cmd)
73+
@pytest.mark.parametrize("meson", meson_parm)
74+
def test_mfpymake(function_tmpdir, meson: bool) -> None:
75+
with set_dir(function_tmpdir):
76+
src = (
77+
"program hello\n"
78+
+ " ! This is a comment line; it is ignored by the compiler\n"
79+
+ " print *, 'Hello, World!'\n"
80+
+ "end program hello\n"
81+
)
82+
src_file = pl.Path("src/hello.f90")
83+
src_file.parent.mkdir(parents=True, exist_ok=True)
84+
with open(src_file, "w") as f:
85+
f.write(src)
86+
cmd = [
87+
"mfpymake",
88+
str(src_file.parent),
89+
"hello",
90+
# "-mc",
91+
"--verbose",
92+
"-fc",
93+
]
94+
if os.environ.get("FC") is None:
95+
cmd.append("gfortran")
96+
else:
97+
cmd.append(os.environ.get("FC"))
98+
if meson:
99+
cmd.append("--meson")
100+
run_cli_cmd(cmd)
101+
cmd = [function_tmpdir / "hello"]
102+
run_cli_cmd(cmd)

autotest/test_mf6.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22
import sys
33
import time
4-
from platform import system
54
from pathlib import Path
5+
from platform import system
66

77
import flopy
88
import pytest

autotest/test_mfusg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
from platform import system
32
from pathlib import Path
3+
from platform import system
44

55
import flopy
66
import pytest

autotest/test_mp6.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import shutil
3-
from platform import system
43
from pathlib import Path
4+
from platform import system
55

66
import flopy
77
import pytest

autotest/test_mp7.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import os
22
import shutil
3-
from platform import system
43
from pathlib import Path
4+
from platform import system
55

66
import flopy
77
import pytest
88

99
import pymake
1010

11-
1211
ext = ".exe" if system() == "Windows" else ""
1312

1413

autotest/test_seawat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import sys
3-
from platform import system
43
from pathlib import Path
4+
from platform import system
55

66
import flopy
77
import pytest

0 commit comments

Comments
 (0)