# makefile - Make distorm.dll OS/2 Watcom

# 2007-12-23 SHL Baseline
# 2010-04-25 SHL Update to cdecl
# 2012-08-14 SHL Build as subsystem DLL - no runtime

# Assumes
# set INCLUDE=*;D:\WATCOM\H;D:\TOOLKIT\H
# set LIB=*;D:\TOOLKIT\LIB;;D:\WATCOM\LIB386\OS2;D:\WATCOM\LIB386

# Required environment variables
# WATCOM
# VENDOR
# BUILDVER
# BUILDMACH
# BUILDTIME

# Supported environment variables
# Define DEBUG in environment for debug build

# Check required environment variables

!ifndef %WATCOM
!error WATCOM not defined
!endif

!ifndef %BUILDTIME
!error BUILDTIME not defined
!endif

!ifndef %BUILDVER
!error BUILDVER not defined
!endif

!ifndef %BUILDMACH
!error BUILDMACH not defined
!endif

!ifndef %VENDOR
!error VENDOR not defined
!endif

.ERASE

.SUFFIXES:
.SUFFIXES: .obj .lrf .lst .sym .map .c .cpp .h .lib

NAME = distorm
DESC = Distorm disassembler library built by Steven H. Levine
EXENAME = $(NAME)
EXEEXT = .dll
MAKEFILE = makefile

C_DIRS = ..\src
H_DIRS = ..

!ifndef DEBUG			# if not defined on wmake command line
!ifdef %DEBUG			# if defined in environment
DEBUG = $(%DEBUG)		# use value from environment
!endif
!endif

CC = wcc386

#  bd		build target is a Dynamic Link Library (DLL) (see bd)
# -bc           console app
# -bm           multithread libs
# -bt=os2       target
# -d2           full debug
#  ecc		set default calling convention to cdecl
#  ecs          set default calling convention to __syscall
# -hd           dwarf
# -j		signed char
# -mf           flat
# -of           generate traceable stack frames
# -olinars      optimze loops, inline, e(n)able fp recip, relax (a)lias, reordering, space
# -s            disable stack checks
# -sg	generate calls to grow the stack
# -st           touch stack through SS first
# -wcd14	no reference to symbol
# -wcd726	no reference to formal parameter
# -wx           max warnings
# -ze           define _cdecl etc.
# -zfp          disable fs use
# -zgp          disable gs use
# -zls		remove automatically inserted symbols (such as runtime library references)
# -zp4          align 4
# -zq           quiet

# -O2 -c -DSUPPORT_64BIT_OFFSET

# Order dependent flags
!ifdef %DEBUG
CFLAGS = -bt=os2 -mf -bm -of -olirs -d2 -s -j -wx -zfp -zgp -zls -hd -I$(H_DIRS)
!else
CFLAGS = -bt=os2 -mf -bm -of -olirs     -s -j -wx -zfp -zgp -zls -hd -I$(H_DIRS)
!endif

# Order independent flags
CFLAGS += -zq
# CFLAGS += -wcd14 -wcd726

#CFLAGS += -DSUPPORT_64BIT_OFFSET -D_DLL
# See config.h
CFLAGS += -D_DLL

LINK = wlink

!ifdef DEBUG
LFLAGS = sys os2v2_dll initinstance terminstance op quiet op verbose op cache &
	 op caseexact op implib op map debug dwarf all
!else
LFLAGS = sys os2v2_dll initinstance terminstance op quiet op verbose op cache &
	 op caseexact op implib op map
!endif

#################
# Inference rules
#################

.c: $(C_DIRS)

.c.obj: .AUTODEPEND $(MAKEFILE)
     $(CC) $(CFLAGS) $[*

##############
# Object files
##############

# objects built here
OBJS =  x86defs.obj &
	wstring.obj &
	textdefs.obj &
	prefix.obj &
	operands.obj &
	insts.obj &
	instructions.obj &
	distorm.obj &
	decoder.obj

# objects built elsewhere
EXTRA_OBJS=

# all object files
ALL_OBJS=$(OBJS) $(EXTRA_OBJS)

#############
# Build rules

all: $(EXENAME)$(EXEEXT)

#   @%write $^@ export distorm_decode64_.1
#   @%write $^@ export distorm_version_.2

# Avoid lost spaces in @%write
FILL = xxxx

$(NAME).lrf: $(MAKEFILE)
   @%write $^@ $(LFLAGS)
   @%write $^@ op desc '@$#$(%VENDOR):$(%BUILDVER)$#@$#$#1$#$# $(%BUILDTIME) $(FILL:x= ) $(%BUILDMACH)::EN:US:0:U:@@$(DESC)'
   @%write $^@ name $(NAME)
   @for %f in ($(ALL_OBJS)) do @%append $^@ file %f
   @%write $^@ library os2386.lib

$(EXENAME)$(EXEEXT): $(OBJS) $(MAKEFILE) $(NAME).lrf
   $(LINK) @$(NAME).lrf

lxlite: $(EXENAME)$(EXEEXT) .symbolic
    dir $<
    lxlite -X- -B- $<
    bldlevel $<
    dir $<

distclean: .symbolic
  -del $(NAME).err
  -del $(NAME).lrf
  -del $(NAME).map
  -del $(NAME).mbr
  -del $(NAME).sym
  -del $(OBJS)

clean: distclean .symbolic
  -del $(EXENAME)$(EXEEXT)
