ESP32 ESP-IDF 項目文件結構

概述

使用ESP-IDF開發時,項目特指一個目錄,其中包含了構建可執行應用程序所需的全部文件和配置,以及其他支持型文件,例如分區表、數據/文件系統分區和引導程序。
ESP-IDF 並不是項目的一部分,它獨立於項目,通過 IDF_PATH 環境變量(保存 esp-idf 目錄的路徑)鏈接到項目,從而將 IDF 框架與項目分離。

項目結構

- myProject/
    - CMakeLists.txt
    - sdkconfig
    - components/
        - component1/
            - CMakeLists.txt
            - Kconfig
            - src1.c
        - component2/
            - CMakeLists.txt
            - Kconfig
            - src1.c
            - include/
                - component2.h
    - main/
        - src1.c
        - src2.c
    - build/
  • 頂層項目 CMakeLists.txt 文件,這是 CMake 用於學習如何構建項目的主要文件,可以在這個文件中設置項目全局的 CMake 變量。頂層項目 CMakeLists.txt 文件會導入 esp-idf/tools/cmake/project.cmake 文件,由它負責實現構建系統的其餘部分。該文件最後會設置項目的名稱,並定義該項目
  • sdkconfig 項目配置文件,執行 idf.py menuconfig 時會創建或更新此文件,文件中保存了項目中所有組件(包括 ESP-IDF 本身)的配置信息。 sdkconfig 文件可能會也可能不會被添加到項目的源碼管理系統中。
  • 可選的 component 目錄中包含了項目的部分自定義組件,並不是每個項目都需要這種自定義組件,但它組件有助於構建可複用的代碼或者導入第三方(不屬於 ESP-IDF)的組件。
  • main 目錄是一個特殊的 僞組件,包含項目本身的源代碼。main 是默認名稱,CMake 變量 COMPONENT_DIRS 默認包含此組件,但您可以修改此變量。或者,您也可以在頂層 CMakeLists.txt 中設置 EXTRA_COMPONENT_DIRS 變量以查找其他指定位置處的組件。
  • build 目錄是存放構建輸出的地方,如果沒有此目錄,idf.py 會自動創建。CMake 會配置項目,並在此目錄下生成臨時的構建文件。隨後,在主構建進程的運行期間,該目錄還會保存臨時目標文件、庫文件以及最終輸出的二進制文件。此目錄通常不會添加到項目的源碼管理系統中,也不會隨項目源碼一同發佈。

參考

hello-world的項目結構分析

- hello_world/
    - main/
        - CMakeLists.txt
        - component.mk
        - hello_world_main.c
    - .gdbinit.ci
    - CMakeLists.txt
    - loadable_elf_example_test.py
    - Makefile
    - README.md
    - sdkconfig
    - sdkconfig.ci

main 目錄

CMakeLists.txt 文件

CMakeLists.txt內容

idf_component_register(SRCS "hello_world_main.c"
                    INCLUDE_DIRS "")

idf_component_register函數中SRC中包含所有的源文件,INCLUDE_DIRS中包含所有的頭文件目錄
如果main中的文件結構這樣

- main/
    - include/
        - hello_world.h
        - light_control.h
    - CMakeLists.txt
    - component.mk
    - hello_world_main.c
    - light_control.c

CMakeLists.txt中的內容應該修改成

idf_component_register(SRCS "hello_world_main.c"
                            "light_control.c"
                    INCLUDE_DIRS "include")

component.mk 文件

GUN Make中使用的文件,通過CMake構建時可以在CMakeLists.txt設置 COMPONENT_ADD_INCLUDEDIRS 和 COMPONENT_SRCDIRS 等變量將組件添加到編譯過程中。

hello_world_main.c 文件

開發的源文件

.gdbinit.ci 文件

gdb初始化配置文件

CMakeLists.txt

CMakeLists.txt 內容

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello-world)
  • cmake_minimum_required(VERSION 3.5) 必須放在 CMakeLists.txt 文件的第一行,它會告訴 CMake 構建該項目所需要的最小版本號。ESP-IDF 支持 CMake 3.5 或更高的版本。
  • include($ENV{IDF_PATH}/tools/cmake/project.cmake) 會導入 CMake 的其餘功能來完成配置項目、檢索組件等任務。
  • project(hello-world) 會創建項目本身,並指定項目名稱。該名稱會作爲最終輸出的二進制文件的名字,即 hello-world.elf 和 hello-world.bin。每個 CMakeLists 文件只能定義一個項目。

Makefile

項目頂層 Makefile,該 Makefile 設置了 PROJECT_NAME 變量,還可以定義作用於整個項目的其它 make 變量(可選)。頂層 Makefile 會導入核心 Makefile 文件 $(IDF_PATH)/make/project.mk ,由它負責實現 ESP-IDF 構建系統的剩餘部分。在CMake編譯過中是不需要的。

README.md 文件

項目說明文件,markdown文本。

sdkconfig

它的功能和使用Make的KConfig文件類似,使用CMake編譯時,從 sdkconfig 文件中加載項目配置信息,生成 sdkconfig.cmake 和 sdkconfig.h 文件,分別用在 CMake 和 C/C++ 中定義配置項。如果項目配置發生了更改,CMake 會自動重新運行,重新生成上述兩個文件,接着重新配置項目。通過idf.py menuconfig可以修改配置項。

idf_component_register函數

idf_component_register函數在/tools/cmake/component.cmake中被定義。

# idf_component_register
#
# @brief Register a component to the build, creating component library targets etc.
#
# @param[in, optional] SRCS (multivalue) list of source files for the component
# @param[in, optional] SRC_DIRS (multivalue) list of source directories to look for source files
#                       in (.c, .cpp. .S); ignored when SRCS is specified.
# @param[in, optional] EXCLUDE_SRCS (multivalue) used to exclude source files for the specified
#                       SRC_DIRS
# @param[in, optional] INCLUDE_DIRS (multivalue) public include directories for the created component library
# @param[in, optional] PRIV_INCLUDE_DIRS (multivalue) private include directories for the created component library
# @param[in, optional] LDFRAGMENTS (multivalue) linker script fragments for the component
# @param[in, optional] REQUIRES (multivalue) publicly required components in terms of usage requirements
# @param[in, optional] PRIV_REQUIRES (multivalue) privately required components in terms of usage requirements
#                      or components only needed for functions/values defined in its project_include.cmake
# @param[in, optional] REQUIRED_IDF_TARGETS (multivalue) the list of IDF build targets that the component only supports
# @param[in, optional] EMBED_FILES (multivalue) list of binary files to embed with the component
# @param[in, optional] EMBED_TXTFILES (multivalue) list of text files to embed with the component
function(idf_component_register)
    set(options)
    set(single_value)
    set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
                    INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
                    PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
    cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})
    ...
    ...
    ...
endfunction()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章