Skip to content

sys/drivers: guard l2filter.h, netstats.h includes#7079

Merged
kaspar030 merged 1 commit intoRIOT-OS:masterfrom
haukepetersen:opt_guard_unusedmoduleheaders
May 23, 2017
Merged

sys/drivers: guard l2filter.h, netstats.h includes#7079
kaspar030 merged 1 commit intoRIOT-OS:masterfrom
haukepetersen:opt_guard_unusedmoduleheaders

Conversation

@haukepetersen
Copy link
Copy Markdown
Contributor

as discussed in #7066, it does not hurt to include netstats.h and l2filter.h only if their modules are actually being build.

@haukepetersen haukepetersen added Area: drivers Area: Device drivers Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: network Area: Networking labels May 18, 2017
@haukepetersen haukepetersen added this to the Release 2017.07 milestone May 18, 2017
@miri64
Copy link
Copy Markdown
Member

miri64 commented May 18, 2017

I still don't see the point to be honest.

@miri64
Copy link
Copy Markdown
Member

miri64 commented May 18, 2017

Also (stemming from this) I don't understand when I should put the header into #ifdefs and when not. Because as I understand it at the moment @cgundogan proposed that anytime a module header is included it needs to be #ifdef'd by the corresponding module define. Which isn't just in my opinion, but objectively overkill (and wouldn't cut it for with this PR).

@haukepetersen
Copy link
Copy Markdown
Contributor Author

Actually, I think the rule is quite simple: if module A depends on module B, no guards around the includes of B's header(s) are needed. In case module A used module C conditionally/optionally, the header(s) of C should be guarded.

Examples of A using B(s):

  • xtimer depends on periph_timer, msg, mutex, ...
  • sc_netif depends on netapi, netopt, ...

Examples of A using C conditionally:

  • sc_netif using netstats and l2filter
  • auto_init using almost everything worth initializing...

So to my understanding (and after looking around RIOT some more), we already do escape optional headers in parts of RIOT -> see auto_init.c, sched.c, kernel_init.c, ... just to name some.

So it seemed to me, that the case with netstats as discussed in #7066 was rather an exception and a shortcoming then the rule... So considering this, I would actually opt to guard this kind of header and make it a general rule for the coding conventions for the future.

Opinions?

@jnohlgard
Copy link
Copy Markdown
Member

I agree with you @haukepetersen. The rule of thumb should be: only include what you use.

@smlng
Copy link
Copy Markdown
Member

smlng commented May 22, 2017

just curious: but why don't move the #ifdef into the respective header file (i.e., at the top there)?

This would avoid clumsy #ifdef in each and every C file that uses netstats or L2filter option. I agree with @miri64 on this point: it looks ugly and bloats the code. As we already see in this PR it touches many files (i.e., 4), instead of just the 2 header files: net/netstats.h and net/l2filter.h.

@kaspar030
Copy link
Copy Markdown
Contributor

I agree with you @haukepetersen. The rule of thumb should be: only include what you use.

Yes. This is also a prerequisite for out-of-tree modules. The header of a module might not be available if the module (/ package) is not built.

@kaspar030
Copy link
Copy Markdown
Contributor

just curious: but why don't move the #ifdef into the respective header file (i.e., at the top there)?

This would avoid clumsy #ifdef in each and every C file that uses netstats or L2filter option. I agree with @miri64 on this point: it looks ugly and bloats the code.

IMO that's the price of conditional compilation at this level.

@smlng
Copy link
Copy Markdown
Member

smlng commented May 22, 2017

IMO that's the price of conditional compilation at this level.

Sure, but in case of netstats and L2filter modules it could be somewhat optimized (?) by putting the conditions directly into the header files. However, you are right: for external packages this would not work.

@kaspar030
Copy link
Copy Markdown
Contributor

kaspar030 commented May 22, 2017

Sure, but in case of netstats and L2filter modules it could be somewhat optimized (?) by putting the conditions directly into the header files.

It might optimize readability slightly. It would de-optimize other things. Imagine a build system that rebuilds reliably only needed stuff (not needing make clean all the time). A simple touch l2filter.h would rebuild all netdev2 using applications, not only the ones actually using l2filter.

edit

There's a reason we don't just do #include "riot.h" and let the preprocessor sort out unneeded headers...

@smlng
Copy link
Copy Markdown
Member

smlng commented May 22, 2017

@kaspar030 good point! Unnecessary rebuilds are suboptimal ...

@haukepetersen
Copy link
Copy Markdown
Contributor Author

So do I read it correctly here, that we close in to a common understanding of #ifdefing out optional headers as done in this PR?

@smlng smlng added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 23, 2017
@miri64
Copy link
Copy Markdown
Member

miri64 commented May 23, 2017

Begrudgingly 👍

@haukepetersen
Copy link
Copy Markdown
Contributor Author

I added a bullet point to the coding conventions: https://github.com/RIOT-OS/RIOT/wiki/Coding-conventions#includes -> feel free to improve!

So anyone now willing to ACK this PR?

@haukepetersen haukepetersen force-pushed the opt_guard_unusedmoduleheaders branch from 7eb8b62 to 40003b8 Compare May 23, 2017 09:14
@haukepetersen
Copy link
Copy Markdown
Contributor Author

fixed issue with gnrc_ipv6_netif...

*/
void gnrc_ipv6_netif_init_by_dev(void);


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicky: why was this introduced?

Copy link
Copy Markdown
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convinced by discussion, ACK.

But remove (newly introduced) double empty line found by @miri64

@haukepetersen haukepetersen force-pushed the opt_guard_unusedmoduleheaders branch from 40003b8 to d488fdc Compare May 23, 2017 11:39
@haukepetersen
Copy link
Copy Markdown
Contributor Author

fixed newline issue

@kaspar030
Copy link
Copy Markdown
Contributor

&go.

@kaspar030 kaspar030 merged commit a3907c9 into RIOT-OS:master May 23, 2017
@haukepetersen haukepetersen deleted the opt_guard_unusedmoduleheaders branch May 23, 2017 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers Area: network Area: Networking CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants