Skip to content

Commit 6251e85

Browse files
committed
Generate Makefile dependencies
* Makefile (generate): New phony goal for generating elisp sources from XML protocol descriptions. (compile): New phony goal to byte-compile all elisp. (all): Depend on both. * Makefile (ELGDS): Infer Makefile dependencies for generating. (ELLDS): Infer Makefile dependencies for byte-compiling. * xelb-gen (xelb-parse): Set `load-prefer-newer' to avoid picking outdated byte-compiled definitions while generating.
1 parent 5c3b314 commit 6251e85

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.elc
2+
# Makefile prerequisites.
3+
*.el.d
24
*-pkg.el
35
*-autoloads.el

Makefile

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
PROTO_PATH := /usr/share/xcb
22

3+
ifeq ($(shell ! test -d $(PROTO_PATH) && echo ok),ok)
4+
$(warning Please set `PROTO_PATH` to the path to the XCB protocol definitions):
5+
$(warning make $(MFLAGS) $(MAKEOVERRIDES) $(MAKECMDGOALS) `PROTO_PATH=/opt/X11/...`)
6+
$(error Could not find directory `$(PROTO_PATH)`)
7+
endif
8+
39
EXTENSIONS := bigreq composite damage dbe dpms dri2 dri3 ge glx present randr \
410
record render res screensaver shape shm sync xc_misc xevie xf86dri \
511
xf86vidmode xfixes xinerama xinput xkb xprint xselinux xtest xvmc xv
@@ -13,59 +19,64 @@ ELS = $(ELLS) $(ELGS)
1319
ELCS = $(ELS:.el=.elc) xelb-gen.elc
1420

1521
.PHONY: all
16-
all: $(ELCS)
22+
all: compile generate
1723

1824
xcb-%.el: $(PROTO_PATH)/%.xml
19-
@echo -n "\n"Generating $@...
25+
@printf '%s\n' 'Generating $@...'
2026
@./xelb-gen $< > $@
2127

22-
# Dependencies needed for generating.
23-
$(ELXS): xcb-xproto.el
24-
xcb-composite.el: xcb-xfixes.el
25-
xcb-cursor.el: xcb-render.el
26-
xcb-damage.el: xcb-xfixes.el
27-
xcb-present.el: xcb-randr.el xcb-xfixes.el xcb-sync.el
28-
xcb-randr.el: xcb-render.el
29-
xcb-xfixes.el: xcb-render.el xcb-shape.el
30-
xcb-xinput.el: xcb-xfixes.el
31-
xcb-xv.el: xcb-shm.el
32-
xcb-xvmc.el: xcb-xv.el
33-
34-
# Generate an intermediate .el file from the xelb-gen script so that
35-
# byte-compiling rules below apply. This file will be deleted after the .elc is
36-
# created.
28+
# Generate an intermediate `.el` file from the xelb-gen script so that
29+
# byte-compiling rules below apply. This file will be deleted after the `.elc`
30+
# is created.
3731
.INTERMEDIATE: xelb-gen.el
3832
xelb-gen.el: xelb-gen
3933
cp $< $@
4034

35+
# Dependencies needed for generating.
36+
# We generate makefile fragments by grepping the `<import>`s in the `.xml`
37+
# protocol definitions.
38+
# See https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html .
39+
ELGDS=$(ELGS:.el=.el.d)
40+
$(ELGDS): xcb-%.el.d: $(PROTO_PATH)/%.xml
41+
@printf "Inferring dependencies for $<\n"
42+
@{ IMPORTS=$$(grep '<import>' $< | sed -E -e 's,^[[:space:]]*<import>([^<]+)</import>,xcb-\1,' | tr '\n' ' '); \
43+
test -n "$$IMPORTS" && printf '%s' 'xcb-$*.el:' && printf ' %s.el' $$IMPORTS && printf '\n'; \
44+
test -n "$$IMPORTS" && printf '%s' 'xcb-$*.elc:' && printf ' %s.elc' $$IMPORTS && printf '\n'; \
45+
true; \
46+
} >$@
47+
# All generated `.el` files require `xcb-types.el`.
48+
$(ELGS): xcb-types.el
49+
$(ELGS:.el=.elc): xcb-types.elc
50+
4151
# Dependencies needed for byte-compiling.
42-
xcb-cursor.elc: xcb-render.elc xcb.elc
43-
xcb-ewmh.elc: xcb.elc xcb-icccm.elc xcb.elc
44-
xcb-icccm.elc: xcb.elc
45-
xcb-keysyms.elc: xcb.elc xcb-xkb.elc
46-
xcb-renderutil.elc: xcb.elc xcb-render.elc
47-
xcb-systemtray.elc: xcb-ewmh.elc xcb.elc
48-
xcb-types.elc: xcb-debug.elc
49-
xcb-xembed.elc: xcb-icccm.elc
50-
xcb-xim.elc: xcb-types.elc xcb-xlib.elc
51-
xcb-xsettings.elc: xcb-icccm.elc xcb-types.elc
52-
xcb.elc: xcb-xproto.elc xcb-xkb.elc
53-
xelb.elc: xcb.elc
54-
$(ELXS:.el=.elc): xcb-xproto.elc xcb-types.elc
55-
xcb-composite.elc: xcb-xfixes.elc
56-
xcb-damage.elc: xcb-xfixes.elc
57-
xcb-present.elc: xcb-randr.elc xcb-xfixes.elc xcb-sync.elc
58-
xcb-randr.elc: xcb-render.elc
59-
xcb-xfixes.elc: xcb-render.elc xcb-shape.elc
60-
xcb-xinput.elc: xcb-xfixes.elc
61-
xcb-xv.elc: xcb-shm.elc
62-
xcb-xvmc.elc: xcb-xv.elc
63-
xelb-gen.elc: xcb-types.elc
52+
# We grep the `require`s in non-generated `.el` files.
53+
ELLDS=$(ELLS:.el=.el.d)
54+
$(ELLDS): %.el.d: %.el
55+
@printf "Inferring dependencies for $<\n"
56+
@{ printf '%s' '$*.elc: '; \
57+
grep "require 'xcb" $< | \
58+
sed -E -e "s,.*\(require '([^)]+)\).*,\1.elc," | \
59+
tr '\n' ' '; \
60+
printf '\n'; \
61+
} >$@
62+
63+
# This is a small crutch: we want to avoid generating the `.el.d`s (which means
64+
# parsing the XML and generating the corresponding `.el`s) in order to clean.
65+
ifneq ($(MAKECMDGOALS),clean)
66+
include $(ELLDS)
67+
include $(ELGDS)
68+
endif
6469

6570
%.elc: %.el
6671
@printf "Compiling $<\n"
67-
emacs --batch -Q -L . -f batch-byte-compile $<
72+
@emacs --batch -Q -L . -f batch-byte-compile $<
73+
74+
.PHONY: compile
75+
compile: $(ELCS)
76+
77+
.PHONY: generate
78+
generate: $(ELGS)
6879

6980
.PHONY: clean
7081
clean:
71-
@rm -vf $(ELGS) $(ELCS)
82+
@rm -vf $(ELGS) $(ELLDS) $(ELCS) $(ELGDS)

xelb-gen

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ an `xelb-auto-padding' attribute."
180180
"Parse an XCB protocol description file FILE (XML)."
181181
(let ((pp-escape-newlines nil) ;do not escape newlines
182182
(pp-default-function 'pp-28) ;avoid unecessary churn
183+
(load-prefer-newer 't) ;avoid loading outdated bytecode
183184
result header)
184185
(with-temp-buffer
185186
(insert-file-contents file)

0 commit comments

Comments
 (0)