#
# libfrr
#

lib_LTLIBRARIES += lib/libfrr.la
lib_libfrr_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0 -Xlinker -e_libfrr_version
lib_libfrr_la_LIBADD = $(LIBCAP) $(UNWIND_LIBS) $(LIBYANG_LIBS) $(LUA_LIB) $(UST_LIBS) $(LIBCRYPT) $(LIBDL) $(LIBM)

lib_libfrr_la_SOURCES = \
	lib/admin_group.c \
	lib/affinitymap.c \
	lib/affinitymap_cli.c \
	lib/affinitymap_northbound.c \
	lib/agg_table.c \
	lib/atomlist.c \
	lib/asn.c \
	lib/base64.c \
	lib/bfd.c \
	lib/buffer.c \
	lib/checksum.c \
	lib/command.c \
	lib/command_graph.c \
	lib/command_lex.l \
	lib/command_match.c \
	lib/command_parse.y \
	lib/cspf.c \
	lib/csv.c \
	lib/darr.c \
	lib/debug.c \
	lib/defaults.c \
	lib/distribute.c \
	lib/explicit_bzero.c \
	lib/ferr.c \
	lib/filter.c \
	lib/filter_cli.c \
	lib/filter_nb.c \
	lib/flex_algo.c \
	lib/frrcu.c \
	lib/frrlua.c \
	lib/frrscript.c \
	lib/frr_pthread.c \
	lib/frrstr.c \
	lib/grammar_sandbox.c \
	lib/graph.c \
	lib/hash.c \
	lib/hook.c \
	lib/id_alloc.c \
	lib/if.c \
	lib/if_rmap.c \
	lib/imsg-buffer.c \
	lib/imsg.c \
	lib/iso.c \
	lib/jhash.c \
	lib/json.c \
	lib/keychain.c \
	lib/keychain_cli.c \
	lib/keychain_nb.c \
	lib/ldp_sync.c \
	lib/lib_errors.c \
	lib/lib_vty.c \
	lib/libagentx.c \
	lib/libfrr.c \
	lib/libfrr_trace.c \
	lib/linklist.c \
	lib/link_state.c \
	lib/log.c \
	lib/log_filter.c \
	lib/log_vty.c \
	lib/md5.c \
	lib/memory.c \
	lib/mgmt_be_client.c \
	lib/mgmt_fe_client.c \
	lib/mgmt_msg.c \
	lib/mgmt_msg_native.c \
	lib/mlag.c \
	lib/module.c \
	lib/mpls.c \
	lib/srv6.c \
	lib/network.c \
	lib/nexthop.c \
	lib/netns_linux.c \
	lib/netns_other.c \
	lib/nexthop_group.c \
	lib/northbound.c \
	lib/northbound_cli.c \
	lib/northbound_db.c \
	lib/northbound_oper.c \
	lib/ntop.c \
	lib/openbsd-tree.c \
	lib/pid_output.c \
	lib/plist.c \
	lib/prefix.c \
	lib/privs.c \
	lib/ptm_lib.c \
	lib/pullwr.c \
	lib/qobj.c \
	lib/ringbuf.c \
	lib/routemap.c \
	lib/routemap_cli.c \
	lib/routemap_northbound.c \
	lib/sbuf.c \
	lib/seqlock.c \
	lib/sha256.c \
	lib/sigevent.c \
	lib/skiplist.c \
	lib/sockopt.c \
	lib/sockunion.c \
	lib/spf_backoff.c \
	lib/segment_routing.c \
	lib/srcdest_table.c \
	lib/stream.c \
	lib/strformat.c \
	lib/strlcat.c \
	lib/strlcpy.c \
	lib/systemd.c \
	lib/table.c \
	lib/termtable.c \
	lib/event.c \
	lib/typerb.c \
	lib/typesafe.c \
	lib/vector.c \
	lib/vrf.c \
	lib/vty.c \
	lib/wheel.c \
	lib/workqueue.c \
	lib/xref.c \
	lib/yang.c \
	lib/yang_translator.c \
	lib/yang_wrappers.c \
	lib/zclient.c \
	lib/zlog.c \
	lib/zlog_5424.c \
	lib/zlog_5424_cli.c \
	lib/zlog_live.c \
	lib/zlog_recirculate.c \
	lib/zlog_targets.c \
	lib/printf/printf-pos.c \
	lib/printf/vfprintf.c \
	lib/printf/glue.c \
	lib/routing_nb.c \
	lib/routing_nb_config.c \
	lib/tc.c \
	# end

nodist_lib_libfrr_la_SOURCES = \
	yang/frr-affinity-map.yang.c \
	yang/frr-filter.yang.c \
	yang/frr-if-rmap.yang.c \
	yang/frr-interface.yang.c \
	yang/frr-route-map.yang.c \
	yang/frr-route-types.yang.c \
	yang/frr-vrf.yang.c \
	yang/frr-routing.yang.c \
	yang/frr-nexthop.yang.c \
	yang/ietf/frr-deviations-ietf-key-chain.yang.c \
	yang/ietf/ietf-routing-types.yang.c \
	yang/ietf/ietf-netconf-acm.yang.c \
	yang/ietf/ietf-key-chain.yang.c \
	yang/ietf/ietf-interfaces.yang.c \
	yang/ietf/ietf-bgp-types.yang.c \
	yang/frr-module-translator.yang.c \
	# end

# Add logic to build mgmt.proto
lib_libfrr_la_LIBADD += $(PROTOBUF_C_LIBS)

BUILT_SOURCES += \
	lib/mgmt.pb-c.c \
	lib/mgmt.pb-c.h \
	# end

CLEANFILES += \
	lib/mgmt.pb-c.h \
	lib/mgmt.pb-c.c \
	# end

lib_libfrr_la_SOURCES += \
	lib/mgmt.pb-c.c \
	#end

if SQLITE3
lib_libfrr_la_LIBADD += $(SQLITE3_LIBS)
lib_libfrr_la_SOURCES += lib/db.c
endif

clippy_scan += \
	lib/affinitymap_cli.c \
	lib/if.c \
	lib/filter_cli.c \
	lib/if_rmap.c \
	lib/keychain_cli.c \
	lib/log_vty.c \
	lib/mgmt_be_client.c \
	lib/mgmt_fe_client.c \
	lib/nexthop_group.c \
	lib/northbound_cli.c \
	lib/plist.c \
	lib/routemap.c \
	lib/routemap_cli.c \
	lib/event.c \
	lib/vty.c \
	lib/zlog_5424_cli.c \
	# end

pkginclude_HEADERS += \
	lib/admin_group.h \
	lib/affinitymap.h \
	lib/agg_table.h \
	lib/asn.h \
	lib/atomlist.h \
	lib/base64.h \
	lib/bfd.h \
	lib/bitfield.h \
	lib/buffer.h \
	lib/checksum.h \
	lib/mlag.h \
	lib/command.h \
	lib/command_graph.h \
	lib/command_match.h \
	lib/compiler.h \
	lib/cspf.h \
	lib/csv.h \
	lib/darr.h \
	lib/db.h \
	lib/debug.h \
	lib/defaults.h \
	lib/distribute.h \
	lib/ferr.h \
	lib/filter.h \
	lib/flex_algo.h \
	lib/freebsd-queue.h \
	lib/frrdistance.h \
	lib/frrlua.h \
	lib/frrscript.h \
	lib/frr_pthread.h \
	lib/frratomic.h \
	lib/frrcu.h \
	lib/frrsendmmsg.h \
	lib/frrstr.h \
	lib/graph.h \
	lib/hash.h \
	lib/hook.h \
	lib/iana_afi.h \
	lib/id_alloc.h \
	lib/if.h \
	lib/if_rmap.h \
	lib/imsg.h \
	lib/ipaddr.h \
	lib/iso.h \
	lib/jhash.h \
	lib/json.h \
	lib/keychain.h \
	lib/ldp_sync.h \
	lib/lib_errors.h \
	lib/lib_vty.h \
	lib/libagentx.h \
	lib/libfrr.h \
	lib/libfrr_trace.h \
	lib/libospf.h \
	lib/linklist.h \
	lib/link_state.h \
	lib/log.h \
	lib/log_vty.h \
	lib/md5.h \
	lib/memory.h \
	lib/mgmt.pb-c.h \
	lib/mgmt_be_client.h \
	lib/mgmt_defines.h \
	lib/mgmt_fe_client.h \
	lib/mgmt_msg.h \
	lib/mgmt_msg_native.h \
	lib/mgmt_pb.h \
	lib/module.h \
	lib/monotime.h \
	lib/mpls.h \
	lib/srv6.h \
	lib/network.h \
	lib/nexthop.h \
	lib/nexthop_group.h \
	lib/nexthop_group_private.h \
	lib/northbound.h \
	lib/northbound_cli.h \
	lib/northbound_db.h \
	lib/ns.h \
	lib/openbsd-queue.h \
	lib/openbsd-tree.h \
	lib/plist.h \
	lib/prefix.h \
	lib/printfrr.h \
	lib/privs.h \
	lib/ptm_lib.h \
	lib/pullwr.h \
	lib/pw.h \
	lib/qobj.h \
	lib/queue.h \
	lib/ringbuf.h \
	lib/routemap.h \
	lib/route_opaque.h \
	lib/sbuf.h \
	lib/seqlock.h \
	lib/sha256.h \
	lib/sigevent.h \
	lib/skiplist.h \
	lib/smux.h \
	lib/sockopt.h \
	lib/sockunion.h \
	lib/spf_backoff.h \
	lib/segment_routing.h \
	lib/srcdest_table.h \
	lib/srte.h \
	lib/stream.h \
	lib/systemd.h \
	lib/table.h \
	lib/termtable.h \
	lib/frrevent.h \
	lib/trace.h \
	lib/typerb.h \
	lib/typesafe.h \
	lib/vector.h \
	lib/vlan.h \
	lib/vrf.h \
	lib/vrf_int.h \
	lib/vty.h \
	lib/vxlan.h \
	lib/wheel.h \
	lib/workqueue.h \
	lib/xref.h \
	lib/yang.h \
	lib/yang_translator.h \
	lib/yang_wrappers.h \
	lib/zclient.h \
	lib/zebra.h \
	lib/zlog.h \
	lib/zlog_5424.h \
	lib/zlog_live.h \
	lib/zlog_recirculate.h \
	lib/zlog_targets.h \
	lib/pbr.h \
	lib/tc.h \
	lib/routing_nb.h \
	\
	lib/assert/assert.h \
	# end


nodist_pkginclude_HEADERS += \
	lib/route_types.h \
	lib/version.h \
	# end

noinst_HEADERS += \
	lib/clippy.h \
	lib/plist_int.h \
	lib/printf/printfcommon.h \
	lib/printf/printflocal.h \
	#end

# General note about module and module helper library (libfrrsnmp, libfrrzmq)
# linking:  If we're linking libfrr statically into daemons, we *must* remove
# libfrr from modules because modules will always link it in dynamically and
# thus 2 copies of libfrr will be loaded... hilarity ensues.
#
# Not linking libfrr into modules should generally work fine because the
# executable refers to libfrr either way and the dynamic linker should make
# libfrr available to modules.  If some OS platform has a dynamic linker that
# doesn't do that, libfrr needs to be readded to modules, but _only_ _if_
# it's not linked into daemons statically.

#
# SNMP support
#
if SNMP
lib_LTLIBRARIES += lib/libfrrsnmp.la
endif

lib_libfrrsnmp_la_CFLAGS = $(AM_CFLAGS) $(SNMP_CFLAGS) -std=gnu11
lib_libfrrsnmp_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0
lib_libfrrsnmp_la_LIBADD = $(SNMP_LIBS)
lib_libfrrsnmp_la_SOURCES = \
	lib/agentx.c \
	lib/snmp.c \
	# end


#
# c-ares support
#
if CARES
lib_LTLIBRARIES += lib/libfrrcares.la
pkginclude_HEADERS += lib/resolver.h
endif

lib_libfrrcares_la_CFLAGS = $(AM_CFLAGS) $(CARES_CFLAGS)
lib_libfrrcares_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0
lib_libfrrcares_la_LIBADD = $(CARES_LIBS)
lib_libfrrcares_la_SOURCES = \
	lib/resolver.c \
	#end

#
# ZeroMQ support
#
if ZEROMQ
lib_LTLIBRARIES += lib/libfrrzmq.la
pkginclude_HEADERS += lib/frr_zmq.h
endif

lib_libfrrzmq_la_CFLAGS = $(AM_CFLAGS) $(ZEROMQ_CFLAGS)
lib_libfrrzmq_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0
lib_libfrrzmq_la_LIBADD = $(ZEROMQ_LIBS)
lib_libfrrzmq_la_SOURCES = \
	lib/frr_zmq.c \
	#end

#
# Sysrepo support
#
if SYSREPO
module_LTLIBRARIES += lib/sysrepo.la
endif

lib_sysrepo_la_CFLAGS = $(AM_CFLAGS) $(SYSREPO_CFLAGS)
lib_sysrepo_la_LDFLAGS = $(MODULE_LDFLAGS)
lib_sysrepo_la_LIBADD = lib/libfrr.la $(SYSREPO_LIBS)
lib_sysrepo_la_SOURCES = lib/northbound_sysrepo.c

#
# gRPC northbound plugin
#
if GRPC
module_LTLIBRARIES += lib/grpc.la
endif

lib_grpc_la_CXXFLAGS = $(WERROR) $(GRPC_CFLAGS)
lib_grpc_la_LDFLAGS = $(MODULE_LDFLAGS)
lib_grpc_la_LIBADD = lib/libfrr.la grpc/libfrrgrpc_pb.la $(GRPC_LIBS)
lib_grpc_la_SOURCES = lib/northbound_grpc.cpp

#
# CLI utilities
#
noinst_PROGRAMS += \
	lib/grammar_sandbox \
	# end

if BUILD_CLIPPY
noinst_PROGRAMS += lib/clippy
else
if HOSTTOOLS_CLIPPY
$(CLIPPY):
	@$(MAKE) -C $(top_builddir)/hosttools lib/route_types.h lib/clippy
endif
endif

lib_grammar_sandbox_SOURCES = \
	lib/grammar_sandbox_main.c
lib_grammar_sandbox_LDADD = \
	lib/libfrr.la

lib_clippy_CPPFLAGS = $(CPPFLAGS_BASE) -D_GNU_SOURCE -DBUILDING_CLIPPY
lib_clippy_CFLAGS = $(AC_CFLAGS) $(PYTHON_CFLAGS)
lib_clippy_LDADD = $(PYTHON_LIBS) $(UST_LIBS) -lelf
# no $(SAN_FLAGS) here
lib_clippy_LDFLAGS = -export-dynamic $(AC_LDFLAGS) $(AC_LDFLAGS_EXEC)
lib_clippy_SOURCES = \
	lib/jhash.c \
	lib/clippy.c \
	lib/command_graph.c \
	lib/command_lex.l \
	lib/command_parse.y \
	lib/command_py.c \
	lib/defun_lex.l \
	lib/elf_py.c \
	lib/graph.c \
	lib/libfrr_trace.c \
	lib/memory.c \
	lib/typesafe.c \
	lib/vector.c \
	# end

# (global) clippy rules for all directories

AM_V_CLIPPY = $(am__v_CLIPPY_$(V))
am__v_CLIPPY_ = $(am__v_CLIPPY_$(AM_DEFAULT_VERBOSITY))
am__v_CLIPPY_0 = @echo "  CLIPPY  " $@;
am__v_CLIPPY_1 =

CLIPPY_DEPS = $(CLIPPY) $(top_srcdir)/python/clidef.py

SUFFIXES += _clippy.c
.c_clippy.c:
	$(AM_V_CLIPPY) $(CLIPPY) $(top_srcdir)/python/clidef.py -o $@ $<

# xrelfo, the ELF xref extractor

AM_V_XRELFO = $(am__v_XRELFO_$(V))
am__v_XRELFO_ = $(am__v_XRELFO_$(AM_DEFAULT_VERBOSITY))
am__v_XRELFO_0 = @echo "  XRELFO  " $@;
am__v_XRELFO_1 =

XRELFO_FLAGS = -Wlog-format -Wlog-args

SUFFIXES += .xref
%.xref: % $(CLIPPY)
	$(AM_V_XRELFO) $(CLIPPY) $(top_srcdir)/python/xrelfo.py $(WERROR) $(XRELFO_FLAGS) -o $@ $<

# these run up to a total of 350k lines at the time of writing.  That's a lot
# for the compiler to chug down - enough to warrant splitting it up so it can
# benefit from parallel build.
vtysh_cmd_split = \
	vtysh/vtysh_cmd.1.c \
	vtysh/vtysh_cmd.2.c \
	vtysh/vtysh_cmd.3.c \
	vtysh/vtysh_cmd.4.c \
	vtysh/vtysh_cmd.5.c \
	vtysh/vtysh_cmd.6.c \
	vtysh/vtysh_cmd.7.c \
	vtysh/vtysh_cmd.8.c \
	# end

# dependencies added in python/makefile.py
frr.xref:
	$(AM_V_XRELFO) $(CLIPPY) $(top_srcdir)/python/xrelfo.py -o $@ $^ \
		-c vtysh/vtysh_cmd.c $(vtysh_cmd_split)
all-am: frr.xref

clean-xref:
	-rm -rf $(xrefs) frr.xref
clean-local: clean-xref

vtysh/vtysh_cmd.c: frr.xref
	@test -f $@ || rm -f frr.xref || true
	@test -f $@ || $(MAKE) $(AM_MAKEFLAGS) frr.xref

vtysh/vtysh_cmd.%.c: vtysh/vtysh_cmd.c
	@test -f $@ || rm -f vtysh/vtysh_cmd.c || true
	@test -f $@ || $(MAKE) $(AM_MAKEFLAGS) vtysh/vtysh_cmd.c

nodist_vtysh_vtysh_SOURCES = vtysh/vtysh_cmd.c $(vtysh_cmd_split)
CLEANFILES += vtysh/vtysh_cmd.c $(vtysh_cmd_split)

## automake's "ylwrap" is a great piece of GNU software... not.
.l.c:
	$(AM_V_LEX)$(am__skiplex) $(LEXCOMPILE) $<
.y.c:
	$(AM_V_YACC)$(am__skipyacc) $(YACCCOMPILE) $<

#
# generated sources & extra foo
#
EXTRA_DIST += \
	lib/command_lex.h \
	lib/command_parse.h \
	lib/gitversion.pl \
	lib/route_types.pl \
	lib/route_types.txt \
	# end

BUILT_SOURCES += \
	lib/gitversion.h \
	lib/route_types.h \
	lib/vtysh_daemons.h \
	# end

## force route_types.h
$(lib_clippy_OBJECTS): lib/route_types.h
$(lib_libfrr_la_OBJECTS): lib/route_types.h

# force lib_daemons.h
$(lib_libfrr_la_OBJECTS): lib/vtysh_daemons.h

CLEANFILES += lib/vtysh_daemons.h
lib/vtysh_daemons.h:
	@$(MKDIR_P) lib
	$(PERL) $(top_srcdir)/vtysh/daemons.pl $(vtysh_daemons) > lib/vtysh_daemons.h


AM_YFLAGS = -d -Dapi.prefix=@BISON_OPENBRACE@cmd_yy@BISON_CLOSEBRACE@ @BISON_VERBOSE@

lib/command_lex.h: lib/command_lex.c
	@if test ! -f $@; then rm -f "lib/command_lex.c"; else :; fi
	@if test ! -f $@; then $(MAKE) $(AM_MAKEFLAGS) "lib/command_lex.c"; else :; fi
lib/command_lex.lo: lib/command_parse.h
lib/command_parse.lo: lib/command_lex.h
lib/clippy-command_lex.$(OBJEXT): lib/command_parse.h
lib/clippy-command_parse.$(OBJEXT): lib/command_lex.h
lib/lib_clippy-command_lex.$(OBJEXT): lib/command_parse.h
lib/lib_clippy-command_parse.$(OBJEXT): lib/command_lex.h

DISTCLEANFILES += lib/command_lex.h \
		  lib/command_lex.c \
		  lib/command_parse.h \
		  lib/command_parse.c

rt_enabled =

if BABELD
rt_enabled += --enabled babeld
endif
if BFDD
rt_enabled += --enabled bfdd
endif
if BGPD
rt_enabled += --enabled bgpd
if ENABLE_BGP_VNC
rt_enabled += --enabled bgpd-vnc
endif
endif
if EIGRPD
rt_enabled += --enabled eigrpd
endif
if ISISD
rt_enabled += --enabled isisd
endif
if FABRICD
rt_enabled += --enabled fabricd
endif
if LDPD
rt_enabled += --enabled ldpd
endif
if NHRPD
rt_enabled += --enabled nhrpd
endif
if OSPFD
rt_enabled += --enabled ospfd
endif
if OSPF6D
rt_enabled += --enabled ospf6d
endif
if PBRD
rt_enabled += --enabled pbrd
endif
if PIMD
rt_enabled += --enabled pimd
endif
if RIPD
rt_enabled += --enabled ripd
endif
if RIPNGD
rt_enabled += --enabled ripngd
endif
if SHARPD
rt_enabled += --enabled sharpd
endif
if ZEBRA
rt_enabled += --enabled zebra
endif

lib/route_types.h: $(top_srcdir)/lib/route_types.txt $(top_srcdir)/lib/route_types.pl
	@$(MKDIR_P) lib
	$(PERL) $(top_srcdir)/lib/route_types.pl $(rt_enabled) < $(top_srcdir)/lib/route_types.txt > $@
DISTCLEANFILES += lib/route_types.h

if GIT_VERSION
# bit of a trick here to always have up-to-date git stamps without triggering
# unnecessary rebuilds.  .PHONY causes the .tmp file to be rebuilt always,
# but if we use that on gitversion.h it'll ripple through the .c file deps.
# (even if gitversion.h's file timestamp doesn't change, make will think it
# did, because of .PHONY...)

PHONY_GITVERSION=lib/gitversion.h.tmp
.SILENT: lib/gitversion.h lib/gitversion.h.tmp
GITH=lib/gitversion.h
lib/gitversion.h.tmp: $(top_srcdir)/.git
	@$(MKDIR_P) lib
	$(PERL) $(top_srcdir)/lib/gitversion.pl $(top_srcdir) > ${GITH}.tmp
lib/gitversion.h: lib/gitversion.h.tmp
	{ test -f ${GITH} && diff -s -q ${GITH}.tmp ${GITH}; } || cp ${GITH}.tmp ${GITH}

else
PHONY_GITVERSION=lib/gitversion.h
lib/gitversion.h:
	true
endif
.PHONY: $(PHONY_GITVERSION)
