A lightweight wrapper for Clang and Clang++ to work around limitations in the Arduino build system.
The Arduino build system applies specific compiler flag combinations based on file extensions (.c
, .cpp
, .S
). It determines the appropriate recipe by extracting the file extension and applying predefined flags. However, this approach lacks flexibility when per-file flag customization is required.
One specific issue arises when compiling the HardwareSerial1.cpp
file from the Arduino AVR core. Clang's Link-Time Optimization (LTO) aggressively removes what it considers unused functions, causing critical functionality to break in the final binary.
This wrapper introduces a special --skip-lto
flag, which allows users to specify files where LTO should be disabled. When invoked, the wrapper:
- Checks if any of the specified files match the current compilation target.
- If a match is found, appends
-fno-lto
to the compiler arguments before invoking Clang. - Otherwise, it forwards all arguments unchanged.
This approach ensures that essential functions remain intact while maintaining the benefits of LTO for other files. Future versions may extend functionality for additional per-file flag manipulations.
make
sudo make install PREFIX=/usr/local
To build for Windows:
make windows
# Disable LTO for specific files
clang-wrapper -O3 -flto --skip-lto=file1.c;file2.c -o output.o -c input.c
# Use as C++ compiler
clang++-wrapper -O3 -flto --skip-lto=file1.cpp;file2.cpp -o output.o -c input.cpp
This project is licensed under the Apache License 2.0. See the LICENSE file for details.