Skip to content

Commit be3ea00

Browse files
HackerSmackerlcsmuller
HackerSmacker
authored andcommitted
feat: cross-compilation guide
1 parent b0fbfd7 commit be3ea00

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ This will install the headers and library files into $PREFIX. You can override t
304304
# PREFIX=/opt/concord make install
305305
```
306306

307+
### Cross-compiling Concord
308+
To cross-compile Concord, see the manual [here](docs/guides/cross_compiling.md).
309+
307310
### Included dependencies
308311

309312
The following are `stable` and well documented dependencies that are packaged with Concord and can be included to your projects:

docs/guides/cross_compiling.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Cross Compiling Concord
2+
3+
Unlike most Autoconf-based projects, Concord uses normal Makefiles. As such,
4+
cross-compilation can be rather daunting at first. Fortunately, once you
5+
understand it, you won't have any trouble (unless your compiler malfunctions
6+
or is misconfigured).
7+
8+
## Step 1: getting a cross compiler
9+
Debian-based Linux distros have cross compilers available in their package
10+
repositories. For example, to get an ARM cross compiler suitable for building
11+
for an old 32-bit Raspberry Pi, run `apt-get install gcc-arm-linux-gnueabihf`.
12+
Search the Debian package repositories for "gcc" to see other cross compilers
13+
available.
14+
15+
If you aren't on Debian, you can use cross-compilers from [here](https://mirrors.edge.kernel.org/pub/tools/crosstool/), although, do note that these compilers
16+
might take a fair bit of work to get working perfectly. If you do use one of
17+
these, ensure that `$PATH` gets updated to include the compiler's bin directory,
18+
or you won't be able to invoke the compiler.
19+
20+
## Step 2: cross-compiling Concord
21+
For this example, we will assume that your host system is a standard x86_64
22+
PC, and uses the target triplet `x86_64-pc-linux-gnu`. We will be
23+
cross-compiling to a 64-bit Raspberry Pi 4, with the target triplet
24+
`aarch64-linux-gnu`. Now, when GCC cross-compilers are made, all of the built
25+
executables for the compiler are prefixed with the target tripet. This means
26+
that our actual C compiler command is `aarch64-linux-gnu-gcc`. This will be
27+
our `CC` value.
28+
29+
The gencodecs system that Concord uses requires that the system C compiler is
30+
available. This means we have to set `HOSTCC` when we compile. Since our
31+
target triplet is `x86_64-pc-linux-gnu`, this means that our host C compiler
32+
is named `x86_64-pc-linux-gnu-gcc`. However, since this is the primary C
33+
compiler on the system, we can just use `gcc` for `CC`.
34+
35+
Finally, we will install Concord to the Raspberry Pi by directly accessing its
36+
root filesystem via NFS. The Pi's Linux image has been mounted read-write into
37+
`/mnt`. Since we want Concord to go into `/usr/local`, our target `PREFIX`
38+
path for `make install` is `/mnt/usr/local`.
39+
40+
Okay, we can now run the commands to actually compile.
41+
42+
```console
43+
cd gencodecs
44+
make HOSTCC=gcc CC=aarch64-linux-gnu-gcc
45+
cd ..
46+
make CC=aarch64-linux-gnu-gcc
47+
make install PREFIX=/mnt/usr/local
48+
```
49+
50+
Easy as that! Granted, you can perform a compilation with fewer typed commands
51+
if you `export` those three variables beforehand, but, this guide was written
52+
with maximum learning in mind (and demonstrating why gencodecs needs the
53+
split compilation procedure).
54+
55+

gencodecs/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# available here to be set.
55
CPP = cc -E
66
CC = cc
7+
HOSTCC = cc
78

89
TOP = ..
910

@@ -63,7 +64,7 @@ DOXYGEN_DESC = "/**\n @file $@\n @author Cogmasters\n @brief Generated code\n*/"
6364
all: $(OUT_O)
6465

6566
$(PP): $(PP).c
66-
$(CC) $(CFLAGS) $< -o $@
67+
$(HOSTCC) $(CFLAGS) $< -o $@
6768

6869
$(OUT_O): $(OUT_C) $(OUT_H)
6970
$(CC) -c $(CFLAGS) $< -o $@

0 commit comments

Comments
 (0)