From 4e0c32863a31d89e1ee91825f1c4412cf3ec31f6 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Wed, 11 Dec 2019 12:47:36 +1030 Subject: [PATCH] Minor improvements to building Moved the index and etag to posts-tabls.o so they can be updated at the same time and a new post doesn't trigger a complete rebuild. Changed the code to use the linker rather than the compiler to access the index tables and etag value. Cleaned up the makefile a little bit. --- .gitignore | 3 +-- Makefile | 31 ++++++++++++------------------- blog.c | 2 +- makeindex.pl | 25 +++++++++++++++++-------- posts.c | 18 +++++++++++++----- posts.h | 3 +++ 6 files changed, 47 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index c82e292..5bedaec 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,5 @@ /config.h /config.make /newpost -/posts-tables.h -/etag.h +/posts-tables.c /blog-*.c diff --git a/Makefile b/Makefile index 0846bff..f7f4a66 100644 --- a/Makefile +++ b/Makefile @@ -34,18 +34,16 @@ blogz_TEMPLATES=blog-header.c blog-footer.c blogz_PROGRAMS= $(cgis) $(tools) $(BLOGZ_FCGI)_SRCS=blog.c posts.c $(blogz_TEMPLATES) blogio-fcgi.c fcgi.c -$(BLOGZ_CGI)_SRCS=blog.c posts.c $(blogz_TEMPLATES) blogio-stdio.c +$(BLOGZ_CGI)_SRCS=blog.c posts.c $(blogz_TEMPLATES) blogio-stdio.c newpost_SRCS=newpost.c -built_SRCS=$(sort $(foreach prog,$(blogz_PROGRAMS),$($(prog)_SRCS))) - dist_PROGRAMS=$(BLOGZ_CGI) $(BLOGZ_FCGI) newpost dist_SRCS=$(sort $(foreach prog,$(dist_PROGRAMS),$($(prog)_SRCS))) dist_EXTRA=Makefile \ README COPYING \ apache-site-cgi.conf apache-site.conf \ config.h.in \ - blogio.h etag.h fast-cgi.h fcgi.h posts.h template.h \ + blogio.h fast-cgi.h fcgi.h posts.h template.h \ $(patsubst blog-%.c,template/blog-%.html.in,$(blogz_TEMPLATES)) \ makeindex.pl \ maketemplate.pl @@ -53,29 +51,23 @@ dist_EXTRA=Makefile \ all: $(blogz_PROGRAMS) dist: blogz-$(dist_VERSION).tar.gz -define make_program= -$(1): $(2) - $$(CC) -o $$@ $$(LDFLAGS) $$^ $$(LDLIBS) -endef +$(BLOGZ_FCGI): $($(BLOGZ_FCGI)_SRCS:.c=.o) posts-tables.o + $(CC) -o $@ $(LDFLAGS) $^ $(LDLIBS) + +$(BLOGZ_CGI): $($(BLOGZ_CGI)_SRCS:.c=.o) posts-tables.o + $(CC) -o $@ $(LDFLAGS) $^ $(LDLIBS) -$(foreach prog,$(blogz_PROGRAMS),$(eval $(call make_program,$(prog),$($(prog)_SRCS:.c=.o)))) -#$(foreach prog,$(blogz_PROGRAMS),$(info $(call make_program,$(prog),$($(prog)_SRCS:.c=.o)))) +newpost: $(newpost_SRCS:.c=.o) + $(CC) -o $@ $(LDFLAGS) $^ $(LDLIBS) # database indexing -posts-tables.h: $(DB_POST) makeindex.pl +posts-tables.c: $(DB_POST) makeindex.pl ./makeindex.pl $(DB_POST) > $@ || rm $@ -.deps/posts.d posts.o: posts-tables.h # template compilation blog-%.c: template/blog-%.html maketemplate.pl ./maketemplate.pl blog_send_$* $< > $@ || rm $@ -# the etag is related to cache management. it changes any time any of the blog contents could change. -etag.h: posts-tables.h $(blogz_TEMPLATES) - date --rfc-3339 ns | md5sum | awk '{ printf "static const char* etag = \"%s\";\n",$$1 }' > etag.h - -blog.o: etag.h - clean: rm -f posts-tables.h $(blogz_TEMPLATES) config.make rm -f *.o @@ -117,7 +109,7 @@ blogz-$(dist_VERSION).tar.gz: $(dist_EXTRA) $(dist_SRCS) $^ # Make management -config.make: config.h Makefile +config.make: config.h sed -e 's@//@#//@' \ -e 's/^#define \([a-zA-Z0-9_]*\) "\(.*\)"/\1=\2/' \ -e 's/^#define \([a-zA-Z0-9_]*\) \(.*\)/\1=\2/' \ @@ -130,6 +122,7 @@ config.make: config.h Makefile .INTERMEDIATE: $(blogz_TEMPLATES) ifeq (,$(filter clean install-db dist,$(MAKECMDGOALS))) +built_SRCS=$(sort $(foreach prog,$(blogz_PROGRAMS),$($(prog)_SRCS))) -include $(patsubst %.c,.deps/%.d,$(built_SRCS)) endif diff --git a/blog.c b/blog.c index 701ae75..f38d3af 100644 --- a/blog.c +++ b/blog.c @@ -31,9 +31,9 @@ #include "posts.h" #include "template.h" #include "blogio.h" -#include "etag.h" static const char *db = DB_POST; +extern const char etag[]; /** * Basic CGI variable parser, silently truncates out of range values and other broken behaviour diff --git a/makeindex.pl b/makeindex.pl index a8688cb..3afe4ef 100755 --- a/makeindex.pl +++ b/makeindex.pl @@ -61,7 +61,14 @@ while () { } close IN; -print "static const postid_t post_id[] = {\n"; +print "#include \"posts.h\"\n"; + +# the etag is related to cache management. it changes any time any of the blog contents could change. +$etag = `date --rfc-3339 ns | md5sum`; +$etag =~ s/ .*\n//; +print "const char etag[] = \"$etag\";\n"; + +print "const postid_t post_id[] = {\n"; $i = 0; foreach $k (@allids) { print " 0x$k,"; @@ -72,21 +79,22 @@ foreach $k (@allids) { } } print "\n};\n"; +print "const int post_id_SIZEOF = sizeof(post_id)/sizeof(post_id[0]);\n"; -print "static const char *post_file[] = {\n"; +print "const char *post_file[] = {\n"; foreach $k (@allfiles) { print "\"$k\",\n"; } print "\n};\n"; -print "static const char *post_title[] = {\n"; +print "const char *post_title[] = {\n"; foreach $k (@alltitles) { $k =~ s/"/\\"/g; print "\"$k\",\n"; } print "\n};\n"; -print "static const struct post_index post_index[] = {\n"; +print "const struct post_index post_index[] = {\n"; foreach $k (sort keys %bymonth) { @foo = @{$bymonth{$k}}; @@ -104,6 +112,7 @@ foreach $k (sort keys %bymonth) { } print "};\n"; +print "const int post_index_SIZEOF = sizeof(post_index)/sizeof(post_index[0]);\n"; # build keyword index %allmeta = (); @@ -129,7 +138,7 @@ foreach $k (@allids) { %bases = (); $i = 0; $base = 0; -print "static const int post_tag_table"."[] = { "; +print "const int post_tag_table"."[] = { "; for $key (@keys) { my @list = @{$bytag{$key}}; print "/* $key */ "; @@ -143,7 +152,7 @@ for $key (@keys) { } print "};\n"; -print "static const struct post_tag post_tag[] = {\n"; +print "const struct post_tag post_tag[] = {\n"; $i = 0; for $key (@keys) { my @list = @{$bytag{$key}}; @@ -152,7 +161,7 @@ for $key (@keys) { $i++; } print "};\n"; - +print "const int post_tag_SIZEOF = sizeof(post_tag)/sizeof(post_tag[0]);\n"; # Build inverse index my %allbits = (); @@ -162,7 +171,7 @@ for $key (@keys) { $i *= 2; } -print "static const uint64_t post_tagmap[] = {\n"; +print "const uint64_t post_tagmap[] = {\n"; $i = 0; foreach $k (@allids) { my $w = $allmeta{$k}; diff --git a/posts.c b/posts.c index f3a917f..55697de 100644 --- a/posts.c +++ b/posts.c @@ -25,11 +25,19 @@ #include "config.h" #include "posts.h" -#include "posts-tables.h" -#define post_id_SIZEOF (sizeof(post_id)/sizeof(post_id[0])) -#define post_index_SIZEOF (sizeof(post_index)/sizeof(post_index[0])) -#define post_tag_SIZEOF (sizeof(post_tag)/sizeof(post_tag[0])) +// from post-tables.o +extern const postid_t post_id[]; +extern const char *post_file[]; +extern const char *post_title[]; +extern const struct post_index post_index[]; +extern const int post_tag_table[]; +extern const struct post_tag post_tag[]; +extern const uint64_t post_tagmap[]; + +extern const int post_id_SIZEOF; +extern const int post_index_SIZEOF; +extern const int post_tag_SIZEOF; static int cmp_postid(const void *ap, const void *bp) { const postid_t *a = ap; @@ -59,7 +67,7 @@ int blog_postid(postid_t id) { * Find up to 'max' posts which are younger than or equal to post 'from'. */ int blog_posts(postid_t from, postid_t *ids, int max, postid_t *newer, postid_t *older) { - int size =sizeof(post_id)/sizeof(post_id[0]); + int size = post_id_SIZEOF; int i; int j = 0; diff --git a/posts.h b/posts.h index b1717c2..25df5fb 100644 --- a/posts.h +++ b/posts.h @@ -20,6 +20,9 @@ #ifndef _POSTS_H #define _POSTS_H +#include +#include + struct post_index { const char month[8]; int i0, i1; -- 2.39.5