Skip to content

Commit

Permalink
lammps-migration: build in container from cmdline and check at every …
Browse files Browse the repository at this point in the history
…period
  • Loading branch information
csegarragonz committed Dec 11, 2023
1 parent e53578f commit e4dff3d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/lammps-migration
Submodule lammps-migration updated 1 files
+2 −3 src/main.cpp
7 changes: 7 additions & 0 deletions tasks/env.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from faasmtools.build import FAASM_LOCAL_DIR
from faasmtools.docker import ACR_NAME
from faasmtools.env import get_version as get_cpp_version
from os import environ
from os.path import dirname, abspath, join
from re import search as re_search
from subprocess import run

PROJ_ROOT = dirname(dirname(abspath(__file__)))
EXAMPLES_IN_DOCKER_ROOT = "/code/examples"
DOCKER_ROOT = join(PROJ_ROOT, "docker")
EXAMPLES_DIR = join(PROJ_ROOT, "examples")
DEV_FAASM_LOCAL = join(PROJ_ROOT, "dev", "faasm-local")
WASM_DIR = join(PROJ_ROOT, "wasm")

# Docker variables
EXAMPLES_BUILD_IMAGE_CTR = "examples-build-workon"
EXAMPLES_BUILD_IMAGE_NAME = "{}/examples-build".format(ACR_NAME)
EXAMPLES_BUILD_DOCKERFILE = join(DOCKER_ROOT, "build.dockerfile")
EXAMPLES_RUN_IMAGE_NAME = "{}/examples-run".format(ACR_NAME)
Expand Down Expand Up @@ -115,3 +118,7 @@ def get_version(name="build"):
return "{}_{}".format(get_cpp_version(), get_python_version())
if name == "run":
return "{}".format(get_faasm_version())


def in_docker():
return "IN_DOCKER" in environ
50 changes: 42 additions & 8 deletions tasks/lammps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from faasmtools.build import CMAKE_TOOLCHAIN_FILE, FAASM_BUILD_ENV_DICT
from faasmtools.compile_util import wasm_copy_upload
from faasmtools.env import LLVM_VERSION
from tasks.env import EXAMPLES_DIR
from tasks.env import DEV_FAASM_LOCAL, EXAMPLES_DIR, in_docker
from tasks.util import run_docker_build_cmd
from invoke import task
from os import environ, makedirs
from os.path import exists, join
Expand Down Expand Up @@ -51,16 +52,49 @@ def build(ctx, clean=False, native=False, migration=False):
"-DLAMMPS_FAASM=ON",
"-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE),
]
cmake_cmd += [cmake_dir]
cmake_cmd = " ".join(cmake_cmd)
if in_docker():
cmake_cmd += [cmake_dir]
cmake_cmd = " ".join(cmake_cmd)

work_env = environ.copy()
work_env.update(FAASM_BUILD_ENV_DICT)
work_env = environ.copy()
work_env.update(FAASM_BUILD_ENV_DICT)

run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env)
run("ninja", shell=True, check=True, cwd=build_dir)
run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env)
run("ninja", shell=True, check=True, cwd=build_dir)

else:
in_docker_cmake_dir = cmake_dir
in_docker_cmake_dir = cmake_dir.removeprefix(EXAMPLES_DIR)
in_docker_cmake_dir = "/code/examples/examples" + in_docker_cmake_dir
in_docker_build_dir = build_dir
in_docker_build_dir = build_dir.removeprefix(EXAMPLES_DIR)
in_docker_build_dir = "/code/examples/examples" + in_docker_build_dir

cmake_cmd += [in_docker_cmake_dir]
cmake_cmd = " ".join(cmake_cmd)

run_docker_build_cmd(
[cmake_cmd, "ninja"],
cwd=in_docker_build_dir,
env=FAASM_BUILD_ENV_DICT,
)

if not native:
# Copy the binary to lammps/main/function.wasm
lammps_func_name = "migration" if migration else "main"
wasm_copy_upload("lammps", lammps_func_name, join(build_dir, "lmp"))
lammps_func = join(build_dir, "lmp")

if in_docker():
wasm_copy_upload("lammps", lammps_func_name, lammps_func)
else:
dest_folder = join(
DEV_FAASM_LOCAL, "wasm", "lammps", lammps_func_name
)

makedirs(dest_folder, exist_ok=True)
dest_file = join(dest_folder, "function.wasm")
run(
"sudo cp {} {}".format(lammps_func, dest_file),
shell=True,
check=True,
)
56 changes: 56 additions & 0 deletions tasks/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from subprocess import run
from tasks.env import (
DEV_FAASM_LOCAL,
EXAMPLES_BUILD_IMAGE_CTR,
EXAMPLES_BUILD_IMAGE_NAME,
PROJ_ROOT,
get_version,
)


def start_docker_build_ctr():
docker_cmd = [
"docker run",
"-td",
"-v {}:/code/examples".format(PROJ_ROOT),
"-v {}:{}".format(DEV_FAASM_LOCAL, "/usr/local/faasm"),
"--name {}".format(EXAMPLES_BUILD_IMAGE_CTR),
"{}:{}".format(EXAMPLES_BUILD_IMAGE_NAME, get_version()),
]

docker_cmd = " ".join(docker_cmd)
out = run(docker_cmd, shell=True, capture_output=True)
assert out.returncode == 0, "Error starting build container: {}".format(
out.stderr.decode("utf-8")
)


def stop_docker_build_ctr():
out = run(
"docker rm -f {}".format(EXAMPLES_BUILD_IMAGE_CTR),
shell=True,
capture_output=True,
)
assert out.returncode == 0, "Error stopping build container: {}".format(
out.stderr.decode("utf-8")
)


def run_docker_build_cmd(cmd_list, cwd=None, env=None):
start_docker_build_ctr()

for cmd in cmd_list:
docker_cmd = [
"docker exec",
"--workdir {}".format(cwd) if cwd is not None else "",
" ".join(['--env {}="{}"'.format(k, env[k]) for k in env])
if env is not None
else "",
EXAMPLES_BUILD_IMAGE_CTR,
cmd,
]

docker_cmd = " ".join(docker_cmd)
run(docker_cmd, shell=True, check=True)

stop_docker_build_ctr()

0 comments on commit e4dff3d

Please sign in to comment.