Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a makefile enforce dependency restrictions in C++
    primarykey
    data
    text
    <p>We are refactoring our code base, and trying to limit the direct dependencies between different components. Our source tree has several top level directories: src/a, src/b and src/c.</p> <p>We want to enforce a set of restirctions:</p> <ul> <li>Files in a cannot depend of files in b or c</li> <li>Files in b can depend on files a but not c</li> <li>Files in c can directly depend on files in b but not a</li> </ul> <p>Enforcing the first one is simple. I have an implicit rule like this:</p> <pre><code>build/a/%.o : src/a/%.cpp $(CXX) -I src/a $(OTHER_FLAGS) -o $@ $&lt; </code></pre> <p>If a file under a tries to include a header file from b or c, the build fails as the header is not found.</p> <p>The second rule has a similar rule, which specifies src/a and src/b as include directories. The problem arises with building c. The following is allowed.</p> <pre><code>src/c/C.cpp #include "b.h" void C() { ... } src/b/b.h #include "a.h" class B { ... }; src/a/a.h class A { ... }; </code></pre> <p>Here, a file from c includes a file from b (allowed), which in turn includes a file from a (also allowed). We want to prevent code like this:</p> <pre><code>src/c/C_bad.cpp // Direct inclusion of a #include "a.h" src/c/c_bad.h // Direct inclusion of a #include "a.h" </code></pre> <p>For the allowed case to compile, the compile command for building files in src/c must include a -Isrc/a, but that allows the second cases to also compile.</p> <p>I suspect that the answer to my problem is writing a script which looks at the dependencies generated from the compiler, finds potentially illegal dependencies and then looks at the source files to determine if this is a direct dependency. Is there a reasonable way to do this combining the compiler and/or makefile constructs?</p> <p>If it matters, we are using GNU Make 3.81 and g++ 4.5.3, but would like to be portable if possible.</p> <p><strong>Update</strong></p> <p>We are looking for something where it takes effort to violate the rules, not one where it takes effort to follow the rules. (Past experience has shown that the latter is unlikely to work.) While there are some good ideas in the other answer, I'm accepting the one that says to write a script, since that is the one that takes the most effort to work around. </p> <p>Thanks to everyone for your answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload