CMake为AVR构建,但在主机上运行测试

时间:2019-01-18 14:03:56

标签: c++ unit-testing cmake avr

我是CMake的新手,正在尝试将现有的多目标多体系结构make project转换为CMake。基本上,我正在构建AVR的固件映像并将其刷新到目标设备上,但是我想在刷新固件映像之前在主机上构建并运行单元测试,特别是我希望构建固件取决于后续的单元测试如果可能的话。

请注意,这与在32/64 configurations中编译相同代码不同,后者似乎是通过使用环境变量告诉cmake从哪个目标进行构建来进行树外构建来解决的。因为我需要同时使用两个拱门,而又不用在它们之间重新运行cmake了。

主体source structure如下:

├── CMakeLists.txt
├── src
│   └── Bar.cpp
├── lib
│   └── external/arch/foo.cpp
└── test
    └── BarTest.cpp

我需要“ src”中的所有文件以及“ lib / external / avr /”中的一些文件才能使用avr-g ++构建我的AVR目标。

要构建在主机(x86(-64))上运行的测试,我需要“ test”中的所有文件以及“ src”(被测代码)中的某些文件,以及“ lib / external /”中的某些文件x86 /”,并且我需要在主机编译器上构建(不管它是gcc还是clang),并带有稍微不同的标志。在上面的我的make文件中,这很复杂,因为同一文件需要为不同体系结构构建的多个目标文件,因此它们需要不同的输出目录。

最后,我希望能够make test运行主机测试,并make upload编译和编程目标设备(仅在测试通过的情况下)。

我目前在CMakeLists.txt中拥有什么:

cmake_minimum_required(VERSION 2.8)

set(AVRCPP   avr-g++)
set(AVRSTRIP avr-strip)
set(OBJCOPY  avr-objcopy)
set(OBJDUMP  avr-objdump)
set(AVRSIZE  avr-size)
set(AVRDUDE  avrdude)

set(CXXSTANDARD "-std=c++14")
set(COPT      "-O3")
set(CWARN     "-Werror -Wall -Wextra -pedantic -Wno-attributes")
set(CTUNING   "-fno-threadsafe-statics -fpack-struct -fshort-enums -fno-stack-protector")

set(BASE_AVR_CXXFLAGS "${CXXSTANDARD} ${COPT} ${CWARN} ${CTUNING}")

set(CMAKE_SYSTEM_NAME  Generic)
set(CMAKE_CXX_COMPILER ${AVRCPP})

#
# Probe project
#
project(probe CXX)

set(MCU "atmega328p")
set(CMCU "-mmcu=${MCU} -DF_CPU=16000000UL")
set(CDEFS "-DUART_BAUD=9600 -DUART_DATA_BITS=8 -DUART_PARITY_BITS=0 -DUART_STOP_BITS=1")
set(CMAKE_CXX_FLAGS "${BASE_AVR_CXXFLAGS} ${CMCU} ${DEFS}")

set(BASE_PATH    "${${PROJECT_NAME}_SOURCE_DIR}")
set(INC_PATH     "${BASE_PATH}/include")
set(SRC_PATH     "${BASE_PATH}/src")
set(LIB_DIR_PATH "${BASE_PATH}/lib")
set(XTD_SRC_PATH "${BASE_PATH}/xtd_uc/src")

include_directories(${INC_PATH} "xtd_uc/include")
file(GLOB SRC_FILES "${SRC_PATH}/*.cpp")
set(XTD_SRC_FILES 
  ${XTD_SRC_PATH}/delay.cpp
  ${XTD_SRC_PATH}/atmega/adc.cpp
  ${XTD_SRC_PATH}/atmega/bootstrap.cpp
  ${XTD_SRC_PATH}/atmega/chrono.cpp
  ${XTD_SRC_PATH}/atmega/gpio.cpp
  ${XTD_SRC_PATH}/atmega/sleep.cpp
  ${XTD_SRC_PATH}/atmega/uart.cpp
  ${XTD_SRC_PATH}/atmega/wdt.cpp
  )
add_executable(${PROJECT_NAME} ${SRC_FILES} ${XTD_SRC_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}.elf")

# Compiling targets
add_custom_target(strip ALL     ${AVRSTRIP} "${PROJECT_NAME}.elf" DEPENDS ${PROJECT_NAME})
add_custom_target(hex   ALL     ${OBJCOPY} -R .eeprom -O ihex "${PROJECT_NAME}.elf" "${PROJECT_NAME}.hex" DEPENDS strip)
add_custom_target(eeprom        ${OBJCOPY} -j .eeprom --change-section-lma .eeprom=0 -O ihex "${PROJECT_NAME}.elf" "${PROJECT_NAME}.eeprom" DEPENDS strip)
add_custom_target(disassemble   ${OBJDUMP} -S "${PROJECT_NAME}.elf" > "${PROJECT_NAME}.lst" DEPENDS strip)

# Flashing targets
set(PROG_TYPE_ARDUINO "-v -c arduino -b 115200 -P /dev/ttyUSB0")
set(PROG_TYPE_AVRISP "-v -c avrisp -b 19200 -P /dev/ttyACM0")
set(PROG_TYPE ${PROG_TYPE_ARDUINO})

add_custom_target(flash_program ${AVRDUDE} ${PROG_TYPE} -p ${MCU} -U flash:w:${PROJECT_NAME}.hex DEPENDS hex)
add_custom_target(flash_eeprom ${AVRDUDE} ${PROG_TYPE} -p ${MCU} -U eeprom:w:${PROJECT_NAME}.hex DEPENDS eeprom)

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.hex;${PROJECT_NAME}.eeprom;${PROJECT_NAME}.lst")

message("* ")
message("* Project Name:\t${PROJECT_NAME}")
message("* Project Source:\t${SRC_PATH}")
message("* Project Include:\t${INC_PATH}")
message("* Library Include:\t${LIB_INC_PATH}")
message("* ")
message("* Project Source Files:\t${SRC_FILES}")
message("* XTD Source Files:\t${XTD_SRC_FILES}")
message("* ")
message("* CXX Flags:\t${CMAKE_CXX_FLAGS}")
message("* ")

它可以用于构建AVR目标,但是我对于如何将测试与其他编译器/目标架构集成在一起感到困惑。

对于我如何使用cmake实现上述目标的任何帮助,将不胜感激。

0 个答案:

没有答案