Skip to content

Commit

Permalink
generate-wire.py: generalize, move to tools.
Browse files Browse the repository at this point in the history
We're going to want to use this for inter-daemon comms, so generalize it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 4, 2017
1 parent b7789bf commit a08a210
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion daemon/p2p_announce.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion daemon/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
35 changes: 25 additions & 10 deletions wire/tools/generate-wire.py → tools/generate-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def addField(self,field):
self.fields.append(field)

def print_structure(self):
if not self.fields:
return

print('struct msg_{} {{'.format(self.name));

for f in self.fields:
Expand All @@ -142,6 +145,9 @@ def print_structure(self):
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:
Expand Down Expand Up @@ -185,6 +191,9 @@ def print_fromwire(self,is_header):
'}\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:
Expand Down Expand Up @@ -221,40 +230,46 @@ def print_towire(self,is_header):
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:
# eg commit_sig,132
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('};')

Expand All @@ -269,4 +284,4 @@ def print_towire(self,is_header):
m.print_towire(options.output_header)

if options.output_header:
print('#endif /* LIGHTNING_WIRE_GEN_WIRE_H */\n')
print('#endif /* LIGHTNING_{} */\n'.format(idem))
18 changes: 9 additions & 9 deletions wire/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ 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

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/%)

Expand All @@ -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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion wire/test/run-wire.c → wire/test/run-peer-wire.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a08a210

Please sign in to comment.