Skip to content
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

initial Rust support #5740

Closed
wants to merge 7 commits into from

Conversation

kaspar030
Copy link
Contributor

@kaspar030 kaspar030 commented Aug 9, 2016

"Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety."

Sounds like exactly what we need. ;)

This PR integrates Rust's build system (Cargo) into RIOT and makes it possible to write RIOT applications in Rust.

Here's an accompanying Rust library which provides (currently more than incomplete) RIOT API bindings to Rust.

This works currently only on Sam R21, but adding other Cortex-M platforms should be easy.
Currently this works only on Cortex-M platforms (no native!). Tested on samr21-xpro and nucleo-f334.

@kaspar030 kaspar030 added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Aug 9, 2016
@kaspar030 kaspar030 force-pushed the add_initial_rust_support branch from f2e2eea to 2025005 Compare August 9, 2016 18:07
@@ -262,7 +262,7 @@ ifeq (,$(RIOTNOLINK))
ifeq ($(BUILDOSXNATIVE),1)
$(AD)$(if $(CPPMIX),$(CXX),$(LINK)) $(UNDEF) -o $(ELFFILE) $$(find $(BASELIBS) -size +8c) $(LINKFLAGS) $(LINKFLAGPREFIX)-no_pie
else
$(AD)$(if $(CPPMIX),$(CXX),$(LINK)) $(UNDEF) -o $(ELFFILE) $(LINKFLAGPREFIX)--start-group $(BASELIBS) -lm $(LINKFLAGPREFIX)--end-group $(LINKFLAGPREFIX)-Map=$(BINDIR)$(APPLICATION).map $(LINKFLAGPREFIX)--cref $(LINKFLAGS)
$(AD)$(if $(CPPMIX),$(CXX),$(LINK)) $(UNDEF) -o $(ELFFILE) $(LINKFLAGPREFIX)--start-group $(BASELIBS) -lm $(wildcard $(BINDIR)/_extra_libs/*.a) $(LINKFLAGPREFIX)--end-group $(LINKFLAGPREFIX)-Map=$(BINDIR)$(APPLICATION).map $(LINKFLAGPREFIX)--cref $(LINKFLAGS)
Copy link
Contributor

Choose a reason for hiding this comment

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

$(wildcard) is evaluated as the rule is read, not when the rule is executed. You need to use $$(find ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, but it worked. :)
I think a plain wildcard will work fine, as the shell can evaluate it.

@Kijewski
Copy link
Contributor

Kijewski commented Aug 9, 2016

How does rustc work? Does it compile to C / C++ as an intermediate target or does it compile directly to an object / assembly?

@kaspar030
Copy link
Contributor Author

How does rustc work? Does it compile to C / C++ as an intermediate target or does it compile directly to an object / assembly?

Rustc uses llvm as code generator. The the cross-compiling version of rust's build system Cargo, xargo, instructs rustc to emit object files and .a archives, then usually calls gcc as linker.
I wrote a (more than ugly) linker stub that just checks if any argument is a .o or .a file and puts that in $(BINDIR)/_extra_libs, and hooked that up into xargo's machine description files.

@@ -0,0 +1,21 @@
#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

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

Iff (sic, please argue for it) bash is really needed, at least invoke it like #!/usr/bin/env bash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see below.

Copy link
Member

Choose a reason for hiding this comment

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

At least you can be nice to non-linux people and change to #!/usr/bin/env bash as requested ;-)

@kaspar030 kaspar030 force-pushed the add_initial_rust_support branch from bb0eedf to cb33876 Compare October 7, 2016 11:27
@kaspar030
Copy link
Contributor Author

  • rebased

Anyone interested in reviewing this?

@miri64 miri64 added this to the Release 2016.10 milestone Oct 18, 2016
@miri64
Copy link
Member

miri64 commented Oct 18, 2016

Maybe someone™ is willing to review this at the next Hack'n'ACK ;-)

@miri64 miri64 self-assigned this Oct 18, 2016
@miri64 miri64 modified the milestones: Release 2017.01, Release 2016.10 Oct 31, 2016
@miri64
Copy link
Member

miri64 commented Oct 31, 2016

Feature freeze -> postponed.

@nmeum
Copy link
Member

nmeum commented Nov 18, 2016

@kaspar030 Is there any particular reason why you wrote that „ugly” linker stub instead of using arm-none-eabi-gcc as a linker in the cargo json files instead? Besides wouldn't it make more sense to move the json files to cpu/ or boards/ instead of keeping them in dist/?

@kaspar030
Copy link
Contributor Author

@kaspar030 Is there any particular reason why you wrote that „ugly” linker stub instead of using arm-none-eabi-gcc as a linker in the cargo json files instead?

Yes, cargo tries to call the linker, but doesn't know anything about RIOT's linking process, thus wouldn't link a correct binary. The linker stub just fetches all objects and archives cargo wants to link (all rust code), and puts them in place so the RIOT linking process can pick them up.

Besides wouldn't it make more sense to move the json files to cpu/ or boards/ instead of keeping them in dist/?

Maybe. Then again there'd be a target specific path to be set somewhere (AFAIR there's a search path for the target specs), compared to one that is identical for all platforms.

@kaspar030 kaspar030 force-pushed the add_initial_rust_support branch from cb33876 to 7570d1f Compare November 18, 2016 15:42
@kaspar030
Copy link
Contributor Author

  • rebased

@nmeum
Copy link
Member

nmeum commented Nov 26, 2016

I started working on rust support for riot as well. My version is very different of yours though, I decided to closely integrated rust into the existing build process instead of using xargo or cargo. I simply adjusted the existing Makefiles and decided to compile the rust code with make thus my version doesn't introduce a separate build system and doesn't need any linker hacks. Furthermore since I don't use xargo it is also possible to build code for the native family. Would you be interested in seeing my changes?

nmeum added a commit to nmeum/RIOT that referenced this pull request Nov 28, 2016
The idea is to integrate rust into the existing build system as well as
possible and to not use a separate buildsystem like cargo or xargo.

Makefile.base has been modified to include support for main.rs and
lib.rs files and a Makefile.rust has been added which sets a few rust
related variables.

This is very loosly based on RIOT-OS#5740
nmeum added a commit to nmeum/RIOT that referenced this pull request Nov 29, 2016
The idea is to integrate rust into the existing build system as well as
possible and to not use a separate buildsystem like cargo or xargo.

Makefile.base has been modified to include support for main.rs and
lib.rs files and a Makefile.rust has been added which sets a few rust
related variables.

This is very loosly based on RIOT-OS#5740
nmeum added a commit to nmeum/RIOT that referenced this pull request Nov 29, 2016
The idea is to integrate rust into the existing build system as well as
possible and to not use a separate buildsystem like cargo or xargo.

Makefile.base has been modified to include support for main.rs and
lib.rs files and a Makefile.rust has been added which sets a few rust
related variables.

This is very loosly based on RIOT-OS#5740
nmeum added a commit to nmeum/RIOT that referenced this pull request Nov 29, 2016
The idea is to integrate rust into the existing build system as well as
possible and to not use a separate buildsystem like cargo or xargo.

Makefile.base has been modified to include support for main.rs and
lib.rs files and a Makefile.rust has been added which sets a few rust
related variables.

This is very loosly based on RIOT-OS#5740
@BafDyce
Copy link

BafDyce commented Dec 21, 2016

Sorry if this does not belong here, but I have problems compiling the rust example.

Upon running the make command I get the following error:

dau@mobsec:~/Desktop/SensorCloudRiotKnoten/RIOT/examples/rust$ BOARD=samr21-xpro make clean all flash term
Building application "rust" for "samr21-xpro" with MCU "samd21".

error: error recursively walking the sysroot
caused by: IO error for operation on /home/dau/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src: No such file or directory (os error 2)
caused by: No such file or directory (os error 2)
note: run with `RUST_BACKTRACE=1` for a backtrace
/home/dau/Desktop/SensorCloudRiotKnoten/RIOT/Makefile.rust:27: recipe for target 'xargo' failed
make: *** [xargo] Error 1

I have followed the setup instructions as specified in examples/rust/README.md (as normal user - without root permissions):

# curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain=nightly
# . ~/.cargo/env
# cargo install xargo

Apparently, the ..../rustlb/src directory is missing:

$ tree /home/dau/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib
/home/dau/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib
├── components
├── etc
│   ├── debugger_pretty_printers_common.py
│   ├── gdb_load_rust_pretty_printers.py
│   ├── gdb_rust_pretty_printing.py
│   └── lldb_rust_formatters.py
├── manifest-cargo-x86_64-unknown-linux-gnu
├── manifest-rustc-x86_64-unknown-linux-gnu
├── manifest-rust-std-x86_64-unknown-linux-gnu
├── multirust-channel-manifest.toml
├── multirust-config.toml
├── rust-installer-version
└── x86_64-unknown-linux-gnu
    └── lib
        ├── liballoc-0720511b45a7223a.rlib
        ├── liballoc_jemalloc-477554c8f244cbba.rlib
        ├── liballoc_system-34e7f110f175a258.rlib
        ├── libarena-a3610c37c7ce55f1.so
        ├── libcollections-63f7707126c5a809.rlib
        ├── libcompiler_builtins-35d2bc471c7ce467.rlib
        ├── libcore-93f19628b61beb76.rlib
        ├── libflate-57bd87feb1669fc9.so
        ├── libfmt_macros-aaffb709550158c5.so
        ├── libgetopts-1dd78a450c70e966.rlib
        ├── libgetopts-1dd78a450c70e966.so
        ├── libgraphviz-6450dd76461436b6.so
        ├── liblibc-ab203041f1ec5313.rlib
        ├── liblog-54730e119c180528.rlib
        ├── liblog-54730e119c180528.so
        ├── libpanic_abort-7afdcc6faca2c514.rlib
        ├── libpanic_unwind-d2ecc8049920bea8.rlib
        ├── libproc_macro-fe3325ac6c450c13.so
        ├── libproc_macro_plugin-e834f7b9a691b4f0.so
        ├── libproc_macro_tokens-c981692fd22343ee.so
        ├── librand-1c6ed188684e7d33.rlib
        ├── librustc_back-8c95cc329d8e5307.so
        ├── librustc_bitflags-01e7ba692dbd8021.rlib
        ├── librustc_borrowck-c96950a653a7b951.so
        ├── librustc-c6c126cf78c297b6.so
        ├── librustc_const_eval-b992e2bb1f3fc644.so
        ├── librustc_const_math-43a8c1adba72b199.so
        ├── librustc_data_structures-b879fad64c9616ec.so
        ├── librustc_driver-4b0bbf37a70f42b1.so
        ├── librustc_errors-24ae80d08263d6d8.so
        ├── librustc_incremental-d6bbdb8a6f3695cd.so
        ├── librustc_lint-b45733a14d6a545f.so
        ├── librustc_llvm-a362f9d07604c723.so
        ├── librustc_metadata-e010419099a9017a.so
        ├── librustc_mir-45892beae7debf21.so
        ├── librustc_passes-0f764442dd3dc4ab.so
        ├── librustc_platform_intrinsics-18d29ae5ec2689a6.so
        ├── librustc_plugin-b93791b83774a1e2.so
        ├── librustc_privacy-c1a3a95c6ab50225.so
        ├── librustc_resolve-895504f8749e4778.so
        ├── librustc_save_analysis-59200ce97aee44d2.so
        ├── librustc_trans-246b1d0d020145ff.so
        ├── librustc_typeck-080302f38ede62f0.so
        ├── librustdoc-c0dcaea09a16c7ec.so
        ├── libserialize-72ff3dbad9254a85.rlib
        ├── libserialize-72ff3dbad9254a85.so
        ├── libstd-b4054fae3db32020.rlib
        ├── libstd-b4054fae3db32020.so
        ├── libstd_shim-7bc32b8fa5084c69.rlib
        ├── libstd_unicode-a9711770523833d4.rlib
        ├── libsyntax-667dbe34cd247b57.so
        ├── libsyntax_ext-5da7e26a48e8bf98.so
        ├── libsyntax_pos-8825f9c970c92cc9.so
        ├── libterm-97d42011bc09bc6d.rlib
        ├── libterm-97d42011bc09bc6d.so
        ├── libtest-cbb1c93280a58d34.rlib
        ├── libtest-cbb1c93280a58d34.so
        ├── libtest_shim-f504cd50d5dc3789.rlib
        └── libunwind-5837d7d3490e00c5.rlib

3 directories, 70 files

OS: Ubuntu 16.04, a SAM-21r-xpro is connected and can be flashed with other examples.

(Note: Yes, I am aware of #6162, however I wanted to give this a try first.)

Thanks for any help!

@miri64
Copy link
Member

miri64 commented Jan 10, 2017

Any hopes to get this in for feature freeze?

@kaspar030
Copy link
Contributor Author

kaspar030 commented Jan 10, 2017 via email

@miri64 miri64 modified the milestones: Release 2017.04, Release 2017.01 Jan 10, 2017
@miri64
Copy link
Member

miri64 commented Jan 10, 2017

Done

@jamesmunns
Copy link

Hey @kaspar030 @miri64, I'm also interested in Rust, RIOT, and Rust+Embedded, (and I live in Berlin). Is there any way I could get more info about the Feb 3rd meeting you mentioned? Feel free to write here, or email/twitter me (my info is on my profile)

@Kijewski Kijewski removed their assignment Jan 24, 2017
@marciol
Copy link

marciol commented Mar 6, 2017

Any new info about it?

@jnohlgard
Copy link
Member

https://github.com/posborne/zinc
I stumbled upon that repo while I was searching for something else, but you might find it interesting and relevant for this PR.

@aabadie
Copy link
Contributor

aabadie commented Jun 20, 2017

ping @kaspar030, this one is nice and it would be great to have it in the next release.

@smlng
Copy link
Member

smlng commented Jan 12, 2018

ping @kaspar030, any news here? This one was postponed several times, I think we should remove the milestone for now, and re-add as soon there is a realistic ETA.

@smlng smlng removed this from the Release 2018.01 milestone Jan 18, 2018
@miri64
Copy link
Member

miri64 commented Mar 15, 2018

No new development for over a year. Let's close this for now.

@miri64 miri64 added the State: archived State: The PR has been archived for possible future re-adaptation label Mar 15, 2018
@miri64 miri64 closed this Mar 15, 2018
@chrysn chrysn mentioned this pull request Aug 20, 2018
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
State: archived State: The PR has been archived for possible future re-adaptation Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.