1# ulp_embed_binary
2#
3# Create ULP binary and embed into the application.
4function(ulp_embed_binary app_name s_sources exp_dep_srcs)
5    if(NOT CMAKE_BUILD_EARLY_EXPANSION)
6        spaces2list(s_sources)
7        foreach(source ${s_sources})
8            get_filename_component(source ${source} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
9            list(APPEND sources ${source})
10        endforeach()
11
12        foreach(source ${sources})
13            get_filename_component(ps_source ${source} NAME_WE)
14            set(ps_output ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${ps_source}.ulp.S)
15            list(APPEND ps_sources ${ps_output})
16        endforeach()
17
18        set(ulp_artifacts_prefix ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name})
19
20        set(ulp_artifacts ${ulp_artifacts_prefix}.bin
21                            ${ulp_artifacts_prefix}.ld
22                            ${ulp_artifacts_prefix}.h)
23
24        set(ulp_artifacts_extras ${ulp_artifacts_prefix}.map
25                            ${ulp_artifacts_prefix}.sym
26                            ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/esp32.ulp.ld)
27
28        # Replace the separator for the list of ULP source files that will be passed to
29        # the external ULP project. This is a workaround to the bug https://public.kitware.com/Bug/view.php?id=16137.
30        string(REPLACE ";" "|" ulp_s_sources "${ulp_s_sources}")
31
32        idf_build_get_property(sdkconfig_header SDKCONFIG_HEADER)
33        idf_build_get_property(idf_path IDF_PATH)
34        idf_build_get_property(python PYTHON)
35        idf_build_get_property(extra_cmake_args EXTRA_CMAKE_ARGS)
36
37        if(IDF_TARGET STREQUAL "esp32")
38            set(TOOLCHAIN_FLAG ${idf_path}/components/ulp/cmake/toolchain-esp32-ulp.cmake)
39            set(ULP_IS_RISCV OFF)
40        endif()
41
42        if(IDF_TARGET STREQUAL "esp32s2")
43            if(CONFIG_ESP32S2_ULP_COPROC_RISCV STREQUAL "y")
44                set(TOOLCHAIN_FLAG ${idf_path}/components/ulp/cmake/toolchain-esp32s2-ulp-riscv.cmake)
45                set(ULP_IS_RISCV ON)
46            else()
47                set(TOOLCHAIN_FLAG ${idf_path}/components/ulp/cmake/toolchain-esp32s2-ulp.cmake)
48                set(ULP_IS_RISCV OFF)
49            endif()
50        endif()
51
52        externalproject_add(${app_name}
53                SOURCE_DIR ${idf_path}/components/ulp/cmake
54                BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${app_name}
55                INSTALL_COMMAND ""
56                CMAKE_ARGS  -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
57                            -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FLAG}
58                            -DULP_S_SOURCES=$<TARGET_PROPERTY:${app_name},ULP_SOURCES>
59                            -DULP_APP_NAME=${app_name}
60                            -DCOMPONENT_DIR=${COMPONENT_DIR}
61                            -DCOMPONENT_INCLUDES=$<TARGET_PROPERTY:${COMPONENT_TARGET},INTERFACE_INCLUDE_DIRECTORIES>
62                            -DIDF_PATH=${idf_path}
63                            -DSDKCONFIG_HEADER=${SDKCONFIG_HEADER}
64                            -DPYTHON=${python}
65                            -DULP_COCPU_IS_RISCV=${ULP_IS_RISCV}
66                            ${extra_cmake_args}
67                BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/${app_name} --target build
68                BUILD_BYPRODUCTS ${ulp_artifacts} ${ulp_artifacts_extras} ${ulp_ps_sources}
69                                ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}
70                BUILD_ALWAYS 1
71                )
72
73        set_property(TARGET ${app_name} PROPERTY ULP_SOURCES "${sources}")
74
75        spaces2list(exp_dep_srcs)
76        set_source_files_properties(${exp_dep_srcs} PROPERTIES OBJECT_DEPENDS ${ulp_artifacts})
77
78        include_directories(${CMAKE_CURRENT_BINARY_DIR}/${app_name})
79
80        add_custom_target(${app_name}_artifacts DEPENDS ${app_name})
81
82        add_dependencies(${COMPONENT_LIB} ${app_name}_artifacts)
83
84        target_linker_script(${COMPONENT_LIB} INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}.ld)
85        target_add_binary_data(${COMPONENT_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}.bin BINARY)
86    endif()
87endfunction()
88