1idf_build_get_property(target IDF_TARGET)
2idf_build_get_property(sdkconfig_header SDKCONFIG_HEADER)
3if(NOT "${target}" STREQUAL "esp32s2")
4    return()
5endif()
6
7if(BOOTLOADER_BUILD)
8    # For bootloader, all we need from esp32s2 is headers
9    idf_component_register(INCLUDE_DIRS include REQUIRES xtensa)
10    target_linker_script(${COMPONENT_LIB} INTERFACE "ld/esp32s2.peripherals.ld")
11else()
12    # Regular app build
13
14    set(srcs "cache_err_int.c"
15             "memprot.c"
16             "clk.c"
17             "crosscore_int.c"
18             "dport_access.c"
19             "hw_random.c"
20             "spiram.c"
21             "spiram_psram.c"
22             "system_api_esp32s2.c"
23             "esp_crypto_lock.c"
24             "esp_hmac.c"
25             "esp_ds.c")
26
27    set(include_dirs "include")
28
29    set(requires driver efuse xtensa)
30
31    # app_update is added here because cpu_start.c uses esp_ota_get_app_description() function.
32    # esp_timer is added here because cpu_start.c uses esp_timer
33    set(priv_requires
34        app_trace app_update bootloader_support esp_system log mbedtls nvs_flash
35        pthread spi_flash vfs espcoredump esp_common esp_timer)
36
37    set(fragments linker.lf ld/esp32s2_fragments.lf)
38
39    idf_component_register(SRCS "${srcs}"
40                        INCLUDE_DIRS "${include_dirs}"
41                        LDFRAGMENTS "${fragments}"
42                        REQUIRES "${requires}"
43                        PRIV_REQUIRES "${priv_requires}"
44                        REQUIRED_IDF_TARGETS esp32s2)
45
46    target_linker_script(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/esp32s2_out.ld")
47
48    # Process the template file through the linker script generation mechanism, and use the output for linking the
49    # final binary
50    target_linker_script(${COMPONENT_LIB} INTERFACE
51                         "${CMAKE_CURRENT_LIST_DIR}/ld/esp32s2.project.ld.in"
52                         PROCESS "${CMAKE_CURRENT_BINARY_DIR}/ld/esp32s2.project.ld")
53
54    target_linker_script(${COMPONENT_LIB} INTERFACE "ld/esp32s2.peripherals.ld")
55    target_link_libraries(${COMPONENT_LIB} PUBLIC gcc)
56    target_link_libraries(${COMPONENT_LIB} INTERFACE "-u call_user_start_cpu0")
57
58    idf_build_get_property(config_dir CONFIG_DIR)
59    # Preprocess esp32s2.ld linker script to include configuration, becomes esp32s2_out.ld
60    set(LD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ld)
61    add_custom_command(
62        OUTPUT esp32s2_out.ld
63        COMMAND "${CMAKE_C_COMPILER}" -C -P -x c -E -o esp32s2_out.ld -I ${config_dir} ${LD_DIR}/esp32s2.ld
64        MAIN_DEPENDENCY ${LD_DIR}/esp32s2.ld ${sdkconfig_header}
65        COMMENT "Generating linker script..."
66        VERBATIM)
67
68    add_custom_target(esp32s2_linker_script DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/esp32s2_out.ld)
69    add_dependencies(${COMPONENT_LIB} esp32s2_linker_script)
70endif()
71