|
| 1 | +# ############################################################################## |
| 2 | +# cmake/nuttx_add_rust.cmake |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 5 | +# license agreements. See the NOTICE file distributed with this work for |
| 6 | +# additional information regarding copyright ownership. The ASF licenses this |
| 7 | +# file to you under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | +# use this file except in compliance with the License. You may obtain a copy of |
| 9 | +# the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | +# License for the specific language governing permissions and limitations under |
| 17 | +# the License. |
| 18 | +# |
| 19 | +# ############################################################################## |
| 20 | + |
| 21 | +include(nuttx_parse_function_args) |
| 22 | + |
| 23 | +# ~~~ |
| 24 | +# Convert architecture type to Rust NuttX target |
| 25 | +# |
| 26 | +# Supported architectures: |
| 27 | +# - armv7a: armv7a-nuttx-eabi, armv7a-nuttx-eabihf |
| 28 | +# - thumbv6m: thumbv6m-nuttx-eabi |
| 29 | +# - thumbv7a: thumbv7a-nuttx-eabi, thumbv7a-nuttx-eabihf |
| 30 | +# - thumbv7m: thumbv7m-nuttx-eabi |
| 31 | +# - thumbv7em: thumbv7em-nuttx-eabihf |
| 32 | +# - thumbv8m.main: thumbv8m.main-nuttx-eabi, thumbv8m.main-nuttx-eabihf |
| 33 | +# - thumbv8m.base: thumbv8m.base-nuttx-eabi, thumbv8m.base-nuttx-eabihf |
| 34 | +# - riscv32: riscv32imc/imac/imafc-unknown-nuttx-elf |
| 35 | +# - riscv64: riscv64imac/imafdc-unknown-nuttx-elf |
| 36 | +# |
| 37 | +# Inputs: |
| 38 | +# ARCHTYPE - Architecture type (e.g. thumbv7m, riscv32) |
| 39 | +# ABITYPE - ABI type (e.g. eabi, eabihf) |
| 40 | +# CPUTYPE - CPU type (e.g. cortex-m4, sifive-e20) |
| 41 | +# |
| 42 | +# Output: |
| 43 | +# OUTPUT - Rust target triple (e.g. riscv32imac-unknown-nuttx-elf, |
| 44 | +# thumbv7m-nuttx-eabi, thumbv7em-nuttx-eabihf) |
| 45 | +# ~~~ |
| 46 | + |
| 47 | +function(nuttx_rust_target_triple ARCHTYPE ABITYPE CPUTYPE OUTPUT) |
| 48 | + if(ARCHTYPE MATCHES "thumb") |
| 49 | + if(ARCHTYPE MATCHES "thumbv8m") |
| 50 | + # Extract just the base architecture type (thumbv8m.main or thumbv8m.base) |
| 51 | + if(ARCHTYPE MATCHES "thumbv8m.main") |
| 52 | + set(ARCH_BASE "thumbv8m.main") |
| 53 | + elseif(ARCHTYPE MATCHES "thumbv8m.base") |
| 54 | + set(ARCH_BASE "thumbv8m.base") |
| 55 | + else() |
| 56 | + # Otherwise determine if we should use thumbv8m.main or thumbv8m.base |
| 57 | + # based on CPU type |
| 58 | + if(CPUTYPE MATCHES "cortex-m23") |
| 59 | + set(ARCH_BASE "thumbv8m.base") |
| 60 | + else() |
| 61 | + set(ARCH_BASE "thumbv8m.main") |
| 62 | + endif() |
| 63 | + endif() |
| 64 | + set(TARGET_TRIPLE "${ARCH_BASE}-nuttx-${ABITYPE}") |
| 65 | + else() |
| 66 | + set(TARGET_TRIPLE "${ARCHTYPE}-nuttx-${ABITYPE}") |
| 67 | + endif() |
| 68 | + elseif(ARCHTYPE STREQUAL "riscv32") |
| 69 | + if(CPUTYPE STREQUAL "sifive-e20") |
| 70 | + set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf") |
| 71 | + elseif(CPUTYPE STREQUAL "sifive-e31") |
| 72 | + set(TARGET_TRIPLE "riscv32imac-unknown-nuttx-elf") |
| 73 | + elseif(CPUTYPE STREQUAL "sifive-e76") |
| 74 | + set(TARGET_TRIPLE "riscv32imafc-unknown-nuttx-elf") |
| 75 | + else() |
| 76 | + set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf") |
| 77 | + endif() |
| 78 | + elseif(ARCHTYPE STREQUAL "riscv64") |
| 79 | + if(CPUTYPE STREQUAL "sifive-s51") |
| 80 | + set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf") |
| 81 | + elseif(CPUTYPE STREQUAL "sifive-u54") |
| 82 | + set(TARGET_TRIPLE "riscv64imafdc-unknown-nuttx-elf") |
| 83 | + else() |
| 84 | + set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf") |
| 85 | + endif() |
| 86 | + endif() |
| 87 | + set(${OUTPUT} |
| 88 | + ${TARGET_TRIPLE} |
| 89 | + PARENT_SCOPE) |
| 90 | +endfunction() |
| 91 | + |
| 92 | +# ~~~ |
| 93 | +# nuttx_add_rust |
| 94 | +# |
| 95 | +# Description: |
| 96 | +# Build a Rust crate and add it as a static library to the NuttX build system |
| 97 | +# |
| 98 | +# Example: |
| 99 | +# nuttx_add_rust( |
| 100 | +# CRATE_NAME |
| 101 | +# hello |
| 102 | +# CRATE_PATH |
| 103 | +# ${CMAKE_CURRENT_SOURCE_DIR}/hello |
| 104 | +# ) |
| 105 | +# ~~~ |
| 106 | + |
| 107 | +function(nuttx_add_rust) |
| 108 | + |
| 109 | + # parse arguments into variables |
| 110 | + nuttx_parse_function_args( |
| 111 | + FUNC |
| 112 | + nuttx_add_rust |
| 113 | + ONE_VALUE |
| 114 | + CRATE_NAME |
| 115 | + CRATE_PATH |
| 116 | + REQUIRED |
| 117 | + CRATE_NAME |
| 118 | + CRATE_PATH |
| 119 | + ARGN |
| 120 | + ${ARGN}) |
| 121 | + |
| 122 | + # Determine build profile based on CONFIG_DEBUG_FULLOPT |
| 123 | + if(CONFIG_DEBUG_FULLOPT) |
| 124 | + set(RUST_PROFILE "release") |
| 125 | + else() |
| 126 | + set(RUST_PROFILE "debug") |
| 127 | + endif() |
| 128 | + |
| 129 | + # Get the Rust target triple |
| 130 | + nuttx_rust_target_triple(${LLVM_ARCHTYPE} ${LLVM_ABITYPE} ${LLVM_CPUTYPE} |
| 131 | + RUST_TARGET) |
| 132 | + |
| 133 | + # Set up build directory in current binary dir |
| 134 | + set(RUST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CRATE_NAME}) |
| 135 | + set(RUST_LIB_PATH |
| 136 | + ${RUST_BUILD_DIR}/${RUST_TARGET}/${RUST_PROFILE}/lib${CRATE_NAME}.a) |
| 137 | + |
| 138 | + # Create build directory |
| 139 | + file(MAKE_DIRECTORY ${RUST_BUILD_DIR}) |
| 140 | + |
| 141 | + # Add a custom command to build the Rust crate |
| 142 | + add_custom_command( |
| 143 | + OUTPUT ${RUST_LIB_PATH} |
| 144 | + COMMAND |
| 145 | + cargo build --${RUST_PROFILE} -Zbuild-std=std,panic_abort --manifest-path |
| 146 | + ${CRATE_PATH}/Cargo.toml --target ${RUST_TARGET} --target-dir |
| 147 | + ${RUST_BUILD_DIR} |
| 148 | + COMMENT "Building Rust crate ${CRATE_NAME}" |
| 149 | + VERBATIM) |
| 150 | + |
| 151 | + # Add a custom target that depends on the built library |
| 152 | + add_custom_target(${CRATE_NAME}_build ALL DEPENDS ${RUST_LIB_PATH}) |
| 153 | + |
| 154 | + # Add imported library target |
| 155 | + add_library(${CRATE_NAME} STATIC IMPORTED GLOBAL) |
| 156 | + set_target_properties(${CRATE_NAME} PROPERTIES IMPORTED_LOCATION |
| 157 | + ${RUST_LIB_PATH}) |
| 158 | + |
| 159 | + # Add the Rust library to NuttX build |
| 160 | + nuttx_add_extra_library(${RUST_LIB_PATH}) |
| 161 | + |
| 162 | + # Ensure the Rust library is built before linking |
| 163 | + add_dependencies(${CRATE_NAME} ${CRATE_NAME}_build) |
| 164 | + |
| 165 | +endfunction() |
0 commit comments