|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the LICENSE |
| 5 | +# file in the root directory of this source tree. |
| 6 | + |
| 7 | +cmake_minimum_required(VERSION 3.13) |
| 8 | + |
| 9 | +# Include FetchContent for Pico SDK |
| 10 | +include(FetchContent) |
| 11 | + |
| 12 | +FetchContent_Declare( |
| 13 | + pico_sdk |
| 14 | + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk.git |
| 15 | + GIT_TAG 2.0.0 |
| 16 | +) |
| 17 | + |
| 18 | +FetchContent_MakeAvailable(pico_sdk) |
| 19 | +set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) |
| 20 | +include(${PICO_SDK_PATH}/pico_sdk_init.cmake) |
| 21 | + |
| 22 | +project(executorch_pico C CXX ASM) |
| 23 | +pico_sdk_init() |
| 24 | + |
| 25 | +set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) |
| 26 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 27 | + |
| 28 | +# Configure input model file |
| 29 | +set(INPUT_MODEL |
| 30 | + "default_model.pte" |
| 31 | + CACHE STRING "Input PTE model file to embed" |
| 32 | +) |
| 33 | + |
| 34 | +# Convert relative paths to absolute |
| 35 | +if(NOT IS_ABSOLUTE "${INPUT_MODEL}") |
| 36 | + set(INPUT_MODEL "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT_MODEL}") |
| 37 | +endif() |
| 38 | + |
| 39 | +message(STATUS "Using model file: ${INPUT_MODEL}") |
| 40 | + |
| 41 | +# Validate model file exists |
| 42 | +if(NOT EXISTS "${INPUT_MODEL}") |
| 43 | + message(FATAL_ERROR "Model file '${INPUT_MODEL}' does not exist.") |
| 44 | +endif() |
| 45 | + |
| 46 | +# Create model_pte.c if it doesn't exist |
| 47 | +set(MODEL_PTE_C "${CMAKE_CURRENT_SOURCE_DIR}/model_pte.c") |
| 48 | +if(NOT EXISTS "${MODEL_PTE_C}") |
| 49 | + file( |
| 50 | + WRITE "${MODEL_PTE_C}" |
| 51 | + "#include \"model_pte.h\"\n" |
| 52 | + "\n" |
| 53 | + "const uint8_t model_pte[] __attribute__((aligned(8))) = {\n" |
| 54 | + " // Model data will be injected here\n" |
| 55 | + "};\n" |
| 56 | + "\n" |
| 57 | + "const unsigned int model_pte_len = sizeof(model_pte);\n" |
| 58 | + ) |
| 59 | + message(STATUS "Created initial model_pte.c") |
| 60 | +endif() |
| 61 | + |
| 62 | +# Use a stamp file instead of declaring model_pte.c as OUTPUT |
| 63 | +set(MODEL_STAMP "${CMAKE_CURRENT_BINARY_DIR}/model_pte.stamp") |
| 64 | + |
| 65 | +add_custom_command( |
| 66 | + OUTPUT ${MODEL_STAMP} |
| 67 | + COMMAND python3 ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py |
| 68 | + --model ${INPUT_MODEL} --file ${MODEL_PTE_C} |
| 69 | + COMMAND ${CMAKE_COMMAND} -E touch ${MODEL_STAMP} |
| 70 | + DEPENDS ${INPUT_MODEL} |
| 71 | + ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py |
| 72 | + COMMENT "Injecting PTE data from '${INPUT_MODEL}' into model_pte.c" |
| 73 | + VERBATIM |
| 74 | +) |
| 75 | + |
| 76 | +# Target to ensure model injection happens |
| 77 | +add_custom_target(inject_model_data ALL DEPENDS ${MODEL_STAMP}) |
| 78 | + |
| 79 | +# Check if source files exist |
| 80 | +if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") |
| 81 | + message(FATAL_ERROR "main.cpp not found in ${CMAKE_CURRENT_SOURCE_DIR}") |
| 82 | +endif() |
| 83 | + |
| 84 | +# Create the executable |
| 85 | +add_executable( |
| 86 | + executorch_pico main.cpp ${MODEL_PTE_C} # Use the full path to be explicit |
| 87 | +) |
| 88 | + |
| 89 | +# Ensure model data is injected before building |
| 90 | +add_dependencies(executorch_pico inject_model_data) |
| 91 | + |
| 92 | +# Set correct target properties for Pico2/RP2350 |
| 93 | +set_target_properties( |
| 94 | + executorch_pico PROPERTIES SUFFIX ".elf" OUTPUT_NAME "executorch_pico" |
| 95 | +) |
| 96 | + |
| 97 | +# Configure Pico-specific settings |
| 98 | +pico_enable_stdio_usb(executorch_pico 1) |
| 99 | +pico_enable_stdio_uart(executorch_pico 0) |
| 100 | + |
| 101 | +# Set correct flags for Pico2 (RP2350) |
| 102 | +target_compile_options( |
| 103 | + executorch_pico PRIVATE -mcpu=cortex-m33 -mfloat-abi=soft -mthumb |
| 104 | +) |
| 105 | + |
| 106 | +target_include_directories( |
| 107 | + executorch_pico |
| 108 | + PRIVATE ${EXECUTORCH_ROOT} ${EXECUTORCH_ROOT}/executorch/third-party |
| 109 | + ${EXECUTORCH_ROOT}/executorch/runtime/core/portable_type/c10 |
| 110 | +) |
| 111 | + |
| 112 | +target_compile_definitions( |
| 113 | + executorch_pico |
| 114 | + PRIVATE C10_USING_CUSTOM_GENERATED_MACROS EXECUTORCH_ENABLE_LOGGING=OFF |
| 115 | + EXECUTORCH_PAL_DEFAULT=minimal |
| 116 | +) |
| 117 | + |
| 118 | +# Optimization flags |
| 119 | +target_compile_options( |
| 120 | + executorch_pico PRIVATE -Os -ffunction-sections -fdata-sections |
| 121 | +) |
| 122 | + |
| 123 | +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") |
| 124 | + |
| 125 | +set(BAREMETAL_BUILD_DIR ${EXECUTORCH_ROOT}/executorch/cmake-out/) |
| 126 | + |
| 127 | +# Link ExecuTorch and Pico libraries |
| 128 | +target_link_libraries( |
| 129 | + executorch_pico |
| 130 | + PRIVATE ${BAREMETAL_BUILD_DIR}/lib/libexecutorch.a |
| 131 | + ${BAREMETAL_BUILD_DIR}/lib/libexecutorch_core.a |
| 132 | + -Wl,--whole-archive |
| 133 | + ${BAREMETAL_BUILD_DIR}/lib/libportable_ops_lib.a |
| 134 | + -Wl,--no-whole-archive |
| 135 | + ${BAREMETAL_BUILD_DIR}/lib/libportable_kernels.a |
| 136 | + pico_stdlib |
| 137 | + pico_stdio_usb |
| 138 | +) |
| 139 | + |
| 140 | +# Only add extra outputs if the target builds successfully |
| 141 | +if(TARGET executorch_pico) |
| 142 | + pico_add_extra_outputs(executorch_pico) |
| 143 | +endif() |
0 commit comments