Skip to content

Commit

Permalink
DEV: meson: allow specifying build directory and install prefix (scip…
Browse files Browse the repository at this point in the history
…y#15502)

- default "install-prefix" to `<build-dir>-install` so installs are not overwritten
- reconfigure build if install directory is inconsistent with the build directory
- ignore everything in the install directory

[skip azp]
  • Loading branch information
tirthasheshpatel authored Feb 3, 2022
1 parent 725a4eb commit 7deaf3e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux_meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:

env:
CCACHE_DIR: "${{ github.workspace }}/.ccache"
INSTALLDIR: "installdir"
INSTALLDIR: "build-install"

jobs:
test_meson:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos_meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- maintenance/**

env:
INSTALLDIR: "installdir"
INSTALLDIR: "build-install"
CCACHE_DIR: "${{ github.workspace }}/.ccache"

jobs:
Expand Down
76 changes: 59 additions & 17 deletions dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import time
import datetime
import importlib.util
import json # noqa: E402
from sysconfig import get_path

try:
Expand All @@ -77,8 +78,6 @@
from imp import new_module

ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__)))
PATH_INSTALLED = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'installdir')


def import_module_from_path(mod_name, mod_path):
Expand Down Expand Up @@ -166,8 +165,24 @@ def main(argv):
"Note: this argument may be removed in the future "
"once a `site.cfg`-like mechanism to select BLAS/LAPACK "
"libraries is implemented for Meson")
parser.add_argument("--build-dir", default="build",
help="Relative path to the build directory. "
"Default is 'build'")
parser.add_argument("--install-prefix", default=None,
help="Relative path to the install directory. "
"Default is <build-dir>-install.")
args = parser.parse_args(argv)

global PATH_INSTALLED
build_dir = Path(args.build_dir)
install_dir = args.install_prefix
if not install_dir:
install_dir = build_dir.parent / (build_dir.stem + "-install")
PATH_INSTALLED = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
install_dir
)

if args.win_cp_openblas and platform.system() != 'Windows':
raise RuntimeError('--win-cp-openblas only has effect on Windows')

Expand Down Expand Up @@ -250,7 +265,7 @@ def main(argv):
sys.exit(0)

if args.coverage:
dst_dir = os.path.join(ROOT_DIR, 'build', 'coverage')
dst_dir = os.path.join(ROOT_DIR, args.build_dir, 'coverage')
fn = os.path.join(dst_dir, 'coverage_html.js')
if os.path.isdir(dst_dir) and os.path.isfile(fn):
shutil.rmtree(dst_dir)
Expand Down Expand Up @@ -358,7 +373,7 @@ def main(argv):
if not args.no_build:
test_dir = site_dir
else:
test_dir = os.path.join(ROOT_DIR, 'build', 'test')
test_dir = os.path.join(ROOT_DIR, args.build_dir, 'test')
if not os.path.isdir(test_dir):
os.makedirs(test_dir)

Expand Down Expand Up @@ -392,11 +407,32 @@ def setup_build(args, env):
"""
Setting up meson-build
"""
cmd = ["meson", "setup", "build", "--prefix", PATH_INSTALLED]
cmd = ["meson", "setup", args.build_dir, "--prefix", PATH_INSTALLED]
build_dir = Path(args.build_dir)
run_dir = os.getcwd()
if build_dir.exists() and not (build_dir / 'meson-info').exists():
if list(build_dir.iterdir()):
raise RuntimeError("Can't build into non-empty directory "
f"'{build_dir.absolute()}'")
if os.path.exists(build_dir):
build_options_file = (build_dir / "meson-info"
/ "intro-buildoptions.json")
with open(build_options_file) as f:
build_options = json.load(f)
installdir = None
for option in build_options:
if option["name"] == "prefix":
installdir = option["value"]
break
if installdir != PATH_INSTALLED:
run_dir = os.path.join(run_dir, build_dir)
cmd = ["meson", "--reconfigure", "--prefix", PATH_INSTALLED]
else:
return
if args.werror:
cmd += ["--werror"]
# Setting up meson build
ret = subprocess.call(cmd, env=env, cwd=ROOT_DIR)
ret = subprocess.call(cmd, env=env, cwd=run_dir)
if ret == 0:
print("Meson build setup OK")
else:
Expand All @@ -405,14 +441,20 @@ def setup_build(args, env):
return


def install_project(show_build_log):
def install_project(args):
"""
Installs the project after building.
"""
cmd = ["meson", "install", "-C", "build"]
if os.path.exists(PATH_INSTALLED):
installdir = get_site_packages()
non_empty = len(os.listdir(PATH_INSTALLED))
if non_empty and not os.path.exists(installdir):
raise RuntimeError("Can't install in non-empty directory: "
f"'{PATH_INSTALLED}'")
cmd = ["meson", "install", "-C", args.build_dir]
log_filename = os.path.join(ROOT_DIR, 'meson-install.log')
start_time = datetime.datetime.now()
if show_build_log:
if args.show_build_log:
ret = subprocess.call(cmd, cwd=ROOT_DIR)
else:
print("Installing, see meson-install.log...")
Expand Down Expand Up @@ -445,12 +487,16 @@ def install_project(show_build_log):
elapsed = datetime.datetime.now() - start_time

if ret != 0:
if not show_build_log:
if not args.show_build_log:
with open(log_filename, 'r') as f:
print(f.read())
print("Installation failed! ({0} elapsed)".format(elapsed))
sys.exit(1)

# ignore everything in the install directory.
with open(Path(PATH_INSTALLED) / ".gitignore", "w") as f:
f.write("*")

print("Installation OK")
return

Expand Down Expand Up @@ -537,15 +583,11 @@ def build_project(args):
env['LDFLAGS'] = " ".join(cvars['LDSHARED'].split()[1:]) +\
' --coverage'

build_dir = os.path.join(ROOT_DIR, 'build')

# Check if meson is already setup
if not os.path.exists(os.path.join(build_dir, 'build.ninja')):
setup_build(args, env)
setup_build(args, env)

site_dir = get_site_packages()

cmd = ["ninja", "-C", "build"]
cmd = ["ninja", "-C", args.build_dir]
if args.parallel > 1:
cmd += ["-j", str(args.parallel)]

Expand All @@ -558,7 +600,7 @@ def build_project(args):
print("Build failed!")
sys.exit(1)

install_project(args.show_build_log)
install_project(args)

if args.win_cp_openblas and platform.system() == 'Windows':
if copy_openblas() == 0:
Expand Down

0 comments on commit 7deaf3e

Please sign in to comment.