#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)Makefile	1.5	99/11/24 SMI"
#
# cmd/perl5/distrib/Makefile
#
# The perl5 installation for Solaris differs from that generated by the
# Perl Configure procedure.  The configuration appropriate for the current
# build is automatically generated from pre-existing configuration files
# for the build platform.
#
include ../../Makefile.cmd
include ../Makefile.locns

PROG= perl

CLOBBERFILES= miniperl
CLEANFILES= perlmain.o

LLPATH= LD_LIBRARY_PATH=`pwd`

CP= /usr/bin/cp
GREP=/usr/bin/grep
INSTALL= /usr/sbin/install
MKDIR= /usr/bin/mkdir
MKFILE= /usr/sbin/mkfile
SED= /usr/bin/sed
SH= /usr/bin/sh

DYNAMIC_EXT_DIRS= \
	ext/B \
	ext/Data/Dumper \
	ext/Fcntl \
	ext/IO \
	ext/IPC/SysV \
	ext/NDBM_File \
	ext/ODBM_File \
	ext/Opcode \
	ext/POSIX \
	ext/SDBM_File \
	ext/Socket \
	ext/attrs \
	ext/re \
	ext/Errno		# Errno produces no binary object, but is
				# platform dependent.

NON_XS_EXTS= \
	ext/Errno/pm_to_blib

TARGET = all

all :=		TARGET = all
install :=	TARGET = install
clobber :=	TARGET = clobber
clean :=	TARGET = clean
test :=		TARGET = test

# 
# Large file support is not present in 5.00503.  Determine the appropriate
# compilation flags once 5.6 has been released.
#
#CFLAGS += -DLARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64

#
# Default target
#
all: perl extensions utilities

#
# Phase Ia.  libperl.so
#   Build the libperl.so and libperl.so.1 shared objects using Makefile.lib,
#   which uses the correct library build environment.
#
libperl.so : config.h
	@echo "Building libperl.so"; \
	$(MAKE) -f Makefile.lib $(TARGET)

#
# Phase Ib.  miniperl
#   Build the bootstrap perl executable.
#
miniperl: miniperlmain.o libperl.so 
	$(LLPATH) $(LINK.c) -o $@ miniperlmain.o -L. -lperl
	$(LLPATH) ./miniperl -w -Ilib -MExporter -e 0 || \
	( echo "error building miniperl" && exit 2 )

miniperlmain.o: miniperlmain.c config.h
	$(COMPILE.c) -o $@ $<

#
# Phase IIa.  extensions, dynamic loading support
#
DYNALOADER=lib/auto/DynaLoader/DynaLoader.a

preplibrary: miniperl lib/Config.pm lib/ExtUtils/Miniperl.pm
	[ -d lib/auto ] || $(MKDIR) -p lib/auto
	$(LLPATH) ./miniperl -Ilib -e 'use AutoSplit; \
	    autosplit_lib_modules(@ARGV)' lib/*.pm lib/*/*.pm

$(DYNALOADER): miniperl preplibrary fake_cflags
	@TOP=`pwd` export TOP; \
	$(LLPATH) export LD_LIBRARY_PATH; \
	cd ext/DynaLoader; pwd; \
	$$TOP/miniperl -I$$TOP/lib Makefile.PL > /dev/null 2>&1; \
	unset KEEP_STATE VERSION; \
	$(MAKE) LINKTYPE=static CCCDLFLAGS= > /dev/null 2>&1; \
	[ -d $$TOP/lib/auto/DynaLoader ] || \
	    $(MKDIR) -p $$TOP/lib/auto/DynaLoader; \
	$(CP) blib/arch/auto/DynaLoader/DynaLoader.a \
	    $$TOP/lib/auto/DynaLoader; \
	$(CP) DynaLoader.pm $$TOP/lib

lib/Config.pm : lib/re.pm config.sh config.h
	$(LLPATH) ./miniperl configpm lib/Config.pm

lib/ExtUtils/Miniperl.pm: miniperlmain.c miniperl minimod.pl lib/Config.pm
	$(LLPATH) ./miniperl minimod.pl > $@

lib/re.pm : ext/re/re.pm
	$(CP) ext/re/re.pm $@

#
# Phase IIb.  extensions with binary components
#
extensions: miniperl fake_cflags .WAIT $(DYNAMIC_EXT_DIRS)

$(DYNAMIC_EXT_DIRS): FRC
	@TOP=`pwd` export TOP; \
	$(LLPATH) export LD_LIBRARY_PATH; \
	PERL5LIB=$$TOP/ext/DynaLoader export PERL5LIB; \
	. $$TOP/config.sh; \
	cd $@; pwd; \
	$$TOP/miniperl -I$$TOP/lib Makefile.PL INSTALLDIRS=perl \
	    > /dev/null 2>&1; \
	unset KEEP_STATE VERSION; \
	$(MAKE) LINKTYPE=dynamic all > /dev/null 2>&1

#
# fake_cflags quiets our compilations of the various core extensions, which are
# otherwise upset by our slightly non-standard build of Perl.
#
fake_cflags:
	$(MKFILE) 1b cflags

FRC:

#
# Phase IIIa.  perl executable
#
perl: perlmain.o libperl.so $(DYNALOADER)
	$(LINK.c) -R$(PERL_REAL_LIB_DIR) -o $@ perlmain.o libperl.so \
	$(DYNALOADER) -lcrypt_i -lsocket -lnsl -ldl -lm -lc

perlmain.o: perlmain.c
	$(COMPILE.c) -o $@ $<

perlmain.c: miniperlmain.c
	$(SH) writemain $(DYNALOADER) > tmp
	$(SH) mv-if-diff tmp perlmain.c

#
# Phase IIIb.  utilities
#
utilities:
	@cd x2p; pwd; $(MAKE) $(TARGET)
	@cd pod; pwd; $(MAKE) $(TARGET)
	@cd utils; pwd; $(MAKE) $(TARGET)

#
# Phase IV.  install target
#   The bin and pod symlinks are created here.  The man link is created by the
#   SUNWpl5m package, which contains the SGML documentation.  Finally, we
#   overwrite the Config.pm we used to build our modules with one sanitized of
#   the /ws/on*-tools dependency.
#
install: all
	$(RM) -f $(PERL_CORE_DIR)/libperl.so
	@$(LLPATH) ./installperl
	@$(MAKE) -f Makefile.lib install_lib
	$(RM) -f $(ROOT)/usr/perl5/bin
	-$(LN) -s ./$(PERL_MAINTENANCE_VERSION)/bin $(ROOT)/usr/perl5/bin
	$(RM) -f $(ROOT)/usr/perl5/pod
	-$(LN) -s ./$(PERL_MAINTENANCE_VERSION)/pod $(ROOT)/usr/perl5/pod
	$(RM) -f $(ROOTPROG)
	-$(LN) $(ROOT)/usr/perl5/$(PERL_MAINTENANCE_VERSION)/bin/perl \
	    $(ROOTPROG)
	-$(SED) -e "s@\'$(CC)@\'cc@" ./lib/Config.pm \
	    > $(PERL_LIB_DIR)/$(PERL_ARCH)-solaris/Config.pm
	-$(CHOWN) -R $(OWNER):$(GROUP) $(PERL_ROOT) 

#
# Perl test suite targets.
#
test:
	@TOP=`pwd` export TOP; \
	cd t; pwd; \
	$(CP) ../perl . ; \
	LD_LIBRARY_PATH=$$TOP PERL5LIB=../lib ./TEST

#
# Cleaning targets.
#
clean_lib:
	$(MAKE) -f Makefile.lib $(TARGET)

clean_utilities:
	@cd utils; pwd; $(MAKE) clean

clean_x2p:
	@cd x2p; pwd; $(MAKE) clean

clean: clean_lib clean_x2p clean_utilities
	$(RM) -f $(CLEANFILES)

clobber_dynaloader:
	@TOP=`pwd` export TOP; \
	unset KEEP_STATE VERSION; \
	cd $$TOP/ext/DynaLoader; pwd; \
	if [ -f Makefile ]; then \
	    $(MAKE) realclean 2>&1 > /dev/null; \
	fi

clobber_extensions:
	@TOP=`pwd` export TOP; \
	unset KEEP_STATE VERSION; \
	for dir in $(DYNAMIC_EXT_DIRS); do \
        	cd $$TOP/$$dir; pwd; \
		if [ -f Makefile ]; then \
		    $(MAKE) realclean 2>&1 > /dev/null; \
		fi; \
	done

clobber_lib:
	$(MAKE) -f Makefile.lib $(TARGET)

clobber_pod:
	@cd pod; pwd; $(MAKE) clobber

clobber_utilities:
	@cd utils; pwd; $(MAKE) clobber

clobber_x2p:
	@cd x2p; pwd; $(MAKE) clobber

clobber: \
	clean clobber_dynaloader clobber_extensions clobber_lib clobber_pod \
	clobber_utilities clobber_x2p

include ../../Makefile.targ
