TOP=$(shell pwd)

DYNAMIC=-I$(TOP)/core/build
INCLUDES=-I$(TOP) -I$(TOP)/externals/include $(DYNAMIC)

include makeflags.inc

ifneq ($(PARALLEL),0)
DEFINES+=-DPARALLEL=$(PARALLEL)
endif

EXTRACFLAGS+=-Wno-unused -Wno-sign-compare # temporary until code is fixed
CFLAGS+=$(EXTRACFLAGS) $(DEFINES) $(INCLUDES)
CXXFLAGS+=$(EXTRACFLAGS) $(DEFINES) $(INCLUDES)

OBJS=engine controller gui sim_hal
ARCH=$(shell uname -s)-$(shell uname -m)
#LIBS=-Lexternals/matrix/lib/$(ARCH) -lmv -lsparse
#ifeq ($(DEBUG),1)
#LIBS+=-lsuperlu-mt_debug
#else
#LIBS+=-lsuperlu-mt
#endif
#LIBS+=-lf77blas -latlas -lspblas
LIBS+=-lpthread

ifeq ($(shell uname -n),powersim)
CXXFLAGS+=-DPOWERSIM=1
endif

# for loadflow using taps, this should be part of the code!
CXXFLAGS+=-DUSE_TAP=1 -D_DETECT_MISSING_TAP_

default: engine_wrapper.a $(OBJS)

engine_wrapper.a: core/build/engine_wrapper.c core/build/engine.h core/build/engine_debug.h
	cd core/build && if [ ! -L system ]; then rm -rf system; ln -s ../system; fi; make engine_wrapper.a && cd ../.. && cp -f core/build/engine_wrapper.a .

Sim_Driver.o: Sim_Driver.cpp Simulation.hpp

#engine: Sim_Driver.o Simulation.o PI.o PI_thread.o PI_atod.o engine_wrapper.a /opt/intel/cc/9.1.044/lib/libimf.a /opt/intel/cc/9.1.044/lib/libsvml.a /opt/intel/cc/9.1.044/lib/libguide.a /opt/intel/cc/9.1.044/lib/libirc.a
engine: Sim_Driver.o Simulation.o engine_wrapper.a
	#cd core/build && make TOP=$(TOP) engine && cd ../..
	#$(CXX) $(CXXFLAGS) $^ $(LIBS) -Wl,global01_=0x40200000 -o $@
	$(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@

# Simulation engine
Simulation.o: Simulation.cpp Simulation.hpp SimProfile.hpp

# HAL
HAL.o: HAL.cpp LPF.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) $< -c -o $@

sim_hal: HAL.o CAtoD.o CDtoA.o
	$(CXX) $^ -o $@
#	$(CXX) $^ -lcomedi -o $@

# non-GUI controller

controller: ui/Sim_Console.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) $^ -o $@

# GUI controller

Sim_GUI.o: ui/Sim_GUI.cpp
	$(CXX) $(CXXFLAGS) `pkg-config --cflags glib-2.0` `pkg-config --cflags gtk+-2.0` `pkg-config --cflags libglade-2.0` $^ -c -o $@
	if [ ! -f console.glade ]; then ln -s ui/console.glade; fi

UICallbacks.o: ui/UICallbacks.c
	$(CC) $(CFLAGS) `pkg-config --cflags glib-2.0` `pkg-config --cflags gtk+-2.0` `pkg-config --cflags libglade-2.0` $^ -c -o $@

gui: Sim_GUI.o UICallbacks.o
	$(CXX) -export-dynamic `pkg-config --libs gtk+-2.0` `pkg-config --libs libglade-2.0` `pkg-config --libs glib-2.0`  -lgthread-2.0 $^ -o $@

clean:
	rm -f *.o *.a $(OBJS) ui/*.o
	rm -f *.csv gmon.out *.profile.*
	if [ -L console.glade ]; then rm console.glade; fi
	rm -f svn-commit.tmp

reallyclean: clean
	cd core/build && make clean && cd ../..

.PHONY: clean reallyclean
