Skip to content

Commit

Permalink
Merge pull request #1 from terasakisatoshi/guide-for-RaspberryPi3
Browse files Browse the repository at this point in the history
Guide for raspberry pi3
  • Loading branch information
terasakisatoshi committed Feb 8, 2020
2 parents 0bac004 + 9265106 commit 184cae9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.PHONY: run, clean
.PHONY: run, rpi3, clean

build: src/HelloX.jl
julia -e 'using Pkg; Pkg.add(PackageSpec(url="https://github.com/KristofferC/PackageCompilerX.jl.git",rev="master"))'
julia -e 'using Pkg; Pkg.add(PackageSpec(url="https://github.com/JuliaComputing/PackageCompilerX.jl.git",rev="master"))'
julia --startup-file=no --project=. -e 'using Pkg; Pkg.instantiate()'
julia build.jl

rpi3: src/HelloX.jl
julia -e 'using Pkg; Pkg.add(PackageSpec(url="https://github.com/terasakisatoshi/PackageCompilerX.jl.git",rev="arm"))'
julia --startup-file=no --project=. -e 'using Pkg; Pkg.instantiate()'
julia build.jl

Expand Down
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,102 @@ Hello, World from Example.jl
```

- Congratulation! You've solved two-language problem.

# Appendix

- In this chapter, we would like test out our binary runs on ARM devices e.g. Raspberry Pi3.
- Although Julia provides 32-bit (ARMv7-a hard float), its [tiers is 2](https://julialang.org/downloads/), that is:
> Tier 2: Julia is guaranteed to build from source using the default build options, but may or may not pass all tests. Official binaries are available on a case-by-case basis.
>
>(Taken fromCurrently supported platforms)
- This means some Julia Packages do not support/consider for ARM environment devices. PacakgeCompiler.jl and PackageCompilerX.jl are not exception to this. If you like to test out `HelloX.jl` on Raspberry Pi3, we need apply a patch for our purpose.


## Modify Code

- In this section, we would like modify a source code to deal with Raspberry Pi3 matters.
- The following snippet code is taken from https://github.com/JuliaComputing/PackageCompilerX.jl/blob/master/src/PackageCompilerX.jl:

```julia:PacakgeCompilerX.jl
const NATIVE_CPU_TARGET = "native"
const APP_CPU_TARGET = "generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)"

current_process_sysimage_path() = unsafe_string(Base.JLOptions().image_file)

all_stdlibs() = readdir(Sys.STDLIB)

yesno(b::Bool) = b ? "yes" : "no"
bitflag() = Int == Int32 ? `-m32` : `-m64`
march() = (Int == Int32 ? `-march=pentium4` : ``)
```

- Since gcc command for Raspberry Pi3 does not have `-m32` option, we also need extract code depends os `bitflag()`.
- Replace `APP_CPU_CONSTANT` with `APP_CPU_TARGET = "generic;cortex-a53"`
- I'm not sure it is correct, but it works on my environment
- Replace `-march=pentium4` with `-march=armv7-a`

- The following snippet should work for our purpose.

```
const NATIVE_CPU_TARGET = "native"
const APP_CPU_TARGET = "generic;cortex-a53"
current_process_sysimage_path() = unsafe_string(Base.JLOptions().image_file)
all_stdlibs() = readdir(Sys.STDLIB)
yesno(b::Bool) = b ? "yes" : "no"
march() = (Int == Int32 ? `-march=armv7-a` : ``)
```

- See [my arm branch from PacakgeCompilerX.jl project](https://github.com/terasakisatoshi/PackageCompilerX.jl/tree/arm) which is applied by a patch above.

## Let's test out

- If you do not have a Raspberry Pi3, you can run environment on Docker.
- We've used Docker image terasakisatoshi/jlcross:rpi3-v1.3.1
- See DockerHub https://hub.docker.com/r/terasakisatoshi/jlcross
- Dockerfile can be found at https://github.com/Julia-Embedded/jlcross
- The follwing command build binary for Raspberry Pi3, it takes a few hours to get it.

```shell
# Script for Raspberry Pi 3
# Run make clean to reset host environment
make clean
# Check Julia version
docker run --rm -it --name versioncheck -v ${PWD}:/work -w /work terasakisatoshi/jlcross:rpi3-v1.3.1 julia -e "using InteractiveUtils; versioninfo()"
# Build executable which will be stored under a directory named `build`
docker run --rm -it --name buildrpi3 -v ${PWD}:/work -w /work terasakisatoshi/jlcross:rpi3-v1.3.1 make rpi3
# Test to run binary on other environments that does not have Julia environment
docker run --rm -it -v ${PWD}:/work -w /work balenalib/raspberrypi3:buster-run-20191106 build/bin/HelloX
```

```console
20191106 build/bin/HelloX
ARGS = String[]
Base.PROGRAM_FILE = "/work/build/bin/HelloX"
Hello World from HelloX.jl
Hello, World from Example.jl
┌────────────────────────────────────────┐
1 │⠀⠀⠀⠀⠀⠀⢀⠖⢹⠉⢢⠀⠀⢀⠞⠉⠉⢢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠞⠉⠀⠀⢀│ cos(x)
│⠀⠀⠀⠀⠀⢠⠊⠀⢸⠀⠀⠳⣠⠊⠀⠀⠀⠀⠣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠃⠀⢀⡠⠒⠁│ sin(x)
│⠀⠀⠀⠀⢀⠇⠀⠀⢸⠀⠀⢠⢷⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⢇⡠⠒⠁⠀⠀⠀│ line
│⠀⠀⠀⠀⡜⠀⠀⠀⢸⠀⠀⡜⠀⢧⠀⠀⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⡞⠁⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⢸⠀⠀⠀⠀⢸⠀⢸⠀⠀⠘⡄⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⢀⡠⠒⠁⡸⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⢀⠇⠀⠀⠀⠀⢸⢀⠇⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⢱⠀⠀⢀⡠⠒⠁⠀⠀⢠⠃⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⡜⠀⠀⠀⠀⠀⢸⡜⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⢈⡦⠒⠁⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀│
f(x) │⠤⠤⠧⠤⠤⠤⠤⠤⢼⠧⠤⠤⠤⠤⠤⠼⡤⠤⠤⡤⠴⠥⠼⡤⠤⠤⠤⠤⠤⢴⠥⠤⠤⠤⠤⠤⢤⠤⠤⠤│
│⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⣣⠔⠉⠀⠀⠀⠀⢣⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⡸⢸⠀⠀⠀⠀⡠⠔⠉⠈⡆⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⡸⠀⠀⠀⠀⠀⠀⡸⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⢀⠇⢸⠀⡠⠔⠉⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⢱⠀⠀⢠⠃⠀⠀⠀⠀⠀⢠⠃⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⡞⡠⢼⠉⠀⠀⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⠀⠀⠀⢇⠀⡞⠀⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⡠⡼⠉⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡆⠀⠀⠀⠀⠀⠘⡾⠀⠀⠀⠀⠀⠀⡸⠀⠀⠀⠀⠀⠀│
│⠀⡠⠔⠉⡰⠁⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢄⠀⠀⠀⠀⡜⠙⣄⠀⠀⠀⠀⡜⠁⠀⠀⠀⠀⠀⠀│
-1 │⠊⢀⣀⠜⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢦⣀⣀⠜⠀⠀⠈⢦⣀⣠⠜⠀⠀⠀⠀⠀⠀⠀⠀│
└────────────────────────────────────────┘
-2 7
x%
```

Yes, you're good to go.
9 changes: 9 additions & 0 deletions rpi3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Script for Raspberry Pi 3
# Run make clean to reset host environment
make clean
# Check Julia version
docker run --rm -it --name versioncheck -v ${PWD}:/work -w /work terasakisatoshi/jlcross:rpi3-v1.3.1 julia -e "using InteractiveUtils; versioninfo()"
# Build executable which will be stored under a directory named `build`
docker run --rm -it --name buildrpi3 -v ${PWD}:/work -w /work terasakisatoshi/jlcross:rpi3-v1.3.1 make rpi3
# Test to run binary on other environments that does not have Julia environment
docker run --rm -it -v ${PWD}:/work -w /work balenalib/raspberrypi3:buster-run-20191106 build/bin/HelloX

0 comments on commit 184cae9

Please sign in to comment.