#
# ***** BEGIN LICENSE BLOCK *****
# 
#  Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
#  Copyright (C) 2017-2019 Olof Hagsand
#  Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate)
#
# This file is part of CLIXON
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Alternatively, the contents of this file may be used under the terms of
# the GNU General Public License Version 3 or later (the "GPL"),
# in which case the provisions of the GPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of the GPL, and not to allow others to
# use your version of this file under the terms of Apache License version 2, 
# indicate your decision by deleting the provisions above and replace them with
# the notice and other provisions required by the GPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the Apache License version 2 or the GPL.
#
# ***** END LICENSE BLOCK *****
#
prefix 		= @prefix@
datarootdir	= @datarootdir@
srcdir  	= @srcdir@
top_srcdir  	= @top_srcdir@
exec_prefix 	= @exec_prefix@
bindir 		= @bindir@
libdir		= @libdir@
dbdir		= @prefix@/db
mandir		= @mandir@
libexecdir	= @libexecdir@
localstatedir	= @localstatedir@
sysconfdir	= @sysconfdir@
HOST_VENDOR     = @host_vendor@

CLIXON_VERSION  = @CLIXON_VERSION@
CLIXON_MAJOR    = @CLIXON_VERSION_MAJOR@
CLIXON_MINOR    = @CLIXON_VERSION_MINOR@

VPATH       	= @srcdir@
CC		= @CC@
LINKAGE         = @LINKAGE@
CFLAGS		= @CFLAGS@
ifeq ($(LINKAGE),dynamic)
CFLAGS  	+= -fPIC
endif
SH_SUFFIX	= @SH_SUFFIX@
LIBSTATIC_SUFFIX = @LIBSTATIC_SUFFIX@
INSTALL		= @INSTALL@
INSTALL_LIB	= @INSTALL@

ifeq ($(HOST_VENDOR),apple)
INSTALLFLAGS =
else
INSTALLFLAGS = @INSTALLFLAGS@
endif

LDFLAGS 	= @LDFLAGS@
LIBS    	= @LIBS@ 

YACC		= @YACC@
LEX		= @LEX@

CPPFLAGS  	= @CPPFLAGS@

INCLUDES = -I. @INCLUDES@ -I$(top_srcdir)/lib/clixon -I$(top_srcdir)/include -I$(top_srcdir)

SRC     = clixon_sig.c clixon_uid.c clixon_log.c clixon_debug.c clixon_err.c clixon_event.c \
	  clixon_string.c clixon_map.c clixon_regex.c clixon_handle.c clixon_file.c \
	  clixon_xml.c clixon_xml_io.c clixon_xml_sort.c clixon_xml_map.c clixon_xml_vec.c \
	  clixon_xml_default.c clixon_xml_bind.c clixon_json.c clixon_proc.c \
	  clixon_yang.c clixon_yang_type.c clixon_yang_module.c clixon_netconf_monitoring.c \
	  clixon_yang_parse_lib.c clixon_yang_sub_parse.c \
          clixon_yang_cardinality.c clixon_yang_schema_mount.c \
          clixon_xml_changelog.c clixon_xml_nsctx.c \
	  clixon_path.c clixon_validate.c clixon_validate_minmax.c \
	  clixon_hash.c clixon_digest.c clixon_options.c clixon_data.c clixon_plugin.c \
	  clixon_proto.c clixon_proto_client.c \
	  clixon_xpath.c clixon_xpath_ctx.c clixon_xpath_eval.c clixon_xpath_function.c \
          clixon_xpath_optimize.c clixon_xpath_yang.c \
	  clixon_datastore.c clixon_datastore_write.c clixon_datastore_read.c \
	  clixon_netconf_lib.c clixon_netconf_input.c clixon_stream.c \
          clixon_nacm.c clixon_client.c clixon_netns.c \
	  clixon_dispatcher.c clixon_text_syntax.c

YACCOBJS = lex.clixon_xml_parse.o clixon_xml_parse.tab.o \
	    lex.clixon_yang_parse.o  clixon_yang_parse.tab.o \
	    lex.clixon_json_parse.o  clixon_json_parse.tab.o \
            lex.clixon_xpath_parse.o clixon_xpath_parse.tab.o \
            lex.clixon_api_path_parse.o clixon_api_path_parse.tab.o \
            lex.clixon_instance_id_parse.o clixon_instance_id_parse.tab.o \
            lex.clixon_text_syntax_parse.o clixon_text_syntax_parse.tab.o \
            lex.clixon_yang_sub_parse.o clixon_yang_sub_parse.tab.o \
            lex.clixon_yang_schemanode_parse.o clixon_yang_schemanode_parse.tab.o 

# Generated src 
GENSRC  = build.c 

OBJS    = $(YACCOBJS) $(SRC:.c=.o) 
GENOBJS  = $(GENSRC:.c=.o) 

# Linker-name: libclixon.so
# so-name: libclixon.so.2
# real-name: libclixon.so.2.0
MYNAME	     = clixon
MYLIBDYNAMIC = lib$(MYNAME)$(SH_SUFFIX).$(CLIXON_MAJOR).$(CLIXON_MINOR)
MYLIBSO      = lib$(MYNAME)$(SH_SUFFIX).$(CLIXON_MAJOR)
MYLIBLINK    = lib$(MYNAME)$(SH_SUFFIX)
MYLIBSTATIC  = lib$(MYNAME)$(LIBSTATIC_SUFFIX)

ifeq ($(LINKAGE),dynamic)
all:	 $(MYLIBLINK) $(MYLIBDYNAMIC)
else
all:	 $(MYLIBSTATIC)
endif

clean:
	rm -f $(OBJS) $(MYLIBLINK) $(MYLIBSTATIC) $(MYLIBDYNAMIC) $(GENOBJS) $(GENSRC) *.core
	rm -f clixon_xml_parse.tab.[ch] clixon_xml_parse.[o]
	rm -f clixon_yang_parse.tab.[ch] clixon_yang_parse.[o]
	rm -f clixon_json_parse.tab.[ch] clixon_json_parse.[o]
	rm -f clixon_xpath_parse.tab.[ch] clixon_xpath_parse.[o]
	rm -f clixon_api_path_parse.tab.[ch] clixon_api_path_parse.[o]
	rm -f clixon_instance_id_parse.tab.[ch] clixon_instance_id_parse.[o]
	rm -f clixon_text_syntax_parse.tab.[ch] clixon_text_syntax_parse.[o]
	rm -f clixon_yang_sub_parse.tab.[ch] clixon_yang_sub_parse.[o]
	rm -f clixon_yang_schemanode_parse.tab.[ch] clixon_yang_schemanode_parse.[o]
	rm -f lex.clixon_xml_parse.c
	rm -f lex.clixon_yang_parse.c
	rm -f lex.clixon_json_parse.c
	rm -f lex.clixon_xpath_parse.c
	rm -f lex.clixon_api_path_parse.c
	rm -f lex.clixon_instance_id_parse.c
	rm -f lex.clixon_text_syntax_parse.c
	rm -f lex.clixon_yang_sub_parse.c
	rm -f *.gcda *.gcno *.gcov # coverage

#############################################################################
# Implicit rules for lex and yacc.
#
# lex files *.l -> *.yy.c
# yacc files *.y -> *.tab.c and *.tab.h
#
# Lex forces yacc include file *.tab.h to be built.
#############################################################################

%.c : %.y  # cancel implicit yacc rule
%.c : %.l  # cancel implicit lex rule

# xml parser
lex.clixon_xml_parse.c : clixon_xml_parse.l clixon_xml_parse.tab.h
	$(LEX) -Pclixon_xml_parse clixon_xml_parse.l # -d is debug

clixon_xml_parse.tab.h: clixon_xml_parse.y
	$(YACC) -l -d -b clixon_xml_parse -p clixon_xml_parse clixon_xml_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_xml_parse.tab.c:	clixon_xml_parse.tab.h

lex.clixon_xml_parse.o : lex.clixon_xml_parse.c clixon_xml_parse.tab.h # special rule to for make clean to work
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# yang parser
lex.clixon_yang_parse.c : clixon_yang_parse.l clixon_yang_parse.tab.h
	$(LEX) -Pclixon_yang_parse clixon_yang_parse.l # -d is debug

clixon_yang_parse.tab.h: clixon_yang_parse.y
	$(YACC) -l -d -b clixon_yang_parse -p clixon_yang_parse clixon_yang_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_yang_parse.tab.c:	clixon_yang_parse.tab.h

lex.clixon_yang_parse.o : lex.clixon_yang_parse.c clixon_yang_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# json parser
lex.clixon_json_parse.c : clixon_json_parse.l clixon_json_parse.tab.h
	$(LEX) -Pclixon_json_parse clixon_json_parse.l # -d is debug

clixon_json_parse.tab.h: clixon_json_parse.y
	$(YACC) -l -d -b clixon_json_parse -p clixon_json_parse clixon_json_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_json_parse.tab.c:	clixon_json_parse.tab.h

lex.clixon_json_parse.o : lex.clixon_json_parse.c clixon_json_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# xpath parser
lex.clixon_xpath_parse.c : clixon_xpath_parse.l clixon_xpath_parse.tab.h
	$(LEX) -Pclixon_xpath_parse clixon_xpath_parse.l # -d is debug

clixon_xpath_parse.tab.h: clixon_xpath_parse.y
	$(YACC) -l -d -b clixon_xpath_parse -p clixon_xpath_parse clixon_xpath_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_xpath_parse.tab.c:	clixon_xpath_parse.tab.h

lex.clixon_xpath_parse.o : lex.clixon_xpath_parse.c clixon_xpath_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# api-path parser
lex.clixon_api_path_parse.c : clixon_api_path_parse.l clixon_api_path_parse.tab.h
	$(LEX) -Pclixon_api_path_parse clixon_api_path_parse.l # -d is debug

clixon_api_path_parse.tab.h: clixon_api_path_parse.y
	$(YACC) -l -d -b clixon_api_path_parse -p clixon_api_path_parse clixon_api_path_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_api_path_parse.tab.c:	clixon_api_path_parse.tab.h

lex.clixon_api_path_parse.o : lex.clixon_api_path_parse.c clixon_api_path_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# instance-identifier parser
lex.clixon_instance_id_parse.c : clixon_instance_id_parse.l clixon_instance_id_parse.tab.h
	$(LEX) -Pclixon_instance_id_parse clixon_instance_id_parse.l # -d is debug

clixon_instance_id_parse.tab.h: clixon_instance_id_parse.y
	$(YACC) -l -d -b clixon_instance_id_parse -p clixon_instance_id_parse clixon_instance_id_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_instance_id_parse.tab.c:	clixon_instance_id_parse.tab.h

lex.clixon_instance_id_parse.o : lex.clixon_instance_id_parse.c clixon_instance_id_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# text syntax parser
lex.clixon_text_syntax_parse.c : clixon_text_syntax_parse.l clixon_text_syntax_parse.tab.h
	$(LEX) -Pclixon_text_syntax_parse clixon_text_syntax_parse.l # -d is debug

clixon_text_syntax_parse.tab.h: clixon_text_syntax_parse.y
	$(YACC) -l -d -b clixon_text_syntax_parse -p clixon_text_syntax_parse clixon_text_syntax_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_text_syntax_parse.tab.c:	clixon_text_syntax_parse.tab.h

lex.clixon_text_syntax_parse.o : lex.clixon_text_syntax_parse.c clixon_text_syntax_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# yang-sub parser
lex.clixon_yang_sub_parse.c : clixon_yang_sub_parse.l clixon_yang_sub_parse.tab.h
	$(LEX) -Pclixon_yang_sub_parse clixon_yang_sub_parse.l # -d is debug

clixon_yang_sub_parse.tab.h: clixon_yang_sub_parse.y
	$(YACC) -l -d -b clixon_yang_sub_parse -p clixon_yang_sub_parse clixon_yang_sub_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_yang_sub_parse.tab.c:	clixon_yang_sub_parse.tab.h

lex.clixon_yang_sub_parse.o : lex.clixon_yang_sub_parse.c clixon_yang_sub_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

# descendant-schema-nodeid parser
lex.clixon_yang_schemanode_parse.c : clixon_yang_schemanode_parse.l clixon_yang_schemanode_parse.tab.h
	$(LEX) -Pclixon_yang_schemanode_parse clixon_yang_schemanode_parse.l # -d is debug

clixon_yang_schemanode_parse.tab.h: clixon_yang_schemanode_parse.y
	$(YACC) -l -d -b clixon_yang_schemanode_parse -p clixon_yang_schemanode_parse clixon_yang_schemanode_parse.y # -t is debug

# extra rule to avoid parallell yaccs
clixon_yang_schemanode_parse.tab.c:	clixon_yang_schemanode_parse.tab.h

lex.clixon_yang_schemanode_parse.o : lex.clixon_yang_schemanode_parse.c clixon_yang_schemanode_parse.tab.h
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -Wno-error -c $<

distclean: clean
	rm -f Makefile *~ .depend

# This is a way to not declare GENSRC as .PHONY. Instead rebuild
# as a side-effect of building library, but not when doing make install

prelib:
	rm -f $(GENSRC)

.SUFFIXES:
.SUFFIXES: .c .o

.c.o:
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -c $<
# Prefix befor $(CC): rm -f $@ && , but unsure of why

# Compute the length of the date command (and add some extra with XXX)
DATELEN = $(shell date +"%Y.%m.%d %H:%M by `whoami` on `hostname`XXXX"|wc -c)

build.c:	
	echo "/* This file is generated from the Clixon Makefile */" > $@;
	date +"const char CLIXON_BUILDSTR[$(DATELEN)]=\"%Y.%m.%d %H:%M by `whoami` on `hostname`"\"\; >> $@;
	echo "const char CLIXON_GITHASH[64]=\"$(shell git log --pretty="format:%(describe) %ci" -n1)\""\; >> $@;
	echo "const char CLIXON_VERSION[64]=\"$(CLIXON_VERSION)\""\; >> $@;

# Note: will always be remade since GENOBS is date dependent
$(MYLIBDYNAMIC) : $(GENOBJS) $(OBJS) 
ifeq ($(HOST_VENDOR),apple)
	$(CC) $(LDFLAGS) -shared -o $@ $(GENOBJS) $(OBJS) $(LIBS) -undefined dynamic_lookup -o $@
else
	$(CC) $(LDFLAGS) -shared -Wl,-soname,$(MYLIBSO) -o $@ $(GENOBJS) $(OBJS) $(LIBS) -Wl,-soname=$(MYLIBSO)
endif

# link-name is needed for application linking, eg for clixon_backend/clixon_cli
$(MYLIBLINK) : $(MYLIBDYNAMIC)

$(MYLIBSTATIC) :  $(GENOBJS) $(OBJS) 
	ar -crs $@  $(GENOBJS) $(OBJS)

install: install-lib

install-include:

ifeq ($(LINKAGE),dynamic)
install-lib: $(MYLIBDYNAMIC)
	$(INSTALL) -m 0755 -d $(DESTDIR)$(libdir)
	$(INSTALL_LIB) -m 0644 $(INSTALLFLAGS) $(MYLIBDYNAMIC) $(DESTDIR)$(libdir)
	ln -sf $(MYLIBDYNAMIC) $(DESTDIR)$(libdir)/$(MYLIBSO)     # -l:libclixon.so.3
	ln -sf $(MYLIBSO) $(DESTDIR)$(libdir)/$(MYLIBLINK) # -l:libclixon.so
else
install-lib: $(MYLIBSTATIC)
	$(INSTALL) -m 0755 -d $(DESTDIR)$(libdir)
	$(INSTALL_LIB) -m 0644 $(INSTALLFLAGS) $(MYLIBSTATIC) $(DESTDIR)$(libdir)
endif

uninstall: 
	rm -f $(DESTDIR)$(libdir)/$(MYLIBLINK)*
	rm -f $(DESTDIR)$(libdir)/$(MYLIBDYNAMIC)
	rm -f $(DESTDIR)$(libdir)/$(MYLIBSTATIC)

TAGS:
	find . -name '*.[chyl]' -print | etags -

depend:
	$(CC) $(DEPENDFLAGS) @DEFS@ $(INCLUDES) $(CFLAGS) -MM $(SRC) > .depend

#include .depend

