# makefile for building python swig interfaces from c files
# ---------------------------------------------------------
# this is designed to work in a directory structure where your c object
# files are stored in another directory, with an independent makefile
# containing rules for each object file and a 'clean' rule, and the 
# interface (.i) file in the directory with this makefile (consider 
# it your python/c interface project directory)
#
# TARGET is the resultant .so file for importing to python
# INTERFACE is the interface (.i) file
# MYOBJS is a list of c object files in your c project directory
# OBJDIR is the relative path to your c project directory to the
#   interface project directory

OBJS=_ksim.so
INTERFACE=controller.i
MYOBJS:=controller.o
OBJLIB=.

include ../../../Makefile.cfg
include ../../../Makefile.src

CC=gcc
SWIG=swig
LD=ld
CC_FLAGS=-c -g
SWIG_FLAGS=-python
LD_FLAGS=-shared

MYOBJS:=$(patsubst %.o, $(OBJLIB)/%.o, $(MYOBJS))

all::_ksim.so sim_control

sim_control: sim_control.o controller.o
	gcc controller.o sim_control.o -o sim_control

sim_control.o: sim_control.c controller.h
	gcc -c sim_control.c -o sim_control.o

$(OBJS): $(INTERFACE:.i=_wrap.o) $(MYOBJS) 
	$(LD) $(LD_FLAGS) $^ -o $@

$(INTERFACE:.i=_wrap.o): $(INTERFACE:.i=_wrap.c) $(MYOBJS:.o=.h)
	$(CC) $(CC_FLAGS) $(INTERFACE:.i=_wrap.c) $(PYTHON_INCLUDE) -I$(OBJLIB)

$(INTERFACE:.i=_wrap.c): $(INTERFACE)
	$(SWIG) $(SWIG_FLAGS) $<

$(OBJLIB)/%.o: $(OBJLIB)/%.c $(OBJLIB)/%.h
	(cd $(OBJLIB) && $(CC) $(CC_FLAGS) $(@F:.o=.c))

clean::
	rm -f $(OBJS) *.o *.html *.doc $(INTERFACE:.i=_wrap.c) core test*.cod $(OBJLIB)/core ksim.py sim_control *.pyc



# notes on some make details:
# ---------------------------
# := variable assignments work like a normal language;
# = is a recursive variable assignment
# :: allows multiple definitions
#
# $(patsubt a, b, c) returns a string with all instances of the pattern
#   'a' found in 'c' replaced by 'b'
# $(varname:a=b) returns a string with all instances of the string 'a'
#   replaced by 'b' in the variable varname)
# % is used in pattern matching in a fairly intuitive manner
#
# $@ is the target of a rule
# $(@F) is just the file part of the target of a rule (so that for a rule
#    dir/file.o, $(@F) is equivalent to file.o)
# 
