From c1757660a6875e84ed4cc07b69a8126775f5cef5 Mon Sep 17 00:00:00 2001 From: Piet Date: Sat, 19 Feb 2022 14:46:44 +0100 Subject: [PATCH] apps: hello_rust example program --- examples/hello_rust/Kconfig | 29 +++++++++++++ examples/hello_rust/Make.defs | 23 +++++++++++ examples/hello_rust/Makefile | 34 +++++++++++++++ examples/hello_rust/hello_rust_main.rs | 57 ++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 examples/hello_rust/Kconfig create mode 100644 examples/hello_rust/Make.defs create mode 100644 examples/hello_rust/Makefile create mode 100644 examples/hello_rust/hello_rust_main.rs diff --git a/examples/hello_rust/Kconfig b/examples/hello_rust/Kconfig new file mode 100644 index 000000000..92e3b1f34 --- /dev/null +++ b/examples/hello_rust/Kconfig @@ -0,0 +1,29 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_HELLO_RUST + tristate "\"Hello, Rust!\" example" + default n + ---help--- + Enable the \"Hello, Rust!\" example + +if EXAMPLES_HELLO_RUST + +config EXAMPLES_HELLO_RUST_PROGNAME + string "Program name" + default "hello_rust" + ---help--- + This is the name of the program that will be used when the + program is installed. + +config EXAMPLES_HELLO_RUST_PRIORITY + int "Hello Rust task priority" + default 100 + +config EXAMPLES_HELLO_RUST_STACKSIZE + int "Hello Rust stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/examples/hello_rust/Make.defs b/examples/hello_rust/Make.defs new file mode 100644 index 000000000..245662e68 --- /dev/null +++ b/examples/hello_rust/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/hello_rust/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you 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 +# +# http://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. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_HELLO_RUST),) +CONFIGURED_APPS += $(APPDIR)/examples/hello_rust +endif diff --git a/examples/hello_rust/Makefile b/examples/hello_rust/Makefile new file mode 100644 index 000000000..6a0b214fd --- /dev/null +++ b/examples/hello_rust/Makefile @@ -0,0 +1,34 @@ +############################################################################ +# apps/examples/hello_rust/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you 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 +# +# http://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. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# Hello, Rust! built-in application info + +PROGNAME = $(CONFIG_EXAMPLES_HELLO_RUST_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_HELLO_RUST_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_HELLO_RUST_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_HELLO_RUST) + +# Hello, Rust! Example + +MAINSRC = hello_rust_main.rs + +include $(APPDIR)/Application.mk diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs new file mode 100644 index 000000000..6d7aaa59e --- /dev/null +++ b/examples/hello_rust/hello_rust_main.rs @@ -0,0 +1,57 @@ +/**************************************************************************** + * apps/examples/hello_rust/hello_rust_main.rs + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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 + * + * http://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. + * + ****************************************************************************/ + +/**************************************************************************** + * Attributes + ****************************************************************************/ + +#![no_main] + +/**************************************************************************** + * Externs + ****************************************************************************/ + +extern "C" +{ + pub fn printf(format: *const u8, ...) -> i32; +} + +/**************************************************************************** + * Public functions + ****************************************************************************/ + +/**************************************************************************** + * hello_rust_main + ****************************************************************************/ + +#[no_mangle] +pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 +{ + /* "Hello, Rust!!" using printf() from libc */ + + unsafe + { + printf(b"Hello, Rust!!\n\0" as *const u8); + } + + /* exit with status 0 */ + + 0 +}