When developing for an external device from a host (like developing for an embedded system from Yocto/Petalinux), we often need to use the host device's cross compiler to compile our code and scp the executable to the device. This is a simple yet tedious task. This script will do it for you.
- Download/copy-paste the script to your Linux machine.
- Set permission:
chmod +x build-and-scp.sh
- Run it:
./build-and-scp.sh <compile_path> <env_init_script> <compile_command> <exe_path> <target_path>
Arguments:compile_path
: The path where you run the compilation command (likemake
,gcc ...
,bitbake xxx
, etc.)env_init_script
: Some tools like Yocto and Petalinux require you to set up the environment. This is where you will tell the script where the setup script is.compile_command
: The command you run atcompile_path
to compile your code.exe_path
: The path where the compiled executable goes; this will be the thing being scp-ed to the device.target_path
: The destination in the scp command. The script will attempt to delete the existing file if it fails to SCP. Hence, when typing out this argument, make sure to include the executable in the path, e.g.[email protected]:~/abc.exe
, not just[email protected]:~
which is common in normal SCPs.
Example:
./build-and-scp.sh /home/jonathan/src "bitbake -c compile -f abc" /home/jonathan/build/src/abc.exe [email protected]:~/abc.exe
- First, it cd into the source code/project folder.
- Second, it sources the env_init_script.
- Then, it runs the compile_command.
- Then, it will try to SCP the generated executable (specified by exe_path) to the target (specified by target_path).
- If it fails the first time, it will ssh into the target and delete the file in that location. And SCP again. If it fails again, an error will be thrown.
- If successful, print the success message and exit.