CLionArduinoPlugin icon indicating copy to clipboard operation
CLionArduinoPlugin copied to clipboard

Optimize compiler options

Open amichair opened this issue 5 years ago • 0 comments

Compiling the same project using CLion with the plugin results in a binary that is much larger than when compiling it with the Arduino IDE. In these microcontrollers with so little flash/ram, every byte matters, so it would be great if compiling in CLion with this plugin would optimize the compiler options to make the output as small as possible, ideally identical to what the Arduino IDE produces (or if possible, better).

For a start, there should be an option (either by default, or with a checkbox in configuration, or at the very least commented out in the generated CMakeLists file) to use the compiler from the Arduino SDK rather than the system default:

set(CMAKE_C_COMPILER   ${ARDUINO_SDK_PATH}/hardware/tools/avr/bin/avr-gcc)
set(CMAKE_CXX_COMPILER ${ARDUINO_SDK_PATH}/hardware/tools/avr/bin/avr-g++)

In my case the Ubuntu built-in compiler was older than the one downloaded with Arduino SDK, which made a huge difference in compiled size. I understand there may be some cases where the opposite is true, but using the same compiler as Arduino IDE seems reasonable, given that you also use other things from the Arduino IDE such as libraries etc. Being consistent seems intuitive.

Beyon that, I looked at the compiler flags used by Arduino IDE vs CLion, and applied the missing flags to the ArduinoToolchain.cmake:

set(CMAKE_AR "gcc-ar")
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH true)
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_FINISH true)
set(ARDUINO_CXX_FLAGS "-g -Os -w -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -MMD")

which further improved compiled size by a lot. These too, I think, should be available out of the box in the cmake configuration.

The generated binary size is still not as good as what the Arduino IDE generates, but it's getting close. I'm not sure what else needs to be configured to get the same output size.

In any case, please apply the above or whatever else helps so that compilation out of the box is as optimized as what the Arduino IDE does. Every byte counts :-)

amichair avatar Apr 23 '20 15:04 amichair