Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building relative to src/ directory with SCons

Tags:

scons

I have an app with the following (I would have thought quite common) directory hierarchy:

/src
    subdir1/  # Subdirs with more source files.
        more.c
        SConscript
    foo.c     # Source files.
    foo.h
    SConscript
/other        # Other top-level directories with no source code.
/stuff        # However, there are other assets I may want to build.
README        # Other top-level files.
SConstruct

The problem is that when I run scons from the top-level directory, it calls gcc from that directory without cding into src, like this:

gcc -o src/foo.o src/foo.c

This is problematic for several reasons:

  1. Within my program, I #include files giving the path relative to the src directory. For example, more.c could include foo.h with #include "foo.h". This fails because GCC is run from the parent directory. I don't want to change my includes to #include "src/foo.h".
  2. I use the __FILE__ special macro for things like logging. When built from the top-level directory, GCC puts "src/" at the front of all filenames, since that was the path it was given to compile. This may seem picky, but I don't want that, because I think of my source tree as being relative to the src directory.

(Edit: I should add that obviously #1 can be fixed by adding -Isrc as a flag to GCC, but this seems like more hacks around the main issue.)

How can I make SCons cd into the src directory before calling gcc?

  • I don't want to get rid of the src directory and move everything up, because there are lots of other (non-code) files at the top level.
  • I don't want SCons to cd into every subdirectory. It should just cd into src and then build all files in the hierarchy from there.
  • I could solve this by moving SConscript inside the src directory and running it from there, perhaps using a Makefile at the top level. But this seems quite hacky, and I also do want to use SCons to build (non-code) assets in other directories than src.

I've read that you can make a custom Builder and make it change directories. However, I do not want to write a whole new Builder for C/C++. Is there a way to modify the behaviour of an existing builder without writing one from scratch? Also, on one forum, someone said that changing directories from within a Builder would break parallel builds since it would change the directory that other tasks are building from. Is this true?

like image 682
mgiuca Avatar asked Oct 22 '22 10:10

mgiuca


1 Answers

This behavior is just how SCons works, and its not possible to avoid/change. I've been looking for some supporting documentation in its favor, and havent found any. Its just something Ive become used to.

As you mention, the include paths are simple to fix. The harder part is the __FILE__ macro. I hadnt ever noticed this until you mentioned it. Unfortunately, I think the only way around this is to strip the path in the logger, which is a rather ugly fix.

like image 155
Brady Avatar answered Nov 09 '22 10:11

Brady