|
| 1 | +========================== |
| 2 | +Including Files in board.h |
| 3 | +========================== |
| 4 | + |
| 5 | +.. warning:: |
| 6 | + Migrated from: https://cwiki.apache.org/confluence/display/NUTTX/Including+Files+in+board.h |
| 7 | + |
| 8 | +Global Scope of board.h |
| 9 | +======================= |
| 10 | + |
| 11 | +Each board under the ``boards/`` directory must provide a header file call |
| 12 | +``board.h`` in the board's ``include/`` sub-directory. |
| 13 | + |
| 14 | +When the board is configured symbolic links will be created to the board's |
| 15 | +``board.h`` header file at ``include/arch/board/board.h``. Header files at that |
| 16 | +location have `Global Scope` meaning they can be included by C/C++ logic |
| 17 | +anywhere in the system. For example, the ``board.h`` file can be included by |
| 18 | +any file with: |
| 19 | + |
| 20 | +.. code-block:: C |
| 21 | +
|
| 22 | + #include <arch/board/board.h> |
| 23 | +
|
| 24 | +Restricted Scope of Architecture-Specific Header Files |
| 25 | +====================================================== |
| 26 | + |
| 27 | +Each architecture also provides internal architecture-specific header files |
| 28 | +that can be included only by (1) the architecture logic itself and (2) by |
| 29 | +board logic based on that architecture. This is made possible by special |
| 30 | +include path options in the CFLAGS provided by the NuttX build system to |
| 31 | +the architecture and board logic `only`. |
| 32 | + |
| 33 | +For this reason, the scope of the architecture-specific header files is |
| 34 | +`restricted`. Any attempt to include the architecture-specific header files |
| 35 | +by logic other than architecture- or board-specific logic will result in a |
| 36 | +compilation error. This is the intended behavior; it is implemented this way |
| 37 | +to prevent the use of such `restricted`, non-portable files throughout the OS. |
| 38 | +Doing so would degrade the modularity of the OS and would take many giant |
| 39 | +steps down the path toward `spaghetti code`. |
| 40 | + |
| 41 | + |
| 42 | +Errors due to Mixed Scope of Header Files |
| 43 | +========================================= |
| 44 | + |
| 45 | +The ``board.h`` header file is included in many contexts including both by the |
| 46 | +architecture- and board-specific logic but also by other common OS logic. |
| 47 | +As examples: |
| 48 | + |
| 49 | +* Architecture-specific code may include the ``board.h`` header file to get GPIO |
| 50 | + pin definitions or to get configuration information to configure processor |
| 51 | + clocking. |
| 52 | +* Common LED and button drivers include board's ``board.h`` header file to get |
| 53 | + LED and button definitions, respectively. |
| 54 | +* Applications may include the ``board.h`` header file to get, for example, |
| 55 | + ``boardctl()`` board-specific IOCTL commands. |
| 56 | + |
| 57 | +In the first example, the ``board.h`` header file often uses pre-processor symbols |
| 58 | +that require definition from an architecture-specific header file. For example, |
| 59 | +GPIO pin definitions or clock configuration register settings. However, `you |
| 60 | +must avoid the temptation to include any of those architecture-specific header |
| 61 | +files in the board.h header file. This is prohibited!` That would be a |
| 62 | +"time bomb" waiting to cause a compilation failure in the future. |
| 63 | + |
| 64 | +So How Do I Avoid This? |
| 65 | +======================= |
| 66 | + |
| 67 | +The only way to avoid this header file inclusion trap is: |
| 68 | + |
| 69 | +* Never include architecture-specific header files in the ``board.h`` header file. |
| 70 | + Instead, |
| 71 | +* Include the header files needed by the the ``board.h`` header file in the C file |
| 72 | + that includes ``board.h`` |
| 73 | +* Make sure that ``board.h`` is the last header file included. |
0 commit comments