如何将autoconf集成到内核Makfile中?

时间:2019-06-10 05:48:59

标签: makefile linux-kernel autoconf

我有一个内核模块项目,并使用autoconf在内核模块内部生成Makefile文件。这种autoconf用法破坏了树型源代码构建中的内核,因为内核Makefile不支持autoconf从而无法为模块生成配置文件。有没有办法使内核Makefile和autoconf兼容?谢谢!

1 个答案:

答案 0 :(得分:1)

我过去经常使用的技巧是拥有两个Makefile-一个自动生成的文件和一个内核的文件,然后在“ make”期间切换它们。

这里是一个例子:

Makefile.am

## Process this file with automake to produce Makefile.in

## this is so that Automake includes the C compiling definitions, and
## includes the source files in the distribution.

## [@]kerneldir[@] is the kernel build directory.
## [@]kernelext[@] is the kernel module extension `ko`.
## [@]moduledir[@] is the module installation directory.
## [@]depmod[@] is the depmod program.

EXTRA_PROGRAMS = automake_dummy
automake_dummy_SOURCES = mymodule.c Makefile.kernel
generated_sources =

## there is no *just* object file support in automake.  This is close enough
module_DATA = mymodule.o
export_objs = mymodule.o

# where the kernel build is located
KERNEL_LOCATION=@kerneldir@

MYMODULE_TOP_SRCDIR = @abs_top_srcdir@
MYMODULE_TOP_BUILDDIR = @abs_top_builddir@
MYMODULE_BUILDDIR = @abs_builddir@

# some magic for using linux kernel settings
# when compiling module(s)
MYMODULE_EXTRA_CFLAGS = -DEXPORT_SYMTAB $(DEFS) -I$(MYMODULE_BUILDDIR) \
    -I$(MYMODULE_TOP_BUILDDIR) -I$(MYMODULE_TOP_SRCDIR)/include
export MYMODULE_EXTRA_CFLAGS KERNEL_LOCATION module_DATA export_objs

.PHONY: FORCE

$(automake_dummy_SOURCES): FORCE
    @test "$(srcdir)" == "." || test -e "$@" || ln -vs "$(srcdir)/$@" .

# Add FORCE in case the kernel has changed.
$(module_DATA): $(generated_sources) $(automake_dummy_SOURCES) FORCE
    mv Makefile Makefile.automake
    cp Makefile.kernel Makefile
    $(MAKE) -C $(KERNEL_LOCATION) SUBDIRS=$(MYMODULE_BUILDDIR) M=$(MYMODULE_BUILDDIR) modules
    mv Makefile.automake Makefile

install-moduleDATA: $(module_DATA)
    $(mkinstalldirs) $(DESTDIR)$(moduledir)
    @list='$(module_DATA:.o=.@kernelext@)'; for p in $$list; do \
      if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
      f="`echo $$p | sed -e 's|^.*/||'`"; \
      echo " $(INSTALL_DATA) $$d$$p $(DESTDIR)$(moduledir)/$$f"; \
      $(INSTALL_DATA) $$d$$p $(DESTDIR)$(moduledir)/$$f; \
    done

uninstall-moduleDATA:
    @list='$(module_DATA:.o=.@kernelext@)'; for p in $$list; do \
      f="`echo $$p | sed -e 's|^.*/||'`"; \
      echo " rm -f $(DESTDIR)$(moduledir)/$$f"; \
      rm -f $(DESTDIR)$(moduledir)/$$f; \
    done

if SANDBOXED
else
install-data-local: install-moduleDATA
    -@depmod@ -a
endif

MOSTLYCLEANFILES = $(module_DATA) $(module_DATA:.o=.@kernelext@) \
    Modules.symvers Module.symvers Module.markers modules.order
CLEANFILES = $(module_DATA:.o=.mod.c) $(generated_sources)

clean-local:
    -rm -f .*.cmd .*.flags
    -rm -rf .tmp_versions
    -if test "$(srcdir)" != "."; then \
        for f in $(automake_dummy_SOURCES); do \
            if test "$$f" -ef "$(srcdir)/$$f"; then \
                rm -f "$$f"; \
            fi; \
        done; \
    fi

FORCE:

它使用一些自动制作替代:

  • kerneldir是内核构建目录,例如/ lib / modules / $ {version} / build
  • kernelext是模块扩展名,应为ko
  • moduledir是模块安装目录,例如/ lib / modules / $ {version} / extra
  • depmod是depmod程序,例如由AC_PATH_PROG(depmod, depmod, /sbin/depmod, $PATH:/sbin)
  • 设置

(如果需要,可以用@kernelext@替换Makefile.am中ko的所有实例。)

可以根据需要调整automake_dummy_SOURCESmodule_DATAexport_objsMYMODULE_EXTRA_CFLAGS变量。

请注意在调用内核的Makefile之前和之后,目标$(module_DATA)的规则如何复制Makefile。原始的(自动生成的)生成文件被重命名为Makefile.automake,另一个生成文件(Makefile.kernel)被复制到其位置。当子make完成而没有错误时,Makefile.automake将移回Makefile。当子Make无法成功完成时,此操作将无效,但由另一个Makefile处理。

Makefile.kernel

EXTRA_CFLAGS += $(MYMODULE_EXTRA_CFLAGS)

obj-m := $(module_DATA)
ifeq ($(VERSION).$(PATCHLEVEL), 2.4)
export-objs := $(export_objs)
endif

all mostlyclean clean maintainer-clean distclean:
    $(warning **************************************************)
    $(warning *** Makefile trick not undone, trying to recover *)
    $(warning **************************************************)
    mv Makefile.automake Makefile
    $(MAKE) $@

# The following is needed for 2.5 kernels and also let's the makefile work
# when things get screwed.
ifneq (,$(wildcard $(KERNEL_LOCATION)/Rules.make))
include $(KERNEL_LOCATION)/Rules.make
endif

在内核子制作期间,该文件通过自动生成的Makefile规则重命名为Makefile,然后又重命名回Makefile.kernel。

all mostlyclean clean maintainer-clean distclean:规则用于在上一个make结束时出错而将Makefile.kernel替换为Makefile的情况下进行恢复。它将原始的由自动makemake生成的Makefile(现在位于Makefile.automake中)移回Makefile并自动再次运行make。