
#########################################################################
#
#  $Id: Makefile 1.2 2017/07/06 09:45:24 martin REL_M $
#
#  Description:
#    Makefile for mbgtools which recurses into the subdirectories.
#
# -----------------------------------------------------------------------
#  $Log: Makefile $
#  Revision 1.2  2017/07/06 09:45:24  martin
#  Support quiet build.
#  Updated list of target subdirs.
#  Also test subdirs for BSDmakefile.
#  Optional parameter USE_TIMESPEC.
#  Revision 1.1  2011/01/27 15:41:01  martin
#  Initial revision
#
#########################################################################

V ?= 0   # set to 1 to build verbosely

# The lines below make the build output non-verbose by default.
# Call make with parameter "V=1" to get verbose output.
.if $(V) != 0
  Q :=
  QM :=
  vecho = @true
.else
  Q := @
  QM := -s
  vecho = @echo
.endif


.ifndef prefix
  prefix := /usr/local
.endif

.ifndef bindir
  bindir := $(prefix)/bin
.endif

.ifndef sbindir
  sbindir := $(prefix)/sbin
.endif

.ifndef sysconfdir
  sysconfdir := /etc
.endif

.ifndef includedir
  includedir := $(prefix)/include
.endif

.ifndef libdir
  CUR_ARCH = $(shell uname -m)
  find_libdirname := $(shell \
	if [ $(CUR_ARCH) = "ia64" -o $(CUR_ARCH) = "x86_64" ]; then \
	    if test -d $(prefix)/lib64; \
              then echo "lib64"; \
	      else echo "lib"; \
	    fi \
	else \
	    if test -d $(prefix)/lib32; \
	      then echo "lib32"; \
              else echo "lib"; \
            fi \
	fi \
  )
  libdir := $(prefix)/$(call find_libdirname)
##$(warning  $(INFO) libdir set to $(libdir))
.endif

INSTALL_PATHS = DESTDIR=$(DESTDIR) prefix=$(prefix) bindir=$(bindir) sbindir=$(sbindir) includedir=$(includedir) libdir=$(libdir) sysconfdir=$(sysconfdir)

.ifndef SO_BASENAMES
  SO_BASENAMES += devio
  SO_BASENAMES += util
  SO_BASENAMES += serio
  SO_BASENAMES += extio
.endif

.ifndef SO_SUBDIRS
#  SO_SUBDIRS += $(foreach dir,$(SO_BASENAMES), libmbg_so/mbg$(dir) )
##  $(warning  $(INFO) **************** SO_SUBDIRS set to $(SO_SUBDIRS))
.endif

##$(warning  $(INFO) SO_SUBDIRS set to $(SO_SUBDIRS))

.ifndef BASEDIR

.DEFAULT_GOAL := all
MAKECMDGOALS ?= $(.DEFAULT_GOAL)

# This section is used only if this makefile is called by the top-level
# make. It specifies some common targets and some subdirs in which a
# recursive make will be executed, which may include this make file again
# and evaluate the second part.

INFO=top 1st:
## $(warning $(INFO) reading top level part)

## export COMMON_MAKEFILE := $(CURDIR)/Makefile
## $(warning  $(INFO) COMMON_MAKEFILE set to $(COMMON_MAKEFILE))

# The macro function below prints an info string whether a target
# is supported by the kernel Makefile

.ifndef SUBDIRS

chk_subdir = $(shell test -f $(strip $(1))/Makefile || test -f $(strip $(1))/BSDmakefile && echo "$(1)" )

.ifndef DONT_BUILD_LIBS
  SUBDIRS += $(SO_SUBDIRS)
##  $(foreach dir,$(SO_SUBDIRS),$(eval SUBDIRS += $(call chk_subdir, $(dir))))
  SUBDIRS += $(call chk_subdir, "libmbg_so/devel" )
.endif

.ifndef DONT_BUILD_TOOLS
  SUBDIRS += $(call chk_subdir, "mbgstatus" )
  SUBDIRS += $(call chk_subdir, "mbgsetsystime" )
  SUBDIRS += $(call chk_subdir, "mbgctrl" )
  SUBDIRS += $(call chk_subdir, "mbgirigcfg" )
  SUBDIRS += $(call chk_subdir, "mbgshowsignal" )
  SUBDIRS += $(call chk_subdir, "mbggpscap" )
  SUBDIRS += $(call chk_subdir, "mbghrtime" )
  SUBDIRS += $(call chk_subdir, "mbgfasttstamp" )
  SUBDIRS += $(call chk_subdir, "mbgsvcd" )
  SUBDIRS += $(call chk_subdir, "mbgxhrtime" )
  SUBDIRS += $(call chk_subdir, "test/mbgtestcalrec" )
  SUBDIRS += $(call chk_subdir, "test/mbgtestio" )
  SUBDIRS += $(call chk_subdir, "test/mbgtestmmio" )
  SUBDIRS += $(call chk_subdir, "test/mbgtestxhrt" )
  SUBDIRS += $(call chk_subdir, "test/mbgchksystime" )
  SUBDIRS += $(call chk_subdir, "test/mbgtestsettime" )
.endif

.ifndef DONT_BUILD_GUI
.ifndef DONT_BUILD_MBGMON
    SUBDIRS += $(call chk_subdir, "wxwidgets/mbgmon" )
.endif
.ifndef DONT_BUILD_MBGSTATSVIEWER
    SUBDIRS += $(call chk_subdir, "wxwidgets/mbgstatsviewer" )
.endif
.endif

.ifndef DONT_BUILD_DRIVER
  SUBDIRS += $(call chk_subdir, "mbgclock" )
  ##SUBDIRS += $(call chk_subdir, "test/mbgclock-test" )
.endif

## $(warning $(INFO) SUBDIRS = $(SUBDIRS))
## $(warning $(INFO) MAKECMDGOALS = $(MAKECMDGOALS))

.endif


SUBDIRS += mbgstatus
SUBDIRS += mbgsvcd
SUBDIRS += mbgsetsystime
SUBDIRS += mbgctrl
SUBDIRS += mbgirigcfg
SUBDIRS += mbgshowsignal
SUBDIRS += mbggpscap
SUBDIRS += mbghrtime
SUBDIRS += mbgfasttstamp
## SUBDIRS += mbgxhrtime  ## not yet tested

SUBDIRS += test/mbgchksystime
## SUBDIRS += test/mbgclock-test
SUBDIRS += test/mbgcmptime
SUBDIRS += test/mbgreadtime
SUBDIRS += test/mbgtcrcal
##SUBDIRS += test/mbgtestcalrec
SUBDIRS += test/mbgtestio
SUBDIRS += test/mbgtestsettime
SUBDIRS += test/mbg_ucap

SUBDIRS += mbgclock

.PHONY: all clean distclean install uninstall
all clean distclean install uninstall:
	@for dir in $(SUBDIRS); do \
	  if test -f $$dir/Makefile || test -f $$dir/BSDmakefile; then \
	    echo "Making $@ in $$dir"; \
	    /bin/sh -c "cd $$dir; $(MAKE) $@"; \
	  fi \
	done

.PHONY: dev dev-nodes dev-clean suse_symvers probe ins rm test reload check \
        refclocks kernel_config
dev dev-nodes dev-clean suse_symvers probe ins rm test reload check \
    refclocks kernel_config:
	@echo "Making $(MAKECMDGOALS) in $@"
	@cd mbgclock && make $@

.PHONY: cleanreload
cleanreload: clean all install reload

.else # ================================================================

# This section is evaluated only if this makefile is included from
# a subproject's makefile.


.ifndef MBGLIB
  MBGLIB := $(BASEDIR)/mbglib
.endif

MBGLIB_DIRS += common linux

# Macros useful to run individual commands as root:
.ifndef CALLED_FROM_SPEC
.ifndef SUDO
    SUDO := $(shell which sudo)
.endif

.ifndef SU
    SU := $(shell which su)
.endif

  UID = $(shell id -u)
.else
  #fake running as root
  UID = 0
.endif

run_as_root = $(shell echo "$(SUDO) $(1) || $(SU) -c \"$(1)\"" )
make_as_root = $(call run_as_root, make $(1))

.ifndef MODULE_OBJS  #===========================================================

# not building a kernel module

CFLAGS += -Wall

.ifdef DEBUG
  CFLAGS += -DDEBUG=$(DEBUG)
  CFLAGS += -g
.else
  CFLAGS += -O2
.endif

.ifdef USE_TIMESPEC
  CFLAGS += -DUSE_TIMESPEC=$(USE_TIMESPEC)
.endif

CFLAGS += -I.
CFLAGS += -I$(BASEDIR)
# foreach doesn't seem to work under FreeBSD :-(
# CFLAGS += $(foreach dir,$(MBGLIB_DIRS),-I$(MBGLIB)/$(dir))
CFLAGS += -I$(BASEDIR)/mbglib/common
CFLAGS += -I$(BASEDIR)/mbglib/bsd

LDFLAGS += -lutil
# foreach doesn't seem to work under FreeBSD :-(
# LDFLAGS += $(foreach dir,$(SO_SUBDIRS),-L $(BASEDIR)/$(dir) )


SO_MAJOR_VERSION = 1
SO_MINOR_VERSION = 0

.ifdef SO_TARGET_BASENAME
.ifndef SO_POINT_RELEASE
    SO_POINT_RELEASE = 0
.endif

  SO_TARGET_NAME = libmbg$(SO_TARGET_BASENAME)
  SO_TARGET_LIBNAME = $(SO_TARGET_NAME).so
  SO_TARGET_SONAME = $(SO_TARGET_LIBNAME).$(SO_MAJOR_VERSION)
  TARGET = $(SO_TARGET_SONAME).$(SO_MINOR_VERSION).$(SO_POINT_RELEASE)

  OBJS += mbg$(SO_TARGET_BASENAME).o

  CLEAN_FILES += $(SO_TARGET_LIBNAME)

  CFLAGS += -fPIC
  CFLAGS += -c

  LDFLAGS += -shared
  LDFLAGS += -lc
  LDFLAGS += -Wl,-soname,$(SO_TARGET_SONAME)

.ifndef LIBDIR
    LIBDIR := $(shell if [ -d /usr/lib64 ]; then echo /usr/lib64; else echo /usr/lib; fi )
.endif
.endif

CXXFLAGS = $(CFLAGS)


.PHONY: all
all: $(TARGET)

#### VPATH += $(foreach dir,$(MBGLIB_DIRS),$(MBGLIB)/$(dir))
VPATH = $(BASEDIR)/mbglib/common:$(BASEDIR)/mbglib/bsd

.ifndef MBGDEVIO_SIMPLE
  MBGDEVIO_SIMPLE = 1
.endif

.ifdef MBGDEVIO_SIMPLE
  CFLAGS += -DMBGDEVIO_SIMPLE=$(MBGDEVIO_SIMPLE)
.endif


.ifdef USE_THREAD_API

  # Check whether thread affinity is supported by the installed pthread library.
  # Newer versions of glibc/pthread (at least glibc 2.5) supports this natively.
  # Older versions of glibc/pthread (e.g. glibc 2.3) may not support this natively
  # but may provide a NPTL (New Posix Thread Library) library located under separate
  # include and lib paths. We try to detect NPTL and add those paths to the search
  # paths only if USE_NTPL has been defined e.g. on the make command line.

  # Check whether pthread_getaffinity is supported by the standard pthread.h
  USE_THREAD_AFFINITY := $(shell grep -q pthread_getaffinity /usr/include/pthread.h && echo 1)

  # If it is not, check nptl/pthread.h
.ifndef USE_THREAD_AFFINITY
.ifdef USE_NPTL
      USE_THREAD_AFFINITY := $(shell grep -q pthread_getaffinity /usr/include/nptl/pthread.h && echo 1)
.endif

.ifndef USE_THREAD_AFFINITY
      ##++++ If not found either, don't support thread affinity
      USE_THREAD_AFFINITY=0
.else
      #if supported via nptl add the associated search paths
      CFLAGS += -I /usr/include/nptl -L /usr/lib/nptl
.endif
.endif

  # May want to use the result for our program
  ## CFLAGS += -DUSE_THREAD_AFFINITY=$(USE_THREAD_AFFINITY)

  CFLAGS += -DMBGDEVIO_USE_THREAD_API=1

  #LDFLAGS += -lpthread
  LDFLAGS += -pthread
.endif

.ifdef INST_TO_BIN
  INST_DIR = $(DESTDIR)$(bindir)
.else
.ifdef INST_TO_SBIN
    INST_DIR = $(DESTDIR)$(sbindir)
.else
.ifdef INST_TO_LIB
      INST_DIR = $(DESTDIR)$(libdir)
.else
.ifdef INST_TO_INCLUDE
          INST_DIR = $(DESTDIR)$(includedir)/mbglib
.endif
.endif
.endif
.endif

.ifdef DO_GUI_INSTALLATION
  EXT_INSTALL += gui_install
  EXT_UNINSTALL += gui_uninstall
.endif


## $(warning $(INFO) CFLAGS=$(CFLAGS))
## $(warning $(INFO) EXTRA_CFLAGS=$(EXTRA_CFLAGS))
## $(warning $(INFO) MAKECMDGOALS = $(MAKECMDGOALS))


.c.o:
	$(vecho) "    $(CC)      ${.IMPSRC}"
	$(Q)$(CC) $(CPPFLAGS) $(CFLAGS) -o ${.TARGET} -c ${.IMPSRC}



$(TARGET): $(OBJS)
	$(vecho) "    Linking $@"
	$(Q)$(CC) -o $@ $(OBJS) $(LDFLAGS)
.ifdef SO_TARGET_LIBNAME
	ln -sf $(TARGET) $(SO_TARGET_LIBNAME)
.endif



.PHONY: install
install: $(EXT_INSTALL)
.ifdef XXX #.ifneq ($(UID),0)
	$(call make_as_root, $@)
.else
.ifdef INCLUDE_FILES #include files
	  mkdir -p $(INST_DIR)
	  for incfile in $(INCLUDE_FILES); do \
	      install -m 644 $(BASEDIR)/mbglib/common/$$incfile $(INST_DIR)/; \
	  done;
	  for basename in $(SO_BASENAMES); do \
	      pushd $(DESTDIR)$(libdir); \
	      ln -sf libmbg$$basename.so.$(SO_MAJOR_VERSION) libmbg$$basename.so; \
	      popd; \
	  done;
.else #usual installation
.ifdef CALLED_FROM_SPEC
	      mkdir -p $(INST_DIR)
.endif
	  install -s $(TARGET) $(INST_DIR)

.ifndef CALLED_FROM_SPEC
.ifdef INST_TO_LIB
	          /sbin/ldconfig
.endif
.endif
.endif
.endif


.PHONY: uninstall
uninstall: $(EXT_UNINSTALL)
.ifdef INCLUDE_FILES #remove include files and
	rm -rf $(INST_DIR)
.ifndef CALLED_FROM_SPEC #remove symbolic links
	    for basename in $(SO_BASENAMES); do \
	        rm -f $(libdir)/libmbg$$basename.so; \
	    done;
.endif
.else
.ifdef XXX #.ifneq ($(UID),0)
	  $(call make_as_root, $@)
.else
	  rm -f $(INST_DIR)/$(TARGET)
.ifndef CALLED_FROM_SPEC
.ifdef INST_TO_LIB
	          /sbin/ldconfig
.endif
.endif
.endif
.endif

.PHONY: gui_install
gui_install:
.ifdef XXX #.ifneq ($(UID),0)
	$(call make_as_root, $@)
.else
	mkdir -p $(DESTDIR)$(prefix)/share/$(TARGET)
	cp $(TARGET).png $(DESTDIR)$(prefix)/share/$(TARGET)/$(TARGET).png
	mkdir -p $(DESTDIR)$(prefix)/share/applications
	cp $(TARGET).desktop $(DESTDIR)$(prefix)/share/applications/$(TARGET).desktop
.endif

.PHONY: gui_uninstall
gui_uninstall:
.ifdef XXX #.ifneq ($(UID),0)
	$(call make_as_root, $@)
.else
	$(Q)rm -rf $(DESTDIR)$(prefix)/share/$(TARGET)
	$(Q)rm -f $(DESTDIR)$(prefix)/share/applications/$(TARGET).desktop
.endif

CLEAN_FILES += *.o
CLEAN_FILES += *~
CLEAN_FILES += core
CLEAN_FILES += $(TARGET)

.PHONY: clean
clean:
.ifdef CLEAN_FILES
	$(Q)rm -f $(CLEAN_FILES)
.endif
.ifdef CLEAN_DIRS
	$(Q)rm -rf $(CLEAN_DIRS)
.endif


.else
# ===== Building module for kernel 2.6 =============================================

.endif  # building kernel module
#==================================================================================


# common targets:

.PHONY: distclean
distclean: clean

.endif
