@@ -102,23 +102,24 @@ The executable will be built at `build-minimal/micropython`.
102
102
Additional variants can be found in the ` variants ` sub-directory of the port,
103
103
although these are mostly of interest to MicroPython maintainers.
104
104
105
- ### Standalone build
105
+ ### Standalone ` libffi ` build
106
106
107
107
By default, the "standard" variant uses ` pkg-config ` to link to the system's
108
108
shared ` libffi ` library.
109
109
110
110
It is possible to instead build a standalone MicroPython where ` libffi ` is built
111
111
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.
113
114
114
- Building standalone requires ` autoconf ` and ` libtool ` to also be installed.
115
+ Building standalone ` libffi ` requires ` autoconf ` and ` libtool ` to also be installed.
115
116
116
- To build standalone:
117
+ To build with standalone ` libffi ` :
117
118
118
119
$ 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
122
123
123
124
` make deplibs ` causes all supported external libraries (currently only ` libffi ` )
124
125
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
128
129
cross-compiling), the same set of options should be passed to both `make
129
130
deplibs` and ` make`.
130
131
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
+
131
199
# ## Other dependencies
132
200
133
201
To actually enable/disable use of dependencies, edit the
0 commit comments