Cross-Compilation
1. Compilation Environment
C++ programs need to compile dependency files to run on the board (you can also use the provided executable files directly). To save pressure on edge devices, we choose to use an X86 Linux environment for cross-compilation (the example uses WSL, for WSL usage refer to the official documentation).
Setting up the cross-compilation environment, here are two methods provided:
(1) Install cross-compilation toolchain via apt:
If your system and target SoC platform have the same libc version (can be queried via ldd --version command), you can install using the following command:
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnuUninstall method:
sudo apt remove cpp-*-aarch64-linux-gnuIf your environment does not meet the above requirements, it is recommended to use method (2).
(2) Set up cross-compilation environment via docker:
You can use our provided docker image -- stream_dev.tar as the cross-compilation environment.
#Install dfss download tool
pip3 install dfss
#Install docker image
python3 -m dfss --url=open@sophgo.com:sophon-demo/common/docker/stream_dev.tar # ubuntu 20.04, gcc-9If you are using Docker for the first time, execute the following commands to install and configure it (this operation is only required for the first time):
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp dockerLoad the image in the downloaded image directory
docker load -i stream_dev.tarYou can view the loaded image via docker images, default is stream_dev:latest.
Create container
docker run --privileged --name stream_dev -v $PWD:/workspace -it stream_dev:latest
# stream_dev is just an example name, please specify the name you want for your container
#For existing docker containers, open using the following command
docker run -v $PWD:/workspace -it stream_dev:latestThe workspace directory in the container will be mounted to the host directory where you run docker run. You can compile projects in this container. The workspace directory is under the root directory, and changes in this directory will map to changes in the corresponding files in the local directory.
Note: When creating a container, you need to go to the parent directory and above of soc-sdk (dependency compilation environment)
2. Packaging Dependency Files
2.1. Packaging libsophon
Extract libsophon_soc_x.y.z_aarch64.tar.gz, where x.y.z represents the version number.
The file can be found in the SDK package at this path: SDK-23.09-LTS-SP4\SDK-23.09-LTS-SP4\sophon-img_20241227_105055, you can download the SDK package via command
#You can download the SDK package via command (skip this operation if you already have the libsophon_soc_x.y.z_aarch64.tar.gz file)
wget https://sophon-assets.sophon.cn/sophon-prod-s3/drive/24/12/31/10/SDK-23.09-LTS-SP4.zip
# Create root directory for dependency files
mkdir -p soc-sdk
# Extract libsophon_soc_x.y.z_aarch64.tar.gz
tar -zxf libsophon_soc_${x.y.z}_aarch64.tar.gz
# Copy related library directories and header file directories to the dependency root directory
cp -rf libsophon_soc_${x.y.z}_aarch64/opt/sophon/libsophon-${x.y.z}/lib soc-sdk
cp -rf libsophon_soc_${x.y.z}_aarch64/opt/sophon/libsophon-${x.y.z}/include soc-sdk2.2. Packaging sophon-ffmpeg and sophon-opencv
Extract sophon-mw-soc_x.y.z_aarch64.tar.gz, where x.y.z represents the version number.
The file can be found in the SDK package at this path: SDK-23.09-LTS-SP4\SDK-23.09-LTS-SP4\sophon-mw_20241223_163201
# Extract sophon-mw-soc_x.y.z_aarch64.tar.gz
tar -zxf sophon-mw-soc_${x.y.z}_aarch64.tar.gz
# Copy ffmpeg and opencv library directories and header file directories to soc-sdk directory
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-ffmpeg_${x.y.z}/lib soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-ffmpeg_${x.y.z}/include soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-opencv_${x.y.z}/lib soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-opencv_${x.y.z}/include soc-sdk3. Performing Cross-Compilation
bmcv mode
After setting up the cross-compilation environment, use the cross-compilation toolchain to compile and generate executable files. Here we use the YOLOv5 C++ example from sophon-demo as an example.
cd cpp/yolov5_opencv
mkdir build && cd build
#Please modify the -DSDK path according to actual situation, use absolute path.
cmake -DTARGET_ARCH=soc -DSDK=/workspace/soc-sdk/ ..
makeAfter compilation completes, a .soc file will be generated in the corresponding directory, for example: cpp/yolov5_bmcv/yolov5_bmcv.soc.
sail mode
If you use the sophon-sail interface, you need to configure sophon-sail for the soc environment (development board environment) first. Configuration method is as follows:
Configure sail environment
Through cross-compilation (using WSL in this example), compile SAIL containing bmcv, sophon-ffmpeg, sophon-opencv.
If the libc version of the compilation platform is different from the target version, you need to enter the docker environment
docker run -v $PWD:/workspace -it stream_dev:latest#Extract sophon-sail_3.8.0.tar.gz
tar -zvxf sophon-sail_3.8.0.tar.gz
#Go to the sophon directory: cd sophon
mkdir build && cd build
cmake -DBUILD_TYPE=soc -DBUILD_PYSAIL=OFF -DCMAKE_TOOLCHAIN_FILE=../cmake/BM168x_SOC/ToolChain_aarch64_linux.cmake -DLIBSOPHON_BASIC_PATH=../../libsophon_soc_0.5.1-LTS_aarch64/opt/sophon/libsophon-0.5.1/ -DFFMPEG_BASIC_PATH=../../sophon-mw-soc_0.12.0_aarch64/opt/sophon/sophon-ffmpeg_0.12.0/ -DOPENCV_BASIC_PATH=../../sophon-mw-soc_0.12.0_aarch64/opt/sophon/sophon-opencv_0.12.0/ ..
make sailInstall SAIL dynamic library and header files. The program will automatically create build_soc in the source directory, and the compilation results will be installed under build_soc
make installCopy the sophon-sail from the build_soc folder to the target SOC's /opt/sophon directory, and you can call it on the soc.
After setting up the cross-compilation environment, use the cross-compilation toolchain to compile and generate executable files.
cd cpp/yolov5_sail
mkdir build && cd build
#Please modify -DSDK and -DSAIL_PATH paths according to actual situation, use absolute paths.
cmake -DTARGET_ARCH=soc -DSDK=/path_to_sdk/soc-sdk -DSAIL_PATH=/wrokspace/sophon-sail/build_soc/sophon-sail ..
makeAfter compilation completes, yolov5_sail.soc will be generated in the yolov5_sail directory.
Since we are moving sophon-sail to the /opt/sophon directory, if using ssh connection for file transfer, you need to log in as root account. The root account has no initial password, you need to use the linaro account to run sudo passwd root to set a password before use.
Generally, Linux systems disable remote root login by default. You need to perform the following operations:
Edit the configuration file
sudo vim /etc/ssh/sshd_config
#Add to the file
PermitRootLogin yesExit and save, restart ssh
sudo service sshd restartAfter copying the sophon-sail library files to the target soc according to the tutorial, you also need to set the following environment variables:
echo 'export LD_LIBRARY_PATH=/opt/sophon/sophon-sail/lib/:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc