#
# Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

#
# Makefile for building the langtools workspace.
#

#
# On Solaris, the standard 'make' utility will not work with these makefiles.
#    This little rule is only understood by Solaris make, and is harmless
#    when seen by the GNU make tool. If using Solaris make, this causes the
#    make command to fail.
#
SUN_MAKE_TEST:sh = @echo "ERROR: PLEASE USE GNU VERSION OF MAKE"; exit 33

#----- cancel implicit rules

%: %.o
%: %.obj
%: %.dll
%: %.c
%: %.cc
%: %.C
%: %.p
%: %.f
%: %.s
%: %.F
%: %.r
%: %.S
%: %.mod
%: %.sh
%: %,v
%: RCS/%,v

#----- imports

ifdef ALT_BOOT_JAVA_HOME
  BOOT_JAVA_HOME = $(ALT_BOOT_JAVA_HOME)
else 
  ifdef ALT_BOOTDIR
    BOOT_JAVA_HOME = $(ALT_BOOTDIR)
  else
    BOOT_JAVA_HOME=/java/re/jdk/1.5.0/archive/fcs/binaries/solaris-sparc
  endif
endif

BOOT_JAVA=$(BOOT_JAVA_HOME)/bin/java
BOOT_JAVAC=$(BOOT_JAVA_HOME)/bin/javac
BOOTJAR=$(BOOT_JAVA_HOME)/bin/jar

ifdef ALT_TESTJAVA_HOME
  TESTJAVA_HOME = $(ALT_TESTJAVA_HOME)
else
  TESTJAVA_HOME=/java/re/jdk/1.6.0/archive/fcs/binaries/solaris-sparc
endif

TESTJAVA=$(TESTJAVA_HOME)/bin/java

ifdef ALT_FINDBUGS_HOME
  FINDBUGS_HOME = $(ALT_FINDBUGS_HOME)
else
  FINDBUGS_HOME = /java/devtools/share/findbugs/1.1.2-rc4
endif

FINDBUGS = $(FINDBUGS_HOME)/bin/findbugs

#----- commands

CHMOD = chmod
CP = cp
ECHO = echo # FIXME
FIND = find
MKDIR = mkdir
SED = sed
ZIP = zip

#-----  locations and deliverables

TOPDIR = ..
SRC_BIN_DIR = $(TOPDIR)/src/share/bin
SRC_CLASSES_DIR = $(TOPDIR)/src/share/classes

BUILD_DIR = $(TOPDIR)/build

CLASSES_DIR = $(BUILD_DIR)/classes
GENSRC_DIR = $(BUILD_DIR)/gensrc

DIST_DIR = $(TOPDIR)/dist
BIN_DIR = $(DIST_DIR)/bin
LIB_DIR = $(DIST_DIR)/lib

JAVAC_JAR = $(LIB_DIR)/javac.jar
JAVADOC_JAR = $(LIB_DIR)/javadoc.jar
JAVAH_JAR = $(LIB_DIR)/javah.jar
JAVAP_JAR = $(LIB_DIR)/javap.jar
APT_JAR = $(LIB_DIR)/apt.jar

CLASSES_JAR = $(DIST_DIR)/classes.jar
SRC_ZIP = $(DIST_DIR)/src.zip

BUILDTOOLSRC_DIR = tools
BUILDTOOLCLASSES_DIR = $(BUILD_DIR)/toolclasses

#-----

ifndef JDK_MAJOR_VERSION
  JDK_MAJOR_VERSION = 1
endif

ifndef JDK_MINOR_VERSION
  JDK_MINOR_VERSION = 7
endif

ifndef JDK_MICRO_VERSION
  JDK_MICRO_VERSION = 0
endif

ifndef JDK_VERSION
  JDK_VERSION = $(JDK_MAJOR_VERSION).$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION)
endif

ifndef MILESTONE
  MILESTONE = internal
endif

# RELEASE is JDK_VERSION and -MILESTONE if MILESTONE is set
ifneq ($(MILESTONE),fcs)
  RELEASE      = $(JDK_VERSION)-$(MILESTONE)$(BUILD_VARIANT_RELEASE)
else
  RELEASE      = $(JDK_VERSION)$(BUILD_VARIANT_RELEASE)
endif

# FULL_VERSION is RELEASE and -BUILD_NUMBER if BUILD_NUMBER is set
ifdef BUILD_NUMBER
  FULL_VERSION = $(RELEASE)-$(BUILD_NUMBER)
else
  BUILD_NUMBER = b00
  USER_RELEASE_SUFFIX := $(shell echo $(USER)_`date '+%d_%b_%Y_%H_%M' | tr "A-Z" "a-z"`)
  FULL_VERSION = $(RELEASE)-$(USER_RELEASE_SUFFIX)-$(BUILD_NUMBER)
endif

#----- useful macros

TOOLS =  javac javadoc javah javap apt

SOURCE_LEVEL = 5
BOOTSTRAP_TARGET_LEVEL = 5
TARGET_LEVEL = 6

ifndef TARGET_JAVA
  TARGET_JAVA = java
endif

NO_PROPRIETARY_API_WARNINGS = -XDignore.symbol.file=true

SELF = $(lastword $(MAKEFILE_LIST))

#-----

# the default is to generate the following:
# dist/{bin,lib}: 
# 	lang tools compiled to run on the target JDK

default:  
	$(MAKE) -f $(SELF) \
		MILESTONE=bootstrap \
		TARGET_LEVEL=$(BOOTSTRAP_TARGET_LEVEL) \
		TARGET_JAVA=$(BOOT_JAVA_HOME)/bin/java \
		GENSRC_DIR=$(BUILD_DIR)/bootstrap/gensrc \
		CLASSES_DIR=$(BUILD_DIR)/bootstrap/classes \
		BIN_DIR=$(BUILD_DIR)/bootstrap/bin \
		LIB_DIR=$(BUILD_DIR)/bootstrap/lib \
		$(BUILD_DIR)/bootstrap/lib/javac.jar \
		$(BUILD_DIR)/bootstrap/bin/javac
	$(MAKE) -f $(SELF) \
		BOOT_JAVAC=$(BUILD_DIR)/bootstrap/bin/javac \
		tools

# for jdk, we generate the following:
# dist/bootstrap/{bin,lib}:
#	lang tools compiled to run on the boot JDK
# dist/lib/classes.jar:
#	lang tools recompiled to run on the target JDK,
#	ready for inclusion in rt.jar and tools.jar
# dist/lib/src.zip
#	.properties and .java files for classes in classes.jar,
#	ready for jdk src.zip 

jdk:
	$(MAKE) -f $(SELF) \
		MILESTONE=bootstrap \
		TARGET_LEVEL=$(BOOTSTRAP_TARGET_LEVEL) \
		TARGET_JAVA=$(BOOT_JAVA_HOME)/bin/java \
		GENSRC_DIR=$(BUILD_DIR)/bootstrap/gensrc \
		CLASSES_DIR=$(BUILD_DIR)/bootstrap/classes \
		BIN_DIR=$(DIST_DIR)/bootstrap/bin \
		LIB_DIR=$(DIST_DIR)/bootstrap/lib \
		tools
	$(MAKE) -f $(SELF) \
		BOOT_JAVAC=$(DIST_DIR)/bootstrap/bin/javac \
		LIB_DIR=$(BUILD_DIR)/jdk/lib \
		$(DIST_DIR)/lib/classes.jar \
		$(DIST_DIR)/lib/src.zip

tools: $(TOOLS:%=$(LIB_DIR)/%.jar) $(TOOLS:%=$(BIN_DIR)/%)

clean:
	$(RM) -r $(BUILD_DIR)

really-clean: clean
	$(RM) -r $(DIST_DIR)

jprt_product_build \
jprt_debug_build \
jprt_fastdebug_build: lib

#-----  javac

JAVAC_DIRS = \
	javax/annotation/processing \
	javax/lang/model \
	javax/tools \
	com/sun/source \
	com/sun/tools/javac 

JAVAC_RESOURCE_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAC_DIRS)) -name SCCS -prune -o -name \*.properties -print )

JAVAC_JAVA_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAC_DIRS)) -name SCCS -prune -o -name \*.java -print ) \
	$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(JAVAC_RESOURCE_FILES)) \
	$(GENSRC_DIR)/com/sun/tools/javac/resources/version.java

$(JAVAC_JAR): $(JAVAC_JAVA_FILES)
	$(MKDIR) -p $(CLASSES_DIR) $(@D)
	$(BOOT_JAVAC) -d $(CLASSES_DIR) -target $(TARGET_LEVEL) $(NO_PROPRIETARY_API_WARNINGS) $(JAVAC_JAVA_FILES)
	( $(ECHO) Main-Class: com.sun.tools.javac.Main ) > $(BUILD_DIR)/javac.mf
	$(BOOTJAR) -cfm $@ $(BUILD_DIR)/javac.mf $(patsubst %,-C $(CLASSES_DIR) %, $(JAVAC_DIRS))

#----- javadoc

### FIXME -- javadoc has a couple of extra non-property resource files
### that need to be included

JAVADOC_DIRS = \
	com/sun/javadoc \
	com/sun/tools/doclets \
	com/sun/tools/javadoc

JAVADOC_RESOURCE_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVADOC_DIRS)) -name SCCS -prune -o -name \*.properties -print )

JAVADOC_JAVA_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVADOC_DIRS)) -name SCCS -prune -o -name \*.java -print ) \
	$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(JAVADOC_RESOURCE_FILES))
 
$(JAVADOC_JAR): $(JAVADOC_JAVA_FILES) $(JAVAC_JAR)
	$(MKDIR) -p $(CLASSES_DIR) $(@D)
	$(BOOT_JAVAC) -sourcepath "" -classpath $(JAVAC_JAR) -d $(CLASSES_DIR) -target $(TARGET_LEVEL) $(JAVADOC_JAVA_FILES)
	( $(ECHO) Main-Class: com.sun.tools.javadoc.Main ; $(ECHO) Class-Path: javac.jar ) > $(BUILD_DIR)/javadoc.mf
	$(BOOTJAR) -cfm $@ $(BUILD_DIR)/javadoc.mf $(patsubst %,-C $(CLASSES_DIR) %, $(JAVADOC_DIRS))

#----- javah

JAVAH_DIRS = \
	com/sun/tools/javah

JAVAH_RESOURCE_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAH_DIRS)) -name SCCS -prune -o -name \*.properties -print )

JAVAH_JAVA_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAH_DIRS)) -name SCCS -prune -o -name \*.java -print ) \
	$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(JAVAH_RESOURCE_FILES))
 
$(JAVAH_JAR): $(JAVAH_JAVA_FILES) $(JAVADOC_JAR)
	$(MKDIR) -p $(CLASSES_DIR) $(@D)
	$(BOOT_JAVAC) -sourcepath "" -classpath $(CLASSES_DIR) -d $(CLASSES_DIR) -target $(TARGET_LEVEL) $(JAVAH_JAVA_FILES)
	( $(ECHO) Main-Class: com.sun.tools.javah.Main ; $(ECHO) Class-Path: javadoc.jar ) > $(BUILD_DIR)/javah.mf
	$(BOOTJAR) -cfm $@ $(BUILD_DIR)/javah.mf $(patsubst %,-C $(CLASSES_DIR) %, $(JAVAH_DIRS))

#----- javap

JAVAP_DIRS = \
	sun/tools/javap

JAVAP_RESOURCE_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAP_DIRS)) -name SCCS -prune -o -name \*.properties -print )

JAVAP_JAVA_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(JAVAP_DIRS)) -name SCCS -prune -o -name \*.java -print ) \
	$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(JAVAP_RESOURCE_FILES))
 
$(JAVAP_JAR): $(JAVAP_JAVA_FILES) \
		$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(JAVAP_RESOURCE_FILES))
	$(MKDIR) -p $(CLASSES_DIR)
	$(BOOT_JAVAC) -sourcepath "" -classpath $(CLASSES_DIR) -d $(CLASSES_DIR) -target $(TARGET_LEVEL) $(JAVAP_JAVA_FILES)
	( $(ECHO) Main-Class: sun.tools.javap.Main ) > $(BUILD_DIR)/javap.mf
	$(BOOTJAR) -cfm $@ $(BUILD_DIR)/javap.mf $(patsubst %,-C $(CLASSES_DIR) %, $(JAVAP_DIRS))

#----- apt

APT_DIRS = \
	com/sun/mirror \
	com/sun/tools/apt

APT_RESOURCE_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(APT_DIRS)) -name SCCS -prune -o -name \*.properties -print )

APT_JAVA_FILES = \
	$(shell find $(patsubst %,$(SRC_CLASSES_DIR)/%,$(APT_DIRS)) -name SCCS -prune -o -name \*.java -print ) \
	$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(APT_RESOURCE_FILES))
 
$(APT_JAR): $(APT_JAVA_FILES) $(JAVAC_JAR) \
		$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(APT_RESOURCE_FILES))
	$(MKDIR) -p $(CLASSES_DIR) $(@D)
	$(BOOT_JAVAC) -sourcepath "" -classpath $(CLASSES_DIR) -d $(CLASSES_DIR) -target $(TARGET_LEVEL) $(NO_PROPRIETARY_API_WARNINGS) \
		$(APT_JAVA_FILES) \
		$(patsubst $(SRC_CLASSES_DIR)/%.properties,$(GENSRC_DIR)/%.java,$(APT_RESOURCE_FILES))
	( $(ECHO) Main-Class: com.sun.tools.apt.Main ; $(ECHO) Class-Path: javac.jar ) > $(BUILD_DIR)/apt.mf
	$(BOOTJAR) -cfm $@ $(BUILD_DIR)/apt.mf $(patsubst %,-C $(CLASSES_DIR) %, $(APT_DIRS))

#-----

build-tools: $(BUILDTOOLCLASSES_DIR)/CompileProperties/CompileProperties.class

$(GENSRC_DIR)/%.java: $(SRC_CLASSES_DIR)/%.properties $(BUILDTOOLCLASSES_DIR)/CompileProperties/CompileProperties.class
	$(MKDIR) -p $(@D)
	$(BOOT_JAVA) -cp $(BUILDTOOLCLASSES_DIR)/CompileProperties CompileProperties $< $(patsubst $(CLASSES_DIR)/%.class,$(GENSRC_DIR)/%.java,$@)

$(GENSRC_DIR)/%.java: $(GENSRC_DIR)/%.properties $(BUILDTOOLCLASSES_DIR)/CompileProperties/CompileProperties.class
	$(MKDIR) -p $(@D)
	$(BOOT_JAVA) -cp $(BUILDTOOLCLASSES_DIR)/CompileProperties CompileProperties $< $(patsubst $(CLASSES_DIR)/%.class,$(GENSRC_DIR)/%.java,$@)

$(GENSRC_DIR)/%.properties: $(SRC_CLASSES_DIR)/%.properties-template
	$(MKDIR) -p $(@D)
	$(SED)  -e 's/$$(JDK_VERSION)/$(JDK_VERSION)/'  \
		-e 's/$$(FULL_VERSION)/$(FULL_VERSION)/' \
		-e 's/$$(RELEASE)/$(RELEASE)/' \
		< $< > $@

$(BUILDTOOLCLASSES_DIR)/%.class : $(BUILDTOOLSRC_DIR)/%.java
	$(MKDIR) -p $(@D)
	$(BOOT_JAVAC) -d $(@D) $<

#----- all classes

$(DIST_DIR)/%/classes.jar: $(JAVAC_JAR) $(JAVADOC_JAR) $(JAVAH_JAR) $(JAVAP_JAR) $(APT_JAR)
	$(MKDIR) -p $(@D)
	$(BOOTJAR) -cf $@ -C $(CLASSES_DIR) .

#----- src.zip

SRC_ZIP_FILES = $(shell $(FIND) $(SRC_CLASSES_DIR) \( -name SCCS -o -name \*-template \) -prune -o -type f -print )

$(DIST_DIR)/%/src.zip: $(SRC_ZIP_FILES)
	abs_src_zip=`cd $(@D) ; pwd`/$(@F) ; \
	( cd $(SRC_CLASSES_DIR) ; $(FIND) . \( -name SCCS -o -name \*-template \) -prune -o -type f -print | $(ZIP) -q $$abs_src_zip -@ ) ; \
	( cd $(SRC_CLASSES_DIR) ; $(FIND) . -name SCCS -prune -o -name \*-template -print | $(SED) -e 's/-template//' ) | ( cd $(GENSRC_DIR) ; $(ZIP) -q $$abs_src_zip -@ ) 

#----- bin files

$(BIN_DIR)/%: $(SRC_BIN_DIR)/launcher.sh-template
	$(MKDIR) -p $(@D)
	$(SED) -e 's|#PROGRAM#|$(@F)|' -e 's|#TARGET_JAVA#|$(TARGET_JAVA)|' $< > $@
	$(CHMOD) +x $@

#-----

findbugs: $(BUILD_DIR)/findbugs.txt

$(BUILD_DIR)/findbugs.txt: $(CLASSES_JAR)
	$(MKDIR) -p $(@D)
	$(FINDBUGS) -textui -javahome $(BOOT_JAVA_HOME) -high -emacs -outputFile $@ $<

#-----

quick-check: $(patsubst %, $(DIST_LIB_DIR)/%.jar, $(TOOLS))
	$(TESTJAVA) -jar $(JAVAC_JAR) -version
	- $(TESTJAVA) -jar $(JAVADOC_JAR) -version
	$(TESTJAVA) -jar $(JAVAH_JAR) -version
	- $(TESTJAVA) -jar $(JAVAP_JAR) -version
	$(TESTJAVA) -jar $(APT_JAR) -version

#-----

.PHONY: \
	all \
	build \
	build-tools \
	clean \
	default \
	findbugs \
	jprt_product_build \
	jprt_debug_build \
	jprt_fastdebug_build \
	tools

