From 62da9a8cfe3e69b49602f4018e8e1af365c10ddc Mon Sep 17 00:00:00 2001
From: Jules Villard <jul@fb.com>
Date: Fri, 13 Apr 2018 03:43:05 -0700
Subject: [PATCH] [make] fix `make devsetup`

Summary:
Making the shell stricter revealed a bug in `make devsetup`:
`shell_config_file` can be used without being initialised. Also, the error path
when testing if `BUILD_MODE` was set to default was wrong and would execute all
except the first `echo` command unconditionally. I changed the test so that it
tests only if `BUILD_MODE` is set in the environment.

Reviewed By: martinoluca

Differential Revision: D7615333

fbshipit-source-id: 08c5eb0
---
 Makefile | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile
index af9b07fd5..66a31b00e 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ default: infer
 ROOT_DIR = .
 include $(ROOT_DIR)/Makefile.config
 
+ORIG_SHELL_BUILD_MODE = $(BUILD_MODE)
 # override this for faster builds (but slower infer)
 BUILD_MODE ?= opt
 
@@ -635,17 +636,17 @@ devsetup: Makefile.autoconf
 # 	expand all occurrences of "~" in PATH and MANPATH
 	$(QUIET)infer_repo_is_in_path=$$(echo $${PATH//\~/$$HOME} | grep -q "$(ABSOLUTE_ROOT_DIR)"/infer/bin; echo $$?); \
 	infer_repo_is_in_manpath=$$(echo $${MANPATH//\~/$$HOME} | grep -q "$(ABSOLUTE_ROOT_DIR)"/infer/man; echo $$?); \
-	if [ "$$infer_repo_is_in_path" != "0" ] || [ "$$infer_repo_is_in_manpath" != "0" ]; then \
-	  shell_config_file="<could not auto-detect, please fill in yourself>"; \
-	  if [ $$(basename "$(ORIG_SHELL)") = "bash" ]; then \
-	    if [ "$(PLATFORM)" = "Linux" ]; then \
-	      shell_config_file="$$HOME"/.bashrc; \
-	    else \
-	      shell_config_file="$$HOME"/.bash_profile; \
-	    fi; \
-	  elif [ $$(basename "$(ORIG_SHELL)") = "zsh" ]; then \
-	    shell_config_file="$$HOME"/.zshrc; \
+	shell_config_file="<could not auto-detect, please fill in yourself>"; \
+	if [ $$(basename "$(ORIG_SHELL)") = "bash" ]; then \
+	  if [ "$(PLATFORM)" = "Linux" ]; then \
+	    shell_config_file="$$HOME"/.bashrc; \
+	  else \
+	    shell_config_file="$$HOME"/.bash_profile; \
 	  fi; \
+	elif [ $$(basename "$(ORIG_SHELL)") = "zsh" ]; then \
+	  shell_config_file="$$HOME"/.zshrc; \
+	fi; \
+	if [ "$$infer_repo_is_in_path" != "0" ] || [ "$$infer_repo_is_in_manpath" != "0" ]; then \
 	  echo >&2; \
 	  echo '$(TERM_INFO)*** NOTE: `infer` is not in your PATH or MANPATH. If you are hacking on infer, you may$(TERM_RESET)' >&2; \
 	  echo '$(TERM_INFO)*** NOTE: want to make infer executables and manuals available in your terminal. Type$(TERM_RESET)' >&2; \
@@ -665,16 +666,17 @@ devsetup: Makefile.autoconf
 	    printf "$(TERM_INFO)  echo 'export MANPATH=\"%s/infer/man\":\$$MANPATH' >> \"$$shell_config_file\"$(TERM_RESET)\n" "$(ABSOLUTE_ROOT_DIR)" >&2; \
 	  fi; \
 	fi; \
-	test "$$BUILD_MODE" = "default" || \
+	if [ -z "$(ORIG_SHELL_BUILD_MODE)" ]; then \
 	  echo >&2; \
 	  echo '$(TERM_INFO)*** NOTE: Set `BUILD_MODE=default` in your shell to disable flambda by default.$(TERM_RESET)' >&2; \
 	  echo '$(TERM_INFO)*** NOTE: Compiling with flambda is ~5 times slower than without, so unless you are$(TERM_RESET)' >&2; \
 	  echo '$(TERM_INFO)*** NOTE: testing infer on a very large project it will not be worth it. Use the$(TERM_RESET)' >&2; \
-	  echo '$(TERM_INFO)*** NOTE: commands below to set the default build mode. You can then use `make BUILD_MODE=opt`$(TERM_RESET)' >&2; \
+	  echo '$(TERM_INFO)*** NOTE: commands below to set the default build mode. You can then use `make opt`$(TERM_RESET)' >&2; \
 	  echo '$(TERM_INFO)*** NOTE: when you really do want to enable flambda.$(TERM_RESET)' >&2; \
 	  echo >&2; \
 	  printf "$(TERM_INFO)  export BUILD_MODE=default$(TERM_RESET)\n" >&2; \
-	  printf "$(TERM_INFO)  echo 'export BUILD_MODE=default' >> \"$$shell_config_file\"$(TERM_RESET)\n" >&2
+	  printf "$(TERM_INFO)  echo 'export BUILD_MODE=default' >> \"$$shell_config_file\"$(TERM_RESET)\n" >&2; \
+	fi
 	$(QUIET)PATH=$(ORIG_SHELL_PATH); if [ "$$(ocamlc -where 2>/dev/null)" != "$$($(OCAMLC) -where)" ]; then \
 	  echo >&2; \
 	  echo '$(TERM_INFO)*** NOTE: The current shell is not set up for the right opam switch.$(TERM_RESET)' >&2; \