#
#  makefile for OpenGPS receiver
#
#  - creates module ogrt_module.o (RT process} and 
#    executable ogrcvr (user process} 
#  - moves both files to ${BINDIR}
#

# copied from OpenSourceGPS's Makefile:
#
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile there then includes 
# this Makefile once again.
#
# The following conditional selects whether we are being included from 
# the kernel Makefile or not.

ifeq ($(KERNELRELEASE),)

# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
  KERNELDIR ?= /lib/modules/$(shell uname -r)/build

# The current directory is passed to sub-makes as argument
  PWD := $(shell pwd)


#  which machine are we running on?

  host-name := $(shell hostname)

#  program version

#  PRGVER = ${VERSION}.${PATCHLEVEL}.${SUBLEVEL}

  OGR_DEF := -DPCIBOARD=$(is_pci_board) -DCHECK_CORR_OUTPUT=$(check_corr_output) \
    -DPROGRAMVERSION=\"$(OPENGPSRELEASE)\"

# compiler & linker

  CC := gcc
  LD := ld


# it is assumed that these directories exist 
# and point to the correct set of header files ...

  LINUX_HDR := /usr/src/kernel-headers-`uname -r`/include
  RTAI_HDR  := /usr/realtime/include

  OGR_HDR   := ../include

  BINDIR    := ../../bin
  LIBDIR    := ../../lib

# compiler flags
# (-Wuninitialized not supported without -O)

  WARN       := -W -Wall -Wstrict-prototypes -Wmissing-prototypes \
   -Wno-trigraphs -Wunused

#  WARNMOD    := -W -Wstrict-prototypes -Wmissing-prototypes \
#   -Wno-trigraphs -Wunused-variable

# include directories

  INCLUDE    := -I${RTAI_HDR} -I${LINUX_HDR} -I${OGR_HDR} 

#  INCLUDEMOD := -isystem ${RTAI_HDR} -isystem ${LINUX_HDR} -I${OGR_HDR}

  CFLAGS := -g ${INCLUDE} ${WARN} -fomit-frame-pointer \
    -fno-strict-aliasing -fno-common -pipe ${OGR_DEF}

#  EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -ffast-math -mhard-float

  LDFLAGS := -L${LIBDIR}


## module requires compilation with flag O2
#
#  CFLAGSMOD  := -O2 -D__KERNEL__ -DMODULE ${INCLUDEMOD} ${WARNMOD} \
#   -fomit-frame-pointer -fno-strict-aliasing -fno-common -pipe ${OGR_DEF}

# rules

  modules: .depend
	  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
	  cp ogrcvr_mod.ko ${BINDIR}
#	sudo cp ogrcvr_mod.ko /lib/modules/`uname -r`/extra

  modules_install: 90-opengps.rules
	  cp 90-opengps.rules /etc/udev/rules.d/
	  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

  90-opengps.rules:
	  echo 'KERNEL=="opengps.status",     NAME="opengps/status",      MODE="0666"' > $@
	  echo 'KERNEL=="opengps.measurement",NAME="opengps/measurement", MODE="0666"' >> $@
	  echo 'KERNEL=="opengps.data",       NAME="opengps/data",        MODE="0666"' >> $@

#  install: modules_install 90-opengps.rules
#	  $(RM) 90-opengps.rules
#	  mv 90-opengps.rules /etc/udev/rules.d/
##	  cp ogrcvr_mod.ko ${BINDIR}

  clean:
	  $(RM) -r *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
	  $(RM) ogrcvr_mod.ko ${BINDIR}/ogrcvr_mod.ko

  SRC := $(wildcard *.c)

  include .depend

  .depend:
	  $(RM) $@
	  gcc -MM $(CFLAGS) $(SRC) > $@

  .PHONY: modules modules_install clean depend

else

# real-time module
# called from kernel build system: just declare what our modules are

#  KDIR         := /lib/modules/$(shell uname -r)/build
#  PWD          := $(shell pwd)
#  EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -ffast-math -mhard-float

  EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -I$(PWD)/../include \
    -DPCIBOARD=$(is_pci_board) -DCHECK_CORR_OUTPUT=$(check_corr_output) \
    -DPROGRAMVERSION=\"$(OPENGPSRELEASE)\" -ffast-math -mhard-float 
#  EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ \
#    -I/home/gbeyerle/software/opengps-0.4.10/src/include \
#    -DPCIBOARD=$(is_pci_board) -DCHECK_CORR_OUTPUT=$(check_corr_output) \
#    -DPROGRAMVERSION=\"$(OPENGPSRELEASE)\" -ffast-math -mhard-float 

#  EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -ffast-math -mhard-float

  ogrcvr_mod-objs := ogrt_gp2021.o ogrt_fifos.o ogrt_maths.o ogrt_modfun.o \
      ogrt_navbit.o ogrt_procfs.o ogrt_preamble.o ogrt_track.o

  obj-m := ogrcvr_mod.o

#default:
#	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
#	gcc -o scope scope.c

endif

# end of makefile

