#
#       Equinox SuperSerial Technology (SST) driver for Linux
#
#       Copyright (C) 1989-2002 Equinox Systems Inc.
 
include ../../version.include
  
HERE=$(shell pwd)
INC = $(HERE)/../../include
.EXPORT_ALL_VARIABLES: 

INCLUDES = -I./include -I$(INC) 
MCFLAGS = -Wall -O2 -pipe -DLINUX -DCHRDRVR
MCFLAGS2 = -DVERSNUM=\"$(REL)\"
LIBS = -lncurses

########################################################################
# Environment tests
 
# Kernel Search Path
# These are all the places where kernel source might exist
KSP :=  /usr/src/linux-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | sed 's/-.*//') \
        /usr/src/linux-$(shell uname -r | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/') \
        /usr/src/linux
			  
# Only look at paths that exist and have an include/linux sub-directory
test_dir = $(shell [ -e $(dir)/include/linux ] && echo $(dir))
KSP := $(foreach dir, $(KSP), $(test_dir))
			   
# Kernel Source Location
# we will use this first valid entry in the search path
KSRC := $(firstword $(KSP))
KINC := $(KSRC)/include        

ifeq (,$(KSRC))
   $(error The Linux kernel source could not be found)
endif
   
# Check for version.h, config.h and modversions.h files
VERSION_FILE := $(KSRC)/include/linux/version.h
CONFIG_FILE  := $(KSRC)/include/linux/config.h
MODV_FILE    := $(KSRC)/include/linux/modversions.h
     
ifeq (,$(wildcard $(VERSION_FILE)))
   $(error Linux kernel source not configured - missing include/linux/version.h)
endif
	 
ifeq (,$(wildcard $(CONFIG_FILE)))
   $(error Linux kernel source not configured - missing include/linux/config.h)
endif

ifeq (,$(wildcard $(MODV_FILE)))
   $(warning - missing include/linux/modversions.h)
endif
	     
# get the kernel version - we use this to find the correct install path
KVER := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(VERSION_FILE) | grep UTS_RELEASE | \
	awk '{ print $$3 }' | sed 's/\"//g')

ifneq ($(KVER),$(shell uname -r))
   $(warning ***)
   $(warning *** Warning: kernel source version ($(KVER)))
   $(warning *** does not match running kernel  ($(shell uname -r)).)
   $(warning *** Its likely that the Equinox SST driver cannot)
   $(warning *** be built or cannot be loaded.)
   $(warning ***)
endif
		   
# check the SMP flag - first from config.h
SMP := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(CONFIG_FILE) | \
	grep CONFIG_SMP | awk '{ print $$3 }')
ifneq ($(SMP),1)
   SMP := 0
endif
			        
# check that running kernel matches SMP configuration from config.h
ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
   $(warning ***)
ifeq ($(SMP),1)
      $(warning *** Warning: kernel source configuration (SMP))
      $(warning *** does not match running kernel (UP).)
else
      $(warning *** Warning: kernel source configuration (UP))
      $(warning *** does not match running kernel (SMP).)
endif
      $(warning *** Its likely that the Equinox SST driver cannot)
      $(warning *** be built or cannot be loaded.)
      $(warning ***)
endif          

ifeq ($(SMP), 1)
   MCFLAGS += -D__SMP__
endif

# check MODVERSIONS configuration from running kernel
MODV := $(shell cat /proc/ksyms | grep tty_register_driver | cut -d" " -f2)
ifneq ($(MODV), tty_register_driver)
   MODV := 1
else
   MODV := 0
endif

# check the MODVERSIONS configuration, if modversions.h available
ifeq (,$(wildcard $(MODV_FILE)))
   MODVER := $(MODV)
else
   MODVER := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(MODV_FILE) | \
	grep tty_register_driver)
   ifeq ($(MODVER),)
      MODVER := 0
   else
      MODVER := 1
   endif
endif

# check that MODVERSIONS configuration matches
ifneq ($(MODV), $(MODVER))
   $(warning ***)
ifeq ($(MODV),1)
   $(warning *** Warning: module versions configuration option: MODVERSIONS)
   $(warning *** [disabled] does not match running kernel [MODVERSIONS enabled].)
else
   $(warning *** Warning: module versions configuration option: MODVERSIONS)
   $(warning *** [enabled] does not match running kernel [MODVERSIONS disabled].)
endif
   $(warning *** Its likely that the Equinox SST driver cannot)
   $(warning *** be loaded.)
   $(warning ***)
endif                    

ifeq "$(findstring 86, $(shell uname -m))" "86"
   MCFLAGS += -DIX86 -m486 -DISA_ENAB -DMCA_ENAB
endif
ifeq "$(findstring ia64, $(shell uname -m))" "ia64"
   MCFLAGS += -DIA64 -Wno-format
endif

# Completed Environment tests
########################################################################

build:  ssdiag sscode

install: ssdiag sscode
	@strip ssdiag sscode
	@mkdir -p $(OBJROOT)/usr/bin
	@install -o root -m 755 ssdiag $(OBJROOT)/usr/bin 
	@install -o root -m 755 sscode $(OBJROOT)/usr/bin 
	@install -o root -m 444 README.ssdiag $(OBJROOT)/.
	@mkdir -p $(OBJROOT)/usr/lib/sst
	@install -o root -m 444 ss.hlp $(OBJROOT)/usr/lib/sst


ssdiag: u4.o ports.o screen.o 
	$(CC) $(MCFLAGS) $(LIBS) u4.o screen.o ports.o -ossdiag

sscode: ccode.o screen.o 
	$(CC) $(MCFLAGS) $(LIBS) ccode.o screen.o -osscode

u4.o: u4.c
	$(CC) $(MCFLAGS) $(MCFLAGS2) $(INCLUDES) -I $(KINC) -c u4.c

screen.o: screen.c
	$(CC) $(MCFLAGS) $(INCLUDES) -I $(KINC) -c screen.c

ports.o: ports.c
	$(CC) $(MCFLAGS) $(INCLUDES) -I $(KINC) -c ports.c

ccode.o: ccode.c
	$(CC) $(MCFLAGS) $(INCLUDES) -I $(KINC) -c ccode.c

clean:
	@rm -f *.o
	@rm -f ssdiag sscode 

clobber: clean
	@rm -f $(OBJROOT)/usr/bin/ssdiag
	@rm -f $(OBJROOT)/usr/bin/sscode
	@rm -f $(OBJROOT)/README.ssdiag
	@rm -f $(OBJROOT)/usr/lib/sst/ss.hlp


