#
# Makefile para izmdb
#
# Copyright (c) 2021 Felipe Sanchez
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files ("the Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# Prefijo de instalacion
PREFIX = /usr

# Destino de los archivos de biblioteca
LIBDIR     = $(PREFIX)/lib
INCLUDEDIR = $(PREFIX)/include

# Descomenta esta variable y recompila para activar
# los mensajes de depuracion de las bibliotecas.
#DEBUG_FLAG="-DDEBUG"

LIBS    = libizmdb.so libSiUSBXp.so
HEADERS = izmdb.h SiUSBXp.h
DEMOS   = lowlevel-izmdb-demo izmdb-demo

############################
##### Reglas generales #####
############################

all: $(LIBS) $(DEMOS)

clean:
	rm -f $(LIBS) $(DEMOS) *.o 
	$(MAKE) -C python clean

#######################
##### Bibliotecas #####
#######################

libSiUSBXp.so: SiUSBXp.c
	gcc $(DEBUG_FLAG) -shared -lusb -o libSiUSBXp.so SiUSBXp.c -Wall -fPIC

libizmdb.so: libSiUSBXp.so izmdb.c izmdb.h SiUSBXp.h
	gcc $(DEBUG_FLAG) -shared -I$(shell pwd) -L$(shell pwd) -lSiUSBXp -o libizmdb.so -Wall -fPIC izmdb.c



##############################
##### Enlaces con Python #####
##############################

python:
	make -C python

python-install: install
	make -C python install


#################################
##### Reglas de instalacion #####
#################################

install-dirs:
	install -d $(DESTDIR)$(LIBDIR) $(DESTDIR)/$(INCLUDEDIR)

install-libs: $(LIBS)
	install -D -m0755 $(LIBS) $(DESTDIR)$(LIBDIR)/

install-headers:
	install -D -m0644 $(HEADERS) $(DESTDIR)$(INCLUDEDIR)/

install: install-dirs install-libs install-headers

install-all: install python-install


###############################################
##### Programas de demostracion de la API #####
###############################################

demos: lowlevel-izmdb-demo izmdb-demo

lowlevel-izmdb-demo: $(LIBS) lowlevel-izmdb-demo.c
	gcc lowlevel-izmdb-demo.c -L$(shell pwd) -I$(shell pwd) -lizmdb -lSiUSBXp -lusb -o lowlevel-izmdb-demo

izmdb-demo: $(LIBS) izmdb-demo.c
	gcc izmdb-demo.c -L$(shell pwd) -I$(shell pwd) -lizmdb -lSiUSBXp -lusb -o izmdb-demo


.PHONY: all clean install install-all install-dirs install-libs install-headers  python python-install demos
