Skip to content

Commit

Permalink
Version 1.3.11 (#14)
Browse files Browse the repository at this point in the history
* Added --elf-template option
Added esp-idf example

* Hope to fix windows compile error

* clang and catch2 don't like each other

* fix typo

* stupid me
  • Loading branch information
mhekkel authored Feb 1, 2024
1 parent aff8ce4 commit 41729ff
Show file tree
Hide file tree
Showing 16 changed files with 793 additions and 185 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
# compilation error with clang/catch2 forced me to comment clang out
# c_compiler: [gcc, clang, cl]
c_compiler: [gcc, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
# - os: ubuntu-latest
# c_compiler: clang
# cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
# - os: windows-latest
# c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
.vscode/
mrc
mrc-unit-test
mrc-bootstrap
build/
**/build/
src/revision.hpp
src/version.hpp
**/sdkconfig*
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ project(mrc VERSION 1.3.11)

include(CTest)

set(CMAKE_CXX_STANDARD 17)
set(CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Filesystem REQUIRED)

if(MSVC)
# make msvc standards compliant...
add_compile_options(/permissive-)
Expand Down Expand Up @@ -87,8 +86,8 @@ add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/mrc-rsrc.obj
add_executable(mrc ${PROJECT_SOURCE_DIR}/src/mrc.cpp ${CMAKE_BINARY_DIR}/mrc-rsrc.obj)
add_executable(mrc-bootstrap ${PROJECT_SOURCE_DIR}/src/mrc.cpp ${PROJECT_SOURCE_DIR}/src/dummy.cpp)

target_link_libraries(mrc std::filesystem libmcfp::libmcfp)
target_link_libraries(mrc-bootstrap std::filesystem libmcfp::libmcfp)
target_link_libraries(mrc libmcfp::libmcfp)
target_link_libraries(mrc-bootstrap libmcfp::libmcfp)

if(BUILD_TESTING)
find_package(Catch2 3 QUIET)
Expand All @@ -99,7 +98,7 @@ if(BUILD_TESTING)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
GIT_TAG v3.5.2 # or a later release
)

FetchContent_MakeAvailable(Catch2)
Expand Down
7 changes: 7 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
Version 1.3.11
- Added option --elf-template that can use an object
file or executable in ELF format as template to find
the correct --elf-* flags to use.
This option is used automatically by mrc when you
use it via the cmake plugin.
- Remove libmcfp as submodule
- Moved from Boost.Test to Catch2 for unit testing
- Using FetchContent for dependencies
- Dropped FindFilesystem, assuming std::filesystem is
standard by now.

Version 1.3.10
- Check cmake version before trying to write dependency file commands
Expand Down
74 changes: 0 additions & 74 deletions cmake/FindFilesystem.cmake

This file was deleted.

35 changes: 33 additions & 2 deletions cmake/mrc-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ find_package_handle_standard_args(Mrc
REQUIRED_VARS MRC_EXECUTABLE
VERSION_VAR MRC_VERSION_STRING)

# internal, create an ELF template file based on the current compiler flags and all

function(_mrc_create_elf_template _target)
try_compile(BUILD_OK
SOURCE_FROM_CONTENT my_code.cpp [[
extern "C" int my_global_int = 42;
int main() { return 0; }
]]
NO_CACHE
NO_LOG
COPY_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc_template.exe"
)

if(NOT BUILD_OK)
message(FATAL_ERROR "Failed to create template executable")
endif()
endfunction()

#[=======================================================================[.rst:
.. command:: mrc_target_resources

Expand Down Expand Up @@ -118,11 +136,17 @@ find_package_handle_standard_args(Mrc
with their filename. Directories will be added recursively, if the
directory name ends with a slash character, the directory part of
the name will be stripped.

``CREATE_ELF_TEMPLATE``
Create a small executable to be used as template for the ELF object
file generation. This is executable is built using the same compiler
settings as the final project and thus contains the correct ELF
header from which mrc can copy the necessary information.
#]=======================================================================]

function(mrc_target_resources _target)

set(flags VERBOSE)
set(flags VERBOSE CREATE_ELF_TEMPLATE)
set(options COFF_TYPE RSRC_FILE DEPENDS_FILE)
set(sources RESOURCES)
cmake_parse_arguments(MRC_OPTION "${flags}" "${options}" "${sources}" ${ARGN})
Expand Down Expand Up @@ -175,9 +199,16 @@ function(mrc_target_resources _target)
list(APPEND MRC_OPTION_RESOURCES "--verbose")
endif()

if(CMAKE_CROSSCOMPILING OR ${MRC_OPTION_CREATE_ELF_TEMPLATE})
_mrc_create_elf_template(${_target})
list(APPEND MRC_OPTION_RESOURCES "--elf-template=${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc_template.exe")
endif()

cmake_policy(SET CMP0116 NEW)

# If we can use DEPFILE, use it.
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.21")
add_custom_target("mrc-depends-file_${_target}" ALL
add_custom_target("mrc-depends-file_${_target}" ALL
BYPRODUCTS ${RSRC_DEP_FILE}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} -d ${RSRC_DEP_FILE} ${MRC_OPTION_RESOURCES}
VERBATIM)
Expand Down
6 changes: 6 additions & 0 deletions examples/esp-idf-rsrc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my-rsrc-app)
101 changes: 101 additions & 0 deletions examples/esp-idf-rsrc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Using resources in ESP-IDF
==========================

It is very easy to add resources to esp-idf projects as is shown in this example.
Here we create a single resource called `hello.txt` and its content comes from
the file `hello.txt`. The header file is created and resources are added to
the main component using the cmake snippet:

```cmake
# locate and include the mrc macro's
find_package(Mrc)
# write a header file
mrc_write_header(${CMAKE_CURRENT_SOURCE_DIR}/mrsrc.hpp)
# Add one resource to the executable
mrc_target_resources(${COMPONENT_LIB} CREATE_ELF_TEMPLATE
RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hello.txt)
```

Now you can simply use that resource:

```c++
#include "esp_log.h"

#include "mrsrc.hpp"

const char TAG[] = "esp-rsrc";

extern "C" void app_main(void)
{
mrsrc::rsrc data("hello.txt");
if (data)
{
std::string s(data.data(), data.size());
ESP_LOGI(TAG, "Resource found: '%s'!", s.c_str());
}
else
ESP_LOGI(TAG, "The resource was not found");
}
```

Use `idf.py build flash monitor` and the output will be:

```console
ESP-ROM:esp32h2-20221101
Build:Nov 1 2022
rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x408460e0,len:0x17ac
load:0x4083cfd0,len:0xe88
load:0x4083efd0,len:0x2c94
entry 0x4083cfda
I (23) boot: ESP-IDF v5.3-dev-1353-gb3f7e2c8a4 2nd stage bootloader
I (24) boot: compile time Feb 1 2024 15:04:08
I (25) boot: chip revision: v0.1
I (28) boot.esp32h2: SPI Speed : 64MHz
I (33) boot.esp32h2: SPI Mode : DIO
I (38) boot.esp32h2: SPI Flash Size : 2MB
I (42) boot: Enabling RNG early entropy source...
I (48) boot: Partition Table:
I (51) boot: ## Label Usage Type ST Offset Length
I (59) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (66) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (74) boot: 2 factory factory app 00 00 00010000 00100000
I (81) boot: End of partition table
I (85) esp_image: segment 0: paddr=00010020 vaddr=42018020 size=0a0e8h ( 41192) map
I (107) esp_image: segment 1: paddr=0001a110 vaddr=40800000 size=05f08h ( 24328) load
I (116) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=1734ch ( 95052) map
I (146) esp_image: segment 3: paddr=00037374 vaddr=40805f08 size=03acch ( 15052) load
I (152) esp_image: segment 4: paddr=0003ae48 vaddr=408099e0 size=011f4h ( 4596) load
I (157) boot: Loaded app from partition at offset 0x10000
I (158) boot: Disabling RNG early entropy source...
I (174) cpu_start: Unicore app
W (183) clk: esp_perip_clk_init() has not been implemented yet
I (190) cpu_start: Pro cpu start user code
I (190) cpu_start: cpu freq: 96000000 Hz
I (191) heap_init: Initializing. RAM available for dynamic allocation:
I (195) heap_init: At 4080BDD0 len 000415B0 (261 KiB): RAM
I (201) heap_init: At 4084D380 len 00002B60 (10 KiB): RAM
I (209) spi_flash: detected chip: generic
I (212) spi_flash: flash io: dio
W (216) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (229) cpu_start: Application information:
I (234) cpu_start: Project name: my-rsrc-app
I (239) cpu_start: App version: v1.3.10-23-gaff8ce4-dirty
I (246) cpu_start: Compile time: Feb 1 2024 15:04:01
I (252) cpu_start: ELF file SHA256: 30e7d4161...
I (257) cpu_start: ESP-IDF: v5.3-dev-1353-gb3f7e2c8a4
I (264) cpu_start: Min chip rev: v0.0
I (269) cpu_start: Max chip rev: v0.99
I (273) cpu_start: Chip rev: v0.1
I (279) sleep: Configure to isolate all GPIO pins in sleep state
I (285) sleep: Enable automatic switching of GPIO sleep configuration
I (292) main_task: Started on CPU0
I (292) main_task: Calling app_main()
I (292) esp-rsrc: Resource found: 'Hello, world!
'!
I (302) main_task: Returned from app_main()
```
9 changes: 9 additions & 0 deletions examples/esp-idf-rsrc/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
find_package(Mrc)

idf_component_register(SRCS esp-rsrc-app.cpp
INCLUDE_DIRS ".")

mrc_write_header(${CMAKE_CURRENT_SOURCE_DIR}/mrsrc.hpp)

mrc_target_resources(${COMPONENT_LIB} CREATE_ELF_TEMPLATE
RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hello.txt)
45 changes: 45 additions & 0 deletions examples/esp-idf-rsrc/main/esp-rsrc-app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Maarten L. Hekkelman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "esp_log.h"

#include "mrsrc.hpp"

const char TAG[] = "esp-rsrc";

extern "C" void app_main(void)
{
mrsrc::rsrc data("hello.txt");
if (data)
{
std::string s(data.data(), data.size());
ESP_LOGI(TAG, "Resource found: '%s'!", s.c_str());
}
else
ESP_LOGI(TAG, "The resource was not found");

}

1 change: 1 addition & 0 deletions examples/esp-idf-rsrc/main/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!
Loading

0 comments on commit 41729ff

Please sign in to comment.