Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Makefile for simple linux device driver

Hello everyone,

i'm trying to write a device driver. It is an external driver. It should be load with insmod in an running system.

My problem is, that i cannot build the module (I started with a 'Hello Kernel' sample). I have reviewed the documentation, which comes along with the linux distribution. I used following makefile source, which can be found in the doc:

com.microtronix.nios2linux.kernel_1.4.0\linux-2.6.x\Documentation\kbuild\modules.txt

assume this:

- the compiled and build kernel is in D:\MyKernel

- the new module source is in D:\MyModule

- and the linux source is standard path

makefile:

------------

ifneq ($(KERNELRELEASE),)

# kbuild part of makefile

obj-m := mymodule.o

else

# Normal Makefile

# KERNELDIR := /lib/modules/`uname -r`/build

KERNELDIR := D:/MyKernel/Build

all::

$(MAKE) -C $KERNELDIR M=`pwd` $@

endif

Obviously it doesn't work because it tries to build for an x86 architecture

so , i set ARCH and CROSS_COMPILE references in the command line. (Note i use the nios ide, of course)

But it doesn't work. Maybe i didn't understand it correctly. Does someone have a working sample?

I want to use makefile, since i don't want to collect all arch-information and core-depandant in each makefile!

Thank you very much.

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    ifneq ($(KERNELRELEASE),)#  kbuild part of makefile
    obj-m := mymodule.o
    else#  Normal Makefile
    ARCH := nios2nommu
    CROSS_COMPILE := nios2-elf-
    KERNELDIR := $(KERNEL_PLUGIN)/linux-2.6.x
    BUILDDIR := /cygdrive/d/MyKernel
    all::
      $(MAKE) -C $(KERNELDIR) M=`pwd` O=$(BUILDDIR) -O $@
    endif

    Give the above a try. I added a couple of variables (ARCH and CROSS_COMPILE)... and I've made sure that all you're variables are referenced using the standard $() syntax for Makefiles. Don't forget to add a "TAB" to the $(MAKE) line.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you very much for your quick reply, Ken.

    Unfortunately it doesn't work.

    Maybe it's good idea to sum up, what i did so far:

    1) I created a kernel project, with this external module loading/unloading support enabled:

    the project directory with the compiled and build files is located at:

    -> d:\MyKernel

    2) I made a Linux Filesystem of course

    3) To create module project, i used "Linux Application". It is located in:

    -> d:\MyModule

    The name of .c file is MyModule.c

    i do not use the settings und rules files. its just to have project in my workspace.

    5) a lot different makefiles

    4) I tried your suggested makefile, but it doesn't work. I get a syntax error about the "-O"

    so i deleted the "-O" option, and tried again. Now I get following error:

    make all

    make -C M='pwd' O=/cygdrive/d/MyKernel/build all

    make: *** M=pwd: No such file or directory. Stop.

    make: *** [all] Error 2

    --

    Thanks a lot.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Well, the syntax error with the M='pwd' is because you're using the wrong "ticks".

    It should be M=`pwd`... using the backticks.

    After fixing the backticks, try putting the O= option back in as well and let me know how it goes. It sounds like you're almost there but not quite.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you again.

    the tip the taps took me a step nearer to the goal.. but it still get a syntax error about '-O', it shows me, that in the syntax list isn't an option with cap O but only a tiny 'o' (this time i did a copy&paste to insert your makefile).

    After all, i'm still not able build the module (by the way... setting enviromental vars ARCH and CROSS_COMPILE in your makefile doesnt work. It works if i set it in the build command line in the 'Create new make target' menu option.)

    With these modification and your suggested make command...

           $(MAKE) -C $(KERNELDIR) M=`pwd` O=$(BUILDDIR) -o $@

    ...i get:

     Nothing to be done for all

    After deleting $@ and trying again i get the following output:

    make ARCH=nios2nommu CROSS_COMPILE=nios2-elf- all 
    make  -C /cygdrive/d/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_1.4.0/linux-2.6.x M=`pwd` 
    O=/cygdrive/d/MyKernel/build -o all
    make: Entering directory `/cygdrive/d/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_1.4.0/linux
    -2.6.x'
      no emulation specific options.
      LD      /cygdrive/d/MyModule/built-in.o
      CC   /cygdrive/d/MyModule/mymodule.o
    /cygdrive/d/MyModule/.mymodule.o.d: done.
      Building modules, stage 2.
      MODPOST
    Signal 11
    make: ***  Error 139
    make: ***  Error 2
    make: ***  Error 2
    make: Leaving directory `/cygdrive/d/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_1.4.0/linux-
    2.6.x'
    make: ***  Error 2

    In addition the build process adds following files and directories to my project folder:

    - a mymodule.o file

    - a buit-in.o

    - a .built-in.o.cmd file

    - a .mod.o.cmd file

    - a '.tmp_versions' directory with a 'mymodule.mod' file

    Maybe I do every possible mistake, that can be done?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    oh geez... I'm so sorry... I didn't even see the -O in the example Makefile I gave. That was a mistake on my part. Sorry for that.

    The line should read:

    $(MAKE) -C $(KERNELDIR) M=`pwd` O=$(BUILDDIR) modules

    aie. That should be what you need.

    The MODPOST error is due to "dos linefeeds" in the Modules.symvers file (in your kernel build directory). So you'll need to run the file through dos2unix.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you for help Ken and for your patience with me.

    It works... the "dos2unix hint" solved my problem.

    Again, thank you very much.