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

added script to generate .cargo/config from toolchain environment 💪 #102

Merged
merged 6 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

#.cargo config dir
.cargo
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ In order to build `libremarkable` and the examples (`spy.so` and `demo`), you'll

You can then set up your Rust toolchain for cross compilation with: `rustup target add armv7-unknown-linux-gnueabihf`.

Once that's done, you should add the following to `.cargo/config` (replace `<path-to-installed-oecore-toochain>` with the directory you installed the Remarkable toolchain to):
In order for rust to leverage the toolchain a `.cargo/config` file is required. This file can be generated using the `gen_cargo_config.py`. First the toolchain environment must be
sourced. It location is can be found within the toolchain installation directory. The correct path is also referenced in the toolchain [wiki](https://remarkablewiki.com/devel/toolchain).
ju6ge marked this conversation as resolved.
Show resolved Hide resolved
After the environment is loaded the script will read the environment variables to generate the correct `.cargo/config` file for your toolchain.
LinusCDE marked this conversation as resolved.
Show resolved Hide resolved

The resulting config file will look something like this:
```
[target.armv7-unknown-linux-gnueabihf]
linker = "<path-to-the-installed-oecore-toolchain>/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc"
linker = "<toolchain_install_path>/sysroots/x86_64-codexsdk-linux/usr/bin/arm-remarkable-linux-gnueabi/arm-remarkable-linux-gnueabi-gcc"
rustflags = [
"-C", "link-arg=-march=armv7-a",
"-C", "link-arg=-marm",
"-C", "link-arg=-mfpu=neon",
"-C", "link-arg=-mfloat-abi=hard",
"-C", "link-arg=-mcpu=cortex-a9",
"-C", "link-arg=--sysroot=<path-to-the-installed-oecore-toolchain>/sysroots/cortexa9hf-neon-oe-linux-gnueabi",
"-C", "link-arg=--sysroot=<toolchain_install_path>/sysroots/cortexa7hf-neon-remarkable-linux-gnueabi",
]
```

(`<path-to-the-installed-oecore-toolchain` will likely be `/usr/local/oecore-x86_64/`, if you did the default install on Linux.)

If you have further questions, feel free to ask in Issues.

You can also add this snippet to the above file in order to default to cross-compiling for this project:

```
Expand Down
47 changes: 47 additions & 0 deletions gen_cargo_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import os
import re

cargo_config = """
[target.armv7-unknown-linux-gnueabihf]
linker = "{gcc_path}"
rustflags = [
"-C", "link-arg=-march=armv7-a",
"-C", "link-arg=-marm",
"-C", "link-arg={fpu}",
"-C", "link-arg={float_s}",
"-C", "link-arg={cpu}",
"-C", "link-arg=--sysroot={sysroot}",
]\n"""

def main():
#get sysroot from environment
ju6ge marked this conversation as resolved.
Show resolved Hide resolved
sysroot = os.environ["SDKTARGETSYSROOT"]
#get comiler name from environment
cc_full = os.environ["CC"]
cc = cc_full.split(" ")[0]
#strip -gcc from compiler to build regex string for parsing PATH
cc_path = cc[0:-4]
re_parse_path = ":[^:]*{cc}:".format(cc=cc_path)
extract_compiler_path = re.compile(re_parse_path)
toolchain_compiler_path = extract_compiler_path.search(os.environ["PATH"]).group(0)[1:-1]
#get other arguments from compiler definition
re_fpu = re.compile("(-mfpu=[^ ]*)")
re_float = re.compile("(-mfloat-abi=[^ ]*)")
re_cpu = re.compile("(-mcpu=[^ ]*)")
fpu_arg = re_fpu.search(cc_full).group(0)
float_arg = re_float.search(cc_full).group(0)
cpu_arg = re_cpu.search(cc_full).group(0)
#join path to get full compiler path
cc = os.path.join(toolchain_compiler_path, cc)

#create .cargo dir if not exsits
if not os.path.isdir(".cargo"):
os.mkdir(".cargo")
with open(".cargo/config","w") as f:
f.write(cargo_config.format(gcc_path=cc, sysroot=sysroot, fpu=fpu_arg, float_s=float_arg, cpu=cpu_arg))
print("Toolchain config written to \".cargo/config\" ✅")

if __name__ == "__main__":
main()