0

I have a tiny library with its own cmake file. I am trying to add this library to the platformio ploject. I create a "CMakeListsUser.txt" and add this line "add_subdirectory(GLibc)" here and when I reload the cmake project I get an infinite loop.

I use cmake version 3.26.0-rc1

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

project("project" C CXX)

include(CMakeListsPrivate.txt)

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt)
include(CMakeListsUser.txt)
endif()

add_custom_target(
    Production ALL
    COMMAND platformio -c clion run "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_custom_target(
    Debug ALL
    COMMAND platformio -c clion debug "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_executable(Z_DUMMY_TARGET ${SRC_LIST})

CMakeListsUser.txt

add_subdirectory(GCLib)

SET(CMAKE_C_COMPILER "${ENV_HOME_PATH}/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-gcc.exe")
SET(CMAKE_CXX_COMPILER "${ENV_HOME_PATH}/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-g++.exe")
SET(CMAKE_CXX_FLAGS "-Wno-frame-address -std=gnu++11 -fexceptions -fno-rtti -mlongcalls -ffunction-sections -fdata-sections -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -ggdb -Os -freorder-blocks -Wwrite-strings -fstack-protector -fstrict-volatile-bitfields -Wno-error=unused-but-set-variable -fno-jump-tables -fno-tree-switch-conversion -MMD")
SET(CMAKE_C_FLAGS "-Wno-frame-address -std=gnu99 -Wno-old-style-declaration -mlongcalls -ffunction-sections -fdata-sections -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -ggdb -Os -freorder-blocks -Wwrite-strings -fstack-protector -fstrict-volatile-bitfields -Wno-error=unused-but-set-variable -fno-jump-tables -fno-tree-switch-conversion -MMD")

Library cmake file:

cmake_minimum_required(VERSION 3.24)

project("GCLib" VERSION 1.0.0 LANGUAGES C CXX DESCRIPTION "Library for C++")

set(PROJECT_PREFIX GCLib-${GCLib_VERSION})

include(CMakeListsOptions.txt)
include(CMakeListsFiles.txt)

add_library(GCLib
        ${HEADER_FILES}
        ${SOURCE_FILES})

add_library(GCLib::GCLib ALIAS GCLib)

target_include_directories(GCLib
        PUBLIC
        $<BUILD_INTERFACE:${GCLib_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include/${PROJECT_PREFIX}>
        PRIVATE
        src)

export(PACKAGE GCLib)

Terminal:

> cmake.exe -DCMAKE_BUILD_TYPE=esp32doit-devkit-v1 "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2022.3.2/bin/ninja/win/x64/ninja.exe" -G Ninja -S C:\Users\Me\CLionProjects\GInvertor -B C:\Users\Me\CLionProjects\GInvertor\cmake-build-esp32doit-devkit-v1
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Tiny mode is enable
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/g++.exe

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Tiny mode is enable
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/g++.exe

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Tiny mode is enable
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/g++.exe

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Tiny mode is enable
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/gcc.exe
CMAKE_CXX_COMPILER= C:/Program Files/JetBrains/CLion 2022.3.2/bin/mingw/bin/g++.exe

Cmake changed the CMAKE_CXX_COMPILER variable, but it shouldn't.

When I remove add_subdirectory(GLibc) from CMakeListsUser.txt everything works perfectly

How can I fix this? Sorry, but I'm new to cmake

Gru2303
  • 9
  • 1
  • 3
  • Are you cross-compiling? If yes, better to include compilers, flags, etc. in a separate toolchain file and then use `-DCMAKE_TOOLCHAIN_FILE` option to use that toolchain file. – kiner_shah Feb 06 '23 at 07:21
  • @kiner_shah I using custom g++ compiler for ESP32. It aren't cross-compilling – Gru2303 Feb 06 '23 at 07:55
  • Note: What you are doing with your `CMakeListsUser.txt` file is exactly what [CMake's presets feature](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) is expressly designed to help you do in a better way! I'd be quite interested to see if your issue still reproduces if/when you switch to using presets here. – starball Feb 06 '23 at 08:18
  • Setting `CMAKE_CXX_COMPILER` after the first `project()` call is plain wrong: https://stackoverflow.com/a/63944545/3440745. Setting `CMAKE_SYSTEM_NAME` in the `CMakeLists.txt` is wrong too. Setting `CMAKE_SYSTEM_NAME` implies cross-compiling, and `xtensa-esp32-elf-gcc` is **cross-compiler**: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-tools.html. If you are new to CMake, then why don't follow some existing guide? – Tsyvarev Feb 06 '23 at 08:53
  • @Tsyvarev The CMakeLists file is the default cmake when I run the platformio project – Gru2303 Feb 06 '23 at 09:00
  • 'The CMakeLists file is the **default** cmake' - This contradicts to your description in the question post: 'I create a "CMakeLists.txt" and **add** this line "add_subdirectory(GLibc)"'. So, what part is **actually** default, and what exact changes have you done? – Tsyvarev Feb 06 '23 at 09:03
  • @Tsyvarev Sorry, that was a typo. In fact, I created a CMakeListsUser that connects already to CMakeLists if it exists – Gru2303 Feb 06 '23 at 09:17
  • Then my previous comment is applied: your setting of CMAKE_CXX_COMPILER is plain wrong because it comes after the `project()` call. – Tsyvarev Feb 06 '23 at 09:48
  • @Tsyvarev Thanks, I solved the problem by digging in this direction – Gru2303 Feb 06 '23 at 10:42

1 Answers1

0

Solved: Setting CMAKE_CXX_COMPILER after the first project() call is plain wrong.

Check this: stackoverflow.com/a/63944545/3440745

Gru2303
  • 9
  • 1
  • 3