[systemd-devel] Removing unnecessary includes

Thomas H.P. Andersen phomes at gmail.com
Sun Feb 22 05:56:03 PST 2015


On Sun, Feb 15, 2015 at 11:53 PM, Thomas H.P. Andersen <phomes at gmail.com> wrote:
> On Tue, Feb 10, 2015 at 10:05 PM, Lennart Poettering
> <lennart at poettering.net> wrote:
>> On Sat, 07.02.15 10:29, Thomas H.P. Andersen (phomes at gmail.com) wrote:
>>
>>> Hi,
>>>
>>> I am looking at ways to automatically trim the unnecessary includes.
>>> One way to do it is a script[1] which simply tests if the compile
>>> still works after removing each include one at a time. It does this in
>>> reverse order for all includes in the .c files. Using -Werror we catch
>>> any new warnings too.
>>
>> I think this is quite useful, but I'd also be really careful with
>> this. glibc versions sometimes require different headers to be
>> included to get some functionality, thus automatic removal of headers
>> that are unnecessary on one system doesn't mean this is universally
>> the case... Moreover depdending on compile-time options you might
>> different headers...
>
> I have used IWYU to only drop headers that we do not use any symbols
> from. There are no automatically added headers nor includes replaced
> by forward declarations.I have manually checked all removals from
> files that contain a #ifdef or #if defined() to catch issues from
> various compile-time option combinations. No includes of "missing.h"
> were removed and I tried to be careful with endianness.
>
> Are there specific headers I should be extra careful with or ignore completely?
>
> For info I am attaching a diff with the changes so far: 1309 includes
> removed out of the current 7707. It is only compile and "make
> check"-tested. I am only looking for comments (and perhaps compile
> testing on an AppArmor system as I do not have a system with that).

The patch bitrots quickly. I have updated it and also dropped a few
removals of "architecture.h" and <endian.h>. It passes make check and
I have not seen any issues with using it on my own system. Still not
tested with AppArmor though.

I moved the patch to github:
https://github.com/phomes/systemd-1/commit/cf3c313747ebae18b63effb251f801ac4c370f05

> Here is a list the headers removed and number of times removed:
> 74 <sys/types.h>
> 69 <unistd.h>
> 65 <string.h>
> 63 <assert.h>
> 53 "util.h"
> 45 <fcntl.h>
> 42 <stdlib.h>
> 38 <inttypes.h>
> 29 <errno.h>
> 28 <sys/stat.h>
> 21 "strv.h"
> 19 "label.h"
> 17 <sys/socket.h>
> 16 "path-util.h"
> 16 "fileio.h"
> 16 "def.h"
> 15 <stdio.h>
> 15 <dirent.h>
> 15 <arpa/inet.h>
> 14 "unit.h"
> 14 "mkdir.h"
> 14 "log.h"
> 14 <ctype.h>
> 13 <sys/un.h>
> 13 "dbus-unit.h"
> 12 <stdarg.h>
> 12 "set.h"
> 12 <poll.h>
> 12 <netinet/ether.h>
> 11 <limits.h>
> 11 "bus-message.h"
> 10 <stdbool.h>
> 10 <signal.h>
>  9 <time.h>
>  9 <sys/wait.h>
>  9 "socket-util.h"
>  9 "sd-bus.h"
>  9 <pwd.h>
>  9 "network-internal.h"
>  9 "list.h"
>  8 <sys/ioctl.h>
>  8 "sd-id128.h"
>  8 <net/if.h>
>  8 "load-fragment.h"
>  8 <getopt.h>
>  7 <sys/param.h>
>  7 "macro.h"
>  7 "hashmap.h"
>  7 "event-util.h"
>  7 "bus-error.h"
>  6 "utf8.h"
>  6 <sys/time.h>
>  6 <sys/prctl.h>
>  6 <grp.h>
>  6 "conf-parser.h"
>  6 "capability.h"
>  6 "bus-util.h"
>  6 "build.h"
>  5 "udev-util.h"
>  5 <sys/timex.h>
>  5 <sys/timerfd.h>
>  5 <sys/syscall.h>
>  5 <sys/epoll.h>
>  5 "load-dropin.h"
>  4 "virt.h"
>  4 <termios.h>
>  4 <sys/signalfd.h>
>  4 <sys/mount.h>
>  4 <stddef.h>
>  4 "sd-messages.h"
>  4 "resolved-manager.h"
>  4 <netinet/in.h>
>  4 "manager.h"
>  4 "logind-seat.h"
>  4 <linux/vt.h>
>  4 <libudev.h>
>  4 "exit-status.h"
>  4 "execute.h"
>  4 "conf-files.h"
>  4 "cgroup-util.h"
>  4 <byteswap.h>
>  4 "bus-control.h"
>  4 "bus-common-errors.h"
>  4 "audit.h"
>  3 "time-util.h"
>  3 <sys/utsname.h>
>  3 <sys/resource.h>
>  3 <stdint.h>
>  3 "special.h"
>  3 "smack-util.h"
>  3 "resolved-dns-scope.h"
>  3 <net/ethernet.h>
>  3 "logind-session.h"
>  3 "logind.h"
>  3 "libudev.h"
>  3 "in-addr-util.h"
>  3 <endian.h>
>  2 <wchar.h>
>  2 "unit-name.h"
>  2 <sys/xattr.h>
>  2 <systemd/sd-login.h>
>  2 "systemd/sd-journal.h"
>  2 <sys/statvfs.h>
>  2 <sys/mman.h>
>  2 "synthesize.h"
>  2 "siphash24.h"
>  2 "sd-rtnl.h"
>  2 "sd-event.h"
>  2 "sd-dhcp-client.h"
>  2 "sd-bus-protocol.h"
>  2 "rtnl-util.h"
>  2 "rtnl-internal.h"
>  2 "resolved-dns-stream.h"
>  2 "resolved-dns-server.h"
>  2 "resolved-dns-rr.h"
>  2 "network-util.h"
>  2 <netinet/if_ether.h>
>  2 "mount-setup.h"
>  2 "mount.h"
>  2 "logind-device.h"
>  2 <linux/types.h>
>  2 <linux/limits.h>
>  2 <linux/ioctl.h>
>  2 <linux/fs.h>
>  2 "libudev-private.h"
>  2 "journal-authenticate.h"
>  2 "install.h"
>  2 "fsprg.h"
>  2 <fnmatch.h>
>  2 "failure-action.h"
>  2 "driver.h"
>  2 "dhcp-lease-internal.h"
>  2 "dbus-kill.h"
>  2 "clock-util.h"
>  2 "cgroup.h"
>  2 "bus-label.h"
>  2 <asm/types.h>
>  2 <arpa/nameser.h>
>  1 "xml.h"
>  1 <xkbcommon/xkbcommon.h>
>  1 "unaligned.h"
>  1 "target.h"
>  1 <sys/vfs.h>
>  1 <sys/uio.h>
>  1 <sys/swap.h>
>  1 <sys/select.h>
>  1 <sys/inotify.h>
>  1 <sys/file.h>
>  1 <sys/eventfd.h>
>  1 "strxcpyx.h"
>  1 "specifier.h"
>  1 "service.h"
>  1 "sd-lldp.h"
>  1 "sd-dhcp-lease.h"
>  1 "sd-daemon.h"
>  1 "resolved-dns-transaction.h"
>  1 "resolved-dns-query.h"
>  1 "resolved-dns-domain.h"
>  1 <pthread.h>
>  1 "path-lookup.h"
>  1 "networkd-netdev.h"
>  1 "networkd-link.h"
>  1 <mntent.h>
>  1 "machine.h"
>  1 "logind-user.h"
>  1 "logind-session-device.h"
>  1 "logind-inhibit.h"
>  1 <locale.h>
>  1 <linux/veth.h>
>  1 <linux/sched.h>
>  1 <linux/ppp_defs.h>
>  1 <linux/oom.h>
>  1 <linux/netlink.h>
>  1 <linux/input.h>
>  1 <linux/in6.h>
>  1 <linux/if_link.h>
>  1 <linux/if.h>
>  1 <linux/if_ether.h>
>  1 <linux/capability.h>
>  1 "keyboard-keys-to-name.h"
>  1 "kdbus.h"
>  1 "journal-internal.h"
>  1 "journald-console.h"
>  1 "grdev-internal.h"
>  1 "fileio-label.h"
>  1 "ether-addr-util.h"
>  1 "errno-list.h"
>  1 "env-util.h"
>  1 <drm_fourcc.h>
>  1 <dlfcn.h>
>  1 "dhcp-internal.h"
>  1 "dbus-manager.h"
>  1 "copy.h"
>  1 "bus-xml-policy.h"
>  1 "bus-signature.h"
>  1 "bus-kernel.h"
>  1 "bus-introspect.h"
>  1 "bus-internal.h"
>  1 "boot.h"
>  1 "architecture.h"
>  1 "af-list.h"
>  1 "acpi-fpdt.h"


More information about the systemd-devel mailing list