arduino-cmake / Arduino-CMake-NG

CMake-Based framework for Arduino platforms
MIT License
138 stars 39 forks source link

C-files in Arduino libraries. Not enough compilation flags. #40

Closed kukuyok closed 6 years ago

kukuyok commented 6 years ago
OS: Linux
Distribution: Xubuntu
OS Version: 16.04
Platform: Arduino Mega
Platform SDK Version: 1.8.5
Arduino-CMake-NG: cbfebf9

CMake options:

-DCMAKE_TOOLCHAIN_FILE=/home/petro/install/arduino-cmake/cmake/Arduino-Toolchain.cmake -DARDUINO_SDK_PATH=/home/petro/install/arduino-1.8.5

I use arduino-timer-api library in my project, and its source-code is written in C, not in C++, as three libraries, which you use in arduino-library example. And there are some problems to build it. That's what I found:

  1. If not architectures set in library.properties library file, I have no -DARDUINO_ARCH_AVR definition for avr-gcc, if compile library C-files. If I specify avr for arch: architecutres=avr,pic32,sam this flag is in place.
  2. I try to convert C-files into C++. It was a perfect cure in my case. Here is the difference between compilation commands.

    Compiling C++

    /home/petro/install/arduino-1.8.5/hardware/tools/avr/bin/avr-g++
    -DARDUINO_ARCH_AVR
    -I/home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src
    -I/home/petro/install/arduino-1.8.5/hardware/arduino/avr/cores/arduino
    -I/home/petro/install/arduino-1.8.5/hardware/arduino/avr/variants/mega
    -c
    -g
    -Os
    -w
    -std=gnu++11
    -fpermissive
    -fno-exceptions
    -ffunction-sections
    -fdata-sections
    -fno-threadsafe-statics
    -MMD
    -flto
    -mmcu=atmega2560
    -DF_CPU=16000000L
    -DARDUINO=10805
    "-DARDUINO_AVR_MEGA2560
    "
    -o
    CMakeFiles/timer_api_lib.dir/home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src/avr/timer_setup.cpp.obj
    -c
    /home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src/avr/timer_setup.cpp

    Compiling C

    /home/petro/install/arduino-1.8.5/hardware/tools/avr/bin/avr-gcc
    -DARDUINO_ARCH_AVR
    -I/home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src
    -I/home/petro/install/arduino-1.8.5/hardware/arduino/avr/cores/arduino
    -I/home/petro/install/arduino-1.8.5/hardware/arduino/avr/variants/mega
    -o
    CMakeFiles/timer_api_lib.dir/home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src/avr/timer_setup.c.obj
    -c
    /home/petro/install/arduino-1.8.5/libraries/Arduino-timer-api/src/avr/timer_setup.c
  3. If I just add missing flags to my CMakeLists.txt, arduino-timer-api library and the whole project also builds successfully (may be not totally correctly):
    add_definitions(-DARDUINO_ARCH_AVR -DARDUINO_AVR_MEGA2560 -DARDUINO=10802 -DF_CPU=16000000L)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmcu=atmega2560")

SuperThnx for your tool!

MrPointer commented 6 years ago

@kukuyok Thanks for the feedback and contribution! I'll review this sometime soon, it seems really really strange that you get such behavior. BTW, could you attach your CMakeLists.txt file please?

kukuyok commented 6 years ago

Thanks! Sure:

cmake_minimum_required(VERSION 3.8)

set(project_name Cocktail)

## Команда для запуска CMake (run CMake in bash)
## cmake -DCMAKE_TOOLCHAIN_FILE=/home/petro/install/arduino-cmake/cmake/Arduino-Toolchain.cmake -DARDUINO_SDK_PATH=/home/petro/install/arduino-1.8.5 -Darduino_port=/dev/ttyUSB0 ..

## Команда для генерации файлов проекта Eclipse (Eclipse project files generator)
## cmake -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-j8 -DCMAKE_TOOLCHAIN_FILE=/home/petro/install/arduino-cmake/cmake/Arduino-Toolchain.cmake -DARDUINO_SDK_PATH=/home/petro/install/arduino-1.8.5 -Darduino_port=/dev/ttyUSB0 ../firmware-ecl

set(CMAKE_VERBOSE_MAKEFILE ON)

## Флаги, компенсирующие косяк в сборке C-файлов Arduino-CMake-NG (temporary cure)
#add_definitions(-DARDUINO_ARCH_AVR -DARDUINO_AVR_MEGA2560 -DARDUINO=10802 -DF_CPU=16000000L)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmcu=atmega2560")

#set(ARDUINO_CMAKE_SKETCHBOOK_PATH /home/petro/Arduino)

project(${project_name} LANGUAGES C CXX ASM)

set(arduino_board mega)
set(board_cpu atmega2560)
get_board_id(board_id ${arduino_board} ${board_cpu})

add_arduino_executable(${project_name} ${board_id} main.cpp)

find_arduino_library(timer_api_lib arduino-timer-api ${board_id})
link_arduino_library(${project_name} timer_api_lib ${board_id})

set(arduino_port /dev/ttyUSB0)
upload_arduino_target(${project_name} ${board_id} ${arduino_port})

I build it in two ways: with Eclipse project (outside project directory) and directly using bash (within project subdirectory) and have same result.

kukuyok commented 6 years ago

+1 more notice: I need to rename library directory from arduino-timer-api to Arduino-timer-api. Otherwise library can't be found.