Cloning the root filesystem for cross-compiling

Hello,

I have a general question when cross-compiling a C/C++ application for the Beagle Bone Black.

I am using the cross-compilers provided by Linaro: Linaro Releases . I have managed to make cross-compiling work both for Windows and for Linux systems using a CMake cross-compile build system and the tcf-agent for debugging with Eclipse, but cloning the root filesystem to get all required libraries onto the host system is a tricky process.

I used rsync to do this

rsync -avHAXR --delete-after --info=progress2 --numeric-ids <user_name>@<ip_address>:/{usr,lib} <rootfs_path>

Unfortunately, there seems to be an issue with some symlinks, for example the symlinks libpthread.so and librt.so. These symlinks appear to use absolute paths, and it is necessary, to delete them and repoint them using relative paths. Is there some clean way to clone a sysroot which solves this issue? I expected that CMake can deal with these absolute links when specifying the rootfs properly, but it does not appear to be able to use those symlinks…

rm libpthread.so
rm librt.so
ln -s ../../../lib/arm-linux-gnueabihf/libpthread.so.0 libpthread.so
ln -s ../../../lib/arm-linux-gnueabihf/librt.so.1 librt.so

On Windows, MinGW rsync somehow is not able to copy some files and I need to copy those manually

scp <user_name>@<ip-address>:/lib/arm-linux-gnueabihf/{libc.so.6,ld-linux-armhf.so.3,libm.so.6} <rootfs_path>/lib/arm-linux-gnueabihf
scp <user_name>@<ip-address>:/usr/lib/arm-linux-gnueabihf/{libpthread.so,libc.so,librt.so} <rootfs_path>/usr/lib/arm-linux-gnueabihf

This could propably be solves by using a linux system to clone it properly and then package it as a zip. This is also the easiest way to avoid doing this on different systems. Clone it once properly, and just use a prepared root filesystem with all required libraries. I am just wondering if there is a clean way to clone the rootfs without these “hacks”.

In any case, if this seems useful, maybe I will add a cross-compiling guide using the tcf-agent for the beagle bone black website or github because I did not find anything similar on the website with all necessary steps.

Kind Regards
Robin

The cross-compiling guide can be found here: GitHub - spacefisch/beaglebone-crosscompiling: Cross-compiling and debugging C/C++ applications for the Beaglebone Black with CMake and the TCF-Agent . Tested for Windows and Linux. Maybe it would be easier to do this whole process in a container but I still need to get familiar with docker…

Here’s a Python script to fix symlinks in a cloned sysroot:

#!/usr/bin/env python
import sys
import os

# Take a sysroot directory and turn all the abolute symlinks and turn them into
# relative ones such that the sysroot is usable within another system.

if len(sys.argv) != 2:
    print("Usage is " + sys.argv[0] + "<directory>")
    sys.exit(1)

topdir = sys.argv[1]
topdir = os.path.abspath(topdir)

def handlelink(filep, subdir):
    link = os.readlink(filep)
    if link[0] != "/":
        return
    if link.startswith(topdir):
        return
    #print("Replacing %s with %s for %s" % (link, topdir+link, filep))
    print("Replacing %s with %s for %s" % (link, os.path.relpath(topdir+link, subdir), filep))
    os.unlink(filep)
    os.symlink(os.path.relpath(topdir+link, subdir), filep)

for subdir, dirs, files in os.walk(topdir):
    for f in files:
        filep = os.path.join(subdir, f)
        if os.path.islink(filep):
            #print("Considering %s" % filep)
            handlelink(filep, subdir)