Bug 530012 – [gtk] Update make_linux to simplify building of launcher.

Details:
- This makes make_linux.mak independent, i.e the following works:
  make -f make_linux.mak clean all
  It does so by assuming default values if they are not set by build.sh.
  However, build.sh still overrides values for backwards compatibility.
  I.e, I expect it will work fine with build servers.

- I migrated some linux-specific logic from build.sh to make_linux.mak
  * JAVA_HOME detection that I added earlier,
  * linux specific x86/x86_64 flags

- Added a new target: dev_build_install
  This can be invoked like:
    export DEV_ECLIPSE="your/eclipse"
    make -f make_linux.mak clean dev_build_install
  It builds the launcher/lib and then you can test the output in your
  dev eclipse. (see target for details.).

Test strategy:
I've done some very through testing, I'm pretty confident it shouldn't
break anything. I've accounted for x86/x86_64 differences.
I've added a print statement that prints info about variables:
$(info Input info: ....)
This line generates the same values for when you run build.sh as well
as when make_linux is invoked directly.

Produced eclipse and eclipse_xxxx.so work well.
Tested on Fedora 27, Wayland, gtk3.22, x86_64.

Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=530012
Change-Id: I07ec8f453acd93d368e5e8f927fa65354da0f68b
Signed-off-by: Leo Ufimtsev <lufimtse@redhat.com>
diff --git a/features/org.eclipse.equinox.executable.feature/library/gtk/README.md b/features/org.eclipse.equinox.executable.feature/library/gtk/README.md
index 5f9906e..adef8c5 100644
--- a/features/org.eclipse.equinox.executable.feature/library/gtk/README.md
+++ b/features/org.eclipse.equinox.executable.feature/library/gtk/README.md
@@ -2,19 +2,13 @@
 
 # Building
 
-To build:
-
-    ./bulid.sh  
-
-This will read the relevant make files.
-
-To clean:
-
+    ./bulid.sh
     ./build.sh clean
 
 # Developer notes:
 
-To inject into a test eclipse instance for testing:  
+See make_linux.mak for info on how to build/test. See dev_build_install target. Common use case:
 
-    cp eclipse (eclipseDir)/eclipse  
-    cp eclipse_16xx.so (eclipseDir)/plugins/org.eclipse.equinox.launcher.<ws>.<os>.<arch>*/
+   export DEV_ECLIPSE="your/dev/eclipse"
+   make -f make_linux.mak clean dev_build_install
+   
diff --git a/features/org.eclipse.equinox.executable.feature/library/gtk/build.sh b/features/org.eclipse.equinox.executable.feature/library/gtk/build.sh
index 691eb40..f29fc9b 100755
--- a/features/org.eclipse.equinox.executable.feature/library/gtk/build.sh
+++ b/features/org.eclipse.equinox.executable.feature/library/gtk/build.sh
@@ -86,23 +86,10 @@
 				defaultOSArch="x86_64"
 				defaultJava=DEFAULT_JAVA_EXEC
 				[ -d /bluebird/teamswt/swt-builddir/JDKs/x86_64/jdk1.5.0 ] && defaultJavaHome="/bluebird/teamswt/swt-builddir/JDKs/x86_64/jdk1.5.0"
-
-				# Automatically figure out where java is installed on the system if it was not found/given otherwise.
-				if [ "$defaultJavaHome" = "" ]; then
-					DYNAMIC_JAVA_HOME=`readlink -f /usr/bin/java | sed "s:jre/::" | sed "s:bin/java::"`
-                                 	if [ -a "${DYNAMIC_JAVA_HOME}include/jni.h" ]; then
-        	                		echo "Info: Java automatically set to: $DYNAMIC_JAVA_HOME "
-						defaultJavaHome="$DYNAMIC_JAVA_HOME"
-					else
-						echo "Warning, java not found on system. Please specify JAVA_HOME manually"
-					fi
-				fi
-				OUTPUT_DIR="$EXEC_DIR/bin/$defaultWS/$defaultOS/$defaultOSArch"
 				;;
 			i?86 | "x86")
 				defaultOSArch="x86"
 				[ -d /bluebird/teamswt/swt-builddir/JDKs/x86/ibm-java2-i386-50 ] && defaultJavaHome="/bluebird/teamswt/swt-builddir/JDKs/x86/ibm-java2-i386-50"
-				OUTPUT_DIR="$EXEC_DIR/bin/$defaultWS/$defaultOS/$defaultOSArch"
 				;;
 			"ppc")
 				defaultOSArch="ppc"
@@ -265,12 +252,6 @@
 		M_ARCH=-m64
 		export M_ARCH
 	fi
-elif [ "$defaultOS" = "linux" ]; then
-	if [ "$defaultOSArch" = "x86_64" ]; then
-		# Bug 517013: Avoid using memcpy() to remain compatible with older glibc
-		M_CFLAGS="-fno-builtin-memcpy -fno-builtin-memmove"
-		export M_CFLAGS
-	fi
 fi
 
 LIBRARY_DIR="$EXEC_DIR/../org.eclipse.equinox.launcher.$defaultWS.$defaultOS.$defaultOSArch"
diff --git a/features/org.eclipse.equinox.executable.feature/library/gtk/make_linux.mak b/features/org.eclipse.equinox.executable.feature/library/gtk/make_linux.mak
index 1167a63..89c8031 100644
--- a/features/org.eclipse.equinox.executable.feature/library/gtk/make_linux.mak
+++ b/features/org.eclipse.equinox.executable.feature/library/gtk/make_linux.mak
@@ -2,45 +2,87 @@
 # Copyright (c) 2000, 2018 IBM Corporation and others.
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
-# which accompanies this distribution, and is available at 
+# which accompanies this distribution, and is available at
 # http://www.eclipse.org/legal/epl-v10.html
-# 
+#
 # Contributors:
 #     IBM Corporation - initial API and implementation
 #     Kevin Cornell (Rational Software Corporation)
 #     Tom Tromey (Red Hat, Inc.)
+#     Leo Ufimtsev (Red Hat, Inc) 2018
 #*******************************************************************************
+
 include ../make_version.mak
 # Makefile for creating the GTK eclipse launcher program.
-#
-# This makefile expects the utility "pkg-config" to be in the PATH.
 
-# This makefile expects the following environment variables set:
-#
+# Can work on it's own or reached from build.sh.
+# Common invocations:
+# make -f make_linux.mak clean all
+# make -f make_linux.mak clean all install # Install as part of eclipse build.
+# make -f make_linux.mak clean all dev_build_install   # For development/testing of launcher, install into your development eclipse, see target below.
+
+# This makefile expects the utility "pkg-config" to be in the PATH.
+# This makefile expects the following environment variables be set. If they are not set, it will figure out reasonable defaults targeting linux build.
 # PROGRAM_OUTPUT  - the filename of the output executable
 # DEFAULT_OS      - the default value of the "-os" switch
 # DEFAULT_OS_ARCH - the default value of the "-arch" switch
 # DEFAULT_WS      - the default value of the "-ws" switch
-# JAVA_HOME      - JAVA_HOME for jni headers      
-#default value for PROGRAM_OUTPUT
-ifeq ($(PROGRAM_OUTPUT),)
-  PROGRAM_OUTPUT=eclipse
+# JAVA_HOME      - JAVA_HOME for jni headers
+# M_ARCH & M_CFLAGS - architecture/cflags for gcc command (if relevant)
+
+# Environment Variables:
+DEFAULT_OS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
+DEFAULT_WS ?= gtk
+DEFAULT_OS_ARCH ?= $(shell uname -m)
+JAVA_HOME ?= $(shell readlink -f /usr/bin/java | sed "s:jre/::" | sed "s:bin/java::")
+PROGRAM_OUTPUT ?= eclipse
+PROGRAM_LIBRARY = $(PROGRAM_OUTPUT)_$(LIB_VERSION).so
+
+EXEC_DIR ?= ../../../../../rt.equinox.binaries/org.eclipse.equinox.executable
+OUTPUT_DIR ?= $(EXEC_DIR)/bin/$(DEFAULT_WS)/$(DEFAULT_OS)/$(DEFAULT_OS_ARCH)
+LIBRARY_DIR ?= $(EXEC_DIR)/../org.eclipse.equinox.launcher.$(DEFAULT_WS).$(DEFAULT_OS).$(DEFAULT_OS_ARCH)
+
+# Architecture specific Vars:
+# 32 bit specific flag:
+ifeq ($(M_ARCH),)
+ifeq ($(DEFAULT_OS_ARCH),x86)
+M_ARCH ?= -m32
+endif
 endif
 
-PROGRAM_LIBRARY=$(PROGRAM_OUTPUT)_$(LIB_VERSION).so
-
-ifeq ($(DEFAULT_JAVA),)
-  DEFAULT_JAVA=DEFAULT_JAVA_JNI
+# 64 bit specific flag:
+ifeq ($(M_CFLAGS),)
+ifeq ($(DEFAULT_OS_ARCH),x86_64)
+# Bug 517013: Avoid using memcpy() to remain compatible with older glibc (not use these flags on 32bit.
+M_CFLAGS ?= -fno-builtin-memcpy -fno-builtin-memmove
 endif
+endif
+
+# Determine launch mode.
+ifeq ($(DEFAULT_OS_ARCH),x86_64)
+DEFAULT_JAVA ?= DEFAULT_JAVA_EXEC
+else ifeq ($(DEFAULT_OS_ARCH),x86)
+DEFAULT_JAVA ?= DEFAULT_JAVA_JNI
+endif
+
+CC ?= gcc
+
+# Useful to figure out if there is any difference between running build.sh and make_linux directly.
+INFO_PROG=CC:$(CC)  PROGRAM_OUTPUT:$(PROGRAM_OUTPUT)  PROGRAM_LIBRARY:$(PROGRAM_LIBRARY) #
+INFO_ARCH=DEFAULT_OS:$(DEFAULT_OS)  DEFAULT_WS:$(DEFAULT_WS)  DEFAULT_OS_ARCH:$(DEFAULT_OS_ARCH)  M_ARCH:$(M_ARCH)  M_CFLAGS:$(M_CFLAGS) #
+INFO_JAVA=JAVA_HOME:$(JAVA_HOME)  DEFAULT_JAVA:$(DEFAULT_JAVA) #
+INFO_OUT_LIB_DIRS=OUTPUT_DIR:$(OUTPUT_DIR)  LIBRARY_DIR:$(LIBRARY_DIR) #
+$(info Input info: $(INFO_PROG) $(INFO_ARCH) $(INFO_JAVA) $(INFO_OUT_LIB_DIRS))
 
 # Define the object modules to be compiled and flags.
-CC?=gcc
 MAIN_OBJS = eclipseMain.o
 COMMON_OBJS = eclipseConfig.o eclipseCommon.o eclipseGtkCommon.o eclipseGtkInit.o
 DLL_OBJS	= eclipse.o eclipseGtk.o eclipseUtil.o eclipseJNI.o eclipseShm.o eclipseNix.o
 
 EXEC = $(PROGRAM_OUTPUT)
+# DLL == 'eclipse_XXXX.so'
 DLL = $(PROGRAM_LIBRARY)
+
 #LIBS = `pkg-config --libs-only-L gtk+-2.0` -lgtk-x11-2.0 -lgdk_pixbuf-2.0 -lgobject-2.0 -lgdk-x11-2.0 -lpthread -ldl
 LIBS = -lpthread -ldl
 GTK_LIBS = \
@@ -67,27 +109,28 @@
 eclipse.o: ../eclipse.c ../eclipseOS.h ../eclipseCommon.h ../eclipseJNI.h
 	$(CC) $(CFLAGS) -c ../eclipse.c -o eclipse.o
 
-eclipseMain.o: ../eclipseUnicode.h ../eclipseCommon.h ../eclipseMain.c 
+eclipseMain.o: ../eclipseUnicode.h ../eclipseCommon.h ../eclipseMain.c
+	$(info Starting Build:)
 	$(CC) $(CFLAGS) -c ../eclipseMain.c -o eclipseMain.o
-	
+
 eclipseCommon.o: ../eclipseCommon.h ../eclipseUnicode.h ../eclipseCommon.c
 	$(CC) $(CFLAGS) -c ../eclipseCommon.c
 
 eclipseGtkCommon.o: ../eclipseCommon.h ../eclipseOS.h eclipseGtk.h eclipseGtkCommon.c
 	$(CC) $(CFLAGS) -c eclipseGtkCommon.c -o eclipseGtkCommon.o
-	
+
 eclipseGtkInit.o: ../eclipseCommon.h eclipseGtk.h eclipseGtkInit.c
 	$(CC) $(CFLAGS) -c eclipseGtkInit.c -o eclipseGtkInit.o
-	
+
 eclipseUtil.o: ../eclipseUtil.c ../eclipseUtil.h ../eclipseOS.h
 	$(CC) $(CFLAGS) -c ../eclipseUtil.c -o eclipseUtil.o
 
 eclipseJNI.o: ../eclipseJNI.c ../eclipseCommon.h ../eclipseOS.h ../eclipseJNI.h
 	$(CC) $(CFLAGS) -c ../eclipseJNI.c -o eclipseJNI.o
-	
+
 eclipseConfig.o: ../eclipseConfig.c ../eclipseConfig.h ../eclipseOS.h
 	$(CC) $(CFLAGS) -c ../eclipseConfig.c -o eclipseConfig.o
-	
+
 eclipseShm.o: ../eclipseShm.h ../eclipseUnicode.h ../eclipseShm.c
 	$(CC) $(CFLAGS) -c ../eclipseShm.c -o eclipseShm.o
 
@@ -95,9 +138,11 @@
 	$(CC) $(CFLAGS) -c ../eclipseNix.c -o eclipseNix.o
 
 $(EXEC): $(MAIN_OBJS) $(COMMON_OBJS)
+	$(info Linking and generating: $(EXEC))
 	$(CC) ${M_ARCH} -o $(EXEC) $(MAIN_OBJS) $(COMMON_OBJS) $(LIBS)
-	
+
 $(DLL): $(DLL_OBJS) $(COMMON_OBJS)
+	$(info Linking and generating: $(DLL))
 	$(CC) $(LFLAGS) -o $(DLL) $(DLL_OBJS) $(COMMON_OBJS) $(LIBS)
 
 install: all
@@ -106,4 +151,26 @@
 	rm -f $(EXEC) $(DLL) $(MAIN_OBJS) $(COMMON_OBJS) $(DLL_OBJS)
 
 clean:
+	$(info Clean up:)
 	rm -f $(EXEC) $(DLL) $(MAIN_OBJS) $(COMMON_OBJS) $(DLL_OBJS)
+
+# Convienience method to install produced output into a developer's eclipse for testing/development.
+dev_build_install: all
+ifeq "$(origin DEV_ECLIPSE)" "environment"
+	$(info Copying $(EXEC) and $(DLL) into your development eclipse folder:)
+	cp $(EXEC) ${DEV_ECLIPSE}/
+	cp $(DLL) ${DEV_ECLIPSE}/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_*/
+else
+	$(error $(DEV_INSTALL_ERROR_MSG))
+endif
+
+define DEV_INSTALL_ERROR_MSG =
+Note:
+   DEV_ECLIPSE environmental variable is not defined.
+   You can download an integration build eclipse for testing and set DEV_ECLIPSE to point to it's folder
+   as per output of 'pwd'. Note, without trailing forwardslash. Integration build can be downloaded here:
+   See: http://download.eclipse.org/eclipse/downloads/
+   That way you can automatically build and copy eclipse and eclipse_XXXX.so into the relevant folders for testing. 
+   E.g: you can put something like the following into your .bashrc
+   export DEV_ECLIPSE="/home/YOUR_USER/Downloads/eclipse-SDK-I20YYMMDD-XXXX-linux-gtk-x86_64/eclipse"
+endef
diff --git a/features/org.eclipse.equinox.executable.feature/library/gtk/makefile b/features/org.eclipse.equinox.executable.feature/library/gtk/makefile
deleted file mode 100644
index 9cd1a6b..0000000
--- a/features/org.eclipse.equinox.executable.feature/library/gtk/makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-all:
-	./build.sh
-
-clean:
-	./build.sh clean
-
-# For devs:
-# Convienience target to move the launcher and the relevant *.so into your development eclipse folder for testing/verification.
-# Typically, you can download the latest integration build from here: http://download.eclipse.org/eclipse/downloads/, and use it as your dev eclipse.
-devinstall:
-	# You should define "DEV_ECLIPSE" to be the directory of your testing eclipse, containing the 'eclipse' executable.
-	# I.e the output of 'pwd' (Note: without trailing forward slash).
-	# export DEV_ECLIPSE="/home/YOUR_USER/Downloads/eclipse-SDK-I20180115-2000-linux-gtk-x86_64/eclipse"
-	cp eclipse ${DEV_ECLIPSE}/
-	cp eclipse_*.so ${DEV_ECLIPSE}/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_*/