# makefile driver kernel out of src folder



## alemoppo (Apr 30, 2021)

Hello!

I would like to manage the source files in folders in the development of a kernel module.
I'm trying to run the makefile with this setup:


```
- Makefile
+ src
    - mySrc.c
+ obj
+ bin
...
```

How can i build with this strucure? My makefile is actually (this run great in the same folder of the source file):

```
SRCS=mySrc.c
KMOD=myModule

.include <bsd.kmod.mk>
```

How i can build the module with the folder structure?
I tried with:

```
SRCS=src/mySrc.c
KMOD=myModule

.include <bsd.kmod.mk>
```
And `make` produces:
`error: unable to open output file 'src/mySrc.o': 'No such file or directory'`

I think i have to setup some variable (?) Where i can check this?

It would be nice also if the myModule.ko was moved in the bin folder and the objects in obj folder.

Thank you
Best regards.


----------



## covacat (Apr 30, 2021)

.PATH:  ${.CURDIR}/src


----------



## alemoppo (Apr 30, 2021)

Great!

With 
	
	



```
.PATH:  ${.CURDIR}/src
```
 it take the ./src source and "magically" put the content in obj folder.

How can I setup also che obj folder? And how can I move the myModule.ko in bin folder? (I could do this with mv comman but there is probably a better way....).

Is there any documentation of these variables?

Thanks!


----------



## SirDice (Apr 30, 2021)

alemoppo said:


> Is there any documentation of these variables?


make(1)?

```
.PATH           A variable that represents the list of directories that
                     make will search for files.  The search list should be
                     updated using the target ‘.PATH’ rather than the
                     variable.
```


----------



## alemoppo (Apr 30, 2021)

Thank you.

With:

```
.OBJDIR: ${.CURDIR}/obj/mysubfolders
```
I can put all the object in the obj/mysubfolder.
Now I want to move the myModule.ko from obj/mysubfolder to bin/mysubfolder.
I can't find any variable to set where to put the .ko module.

I tried with the mv command:


```
.OBJDIR: ${.CURDIR}/obj/mysubfolder
.PATH: ${.CURDIR}/src
SRCS=mySrc.c
KMOD=myModule

.include <bsd.kmod.mk>

        mv ${OBJDIR}/${KMOD}.ko ${.CURDIR}/bin/mysubfolder
```

But I get:

```
Unassociated shell command "mv ${OBJDIR}/${KMOD}.ko ${.CURDIR}/bin/mysubfolder"
make: Fatal errors encountered -- cannot continue
```


----------



## covacat (Apr 30, 2021)

```
.SUFFIXES: .c.o
.OBJDIR = obj
.PATH: $(.CURDIR)/src
BINDIR=$(.CURDIR)/bin

OBJS = 1.o 2.o
.c.o:
        cc -c $(.IMPSRC)


all: $(BINDIR)/x
$(BINDIR)/x:    $(OBJS)
        cc $(OBJS) -o $(.TARGET)     


clean:
        rm -f *.o $(BINDIR)/*
```


$ls src
1.c    2.c

see 

/usr/share/doc/psd/12.make/paper.ascii.gz


----------



## alemoppo (May 3, 2021)

covacat said:


> see
> 
> /usr/share/doc/psd/12.make/paper.ascii.gz



I can't find the "psd" folder in /usr/share/doc

Currently my Makefile is:


```
.SUFFIXES: .c.o
.OBJDIR = obj
.PATH: $(.CURDIR)/src
BINDIR=$(.CURDIR)/bin

OBJS = mySrc.c
.c.o:
        cc -c $(.IMPSRC)


all: $(BINDIR)/myKernel
$(BINDIR)/myKernel:    $(OBJS)
        cc $(OBJS) -o $(.TARGET)


clean:
        rm -f *.o $(BINDIR)/*
```

When i do make, i get:



> <my path> # make
> <my path>/Makefile" line 8: Missing dependency operator
> <my path>/Makefile" line 13: Need an operator
> <my path>/Makefile" line 17: Need an operator


----------



## covacat (May 3, 2021)

alemoppo said:


> I can't find the "psd" folder in /usr/share/doc
> 
> Currently my Makefile is:
> 
> ...


use tab for indenting not spaces


----------



## alemoppo (May 3, 2021)

Yes! Tab was an error.

I added also $(.PATH) to OBJS variable. So now I have:

```
.SUFFIXES: .c.o
.OBJDIR = obj
.PATH: $(.CURDIR)/src
BINDIR=$(.CURDIR)/bin

OBJS = $(.PATH)/mySrc.c
.c.o:
        cc -c $(.IMPSRC)


all: $(BINDIR)/myKernel
$(BINDIR)/myKernel:    $(OBJS)
        cc $(OBJS) -o $(.TARGET)


clean:
        rm -f *.o $(BINDIR)/*
```

But now i got the errors for the internal dependencies, for example it doesn't recognise "d_open_t, d_close_t, ..., D_VERSION, uprintf, ... " (total of 12 warnings + 20 errors). Probably i have to modity the includes in .c file? 
	
	



```
#include <??/sys/xxx.h>
```
 Or it's required someting defined in 
	
	



```
.include <bsd.kmod.mk>
```
 ?

(I followed example 1 here, i can compile without any problemi with the original makefile).

Thanks for the help!


----------

