Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/modflow-devtools-linting-install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: Install packages
run: |
pip install numpy flopy pylint flake8 black
pip install numpy flopy pylint flake8 black requests

- name: Run isort
run: |
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ dmypy.json
# pycharme
.idea/

# downloaded exe
modflow_devtools/bin
modflow_devtools/utilities/temp

33 changes: 33 additions & 0 deletions modflow_devtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
development."""


from .common_regression import (
get_example_basedir,
get_example_dirs,
get_home_dir,
get_select_dirs,
get_select_packages,
is_directory_available,
set_mf6_regression,
)

# modflow_devtools
from .config import (
__author__,
Expand Down Expand Up @@ -47,10 +57,27 @@
write_head,
)
from .utilities.disu_util import get_disu_kwargs
from .utilities.download import (
download_and_unzip,
get_repo_assets,
getmfexes,
getmfnightly,
repo_latest_version,
zip_all,
)
from .utilities.usgsprograms import usgs_program_data

# define public interfaces
__all__ = [
"__version__",
# common_regression
"get_example_basedir",
"get_example_dirs",
"get_home_dir",
"get_select_dirs",
"get_select_packages",
"is_directory_available",
"set_mf6_regression",
# targets
"run_exe",
"get_mf6_version",
Expand Down Expand Up @@ -89,4 +116,10 @@
"write_head",
"write_budget",
"get_disu_kwargs",
"usgs_program_data",
"download_and_unzip",
"getmfexes",
"repo_latest_version",
"get_repo_assets",
"zip_all",
]
156 changes: 156 additions & 0 deletions modflow_devtools/utilities/build_exes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Build targets

# to use ifort on windows, run this
# python build_exes.py -fc ifort

# can compile only mf6 directly using this command:
# python -c "import build_exes; build_exes.test_build_modflow6()"

import os
import pathlib as pl
import subprocess as sp
import sys
from contextlib import contextmanager

from modflow_devtools import running_on_CI

if running_on_CI():
print("running on CI environment")
os.environ["PYMAKE_DOUBLE"] = "1"

# set OS dependent extensions
eext = ""
soext = ".so"
if sys.platform.lower() == "win32":
eext = ".exe"
soext = ".dll"
elif sys.platform.lower() == "darwin":
soext = ".dylib"

mfexe_pth = "temp/mfexes"

# use the line below to set fortran compiler using environmental variables
# os.environ["FC"] = "ifort"

# some flags to check for errors in the code
# add -Werror for compilation to terminate if errors are found
strict_flags = (
"-fall-intrinsics "
"-Wtabs -Wline-truncation -Wunused-label "
"-Wunused-variable -pedantic -std=f2008 "
"-Wcharacter-truncation"
)


@contextmanager
def set_directory(path: str):
"""Sets the cwd within the context

Args:
path (Path): The path to the cwd

Yields:
None
"""

origin = os.path.abspath(os.getcwd())
path = os.path.abspath(path)
try:
os.chdir(path)
print(f"change from {origin} -> {path}")
yield
finally:
os.chdir(origin)
print(f"change from {path} -> {origin}")


def relpath_fallback(pth):
try:
# throws ValueError on Windows if pth is on a different drive
return os.path.relpath(pth)
except ValueError:
return os.path.abspath(pth)


def create_dir(pth):
# create pth directory
print(f"creating... {os.path.abspath(pth)}")
os.makedirs(pth, exist_ok=True)

msg = f"could not create... {os.path.abspath(pth)}"
assert os.path.exists(pth), msg


def set_compiler_environment_variable():
fc = None

# parse command line arguments
for idx, arg in enumerate(sys.argv):
if arg.lower() == "-fc":
fc = sys.argv[idx + 1]
elif arg.lower().startswith("-fc="):
fc = arg.split("=")[1]

# determine if fc needs to be set to the FC environmental variable
env_var = os.getenv("FC", default="gfortran")
if fc is None and fc != env_var:
fc = env_var

# validate Fortran compiler
fc_options = (
"gfortran",
"ifort",
)
if fc not in fc_options:
raise ValueError(
f"Fortran compiler {fc} not supported. Fortran compile must be "
+ f"[{', '.join(str(value) for value in fc_options)}]."
)

# set FC environment variable
os.environ["FC"] = fc


def meson_build(
dir_path: str = "..",
libdir: str = "bin",
):
set_compiler_environment_variable()
is_windows = sys.platform.lower() == "win32"
with set_directory(dir_path):
cmd = (
"meson setup builddir "
+ f"--bindir={os.path.abspath(libdir)} "
+ f"--libdir={os.path.abspath(libdir)} "
+ "--prefix="
)
if is_windows:
cmd += "%CD%"
else:
cmd += "$(pwd)"
if pl.Path("builddir").is_dir():
cmd += " --wipe"
print(f"setup meson\nrunning...\n {cmd}")
sp.run(cmd, shell=True, check=True)

cmd = "meson install -C builddir"
print(f"build and install with meson\nrunning...\n {cmd}")
sp.run(cmd, shell=True, check=True)


def test_create_dirs():
pths = [os.path.join("..", "bin"), os.path.join("temp")]

for pth in pths:
create_dir(pth)

return


def test_meson_build():
meson_build()


if __name__ == "__main__":
test_create_dirs()
test_meson_build()
Loading