
SHELL  := /bin/sh
CD     := cd
MV     := /bin/mv
RM     := /bin/rm
CP     := /bin/cp
PWD    = $(shell /bin/pwd)

export TRICK_HOST_TYPE := $(shell export TRICK_HOME=$(TRICK_HOME) && $(TRICK_HOME)/bin/trick-gte TRICK_HOST_TYPE)
export TRICK_VERSION := $(shell export TRICK_HOME=$(TRICK_HOME) && $(TRICK_HOME)/bin/trick-version -v)
export TRICK_MAJOR := $(shell export TRICK_HOME=$(TRICK_HOME) && $(TRICK_HOME)/bin/trick-version -y)
export TRICK_MINOR := $(shell export TRICK_HOME=$(TRICK_HOME) && $(TRICK_HOME)/bin/trick-version -m)

include $(TRICK_HOME)/share/trick/makefiles/config_${TRICK_HOST_TYPE}.mk
-include $(TRICK_HOME)/share/trick/makefiles/config_user.mk

ifndef TRICK_CC
 export TRICK_CC := $(CC)
endif
ifndef TRICK_CPPC
 export TRICK_CPPC := $(CXX)
endif
ifndef TRICK_LD
 export TRICK_LD := $(LD)
endif

ifndef TRICK_SFLAGS
 export TRICK_SFLAGS :=
endif

export TRICK_CFLAGS
export TRICK_CXXFLAGS
export TRICK_LDFLAGS
export TRICK_SFLAGS
export TRICK_ICG_EXCLUDE
export TRICK_ICG_NOCOMMENT
export TRICK_SWIG_EXCLUDE
export TRICK_GTE_EXT
export TRICK_HOST_CPU := $(shell export TRICK_CC=$(TRICK_CC) && $(TRICK_HOME)/bin/trick-gte TRICK_HOST_CPU)

ifndef CONFIG_MK
ifneq ($(MAKECMDGOALS), clean)
$(error Please run $(TRICK_HOME)/configure before running make)
endif
endif

# Do this section only once
ifndef TRICK_MAKE_COMMON
export TRICK_MAKE_COMMON := 1

export TRICK_INCLUDES := -I${TRICK_HOME}/trick_source -I../include -I${TRICK_HOME}/include -I${TRICK_HOME}/include/trick/compat
export TRICK_VERSIONS := -DTRICK_VER=$(TRICK_MAJOR) -DTRICK_MINOR=$(TRICK_MINOR)
TRICK_CFLAGS += $(TRICK_INCLUDES) $(TRICK_VERSIONS) -fpic
ifdef TRICK_CXXFLAGS
    TRICK_CXXFLAGS += $(TRICK_INCLUDES) $(TRICK_VERSIONS) -fpic
else
    TRICK_CXXFLAGS := ${TRICK_CFLAGS}
endif
TRICK_SFLAGS += -I${TRICK_HOME}/share/trick $(TRICK_INCLUDES) $(TRICK_VERSIONS)

# append additional c and cxx flags defined in config_*.mk files.
TRICK_CFLAGS += $(TRICK_ADDITIONAL_CFLAGS)
TRICK_CXXFLAGS += $(TRICK_ADDITIONAL_CXXFLAGS)

# -fexceptions is included to propogate execptions through C code.
TRICK_CFLAGS += -fexceptions

HAVE_ZEROCONF ?= 0

ifeq ($(HAVE_ZEROCONF),1)
TRICK_CFLAGS += -DHAVE_ZEROCONF
TRICK_CXXFLAGS += -DHAVE_ZEROCONF
endif

export USE_ER7_UTILS_INTEGRATORS := 0
ifneq ("$(wildcard $(TRICK_HOME)/trick_source/er7_utils)","")  # if the er7_utils directory exists
  ER7_UTILS_HOME := $(TRICK_HOME)/trick_source/er7_utils
  USE_ER7_UTILS_INTEGRATORS := 1
  TRICK_CFLAGS += -DUSE_ER7_UTILS_INTEGRATORS
  TRICK_CXXFLAGS += -DUSE_ER7_UTILS_INTEGRATORS
endif

endif #TRICK_MAKE_COMMON

ifeq ($(TRICK_FORCE_32BIT), 1)
 TRICK_CFLAGS += -m32
 TRICK_CXXFLAGS += -m32
 TRICK_LDFLAGS += -m32
endif

IS_CC_CLANG = $(shell ${TRICK_CPPC} -xc /dev/null -dM -E | grep -c __clang__)
ifeq ($(IS_CC_CLANG), 1)
 TRICK_CFLAGS += -fcolor-diagnostics
 TRICK_CXXFLAGS += -fcolor-diagnostics
else
 GCC_MAJOR:=$(shell $(TRICK_CC) -dumpversion | cut -f1 -d.)
 GCC_MINOR:=$(shell $(TRICK_CC) -dumpversion | cut -f2 -d.)
endif

ifneq ($(HDF5),)
ifneq ($(HDF5),/usr)
 HDF5_LIB := -L$(HDF5)/lib -lhdf5_hl -lhdf5 -lsz
 HAVE_HDF5_HOME_LIB64 := $(shell test -e ${HDF5}/lib64 && echo "1")
 ifeq ($(HAVE_HDF5_HOME_LIB64),1)
   HDF5_LIB = -L$(HDF5)/lib64 $(HDF5_LIB)
 endif
else
 HDF5_LIB = -lhdf5_hl -lhdf5
endif
endif

HAVE_GSL := $(shell test -e ${GSL_HOME}/include/gsl && echo "1")
ifeq ($(HAVE_GSL),1)
  ifneq ($(GSL_HOME),/usr)
  HAVE_GSL_HOME_LIB64 := $(shell test -e ${GSL_HOME}/lib64 && echo "1")
    ifeq ($(HAVE_GSL_HOME_LIB64),1)
      TRICK_EXEC_LINK_LIBS += -L$(GSL_HOME)/lib64
    endif
  TRICK_EXEC_LINK_LIBS += -L$(GSL_HOME)/lib
  endif
  TRICK_EXEC_LINK_LIBS += -lgsl -lgslcblas
endif

TRICK_INCLUDE = $(shell $(PERL) -e '@inc_paths = "${TRICK_CFLAGS}" =~ /-I\s*(\S+)/g ; foreach $$i (@inc_paths) { print "-I$$i "}')
TRICK_DEFINES = $(shell $(PERL) -e '@defines = "${TRICK_CFLAGS}" =~ /-D\s*(\S+)/g ; foreach $$i (@defines) { print "-D$$i "}')

IO_SRC_DIR     := io_src/
OBJ_DIR        := object_${TRICK_HOST_CPU}
TRICK_STATIC_LIB := ${TRICK_LIB_DIR}/libtrick.a
TRICK_RANLIB =

DEPFILE = $(PWD)/Makefile_deps
DEPTEMPFILE = $(PWD)/Makefile_temp_depends

