From 35acc111ffb28fe6b0b8214f1a11d6049a3ac9f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 4 Sep 2019 11:38:44 +0200
Subject: [PATCH] Windows package: fix random build failure due to race
 condition

The MEX files are built out-of-tree (because we want to do them in parallel).
This would create a potential race condition if several builds want to create
the symlinks under mex/matlab/ or mex/octave/.

The solution is to disable those symlinks for out-of-tree builds.
---
 mex/build/matlab/mex.am | 14 +++++++++-----
 mex/build/octave/mex.am | 14 +++++++++-----
 windows/build.sh        |  3 +--
 3 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/mex/build/matlab/mex.am b/mex/build/matlab/mex.am
index 620de35386..7a91726c41 100644
--- a/mex/build/matlab/mex.am
+++ b/mex/build/matlab/mex.am
@@ -13,11 +13,15 @@ LIBS += $(MATLAB_LIBS)
 mexdir = $(libdir)/dynare/mex/matlab
 
 all-local: $(PROGRAMS)
-	$(MKDIR_P) $(top_srcdir)/../../matlab
-	cd $(top_srcdir)/../../matlab && \
-	for p in $(PROGRAMS); do \
-		$(LN_S) -f $(abs_builddir)/$$p $$p; \
-	done
+## Create symlinks in mex/matlab/, but only if the build is not
+## out-of-tree (because of the Windows installer)
+	if [ "$(abs_srcdir)" = "$(abs_builddir)" ]; then \
+		$(MKDIR_P) $(top_srcdir)/../../matlab && \
+		cd $(top_srcdir)/../../matlab && \
+		for p in $(PROGRAMS); do \
+			$(LN_S) -f $(abs_builddir)/$$p $$p; \
+		done; \
+	fi
 
 clean-local:
 	if test -d $(top_srcdir)/../../matlab; then \
diff --git a/mex/build/octave/mex.am b/mex/build/octave/mex.am
index df5505c270..f0da052bee 100644
--- a/mex/build/octave/mex.am
+++ b/mex/build/octave/mex.am
@@ -20,11 +20,15 @@ LIBS += $(shell $(MKOCTFILE) -p FLIBS)
 mexdir = $(libdir)/dynare/mex/octave
 
 all-local: $(PROGRAMS)
-	$(MKDIR_P) $(top_srcdir)/../../octave
-	cd $(top_srcdir)/../../octave && \
-	for p in $(PROGRAMS); do \
-		$(LN_S) -f $(abs_builddir)/$$p $$p; \
-	done
+## Create symlinks in mex/octave/, but only if the build is not
+## out-of-tree (because of the Windows installer)
+	if [ "$(abs_srcdir)" = "$(abs_builddir)" ]; then \
+		$(MKDIR_P) $(top_srcdir)/../../octave && \
+		cd $(top_srcdir)/../../octave && \
+		for p in $(PROGRAMS); do \
+			$(LN_S) -f $(abs_builddir)/$$p $$p; \
+		done; \
+	fi
 
 clean-local:
 	if test -d $(top_srcdir)/../../octave; then \
diff --git a/windows/build.sh b/windows/build.sh
index 36d8e00c5f..dd31a80fe6 100755
--- a/windows/build.sh
+++ b/windows/build.sh
@@ -220,8 +220,7 @@ cd "$ROOT_DIRECTORY"
 export TMP_DIRECTORY ROOT_DIRECTORY LIB32 LIB64 VERSION NTHREADS
 export -f "${TASKS[@]}"
 parallel "set -ex;shopt -s globstar;" ::: "${TASKS[@]}"
-# Clean up bogus symlinks left by parallel builds of MEX
-rm -f ../mex/matlab/*.mexw32 ../mex/matlab/*.mexw64 ../mex/octave/*.mex
+
 # Add supported_octave_version.m (see matlab/dynare.m)
 while read -r line
 do
-- 
GitLab