linuxmodulekernelbuildroot

Include *.h from another package in buildroot


I am developing my packages in buildroot with kernel modules: A and B. The source code from B requires include <A/header.h>. How can I access <A/header.h> from package B?

A/Config.in

config BR2_PACKAGE_A
        bool "A"
        depends on BR2_LINUX_KERNEL
        help
                Linux Kernel Module A.

A/a.mk

################################################################################
#
# B
#
################################################################################

A_VERSION = 1.0
A_SITE = ./package/av/a/src
A_SITE_METHOD = local

A_MODULE_SUBDIRS = linux_kernel

$(eval $(kernel-module))
$(eval $(generic-package))

B/Config.in

config BR2_PACKAGE_B
        bool "B"
        depends on BR2_LINUX_KERNEL
        depends on BR2_PACKAGE_A
        help
                Linux Kernel Module B.

B/b.mk

################################################################################
#
# B
#
################################################################################

B_VERSION = 1.0
B_SITE = ./package/av/b
B_SITE_METHOD = local
B_DEPENDENCIES += a

$(eval $(kernel-module))
$(eval $(generic-package))

B/Makefile

EXTRA_CFLAGS := -I$(BR2_PACKAGE_A)/include/ -I$(src)
KBUILD_EXTRA_SYMBOLS:= $(BR2_PACKAGE_A)/Module.symvers 

obj-m += b.o

all:
    make -C  $(LINUX_DIR) M=$(PWD) modules

Of course, BR2_PACKAGE_A does not work. I have not found a valid option


Solution

  • In general, the best option is to make sure that package A installs its header file(s) in $(STAGING_DIR).

    In a/a.mk:

    A_INSTALL_STAGING = YES
    
    define A_INSTALL_STAGING_CMDS
            install -m 0644 -D $(A_BUILDDIR)/a.h $(STAGING_DIR)/usr/include/a.h
    endef
    

    Then you don't need to add any extra -I in B's Makefile, it is in the standard include path.

    Unfortunately, this approach won't work here because B is a kernel module, and these don't use the standard include path. You could still use the same approach by installing in e.g. $(STAGING_DIR)/usr/src/linux/include - kernel headers should not be installed in /usr/include.

    Alternatively, you can use -I$(A_SRCDIR)/include or -I$(A_BUILDDIR)/include in b.mk (SRCDIR if it is a source file, BUILDDIR if it is a generated file). There are two examples of this approach in Buildroot: sysdig which looks into falcosecurity-libs source, and external kernel modules which look into the kernel's build directory. Since B is in fact a kernel module, it's probably best to use the same approach here.

    A third, even more complex alternative is to not make A a separate package, but instead include it in the build of B using the EXTRA_DOWNLOADS and pre/post extract and build hooks. But that makes it impossible to build A without B, so probably not what you need either.