|
| 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 "default_model.pte" CACHE STRING "Input PTE model file to embed") |
| 30 | + |
| 31 | +# Convert relative paths to absolute |
| 32 | +if(NOT IS_ABSOLUTE "${INPUT_MODEL}") |
| 33 | + set(INPUT_MODEL "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT_MODEL}") |
| 34 | +endif() |
| 35 | + |
| 36 | +message(STATUS "Using model file: ${INPUT_MODEL}") |
| 37 | + |
| 38 | +# Validate model file exists |
| 39 | +if(NOT EXISTS "${INPUT_MODEL}") |
| 40 | + message(FATAL_ERROR "Model file '${INPUT_MODEL}' does not exist.") |
| 41 | +endif() |
| 42 | + |
| 43 | +# Create model_pte.c if it doesn't exist |
| 44 | +set(MODEL_PTE_C "${CMAKE_CURRENT_SOURCE_DIR}/model_pte.c") |
| 45 | +if(NOT EXISTS "${MODEL_PTE_C}") |
| 46 | + file(WRITE "${MODEL_PTE_C}" |
| 47 | + "#include \"model_pte.h\"\n" |
| 48 | + "\n" |
| 49 | + "const uint8_t model_pte[] __attribute__((aligned(8))) = {\n" |
| 50 | + " // Model data will be injected here\n" |
| 51 | + "};\n" |
| 52 | + "\n" |
| 53 | + "const unsigned int model_pte_len = sizeof(model_pte);\n" |
| 54 | + ) |
| 55 | + message(STATUS "Created initial model_pte.c") |
| 56 | +endif() |
| 57 | + |
| 58 | +# Use a stamp file instead of declaring model_pte.c as OUTPUT |
| 59 | +set(MODEL_STAMP "${CMAKE_CURRENT_BINARY_DIR}/model_pte.stamp") |
| 60 | + |
| 61 | +add_custom_command( |
| 62 | + OUTPUT ${MODEL_STAMP} |
| 63 | + COMMAND python3 ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py |
| 64 | + --model ${INPUT_MODEL} |
| 65 | + --file ${MODEL_PTE_C} |
| 66 | + COMMAND ${CMAKE_COMMAND} -E touch ${MODEL_STAMP} |
| 67 | + DEPENDS ${INPUT_MODEL} ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py |
| 68 | + COMMENT "Injecting PTE data from '${INPUT_MODEL}' into model_pte.c" |
| 69 | + VERBATIM |
| 70 | +) |
| 71 | + |
| 72 | +# Target to ensure model injection happens |
| 73 | +add_custom_target(inject_model_data ALL DEPENDS ${MODEL_STAMP}) |
| 74 | + |
| 75 | +# Check if source files exist |
| 76 | +if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") |
| 77 | + message(FATAL_ERROR "main.cpp not found in ${CMAKE_CURRENT_SOURCE_DIR}") |
| 78 | +endif() |
| 79 | + |
| 80 | +# Create the executable |
| 81 | +add_executable(executorch_pico |
| 82 | + main.cpp |
| 83 | + ${MODEL_PTE_C} # Use the full path to be explicit |
| 84 | +) |
| 85 | + |
| 86 | +# Ensure model data is injected before building |
| 87 | +add_dependencies(executorch_pico inject_model_data) |
| 88 | + |
| 89 | +# Set correct target properties for Pico2/RP2350 |
| 90 | +set_target_properties( |
| 91 | + executorch_pico |
| 92 | + PROPERTIES |
| 93 | + SUFFIX ".elf" |
| 94 | + 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 |
| 104 | + -mcpu=cortex-m33 # Correct CPU for Pico2 |
| 105 | + -mfloat-abi=soft |
| 106 | + -mthumb |
| 107 | +) |
| 108 | + |
| 109 | +target_include_directories( |
| 110 | + executorch_pico |
| 111 | + PRIVATE |
| 112 | + ${EXECUTORCH_ROOT} |
| 113 | + ${EXECUTORCH_ROOT}/executorch/third-party |
| 114 | + ${EXECUTORCH_ROOT}/executorch/runtime/core/portable_type/c10 |
| 115 | +) |
| 116 | + |
| 117 | +target_compile_definitions( |
| 118 | + executorch_pico |
| 119 | + PRIVATE |
| 120 | + C10_USING_CUSTOM_GENERATED_MACROS |
| 121 | + EXECUTORCH_ENABLE_LOGGING=OFF |
| 122 | + EXECUTORCH_PAL_DEFAULT=minimal |
| 123 | +) |
| 124 | + |
| 125 | +# Optimization flags |
| 126 | +target_compile_options( |
| 127 | + executorch_pico PRIVATE |
| 128 | + -Os |
| 129 | + -ffunction-sections |
| 130 | + -fdata-sections |
| 131 | +) |
| 132 | + |
| 133 | +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") |
| 134 | + |
| 135 | +set(BAREMETAL_BUILD_DIR ${EXECUTORCH_ROOT}/executorch/cmake-out/) |
| 136 | + |
| 137 | +# Link ExecuTorch and Pico libraries |
| 138 | +target_link_libraries( |
| 139 | + executorch_pico |
| 140 | + PRIVATE |
| 141 | + ${BAREMETAL_BUILD_DIR}/lib/libexecutorch.a |
| 142 | + ${BAREMETAL_BUILD_DIR}/lib/libexecutorch_core.a |
| 143 | + -Wl,--whole-archive |
| 144 | + ${BAREMETAL_BUILD_DIR}/lib/libportable_ops_lib.a |
| 145 | + -Wl,--no-whole-archive |
| 146 | + ${BAREMETAL_BUILD_DIR}/lib/libportable_kernels.a |
| 147 | + pico_stdlib |
| 148 | + pico_stdio_usb |
| 149 | +) |
| 150 | + |
| 151 | +# Only add extra outputs if the target builds successfully |
| 152 | +if(TARGET executorch_pico) |
| 153 | + pico_add_extra_outputs(executorch_pico) |
| 154 | +endif() |
0 commit comments