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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-01-04 14:09:20 +10:30
parent b7789bf065
commit a08a2105ea
7 changed files with 39 additions and 22 deletions

View File

@ -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 \

View File

@ -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);

View File

@ -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 <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/shachain/shachain.h>
#include <ccan/list/list.h>

View File

@ -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 <ccan/tal/tal.h>\n'
'#include <wire/wire.h>\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))

View File

@ -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

View File

@ -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