From 660b77270e599fed9a8a6bc94a0e2d44b8f282dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Sun, 7 Nov 2010 01:12:05 +0200 Subject: build: use build system from apk-tools/opennhrp --- Make.rules | 307 +++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 34 ++++++ man/Makefile | 5 + src/Makefile | 43 +++---- src/authdb.c | 2 + src/blob.c | 1 + src/squark-auth-snmp.c | 8 +- src/squark-filter.c | 2 + 8 files changed, 374 insertions(+), 28 deletions(-) create mode 100644 Make.rules create mode 100644 Makefile create mode 100644 man/Makefile diff --git a/Make.rules b/Make.rules new file mode 100644 index 0000000..9fd4862 --- /dev/null +++ b/Make.rules @@ -0,0 +1,307 @@ +## +# A set of makefile rules loosely based on kbuild. + +all: compile + +ifndef build + +toplevelrun:=yes + +## +# Disable default rules and make output pretty. + +MAKEFLAGS += -rR --no-print-directory + +Makefile: ; + +ifdef V + ifeq ("$(origin V)", "command line") + VERBOSE = $(V) + endif +endif +ifndef VERBOSE + VERBOSE = 0 +endif + +ifeq ($(VERBOSE),1) + quiet = + Q = +else + quiet=quiet_ + Q = @ +endif + +ifneq ($(findstring s,$(MAKEFLAGS)),) + quiet=silent_ +endif + +export quiet Q VERBOSE + +## +# Recursion helpers. +srctree := $(CURDIR) +objtree := $(CURDIR) + +export srctree objtree + +## +# Consult SCM for better version string. + +TAGPREFIX ?= v + +GIT_REV := $(shell test -d .git && git describe || echo exported) +ifneq ($(GIT_REV), exported) +FULL_VERSION := $(patsubst $(TAGPREFIX)%,%,$(GIT_REV)) +else +FULL_VERSION := $(VERSION) +endif + +RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o -name CVS -o -name .pc -o -name .hg -o -name .git \) -prune -o + +export FULL_VERSION RCS_FIND_IGNORE + +## +# Utilities and default flags for them. + +CROSS_COMPILE ?= +CC := $(CROSS_COMPILE)gcc +LD := $(CROSS_COMPILE)ld +INSTALL := install +INSTALLDIR := $(INSTALL) -d + +CFLAGS ?= -g -O2 +CFLAGS_ALL := -Werror -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 -fPIC +CFLAGS_ALL += $(CFLAGS) + +LDFLAGS ?= -g +LDFLAGS_ALL += $(LDFLAGS) + +export CC LD INSTALL INSTALLDIR CFLAGS_ALL LDFLAGS_ALL + +build := + +endif + +## +# Reset all variables. +ifneq ($(origin targets),file) +targets := +endif + +src := +obj := + +src += $(build) +obj := $(build) + +## +# Include directory specific stuff + +ifneq ($(build),) +$(build)/Makefile: ; +include $(build)/Makefile +endif + +## +# Rules and helpers + +PHONY += all compile install clean FORCE + +# Convinient variables +comma := , +squote := ' +empty := +space := $(empty) $(empty) + +# The temporary file to save gcc -MD generated dependencies must not +# contain a comma +depfile = $(subst $(comma),_,$(@D)/.$(@F).d) + +build-dir = $(patsubst %/,%,$(dir $@)) +target-dir = $(dir $@) + +## +# Build rules + +ifneq ($(NOCMDDEP),1) +# Check if both arguments has same arguments. Result in empty string if equal +# User may override this check using make NOCMDDEP=1 +# Check if both arguments has same arguments. Result is empty string if equal. +# User may override this check using make KBUILD_NOCMDDEP=1 +arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ + $(filter-out $(cmd_$@), $(cmd_$(1))) ) +endif + +# echo command. +# Short version is used, if $(quiet) equals `quiet_', otherwise full one. +echo-cmd = $(if $($(quiet)cmd_$(1)),\ + echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) + +make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) + +# printing commands +cmd = @$(echo-cmd) $(cmd_$(1)) + +# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o +dot-target = $(dir $@).$(notdir $@) + +# The temporary file to save gcc -MD generated dependencies must not +# contain a comma +depfile = $(subst $(comma),_,$(dot-target).d) + +# Escape single quote for use in echo statements +escsq = $(subst $(squote),'\$(squote)',$1) + +# Find any prerequisites that is newer than target or that does not exist. +# PHONY targets skipped in both cases. +local-target-prereqs = % +any-prereq = $(filter $(local-target-prereqs), $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^), $^)) + +# Execute command if command has changed or prerequisite(s) are updated. +# +if_changed = $(if $(strip $(any-prereq) $(arg-check)), \ + @set -e; \ + $(echo-cmd) $(cmd_$(1)); \ + echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) + +# Usage: $(call if_changed_rule,foo) +# Will check if $(cmd_foo) or any of the prerequisites changed, +# and if so will execute $(rule_foo). +if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ + @set -e; \ + $(rule_$(1))) + +##### +# Handle options to gcc. + +c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS_ALL) $(CFLAGS_EXTRA) \ + $(CFLAGS_$(notdir $@)) +ld_flags = $(LDFLAGS_ALL) $(LDFLAGS_EXTRA) $(LDFLAGS_$(notdir $@)) + +##### +# Compile c-files. +quiet_cmd_cc_o_c = CC $@ + +cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< + +define rule_cc_o_c + $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \ + (echo 'cmd_$@ := $(call make-cmd,cc_o_c)'; echo; cat $(depfile)) \ + > $(dot-target).cmd ; \ + rm $(depfile) +endef + +$(obj)/%.o: override local-target-prereqs=% + +$(obj)/%.o: $(src)/%.c FORCE + $(call if_changed_rule,cc_o_c) + +##### +# Link shared libraries +# +__shlibs := $(addprefix $(obj)/,$(sort $(shlibs-y))) +shobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(shlibs-y),$($(m)-objs)))) + +# link shared library +quiet_cmd_shlib = LD -shared $@ + cmd_shlib = $(CC) $(ld_flags) -shared -o $@ \ + $(addprefix $(obj)/,$($(@F)-objs)) \ + $(LIBS) $(LIBS_$(@F)) + +$(__shlibs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs)) + +$(__shlibs): $(obj)/%: $(shobjs) FORCE + $(call if_changed,shlib) + +targets += $(__shlibs) $(shobjs) +##### +# Link programs + +# Link an executable based on list of .o files, all plain c +# host-cmulti -> executable +__progs := $(addprefix $(obj)/,$(sort $(progs-y))) +cobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(progs-y),$($(m)-objs)))) + +quiet_cmd_ld = LD $@ + cmd_ld = $(CC) $(ld_flags) -o $@ \ + $(addprefix $(obj)/,$($(@F)-objs)) \ + $(LIBS) $(LIBS_$(@F)) + +$(__progs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs)) + +$(__progs): $(obj)/%: $(cobjs) FORCE + $(call if_changed,ld) + +targets += $(__progs) $(cobjs) + +### +# why - tell why a a target got build +ifeq ($(VERBOSE),2) +why = \ + $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ + $(if $(wildcard $@), \ + $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ + $(if $(arg-check), \ + $(if $(cmd_$@),- due to command line change: $(arg-check), \ + $(if $(filter $@, $(targets)), \ + - due to missing .cmd file, \ + - due to $(notdir $@) not in $$(targets) \ + ) \ + ) \ + ) \ + ), \ + - due to target missing \ + ) \ + ) + +echo-why = $(call escsq, $(strip $(why))) +endif + +## +# Top level rules. + +%/: FORCE + $(Q)$(MAKE) -f Make.rules build=$(build-dir) $(MAKECMDGOALS) + +compile: $(targets) + @: + +install: $(targets) FORCE + +clean: $(filter %/,$(targets)) +ifeq ($(toplevelrun),yes) + $(Q)find . $(RCS_FIND_IGNORE) \ + \( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \ + -type f -print | xargs rm -f +endif + $(Q)rm -rf $(addprefix $(obj)/,$(sort $(progs-y) $(progs-n) $(progs-) $(shlibs-y) $(shlibs-n) $(shlibs-))) + +ifeq ($(origin VERSION),command line) +DIST_VERSION=$(VERSION) +else +DIST_VERSION=$(FULL_VERSION) +endif + +dist: + git archive --format tar --prefix=$(PACKAGE)-$(DIST_VERSION)/ \ + $(TAGPREFIX)$(DIST_VERSION) \ + | bzip2 -9 > $(PACKAGE)-$(DIST_VERSION).tar.bz2 + +FORCE: + +# Read all saved command lines and dependencies for the $(targets) we +# may be building above, using $(if_changed{,_dep}). As an +# optimization, we don't need to read them if the target does not +# exist, we will rebuild anyway in that case. + +targets := $(wildcard $(sort $(targets))) +cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) + +ifneq ($(cmd_files),) + include $(cmd_files) +endif + +# Declare the contents of the .PHONY variable as phony. We keep that +# information in a variable se we can use it in if_changed and friends. + +.PHONY: $(PHONY) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d4a739 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +## +# Building opennhrp + +PACKAGE := squark +VERSION := 0.1 + +## +# Default directories + +DESTDIR:= +BINDIR:=/usr/bin +SBINDIR:=/usr/sbin +CONFDIR:=/etc/$(PACKAGE) +MANDIR:=/usr/share/man +DOCDIR:=/usr/share/doc/$(PACKAGE) +STATEDIR:=/var/run +LUALIBDIR:=/usr/lib/lua/5.1 + +export DESTDIR BINDIR SBINDIR CONFDIR MANDIR DOCDIR STATEDIR LUALIBDIR + +## +# Top-level rules and targets + +targets := src/ man/ + +## +# Include all rules and stuff + +include Make.rules + +## +# Top-level targets + +install: diff --git a/man/Makefile b/man/Makefile new file mode 100644 index 0000000..d1fd29a --- /dev/null +++ b/man/Makefile @@ -0,0 +1,5 @@ +MAN8PAGES = squark-auth-snmp.8 + +install: + $(INSTALLDIR) $(DESTDIR)$(MANDIR)/man8 + $(INSTALL) $(addprefix $(src)/,$(MAN8PAGES)) $(DESTDIR)$(MANDIR)/man8 diff --git a/src/Makefile b/src/Makefile index db683a2..5863414 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,30 +1,25 @@ -TARGETS=squark-auth-snmp squark-auth-ip squark-filter squarkdb.so +progs-y += squark-filter squark-auth-snmp squark-auth-ip +shlibs-y += squarkdb.so +scripts-y += sqdb-build.lua -NETSNMP_CFLAGS:=$(shell net-snmp-config --cflags) -NETSNMP_LIBS:=$(shell net-snmp-config --libs) -LUA_CFLAGS:=$(shell pkg-config --cflags lua5.1) -LUA_LIBS:=$(shell pkg-config --libs lua5.1) -CMPH_CFLAGS:=$(shell pkg-config --cflags cmph) -CMPH_LIBS:=$(shell pkg-config --libs cmph) +common-objs += filterdb.o authdb.o blob.o addr.o -CC=gcc -CFLAGS=-g -I. $(NETSNMP_CFLAGS) $(LUA_CFLAGS) $(CMPH_CFLAGS) -std=gnu99 -D_GNU_SOURCE -Wall -LIBS+=-lrt +squark-filter-objs += squark-filter.o $(common-objs) +squark-auth-snmp-objs += squark-auth-snmp.o $(common-objs) +squark-auth-ip-objs += squark-auth-ip.o $(common-objs) +squarkdb.so-objs += lua-squarkdb.o filterdb.o blob.o -all: $(TARGETS) +CFLAGS += -DSQUARK_VERSION=\"$(FULL_VERSION)\" +LIBS += -lrt -squark-auth-snmp: squark-auth-snmp.o filterdb.o authdb.o blob.o addr.o - $(CC) -o $@ $^ $(NETSNMP_LIBS) $(LIBS) +CFLAGS_lua-squarkdb.o += $(shell pkg-config --cflags lua5.1) +LIBS_squark-filter += $(shell pkg-config --libs cmph) +LIBS_squark-auth-snmp += $(shell net-snmp-config --libs) +LIBS_squarkdb.so += $(shell pkg-config --libs lua5.1) -squark-auth-ip: squark-auth-ip.o filterdb.o authdb.o blob.o addr.o - $(CC) -o $@ $^ $(LIBS) - -squark-filter: squark-filter.o filterdb.o authdb.o blob.o addr.o - $(CC) -o $@ $^ $(CMPH_LIBS) $(LIBS) - -squarkdb.so: lua-squarkdb.o filterdb.o blob.o - $(CC) -shared -o $@ $^ $(LUA_LIBS) $(CMPH_LIBS) $(LIBS) - -clean: - rm $(OBJS1) $(TARGETS) +install: + $(INSTALLDIR) $(DESTDIR)$(BINDIR) + $(INSTALL) $(addprefix $(obj)/,$(progs-y) $(scripts-y)) $(DESTDIR)$(BINDIR) + $(INSTALLDIR) $(DESTDIR)$(LUALIBDIR) + $(INSTALL) $(addprefix $(obj)/,$(shlibs-y)) $(DESTDIR)$(LUALIBDIR) \ No newline at end of file diff --git a/src/authdb.c b/src/authdb.c index e6e71c4..85fb44e 100644 --- a/src/authdb.c +++ b/src/authdb.c @@ -361,4 +361,6 @@ int adbc_refresh(struct authdb_config *cfg, time_t now) cfg->block_categories |= cfg->hard_block_categories; fclose(in); + + return 1; } diff --git a/src/blob.c b/src/blob.c index 1604308..3e62fcc 100644 --- a/src/blob.c +++ b/src/blob.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "blob.h" diff --git a/src/squark-auth-snmp.c b/src/squark-auth-snmp.c index 81b846d..aa45c73 100644 --- a/src/squark-auth-snmp.c +++ b/src/squark-auth-snmp.c @@ -389,10 +389,10 @@ void link_switch(const char *a, int ap, const char *b, int bp) struct switch_port_info *spia, *spib; sockaddr_any addr; - sia = get_switch(addr_parse(BLOB_STRLEN(a), &addr)); + sia = get_switch(addr_parse(BLOB_STRLEN((char *) a), &addr)); spia = get_switch_port(sia, ap); - sib = get_switch(addr_parse(BLOB_STRLEN(b), &addr)); + sib = get_switch(addr_parse(BLOB_STRLEN((char *) b), &addr)); spib = get_switch_port(sib, bp); addr_copy(&spia->link_partner, &sib->addr); @@ -1110,9 +1110,9 @@ int main(int argc, char **argv) if (l2_root == NULL) l2_root = l3_root; - l3_root_dev = get_switch(addr_parse(BLOB_STRLEN(l3_root), &addr)); + l3_root_dev = get_switch(addr_parse(BLOB_STRLEN((char *) l3_root), &addr)); l3_if_ndx = resolve_ifName2ifIndex(l3_root_dev, BLOB_STRLEN((char *) l3_ifname)); - l2_root_dev = get_switch(addr_parse(BLOB_STRLEN(l2_root), &addr)); + l2_root_dev = get_switch(addr_parse(BLOB_STRLEN((char *) l2_root), &addr)); l2_vlan_ndx = atoi(l2_vlan); username_format_flags = parse_format(username_format); diff --git a/src/squark-filter.c b/src/squark-filter.c index 995da40..14515cf 100644 --- a/src/squark-filter.c +++ b/src/squark-filter.c @@ -183,6 +183,7 @@ error: return 0; } +#if 0 static void url_print(struct url_info *nfo) { #define print_field(nfo, x) if (!blob_is_null(nfo->x)) printf(" %s{%.*s}", #x, nfo->x.len, nfo->x.ptr) @@ -198,6 +199,7 @@ static void url_print(struct url_info *nfo) printf("\n"); fflush(stdout); } +#endif static int url_classify(struct url_info *url, struct sqdb *db) { -- cgit v1.2.3