Skip to content

Commit

Permalink
#3 - Use common project CMake template for test projects. Note: requi…
Browse files Browse the repository at this point in the history
…res changes in ESP-IDF from PR: espressif/esp-idf#2601
  • Loading branch information
PerMalmberg committed Oct 23, 2018
1 parent d03b093 commit f114438
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 19 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
# the one in the lib/smooth-folder instead.

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)

set(ACTIVE_TEST hello_world)
# Select the test project, then build target "main"

set(CURRENT_PROJECT_NAME logging)

set(CURRENT_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}/test/${CURRENT_PROJECT_NAME})

if (${ESP_PLATFORM})
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(EXTRA_COMPONENT_DIRS test lib)
set(COMPONENT_REQUIRES ${ACTIVE_TEST})
set(COMPONENT_REQUIRES ${CURRENT_PROJECT_NAME})
add_definitions(-D_GLIBCXX_USE_C99) # for std::to_string (until using xtensa-gcc 8)
project(smooth-test)
else ()
add_subdirectory(lib/smooth)
add_subdirectory(test/hello_world)
add_subdirectory(test/logging)
add_subdirectory(test/main)
endif ()
6 changes: 4 additions & 2 deletions lib/smooth/include/smooth/core/logging/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,16 @@ namespace smooth


/// Class to log a uint32 in hex format
template<typename T>
class Hex
: public BaseArgWithData
{
public:
explicit Hex(uint32_t value)
explicit Hex(const T&& value, bool show_base = false)
{
std::stringstream ss;
ss << std::hex << value;
// We cast to an uint64_t to get the formatting we want
ss << std::hex << (show_base ? std::showbase : std::noshowbase) << static_cast<uint64_t>(value);
data = ss.str();
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/common_project.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
get_filename_component(PROJECT ${CMAKE_CURRENT_SOURCE_DIR} NAME)

message(STATUS "Building: ${PROJECT}")

set(SOURCES
${PROJECT}.cpp
${PROJECT}.h)

if (${ESP_PLATFORM})
set(COMPONENT_SRCS ${SOURCES})
set(COMPONENT_REQUIRES smooth)
set(COMPONENT_ADD_INCLUDEDIRS ${COMPONENT_PATH})
register_component()
else ()
project(${PROJECT})
add_library(${PROJECT} ${SOURCES})
target_link_libraries(${PROJECT} smooth)
target_include_directories(${PROJECT} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
endif ()
12 changes: 1 addition & 11 deletions test/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
if (${ESP_PLATFORM})
set(COMPONENT_SRCS hello_world.cpp hello_world.h)
set(COMPONENT_REQUIRES smooth)
set(COMPONENT_ADD_INCLUDEDIRS ${COMPONENT_PATH})
register_component()
else ()
project(hello_world)
add_library(${PROJECT_NAME} hello_world.cpp hello_world.h)
target_link_libraries(${PROJECT_NAME} smooth)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
endif ()
include(${CMAKE_CURRENT_LIST_DIR}/../common_project.cmake)
1 change: 1 addition & 0 deletions test/logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/../common_project.cmake)
38 changes: 38 additions & 0 deletions test/logging/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by permal on 2018-10-21.
//

#include <limits>

#include "logging.h"
#include <smooth/core/logging/log.h>

using namespace std;
using namespace smooth::core::logging;

namespace logging
{
App::App()
: Application(smooth::core::APPLICATION_BASE_PRIO, std::chrono::seconds(1))
{
}

void App::init()
{

}

void App::tick()
{

Log::info("Tag", Format("Log a 32-bit int: {1}", Int32(numeric_limits<int32_t>::min())));
Log::info("Tag", Format("Log a 64-bit int: {1}", Int64(numeric_limits<int64_t>::max())));
Log::info("Tag", Format("Log {1} {2} {3} {4} {5}",
Str("multiple"),
Str("hex values of different types:"),
Hex<uint32_t>(numeric_limits<uint32_t>::max()),
Hex<uint64_t>(numeric_limits<uint64_t>::max()),
Hex<uint8_t>(numeric_limits<uint8_t>::max(), true))
);
}
}
17 changes: 17 additions & 0 deletions test/logging/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <smooth/core/Application.h>
#include <smooth/core/task_priorities.h>
#include <iostream>

namespace logging
{
class App : public smooth::core::Application
{
public:
App();

void init() override;
void tick() override;
};
}
2 changes: 1 addition & 1 deletion test/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ if (${ESP_PLATFORM})
else ()
project(main)
add_executable(${PROJECT_NAME} generated_test.cpp)
target_link_libraries(${PROJECT_NAME} ${ACTIVE_TEST})
target_link_libraries(${PROJECT_NAME} ${CURRENT_PROJECT_NAME})
endif ()
6 changes: 3 additions & 3 deletions test/main/test.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
// Created by permal on 2018-10-21.
//

#include <${ACTIVE_TEST}.h>
#include <${CURRENT_PROJECT_NAME}.h>

extern "C"
{
#ifdef ESP_PLATFORM
void app_main()
{
${ACTIVE_TEST}::App app;
${CURRENT_PROJECT_NAME}::App app;
app.start();
}
#else
int main(int argc, char** argv)
{
${ACTIVE_TEST}::App app;
${CURRENT_PROJECT_NAME}::App app;
app.start();
return 0;
}
Expand Down

0 comments on commit f114438

Please sign in to comment.