aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/mesonlib.py38
-rw-r--r--mesonbuild/mesonmain.py3
2 files changed, 40 insertions, 1 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py
index 247b4f9..a076e3e 100644
--- a/mesonbuild/mesonlib.py
+++ b/mesonbuild/mesonlib.py
@@ -21,6 +21,21 @@ import platform, subprocess, operator, os, shutil, re
import collections
from mesonbuild import mlog
+have_fcntl = False
+have_msvcrt = False
+
+try:
+ import fcntl
+ have_fcntl = True
+except Exception:
+ pass
+
+try:
+ import msvcrt
+ have_msvcrt = True
+except Exception:
+ pass
+
from glob import glob
def detect_meson_py_location():
@@ -978,3 +993,26 @@ class OrderedSet(collections.MutableSet):
def difference(self, set_):
return type(self)(e for e in self if e not in set_)
+
+class BuildDirLock:
+
+ def __init__(self, builddir):
+ self.lockfilename = os.path.join(builddir, 'meson-private/meson.lock')
+
+ def __enter__(self):
+ self.lockfile = open(self.lockfilename, 'w')
+ try:
+ if have_fcntl:
+ fcntl.flock(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ elif have_msvcrt:
+ msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1)
+ except (BlockingIOError, PermissionError):
+ self.lockfile.close()
+ raise MesonException('Some other Meson process is already using this build directory. Exiting.')
+
+ def __exit__(self, *args):
+ if have_fcntl:
+ fcntl.flock(self.lockfile, fcntl.LOCK_UN)
+ elif have_msvcrt:
+ msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1)
+ self.lockfile.close()
diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py
index 9c4498c..651224e 100644
--- a/mesonbuild/mesonmain.py
+++ b/mesonbuild/mesonmain.py
@@ -147,7 +147,8 @@ class MesonApp:
def generate(self):
env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_launcher, self.options, self.original_cmd_line_args)
mlog.initialize(env.get_log_dir())
- self._generate(env)
+ with mesonlib.BuildDirLock(self.build_dir):
+ self._generate(env)
def _generate(self, env):
mlog.debug('Build started at', datetime.datetime.now().isoformat())