Skip to content

Commit 7b0a2f1

Browse files
author
o-murphy
committed
unix: Extend README with fully static builds section.
Add a new section to `ports/unix/README.md` that describes how to perform fully static builds of the MicroPython unix port. This provides clearer guidance for advanced users and CI configurations, improving documentation clarity. Fixes #17399, #17353 Signed-off-by: o-murphy <thehelixpg@gmai.com>
1 parent 5f058e9 commit 7b0a2f1

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

ports/minimal/Makefile – копія:Zone.Identifier

Whitespace-only changes.

ports/unix/README.md

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,24 @@ The executable will be built at `build-minimal/micropython`.
102102
Additional variants can be found in the `variants` sub-directory of the port,
103103
although these are mostly of interest to MicroPython maintainers.
104104

105-
### Standalone build
105+
### Standalone `libffi` build
106106

107107
By default, the "standard" variant uses `pkg-config` to link to the system's
108108
shared `libffi` library.
109109

110110
It is possible to instead build a standalone MicroPython where `libffi` is built
111111
from source and linked statically into the `micropython` executable. This is
112-
mostly useful for embedded or cross-compiled applications.
112+
mostly useful for embedded or cross-compiled applications where `libffi` needs
113+
to be bundled.
113114

114-
Building standalone requires `autoconf` and `libtool` to also be installed.
115+
Building standalone `libffi` requires `autoconf` and `libtool` to also be installed.
115116

116-
To build standalone:
117+
To build with standalone `libffi`:
117118

118119
$ export MICROPY_STANDALONE=1
119-
$ make submodules # fetches libffi submodule
120-
$ make deplibs # build just the external libraries
121-
$ make # build MicroPython itself
120+
$ make submodules # fetches libffi submodule
121+
$ make deplibs # build just the external libraries (libffi)
122+
$ make # build MicroPython itself
122123

123124
`make deplibs` causes all supported external libraries (currently only `libffi`)
124125
to be built inside the build directory, so it needs to run again only after
@@ -128,6 +129,73 @@ If you intend to build MicroPython with additional options (like
128129
cross-compiling), the same set of options should be passed to both `make
129130
deplibs` and `make`.
130131

132+
### Fully Static Builds (for minimal environments)
133+
134+
For deploying MicroPython to very minimal Linux environments (like those generated by Buildroot)
135+
or for creating a truly self-contained binary, you might need to build MicroPython
136+
with **all** its dependencies statically linked. This means including the C standard library
137+
(`libc`), pthreads, and other system libraries directly into the executable, removing
138+
the dependency on the target system's dynamic linker (`ld.so`).
139+
140+
**Important Considerations:**
141+
* **Increased Binary Size:** Statically linked executables are significantly larger.
142+
* **No Runtime Library Updates:** Security patches or updates to system libraries
143+
will require recompiling and redeploying your MicroPython binary.
144+
* **Toolchain Support:** Your compiler and linker toolchain (especially for cross-compilation)
145+
must provide the necessary static library archives (`.a` files).
146+
* **Compatibility:** Statically linking `glibc` on Linux can sometimes lead to
147+
compatibility issues if not done carefully, as some `glibc` features are designed
148+
with dynamic linking in mind. `musl libc` is often a better choice for fully
149+
static embedded builds due to its design for static linking.
150+
151+
**Steps for a Fully Static Build:**
152+
153+
1. **Ensure Static Library Development Packages:**
154+
For your host system or cross-compilation target, you'll need the static
155+
development packages for the C standard library and any other required libraries.
156+
For `glibc` on Debian/Ubuntu, this typically means `libc6-dev` (or `libc6-dev:i386`
157+
for 32-bit builds). For cross-compilation (e.g., `mipsel-linux-gnu`), you'd need
158+
`libc6-dev-mipsel-cross` and potentially `libpthread-stati`c for thread support.
159+
160+
Example for `mipsel-linux-gnu` on Debian/Ubuntu:
161+
```bash
162+
sudo apt-get install build-essential git python3 pkg-config libffi-dev autoconf libtool
163+
sudo apt-get install gcc-mipsel-linux-gnu g++-mipsel-linux-gnu libc6-mipsel-cross libc6-dev-mipsel-cross
164+
# If thread support is enabled (MICROPY_PY_THREAD=1), you might also need:
165+
# sudo apt-get install libpthread-stubs0-dev # Or similar static pthread dev package
166+
```
167+
168+
2. **Build MicroPython with `LDFLAGS_EXTRA="-static"`:**
169+
This flag instructs the linker to prefer static linking. Combine this with
170+
`MICROPY_STANDALONE=1` to ensure `libffi` is also statically bundled.
171+
172+
Example for a fully static `mipsel` build:
173+
```bash
174+
cd ports/unix
175+
make -C ../../mpy-cross
176+
make submodules
177+
export MICROPY_STANDALONE=1
178+
# For cross-compilation, add CROSS_COMPILE and ensure path to toolchain is correct
179+
make deplibs CROSS_COMPILE=mipsel-linux-gnu- LDFLAGS_EXTRA="-static"
180+
make CROSS_COMPILE=mipsel-linux-gnu- LDFLAGS_EXTRA="-static"
181+
```
182+
Replace `mipsel-linux-gnu-` with your actual `CROSS_COMPILE` prefix if different.
183+
184+
3. **Verification:**
185+
After building, use `file` and `ldd` (if available for the target architecture)
186+
to confirm that the binary is statically linked:
187+
188+
```bash
189+
file build-<variant>/micropython
190+
ldd build-<variant>/micropython # For cross-compiled targets, you might need a target-specific ldd
191+
```
192+
A truly static binary will show "statically linked" and `ldd` will report "not a dynamic executable" or similar.
193+
194+
If you encounter linker errors during a fully static build, it almost always means
195+
that the static library (`.a` file) for a required dependency is missing from your
196+
toolchain's search paths or simply not installed. You'll need to identify the
197+
missing library and install its corresponding static development package.
198+
131199
### Other dependencies
132200

133201
To actually enable/disable use of dependencies, edit the

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy