###############################################################################
# Makefile - makefile for os2ahci driver
# This is an Open Watcom makefile
#
# Copyright (c) 2010 Christian Mueller, Markus Thielen.
# Copyright (c) 2013 David Azarewicz
# 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
###############################################################################
# Environment

.ERASE
.SUFFIXES
.SUFFIXES: .lst .obj .c .asm .lib .def

# Define default build version if not specified in environment
BLD_MAJOR=1
BLD_MINOR=27 # must be 2 digits
BLD_REV=0 # not used at this time
FIXPACK=Test Build

!ifndef %WATCOM # if not defined in the environment
!error WATCOM must be set in the environment.
!endif
WATCOM=$(%WATCOM)

!ifndef %DDK # if not defined in the environment
!error DDK must be set in the environment.
!endif
DDK=$(%DDK)

!ifdef %VENDOR
VENDOR=$(%VENDOR)
!else
VENDOR=Unknown
!endif

!ifdef %BLD_MAJOR
BLD_MAJOR=$(%BLD_MAJOR)
!endif

!ifdef %BLD_MINOR
BLD_MINOR=$(%BLD_MINOR)
!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 &

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.
# The updated DDK devhelp.h is required and a copy is in the include directory
# of this project.
#

%PATH=..\..\tools;$(WATCOM)\BINP;$(WATCOM)\BINW;
AS     = wasm
CC     = wcc
MAPSYM = $(DDK)\base\tools\mapsym.exe

!ifdef __LOADDLL__
!loaddll wcc   $(WATCOM)\BINP\DLL\wccd.dll
!loaddll wlink $(WATCOM)\BINP\DLL\wlinkd.dll
!endif

AFLAGS = -q
# In order to build the SMP safe version you must have the updated devhelp.h and define OS2AHCI_SMP.
CFLAGS = -ei -6 -d0 -bt=os2 -ms -zu -wx -ecc -zp=1 -q -s -zgp -zfp -oi -DOS2AHCI_SMP
CFLAGS_DEBUG = -ei -6 -d3 -hc -bt=os2 -ms -zu -ecc -zp=1 -q -s -zgp -zfp -od

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

LIBS = addcalls doscalls rmcalls apmcalls os2286p.lib

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

all: os2ahci.add os2ahci.sym

clean: .symbolic
    rm -f $(OBJS) os2ahci.add *.cod *.lst *.def *.map *.sym *.err *.lnk version.h

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

libc.obj: libc.c Makefile $(INCS)

os2ahci.obj: os2ahci.c Makefile $(INCS) version.h ioctl.h

pci.obj: pci.c Makefile $(INCS)

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

ata.obj: ata.c Makefile $(INCS) ata.h

atapi.obj: atapi.c Makefile $(INCS) atapi.h ata.h

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

apm.obj: apm.c Makefile $(INCS)

ioctl.obj: ioctl.c Makefile $(INCS) ioctl.h atapi.h

trace.obj: trace.c Makefile $(INCS)

###############################################################################
# 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)

version.h: Makefile .always
    @%create $^@
    @%append $^@ /* Autogenerated by Makefile */
    @%append $^@ $#define VERSION $(BLD_MAJOR)$(BLD_MINOR) /* driver version (2 implied decimals) */
    @%append $^@ $#define DVENDOR "$(VENDOR)"
    @AddToFile $^@,$#define BLD_YEAR,DATEYEAR
    @AddToFile $^@,$#define BLD_MONTH,DATEMONTH
    @AddToFile $^@,$#define BLD_DAY,DATEDAY
    @AddToFile $^@,$#define BLDLEVEL,BLDLEVEL2,$(VENDOR),$(BLD_MAJOR).$(BLD_MINOR),AHCI Driver (c) Mensys BV 2013,$(FIXPACK)

.asm.obj: .autodepend
    $(AS) $(AFLAGS) $(AS_INCLUDE) $[@
    #wdis -l $^@

.c.obj: .autodepend
    $(CC) $(CFLAGS) $(CC_INCLUDE) $[@
    #wdis -l $^@

os2ahci.add: $(OBJS) Makefile
    # Target name, system and type
    @%create $*.lnk
    @%append $*.lnk name $*.add
    @%append $*.lnk sys os2 dll
    # 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
    @%erase $*.lnk

os2ahci.sym: os2ahci.map
    wat2map.cmd $[@ $^*.ma1
    $(MAPSYM) $^*.ma1
    @%erase $^*.ma1

