title: Makefile variables: difference between "MY_FILE" and "obj-m"
body: <p>I have two simple programs (application, driver) and a Makefile that dynamically compiles these applications. When I run "make test_prg", it compiles the application correctly. This doesn't, however, work for the driver "make test_drv".</p>
  
  <p>~/test/test_prg/hello-world.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(void) {
      printf("Hello world\n");
      return 0;
  }
  </code></pre>
  
  <p>~/test/test_drv/hello-world.c</p>
  
  <pre><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/init.h&gt;
  int __init hello_init(void) {
      printk(KERN_INFO "Hello world\n");
      return 0;
  }
  void __exit hello_exit(void) {
      printk(KERN_INFO "Bye world\n");
  }
  module_init(hello_init);
  module_exit(hello_exit);
  </code></pre>
  
  <p>~/test/Makefile</p>
  
  <pre><code>MAKEFLAGS       := -B
  DRV_PATH        := /lib/modules/$(shell uname -r)/build
  PRG_PATH        := .
  
  MY_PATH         := 
  MY_TARGET       :=
  MY_FILE         ?=
  
  test_%: _setup_test_%
          make M=$(PWD) -C $(MY_PATH) MY_FILE=$@/$(MY_FILE) $(MY_TARGET)
  
  build:
          gcc $(MY_FILE)
  
  _setup_test_drv:
          $(eval MY_PATH=$(DRV_PATH))
          $(eval MY_TARGET=modules)
          $(eval obj-m=hello-world.o)
          echo $(obj-m) $(MY_PATH)
  
  _setup_test_prg:
          $(eval MY_PATH=$(PRG_PATH))
          $(eval MY_TARGET=build)
          $(eval MY_FILE=hello-world.c)
  </code></pre>
  
  <p>The correct values are set, but it doesn't find the module to compile:</p>
  
  <blockquote>
  <pre><code>echo hello-world.o /lib/modules/4.6.7-pd-4.6.y/build
  hello-world.o /lib/modules/4.6.7-pd-4.6.y/build
  make M=/home/timgru/test -C /lib/modules/4.6.7-pd-4.6.y/build MY_FILE=test_drv/ modules
  make[1]: Entering directory '/usr/src/linux-headers-4.6.7-pd-4.6.y'
    Building modules, stage 2.
    MODPOST 0 modules
  make[1]: Leaving directory '/usr/src/linux-headers-4.6.7-pd-4.6.y'
  </code></pre>
  </blockquote>
  
  <p>Strangely, if I hard-code the "driver parameters", the driver's being compiled correctly:</p>
  
  <p>~/test/test_drv/Makefile</p>
  
  <pre><code>obj-m:=hello-world.o
  #hello-world-objs:=$(shell find . -name '*.c')
  
  all:
      make -C/lib/modules/$(shell uname -r)/build M=$(PWD) modules
  </code></pre>
  
  <p>I'm trying to understand why the ~/test/Makefile solution for compiling the driver doesn't work. Furthermore, I would like to bypass this problem somehow :-)</p>