From e7e059cc0a5e10d42ad7e949e63ebb29a6603b7a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 16 Nov 2021 00:08:12 +0100 Subject: [PATCH 1/8] linux: allow "sys/queue.h" to be used on macOS macOS and other BSD-like systems have header. Fall back to it if bsd/sys/queue.h (provided on Linux by libbsd) is not found. --- components/linux/include/sys/queue.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/linux/include/sys/queue.h b/components/linux/include/sys/queue.h index 9787d9d5f8..34f5a6caa4 100644 --- a/components/linux/include/sys/queue.h +++ b/components/linux/include/sys/queue.h @@ -1 +1,7 @@ -#include "bsd/sys/queue.h" +#if __has_include() +/* On Linux, try using sys/queue.h provided by libbsd-dev */ +#include +#else +/* Fall back to sys/queue.h which may exist e.g. on macOS */ +#include_next +#endif From fd1c213be4dcf7260600288519c0580362b5b2f9 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 16 Nov 2021 00:06:57 +0100 Subject: [PATCH 2/8] esp_rom: support building for Linux target with Clang This fixes passing -Wimplicit-fallthrough=0 flag to Clang, which it doesn't recognize, and is not necessary for this file. Also the flag is changed from PUBLIC to PRIVATE since it is necessary when compiling this component only. --- components/esp_rom/CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/esp_rom/CMakeLists.txt b/components/esp_rom/CMakeLists.txt index 251f60edfb..ad48f71bd1 100644 --- a/components/esp_rom/CMakeLists.txt +++ b/components/esp_rom/CMakeLists.txt @@ -48,8 +48,11 @@ endfunction() if(target STREQUAL "linux") # We need to disable some warnings due to the ROM code's printf implementation - if(${CMAKE_CXX_COMPILER_VERSION} GREATER "7.0.0") # TODO: clang compatibility - target_compile_options(${COMPONENT_LIB} PUBLIC -Wimplicit-fallthrough=0 -Wno-shift-count-overflow) + if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND ${CMAKE_CXX_COMPILER_VERSION} GREATER "7.0.0") + target_compile_options(${COMPONENT_LIB} PRIVATE -Wimplicit-fallthrough=0 -Wno-shift-count-overflow) + endif() + if(CMAKE_C_COMPILER_ID MATCHES "Clang") # Clang or AppleClang + target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-integer-overflow -Wno-shift-count-overflow) endif() else() target_linker_script(${COMPONENT_LIB} INTERFACE "${target}/${ld_folder}/${target}.rom.ld") From 9901fc3058fbdba2443c9634a24586c3f506dbda Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 16 Nov 2021 00:04:36 +0100 Subject: [PATCH 3/8] cmake: don't pass --gc-sections to macOS linker, use -dead_strip When building for "linux" (~POSIX) target on macOS, the system linker is normally used. MacOS linker doesn't recognise --gc-sections, but has a -dead_strip flag which is equivalent. --- CMakeLists.txt | 6 ++++++ tools/cmake/build.cmake | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b86ac6027..ec4e2bcc66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -196,6 +196,12 @@ endif() list(APPEND link_options "-fno-lto") +if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + list(APPEND link_options "-Wl,-dead_strip") +else() + list(APPEND link_options "-Wl,--gc-sections") +endif() + # Placing jump tables in flash would cause issues with code that required # to be placed in IRAM list(APPEND compile_options "-fno-jump-tables") diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 78b95f8d45..51fd0d40c4 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -111,13 +111,10 @@ function(__build_set_default_build_specifications) list(APPEND cxx_compile_options "-std=gnu++11") - list(APPEND link_options "-Wl,--gc-sections") - idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND) idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND) idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND) idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND) - idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND) endfunction() # From d5a1f6bafb7668f7a3545e053fe642ef5865ad62 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Nov 2021 23:58:32 +0100 Subject: [PATCH 4/8] global: Kconfig: add CONFIG_IDF_TARGET_LINUX Can be used in target-specific C code to distinguish when Linux target is used. Also don't enable CONFIG_APP_BUILD_GENERATE_BINARIES and CONFIG_APP_BUILD_BOOTLOADER when building for Linux, since the bootloader is not used. CONFIG_APP_BUILD_GENERATE_BINARIES might make sense in some cases, can be re-enabled later. --- Kconfig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Kconfig b/Kconfig index 9a18909c8b..17ca73813b 100644 --- a/Kconfig +++ b/Kconfig @@ -37,6 +37,10 @@ mainmenu "Espressif IoT Development Framework Configuration" string default "$IDF_TARGET" + config IDF_TARGET_LINUX + bool + default "y" if IDF_TARGET="linux" + config IDF_TARGET_ESP32 bool default "y" if IDF_TARGET="esp32" @@ -163,6 +167,7 @@ mainmenu "Espressif IoT Development Framework Configuration" config APP_BUILD_TYPE_APP_2NDBOOT bool prompt "Default (binary application + 2nd stage bootloader)" + depends on !IDF_TARGET_LINUX select APP_BUILD_GENERATE_BINARIES select APP_BUILD_BOOTLOADER select APP_BUILD_USE_FLASH_SECTIONS From 7830af1eb999d6a4678f97331e7d180fe643caff Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Nov 2021 23:59:59 +0100 Subject: [PATCH 5/8] partition_table: allow enabling this component for Linux target Currently partition_table_bin target is not added as a dependency when building Linux app, and has to be specified manually (idf.py partition-table). --- components/partition_table/CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/partition_table/CMakeLists.txt b/components/partition_table/CMakeLists.txt index 385d2a5d96..f3b607d685 100644 --- a/components/partition_table/CMakeLists.txt +++ b/components/partition_table/CMakeLists.txt @@ -1,4 +1,10 @@ -idf_component_register(PRIV_REQUIRES esptool_py) +idf_build_get_property(target IDF_TARGET) +set(priv_req) +if(NOT ${target} STREQUAL "linux") + list(APPEND priv_req esptool_py) +endif() + +idf_component_register(PRIV_REQUIRES ${priv_req}) if(BOOTLOADER_BUILD) return() From f72ce6720ddffe89ce25f882fa16f8d0505588b2 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 16 Nov 2021 00:26:14 +0100 Subject: [PATCH 6/8] linux: add dummy sys/lock.h implementation (single threaded only) Useful for building IDF code which relies on the legacy locking functions from newlib. --- components/linux/include/sys/lock.h | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 components/linux/include/sys/lock.h diff --git a/components/linux/include/sys/lock.h b/components/linux/include/sys/lock.h new file mode 100644 index 0000000000..71d2f7654a --- /dev/null +++ b/components/linux/include/sys/lock.h @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef __SYS_LOCK_H__ +#define __SYS_LOCK_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* newlib locks implementation for CONFIG_IDF_TARGET_LINUX, single threaded. + * Note, currently this doesn't implement the functions required + * when _RETARGETABLE_LOCKING is defined. They should be added. + */ + + +/* Compatibility definitions for legacy newlib locking functions */ +typedef int _lock_t; + +static inline void _lock_init(_lock_t *plock) {} +static inline void _lock_init_recursive(_lock_t *plock) {} +static inline void _lock_close(_lock_t *plock) {} +static inline void _lock_close_recursive(_lock_t *plock) {} +static inline void _lock_acquire(_lock_t *plock) {} +static inline void _lock_acquire_recursive(_lock_t *plock) {} +static inline int _lock_try_acquire(_lock_t *plock) +{ + return 1; +} +static inline int _lock_try_acquire_recursive(_lock_t *plock) +{ + return 1; +} +static inline void _lock_release(_lock_t *plock) {} +static inline void _lock_release_recursive(_lock_t *plock) {} + + +#ifdef __cplusplus +} +#endif + +#endif /* __SYS_LOCK_H__ */ From f18d16d5e69d2b265ee77e82f81c550374224a6b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Nov 2021 23:55:33 +0100 Subject: [PATCH 7/8] esp_common: don't generate custom sections when building for Linux When building for Linux, a standard linker script is used. Ignore all attributes related to custom sections. --- components/esp_common/include/esp_attr.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/esp_common/include/esp_attr.h b/components/esp_common/include/esp_attr.h index 84de204618..acd895c7be 100644 --- a/components/esp_common/include/esp_attr.h +++ b/components/esp_common/include/esp_attr.h @@ -147,9 +147,14 @@ FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a = a << b; return a; } // // Using unique sections also means --gc-sections can remove unused // data with a custom section type set +#ifndef CONFIG_IDF_TARGET_LINUX #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER)))) - #define _COUNTER_STRINGIFY(COUNTER) #COUNTER +#else +// Custom section attributes are generally not used in the port files for Linux target, but may be found +// in the common header files. Don't declare custom sections in that case. +#define _SECTION_ATTR_IMPL(SECTION, COUNTER) +#endif /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in ESP-IDF's own source code, but not deprecated for external users. From 3f6448b82cb433c0d90f152653d0594a06055852 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 24 Jan 2022 19:02:51 +0100 Subject: [PATCH 8/8] gitlab: add linux component to CODEOWNERS --- .gitlab/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab/CODEOWNERS b/.gitlab/CODEOWNERS index 0a860d3fad..bfe7cf3a1c 100644 --- a/.gitlab/CODEOWNERS +++ b/.gitlab/CODEOWNERS @@ -113,6 +113,7 @@ /components/idf_test/ @esp-idf-codeowners/ci /components/ieee802154/ @esp-idf-codeowners/ieee802154 /components/json/ @esp-idf-codeowners/app-utilities +/components/linux/ @esp-idf-codeowners/system /components/log/ @esp-idf-codeowners/system /components/lwip/ @esp-idf-codeowners/lwip /components/mbedtls/ @esp-idf-codeowners/app-utilities @esp-idf-codeowners/security