LIBOSL_BOARD  = libosl_board.a
LIBOSL_SEARCH = libosl_search.a

### tests for user defiend variables (better than nothing)
ifneq ($(strip $(USE_TCMALLOC)),$(USE_TCMALLOC))
$(error variable USE_TCMALLOC has white space(s))
endif
ifneq ($(strip $(USE_TBB_SCALABLE_ALLOCATOR)),$(USE_TBB_SCALABLE_ALLOCATOR))
$(error variable USE_TBB_SCALABLE_ALLOCATOR has white space(s))
endif
ifneq ($(strip $(USE_TBB)),$(USE_TBB))
$(error variable USE_TBB has white space(s))
endif
ifneq ($(strip $(USE_BOOST_POOL_ALLOCATOR)),$(USE_BOOST_POOL_ALLOCATOR))
$(error variable USE_BOOST_POOL_ALLOCATOR has white space(s))
endif

###
ifneq ($(CXX),icc)
CPUOPTION  ?=
else
CPUOPTION ?= -gxx-name=g++-4.2
endif
GXX	 ?= g++
CXX      ?= $(GXX)
# CXX_HOME ?= # default は空
USE_GCH ?= no
USE_BOOST_POOL_ALLOCATOR ?= 0
USE_TBB_SCALABLE_ALLOCATOR ?= 0
USE_TCMALLOC ?= 1
TCMALLOC ?= google-perftools-1.7
USE_TBB ?= 0
GTEST ?= gtest-1.1.0
PROTOBUF ?= protobuf-2.1.0
GPSSHOGI_HOME ?= $(OSL_HOME)/../gpsshogi
RPATH ?= -Wl,-rpath,
ifdef CXX_HOME
LDFLAGS_LOCAL ?= -L$(CXX_HOME)/lib $(RPATH)$(CXX_HOME)/lib
endif
BOOST_POSTFIX_MT ?= $(BOOST_POSTFIX)
LDLIBS += -lboost_filesystem$(BOOST_POSTFIX) -lboost_serialization$(BOOST_POSTFIX) -lboost_iostreams$(BOOST_POSTFIX) -lboost_date_time$(BOOST_POSTFIX) -lboost_system$(BOOST_POSTFIX)

ARC ?= ar cru
RANLIB ?= ranlib
DEBUGFLAGS ?= -g

ifdef PROFILE
OSL_LIB_DIR=$(OSL_HOME)/profile
else
ifdef RELEASE
OSL_LIB_DIR=$(OSL_HOME)/release
else
OSL_LIB_DIR=$(OSL_HOME)/lib
endif
endif

FILE_OSL_BOARD  = $(OSL_LIB_DIR)/libosl_board.a
FILE_OSL_SEARCH = $(OSL_LIB_DIR)/libosl_search.a
FILE_OSL_ALL = $(FILE_OSL_SEARCH) $(FILE_OSL_BOARD)


OSL_ADD_INCLUDE_DIR = $(OSL_HOME)/include
INCLUDES += -I$(OSL_ADD_INCLUDE_DIR) -D_REENTRANT
LDFLAGS +=  -L$(OSL_LIB_DIR)

ifeq ($(USE_BOOST_POOL_ALLOCATOR),1)
INCLUDES += -I$(GPSSHOGI_HOME)/include -DUSE_BOOST_POOL_ALLOCATOR
endif
ifeq ($(USE_TBB_SCALABLE_ALLOCATOR),1)
INCLUDES += -DUSE_TBB_SCALABLE_ALLOCATOR
LOADLIBES += -ltbbmalloc
endif
ifeq ($(USE_TCMALLOC),1)
ifeq ($(TCMALLOC),google-perftools-1.7)
FILE_TCMALLOC ?= $(OSL_HOME)/lib/third_party/$(TCMALLOC)/.libs/libtcmalloc_minimal.a
else
FILE_TCMALLOC ?= $(OSL_HOME)/lib/third_party/$(TCMALLOC)/libtcmalloc.a
endif
FILE_OSL_BOARD += $(FILE_TCMALLOC)
endif
ifeq ($(USE_TBB),1)
INCLUDES += -DOSL_USE_TBB
LOADLIBES += -ltbb
endif
LDLIBS += -lpthread

CXX_CPP_FLAGS = $(filter-out -I.gch,$(CXXFLAGS))
CXX_CPP_HEADER_FLAGS = -x c++-header

ifdef CXX_HOME
INCLUDES += -I$(CXX_HOME)/include
endif

WARN_INLINE ?=
# -Winline
ifeq ($(CXX),icc)
PROFILE_USE = -prof-use
PROFILE_GENERATE = -prof-gen
INCLUDES += -gcc-sys
WARNING_FLAGS = -w1 
RELEASE_CXX_OPTFLAGS ?= -fast -mcmodel=medium
LDFLAGS +=  -L/opt/intel/Compiler/11.0/081/lib/intel64/
else
PROFILE_USE = -static -fprofile-use -fprofile-correction
PROFILE_GENERATE = -fprofile-generate 
WARNING_FLAGS += -Wall -Wwrite-strings -Wextra -Wstrict-aliasing=2 -Woverloaded-virtual \
 -Wcast-align -Wwrite-strings -Wpointer-arith $(WARN_UNUSED) $(WARN_FIELD_INITIALIZER)
WARN_UNUSED ?= -Wno-unused-parameter
WARN_FIELD_INITIALIZER ?= -Wno-missing-field-initializers

RELEASE_CXX_OPTFLAGS ?= -O3 -funroll-loops  -funswitch-loops -fpeel-loops \
--param large-function-growth=48000 \
--param inline-unit-growth=8000 --param max-inline-insns-single=8000
endif

#PROFILE_FLAGS ?= -g -pg
PROFILE_FLAGS ?= -g 
ifdef PROFILE
PROF = $(PROFILE_FLAGS)
endif

# gcc 3.4
ifeq ($(USE_GCH),yes)
GCH_INCLUDES = -I$(OSL_LIB_DIR)/.gch -Winvalid-pch
endif


HEADERS = $(shell (cd $(OSL_HOME)/include; find . -name "*.h" -o -name "*.tcc"))
LDFLAGS +=  $(LDFLAGS_LOCAL) $(PROF)

%.s: %.cc %.o
	$(CXX) -S $(CXXFLAGS) $<
%.e: %.cc
	$(CXX) -E -o $@ $(CXXFLAGS) $<

.deps/%.cc.d: %.cc
	if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi; 
	$(GXX) -MM $(CXX_CPP_FLAGS) $< \
		| sed 's/\(.*\.o\)[ :]*/$(subst /,\/,$(patsubst .deps/%.cc.d,%.o,$@)) $(subst /,\/,$@) : /g' > $@
	grep $< $@ > /dev/null 2>&1 || (rm -f $@; false)
ifeq ($(USE_GCH),yes)
.deps/%.h.d: $(OSL_HOME)/include/%.h
	if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi; 
	$(GXX) -MM $(CXX_CPP_FLAGS) $(CXX_CPP_HEADER_FLAGS) $< \
		| sed 's/\(.*\.o\)[ :]*/$(subst /,\/,$(patsubst .deps/%.h.d,.gch/%.h.gch,$@)) $(subst /,\/,$@) : /g' > $@
	grep $< $@ > /dev/null 2>&1 || (rm -f $@; false)
.deps/%.tcc.d: $(OSL_HOME)/include/%.tcc
	if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi; 
	$(GXX) -MM $(CXX_CPP_FLAGS) $(CXX_CPP_HEADER_FLAGS) $< \
		| sed 's/\(.*\.o\)[ :]*/$(subst /,\/,$(patsubst .deps/%.tcc.d,.gch/%.tcc.gch,$@)) $(subst /,\/,$@) : /g' > $@
	grep $< $@ > /dev/null 2>&1 || (rm -f $@; false)

.gch/%.h.gch: $(OSL_HOME)/include/%.h
	if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi; 
	$(GXX) -o $@ -c $(CXX_CPP_FLAGS) $(CXX_CPP_HEADER_FLAGS) $< 
.gch/%.tcc.gch: $(OSL_HOME)/include/%.tcc
	if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi; 
	$(GXX) -o $@ -c $(CXX_CPP_FLAGS) $(CXX_CPP_HEADER_FLAGS) $< 
endif

# ;;; Local Variables:
# ;;; coding:utf-8
# ;;; End:
