{"id":13437802,"url":"https://github.com/Ogeon/rust-on-raspberry-pi","last_synced_at":"2025-03-19T18:30:37.726Z","repository":{"id":28960767,"uuid":"32486935","full_name":"Ogeon/rust-on-raspberry-pi","owner":"Ogeon","description":"[OUTDATED] Instructions for how to cross compile Rust projects for the Raspberry Pi","archived":true,"fork":false,"pushed_at":"2017-04-20T13:02:44.000Z","size":22,"stargazers_count":292,"open_issues_count":8,"forks_count":15,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-27T23:23:36.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ogeon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-18T22:07:33.000Z","updated_at":"2024-04-17T13:43:31.000Z","dependencies_parsed_at":"2022-08-17T18:45:35.276Z","dependency_job_id":null,"html_url":"https://github.com/Ogeon/rust-on-raspberry-pi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ogeon%2Frust-on-raspberry-pi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ogeon%2Frust-on-raspberry-pi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ogeon%2Frust-on-raspberry-pi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ogeon%2Frust-on-raspberry-pi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ogeon","download_url":"https://codeload.github.com/Ogeon/rust-on-raspberry-pi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244483109,"owners_count":20460057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:00.345Z","updated_at":"2025-03-19T18:30:37.484Z","avatar_url":"https://github.com/Ogeon.png","language":"Shell","funding_links":[],"categories":["Development tools","Shell","开发工具","开发工具 Development tools","others","\u003ca name=\"Shell\"\u003e\u003c/a\u003eShell"],"sub_categories":["Embedded","嵌入式","嵌入式 Embedded"],"readme":"# Cross Compiling for Raspberry Pi\n\nThis guide will show how Rust programs can be cross compiled for the Raspberry\nPi using Cargo. These instructions may or may not work for your particular\nsystem, so you may have to adjust the procedure to fit your configuration.\n\nThere is also a docker container version of the Rust cross-compiler\nfor the Raspberry: https://github.com/Ragnaroek/rust-on-raspberry-docker.\nIf you are using docker this may be a easier solution for cross-compiling\nyour project.\n\n## Cross Compiling on a Linux Host\n\nThese instructions are based on [the rusty-pi guide](https://github.com/npryce/rusty-pi/blob/master/doc/compile-the-compiler.asciidoc),\nbut with some additions and adaptations for the current build systems.\n\n## Preparing the Tools\n\nThe first step is to download the Raspberry Pi toolchain. It's a collection of\nbinaries and libraries for cross compiling. Begin by installing `git` if it's\nnot already installed:\n\n```\nsudo apt-get install git\n```\n\n(replace `apt-get install` with the install command for your favorite package\nmanager)\n\nand procede by cloning the toolchain repository:\n\n```\ngit clone https://github.com/raspberrypi/tools.git ~/pi-tools\n```\n\n## Compiling the Compiler\n\nThe next step is to compile the Rust compiler and standard libraries. The\nstandard libraries are particularly important, since they have to be compiled\nfor the ARM platform to make them usable in your program. See Appendix A\nfor information on how to add more libraries.\n\nstart by cloning the Rust repository and `cd` into it:\n\n```\ngit clone http://github.com/rust-lang/rust.git\ncd rust\n```\n\nYou may want to check out the same revision as your current copy of `rustc` to\nkeep everything in sync. You can find the revision hash by running `rustc`\nwith the `-V` flag:\n\n```\n$ rustc -V\nrustc 1.0.0-nightly (30e1f9a1c 2015-03-14) (built 2015-03-15)\n                     ^-------^ This is what you are looking for\n```\n\nCopy the hash and use it to reset the repository to the same revision:\n\n```\ngit reset --hard 30e1f9a1c\n```\n\nAlright, finally time to build it. Begin by adding the binary directory from\nthe Raspberry Pi toolchain to your path. Use the following command to use the\n64 bit tools:\n\n```\nexport PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:$PATH\n```\n\nor use this command for the 32 bit tools:\n\n```\nexport PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH\n```\n\nConfigure the compiler to build everything for ARM Linux and set the install\ndestination to `$HOME/pi-rust`:\n\n```\n./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/pi-rust\n```\n\nAnd build+install!\n\n```\nmake -j4 \u0026\u0026 make install\n```\n\n(Change the 4 to your preferred number of parallel build processes)\n\nThis is will take a while, so go and grab some coffee or take a walk while\nwaiting.\n\nIs it done? Great! Now, move on to the next part.\n\n## Pointing Cargo in the Right Direction\n\nWe are almost ready to actually build things. You may actually be able to use\n`rustc` directly, but we want more, right? We want the convenience of Cargo!\nBut Cargo doesn't come without demands. It has to know what we are using to\nlink our program, so let's tell it. Cargo will be looking for [configuration\nfiles](doc.crates.io/config.html) where we can specify what to use when\nbuilding ARM programs.\n\nYou may already have a directory in your `$HOME` called `.cargo`. Create one,\nif you don't have it. Now, create a file called `config`, or edit an existing\none, and add the following lines to associate the target triple\n`arm-unknown-linux-gnueabihf` with our cross compilers:\n\n```\n[target.arm-unknown-linux-gnueabihf]\nar = \"arm-linux-gnueabihf-gcc-ar\"\nlinker = \"gcc-sysroot\"\n```\n\nWait a minute! What is `gcc-sysroot`? Well, that is a semi ugly hack and we\nare going to use it.\n\nThe thing is that Cargo has some problems when it comes to cross compiling\nwhile depending on share libraries. These libraries are placed somewhere in\nthe `sysroot` and `gcc` is using `ld` to link them. The problem is that the\ndefault `sysroot` is where the host libraries are located and those are not\nbuilt for ARM. There is currently no good way to tell Cargo to tell `rustc` to\ntell `gcc` to tell `ld` to look somewhere else, so we have to do it for them.\n\nThe Raspberry Pi toolchain contains a directory with various system\ndirectories filled with common libraries (if you want to add more libraries,\nsee Appendix A). This is where we want `ld` to look\nfor things, so we are going to use a simple script to tell `gcc` to tell it\nwhere this directory can be found. Create a file in the binary directory you\nadded to your `$PATH` before\n(`~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin`\nif you used the 64 bit tools or\n`~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin`\nif you prefer the 32 bit tools) and call it `gcc-sysroot`.\n\nAdd the following lines in `gcc-sysroot`:\n\n```\n#!/bin/bash\narm-linux-gnueabihf-gcc --sysroot=$HOME/pi-tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/arm-bcm2708hardfp-linux-gnueabi/sysroot \"$@\"\n```\n\nThis is basically an alias for `arm-linux-gnueabihf-gcc`, but with the\n`--sysroot` set to where the libraries are kept. The `\"$@\"` part is there to\npass all the incoming argument forwards to `arm-linux-gnueabihf-gcc`. Alright,\nmake the file executable:\n\n```\nchmod +x ~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/gcc-sysroot\n```\n\nor\n\n```\nchmod +x ~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/gcc-sysroot\n```\n\nIf your crate requires building C++ code, then you'll need to create a `g++-sysroot` just\nlike `gcc-sysroot`, substituting `arm-linux-gnueabihf-g++` for `arm-linux-gnueabihf-gcc`\ninside.\n\nAnd you should be done! Or, kind of done. There is one thing left.\n\n## Running Cargo\n\nIt would be nice to just be able to run `cargo build` and be done with it, but\nthe reality is not exactly that nice. Almost, but not exactly. You will have\nto add the binaries to your `$PATH` every time you open a new terminal and get\nready for compiling. You may also have to define some other variables to\nsatisfy some other systems. Sound tedious and boring, right? Well, that's what\nwe have scripts for.\n\nThis repository contains two versions of the same script (`cross32` for 32 bit\nand `cross64` for 64 bit). They can be used as a substitute for Cargo, like\nthis:\n\n```\n./cross64 cargo-command path/to/rust path/to/pi/toolchain\n\nExamples:\n./cross64 build ~/some-other-rust ~/some-other-pi-tools\n./cross64 doc\n./cross64 \"build --release\"\n```\n\nThe first argument is what you would normally pass to `cargo`. It can be\nsingle commands, like `build` or `doc`, or multiple arguments like `\"build\n--release\"`. Note that the combined commands have to be passed as a single\nstring. `path/to/rust` and `path/to/pi/toolchain` are optional and should only\nbe used if you want to use tools from somewhere else than what this guide\nrecommends.\n\nThese scripts will set up the required environment variables and run cargo for\nyou. All without polluting the external environment. You can include them in\nyour projects and modify them however you want.\n\nThat's it! You should now be ready to cross compile your Raspberry Pi\nprojects.\n\nHave fun!\n\n\n# Appendix A: Extending the toolset to support more system dependencies\n\nLet's say your project uses some crate that depends on having openssl\ninstalled on the system. In this case you have to install the package\nmanually into the ARM toolset.\n\nGet these packages either from the raspberry, or download them online.\n\nWe'll assume that you're running Raspbian (i.e. deb files), but it should be\nstraight forward to adapt the steps to your dist/packages.\n\nIf you do `apt-cache show libssl1.0.0` on the raspberry, you'll see this in the\noutput:\n\n    Filename:    pool/main/o/openssl/libssl1.0.0_1.0.1e-2+rvt+deb7u17_armhf.deb\n\nYou should be able to find a match for that under ftp.debian.org/debian/pool, so\nthe resulting URL in this case is\n\n    http://ftp.debian.org/debian/pool/main/o/openssl/libssl-dev_1.0.1e-2+deb7u17_armhf.deb\n\nIf it's not there, see if it is still on the raspberry under\n`/var/cache/apt/archive`.\n\nIf you still can't find it, try searching for the filename online.\n\nWhen you have the dependencies downloaded, extract them into the ARM toolchain:\n\n    # cd into the sysroot of the arm toolchain\n    cd ~/pi-tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/arm-bcm2708hardfp-linux-gnueabi/sysroot/\n\n    # move the deb files here and use `ar` to extract the contents:\n    ar p libssl1.0.0_1.0.1e-2+rvt+deb7u17_armhf.deb data.tar.gz | tar zx\n\n    # Repeat for any other dependencies you may have..\n    ar p libssl-dev_1.0.1e-2+rvt+deb7u17_armhf.deb data.tar.gz | tar zx\n    ar p zlib1g_1.2.7.dfsg-13_armhf.deb data.tar.gz | tar zx\n    ar p zlib1g-dev_1.2.7.dfsg-13_armhf.deb data.tar.gz | tar zx\n\nNow you're ready to build the project.\n\n\nPull requests with enhancements, corrections or additional instructions are\nvery much appreciated. Good luck!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOgeon%2Frust-on-raspberry-pi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FOgeon%2Frust-on-raspberry-pi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOgeon%2Frust-on-raspberry-pi/lists"}