From f2cb035c8f931efff4a020b164e657f16f51934b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 17 Jan 2025 10:39:47 +0100 Subject: pkg/declextract: remove unused includes and defines This is nice on its own, but this will also help to prevent lots of problems when we export more info from the clang tool in future. The clang tool does not know what will end up in the final descriptions, so it exports info about all consts that it encounters. As the result we pull in lots of includes/defines, and lots of kernel includes/defines are broken or create problems. So the fewer we have, the better. --- pkg/declextract/declextract.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'pkg/declextract') diff --git a/pkg/declextract/declextract.go b/pkg/declextract/declextract.go index fdf06b373..9df449b63 100644 --- a/pkg/declextract/declextract.go +++ b/pkg/declextract/declextract.go @@ -16,7 +16,7 @@ import ( ) func Run(out *Output, probe *ifaceprobe.Info, syscallRename map[string][]string, trace io.Writer) ( - []byte, []*Interface, error) { + []byte, []*Interface, map[string]string, error) { ctx := &context{ Output: out, probe: probe, @@ -29,7 +29,7 @@ func Run(out *Output, probe *ifaceprobe.Info, syscallRename map[string][]string, } ctx.processFunctions() ctx.processTypingFacts() - ctx.processConsts() + includeUse := ctx.processConsts() ctx.processEnums() ctx.processStructs() ctx.processSyscalls() @@ -37,7 +37,7 @@ func Run(out *Output, probe *ifaceprobe.Info, syscallRename map[string][]string, ctx.serialize() ctx.finishInterfaces() - return ctx.descriptions.Bytes(), ctx.interfaces, errors.Join(ctx.errs...) + return ctx.descriptions.Bytes(), ctx.interfaces, includeUse, errors.Join(ctx.errs...) } type context struct { @@ -75,7 +75,7 @@ func (ctx *context) trace(msg string, args ...any) { } } -func (ctx *context) processConsts() { +func (ctx *context) processConsts() map[string]string { replaces := map[string]string{ // Arches may use some includes from asm-generic and some from arch/arm. // If the arch used for extract used asm-generic for a header, @@ -85,6 +85,7 @@ func (ctx *context) processConsts() { "include/uapi/asm-generic/sockios.h": "asm/sockios.h", } defineDedup := make(map[string]bool) + includeUse := make(map[string]string) for _, ci := range ctx.Consts { if strings.Contains(ci.Filename, "/uapi/") && !strings.Contains(ci.Filename, "arch/x86/") && strings.HasSuffix(ci.Filename, ".h") { @@ -93,6 +94,7 @@ func (ctx *context) processConsts() { filename = replace } ctx.includes = append(ctx.includes, filename) + includeUse[ci.Name] = filename continue } // Remove duplicate defines (even with different values). Unfortunately we get few of these. @@ -118,6 +120,11 @@ func (ctx *context) processConsts() { "linux/types.h", "net/netlink.h", }, ctx.includes...) + // Also pretend they are used. + includeUse["__NR_read"] = "vdso/bits.h" + includeUse["__NR_write"] = "linux/types.h" + includeUse["__NR_close"] = "net/netlink.h" + return includeUse } func (ctx *context) processEnums() { -- cgit mrf-deployment