Skip to content

examples/hello_rust: Add -O flag #2333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2024
Merged

examples/hello_rust: Add -O flag #2333

merged 1 commit into from
Mar 21, 2024

Conversation

rushabhvg
Copy link
Contributor

@rushabhvg rushabhvg commented Mar 18, 2024

Summary

The current issue with the "hello_rust_main.rs" file not building successfully is addressed by this pull request, which aims to enhance the build process by incorporating the "-O" flag into the rustc flags. This adjustment is crucial for optimizing the code at level 2, ensuring better performance and efficiency. The modifications are made in the Makefile located at apps/examples/hello_rust/Makefile

The discussion surrounding the Rust build issue can be found in the Rust-Lang issue #245, where various aspects of the problem and potential solutions are discussed. This issue highlights the importance of optimization flags in the Rust compiler and how they can impact the build process and the resulting application's performance.

The "-O" flag in rustc is used to specify the optimization level for the compiler. According to the Rust documentation, the optimization levels in rustc are loosely based on the ones that GCC has. The "-O" level, in particular, enables a broader range of optimizations compared to "-opt-level=1", aiming to provide a good balance between performance and compilation time. For more information: Rust Documentation

Incorporating the "-O" flag into the rustc flags for the "hello_rust_main.rs" file is a move to ensure that the code is optimized at a higher level, potentially leading to improved performance and efficiency. This change is expected to address the build issue by enabling more aggressive optimizations during the compilation process.

Impact

This pull request (PR) introduces a significant improvement to the build process for Rust files within the NuttX environment, ensuring successful compilation.
The impact of this PR is primarily focused on the successful build of Rust files, with no adverse effects on the NuttX kernel or the NuttX applications. This ensures that the optimization does not introduce any unintended side effects or compatibility issues within the broader NuttX ecosystem.

Testing

extern "C" {
    pub fn printf(format: *const u8, ...) -> i32;
    pub fn puts(s: *const core::ffi::c_char) -> core::ffi::c_int;
    pub fn fgets(
        buf: *mut core::ffi::c_char,
        n: core::ffi::c_int,
        stream: *mut core::ffi::c_void
    ) -> *mut core::ffi::c_char;
    pub fn lib_get_stream(fd: core::ffi::c_int) -> *mut core::ffi::c_void;
}
printf(b"Please give your output:\n\0" as *const u8);
let stdin: *mut core::ffi::c_void = lib_get_stream(0);
let mut buf: [core::ffi::c_char; 256] = [0; 256];
if !fgets(
    &mut buf[0], 
    buf.len() as i32 - 1,
    stdin
).is_null() {
    printf(b"You entered...\n\0" as *const u8);
    puts(&buf[0]);
}
rushabhvg@rushabhvg-laptop:~/nuttxspace/nuttx$ make
LD: nuttx
riscv64-unknown-elf-ld: warning: /home/rushabhvg/nuttxspace/nuttx/staging/libapps.a(hello_rust_main.rs.home.rushabhvg.nuttxspace.apps.examples.hello_rust_1.o): mis-matched ISA version 2.1 for 'i' extension, the output version is 2.0
...
riscv64-unknown-elf-ld: /home/rushabhvg/nuttxspace/nuttx/staging/libapps.a(hello_rust_main.rs.home.rushabhvg.nuttxspace.apps.examples.hello_rust_1.o): in function `no symbol':
/home/rushabhvg/nuttxspace/apps/examples/hello_rust/hello_rust_main.rs:127: undefined reference to `core::panicking::panic'
make[1]: *** [Makefile:179: nuttx] Error 1
make: *** [tools/Unix.mk:546: nuttx] Error 2\
  • Adding "-O" will solve the linking problem: examples\hello_rust\Makefile
RUSTFLAGS += -C panic=abort -O

We tested with QEMU:

qemu-system-riscv32 \
  -semihosting \
  -M virt,aclint=on \
  -cpu rv32 \
  -smp 8 \
  -bios none \
  -kernel nuttx \
  -nographic

The Rust build log for NuttX shows that rust apps are build successfully.

@lupyuen lupyuen self-requested a review March 19, 2024 05:54
@lupyuen lupyuen changed the title apps/examples/hello_rust/Makefile: Add -O flag examples/hello_rust: Add -O flag Mar 20, 2024
@lupyuen lupyuen marked this pull request as ready for review March 20, 2024 13:51
Copy link
Member

@lupyuen lupyuen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks Great To Me, I tested OK on macOS, Welcome to NuttX! For Future PRs: Let's keep the description in point form, short and concise. It's easier for our review. Thanks! :-)

Check out the Sample PRs: https://lupyuen.github.io/articles/pr

@rushabhvg
Copy link
Contributor Author

Okay, point noted down, from now onwards I will be concise and write in bullet-points.

@xiaoxiang781216 xiaoxiang781216 merged commit b311c46 into apache:master Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants