how to run perl in kbuild makefile

I'm trying to add new feature to the current build. to compile, I've add obj-y += mynewfeature.c into the makefile in the same directory. But i still need to generate a header file using a perl script from the same directory. the problem i can't deal with is how to call this perl script from kbuild file. it seems makefile cannot locate the right path to perl script. So what should I add to top of kbuild file to call this perl script file


 |- mynewfeature.c
 |- some_template //perl script will read this file and generate a header file
 |- Makefile

code_to_call_perl_script // how to write this line
obj-y += mynewfeature.o

1 answer

  • answered 2019-11-14 17:42 Ian Abbott

    The Makefile rules should be something like this:

    obj-y += mynewfeature.o
    # Add dependency: mynewfeature.o depends on generated header mynewheader.h.
    # Note: Generated sources go in $(obj).
    $(obj)/mynewfeature.o: $(obj)/mynewheader.h
    # Add rule to generate the header file mynewheader.h.
    # It depends on the script and the template file.
    # Note: Generated sources go in $(obj).
    $(obj)/mynewheader.h: $(src)/ $(src)/some_template
        # (I don't know the exact command you use, but here is an example)
        $(PERL) -s $(src)/ $(src)/some_template > $@
    # Allow generated header file to be cleaned by "make clean".
    clean-files := mynewheader.h

    Reference: Special Rules in Linux Kernel Makefiles in Kernel Build System in The Linux Kernel documentation.

    For a concrete example, see the generation of "53c700_d.h" in drivers/scsi/Makefile.