#**************************************************************************
# Fichero makefile.
# --------------------------------------------------------------------------
# Licencia GPL. Juan Gonzalez Gomez
# --------------------------------------------------------------------------
#
#***************************************************************************

VERSION = 1.0.1

# Edited for Debian GNU/Linux.
DESTDIR =

# ¿Dónde poner las librerias en el 'make install'?
LIB  = $(DESTDIR)/usr/lib

#-- Donde instalar las paginas man?
MAN  = $(DESTDIR)/usr/local/man

#-- Donde instalar los fichero .h?
INCLUDE = $(DESTDIR)/usr/local/include/stargate

DOC = $(DESTDIR)/usr/local/doc/stargate

#--- Librerias dinamicas
LIBSTARGATE_D  = libstargate.so.$(VERSION)
LIBSTARGATEH_D = libstargateh.so.$(VERSION)

#--- Librerias estaticas
LIBSTARGATE_S  = libstargate-$(VERSION).a
LIBSTARGATEH_S = libstargateh-$(VERSION).a

#---- Compilador
CC = gcc
CFLAGS = -g -Wall

all: libstargate-target libstargate-test

#-- Construccion de los ejemplos de prueba
libstargate-test: test-null test-null-hilos test-generic test-generic-hilos

#-- Construccion de la libreria estatica y dinamica
libstargate-target: $(LIBSTARGATE_D) $(LIBSTARGATEH_D) \
                    $(LIBSTARGATE_S) $(LIBSTARGATEH_S)

#----- Paquetes que componen la Libreria LIBSTARGATE y LIBSTARGATEH
LIBSTARGATE_DEP = sg-serial.o sg-tramas.o
LIBSTARGATEH_DEP = $(LIBSTARGATE_DEP) sg-motor.o

#---------------------------
#       OBJETIVOS
#---------------------------
# ---- Generacion de la libreria libstargate dinamica
$(LIBSTARGATE_D):  $(LIBSTARGATE_DEP)
	           $(CC) -shared -W1,-soname,libstargate.so -o $(LIBSTARGATE_D) \
                   $(LIBSTARGATE_DEP)
clean::
	  rm -f $(LIBSTARGATE_D) $(LIBSTARGATE_DEP)

# ----- Generacion de la libreria libstargateh dinamica
$(LIBSTARGATEH_D):  $(LIBSTARGATEH_DEP)
	           $(CC) -shared -W1,-soname,libstargateh.so -o $(LIBSTARGATEH_D) \
                   $(LIBSTARGATEH_DEP)
clean::
	  rm -f $(LIBSTARGATEH_D) $(LIBSTARGATEH_DEP)

# ----- Generacion de la libreria libstargate estatica
$(LIBSTARGATE_S):  $(LIBSTARGATE_DEP)
	           ar rs $(LIBSTARGATE_S) $(LIBSTARGATE_DEP)
clean::
	  rm -f $(LIBSTARGATE_S) $(LIBSTARGATE_DEP)    

# ----- Generacion de la libreria libstargateh estatica
$(LIBSTARGATEH_S):  $(LIBSTARGATEH_DEP)
	           ar rs $(LIBSTARGATEH_S) $(LIBSTARGATEH_DEP)
clean::
	  rm -f $(LIBSTARGATEH_S) $(LIBSTARGATEH_DEP)    

#--------------------------
#  Programas de prueba
#--------------------------
NAME1= test-null
DEP1 = $(NAME1).o $(LIBSTARGATE_S) consola_io.o

$(NAME1): $(DEP1)
	    $(CC) -o $(NAME1) $(DEP1)

clean::
	  rm -f $(NAME1) $(DEP1)
    
#--------------------------
NAME2= test-null-hilos
DEP2 = $(NAME2).o $(LIBSTARGATEH_S) consola_io.o animatxt.o

$(NAME2): $(DEP2)
	    $(CC) -o $(NAME2) $(DEP2) -lpthread

clean::
	  rm -f $(NAME2) $(DEP2)
    
#--------------------------
NAME3= test-generic
DEP3 = $(NAME3).o $(LIBSTARGATE_S) consola_io.o

$(NAME3): $(DEP3)
	    $(CC) -o $(NAME3) $(DEP3)

clean::
	  rm -f $(NAME3) $(DEP3)  

#--------------------------
NAME4= test-generic-hilos
DEP4 = $(NAME4).o $(LIBSTARGATEH_S) consola_io.o animatxt.o

$(NAME4): $(DEP4)
	    $(CC) -o $(NAME4) $(DEP4) -lpthread

clean::
	  rm -f $(NAME4) $(DEP4)
    
#-----------------------------
#--- Dependencias
#-----------------------------
sg-tramas.o : sg-tramas.h 
sg-serial.o : sg-serial.h
sg-motor.o  : sg-motor.h

#---------------------------
#-- INSTALACION
#---------------------------
install: #-- libstargate-target
	     install -d $(LIB) $(INCLUDE) 
	     install $(LIBSTARGATE_D) $(LIBSTARGATEH_D) $(LIB)
	     install $(LIBSTARGATE_S) $(LIBSTARGATEH_S) $(LIB)
	     install sg-serial.h sg-tramas.h sg-motor.h \
               stargate.h stargateh.h $(INCLUDE)
	     ln -s $(LIB)/$(LIBSTARGATE_D)  $(LIB)/libstargate.so
	     ln -s $(LIB)/$(LIBSTARGATEH_D) $(LIB)/libstargateh.so
	     ldconfig
                       

#---------------------------
#-- DESINSTALACION
#---------------------------
uninstall:
	   rm -f $(LIB)/$(LIBSTARGATE_D) $(LIB)/$(LIBSTARGATEH_D) \
           $(LIB)/$(LIBSTARGATE_S) $(LIB)/$(LIBSTARGATEH_S)
	   rm -f $(LIB)/libstargate.so $(LIB)/libstargateh.so
	   rm -f $(INCLUDE)/sg-serial.h $(INCLUDE)/sg-tramas.h \
           $(INCLUDE)/sg-motor.h  $(INCLUDE)/stargate.h $(INCLUDE)/stargateh.h

#--------------------------
#  REGLAS GENERICAS
#--------------------------
.c.o:		
		$(CC) $(CFLAGS) -c $<
