###############################################################################
# 
#  Copyright (c) 2005, 2006, 2007, 2008
#  University of Rochester
#  Department of Computer Science
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * Neither the name of the University of Rochester nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
# 
# 
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

# pull in all global Make variable definitions
include ../Makefile.inc
INCLUDEPATH = -I../ -I./

########################################
# .o and .h dependencies for building librstm.a
RSTM_OBJS = obj/rstm.o obj/GCHeap.o obj/ContentionManager.o
RSTM_HEADERS = rstm.hpp                     support/atomic_ops.h           \
               cm/ContentionManager.hpp     cm/CMPolicies.hpp              \
               support/ConflictDetector.hpp support/Inevitability.hpp      \
               support/MiniVector.hpp       support/MMPolicy.hpp           \
               support/defs.hpp             support/ThreadLocalPointer.hpp \
               support/TokenManager.hpp     ../config.h
LIBRSTM = obj/librstm.a

########################################
# .o and .h dependencies for building libredo_lock.a
REDO_LOCK_OBJS = obj/redo_lock.o obj/GCHeap.o \
                 obj/ContentionManager.o
REDO_LOCK_HEADERS = redo_lock.hpp                support/atomic_ops.h         \
                    cm/ContentionManager.hpp     cm/CMPolicies.hpp            \
                    support/ConflictDetector.hpp support/Inevitability.hpp    \
                    support/MiniVector.hpp       support/MMPolicy.hpp         \
                    support/defs.hpp             ../config.h \
                    support/ThreadLocalPointer.hpp 
LIBREDO_LOCK = obj/libredo_lock.a

########################################
# .o and .h dependencies for building libcgl.a
CGL_OBJS = obj/cgl.o obj/GCHeap.o
CGL_HEADERS = cgl.hpp          support/atomic_ops.h       \
              support/defs.hpp support/ThreadLocalPointer.hpp \
              ../config.h
LIBCGL = obj/libcgl.a

########################################
# .o and .h dependencies for building libealv.a
LLT_OBJS = obj/llt.o obj/GCHeap.o
LLT_HEADERS = llt.hpp                                             \
              support/atomic_ops.h support/MiniVector.hpp         \
              support/defs.hpp     support/ThreadLocalPointer.hpp \
              ../config.h          support/hrtime.h               \
              support/Bloom.hpp
LIBLLT = obj/libllt.a

########################################
# figure out which lib to copy to 'libstm.a'
LIBSTM = obj/libstm.a
BASELIB = obj/lib$(STM_VERSION).a

.PHONY: all
all:  obj/ $(LIBSTM)

obj/:
	mkdir $@

# to build the final lib, just copy another lib
$(LIBSTM): $(BASELIB) 
	cp $< $@

########################################
# build rules for librstm
$(LIBRSTM): $(RSTM_OBJS)
	$(AR) cru $@ $^
obj/rstm.o: rstm.cpp $(RSTM_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@

########################################
# build rules for libredo_lock
$(LIBREDO_LOCK): $(REDO_LOCK_OBJS)
	$(AR) cru $@ $^
obj/redo_lock.o: redo_lock.cpp $(REDO_LOCK_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@

########################################
# build rules for libcgl
$(LIBCGL): $(CGL_OBJS)
	$(AR) cru $@ $^
obj/cgl.o: cgl.cpp $(CGL_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@

########################################
# build rules for libllt
$(LIBLLT): $(LLT_OBJS)
	$(AR) cru $@ $^
obj/llt.o: llt.cpp $(LLT_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@

########################################
# build rules to clean up .o and .a files

# /clean/ gets rid of the configuration specified by STM_VERSION
clean:
	rm -f obj/*

# /realclean/ gets rid of everything you've built, all configurations
realclean:
	rm -rf obj/

########################################
# for building the GCHeap allocator
GCHEAP_HEADERS = support/atomic_ops.h support/ThreadLocalPointer.hpp \
                 support/defs.hpp     ../config.h

obj/GCHeap.o: support/GCHeap.cpp $(GCHEAP_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@

########################################
# for building the CM factory
CM_HEADERS = cm/ContentionManager.hpp \
             cm/Eruption.hpp     cm/Karma.hpp      cm/Justice.hpp\
             cm/Killblocked.hpp  cm/Polite.hpp     cm/Greedy.hpp \
             cm/Polkaruption.hpp cm/Timestamp.hpp  cm/Aggressive.hpp \
             cm/Highlander.hpp   cm/Whpolka.hpp    cm/Polka.hpp \
             cm/Polkavis.hpp     cm/Serializer.hpp cm/Timid.hpp \
             cm/Reincarnate.hpp \
             support/atomic_ops.h support/hrtime.h

obj/ContentionManager.o: cm/ContentionManager.cpp $(CM_HEADERS)
	$(CXX) $(CXXFLAGS) $(INCLUDEPATH) -c $< -o $@
