# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Java XPCOM Bindings.
#
# The Initial Developer of the Original Code is IBM Corporation.
# Portions created by the Initial Developer are Copyright (C) 2006
# IBM Corporation. All Rights Reserved.
#
# Contributor(s):
#   Javier Pedemonte (jhpedemonte@gmail.com)
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

DEPTH		= ../../../..
topsrcdir	= @top_srcdir@
srcdir		= @srcdir@
VPATH		= @srcdir@

include $(DEPTH)/config/autoconf.mk

JAVA_LIBRARY_NAME = MozillaGlue

# Don't install MozillaGlue.jar in $(DIST)/bin
NO_DIST_INSTALL=1
NO_INSTALL=1

PACKAGE_DIR = org/mozilla/xpcom

JAVA_SRCS = \
		$(PACKAGE_DIR)/Mozilla.java \
		$(PACKAGE_DIR)/GREVersionRange.java \
		$(PACKAGE_DIR)/IMozilla.java \
		$(PACKAGE_DIR)/IGRE.java \
		$(PACKAGE_DIR)/IXPCOM.java \
		$(PACKAGE_DIR)/IJavaXPCOMUtils.java \
		$(PACKAGE_DIR)/IAppFileLocProvider.java \
		$(PACKAGE_DIR)/INIParser.java \
		$(PACKAGE_DIR)/VersionComparator.java \
		$(PACKAGE_DIR)/IXPCOMError.java \
		$(PACKAGE_DIR)/ProfileLock.java \
		$(PACKAGE_DIR)/XPCOMException.java \
		$(PACKAGE_DIR)/XPCOMInitializationException.java \
		$(NULL)

IFACES_JAR = MozillaInterfaces.jar
IFACES_JAR_SRC = $(patsubst %.jar,%-src.jar,$(IFACES_JAR))

JAVA_CLASSPATH += $(IFACES_JAR)

# install jars into SDK
SDK_LIBRARY = $(JAVA_LIBRARY_NAME).jar $(IFACES_JAR) $(IFACES_JAR_SRC)

GARBAGE += $(IFACES_JAR) $(IFACES_JAR_SRC) $(PACKAGE_DIR)/IXPCOMError.java java.files

include $(topsrcdir)/config/rules.mk

# MozillaGlue.jar depends on MozillaInterfaces.jar, so make sure to generate
# the Mozilla Java interfaces first.
$(addprefix $(_JAVA_DIR)/,$(JAVA_SRCS:.java=.class)): $(IFACES_JAR)

##
# Generate IXPCOMError.java
##

$(_JAVA_DIR)/$(PACKAGE_DIR)/Mozilla.class: $(PACKAGE_DIR)/IXPCOMError.java

$(PACKAGE_DIR)::
	$(NSINSTALL) -D $@

$(PACKAGE_DIR)/IXPCOMError.java: $(topsrcdir)/xpcom/base/nsError.h gen-nsError.pl $(PACKAGE_DIR) $(GLOBAL_DEPS)
	$(PERL) $(srcdir)/gen-nsError.pl < $< > $@

##
# Compile generated Java interfaces
##

IFACES_DIR = _ifaces
$(IFACES_DIR)::
	$(NSINSTALL) -D $@

GARBAGE_DIRS += $(IFACES_DIR)

# Don't include the interface nsIPref, since it depends on the obsolete
# interface nsIFileSpec.
IGNORE_IFACES_NAMES = nsIPref
ifneq (,$(IGNORE_IFACES_NAMES))
IGNORE_IFACES = $(patsubst %, ! -name "*%.java", $(IGNORE_IFACES_NAMES))
endif

# Using the temporary "java.files" avoids an "argument list too long"
# error on Windows when using *.java
$(IFACES_DIR)/$(JAVA_IFACES_PKG_NAME)/nsISupports.class: $(IFACES_DIR) $(wildcard $(JAVA_DIST_DIR)/default/$(JAVA_IFACES_PKG_NAME)/*.java) $(GLOBAL_DEPS)
	@echo Compiling Java interfaces
	find $(JAVA_DIST_DIR)/default -name "*.java" $(IGNORE_IFACES) > java.files
	$(CYGWIN_WRAPPER) $(JAVAC) $(JAVAC_FLAGS) -classpath $(IFACES_DIR) 		\
		-d $(IFACES_DIR) -sourcepath $(JAVA_DIST_DIR)/default @java.files

$(IFACES_JAR): $(IFACES_DIR)/$(JAVA_IFACES_PKG_NAME)/nsISupports.class Makefile
	$(JAR) cf $@ -C $(IFACES_DIR) org

$(IFACES_JAR_SRC): $(IFACES_JAR)
	$(JAR) cf $@ -C $(JAVA_DIST_DIR)/default org
	$(ZIP) -d $@ $(patsubst %, $(JAVA_IFACES_PKG_NAME)/%.java, $(IGNORE_IFACES_NAMES))

# Since we set NO_DIST_INSTALL above, the jars don't get copied into the SDK.
# Handle that here.
libs:: $(SDK_LIBRARY) $(SDK_LIB_DIR)
	$(INSTALL) $(IFLAGS2) $^
