Skip to content

Commit

Permalink
[nRF Connect] Run some unit tests on Zephyr's native_posix platform (p…
Browse files Browse the repository at this point in the history
…roject-chip#1997)

* Make several tests more portable

* Implement test runner for Zephyr native_posix

* Put more unit tests in anonymous namespace

Some tests were failing because linker selected incorrect
implementation for TestContext c-tor when running the whole
test directory at once. It's safer to hide the symbols so
they don't collide with each other.

* Workaround for Zephyr not to run tests twice.

* Run more unit tests for Zephyr native_posix platform

It required adapting mbedtls configuration and solving
several issues with unit tests being linked into
a single executable.

* Execute tests of ConfigurationManager and clean a few things up

* Restyled by whitespace

* Restyled by shfmt

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
Damian-Nordic and restyled-commits authored Aug 7, 2020
1 parent 73d47e4 commit f12132c
Show file tree
Hide file tree
Showing 28 changed files with 722 additions and 210 deletions.
166 changes: 166 additions & 0 deletions config/nrfconnect/chip-lib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# @file
# CMake module for configuring and building CHIP libraries to be used
# in Zephyr applications.
#
# It is assumed that at this point:
# - CHIP_ROOT is defined
# - find_package(Zephyr) has been called
#

# ==================================================
# Helpers & settings
# ==================================================

include(ExternalProject)

# Directory for CHIP build artifacts
set(CHIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/chip")

# Helper macros to conditionally append a list
macro(append_if VAR CONDITION)
if (${${CONDITION}})
list(APPEND ${VAR} ${ARGN})
endif()
endmacro()

macro(append_if_not VAR CONDITION)
if (${${CONDITION}})
else()
list(APPEND ${VAR} ${ARGN})
endif()
endmacro()

# Function to retrieve Zephyr compilation flags for the given language (C or CXX)
function(zephyr_get_compile_flags VAR LANG)
zephyr_get_include_directories_for_lang(${LANG} INCLUDES)
zephyr_get_system_include_directories_for_lang(${LANG} SYSTEM_INCLUDES)
zephyr_get_compile_definitions_for_lang(${LANG} DEFINES)
zephyr_get_compile_options_for_lang(${LANG} FLAGS)
set(${VAR} ${INCLUDES} ${SYSTEM_INCLUDES} ${DEFINES} ${FLAGS} ${${VAR}} PARENT_SCOPE)
endfunction()

# ==================================================
# Define chip configuration target
# ==================================================

function(chip_configure TARGET_NAME)
cmake_parse_arguments(CHIP
"BUILD_TESTS"
"ARCH;PROJECT_CONFIG"
"CFLAGS;CXXFLAGS"
${ARGN})

# Prepare CFLAGS & CXXFLAGS
zephyr_get_compile_flags(CHIP_CFLAGS C)
convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS)

zephyr_get_compile_flags(CHIP_CXXFLAGS CXX)
list(FILTER CHIP_CXXFLAGS EXCLUDE REGEX -std.*) # CHIP adds gnu++11 anyway...
convert_list_of_flags_to_string_of_flags(CHIP_CXXFLAGS CHIP_CXXFLAGS)

# Prepare command line arguments passed to the CHIP's ./configure script
set(CHIP_CONFIGURE_ARGS
AR=${CMAKE_AR}
AS=${CMAKE_AS}
CC=${CMAKE_C_COMPILER}
CXX=${CMAKE_CXX_COMPILER}
LD=${CMAKE_LINKER}
OBJCOPY=${CMAKE_OBJCOPY}
RANLIB=${CMAKE_RANLIB}
CFLAGS=${CHIP_CFLAGS}
CXXFLAGS=${CHIP_CXXFLAGS}
--prefix=${CHIP_OUTPUT_DIR}
--exec-prefix=${CHIP_OUTPUT_DIR}
--host=${CHIP_ARCH}
--with-device-layer=nrfconnect
--with-network-layer=all
--with-target-network=sockets
--with-inet-endpoint=udp
--with-logging-style=external
--with-target-style=embedded
--with-chip-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-system-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-inet-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-ble-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-warm-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-device-project-includes=${CHIP_PROJECT_CONFIG}
--enable-debug
--enable-optimization=no
--disable-tools
--disable-docs
--disable-java
--disable-device-manager
--with-mbedtls=${ZEPHYR_BASE}/../modules/crypto/mbedtls
--with-crypto=mbedtls
)

append_if(CHIP_CONFIGURE_ARGS CONFIG_NET_L2_OPENTHREAD --with-openthread=${ZEPHYR_BASE}/../modules/lib/openthread)
append_if_not(CHIP_CONFIGURE_ARGS CHIP_BUILD_TESTS --disable-tests)
append_if_not(CHIP_CONFIGURE_ARGS CONFIG_NET_IPV4 --disable-ipv4)

# Define target
ExternalProject_Add(
${TARGET_NAME}
PREFIX ${CHIP_OUTPUT_DIR}
SOURCE_DIR ${CHIP_ROOT}
BINARY_DIR ${CHIP_OUTPUT_DIR}
CONFIGURE_COMMAND ${CHIP_ROOT}/configure ${CHIP_CONFIGURE_ARGS}
BUILD_COMMAND ""
INSTALL_COMMAND ""
BUILD_ALWAYS TRUE
)
endfunction()

# ==================================================
# Define chip build target
# ==================================================

function(chip_build TARGET_NAME BASE_TARGET_NAME)
cmake_parse_arguments(CHIP "" "" "BUILD_COMMAND;BUILD_ARTIFACTS" ${ARGN})

# Define build target
ExternalProject_Add(
${TARGET_NAME}Build
PREFIX ${CHIP_OUTPUT_DIR}
SOURCE_DIR ${CHIP_ROOT}
BINARY_DIR ${CHIP_OUTPUT_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ${CHIP_BUILD_COMMAND}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${CHIP_BUILD_ARTIFACTS}
BUILD_ALWAYS TRUE
)

# Define interface library containing desired CHIP byproducts
add_library(${TARGET_NAME} INTERFACE)
target_include_directories(${TARGET_NAME} INTERFACE
${CHIP_ROOT}/src
${CHIP_ROOT}/src/lib
${CHIP_ROOT}/src/lib/core
${CHIP_ROOT}/src/include
${CHIP_OUTPUT_DIR}/include
${CHIP_OUTPUT_DIR}/src/include
)
target_link_directories(${TARGET_NAME} INTERFACE ${CHIP_OUTPUT_DIR}/lib)
target_link_libraries(${TARGET_NAME} INTERFACE -Wl,--start-group ${CHIP_BUILD_ARTIFACTS} -Wl,--end-group)

add_dependencies(${TARGET_NAME}Build ${BASE_TARGET_NAME})
add_dependencies(${TARGET_NAME} ${TARGET_NAME}Build)
endfunction()
116 changes: 16 additions & 100 deletions config/nrfconnect/nrfconnect-app.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,12 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
# General settings
# ==================================================

# Archtecture for which CHIP will be built.
set(CHIP_HOST_ARCH arm-none-eabi)
include(chip-lib)

# Directory into which the CHIP build system will place its output.
set(CHIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/chip")

# An optional file containing application-specific configuration overrides.
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/CHIPProjectConfig.h")
set(CHIP_PROJECT_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/CHIPProjectConfig.h")
else()
set(CHIP_PROJECT_CONFIG "")
endif()

zephyr_include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/main/include
${CHIP_ROOT}/src
${CHIP_ROOT}/src/lib
${CHIP_ROOT}/src/lib/core
${CHIP_ROOT}/src/include
${CHIP_OUTPUT_DIR}/include
${CHIP_OUTPUT_DIR}/src/include
set(CHIP_COMMON_FLAGS
-D_SYS__PTHREADTYPES_H_
-isystem${ZEPHYR_BASE}/include/posix
-isystem${ZEPHYR_BASE}/../modules/crypto/mbedtls/configs
)

set(CHIP_OUTPUT_LIBRARIES
Expand All @@ -81,95 +66,26 @@ set(CHIP_OUTPUT_LIBRARIES
${CHIP_OUTPUT_DIR}/lib/libBleLayer.a
${CHIP_OUTPUT_DIR}/lib/libDeviceLayer.a
${CHIP_OUTPUT_DIR}/lib/libCHIPDataModel.a
)
)

# ==================================================
# Setup CHIP build
# ==================================================

# Function to retrieve Zephyr compilation flags for the given language (C or CXX)
function(get_zephyr_compilation_flags var lang)
zephyr_get_include_directories_for_lang(${lang} INCLUDES)
zephyr_get_system_include_directories_for_lang(${lang} SYSTEM_INCLUDES)
zephyr_get_compile_definitions_for_lang(${lang} DEFINES)
zephyr_get_compile_options_for_lang(${lang} FLAGS)
set(${var} ${INCLUDES} ${SYSTEM_INCLUDES} ${DEFINES} ${FLAGS} PARENT_SCOPE)
endfunction(get_zephyr_compilation_flags)

# Inherit Zephyr build settings and add some extra ones
get_zephyr_compilation_flags(CHIP_CFLAGS C)
get_zephyr_compilation_flags(CHIP_CXXFLAGS CXX)
list(FILTER CHIP_CXXFLAGS EXCLUDE REGEX -std.*) # CHIP adds gnu++11 anyway...

set(CHIP_COMMON_FLAGS
-D_SYS__PTHREADTYPES_H_
-isystem${ZEPHYR_BASE}/include/posix
-isystem${ZEPHYR_BASE}/../modules/crypto/mbedtls/configs)

set(CHIP_CFLAGS ${CHIP_CFLAGS} ${CHIP_COMMON_FLAGS} --specs=nosys.specs)
set(CHIP_CXXFLAGS ${CHIP_CXXFLAGS} ${CHIP_COMMON_FLAGS})

convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS)
convert_list_of_flags_to_string_of_flags(CHIP_CXXFLAGS CHIP_CXXFLAGS)

# Prepare command line arguments passed to the CHIP's ./configure script
set(CHIP_CONFIGURE_ARGS
AR=${CMAKE_AR}
AS=${CMAKE_AS}
CC=${CMAKE_C_COMPILER}
CXX=${CMAKE_CXX_COMPILER}
LD=${CMAKE_LINKER}
OBJCOPY=${CMAKE_OBJCOPY}
RANLIB=${CMAKE_RANLIB}
CFLAGS=${CHIP_CFLAGS}
CXXFLAGS=${CHIP_CXXFLAGS}
--prefix=${CHIP_OUTPUT_DIR}
--exec-prefix=${CHIP_OUTPUT_DIR}
--host=${CHIP_HOST_ARCH}
--with-device-layer=nrfconnect
--with-network-layer=all
--with-target-network=sockets
--with-inet-endpoint=udp
--with-logging-style=external
--with-target-style=embedded
--with-chip-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-system-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-inet-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-ble-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-warm-project-includes=${CHIP_PROJECT_CONFIG}
--with-chip-device-project-includes=${CHIP_PROJECT_CONFIG}
--enable-debug
--enable-optimization=no
--disable-ipv4
--disable-tests
--disable-tools
--disable-docs
--disable-java
--disable-device-manager
--with-mbedtls=${ZEPHYR_BASE}/../modules/crypto/mbedtls
--with-crypto=mbedtls
)

if (${CONFIG_NET_L2_OPENTHREAD})
list(APPEND CHIP_CONFIGURE_ARGS --with-openthread=${ZEPHYR_BASE}/../modules/lib/openthread)
endif()
chip_configure(ChipConfig
ARCH arm-none-eabi
CFLAGS ${CHIP_COMMON_FLAGS} --specs=nosys.specs
CXXFLAGS ${CHIP_COMMON_FLAGS}
)

# Define CHIP as an external project (since CHIP doesn't support CMake natively)
include(ExternalProject)
ExternalProject_Add(
chip_project
PREFIX ${CHIP_OUTPUT_DIR}
SOURCE_DIR ${CHIP_ROOT}
BINARY_DIR ${CHIP_OUTPUT_DIR}
CONFIGURE_COMMAND ${CHIP_ROOT}/configure ${CHIP_CONFIGURE_ARGS}
BUILD_COMMAND make --no-print-directory all install V=${CMAKE_AUTOGEN_VERBOSE}
BUILD_BYPRODUCTS ${CHIP_OUTPUT_LIBRARIES}
BUILD_ALWAYS TRUE
chip_build(ChipLib ChipConfig
BUILD_COMMAND make --no-print-directory install V=${CMAKE_AUTOGEN_VERBOSE}
BUILD_ARTIFACTS ${CHIP_OUTPUT_LIBRARIES}
)

# ==================================================
# Configure application
# ==================================================

target_compile_definitions(app PRIVATE HAVE_CONFIG_H)
target_link_libraries(app PRIVATE -Wl,--start-group ${CHIP_OUTPUT_LIBRARIES} -Wl,--end-group)
add_dependencies(app chip_project)
target_link_libraries(app PUBLIC -Wl,--start-group ChipLib -Wl,--end-group)
30 changes: 30 additions & 0 deletions scripts/tests/nrfconnect_native_posix_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

#
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -x
[[ -n $ZEPHYR_BASE ]] && source "$ZEPHYR_BASE/zephyr-env.sh"
env

CHIP_ROOT="$(dirname "$0")/../.."

cd "$CHIP_ROOT" &&
./bootstrap &&
cd src/test_driver/nrfconnect &&
west build -b native_posix &&
cd build &&
timeout 5m ctest -V
2 changes: 1 addition & 1 deletion src/crypto/tests/CHIPCryptoPALTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ static void TestSPAKE2P_RFC(nlTestSuite * inSuite, void * inContext)

namespace chip {
namespace Logging {
void LogV(uint8_t module, uint8_t category, const char * format, va_list argptr)
void __attribute__((weak)) LogV(uint8_t module, uint8_t category, const char * format, va_list argptr)
{
(void) module, (void) category;
vfprintf(stderr, format, argptr);
Expand Down
Loading

0 comments on commit f12132c

Please sign in to comment.