###############################################################################
# wmakefile - makefile for os2ahci driver using WATCOM compiler
#
# Copyright (c) 2010 Christian Mueller, Markus Thielen.
# Parts copied from/inspired by the Linux AHCI driver;
# those parts are (c) Linux AHCI/ATA maintainers
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program 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 for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

###############################################################################
# Calling Syntax
#
# wmake -ms -f wmakefile
#
# NOTE: we use MS compatibility mode (-ms) so we can use a syntax similar
#       to the well known nmake syntax.
#

###############################################################################
# wmake directives
.HOLD # do not prompt for deletion of created files on error; leave them alone



###############################################################################
# Environment


# main path to OS/2 DDK; this needs to be set before this makefile will work
!ifndef DDK
DDK          = i:\ddk
!endif

# main path to WATCOM installation
!ifndef WATCOM
WATCOM       = i:\watcom
!endif

CC_INCLUDE   = -I..\include \
               -I$(DDK)\base\h \
               -I$(DDK)\base\ibmh \
               -I$(DDK)\base\src\dev\dasd\diskh \
               -I$(DDK)\base\src\dev\thinkpad\dockii\apmcalls \
#               -I$(CC16)\include

AS_INCLUDE   = -I=$(DDK)\base\inc \
               -I=$(DDK)\base\src\dev\dasd\diskinc

LIB_DIRS     = $(DDK)\base\lib\ \
               $(DDK)\base\src\dev\dasd\devhlp\ \
               $(DDK)\base\src\dev\thinkpad\dockii\apmcalls\ \




###############################################################################
# Tool Chain
#
# This makefile uses the Watcom 16-bit compiler, Watcom Assembler and Linker.
# You can change to the DDK Linker and Assembler by modifying the macros below.
#


#LINKER=ibm
LINKER=watcom


#AS         = $(DDK)\tools\alp.exe
#AS         = jwasm -Cp
AS         = wasm
CC         = wcc
LD         = $(DDK)\base\tools\link.exe
BLDLEVEL   = cmd.exe /c ..\..\tools\bldlvl.cmd
BLDDATE    = cmd.exe /c ..\..\tools\blddate.cmd
MAPSYM     = $(DDK)\base\tools\mapsym.exe

#AFLAGS        = -Mb
#CFLAGS        = -ei -5 -d3 -hc -bt=os2 -ms -zu -w3 -wcd=138 -wcd=300 -ecc -zp1 -q -s -zgp -zfp -oi
CFLAGS        = -ei -5 -d0 -bt=os2 -ms -zu -w=0 -ecc -zp=1 -q -s -zgp -zfp -oi
CFLAGS_DEBUG  = -ei -5 -d3 -hc -bt=os2 -ms -zu -ecc -zp=1 -q -s -zgp -zfp -od

LFLAGS        = /noe /nod /packd /a:16 /batch /map /line

###############################################################################
# Main dependencies

TARGET   = os2ahci.add

#LIBS     = addcalls doscalls rmcalls apmcalls # dhcalls not needed, see local devhelp.h
LIBS     = addcalls doscalls rmcalls apmcalls dhcalls

SRCS     = init.asm math.asm libc.c os2ahci.c pci.c ahci.c ata.c atapi.c \
           ctxhook.c trace.c ioctl.c apm.c

OBJS     = init.obj libc.obj os2ahci.obj pci.obj ahci.obj ata.obj atapi.obj \
           ctxhook.obj trace.obj ioctl.obj apm.obj

INCS     = os2ahci.h ahci.h version.h


all: $(TARGET)

clean:
        rm -f $(OBJS) $(TARGET) *.cod *.lst *.def *.map *.sym *.err *.lnk \
                bldday.h

###############################################################################
# Object/source dependencies

init.obj:       $*.asm  wmakefile

libc.obj:       $*.c    wmakefile $(INCS)

os2ahci.obj:    $*.c    wmakefile $(INCS) bldday.h ioctl.h

pci.obj:        $*.c    wmakefile $(INCS)

ahci.obj:       $*.c    wmakefile $(INCS) ata.h atapi.h

ata.obj:        $*.c    wmakefile $(INCS) $*.h

atapi.obj:      $*.c    wmakefile $(INCS) $*.h ata.h

ctxhook.obj:    $*.c    wmakefile $(INCS) ata.h atapi.h

apm.obj:        $*.c    wmakefile $(INCS)

ioctl.obj:      $*.c    wmakefile $(INCS) $*.h atapi.h

trace.obj:      $*.c    wmakefile $(INCS)

os2ahci.def:  version.h $*.def.template

###############################################################################
# Action definitions (compile/link commands)

# emacs TAGS file creation
# NOTE: OS/2 emacs etags.exe expects an empty file named c:\dev\null...
tags:   $(SRCS) $(INCS)
        etags.exe $(SRCS) $(INCS)

bldday.h:
        $(BLDDATE) > bldday.h

.asm.obj:
        $(AS) $(AFLAGS) $(AS_INCLUDE) $*.asm
        wdis -l $*.obj

.c.obj:
        $(CC) $(CFLAGS) $(CC_INCLUDE) $*.c
        wdis -l $*.obj

os2ahci.def:
        $(BLDLEVEL) $*.def.template $*.def version.h

$(TARGET): $(OBJS) $*.def wmakefile

!if "$(LINKER)"=="watcom"
        # Target name, system and type
        @%create $*.lnk
        @%append $*.lnk name $*.add
        @%append $*.lnk sys os2 dll
        # Merge BLDLEVEL information
        @%append $*.lnk option
        @qgrep -y -e "description" $*.def >> $*.lnk
        # Set various options
        @%append $*.lnk option protmode
        @%append $*.lnk option map
        @%append $*.lnk option quiet
        @%append $*.lnk option nocase
        @%append $*.lnk option stack=0
        # Generate directives for objects, libraries and library search-paths
        @for %f in ($(OBJS)) do @%append $*.lnk file %f
        @for %f in ($(LIBS)) do @%append $*.lnk lib %f
        @for %f in ($(LIB_DIRS)) do @%append $*.lnk libpath %f
        # Attributes for the DATA and CODE segments
        @%append $*.lnk segment type DATA SHARED
        @%append $*.lnk segment type CODE IOPL
        # Order segments by class
        @%append $*.lnk order
        @%append $*.lnk clname 'DATA'
        @%append $*.lnk clname 'CONST'
        @%append $*.lnk clname 'BSS'
        @%append $*.lnk clname 'CODE'
        # Link the stuff together and build the target
        wlink  @$*.lnk

!elseif "$(LINKER)"=="ibm"
        $(LD) $(LFLAGS) $(OBJS),$(TARGET),$*.map,$(LIB_DIRS) $(LIBS),$*.def
        mapsym $*
!else
        @echo "Error: No valid linker specified"
!endif

