#
# Makefile for the Linux RTL8139 device driver, v1.0.
#  
# To create a debuggable driver with emulation, do
#   make clean
#   make depend
#   make all
#

#-------------------------------------------------------------
# Compilation Tools
#-------------------------------------------------------------
CC := gcc
LD := ld

# The adjust-vma value may need to be changed if listing file addresses
# don't match addresses when loaded in the kernel.
# The value was 4, then changed to 10 in kernel version 2.0.35
LISTING := objdump -S 
LISTINGFLAGS := --adjust-vma=10

RELOCATESTABS := ./relocate_stabs.pl

#-------------------------------------------------------------
# Object files
#-------------------------------------------------------------
DRIVER_MODULE  := rtl8139.o

DRIVER_OBJ     := rtl8139.o

#-------------------------------------------------------------
# Compilation flags
#-------------------------------------------------------------
ifndef TOPDIR
TOPDIR := /usr/src/linux
endif

INCLUDES := -I $(TOPDIR)/include
CFLAGS := -D__KERNEL__ -DMODULE -O2 -m486 -Wall -finline-functions $(INCLUDES) 
CFLAGS := $(CFLAGS) -DMODVERSIONS


# this is for SMP support, be sure to uncomment on multiprocessor boxes 
#CFLAGS := $(CFLAGS) -D__SMP__

#------------------------------------------------------------
# Common rules
#------------------------------------------------------------

%.o: %.c
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<


# Our own rule to make source/assembly listing files
%.lst: %.o
	$(LISTING) $(LISTINGFLAGS) $< > $@

#------------------------------------------------------------
# Default target
#------------------------------------------------------------
.PHONY: all
all: $(DRIVER_MODULE)

#------------------------------------------------------------
# Include dependency files they exist
#------------------------------------------------------------
ifeq (.depend,$(wildcard .depend))
include .depend
endif

#------------------------------------------------------------
# Targets
#------------------------------------------------------------

# Target to make the plain driver
$(DRIVER_MODULE) : $(DRIVER_OBJ:.o=.c)
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
	$(LISTING) $(LISTINGFLAGS) $@ > $(@:.o=.lst)

# Target to make a debuggable driver
# Must do objdump before relocate
$(DEBUG_OBJ) : $(DRIVER_OBJ:.o=.c)

$(DEBUG_MODULE) $(DEBUG_MODULE:.o=.lst) : $(DEBUG_OBJ)
	$(LD) $(LDFLAGS) -r -o $@ $(DEBUG_OBJ)
	$(LISTING) $(LISTINGFLAGS) $@ > $(@:.o=.lst)

# This only to use kernel debugger
	#$(RELOCATESTABS) $@

# Remove all temporary and generated files
.PHONY: clean
clean:
	rm -f core *.o *.a *.s *.lst

# Compute the source file dependencies
#.PHONY: depend
#depend:
#	gcc -M $(EMU_OTHER_OBJS:_e.o=.c) $(CFLAGS) > .depend
#	perl -p -i -e 's/\.o/_e\.o/g' .depend

# Converts the source files from Windows
.PHONY: convert
convert: 
	dos2unix *.c makefile


