ifneq ($(wildcard ../config.mak),)
include ../config.mak
else
AWK=gawk
CC=gcc
endif

MAJOR_VERSION = 1
MINOR_VERSION = 0
VERSION = $(MAJOR_VERSION).$(MINOR_VERSION) 

ifeq ($(TARGET_OS),CYGWIN)
SHORTNAME = libdha2.dll
else
SHORTNAME = libdha2.so.$(MAJOR_VERSION)
VSHORTNAME = libdha2.so
endif
ifeq ($(TARGET_WIN32),yes)
LIBNAME = libdha2.a
SHORTNAME = libdha2.a
else
LIBNAME = libdha2.so.$(VERSION)
endif

CFLAGS  = $(OPTFLAGS) -fPIC -I. -I.. -Iinclude -Wall
LIBS = 
ifeq ($(TARGET_OS),OpenBSD)
ifeq ($(TARGET_ARCH_X86),yes)
LIBS += -li386
endif
endif

SRCS = dha2.c pciscan.c pci_names.c dha2_svgahelper.c dha2_dhahelper.c dha2_mtrr_linux.c dha2_pci_proc_linux.c dha2_devmem_mmap.c dha2_os2_mmap.c dha2_win9x_mmap.c dha2_windha_mmap.c dha2_pci_generic.c dha2_ports_direct.c dha2_mtrr_netbsd.c dha2_pciconfig_pci.c

OBJS=$(SRCS:.c=.o)

.SUFFIXES: .c .o

all: $(LIBNAME)

$(LIBNAME): $(OBJS)
ifeq ($(TARGET_WIN32),yes)
	$(AR) r $(LIBNAME) $(OBJS)
else
	$(CC) -shared -Wl,-soname -Wl,$(SHORTNAME) -o $(LIBNAME) $(OBJS) $(LIBS)
	ln -sf $(LIBNAME) $(SHORTNAME)
	ln -sf $(LIBNAME) $(VSHORTNAME)
endif

clean:
	-rm -f $(OBJS) $(LIBNAME) $(SHORTNAME) $(VSHORTNAME)

distclean: clean
	-rm -f pci_dev_ids.c pci_ids.h pci_names.c pci_names.h pci_vendors.h pci.db

pci_names.c: oth/pci.db pci_db2c.awk
	LC_ALL=C $(AWK) -f pci_db2c.awk oth/pci.db

dep:    depend

depend: pci_names.c
	$(CC) -MM $(CFLAGS) $(SRCS) 1>.depend

install:
	mkdir -p $(prefix)/lib
	install -m 755 -s -p $(LIBNAME) $(prefix)/lib/$(LIBNAME)
	rm -f $(prefix)/lib/libdha.so
	ln -sf $(LIBNAME) $(prefix)/lib/$(SHORTNAME)
ifeq ($(TARGET_OS),OpenBSD)
	ldconfig -R
else
	ldconfig
endif

uninstall:
	rm -f $(prefix)/lib/libdha.so $(prefix)/lib/$(SHORTNAME) $(prefix)/lib/$(LIBNAME)
ifeq ($(TARGET_OS),OpenBSD)
	ldconfig -R
else
	ldconfig
endif

#
# include dependency files if they exist
#
ifneq ($(wildcard .depend),)
include .depend
endif
