Makefile issues

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Makefile issues

Postby iggarpe » Tue Oct 29, 2013 9:36 pm

Hi all,

While compiling a Makefile based project under linux I've encountered what seems to be a bug in the ChibiOS/RT makefile mechanism: when you use in your project a source file that:

1- Is not located in the root directory of your project.
2- Has the same name than some ChibiOS/RT file.

Then make compiles the ChibiOS/RT source file, NOT YOURS.

Example:

1- Copy chprintf.c into a subfolder in your project.
2- Modify the Makefile to use subfolder/chprintf.c source instead of $(CHIBIOS)/subdir/chprint.c
3- Edit subdir/chprintf.c and add a #error directive so compilation stops if this file is actually compiled.
4- Run make

You'll see the project compile without errors.

If you repeat the steps above BUT you place the modified chprintf.c file in the root directory of your project (and modify your makefile to reflext this new location) then the compilation will stop with an error as expected.

Of course this is no big deal, but it's quite annoying and caused a nice headache until I figured out what was going on. Any ideas on where the problem is and how to fix it?

Thanks.

P.S: I'm using ChibiOS/RT 2.6.1 and this toolchain: https://launchpad.net/~terry.guo/+archi ... m-embedded

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Makefile issues

Postby iggarpe » Tue Oct 29, 2013 9:56 pm

I've just verified that no matter the path you specify for chprintf.c in YOUR makefile, make will FIND and compile always $(CHIBIOS)/os/various/chprintf.c.

For example if instead of:

$(CHIBIOS)/os/various/chprintf.c

You place something like this:

$(CHIBIOS)/thispathdoesntexist/chprintf.c

The project will compile without problems.

efuentes
Posts: 35
Joined: Wed Jul 25, 2012 6:16 am

Re: Makefile issues

Postby efuentes » Tue Oct 29, 2013 10:10 pm

This is because the makefiles use vpath.

The simple solution is to give each file a unique file name.

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: Makefile issues

Postby Giovanni » Tue Oct 29, 2013 10:27 pm

Correct, it is an intrinsic limitation in Make, I haven't found a workaround except letting the .o files be in the same directory of .c files but I don't like this.

Probably a check could be added to stop the compilation with a clear error message.

Giovanni

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Makefile issues

Postby iggarpe » Tue Oct 29, 2013 11:16 pm

I delved a little into the rules.mk file and found the problem:

SRCPATHS = $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(ASRC)) $(dir $(TSRC)))
VPATH = $(SRCPATHS)

As you see make will look for a source file in ANY of the source file directories. To make things worse the source directory list is sorted thus not even allowing you to give your sources precedence over ChibiOS/RT sources in your makefile.

That means if there's a source file in ANY source directory which is named like one of your sources it will take precedence, even if it's not even included as a source itself, just because some OTHER source is in that directory, which becomes part of the list of directories in which to look for sources.

Seriously, I think this approach is fundamentally flawed and needs fixing.

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: Makefile issues

Postby Giovanni » Wed Oct 30, 2013 9:10 am

The problem is not with sources but that .o files are all generated in the same directory, so you cannot have name conflicts. I haven't found a way to build a subtree of object files. Same with .lst files but less critical.

Giovanni

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Makefile issues

Postby iggarpe » Wed Oct 30, 2013 9:21 am

Sorry, I posted my last message without first refreshing so I didn't see your replies.

I'm ok with using unique names for the files in one's project. The opposite is bad practice and can lead to all sorts of problems. However here we're talking about external files not even included in the compile that conflict (and overtake) your own files. With ChibiOS/RT most of the time this is not a problem because Giovanni followed a naming convention that makes conflicts unlikely (the 'ch' prefix, the '_lld' suffix, etc). I guess I just stumbled upon the file with more potential to cause problems: shell.c (don't know why I used chprintf.c for the example, I encountered the problem when using my own shell in my project).

I do like having all the object files separate from the source. Would it be possible to create a subdirectory tree into the build dir reflecting the source code organization?. That would also allow to have source files with the same name (which is a no-no IMO, but anyway).

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Makefile issues

Postby iggarpe » Wed Oct 30, 2013 9:35 am

Would this work?:

1- Remove VPATH.

2- Keep the object files full path (i.e. just replace .c with .o and add $(OBJDIR) prefix)

3- In each static rule do a mkdir -p $(dir $(ACOBJS))

The only shortcoming I see is with sources in directories outside of your project (../ChibiOS-RT261 in my case).

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Makefile issues

Postby iggarpe » Wed Oct 30, 2013 9:42 am

I just tested the above and it works. Recreates the source tree structure into the build subdirectory.

Problem now is:

1- Relative paths pointing outside of your sources: they are being created relative to the build/obj directory and hence outside of it). Maybe if we remove all the "../" from the beginning of those paths...

2- Absolute paths (may result in a path too long when trying to create them as subdirectories in the build/obj directory).

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: Makefile issues

Postby Giovanni » Wed Oct 30, 2013 10:06 am

The issue with paths outside the ChibiOS directory makes that solution problematic. Would be possible to add an unique numeric prefix to .o and .lst files? something like 0034_adc.o starting from adc.c.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 10 guests