
#
#  check ROOTSYS
#
ifndef ROOTSYS
   TMP := $(shell echo " ========================================= " 1>&2 )
   TMP := $(shell echo "         ERROR: set ROOTSYS                " 1>&2 )
   TMP := $(shell echo " ========================================= " 1>&2 )
   THIS_MUST_CRASH
endif

ROOTCFLAGS   := $(shell root-config --cflags) 
ROOTLIBS     := $(shell root-config --glibs) -lTreePlayer -lMinuit
ROOTLIBDIR   := $(shell root-config --libdir)

#
#  System name
#
SYSTEM := $(shell uname -s)

#
#  Hardware platform
#
PLATFORM := $(shell uname -p)

#
#  Compiler setting
#
#
#      UseGCC34 := true
#      UseGCC := true
#      UseGCC4 := true
#      UseICC := true
#      UseAbsoft := true
#
@DEFINE_CC@


ifneq ($(findstring sukap,$(HOST)),)
  KAMIOKA_SUKAP64 := true 
endif

ifneq ($(findstring sukonh,$(HOST)),)
  KAMIOKA_SUKAP64 := true 
endif

######################################################################
#
#  Directory setting
#


######################################################################
#
#  Site libraries
#

SITE_LDOPTIONS = 
SITE_DEFINES   = -DNOSKLIBRARIES
SITE_INCLUDES  = $(LOCAL_INC) $(ROOTCFLAGS) -I$(WCSIMDIR)/include
SITE_LIBRARIES =  @SYSLIB@
SITE_LDLIBS = 
ALLDEFINES = 
OBJFJT =

#For Darwin/OSX
ifeq ($(strip $(SYSTEM)),Darwin) 
 SITE_LDOPTIONS += -Wl,-search_paths_first
else
 SITE_LDOPTIONS += -Wl,-z -Wl,muldefs
endif 

# for Linux, x86_64
ifeq ($(strip $(SYSTEM)),Linux) 
  ifeq ($(strip $(PLATFORM)),x86_64) 
    ifdef UseG77
      SITE_LDLIBS += /usr/lib64/libg2c.so.0  
    endif
  endif
endif

# for CERNLIB, etc.
#CLIB = `cernlib graflib grafX11 packlib mathlib kernlib lapack3 blas`

##ifeq ($(strip $(PLATFORM)),x86_64) 
##  CLIB = -L/usr/X11R6/lib64 -lgraflib -lgrafX11 -lpacklib -lnsl -lcrypt -lX11 
##else
##   CLIB = -L/usr/X11R6/lib -lgeant321 -lgraflib -lgrafX11 -lpacklib \
##	-lmathlib -lkernlib -lnsl -lcrypt -lX11
##endif

ifeq ($(strip $(SYSTEM)),Linux) 
  ALLDEFINES = -Dlinux -D__linux__
endif

ifeq ($(strip $(SYSTEM)),SunOS) 
  ALLDEFINES = -Dsun -DSolaris
endif

##########################################################################
### for sample programs
##########################################################################
LIBS	= -lm -lstdc++

LDOPTS	= $(SITE_LDOPTIONS)

## some sample programs require $(LIBS) $(LIBS) on sukapXXX 
## may be the order isn't optimized.. y.takeuchi (02-JAN-2009)
LDLIBS	=  $(LDOPTS) $(OBJFJT) $(LOCAL_LIBS) $(LIBS) $(LIBS) $(CLIB) $(ROOTLIBS) \
	   $(SITE_LIBRARIES) $(SITE_LDLIBS)

######################################################################
# Set the global debug level here.  You can override this for a particular
# compiler by overriding that flag (for example FDEBUGFLAGS)

SKDEBUGFLAGS = -g -O

######################################################################
#
#  C Compiler setting
#

ifdef UseGCC34
CC = gcc34
CDEBUGFLAGS = $(SKDEBUGFLAGS) 
COPTIONS  = -Wstrict-prototypes -fpcc-struct-return -Wall -Wno-unused -fpic
endif

ifdef UseGCC
CC = gcc
CDEBUGFLAGS = $(SKDEBUGFLAGS)
COPTIONS  = -Wstrict-prototypes -fpcc-struct-return -Wall -Wno-unused -fpic
endif

ifdef UseGCC4
CC = gcc4
CDEBUGFLAGS = $(SKDEBUGFLAGS) 
COPTIONS  = -Wstrict-prototypes -fpcc-struct-return -Wall -Wno-unused -fpic
endif

ifdef UseICC
CC = icc
CDEBUGFLAGS = $(SKDEBUGFLAGS)
COPTIONS = -ansi -mp -no-gcc -w1
endif

CDEFINES = $(ALLDEFINES) $(SITE_DEFINES)
CINCLUDES = $(SITE_INCLUDES) -I.  
CFLAGS = $(CDEBUGFLAGS) $(CCOPTIONS) $(CINCLUDES) $(CDEFINES)


######################################################################
#
#  C++ Compiler setting
#

ifdef UseGCC34
CXX = g++34
CXXDEBUGFLAGS = $(SKDEBUGFLAGS)
CXXOPTIONS = -fpic
endif

ifdef UseGCC
CXX = g++
CXXDEBUGFLAGS = $(SKDEBUGFLAGS)
CXXOPTIONS = -fpic
endif

ifdef UseGCC4
CXX = g++
CXXDEBUGFLAGS = $(SKDEBUGFLAGS)
CXXOPTIONS = -fpic
endif

ifdef UseICC
CXX = icc
CXXDEBUGFLAGS = $(SKDEBUGFLAGS)
CXXOPTIONS = -ansi -mp -no-gcc -w1 -fpic
endif

CXXDEFINES = $(ALLDEFINES) $(SITE_DEFINES)
CXXINCLUDES = $(SITE_INCLUDES) -I. 
CXXFLAGS =  $(CXXDEBUGFLAGS) $(CXXOPTIONS) $(CXXINCLUDES) $(CXXDEFINES)



######################################################################
#
#  variable setting
#

SHELL   = @SHELL@
AR      = ar clq
CPP     = /lib/cpp
MKDIR   = mkdir -p
SED     = sed
TOUCH   = touch
RANLIB  = ranlib
MV      = mv
LN      = ln -s
CP      = cp
MAKE    = @GNU_MAKE@

# The yacc command
#YACC	= bison -y
# If you installed Fedora12, you have to use byacc instead of bison. See $SKOFL/doc/Fedora.txt
#YACC	= byacc
# 2009/12/10 bison or byacc is set by autoconf
YACC	= @YACC@

YACCLIB	=
# 2009/12/10 seems not used
#CCYACC	= bison -y

# The lex command.
LEX	= flex -l
LEXLIB	= -lfl

SKOFL_CONFIGDIR      = $(SKOFL_ROOT)/config/
CONFIGDIR            = $(SKOFL_CONFIGDIR)

INSTALL     = $(SHELL) $(CONFIGDIR)/install-sh
INSTALL_BIN = $(INSTALL) -c -m 0755
INSTALL_LIB = $(INSTALL) -c -m 0644
INSTALL_INC = $(INSTALL) -c -m 0444
INSTALL_DAT = $(INSTALL) -c -m 0444
INSTALL_MAN = $(INSTALL) -c -m 0444

FH2H    = $(CONFIGDIR)/fh2h.pl

### ROOT macro (.C) is moved to the lowest priority
.SUFFIXES:
.SUFFIXES: .o .cxx .cc .c .cpp .so .h .C .l

.c.o :
	$(RM) $@
	$(CC) -c $(CFLAGS) -o $@ $(CURDIR)/$< 

.cc.o :
	$(RM) $@
	$(CXX) -c $(CXXFLAGS) -o $@ $(CURDIR)/$< 

.cpp.o :
	$(RM) $@
	$(CXX) -c $(CXXFLAGS) -o $@ $(CURDIR)/$< 

.C.o :
	$(RM) $@
	$(CXX) -c $(CXXFLAGS) -o $@ $(CURDIR)/$< 

.cxx.o :
	$(RM) $@
	$(CXX) -c $(CXXFLAGS) -o $@ $(CURDIR)/$< 

### for sample programs
.cc:
	$(RM) $@.o $@
	$(CXX) $(CXXFLAGS) -c  $(CURDIR)/$< 
	LD_RUN_PATH=$(SKOFL_LIBDIR):$(A_LIBDIR) $(CXX) $(CXXFLAGS) -o $* $*.o $(LDLIBS) 

.c:
	$(RM) $@.o $@
	$(CC) $(CFLAGS) -c  $(CURDIR)/$< 
	LD_RUN_PATH=$(SKOFL_LIBDIR):$(A_LIBDIR) $(CC) $(CFLAGS) -o $* $*.o $(LDLIBS) 

.o:
	LD_RUN_PATH=$(SKOFL_LIBDIR):$(A_LIBDIR) $(CXX) $(CXXFLAGS) -o $* $*.o $(LDLIBS) 


 _NULLCMD_ = @ echo -n

# Define the command used for recursive cases in the makefiles.
# Please note that even with an error this will keep building
# without stopping. So, be careful. Don't use this for the library
# targets.  Use a phony target instead.  See CWW if you have a
# question.  

define recursive-command
        list='$(SUBDIRS)'; for subdir in $$list; do \
	( if test -d $$subdir; then cd "$$subdir" && $(MAKE) $@; fi ); \
        done
endef

# You must use this with a phony subdirectory target.
define recursive-library-build
if test -d $@; then $(MAKE) -C $@ lib; fi
endef
