# This file is read by Makefile.SH to produce rules for $(LIBPERL) (and
# some additional rules as well).

# Rerun `sh Makefile.SH; make depend' after making any change.

# Additional rules supported: libperls.a (for static linking),
# ld3 (!! NOT a typo !!), perlld (dynamic linking tools)
#

#! /bin/sh

TOP=`pwd`
case $PERL_CONFIG_SH in
  '')
    if test -f config.sh; then TOP=.;
    elif test -f ../config.sh; then TOP=..;
    elif test -f ../../config.sh; then TOP=../..;
    elif test -f ../../../config.sh; then TOP=../../..;
    elif test -f ../../../../config.sh; then TOP=../../../..;
    else echo "Can't find config.sh."; exit 1
    fi
    . $TOP/config.sh
    ;;
esac
addtopath=`pwd`

# --------------------------------------------------------------- #
#              PIECE 1 OF MAKEFILE                                #
# ----------------------------------------------------------------#
$spitshell >>Makefile <<!GROK!THIS!

##------------------------------------------------------------##
###        START of CYGWIN-CUSTOMIZED Makefile portion.      ###
##------------------------------------------------------------##

# GNU Makefile "punctuation" tricks.
cma = ,
nul=
spc= $(nul) #leave this here.

SYMD =
ifneq "${TOP}/" "\$(dir ${config_arg0})"
SYMD = \$(CURDIR)
# doesn't fix "config.h" problem. override OPTIMIZE += -I.
endif

cygwin.c: cygwin/cygwin.c
    \$(LNS) cygwin/cygwin.c

# added config.h here because "ld3" is an automatic target, made very early.
# 'make' wasn't creating config.h. Major pita.
ld3: ${src}/cygwin/ld3.in config.h
    @echo "extracting ld3 (with variable substitutions)..."
    @$sed s,@buildpath@,$addtopath,g <$<  >${TOP}/ld3
    @echo "done: successful extraction of ld3."


# shell script feeding perlld to decent perl  -- deprecated by ME.
ld2: $installbin perlld ${src}/cygwin/ld2.in
    @ if [ ! -d "$installbin" ] ; then \\
   /usr/bin/install -d -v "$installbin"; fi
    @echo "extracting ld2 (with variable substitutions)"
    @$sed s,@buildpath@,$addtopath,g <${src}/cygwin/ld2.in  >${TOP}/ld2
    @echo "installing ld2 into $installbin"
    @/usr/bin/install -c -m 755 ld2 ${installbin}/ld2
    @if [ ! -f  ${installbin}/ld2 ] ; then echo -e \\
************************************************* \n \\
Make will probably fail in a few more steps. \n \\
When it does, copy \"ld2\" to a directory in \n \\
your path, other than \".\". \n \\
\"/usr/local/bin\" or something similar will do. \n \\
Then restart make. \n \\
************************************************* \n ; \\
    else echo "done: successful extraction and installation of ld2." ; fi

!GROK!THIS!


# --------------------------------------------------------------- #
#              PIECE 2 OF MAKEFILE                                #
# ----------------------------------------------------------------#
$spitshell >>Makefile <<!GROK!THIS!

# perlld parameters

# this one is pretty mandatory <- HOOEY. Not anymore.
DLLWRAP = dllwrap
# following are optional.
WRAPDRIVER = gcc
DLLTOOL = dlltool
EXPORT_ALL =
# if some of extensions are empty,
# no corresponding output will be done.
# most probably, you'd like to have an export library
DEF_EXT = .def
EXP_EXT = .exp

# Note: can still use .DEF to build w/out using dllwrap.

perlld: Makefile ${src}/cygwin/perlld.in
    @echo "extracting perlld (with variable substitutions)"
    @$sed -e s,@CC@,\${CC}, -e s,@DLLWRAP@,\${DLLWRAP},g \\
   -e s,@WRAPDRIVER@,\${WRAPDRIVER},g -e s,@DLLTOOL@,\${DLLTOOL},g \\
   -e s,@AS@,\${AS},g -e s,@EXPORT_ALL@,\${EXPORT_ALL},g \\
   -e s,@DEF_EXT@,\${DEF_EXT},g -e s,@EXP_EXT@,\${EXP_EXT},g \\
   -e s,@LIB_EXT@,\${LIB_EXT},g \\
   ${src}/cygwin/perlld.in >perlld
    @echo -e "Done: successful creation of perlld.\n\n"


# we need to have ONE arbiter of the lib names, not "Makefile" vs. "perlld".
include mk_libnames

!GROK!THIS!

# make sure that all library names are not malformed
libperl=`echo $libperl|sed -e s,\\\..*,,`


# --------------------------------------------------------------- #
#              PIECE 3 OF MAKEFILE                                #
# ----------------------------------------------------------------#
$spitshell >>Makefile <<!GROK!THIS!

LIBSTEM   = \$(patsubst %\$(DLSUFFIX),%,\$(patsubst %\$(LIB_EXT),%,\$(LIBPERL)))
CLDFLAGS  = -L$addtopath $ldflags
# override CCCDLFLAGS = -DPERLDLL
CAT = $cat
AWK = $awk

.DELETE_ON_ERROR: mk_libnames
# Make sure this gets cleaned up at appropriate times.
override shextract += mk_libnames
# REMEMBER, "DLLNAME" & pals won't be defined yet, when we run this!
mk_libnames: Makefile ld3
    rm -f \$@
    @${TOP}/ld3 -libnames \$(LIBSTEM)\$(DLSUFFIX)
    @echo Successfully wrote \$@
.PHONY: check_libnames
check_libnames: ${TOP}/mk_libnames
    @echo The libraries to be created for this Perl are named:
    @cat $^

!GROK!THIS!

case "$useshrplib" in
  true)
# --------------------------------------------------------------- #
#          PIECE 4 OF MAKEFILE, ALTERNATE (a) [most often used]   #
# ----------------------------------------------------------------#
         $spitshell >>Makefile <<'!NO!SUBS!'
cwobj = $(patsubst %$(OBJ_EXT),%.w32dll$(OBJ_EXT),$(obj))
DYNPERL_OBJ = perl.w32dll$(OBJ_EXT)

# New static pattern rules for dll and static object modules
#   test declspec(...) issue:
MYOVERIDEHDR = -idirafter . -include cygwin/somian_cygwin.h
# 
$(cwobj) $(DYNPERL_OBJ): override OPTIMIZE += -DPERLDLL $(MYOVERIDEHDR) -o $@
$(cwobj) $(DYNPERL_OBJ): %.w32dll$(OBJ_EXT): %.c
    gcc -c -E -dD $(subst -o $@,-o '-' ,$(OPTIMIZE)) -DPERL_CORE $< | sed -e '/./,/^$$/!d' \
 >>dllo_defines.log
    @echo -e '\n\n------------------------------------\n\n' >>dllo_defines.log
    $(strip $(CCCMD) $<)


$(obj) perl$(OBJ_EXT): %$(OBJ_EXT): %.c
    $(strip $(CCCMD) $<)

# override default rule (NB: make croaks!) to force dll usage
perlmain$(OBJ_EXT): perlmain.c
    $(CCCMD) $< -DUSEIMPORTLIB -o $@

.LIBPATTERNS: lib%.dll.a %.dll.a lib%.a %.a cyg%.dll lib%.dll %.dll
.SUFFIXES: .c $(OBJ_EXT) .w32dll$(OBJ_EXT) .i .s
.INTERMEDIATE: %$(OBJ_EXT)
.PRECIOUS: %$(OBJ_EXT) GNUmakefile
# library used to make statically linked executables (...uhh, "static lib")
# miniperl is linked against it to avoid libperl.dll locking
-l$(patsubst lib%,%,$(STATICLIB)) \
$(STATICLIB)$(LIB_EXT): perl$(OBJ_EXT) $(obj)
    $(AR) rcu $(STATICLIB)$(LIB_EXT) perl$(OBJ_EXT) $(obj)

# dll and import library
WANT_DEF =
ifneq "" "$(findstring DEF,$(MAKECMDGOALS))"
WANT_DEF = 1
endif
# Just mentioning 'USE_DLLTOOL' on the 'make' cmdln will toggle this.
ifneq "undefined" "$(origin USE_DLLTOOL)"
WANT_DEF = 1
endif

ifeq "1" "$(WANT_DEF)"
DEF_FILE = $(patsubst %.dll,%.def,$(IMPORTLIB))
.SECONDARY: $(DEF_FILE)
EXPORTALL_FLAG = $(if $(EXPORT_ALL),--export-all-symbols)
else
DEF_FILE =
endif
ifeq "undefined" "$(origin QUIK_RBLD)"
perlDLL = perlDLL
dllDEF  = dllDEF
.PHONY: $(perlDLL) $(dllDEF)
endif

HIDE_SYMBOLS := $(strip crypt   do_aspawn    do_spawn    encrypt    htovl \
htovs   init_os_extras   perlsio_binmode   setkey    vtohl   vtohs)
LD_EXCLUDES = -Wl,--exclude-symbols,$(subst $(spc),:,$(HIDE_SYMBOLS))

cdefines:
    @rm -f dllo_defines.log $(cwobj) $(DYNPERL_OBJ)
    $(MAKE) noDLLTOOL_DEF

$(perlDLL) \
$(DLLNAME) $(IMPORTLIB)$(LIB_EXT): \
 $(if $(DEF_FILE),$(DEF_FILE),miniperl.exe $(DYNPERL_OBJ) $(cwobj))
    @echo -e '\n\n\t***building DLL with creation of import lib $(IMPORTLIB)$(LIB_EXT).***\n\n'
    $(LDLIBPTH) $(LD) $(SHRPLDFLAGS) -o $(DLLNAME) $(VERS_LDCM) \
       -Wl,--out-implib,$(IMPORTLIB)$(LIB_EXT),--enable-auto-import \
       -Wl,--stack,8388608 $(DEF_FILE) $(DYNPERL_OBJ) $(cwobj) $(libs)

noDLLTOOL_DEF \
 $(DEF_FILE): miniperl.exe $(cwobj) $(DYNPERL_OBJ)
    @echo -e '\n\n\t***Running ld to create .def file only, throwing away junk dll.***\n\n'
    $(LD) $(SHRPLDFLAGS) -o junkdll.dll -Wl,--enable-auto-import,--kill-at \
 $(addprefix -Wl$(cma),$(EXPORTALL_FLAG) --output-def$(cma)$(DEF_FILE)) $(LD_EXCLUDES) \
 $(wordlist 2,$(words $^),$^) $(libs)
    @$(RMS) junkdll.dll

$(dllDEF) : miniperl.exe $(cwobj) $(DYNPERL_OBJ)
    @echo Generating .def file for DLL assembly.
    @$(DLLTOOL) $(EXPORTALL_FLAG) --kill-at -z $(DEF_FILE) $(wordlist 2,$(words $^),$^)
    @echo Please check the unrepaired .def file head:
    @echo -e '\n------------------------------------------------------------------------\n' ;
    -@head -8 $(DEF_FILE)
    @echo -e '------------------------------------------------------------------------\n' ; sleep 2
    @echo Fixing .def file using sed substitutions...
    @sed -e 1c\ 'LIBRARY $(DLLNAME)\ \
' -e s', @.*$$,,'  $(DEF_FILE) >fixupdef.tmp
    @echo Please check the fixed .def file head:
    @echo -e '\n------------------------------------------------------------------------\n'
    -@head -8 fixupdef.tmp
    @echo -e '------------------------------------------------------------------------\n' ; sleep 4
    @mv -i fixupdef.tmp $(DEF_FILE)


# How to build executables.

# The miniperl -w -MExporter line is a basic cheap test to catch errors before make
# goes on to run preplibrary and then MakeMaker on extensions. This is very handy
# because later errors are often caused by miniperl build problems but that's not
# obvious to the novice. The Module used here must not depend on Config or any
# extensions.

override FORCE =
miniperl.exe \
miniperl: opmini$(OBJ_EXT) miniperlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT)
    $(CC) $(CLDFLAGS) -o miniperl miniperlmain$(OBJ_EXT) opmini$(OBJ_EXT) \
 -Bstatic $(STATICLIB)$(LIB_EXT) -Bdynamic $(libs)
    ./miniperl -w -Ilib -MExporter -e '<?>' || $(MAKE) minitest

perl: $(IMPORTLIB)$(LIB_EXT) perlmain$(OBJ_EXT) \
                    $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) $(CC) $(CLDFLAGS) -o perl$(EXE_EXT) \
  perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) \
  $(CCDLFLAGS) -L. -l$(patsubst lib%,%,$(IMPORTLIB)) `cat ext.libs` $(libs)

pureperl: $(IMPORTLIB)$(LIB_EXT) perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) purify $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o pureperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(IMPORTLIB)) `cat ext.libs` $(libs)

purecovperl: $(IMPORTLIB)$(LIB_EXT) perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) purecov $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o purecovperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(IMPORTLIB)) `cat ext.libs` $(libs)

quantperl: $(IMPORTLIB)$(LIB_EXT) perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) quantify $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o quantperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(IMPORTLIB)) `cat ext.libs` $(libs)

!NO!SUBS!
    ;;
  *)
# --------------------------------------------------------------- #
#              PIECE 4 OF MAKEFILE, ALTERNATE (b)                 #
# ----------------------------------------------------------------#
         $spitshell >>Makefile <<'!NO!SUBS!'
cwobj = $(obj)

# perl library
$(STATICLIB)$(LIB_EXT): perl$(OBJ_EXT) $(cwobj)
    $(AR) rcu $@ perl$(OBJ_EXT) $(cwobj)

# How to build executables.

# The miniperl -w -MExporter line is a basic cheap test to catch errors
# before make goes on to run preplibrary and then MakeMaker on extensions.
# This is very handy because later errors are often caused by miniperl
# build problems but that's not obvious to the novice.
# The Module used here must not depend on Config or any extensions.

miniperl.exe \
miniperl: miniperlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT) opmini$(OBJ_EXT)
    $(LDLIBPTH) $(CC) $(CLDFLAGS) -o miniperl miniperlmain$(OBJ_EXT) opmini$(OBJ_EXT) $(LLIBPERL) $(libs)
    $(LDLIBPTH) ./miniperl -w -Ilib -MExporter -e '<?>' || $(MAKE) minitest

perl.exe \
perl: perlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o perl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) -Wl,-Bstatic $(patsubst lib%,-L. -l%,$(STATICLIB)) -Wl,-Bdynamic `cat ext.libs` $(libs)

pureperl: perlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) purify $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o pureperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(STATICLIB)) `cat ext.libs` $(libs)

purecovperl: perlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) purecov $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o purecovperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(STATICLIB)) `cat ext.libs` $(libs)

quantperl: perlmain$(OBJ_EXT) $(STATICLIB)$(LIB_EXT) $(DYNALOADER) $(static_ext) ext.libs
    $(SHRPENV) $(LDLIBPTH) quantify $(CC) $(CLDFLAGS) $(CCDLFLAGS) -o quantperl perlmain$(OBJ_EXT) $(DYNALOADER) $(static_ext) $(patsubst lib%,-L. -l%,$(STATICLIB)) `cat ext.libs` $(libs)

!NO!SUBS!
    ;;
esac

# ["libperl.a is _the_ library both in dll and static cases"] NO IT ISN'T.
# "$(LIBPERL)$(LIB_EXT) expands to this name dependless of build model" NO.
#
# NOTE: The "-Wl,-Bstatic $(LLIBPERL) -Wl,-Bdynamic" is required to give
# the import library linking priority over the dynamic library, since both
# the .dll and .a are in the same directory.  When the new standard for
# naming import/dynamic/static libraries emerges this should be updated.


# --------------------------------------------------------------- #
#              PIECE 5 OF MAKEFILE                                #
# ----------------------------------------------------------------#
$spitshell >>Makefile <<'!NO!SUBS!'


!NO!SUBS!

# suid perl is removed - i've never seen suid scripts for win32

##############################################
# additional targets
# --------------------------------------------------------------- #
#              PIECE 6 OF MAKEFILE                                #
# ----------------------------------------------------------------#
$spitshell >>Makefile <<'!NO!SUBS!'

DIST_DIRECTORY = .dist

distdir: miniperl
    -mkdir $(DIST_DIRECTORY)
    ./miniperl '-MExtUtils::Manifest' \
    -e "ExtUtils::Manifest::manicopy(ExtUtils::Manifest::maniread(),'$(DIST_DIRECTORY)')"

!NO!SUBS!

code syntax highlighting by GVIM, using a modified "darkblue" theme.