From a08a2105eaaeab83206953f44cefc1a8c70a242e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 4 Jan 2017 14:09:20 +1030 Subject: [PATCH] generate-wire.py: generalize, move to tools. We're going to want to use this for inter-daemon comms, so generalize it. Signed-off-by: Rusty Russell --- Makefile | 2 ++ daemon/p2p_announce.h | 2 +- daemon/peer.h | 2 +- {wire/tools => tools}/generate-wire.py | 35 ++++++++++++++++------- wire/Makefile | 18 ++++++------ wire/{gen_wire_csv => gen_peer_wire_csv} | 0 wire/test/{run-wire.c => run-peer-wire.c} | 2 +- 7 files changed, 39 insertions(+), 22 deletions(-) rename {wire/tools => tools}/generate-wire.py (91%) rename wire/{gen_wire_csv => gen_peer_wire_csv} (100%) rename wire/test/{run-wire.c => run-peer-wire.c} (99%) diff --git a/Makefile b/Makefile index 64a0adea1..099b97256 100644 --- a/Makefile +++ b/Makefile @@ -180,6 +180,8 @@ GEN_HEADERS := gen_version.h \ CDUMP_OBJS := ccan-cdump.o ccan-strmap.o +WIRE_GEN := tools/generate-wire.py + MANPAGES := doc/lightning-cli.1 \ doc/lightning-delinvoice.7 \ doc/lightning-getroute.7 \ diff --git a/daemon/p2p_announce.h b/daemon/p2p_announce.h index bc8848828..77a86192b 100644 --- a/daemon/p2p_announce.h +++ b/daemon/p2p_announce.h @@ -2,7 +2,7 @@ #define LIGHTNING_DAEMON_P2P_ANNOUNCE_H #include "config.h" #include "lightningd.h" -#include "wire/gen_wire.h" +#include "wire/gen_peer_wire.h" void setup_p2p_announce(struct lightningd_state *dstate); diff --git a/daemon/peer.h b/daemon/peer.h index 174cf7f79..e4e7791c4 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -14,7 +14,7 @@ #include "netaddr.h" #include "protobuf_convert.h" #include "state.h" -#include "wire/gen_wire.h" +#include "wire/gen_peer_wire.h" #include #include #include diff --git a/wire/tools/generate-wire.py b/tools/generate-wire.py similarity index 91% rename from wire/tools/generate-wire.py rename to tools/generate-wire.py index 22269e29d..c5f4e4cd3 100755 --- a/wire/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -128,6 +128,9 @@ class Message(object): self.fields.append(field) def print_structure(self): + if not self.fields: + return + print('struct msg_{} {{'.format(self.name)); for f in self.fields: @@ -142,6 +145,9 @@ class Message(object): print('};') def print_fromwire(self,is_header): + if not self.fields: + return + print('struct msg_{0} *fromwire_{0}(const tal_t *ctx, const void *p, size_t *len)'.format(self.name), end='') if is_header: @@ -185,6 +191,9 @@ class Message(object): '}\n') def print_towire(self,is_header): + if not self.fields: + return + print('u8 *towire_{0}(const tal_t *ctx, const struct msg_{0} *out)'.format(self.name), end='') if is_header: @@ -221,27 +230,31 @@ class Message(object): parser = OptionParser() parser.add_option("--header", action="store_true", dest="output_header", default=False, - help="Create gen_wire.h") + help="Create wire header") (options, args) = parser.parse_args() +if len(args) != 2: + parser.error("Expect headerfilename and enumname") + if options.output_header: - print('#ifndef LIGHTNING_WIRE_GEN_WIRE_H\n' - '#define LIGHTNING_WIRE_GEN_WIRE_H\n' + idem = re.sub(r'[^A-Z]+', '_', args[0].upper()) + print('#ifndef LIGHTNING_{0}\n' + '#define LIGHTNING_{0}\n' '#include \n' '#include \n' '\n' 'typedef u8 pad;\n' - '') + ''.format(idem)) else: - print('#include "gen_wire.h"\n' - '') + print('#include <{}>\n' + ''.format(args[0])) # Maps message names to messages messages = { } # Read csv lines. Single comma is the message values, more is offset/len. -for line in fileinput.input(args): +for line in fileinput.input(args[2:]): parts = line.rstrip().split(',') if len(parts) == 2: @@ -249,12 +262,14 @@ for line in fileinput.input(args): messages[parts[0]] = Message(parts[0],Enumtype("WIRE_" + parts[0].upper(), int(parts[1]))) else: # eg commit_sig,0,channel-id,8 + if not parts[0] in messages: + messages[parts[0]] = Message(parts[0],None) messages[parts[0]].addField(Field(parts[0], parts[2], parts[3])) if options.output_header: # Dump out enum, sorted by value order. - print('enum wire_type {') - for m in sorted(messages.values(),key=lambda x:x.enum.value): + print('enum {} {{'.format(args[1])) + for m in sorted([x for x in messages.values() if x.enum is not None],key=lambda x:x.enum.value): print('\t{} = {},'.format(m.enum.name, m.enum.value)) print('};') @@ -269,4 +284,4 @@ for m in messages.values(): m.print_towire(options.output_header) if options.output_header: - print('#endif /* LIGHTNING_WIRE_GEN_WIRE_H */\n') + print('#endif /* LIGHTNING_{} */\n'.format(idem)) diff --git a/wire/Makefile b/wire/Makefile index 8708270ce..03dc754d4 100644 --- a/wire/Makefile +++ b/wire/Makefile @@ -5,8 +5,8 @@ wire-wrongdir: $(MAKE) -C .. wire-all WIRE_HEADERS := wire/wire.h -WIRE_GEN_HEADERS := wire/gen_wire.h -WIRE_GEN_SRC := wire/gen_wire.c +WIRE_GEN_HEADERS := wire/gen_peer_wire.h +WIRE_GEN_SRC := wire/gen_peer_wire.c WIRE_SRC := wire/fromwire.c \ wire/towire.c @@ -14,16 +14,16 @@ WIRE_OBJS := $(WIRE_SRC:.c=.o) $(WIRE_GEN_SRC:.c=.o) # They may not have the bolts. BOLT_EXTRACT=$(BOLTDIR)/tools/extract-formats.py -wire/gen_wire_csv: FORCE +wire/gen_peer_wire_csv: FORCE @set -e; if [ -f $(BOLT_EXTRACT) ]; then for f in $(BOLTDIR)/*.md $(BOLT_EXTRACT); do if [ $$f -nt $@ -o ! -f $@ ]; then $(BOLT_EXTRACT) --message-fields --message-types --check-alignment $(BOLTDIR)/*.md > $@; break; fi; done; fi -wire/gen_wire.h: wire/tools/generate-wire.py wire/gen_wire_csv - wire/tools/generate-wire.py --header < wire/gen_wire_csv > $@ +wire/gen_peer_wire.h: $(WIRE_GEN) wire/gen_peer_wire_csv + $(WIRE_GEN) --header $@ wire_type < wire/gen_peer_wire_csv > $@ -wire/gen_wire.c: wire/tools/generate-wire.py wire/gen_wire_csv - wire/tools/generate-wire.py < wire/gen_wire_csv > $@ +wire/gen_peer_wire.c: $(WIRE_GEN) wire/gen_peer_wire_csv + $(WIRE_GEN) ${@:.c=.h} wire_type < wire/gen_peer_wire_csv > $@ -wire/gen_wire.o: wire/gen_wire.h +wire/gen_peer_wire.o: wire/gen_peer_wire.h check-source: $(WIRE_SRC:%=check-src-include-order/%) $(WIRE_HEADERS:%=check-hdr-include-order/%) @@ -33,7 +33,7 @@ check-whitespace: $(WIRE_SRC:%=check-whitespace/%) $(WIRE_HEADERS:%=check-whites clean: wire-clean -wire-all: wire/gen_wire.o wire/fromwire.o wire/towire.o +wire-all: wire/gen_peer_wire.o wire/fromwire.o wire/towire.o wire-clean: $(RM) $(WIRE_OBJS) $(WIRE_GEN_SRC) $(WIRE_GEN_HEADERS) towire.c fromwire.c diff --git a/wire/gen_wire_csv b/wire/gen_peer_wire_csv similarity index 100% rename from wire/gen_wire_csv rename to wire/gen_peer_wire_csv diff --git a/wire/test/run-wire.c b/wire/test/run-peer-wire.c similarity index 99% rename from wire/test/run-wire.c rename to wire/test/run-peer-wire.c index 8d2b8ef6a..b1993094a 100644 --- a/wire/test/run-wire.c +++ b/wire/test/run-peer-wire.c @@ -1,4 +1,4 @@ -#include "../gen_wire.c" +#include "../gen_peer_wire.c" void towire_pad_array_orig(u8 **pptr, const u8 *arr, size_t num); #define towire_pad_array towire_pad_array_orig