最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to reliably determine that current CMake compiler is the avr compiler? - Stack Overflow

programmeradmin1浏览0评论

I could of course be lazy and determine it by path alone. But say I want a more reliable approach, like:

if(IS_AVR)
   add_compile_options(
        --arduino-avr-specific-option
    )
endif()

CMake does not support this natively, like it does with detecting windows/ios/linux. What I am doing currently is the inverse approach:

# If CMAKE_AVR_ROOT is set, I assume we compile for arduino/atmel and set the flags accordingly
if(CMAKE_AVR_ROOT)
    set(CMAKE_SYSTEM_NAME  Generic)
    # another problem with this approach is you get .exe on windows, but not linux
    # so this won't work on linux without an additional IF
    set(CMAKE_C_COMPILER ${CMAKE_AVR_BIN}/avr-gcc.exe)
    set(CMAKE_CXX_COMPILER ${CMAKE_AVR_BIN}/avr-g++.exe)
endif()

I would prefer if the CMake was set up to detect avr-gcc.exe somehow from CMAKE_C_COMPILER. Then, I'd set IS_AVR and all my conditional flags/includes would be under if(IS_AVR). This would make the CMake work with other tools that set compiler automatically.

What is an elegant way of detecting AVR target of compilation by compiler alone?

I could of course be lazy and determine it by path alone. But say I want a more reliable approach, like:

if(IS_AVR)
   add_compile_options(
        --arduino-avr-specific-option
    )
endif()

CMake does not support this natively, like it does with detecting windows/ios/linux. What I am doing currently is the inverse approach:

# If CMAKE_AVR_ROOT is set, I assume we compile for arduino/atmel and set the flags accordingly
if(CMAKE_AVR_ROOT)
    set(CMAKE_SYSTEM_NAME  Generic)
    # another problem with this approach is you get .exe on windows, but not linux
    # so this won't work on linux without an additional IF
    set(CMAKE_C_COMPILER ${CMAKE_AVR_BIN}/avr-gcc.exe)
    set(CMAKE_CXX_COMPILER ${CMAKE_AVR_BIN}/avr-g++.exe)
endif()

I would prefer if the CMake was set up to detect avr-gcc.exe somehow from CMAKE_C_COMPILER. Then, I'd set IS_AVR and all my conditional flags/includes would be under if(IS_AVR). This would make the CMake work with other tools that set compiler automatically.

What is an elegant way of detecting AVR target of compilation by compiler alone?

Share Improve this question edited Mar 29 at 9:44 Tomáš Zato asked Mar 28 at 17:46 Tomáš ZatoTomáš Zato 53.5k63 gold badges310 silver badges827 bronze badges 3
  • Would cmake's find_* commands help? What about find_program avr-gcc? – Craig Estey Commented Mar 29 at 2:17
  • @CraigEstey I edited the question. What I really want is to detect that user set compiler to the avr compiler. If they did, I enable all arduino specific behavior. And in future, I'd then also throw cmake errors if arduino specific configs are mising. – Tomáš Zato Commented Mar 29 at 9:45
  • Hmm ... Still not totally sure what your needs/desires are. So, I'll just take another guess [partly because I'm not super familiar with cmake). If user does env CMAKE_C_COMPILER=avr-gcc cmake .... Or, in the file: find_program(CMAKE_C_COMPILER NAMES gcc clang cc), you want to know if CMAKE_C_COMPILER contains avr-gcc in some form? That would be a string operation? string(FIND ${CMAKE_C_COMPILER} avr-gcc IS_AVR_CC), string(LENGTH ${IS_AVR_CC} IS_AVR (Or, using the REGEX subcommand). – Craig Estey Commented Mar 30 at 0:13
Add a comment  | 

1 Answer 1

Reset to default 0

You can use check_c_compiler_flag.

For example:


check_c_compiler_flag(-mmcu=atmega328p IS_AVR_COMPILER)
if(NOT IS_AVR_COMPILER)
    message(FATAL_ERROR "Detected non avr gcc")
endif()

https://cmake./cmake/help/latest/module/CheckCCompilerFlag.html

发布评论

评论列表(0)

  1. 暂无评论