From a3e9b9dacf290966acfa735026a2015e67128598 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Tue, 11 Oct 2016 14:12:28 -0700
Subject: [PATCH] native build: avoid generally unnecessary work

---
 .gitignore                                    |  5 +++-
 com.oracle.truffle.r.native/fficall/Makefile  | 29 ++++++++++++-------
 .../gnur/Makefile.libs                        | 13 ++++++---
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/.gitignore b/.gitignore
index 49bdcfc69b..92e046d429 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,10 +20,14 @@
 /com.oracle.truffle.r.native/gnur/Makeconf.done
 /com.oracle.truffle.r.native/gnur/platform.mk.temp*
 /com.oracle.truffle.r.native/gnur/R-*
+/com.oracle.truffle.r.native/gnur/rcopylib.done
 /com.oracle.truffle.r.native/run/Makeconf.etc
 /com.oracle.truffle.r.native/include/*.h
 /com.oracle.truffle.r.native/include/R_ext/*.h
 /com.oracle.truffle.r.native/include/linked
+/com.oracle.truffle.r.native/fficall/jni.done
+/com.oracle.truffle.r.native/fficall/jniboot.done
+
 /com.oracle.truffle.r.test.native/packages/copy_recommended
 /com.oracle.truffle.r.test.native/packages/recommended
 /com.oracle.truffle.r.test.native/packages/*/lib/*
@@ -123,4 +127,3 @@ test_fastr
 lib.install.cran*
 package.blacklist
 com.oracle.truffle.r.test.native/embedded/lib
-
diff --git a/com.oracle.truffle.r.native/fficall/Makefile b/com.oracle.truffle.r.native/fficall/Makefile
index 58b03709a5..46b4f1d95f 100644
--- a/com.oracle.truffle.r.native/fficall/Makefile
+++ b/com.oracle.truffle.r.native/fficall/Makefile
@@ -31,12 +31,12 @@ $(error no platform.mk available)
 endif
 endif
 
-.PHONY: all clean objs
+.PHONY: all clean
 
 R_LIBNAME := libR$(DYLIB_EXT)
 R_LIB := $(FASTR_LIB_DIR)/$(R_LIBNAME)
-BOOTJNI_LIBNAME := libjniboot$(DYLIB_EXT)
-BOOTJNI_LIB := $(FASTR_LIB_DIR)/$(BOOTJNI_LIBNAME)
+JNIBOOT_LIBNAME := libjniboot$(DYLIB_EXT)
+JNIBOOT_LIB := $(FASTR_LIB_DIR)/$(JNIBOOT_LIBNAME)
 
 ifeq ($(OS_NAME), Darwin)
 VERSION_FLAGS := -current_version $(R_VERSION) -compatibility_version $(R_VERSION)
@@ -45,9 +45,13 @@ endif
 BLAS_TARGET := $(FASTR_LIB_DIR)/libRblas$(DYLIB_EXT)
 LAPACK_TARGET := $(FASTR_LIB_DIR)/libRlapack$(DYLIB_EXT)
 
-all: $(R_LIB) $(BOOTJNI_LIB)
+all: $(R_LIB) $(JNIBOOT_LIB)
 
-$(R_LIB): objs 
+# use sentinels to avoid (usually unnecessary) rebuilds.
+# N.B. if things change in the subdirs, a clean must be invoked
+# to remove the sentinels
+
+$(R_LIB): jni.done 
 ifeq ($(OS_NAME),Darwin)
 	$(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -o $(R_LIB) $(wildcard lib/*.o) -L$(FASTR_LIB_DIR) -lRblas -lRlapack  -lpcre -lz $(VERSION_FLAGS)
 	install_name_tool -change libRblas.dylib @rpath/libRblas.dylib $(R_LIB)
@@ -59,22 +63,25 @@ else
 	$(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,'$$ORIGIN' -o $(R_LIB) $(wildcard lib/*.o) -L$(FASTR_LIB_DIR) -lRblas -lRlapack  -lpcre -lz
 endif
 
-objs:
+jni.done:
 	$(MAKE) -C src/common all
 	$(MAKE) -C src/jni all
+	touch jni.done
 
-$(BOOTJNI_LIB): bootobjs
-	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(BOOTJNI_LIB) src/jniboot/jniboot.o $(VERSION_FLAGS)
+$(JNIBOOT_LIB): jniboot.done
+	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(JNIBOOT_LIB) src/jniboot/jniboot.o $(VERSION_FLAGS)
 ifeq ($(OS_NAME),Darwin)
-	install_name_tool -id @rpath/libjniboot.dylib $(BOOTJNI_LIB)
+	install_name_tool -id @rpath/libjniboot.dylib $(JNIBOOT_LIB)
 endif
 
-bootobjs:
+jniboot.done:
 	$(MAKE) -C src/jniboot all
+	touch jniboot.done
 
 clean:
 	$(MAKE) -C src/common clean
 	$(MAKE) -C src/jni clean
 	rm -rf $(R_LIB)
-	rm -rf $(BOOTJNI_LIB)
+	rm -rf $(JNIBOOT_LIB)
+	rm -rf jni.done jniboot.done
 	
diff --git a/com.oracle.truffle.r.native/gnur/Makefile.libs b/com.oracle.truffle.r.native/gnur/Makefile.libs
index 7463102b12..7e6fa89861 100644
--- a/com.oracle.truffle.r.native/gnur/Makefile.libs
+++ b/com.oracle.truffle.r.native/gnur/Makefile.libs
@@ -38,9 +38,9 @@ LAPACK_TARGET := $(FASTR_LIB_DIR)/libRlapack$(DYLIB_EXT)
 # they were found in the standard system locations
 OTHER_LIB_TARGETS = pcre z gfortran quadmath gcc_s
 
-.PHONY: all other_lib_targets
+.PHONY: all
 
-all: $(FASTR_LIB_DIR) $(BLAS_TARGET) $(LAPACK_TARGET) other_lib_targets
+all: $(FASTR_LIB_DIR) $(BLAS_TARGET) $(LAPACK_TARGET) rcopylib.done
 
 $(FASTR_LIB_DIR):
 	mkdir -p $(FASTR_LIB_DIR)
@@ -60,12 +60,17 @@ ifeq ($(OS_NAME),Darwin)
 	install_name_tool -id @rpath/libRlapack.dylib $(LAPACK_TARGET)
 endif
 
-other_lib_targets:
+# we can't create exact dependencies since we don't know exactly
+# what versions of the lubs (if any) will be copied, so we use a sentinel
+# file to indicate that the check has been done. 
+rcopylib.done: 
 	for target in $(OTHER_LIB_TARGETS); do \
 		mx rcopylib $$target $(FASTR_LIB_DIR) || exit 1; \
 	done
-	
+	touch rcopylib.done
+
 clean:
 	rm -f $(BLAS_TARGET) $(LAPACK_TARGET) 
 	rm -f $(foreach target,$(OTHER_LIB_TARGETS),$(wildcard $(FASTR_LIB_DIR)/lib$(target).*))
+	rm -f rcopylib.done
 
-- 
GitLab