cmake: integrating library for state machine (hsmcpp) with esp-idf needs FreeRTOS CMakeFiles.txt

sebams
Posts: 4
Joined: Fri Mar 03, 2023 7:51 pm

cmake: integrating library for state machine (hsmcpp) with esp-idf needs FreeRTOS CMakeFiles.txt

Postby sebams » Tue Apr 11, 2023 10:18 pm

Hi, I'm trying to integrate hsmcpp library (https://github.com/igor-krechetov/hsmcpp) to my existing project.

This library allows code generation of hierarchical state machines and FSM from statechart diagrams and has a dispatcher for FreeRTOS.

They provide an example https://github.com/igor-krechetov/hsmcp ... 8_freertos to use it with FreeRTOS, that example requires modifying their CMakeLists.txt so it can point to FreeRTOS path (details: https://hsmcpp.readthedocs.io/en/latest ... erfreertos).

The entire CMakeLists.txt provided by them is here https://github.com/igor-krechetov/hsmcp ... eLists.txt the remarkable part is shown below:
  1. SET(FREERTOS_ROOT "full path to FreeRTOS directory")
  2. SET(FREERTOS_DIR "${FREERTOS_ROOT}/FreeRTOS")
  3. SET(FREERTOS_PLUS_DIR "${FREERTOS_ROOT}/FreeRTOS-Plus")
  4. SET(FREERTOS_KERNEL_DIR  "${FREERTOS_DIR}/Source")
  5. SET(FREERTOS_LIB_DIR "${CMAKE_BINARY_DIR}/freertos")
  6. SET(FREERTOS_PORT_LIB_DIR "${FREERTOS_LIB_DIR}/portable")
  7. SET(FREERTOS_LIBS freertos_kernel freertos_kernel_port pthread)
As requested I modified the FREERTOS_ROOT path (and also FREERTOS_KERNEL_DIR) so it points to esp-idf freertos component path($IDF_PATH/components/freertos), but I'm struggling with some errors there since the example provided is intended to use with unmodified FreeRTOS and the one provided with esp-idf has modifications to be added in the projects as a component.
Now it fails because CMakeFiles.txt inside FreeRTOS-Kernel folder is missing, the only CMakeFiles.txt file is present on $IDF_PATH/components/freertos, but this one has idf commands like idf_build_get_property() that can't be interpreted by cmake when calls this path using add_subdirectory.

Any hint to solve this?

As a second approach I'm considering to clone FreeRTOS and then add "by hand" the code generated by this example. But I'm not pretty sure if there could be some kind of version mismatch of FreeRTOS, could be messy and in a future I would like to generate the statechart code when building my esp project (modifying the esp project CMakeLists.txt and adding the generated code as a component), so the first approach would be the most appropriate.

Thanks.

MicroController
Posts: 1692
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: cmake: integrating library for state machine (hsmcpp) with esp-idf needs FreeRTOS CMakeFiles.txt

Postby MicroController » Wed Apr 12, 2023 7:17 am

Esp FreeRTOS is an Idf component , so instead of setting the FreeRTOS paths in the library you could try and make the library an Idf component too with a REQUIRES dependency to FreeRTOS and then build it via idf.

sebams
Posts: 4
Joined: Fri Mar 03, 2023 7:51 pm

Re: cmake: integrating library for state machine (hsmcpp) with esp-idf needs FreeRTOS CMakeFiles.txt

Postby sebams » Fri Apr 14, 2023 11:23 pm

Hi, thanks for answering.
Already tried that but didn't get it, there is some trouble that makes pthreads unreachable for the linking process, the same that is described at the end of this post.

I've found this in esp-idf doc: Using Third-Party CMake Projects with Components

Considering that, and using as template the example provided to add tinyxml2, I arrived to this CMakeLists.txt that I added in a folder as an esp-idf component (I left the comments from the tinyxml2 example as reference):
  1. # This component demonstrates how to add an existing third-party library as a component
  2. # to ESP-IDF build system.
  3. #
  4. # Since we are wrapping the library inside a component,
  5. # the component has to be registered first:
  6. idf_component_register()
  7.  
  8. # To build a third-party library, ExternalProject CMake module can be used.
  9. # ExternalProject offers many features which are impossible to demonstrate
  10. # in a single example. Please refer to its documentation for more info:
  11. #   https://cmake.org/cmake/help/latest/module/ExternalProject.html
  12. include(ExternalProject)
  13.  
  14. # Define the location where hsmcpp will be installed:
  15. set(HSMCPP_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/hsmcpp_install)
  16.  
  17. # This function downloads the project, calls CMake to configure it,
  18. # builds the project and installs it to the specified location:
  19. externalproject_add(hsmcpp_proj
  20.     # Download the source code of the third party project from the following URL.
  21.     URL https://github.com/igor-krechetov/hsmcpp/archive/refs/tags/0.33.0.zip
  22.     # (Downloading is not the only option; the library can also be located in your source tree.
  23.     #  Consult ExternalProject_Add function documentation for other options.)
  24.  
  25.     # Specify arguments to be passed when running CMake for this subproject.
  26.     # Note that ExternalProject_Add also works with non-CMake projects, so this
  27.     # is just an example.
  28.     CMAKE_ARGS
  29.         # Use the same CMake toolchain file as for the main project.
  30.         -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
  31.         # hsmcpp-specific settings (taken from build.sh inside hsmcpp freertos example)
  32.         -DHSMBUILD_VERBOSE=OFF
  33.         -DHSMBUILD_EXAMPLES=OFF
  34.         -DHSMBUILD_TESTS=OFF
  35.         -DHSMBUILD_DISPATCHER_GLIB=OFF
  36.         -DHSMBUILD_DISPATCHER_GLIBMM=OFF
  37.         -DHSMBUILD_DISPATCHER_QT=OFF
  38.         -DHSMBUILD_DISPATCHER_STD=OFF
  39.         -DHSMBUILD_PLATFORM=FREERTOS
  40. #        -DHSMBUILD_FREERTOS_ROOT "${FREERTOS_ROOT}" CACHE STRING "")
  41.         -DHSMBUILD_DISPATCHER_FREERTOS=ON
  42.         -DHSMBUILD_FREERTOS_DEFAULT_ISR_DETECT=ON
  43.         # Pass the install directory to the subproject.
  44.         -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
  45.  
  46.  
  47.     # These options are set so that Ninja immediately outputs
  48.     # the subproject build to the terminal. Otherwise it looks like the
  49.     # build process "hangs" while the subproject is being built.
  50.     USES_TERMINAL_DOWNLOAD TRUE
  51.     USES_TERMINAL_CONFIGURE TRUE
  52.     USES_TERMINAL_BUILD TRUE
  53.  
  54.     # Specify the installation directory for the subproject
  55.     INSTALL_DIR ${HSMCPP_INSTALL_DIR}
  56.     # Let CMake know that the library is generated by the subproject build step.
  57.     BUILD_BYPRODUCTS "${HSMCPP_INSTALL_DIR}/lib/libhsmcpp.a"
  58. )
  59.  
  60. # Now that the subproject build is set up, we need to consume the results
  61. # of the build: the header file and the static library.
  62. # To do this, define an imported CMake library:
  63. add_prebuilt_library(hsmcpp_lib "${HSMCPP_INSTALL_DIR}/lib/libhsmcpp.a"
  64.                      # hsmcpp calls certain support library functions
  65.                      # so a dependency on IDF's freertos component is added here:
  66.                      PRIV_REQUIRES freertos pthread)
  67. target_include_directories(hsmcpp_lib INTERFACE "${HSMCPP_INSTALL_DIR}/include")
  68. add_dependencies(hsmcpp_lib hsmcpp_proj)
  69.  
  70. # Link the imported library to the current component.
  71. target_link_libraries(${COMPONENT_LIB} INTERFACE hsmcpp_lib)
  72.  
  73. # To use hsmcpp in another component, add 'hsmcpp' (the name of this component)
  74. # to PRIV_REQUIRES or REQUIRES list its idf_component_register call.
  75. # See ../../main/CMakeLists.txt for an example.
But then happens the same error thatThe error that I'm facing right now is provided below:

Code: Select all

--------------------------------------
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - no
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
CMake Error at /home/s/.espressif/tools/cmake/3.24.0/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
  /home/s/.espressif/tools/cmake/3.24.0/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /home/s/.espressif/tools/cmake/3.24.0/share/cmake-3.24/Modules/FindThreads.cmake:226 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:160 (find_package)


-- Configuring incomplete, errors occurred!
This happens even though I add pthread (PRIV_REQUIRES) on line 66 of CMakeLists.txt

Thanks again.

sebams
Posts: 4
Joined: Fri Mar 03, 2023 7:51 pm

Re: cmake: integrating library for state machine (hsmcpp) with esp-idf needs FreeRTOS CMakeFiles.txt

Postby sebams » Sun Apr 16, 2023 7:42 pm

Found that another user of this forum was facing with a similar problem with another third party library and pthread, link here

Who is online

Users browsing this forum: No registered users and 287 guests