Bladeren bron

Merge branch 'feature/cmake_use_new_features' into 'master'

cmake: use features introduced between 3.5 and 3.16

Closes IDF-5955

See merge request espressif/esp-idf!19819
Ivan Grokhotkov 3 jaren geleden
bovenliggende
commit
4a69a3f7ef

+ 1 - 1
components/bootloader/subproject/CMakeLists.txt

@@ -57,7 +57,7 @@ idf_build_set_property(__COMPONENT_REQUIRES_COMMON "${common_req}")
 idf_build_set_property(__OUTPUT_SDKCONFIG 0)
 project(bootloader)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DBOOTLOADER_BUILD=1" APPEND)
+idf_build_set_property(COMPILE_DEFINITIONS "BOOTLOADER_BUILD=1" APPEND)
 idf_build_set_property(COMPILE_OPTIONS "-fno-stack-protector" APPEND)
 
 idf_component_get_property(main_args esptool_py FLASH_ARGS)

+ 5 - 5
components/bt/CMakeLists.txt

@@ -687,17 +687,17 @@ idf_component_register(SRCS "${srcs}"
 if(CONFIG_BT_ENABLED)
     target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-implicit-fallthrough -Wno-unused-const-variable)
     if(CONFIG_IDF_TARGET_ESP32)
-        target_link_libraries(${COMPONENT_LIB} INTERFACE "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32/esp32")
+        target_link_directories(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32/esp32")
         target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app)
 
         target_link_libraries(${COMPONENT_LIB} INTERFACE "-u ld_include_hli_vectors_bt")
     elseif(CONFIG_IDF_TARGET_ESP32C3)
-        target_link_libraries(${COMPONENT_LIB} INTERFACE
-                "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32c3")
+        target_link_directories(${COMPONENT_LIB} INTERFACE
+                "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32c3")
         target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app)
     elseif(CONFIG_IDF_TARGET_ESP32S3)
-        target_link_libraries(${COMPONENT_LIB} INTERFACE
-                "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32s3")
+        target_link_directories(${COMPONENT_LIB} INTERFACE
+                "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32s3")
         target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app)
     elseif(CONFIG_IDF_TARGET_ESP32H2)
         if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_1)

+ 0 - 1
components/esp_event/host_test/esp_event_unit_test/CMakeLists.txt

@@ -3,6 +3,5 @@ cmake_minimum_required(VERSION 3.16)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 set(COMPONENTS main)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")
 project(test_esp_event_host)

+ 3 - 3
components/esp_phy/CMakeLists.txt

@@ -39,13 +39,13 @@ idf_component_register(SRCS "${srcs}"
 set(target_name "${idf_target}")
 if(IDF_TARGET STREQUAL "esp32h2")
     if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_2)
-        target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev2\"")
+        target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev2")
     endif()
     if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_1)
-        target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev1\"")
+        target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev1")
     endif()
 else()
-    target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}\"")
+    target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}")
 endif()
 
 # Override functions in PHY lib with the functions in 'phy_override.c'

+ 1 - 1
components/esp_wifi/CMakeLists.txt

@@ -39,7 +39,7 @@ if(CONFIG_ESP32_WIFI_ENABLED)
     idf_build_get_property(build_dir BUILD_DIR)
 
     set(target_name "${idf_target}")
-    target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}\"")
+    target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}")
 
     if(link_binary_libs)
         if(CONFIG_IDF_TARGET_ESP32C2)

+ 2 - 41
components/esptool_py/project_include.cmake

@@ -325,43 +325,6 @@ function(esptool_py_flash_target_image target_name image_name offset image)
     endif()
 endfunction()
 
-# Use this function to generate a ternary expression that will be evaluated.
-# - retexpr is the expression returned by the function
-# - condition is the expression evaluated to a boolean
-# - condtrue is the expression to evaluate if condition is true
-# - condfalse is the expression to evaluate if condition is false
-# This function can be summarized as:
-#   retexpr = condition ? condtrue : condfalse
-function(if_expr_generator retexpr condition condtrue condfalse)
-    # CMake version 3.8 and above provide a ternary operator for expression
-    # generator. For version under, we must simulate this behaviour
-    if(${CMAKE_VERSION} VERSION_LESS "3.8.0")
-
-        # If condtrue is not empty, then we have to do something in case the
-        # condition is true. Generate the expression that will be used in that
-        # case
-        if(condtrue)
-            set(iftrue "$<${condition}:${condtrue}>")
-        endif()
-
-        # Same for condfalse. If it is empty, it is useless to create an
-        # expression that will be evaluated later
-        if(condfalse)
-            set(iffalse "$<$<NOT:${condition}>:${condfalse}>")
-        endif()
-
-        # Concatenate the previously generated expressions. If one of them was
-        # not initialized (because of empty condtrue/condfalse) it will be
-        # replaced by an empty string
-        set(${retexpr} "${iftrue}${iffalse}" PARENT_SCOPE)
-
-    else()
-        # CMake 3.8 and above implement what we want, making the expression
-        # simpler
-        set(${retexpr} "$<IF:${condition},${condtrue},${condfalse}>" PARENT_SCOPE)
-    endif()
-endfunction()
-
 
 function(esptool_py_flash_target target_name main_args sub_args)
     set(single_value OFFSET IMAGE) # template file to use to be able to
@@ -444,8 +407,7 @@ $<JOIN:$<TARGET_PROPERTY:${target_name},IMAGES>,\n>")
 encrypted-${target_name},NON_ENCRYPTED_IMAGES>>")
 
         # Prepare esptool arguments (--encrypt or --encrypt-files)
-        if_expr_generator(if_non_enc_expr ${has_non_encrypted_images}
-                          "" "--encrypt")
+        set(if_non_enc_expr "$<IF:${has_non_encrypted_images},,--encrypt>")
         set_target_properties(encrypted-${target_name} PROPERTIES SUB_ARGS
                              "${sub_args}; ${if_non_enc_expr}")
 
@@ -457,8 +419,7 @@ encrypted-${target_name},NON_ENCRYPTED_IMAGES>,\n>")
 
         # Put both lists together, use --encrypted-files if we do also have
         # plain images to flash
-        if_expr_generator(if_enc_expr ${has_non_encrypted_images}
-                          "--encrypt-files\n" "")
+        set(if_enc_expr "$<IF:${has_non_encrypted_images},--encrypt-files\n,>")
         set(flash_args_content "$<JOIN:$<TARGET_PROPERTY:\
 encrypted-${target_name},SUB_ARGS>, >\
 ${non_encrypted_files}\n\

+ 1 - 1
components/ieee802154/CMakeLists.txt

@@ -22,7 +22,7 @@ if(CONFIG_IEEE802154_ENABLED)
                             "-L ${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}/rev2")
             endif()
         else()
-            target_link_libraries(${COMPONENT_LIB} INTERFACE "-L ${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}")
+            target_link_directories(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}")
         endif()
         target_link_libraries(${COMPONENT_LIB} INTERFACE $<TARGET_FILE:${esp_phy_lib}> lib802154.a libphy.a libbtbb.a
                             $<TARGET_FILE:${esp_phy_lib}>)

+ 1 - 1
components/nvs_flash/host_test/nvs_page_test/CMakeLists.txt

@@ -7,7 +7,7 @@ list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/driver/")
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/spi_flash/")
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
+idf_build_set_property(COMPILE_DEFINITIONS "NO_DEBUG_STORAGE" APPEND)
 project(test_nvs_page_host)
 
 

+ 1 - 1
components/pthread/CMakeLists.txt

@@ -6,7 +6,7 @@ set(sources "pthread.c"
 idf_component_register(SRCS ${sources}
                     INCLUDE_DIRS include)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-D_POSIX_READER_WRITER_LOCKS" APPEND)
+idf_build_set_property(COMPILE_DEFINITIONS "_POSIX_READER_WRITER_LOCKS" APPEND)
 
 set(extra_link_flags "-u pthread_include_pthread_impl")
 list(APPEND extra_link_flags "-u pthread_include_pthread_cond_impl")

+ 0 - 2
examples/cxx/experimental/experimental_cpp_component/host_test/gpio/CMakeLists.txt

@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.16)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 set(COMPONENTS main)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
-
 # Overriding components which should be mocked
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/driver/")
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")

+ 0 - 2
examples/cxx/experimental/experimental_cpp_component/host_test/i2c/CMakeLists.txt

@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.16)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 set(COMPONENTS main)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
-
 # Overriding components which should be mocked
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/driver/")
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")

+ 0 - 2
examples/cxx/experimental/experimental_cpp_component/host_test/spi/CMakeLists.txt

@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.16)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 set(COMPONENTS main)
 
-idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
-
 # Overriding components which should be mocked
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/driver/")
 list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")

+ 22 - 5
tools/cmake/build.cmake

@@ -30,6 +30,9 @@ endfunction()
 function(idf_build_set_property property value)
     cmake_parse_arguments(_ "APPEND" "" "" ${ARGN})
 
+    # Fixup property value, e.g. for compatibility. (Overwrites variable 'value'.)
+    __build_fixup_property("${property}" "${value}" value)
+
     if(__APPEND)
         set_property(TARGET __idf_build_target APPEND PROPERTY ${property} ${value})
     else()
@@ -73,7 +76,7 @@ function(__build_get_idf_git_revision)
     endif()
     # cut IDF_VER to required 32 characters.
     string(SUBSTRING "${idf_ver_t}" 0 31 idf_ver)
-    idf_build_set_property(COMPILE_DEFINITIONS "-DIDF_VER=\"${idf_ver}\"" APPEND)
+    idf_build_set_property(COMPILE_DEFINITIONS "IDF_VER=\"${idf_ver}\"" APPEND)
     git_submodule_check("${idf_path}")
     idf_build_set_property(IDF_VER ${idf_ver})
 endfunction()
@@ -90,7 +93,7 @@ function(__build_set_default_build_specifications)
     unset(c_compile_options)
     unset(cxx_compile_options)
 
-    list(APPEND compile_definitions "-D_GNU_SOURCE")
+    list(APPEND compile_definitions "_GNU_SOURCE")
 
     list(APPEND compile_options     "-ffunction-sections"
                                     "-fdata-sections"
@@ -164,6 +167,21 @@ function(__build_set_lang_version)
     idf_build_set_property(CXX_COMPILE_OPTIONS "-std=${cxx_std}" APPEND)
 endfunction()
 
+#
+# Perform any fixes or adjustments to the values stored in IDF build properties.
+# This function only gets called from 'idf_build_set_property' and doesn't affect
+# the properties set directly via 'set_property'.
+#
+function(__build_fixup_property property value out_var)
+
+    # Fixup COMPILE_DEFINITIONS property to support -D prefix, which had to be used in IDF v4.x projects.
+    if(property STREQUAL "COMPILE_DEFINITIONS" AND NOT "${value}" STREQUAL "")
+        string(REGEX REPLACE "^-D" "" stripped_value "${value}")
+        set("${out_var}" "${stripped_value}" PARENT_SCOPE)
+    endif()
+
+endfunction()
+
 #
 # Initialize the build. This gets called upon inclusion of idf.cmake to set internal
 # properties used for the processing phase of the build.
@@ -592,7 +610,7 @@ macro(idf_build_process target)
 
     # All targets built under this scope is with the ESP-IDF build system
     set(ESP_PLATFORM 1)
-    idf_build_set_property(COMPILE_DEFINITIONS "-DESP_PLATFORM" APPEND)
+    idf_build_set_property(COMPILE_DEFINITIONS "ESP_PLATFORM" APPEND)
 
     # Perform component processing (inclusion of project_include.cmake, adding component
     # subdirectories, creating library targets, linking libraries, etc.)
@@ -612,8 +630,7 @@ endmacro()
 function(idf_build_executable elf)
     # Set additional link flags for the executable
     idf_build_get_property(link_options LINK_OPTIONS)
-    # Using LINK_LIBRARIES here instead of LINK_OPTIONS, as the latter is not in CMake 3.5.
-    set_property(TARGET ${elf} APPEND PROPERTY LINK_LIBRARIES "${link_options}")
+    set_property(TARGET ${elf} APPEND PROPERTY LINK_OPTIONS "${link_options}")
 
     # Propagate link dependencies from component library targets to the executable
     idf_build_get_property(link_depends __LINK_DEPENDS)

+ 2 - 8
tools/cmake/component.cmake

@@ -453,6 +453,7 @@ function(idf_component_register)
     # idf_build_process
     idf_build_get_property(include_directories INCLUDE_DIRECTORIES GENERATOR_EXPRESSION)
     idf_build_get_property(compile_options COMPILE_OPTIONS GENERATOR_EXPRESSION)
+    idf_build_get_property(compile_definitions COMPILE_DEFINITIONS GENERATOR_EXPRESSION)
     idf_build_get_property(c_compile_options C_COMPILE_OPTIONS GENERATOR_EXPRESSION)
     idf_build_get_property(cxx_compile_options CXX_COMPILE_OPTIONS GENERATOR_EXPRESSION)
     idf_build_get_property(asm_compile_options ASM_COMPILE_OPTIONS GENERATOR_EXPRESSION)
@@ -460,18 +461,11 @@ function(idf_component_register)
 
     include_directories("${include_directories}")
     add_compile_options("${compile_options}")
+    add_compile_definitions("${compile_definitions}")
     add_c_compile_options("${c_compile_options}")
     add_cxx_compile_options("${cxx_compile_options}")
     add_asm_compile_options("${asm_compile_options}")
 
-    # Unfortunately add_definitions() does not support generator expressions. A new command
-    # add_compile_definition() does but is only available on CMake 3.12 or newer. This uses
-    # add_compile_options(), which can add any option as the workaround.
-    #
-    # TODO: Use add_compile_definitions() once minimum supported version is 3.12 or newer.
-    idf_build_get_property(compile_definitions COMPILE_DEFINITIONS GENERATOR_EXPRESSION)
-    add_compile_options("${compile_definitions}")
-
     if(common_reqs) # check whether common_reqs exists, this may be the case in minimalistic host unit test builds
         list(REMOVE_ITEM common_reqs ${component_lib})
     endif()

+ 6 - 2
tools/cmake/project.cmake

@@ -524,8 +524,12 @@ macro(project project_name)
         set(mapfile "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map")
         set(idf_target "${IDF_TARGET}")
         string(TOUPPER ${idf_target} idf_target)
-        target_link_libraries(${project_elf} PRIVATE "-Wl,--cref" "-Wl,--defsym=IDF_TARGET_${idf_target}=0"
-        "-Wl,--Map=\"${mapfile}\"")
+        # Add cross-reference table to the map file
+        target_link_options(${project_elf} PRIVATE "-Wl,--cref")
+        # Add this symbol as a hint for idf_size.py to guess the target name
+        target_link_options(${project_elf} PRIVATE "-Wl,--defsym=IDF_TARGET_${idf_target}=0")
+        # Enable map file output
+        target_link_options(${project_elf} PRIVATE "-Wl,--Map=${mapfile}")
         unset(idf_target)
     endif()
 

+ 10 - 16
tools/cmake/utilities.cmake

@@ -152,24 +152,18 @@ function(target_linker_script target deptype scriptfiles)
         get_filename_component(search_dir "${abs_script}" DIRECTORY)
         get_filename_component(scriptname "${abs_script}" NAME)
 
-        if(deptype STREQUAL INTERFACE OR deptype STREQUAL PUBLIC)
-            get_target_property(link_libraries "${target}" INTERFACE_LINK_LIBRARIES)
-        else()
-            get_target_property(link_libraries "${target}" LINK_LIBRARIES)
-        endif()
-
-        list(FIND "${link_libraries}" "-L \"${search_dir}\"" found_search_dir)
-        if(found_search_dir EQUAL "-1")  # not already added as a search path
-            target_link_libraries("${target}" "${deptype}" "-L \"${search_dir}\"")
-        endif()
-
-        target_link_libraries("${target}" "${deptype}" "-T ${scriptname}")
+        target_link_directories("${target}" "${deptype}" ${search_dir})
+        # Regarding the usage of SHELL, see
+        # https://cmake.org/cmake/help/latest/command/target_link_options.html#option-de-duplication
+        target_link_options("${target}" "${deptype}" "SHELL:-T ${scriptname}")
 
         # Note: In ESP-IDF, most targets are libraries and libary LINK_DEPENDS don't propagate to
-        # executable(s) the library is linked to. Attach manually to executable once it is known.
-        #
-        # Property INTERFACE_LINK_DEPENDS is available in CMake 3.13 which should propagate link
-        # dependencies.
+        # executable(s) the library is linked to. Since CMake 3.13, INTERFACE_LINK_DEPENDS is
+        # available to solve this. However, when GNU Make generator is used, this property also
+        # propagates INTERFACE_LINK_DEPENDS dependencies to other static libraries.
+        # TODO: see if this is an expected behavior and possibly report this as a bug to CMake.
+        # For the time being, record all linker scripts in __LINK_DEPENDS and attach manually to
+        # the executable target once it is known.
         if(NOT __PROCESS)
             idf_build_set_property(__LINK_DEPENDS ${abs_script} APPEND)
         endif()

+ 0 - 2
tools/mocks/driver/CMakeLists.txt

@@ -20,5 +20,3 @@ idf_component_mock(INCLUDE_DIRS ${include_dirs}
     ${original_driver_dir}/include/driver/spi_common.h
     ${original_driver_dir}/include/driver/i2c.h
     ${original_driver_dir}/include/driver/gpio.h)
-
-idf_build_set_property(COMPILE_DEFINITIONS "-DSPI_MOCK" APPEND)