diff --git a/.ci/scripts/setup-arm-baremetal-tools.sh b/.ci/scripts/setup-arm-baremetal-tools.sh index 454b9f336e9..e27c52f5125 100755 --- a/.ci/scripts/setup-arm-baremetal-tools.sh +++ b/.ci/scripts/setup-arm-baremetal-tools.sh @@ -8,4 +8,4 @@ # Setup arm example environment (including TOSA tools) git config --global user.email "github_executorch@arm.com" git config --global user.name "Github Executorch" -bash examples/arm/setup.sh --i-agree-to-the-contained-eula +bash examples/arm/setup.sh --i-agree-to-the-contained-eula ${@:-} diff --git a/.github/workflows/build-presets.yml b/.github/workflows/build-presets.yml index 1a40c6ef68d..ae8ff8b7493 100644 --- a/.github/workflows/build-presets.yml +++ b/.github/workflows/build-presets.yml @@ -34,6 +34,45 @@ jobs: ${CONDA_RUN} cmake --preset ${{ matrix.preset }} ${CONDA_RUN} cmake --build cmake-out -j$(( $(sysctl -n hw.ncpu) - 1 )) + zephyr: + uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main + strategy: + fail-fast: false + matrix: + preset: [zephyr] + with: + job-name: build + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + runner: linux.2xlarge + docker-image: executorch-ubuntu-22.04-arm-sdk + submodules: recursive + timeout: 90 + script: | + set -eux + # The generic Linux job chooses to use base env, not the one setup by the image + CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]") + conda activate "${CONDA_ENV}" + + ./install_requirements.sh > /dev/null + + # Download toolchain + toolchain_url="https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/toolchain_linux-x86_64_arm-zephyr-eabi.tar.xz" + toolchain_dir="arm-zephyr-eabi" + curl --output "${toolchain_dir}.tar.xz" -L "${toolchain_url}" + + # Verify download + echo "93128be0235cf5cf5f1ee561aa6eac5f ${toolchain_dir}.tar.xz" > arm-zephyr-eabi.md5 + md5sum -c --strict arm-zephyr-eabi.md5 + + # Extract and install to PATH + tar xf "${toolchain_dir}.tar.xz" + rm -f "${toolchain_dir}.tar.xz" + toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)" + export PATH=$PATH:${toolchain_bin_path} + + # Build Arm Zephyr Preset + cmake --preset ${{ matrix.preset }} + cmake --build cmake-out -j$(( $(nproc) - 1 )) linux: uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main strategy: diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index 568813b3858..4baa4ec3b75 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -223,6 +223,10 @@ jobs: permissions: id-token: write contents: read + strategy: + matrix: + os: [bare_metal, zephyr-preset] + fail-fast: false with: runner: linux.2xlarge docker-image: executorch-ubuntu-22.04-arm-sdk @@ -234,35 +238,62 @@ jobs: CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]") conda activate "${CONDA_ENV}" + cxx_flags="-fno-exceptions -fno-rtti -Wall -Werror -Wno-int-in-bool-context -DET_HAVE_PREAD=0" + setup_script_args="" + if [[ ${{ matrix.os}} == "bare_metal" ]]; then + toolchain_prefix=arm-none-eabi- + threshold="103268" # ~100KiB + toolchain_cmake=examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake + elif [[ ${{ matrix.os}} == "zephyr-preset" ]]; then + setup_script_args="--target-toolchain zephyr" + toolchain_prefix=arm-zephyr-eabi- + threshold="133120" # should be ~125KB, set threshold to 130KB + toolchain_cmake=examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake + else + echo "Fail unsupport OS selection ${{ matrix.os }}" + exit 1 + fi + source .ci/scripts/utils.sh install_executorch "--use-pt-pinned-commit" - .ci/scripts/setup-arm-baremetal-tools.sh + .ci/scripts/setup-arm-baremetal-tools.sh ${setup_script_args} source examples/arm/ethos-u-scratch/setup_path.sh - # User baremetal toolchain - arm-none-eabi-c++ --version - toolchain_cmake=examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake + # User toolchain + ${toolchain_prefix}c++ --version + + # Setup cmake target to desired toolchain toolchain_cmake=$(realpath ${toolchain_cmake}) - # Build and test size test - bash test/build_size_test.sh "-DCMAKE_TOOLCHAIN_FILE=${toolchain_cmake} -DEXECUTORCH_BUILD_ARM_BAREMETAL=ON" + # Build and run size test + if [[ ${{ matrix.os}} == "bare_metal" ]]; then + bash test/build_size_test.sh "-DCMAKE_TOOLCHAIN_FILE=${toolchain_cmake} -DEXECUTORCH_BUILD_ARM_BAREMETAL=ON" + elif [[ ${{ matrix.os}} == "zephyr-preset" ]]; then + CXXFLAGS=${cxx_flags} cmake --preset zephyr -DCMAKE_BUILD_TYPE=Release -DEXECUTORCH_OPTIMIZE_SIZE=ON -DCMAKE_INSTALL_PREFIX=cmake-out -Bcmake-out . + cmake --build cmake-out -j9 --target install --config Release + CXXFLAGS=${cxx_flags} cmake -DCMAKE_TOOLCHAIN_FILE=${toolchain_cmake} -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=cmake-out -Bcmake-out/test test + cmake --build cmake-out/test -j9 --config Release + else + echo "Fail unsupport OS selection ${{ matrix.os }}" + exit 1 + fi + elf="cmake-out/test/size_test" # Dump basic info ls -al ${elf} - arm-none-eabi-size ${elf} + ${toolchain_prefix}size ${elf} - # Dump symbols + # Dump symbol python .github/scripts/run_nm.py -e ${elf} - python .github/scripts/run_nm.py -e ${elf} -f "executorch" -p "arm-none-eabi-" - python .github/scripts/run_nm.py -e ${elf} -f "executorch_text" -p "arm-none-eabi-" + python .github/scripts/run_nm.py -e ${elf} -f "executorch" -p "${toolchain_prefix}" + python .github/scripts/run_nm.py -e ${elf} -f "executorch_text" -p "${toolchain_prefix}" # Add basic guard - TODO: refine this! - arm-none-eabi-strip ${elf} + ${toolchain_prefix}strip ${elf} output=$(ls -la ${elf}) arr=($output) size=${arr[4]} - threshold="103268" # ~100KiB echo "size: $size, threshold: $threshold" if [[ "$size" -le "$threshold" ]]; then echo "Success $size <= $threshold" diff --git a/CMakePresets.json b/CMakePresets.json index 9ea91fab343..8cb7fce482f 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -104,6 +104,17 @@ "Windows" ] } + }, + { + "name": "zephyr", + "displayName": "Build everything buildable on Zephyr RTOS", + "inherits": [ + "common" + ], + "cacheVariables": { + "EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/zephyr.cmake", + "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake" + } } ] } diff --git a/examples/arm/setup.sh b/examples/arm/setup.sh index 105abb33699..80a7f5ad721 100755 --- a/examples/arm/setup.sh +++ b/examples/arm/setup.sh @@ -242,6 +242,7 @@ function select_toolchain() { fi echo "[main] Info selected ${toolchain_dir} for ${ARCH} - ${OS} platform" } + function setup_toolchain() { # Download and install the arm toolchain (default is arm-none-eabi) # setting --target-toolchain to zephyr sets this to arm-zephyr-eabi diff --git a/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake b/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake new file mode 100644 index 00000000000..8e1a478a6cb --- /dev/null +++ b/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake @@ -0,0 +1,106 @@ +# +# Copyright (c) 2020-2022 Arm Limited. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the License); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Copied this file from core_platform/cmake/toolchain/arm-non-eabi-gcc.cmake And +# modified to align better with cs300 platform + +set(TARGET_CPU + "cortex-m55" + CACHE STRING "Target CPU" +) +string(TOLOWER ${TARGET_CPU} CMAKE_SYSTEM_PROCESSOR) + +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_C_COMPILER "arm-zephyr-eabi-gcc") +set(CMAKE_CXX_COMPILER "arm-zephyr-eabi-g++") +set(CMAKE_ASM_COMPILER "arm-zephyr-eabi-gcc") +set(CMAKE_LINKER "arm-zephyr-eabi-ld") + +set(CMAKE_EXECUTABLE_SUFFIX ".elf") +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +# Select C/C++ version +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +set(GCC_CPU ${CMAKE_SYSTEM_PROCESSOR}) +string(REPLACE "cortex-m85" "cortex-m55" GCC_CPU ${GCC_CPU}) + +# Compile options +add_compile_options( + -mcpu=${GCC_CPU} -mthumb "$<$:-gdwarf-3>" + "$<$:-fno-unwind-tables;-fno-rtti;-fno-exceptions>" + -fdata-sections -ffunction-sections +) + +# Compile defines +add_compile_definitions("$<$>:NDEBUG>") + +# Link options +add_link_options(-mcpu=${GCC_CPU} -mthumb) + +if(SEMIHOSTING) + add_link_options(--specs=rdimon.specs) +else() + add_link_options(--specs=nosys.specs) +endif() + +# Set floating point unit +if(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+fp") + set(FLOAT hard) +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+nofp") + set(FLOAT soft) +elseif( + CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m33(\\+|$)" + OR CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m55(\\+|$)" + OR CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m85(\\+|$)" +) + set(FLOAT hard) +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m4(\\+|$)" + OR CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m7(\\+|$)" +) + set(FLOAT hard) + set(FPU_CONFIG "fpv4-sp-d16") + add_compile_options(-mfpu=${FPU_CONFIG}) + add_link_options(-mfpu=${FPU_CONFIG}) +else() + set(FLOAT soft) +endif() + +if(FLOAT) + add_compile_options(-mfloat-abi=${FLOAT}) + add_link_options(-mfloat-abi=${FLOAT}) +endif() + +add_link_options(LINKER:--nmagic,--gc-sections) + +# Compilation warnings +add_compile_options( + # -Wall -Wextra -Wcast-align -Wdouble-promotion -Wformat + # -Wmissing-field-initializers -Wnull-dereference -Wredundant-decls -Wshadow + # -Wswitch -Wswitch-default -Wunused -Wno-redundant-decls + -Wno-stringop-overread + -Wno-error=format= + -Wno-error=maybe-uninitialized + -Wno-error=deprecated-declarations + -Wno-error=shift-count-overflow + -Wno-psabi +) diff --git a/tools/cmake/preset/zephyr.cmake b/tools/cmake/preset/zephyr.cmake new file mode 100644 index 00000000000..a78af0ea44a --- /dev/null +++ b/tools/cmake/preset/zephyr.cmake @@ -0,0 +1,33 @@ + +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +set_overridable_option(EXECUTORCH_BUILD_COREML OFF) +set_overridable_option(EXECUTORCH_ENABLE_EVENT_TRACER OFF) +set_overridable_option(EXECUTORCH_BUILD_KERNELS_CUSTOM OFF) +set_overridable_option(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_LLM OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_MODULE OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_TRAINING OFF) +set_overridable_option(EXECUTORCH_BUILD_EXTENSION_APPLE OFF) +set_overridable_option(EXECUTORCH_BUILD_MPS OFF) +set_overridable_option(EXECUTORCH_BUILD_NEURON OFF) +set_overridable_option(EXECUTORCH_BUILD_OPENVINO OFF) +set_overridable_option(EXECUTORCH_BUILD_PYBIND OFF) +set_overridable_option(EXECUTORCH_BUILD_QNN OFF) +set_overridable_option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED OFF) +set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED OFF) +set_overridable_option(EXECUTORCH_BUILD_DEVTOOLS OFF) +set_overridable_option(EXECUTORCH_BUILD_TESTS OFF) +set_overridable_option(EXECUTORCH_BUILD_XNNPACK OFF) +set_overridable_option(EXECUTORCH_BUILD_VULKAN OFF) +set_overridable_option(EXECUTORCH_BUILD_PORTABLE_OPS ON) +set_overridable_option(EXECUTORCH_BUILD_CADENCE OFF) +set_overridable_option(EXECUTORCH_BUILD_PTHREADPOOL OFF) +set_overridable_option(EXECUTORCH_BUILD_CPUINFO OFF) +set_overridable_option(EXECUTORCH_USE_CPP_CODE_COVERAGE OFF)