# ============================================
#
# Makefile for creating a new user library
#
# It is recommended that each library have its own makefile and
# its own subdirectory for clarity of organization.
# This subdirectory should contain all of the object files
# needed to compile the library as well as all header files,
# support files, and the script file for loading the library (e.g. userlib.g).
#
# The user should specify the values of the following makefile variables :
#
# GENESIS 
#	set this to the directory in which the simulator was installed
# LIBRARY_NAME
#	set this to the desired name of the library 
# FUNCTIONS
#	set this to the name of the file containing the list of
#	public function names in the library
# STRUCTURES
#	set this to the name of the file containing the structure definitions
#	for objects in the library
# EXT_HEADER
#	set this to the name of the header file containing the appropriate
#	include references needed to compile the library files
# TARGET_OBJ
#	set this to the name of the library object which will be compiled
#	This is normally just the LIBRARY_NAME with 'lib.o' appended to it
# OBJECTS
#	list the user object files (with the .o extension) which will be 
#	a part of the library
#
# ============================================

GENESIS		= 	/usr/genesis
LIBRARY_NAME 	= 	example
FUNCTIONS 	= 	examplefuncs
STRUCTURES 	= 	example_struct.h
EXT_HEADER	= 	example_ext.h
TARGET_OBJ	=	examplelib.o

OBJECTS = 

# ============================================
# everything below here should maintain itself
# ============================================

# If you want to use the debug option (which will cost you in both
# speed and memory), use the alternate CFLAGS = -g.
# Otherwise use "-O" to optimize.

CFLAGS 		= 	-O $(TOPFLAGS) -D$(MACHINE)
LIBS 		=  	-lm
CPP 		= 	/lib/cpp
GENESIS_LIB	= 	$(GENESIS)/lib
GENESIS_INCLUDE	= 	-I. -I$(GENESIS)/include
HEADERS 	=  	$(STRUCTURES)

default : $(TARGET_OBJ)

$(OBJECTS) : $(HEADERS) 

.c.o:
	$(CC) $(CFLAGS) $(GENESIS_INCLUDE) $< -c 

# make the data structure section of the symbol table

$(LIBRARY_NAME)_d@.c : $(STRUCTURES) $(GENESIS_LIB)/code_sym
	- $(CPP) $(STRUCTURES) /tmp/$(STRUCTURES) $(GENESIS_INCLUDE)
	- $(GENESIS_LIB)/code_sym /tmp/$(STRUCTURES) $(LIBRARY_NAME) \
	  -I $(EXT_HEADER) -NI -o $(LIBRARY_NAME)_d@.c
	- rm /tmp/$(STRUCTURES)

# make the function list section of the symbol table

$(LIBRARY_NAME)_f@.c : $(FUNCTIONS) $(GENESIS_LIB)/code_func
	- $(GENESIS_LIB)/code_func $(FUNCTIONS) $(LIBRARY_NAME) \
	  > $(LIBRARY_NAME)_f@.c

# make the library header function

$(LIBRARY_NAME)_l@.c: $(LIBRARY_NAME)_f@.c $(LIBRARY_NAME)_d@.c $(GENESIS_LIB)/code_lib $(OBJECTS)
	- $(GENESIS_LIB)/code_lib $(LIBRARY_NAME) -o $(LIBRARY_NAME)_l@.c

SYMBOLTAB = $(LIBRARY_NAME)_d@.o $(LIBRARY_NAME)_f@.o $(LIBRARY_NAME)_l@.o

$(TARGET_OBJ): $(OBJECTS) $(SYMBOLTAB)
	ld -r -o $(TARGET_OBJ) $(OBJECTS) $(SYMBOLTAB)

clean:
	-(rm -rf *.o; rm -rf *@.c)
