#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
#ident	"$Id: Makefile,v 1.3 2007/08/01 22:18:34 chravel Exp $"
#

###################################################################
# Makefile to create packages and patches for NSPR on Solaris     #
###################################################################

# where are we in the source tree ?
MOZILLA_DIR = ../../../..
MOZILLA_ABS := $(shell cd $(MOZILLA_DIR);pwd)
CUR_DIR := $(shell pwd)

# WARNING: uses NSS config files here because it is much simplier (no autoconf)
CORE_DEPTH := $(MOZILLA_DIR)/nss
include $(CORE_DEPTH)/coreconf/config.mk

# Get actual version of NSPR
VERSION := $(shell grep PR_VERSION $(MOZILLA_ABS)/dist/$(OBJDIR)/include/prinit.h \
		   | sed -e 's/"$$//' -e 's/.*"//' -e 's/ .*//')
Timestamp := $(shell /bin/date +%Y.%m.%d.%H.%M)

# source directories

ifeq ($(USE_64), 1)
        OBJDIR64 = $(OBJDIR)
        OBJDIR32 = $(shell echo $(OBJDIR) | sed -e "s|_64_OPT|_OPT|g" -e "s|_64_DBG|_DBG|g")
else
        OBJDIR32 = $(OBJDIR)
        OBJDIR64 = $(shell echo $(OBJDIR) | sed -e "s|_OPT|_64_OPT|g" -e "s|_DBG|_64_DBG|g")
endif
DIST32 := $(MOZILLA_ABS)/dist/$(OBJDIR32)
DIST64 := $(MOZILLA_ABS)/dist/$(OBJDIR64)

COPYRIGHT := $(CUR_DIR)/copyright

# destination directory
BUILD_DIR := $(CUR_DIR)/$(OBJDIR)
ROOT := $(BUILD_DIR)/ROOT
PKG_DIR := $(MOZILLA_ABS)/dist/$(OBJDIR)/pkgarchive

# List of packages to build
PKG_LIST := \
	SUNWpr \
	SUNWprd
PROTO_LIST := $(addsuffix _proto,$(PKG_LIST))
PKGINFO_LIST := $(addsuffix _pkginfo,$(PKG_LIST))
DEPEND_LIST := $(addsuffix _depend,$(PKG_LIST))

# Architecture depend section
MACH := $(shell mach)

ifeq ($(MACH), sparc)
# SPARC
	DIR64 := sparcv9
	PROTO_KEEP := \#SPARC\#
	PROTO_DEL := \#AMD64\#
else
# AMD64
	DIR64 := amd64
	PROTO_KEEP := \#AMD64\#
	PROTO_DEL := \#SPARC\#
endif

# Layout of the packages
PROTO := \
	$(ROOT) \
	$(ROOT)/usr/lib/mps \
	$(ROOT)/usr/include/mps \
	$(ROOT)/usr/lib/mps/$(DIR64) \
	$(ROOT)/usr/lib/pkgconfig

ifeq ($(MACH), sparc)
	PROTO += $(ROOT)/usr/lib/mps/cpu/sparcv8plus
endif

# Copy and layout the bits to package ---------------------------------------
$(ROOT):
	mkdir -p $@

$(ROOT)/usr/lib/mps:
	mkdir -p $@
	cp $(DIST32)/lib/*.so $@
$(ROOT)/usr/include/mps:
	mkdir -p $@
	cp -r $(DIST32)/include/* $@
$(ROOT)/usr/lib/mps/$(DIR64):
	mkdir -p $@
	cp $(DIST64)/lib/*.so $@
$(ROOT)/usr/lib/mps/cpu/sparcv8plus:
	mkdir -p $@
	cp $(DIST32)/lib/cpu/sparcv8plus/*.so $@
$(ROOT)/usr/lib/pkgconfig: nspr.pc
	mkdir -p $@
	cp $(BUILD_DIR)/$< $@

# ============== below is independent of the product to build ==============
# List of targets ------------------------------
package: $(PROTO) $(PKG_DIR) $(PKG_LIST)
	@echo "NSPR packages done."
	@echo "-----------------------------------------------"
	
%: %_proto %_pkginfo %_depend
	@echo "==============================================="
	@echo "Building package $@"
	@echo "==============================================="
	pkgmk -f $(BUILD_DIR)/$< -d $(PKG_DIR) -r $(ROOT) -o $@
	@echo "==============================================="
	@echo "Package is in $(PKG_DIR)/$@
	@echo "==============================================="

# prototype file
%_proto: %_proto.tmpl
	@echo "Creating prototype $@ from template $<"
	sed \
	  -e "s|#DIR64#|$(DIR64)|g" \
	  -e "s|$(PROTO_KEEP)||g" \
	  -e "/$(PROTO_DEL)/d" \
	  -e "s|#BUILD_DIR#|$(BUILD_DIR)|g" \
	  -e "s|#COPYRIGHT#|$(COPYRIGHT)|g" \
	 $< > $(BUILD_DIR)/$@

# pkginfo file
%_pkginfo: %_pkginfo.tmpl
	@echo "Creating pkginfo $@ from template $<"
	sed \
	  -e "s/#MACH#/$(MACH)/g" \
	  -e "s/#PKGVER#/$(VERSION)/g" \
	  -e "s/#PKGREV#/$(Timestamp)/g" \
	$< > $(BUILD_DIR)/$@
	
# depend file
%_depend: %_depend.tmpl
	@echo "Creating depend $@ from template $<"
	cp $< $(BUILD_DIR)/$@

# .pc file (pkgconfig)
%.pc: %.pc.tmpl
	@echo "Creating pkgconfig file $@ from template $<"
	sed \
	  -e "s|#VERSION#|$(VERSION)|g" \
	 $< > $(BUILD_DIR)/$@

# Cleanup target
clean clobber::
	rm -r $(BUILD_DIR)
	rm -r $(PKG_DIR)

# mkdir targets
$(PKG_DIR) $(BUILD_DIR):
	mkdir -p $@

