Skip to content

Commit d14f316

Browse files
committed
Documentation/Makefile: add make_build_system.rst
1 parent abf7a74 commit d14f316

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

Documentation/guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Guides
5252
fully_linked_elf.rst
5353
building_nuttx_with_app_out_of_src_tree.rst
5454
building_uclibcpp.rst
55+
make_build_system.rst
5556
custom_app_directories.rst
5657
multiple_nsh_sessions.rst
5758
nsh_network_link_management.rst
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
.. contributor license agreements. See the NOTICE file distributed with
3+
.. this work for additional information regarding copyright ownership. The
4+
.. ASF licenses this file to you under the Apache License, Version 2.0 (the
5+
.. "License"); you may not use this file except in compliance with the
6+
.. License. You may obtain a copy of the License at
7+
..
8+
.. http://www.apache.org/licenses/LICENSE-2.0
9+
..
10+
.. Unless required by applicable law or agreed to in writing, software
11+
.. distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
.. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
.. License for the specific language governing permissions and limitations
14+
.. under the License.
15+
16+
.. _make_build_system:
17+
18+
Make Build System
19+
=================
20+
21+
This guide explains the NuttX `make`-based build system.
22+
23+
Overview
24+
--------
25+
26+
The build system uses a series of Makefiles to compile the NuttX kernel and
27+
your applications.
28+
29+
Key components:
30+
31+
- **Root `Makefile`:** The main entry point for any build. It's located in the
32+
NuttX root directory. It checks for a ``.config`` file and then pulls in the
33+
correct host-specific Makefile (``tools/Unix.mk`` or ``tools/Win.mk``).
34+
35+
- **Host-specific Makefiles:** ``tools/Unix.mk`` and ``tools/Win.mk`` contain
36+
the core build logic. They set up the environment, create symlinks, build
37+
libraries, and link the final executable.
38+
39+
- **LibTargets.mk:** ``tools/LibTargets.mk`` is included by the host-specific
40+
Makefiles. It handles compiling source files and creating libraries
41+
(``.a`` files) for both the kernel and applications.
42+
43+
- **Config.mk:** ``tools/Config.mk`` defines configuration variables based
44+
on ``.config`` file. It sets compiler flags, paths, and other build
45+
options.
46+
47+
Verbosity
48+
~~~~~~~~~
49+
50+
You can control the build verbosity with the ``V`` variable:
51+
52+
* **Quiet (Default):** By default, the build output is minimal.
53+
54+
.. code-block:: console
55+
56+
make
57+
58+
* **Verbose (`V=0` or `V=1`):** Shows the full compiler commands. There is no
59+
difference between ``V=0`` and ``V=1``.
60+
61+
.. code-block:: console
62+
63+
make V=1
64+
65+
* **Debug (`V=2`):** Enables verbose output and adds extra debug flags to
66+
certain build tools, such as the ``mkexport.sh`` script.
67+
68+
.. code-block:: console
69+
70+
make V=2
71+
72+
Common Build Setup
73+
------------------
74+
75+
Entrypoint: **Root `Makefile`**
76+
77+
The build starts in the root ``Makefile``. Its main job is to check for a
78+
``.config`` file and then include the appropriate (either Unix or Windows)
79+
**host-specific Makefile**.
80+
81+
Unix Build Setup
82+
----------------
83+
84+
Entrypoint: **``tools/Unix.mk``**
85+
86+
The build system, driven by ``tools/Unix.mk``, uses symlinks to create a source
87+
tree view that matches your ``.config``. This lets the core NuttX code stay
88+
generic while pointing to the specific board and architecture files needed for
89+
your build.
90+
91+
1. **Initial Setup:**
92+
* The build starts by setting the ``TOPDIR`` and ``WSDIR`` variables.
93+
* The main ``Make.defs`` file gets included, set while running
94+
``tools/configure.sh``.
95+
* If GIT is available, the build system will impact versioning.
96+
See :ref:`versioning`.
97+
98+
2. **Directory Setup:** Key directories variables are defined:
99+
* ``ARCH_DIR``: Points to the architecture directory, determined by
100+
``CONFIG_ARCH``.
101+
* ``APPDIR``: Points to the applications directory, determined by
102+
``CONFIG_APPS_DIR``.
103+
* ``EXTERNALDIR``: Points to the ``external/`` directory if it contains a
104+
``Kconfig`` file.
105+
* ``tools/Directories.mk`` gets included. See :ref:`directories_mk`.
106+
* ``CONTEXTDIRS``: Directories needing a one-time setup.
107+
* ``CLEANDIRS``: All directories the ``clean`` target needs to hit.
108+
109+
3. **Symbolic Linking:** The ``context`` target creates symlinks to map generic
110+
paths to your configured source files. This is the core of the configuration
111+
process. For example:
112+
* ``include/arch`` links to the correct ``arch/$(CONFIG_ARCH)/include`` directory.
113+
* ``drivers/platform`` links to the board-specific driver directory.
114+
115+
4. **Final Context:** The build touches ``.context`` files in the subdirectories
116+
(``CONTEXTDIRS``) to mark the context setup as complete for those locations.
117+
118+
The Build Process
119+
-----------------
120+
121+
Running ``make`` kicks off a sequence of steps to build the final executable.
122+
123+
1. **Configure:** First, you need a ``.config`` file. You typically create
124+
this by running ``make menuconfig``, selecting your board and options, and
125+
saving the configuration.
126+
127+
2. **Set Up Context:** The ``context`` target runs automatically. It creates
128+
symlinks and generates ``config.h`` and ``version.h`` based on your
129+
``.config`` file.
130+
131+
3. **Build Libraries:** The build system compiles the source code and creates
132+
a set of libraries (``.a`` files) in the ``staging/`` directory.
133+
134+
4. **Link Executable:** Finally, the build system links all the libraries from
135+
``staging/`` to create the ``nuttx`` executable in the root directory.
136+
137+
The build is modular, so it only recompiles files that have changed, making
138+
development faster.
139+
140+
Cleaning the Build
141+
------------------
142+
143+
There are two targets for cleaning the build:
144+
145+
**``make clean``**
146+
147+
This removes most build artifacts like object files (``.o``), libraries (``.a``),
148+
and the final ``nuttx`` executable.
149+
150+
It intentionally **leaves** your ``.config`` file, the ``staging/`` directory,
151+
and the symlinks created by the build. This is useful when you want to do a
152+
fresh build without losing your configuration.
153+
154+
**``make distclean``**
155+
156+
This is a complete reset. It does everything ``clean`` does, but **also deletes**:
157+
158+
* Your ``.config`` file
159+
* The ``staging/`` directory
160+
* All symlinks
161+
162+
Use this to return your source tree to a pristine state.
163+
164+
.. _directories_mk:
165+
166+
Directories.mk
167+
--------------
168+
169+
``tools/Directories.mk`` defines groups of directories used throughout the
170+
171+
.. _libtargets_mk:
172+
173+
LibTargets.mk
174+
-------------
175+
176+
The ``tools/LibTargets.mk`` file is the engine for building all the libraries
177+
(archives) for the kernel and apps. It's included by ``tools/Unix.mk`` and
178+
handles compiling source files and installing the resulting libraries into the
179+
``staging/`` directory.
180+
181+
A key job for ``LibTargets.mk`` is handling NuttX's build modes:
182+
183+
* **Flat Build (``CONFIG_BUILD_FLAT``):** The simplest mode. Kernel and
184+
user-space code are built into a single binary.
185+
* **Protected Build (``CONFIG_BUILD_PROTECTED``):** Kernel and user-space
186+
code are built separately, with the kernel in a privileged mode.
187+
* **Kernel Build (``CONFIG_BUILD_KERNEL``):** Similar to the protected build,
188+
but with support for kernel modules.
189+
190+
The Makefile contains different logic for kernel-mode builds (Protected/Kernel)
191+
versus user-mode builds (Flat) to ensure libraries are compiled with the
192+
correct flags for each scenario.

Documentation/guides/versioning_and_task_names.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Answer
3333

3434
This is probably normal behavior. There are two separate, unrelated issues here.
3535

36+
.. _versioning:
37+
3638
Versioning
3739
----------
3840

0 commit comments

Comments
 (0)