tools: ynl: work around stale system headers
authorJakub Kicinski <kuba@kernel.org>
Wed, 14 Jun 2023 00:28:00 +0000 (17:28 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 15 Jun 2023 10:44:37 +0000 (11:44 +0100)
The inability to include the uAPI headers directly in tools/
is one of the bigger annoyances of compiling user space code.
Most projects trade the pain for smaller inconvenience of having
to copy the headers under tools/include.

In case of netlink headers I think that we can avoid both.
Netlink family headers are simple and should be self-contained.
We can try to twiddle the Makefile a little to force-include
just the family header, and use system headers for the rest.

This works fairly well. There are two warts - for some reason
if we specify -include $path/family.h as a compilation flag,
the #ifdef header guard does not seem to work. So we need
to throw the guard in on the command line as well. Seems like
GCC detects that the header is different and tries to include
both. Second problem is that make wants hash sign to be escaped
or not depending on the version. Sigh.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/net/ynl/Makefile.deps [new file with mode: 0644]
tools/net/ynl/generated/Makefile
tools/net/ynl/samples/Makefile

diff --git a/tools/net/ynl/Makefile.deps b/tools/net/ynl/Makefile.deps
new file mode 100644 (file)
index 0000000..524fc4b
--- /dev/null
@@ -0,0 +1,28 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Try to include uAPI headers from the kernel uapi/ path.
+# Most code under tools/ requires the respective kernel uAPI headers
+# to be copied to tools/include. The duplication is annoying.
+# All the family headers should be self-contained. We avoid the copying
+# by selectively including just the uAPI header of the family directly
+# from the kernel sources.
+
+UAPI_PATH:=../../../../include/uapi/
+
+# If the header does not exist at all in the system path - let the
+# compiler fall back to the kernel header via -Idirafter.
+# GCC seems to ignore header guard if the header is different, so we need
+# to specify the -D$(hdr_guard).
+# And we need to define HASH indirectly because GNU Make 4.2 wants it escaped
+# and Gnu Make 4.4 wants it without escaping.
+
+HASH := \#
+
+get_hdr_inc=$(if $(shell echo "$(HASH)include <linux/$(2)>" | \
+                        cpp >>/dev/null 2>/dev/null && echo yes),\
+               -D$(1) -include $(UAPI_PATH)/linux/$(2))
+
+CFLAGS_devlink:=$(call get_hdr_inc,_UAPI_LINUX_DEVLINK_H_,devlink.h)
+CFLAGS_ethtool:=$(call get_hdr_inc,_LINUX_ETHTOOL_NETLINK_H_,ethtool_netlink.h)
+CFLAGS_handshake:=$(call get_hdr_inc,_UAPI_LINUX_HANDSHAKE_H,handshake.h)
+CFLAGS_netdev:=$(call get_hdr_inc,_UAPI_LINUX_NETDEV_H,netdev.h)
index f15c248..f8817d2 100644 (file)
@@ -2,11 +2,13 @@
 
 CC=gcc
 CFLAGS=-std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \
-       -I../lib/
+       -I../lib/ -idirafter $(UAPI_PATH)
 ifeq ("$(DEBUG)","1")
   CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
 endif
 
+include ../Makefile.deps
+
 YNL_GEN_ARG_ethtool:=--user-header linux/ethtool_netlink.h \
        --exclude-op stats-get
 
@@ -33,7 +35,7 @@ protos.a: $(OBJS)
 
 %-user.o: %-user.c %-user.h
        @echo -e "\tCC $@"
-       @$(COMPILE.c) -c -o $@ $<
+       @$(COMPILE.c) $(CFLAGS_$*) -o $@ $<
 
 clean:
        rm -f *.o
index 714316c..f2db8bb 100644 (file)
@@ -1,8 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0
 
+include ../Makefile.deps
+
 CC=gcc
 CFLAGS=-std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \
-       -I../lib/ -I../generated/
+       -I../lib/ -I../generated/ -idirafter $(UAPI_PATH)
 ifeq ("$(DEBUG)","1")
   CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
 endif