refactor(linux): Unified libbsd handling

* Users can now use libbsd string.h and sys/cdefs.h functionality
  (e.g., strlcpy, containerof) on Linux by just including
  string.h or sys/cdefs.h. In other words, the includes are the same
  on the Linux target as well as on chips targets (ESP32, etc.).
* libbsd linking is done by the linux component (belongs to common
  components) now instead of handling it separately in each component
This commit is contained in:
Jakob Hasse 2024-03-06 15:21:10 +08:00
parent 8e863fa9a8
commit 4d629be602
20 changed files with 61 additions and 83 deletions

View File

@ -35,13 +35,3 @@ idf_component_register(SRCS ${srcs}
PRIV_REQUIRES esp_driver_uart
esp_driver_usb_serial_jtag
)
if(${target} STREQUAL "linux")
# link bsd library for strlcpy
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()

View File

@ -6,10 +6,6 @@
#include <stdio.h>
#include <string.h>
#if __has_include(<bsd/string.h>)
// for strlcpy
#include <bsd/string.h>
#endif
#include <stdlib.h>
#include <sys/param.h>
#include "esp_heap_caps.h"

View File

@ -1,17 +1,15 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include <sys/cdefs.h> // __containerof
#include "esp_console.h"
#include "console_private.h"
#include "esp_log.h"
#include "linenoise/linenoise.h"
#if CONFIG_IDF_TARGET_LINUX
#include "esp_linux_helper.h" // __containerof
#endif
static const char *TAG = "console.common";

View File

@ -12,10 +12,10 @@
#include <termios.h>
#include <string.h>
#include <assert.h>
#include <sys/cdefs.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_linux_helper.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

View File

@ -1,16 +1,13 @@
/*
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/cdefs.h>
#include "esp_tls.h"
#include "esp_tls_error_capture_internal.h"
#if CONFIG_IDF_TARGET_LINUX
#include "esp_linux_helper.h"
#endif
typedef struct esp_tls_error_storage {
struct esp_tls_last_error parent; /*!< standard esp-tls last error container */
int sock_errno; /*!< last socket error captured in esp-tls */

View File

@ -43,12 +43,3 @@ foreach(req ${optional_reqs})
target_link_libraries(${COMPONENT_LIB} PRIVATE ${req_lib})
endif()
endforeach()
if(${IDF_TARGET} STREQUAL "linux" AND CONFIG_ESP_ERR_TO_NAME_LOOKUP)
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()

View File

@ -7,10 +7,6 @@
//Do not edit this file because it is autogenerated by gen_esp_err_to_name.py
#include <string.h>
#if __has_include(<bsd/string.h>)
// for strlcpy
#include <bsd/string.h>
#endif
#include "esp_err.h"
#if __has_include("soc/soc.h")
#include "soc/soc.h"

View File

@ -7,10 +7,6 @@
@COMMENT@
#include <string.h>
#if __has_include(<bsd/string.h>)
// for strlcpy
#include <bsd/string.h>
#endif
#include "esp_err.h"
#if __has_include("soc/soc.h")
#include "soc/soc.h"

View File

@ -20,12 +20,3 @@ idf_component_register(SRCS "src/httpd_main.c"
PRIV_INCLUDE_DIRS ${priv_inc_dir}
REQUIRES ${requires}
PRIV_REQUIRES ${priv_req})
if(${IDF_TARGET} STREQUAL "linux")
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()

View File

@ -7,10 +7,6 @@
#include <stdlib.h>
#include <string.h>
#if __has_include(<bsd/string.h>)
// for strlcpy
#include <bsd/string.h>
#endif
#include <sys/param.h>
#include <esp_log.h>
#include <esp_err.h>

View File

@ -26,14 +26,6 @@ idf_component_register(SRCS "${srcs}"
if(${target} STREQUAL "linux")
# set BUILD_DIR because partition_linux.c uses a file created in the build directory
target_compile_definitions(${COMPONENT_LIB} PRIVATE "BUILD_DIR=\"${build_dir}\"")
# link bsd library for strlcpy
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()
if(CMAKE_C_COMPILER_ID MATCHES "GNU")

View File

@ -7,9 +7,6 @@
*/
#include <string.h>
#if __has_include(<bsd/string.h>)
#include <bsd/string.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include "esp_err.h"

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -14,7 +14,7 @@
* should go back to #include "sys/queue.h" once the tests are switched to CMake
* see IDF-7000
*/
#if __has_include(<bsd/string.h>)
#if __has_include(<bsd/sys/queue.h>)
#include <bsd/sys/queue.h>
#else
#include "sys/queue.h"
@ -33,9 +33,6 @@
#include "bootloader_util.h"
#if CONFIG_IDF_TARGET_LINUX
#if __has_include(<bsd/string.h>)
#include <bsd/string.h>
#endif
#include "esp_private/partition_linux.h"
#endif

View File

@ -8,10 +8,6 @@
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#if __has_include(<bsd/string.h>)
// for strlcpy
#include <bsd/string.h>
#endif
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>

View File

@ -14,3 +14,12 @@ endif()
idf_component_register(INCLUDE_DIRS ${includes}
REQUIRED_IDF_TARGETS linux
SRCS ${srcs})
if(${IDF_TARGET} STREQUAL "linux")
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()

View File

@ -1,10 +1,16 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
/*
* NOTE: This file is deprecated and will be removed in the future.
* Use include sys/cdefs.h instead, it will also include
* the corresponding libbsd header. TODO: IDF-9391
*/
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -0,0 +1,15 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This header file also includes the additional libbsd string.h with functions such as strlcpy(),
* which are present in the ESP-IDF toolchain and on MacOS, but not on Linux.
*/
#pragma once
#include <bsd/string.h>
#include_next <string.h>

View File

@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This header file adds the libbsd defines of header cdefs.h.
* It also defines __uintptr_t which is still used by older libbsd versions (< 0.11.3)
* but not (or not guaranteed to be) defined on Linux.
*/
#pragma once
#include <stdint.h>
// We need a define. We can't typedef here since, depending on the include order,
// uintptr_t may not be defined yet.
#define __uintptr_t uintptr_t
#include_next <sys/cdefs.h>
#include <bsd/sys/cdefs.h>

View File

@ -25,7 +25,7 @@
#define LWIP_TIMEVAL_PRIVATE 0
#include <sys/time.h>
#include "esp_linux_helper.h"
#include <sys/cdefs.h>
#define LWIP_ERRNO_INCLUDE <errno.h>

View File

@ -30,13 +30,6 @@ if(${target} STREQUAL "linux")
target_compile_options(${COMPONENT_LIB} PUBLIC "-DLINUX_TARGET")
target_compile_options(${COMPONENT_LIB} PUBLIC --coverage)
target_link_libraries(${COMPONENT_LIB} PUBLIC --coverage)
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
else()
target_sources(${COMPONENT_LIB} PRIVATE "src/nvs_encrypted_partition.cpp")
target_link_libraries(${COMPONENT_LIB} PRIVATE idf::mbedtls)