setlocal enableextensions enabledelayedexpansion
rem // what we're building
set PRODUCT_NAME=the LLVM compiler suite
rem // the WSL distribution to use and hostname where its packages are downloaded from
set WSL_DISTR=Ubuntu-22.04
set WSL_SOURCE=archive.ubuntu.com
rem // name of the QNX SDK root directory
set QNXSDK_DIRNAME=qnx800
rem // list of Linux tools required by the cross-build script
set REQUIRED_TOOLS=wget python3 cmake gcc g++ ninja:ninja-build ccache
rem // welcome the wary user
echo QNX8 toolchain cross-build script
for WSL by Pierre-Marie Baty
^<pm
@pmbaty.com
^>
rem // make sure we have at least Windows 10
for /f "tokens=4 delims=. "
%%i in ('ver'
) do set VERSION=
%%i
echo Detected Windows NT kernel version:
%VERSION%
echo Error: you need at least Windows 10 to cross-build
%PRODUCT_NAME% to QNX 8.
echo Please upgrade your Windows operating system to Windows 10 version 2004 build
echo 19041 or a later version.
)
rem // if WSL is not installed, do so
echo|set /p=Checking whether the Windows Subsystem
for Linux is installed...
wsl --list -v
> nul 2
>&1
|| ( echo no
& goto :
install_wsl )
rem // WSL is installed, make sure the Linux distribution we need is there
echo|set /p=Checking
for the presence of the GNU/Linux distribution
%WSL_DISTR% in WSL...
wsl --distribution
%WSL_DISTR% -- cat /etc/os-release
|find "
%WSL_DISTR:-= %"
> nul || ( echo no
& goto :
install_wsl )
rem // WSL is installed with the right Linux distribution, make sure it's running WSL2
echo|set /p=Checking whether the WSL hypervisor is version 2...
wsl --distribution
%WSL_DISTR% -- cat /proc/version
|find "WSL2"
> nul || (
echo|set /p=Attempting to convert the WSL distribution
%WSL_DISTR% to WSL2...
rem // this distribution needs to be converted
wsl --set-version %WSL_DISTR% 2 || (
echo Error: you need the Windows Subsystem
for Linux to support WSL2
for this
echo cross-build script to work consistently.
echo Please upgrade your Windows operating system to Windows 10 version 2004 build
echo 19041 or a later version.
)
rem // alright, this distribution is on WSL2
)
rem // make sure we're running from LOCAL STORAGE - if not, WSL2 will have relocated to $HOME and this will fuck everything up
echo|set /p=Checking whether WSL knows the path to this very directory...
wsl --distribution %WSL_DISTR% -- test ^"$^(pwd^)^" = ^"${HOME}^" && (
echo Error: you need to locate your working
copy of this SVN repository on *local*
echo storage
for the cross-build script to work consistently. The Windows Subsystem
echo for Linux cannot access Windows network shares.
echo Please relocate this repository on a local hard drive and try again.
)
rem // make sure we have the required tools, install them if not
set ETC_SUDOERS_PATCHED=0
rem // tool syntax: "<executable>:<optional APT package name>"
rem // if package name is not specified, it defaults to <executable>
set REQUIRED_TOOL_AND_PACKAGE=
%%i
for /f "tokens=1-2 delims=:"
%%j in ('
echo !REQUIRED_TOOL_AND_PACKAGE!'
) do (
if "
!REQUIRED_PACKAGE!"==""
set REQUIRED_PACKAGE=
!REQUIRED_TOOL!
)
echo|set /p=Checking
for the presence of
!REQUIRED_TOOL! from APT package !REQUIRED_PACKAGE!...
wsl --distribution
%WSL_DISTR% --
!REQUIRED_TOOL! --version
> nul 2
>&1
&& (
) || (
echo !REQUIRED_TOOL! needs installation. Attempting to install it.
if "
!ETC_SUDOERS_PATCHED!"=="0"
(
rem // allow ourselves to run any commands by patching /etc/sudoers. Do this once, and only if necessary. Also drop a note for posterity on why this was done.
wsl --distribution
%WSL_DISTR% -- sudo sh -c
^"grep -q '
%%sudo ALL=
^(ALL
^) NOPASSWD:ALL' /etc/sudoers
^|^| {
echo '';
echo '# Pierre-Marie Baty -- added automatically on
%DATE% by
%~nx0 script';
echo '# This line allows members of the sudo group to execute any command WITHOUT the need to enter the root password';
echo '
%%sudo ALL=
^(ALL
^) NOPASSWD:ALL'; }
^>^> /etc/sudoers
^"
set ETC_SUDOERS_PATCHED=1
rem // TEST if we have a working network from within WSL. It's no point waiting around if we don't. If it turns out we don't, jump to the relevant label
echo Testing network connectivity...
wsl --distribution
%WSL_DISTR% -- ping -c 1
%WSL_SOURCE% > nul 2
>&1
|| goto :
no_network
rem // we MIGHT have a working network. Update APT cache before installing stuff, but just once. If it turns out we can't, jump to the relevant label
wsl --distribution
%WSL_DISTR% -- sudo apt-get -y update
|| goto :
no_network
rem // at this point we can consider that networking works from within WSL.
)
rem // attempt installation of this package from the network. If it fails, assume it's a network problem (cable unplugged?) and jump to the relevant label
wsl --distribution
%WSL_DISTR% sudo apt-get -y install
!REQUIRED_PACKAGE! || goto :
no_network
)
)
rem // if we reach here it means all the tools were successfully installed from the network, so jump to the next step
:no_network
rem // if we reach here, it means package installation from the network failed. Stop being nice and stuff them up Linux's ass with maximal pain from local storage.
echo Looks like the network is unavailable. Nevermind the bollocks.
echo Installing all the necessary packages at once...
wsl --distribution
%WSL_DISTR% sudo dpkg -i "../../Third-party software/
%WSL_DISTR:-= % Debian packages
for WSL2/"*.deb
|| (
echo Error: the required Linux tools
%REQUIRED_TOOLS% can't be installed.
)
rem // nuff said.
:packages_installed
rem // setup the QNX SDP on the WSL side
rem // The build script initially recursively transferred the contents of $QNX_HOST and $QNX_TARGET to the WSL2 ext4 partition
rem // when necessary using the Plan9 NTFS to ext4 bridge (9p file protocol) implemented by Microsoft to transfer files
rem // from Windows to WSL2. The only problem with this is, and Microsoft admits it, that 9p is *catastrophically slow*.
rem // See https://learn.microsoft.com/en-us/windows/wsl/compare-versions - And when Microsoft says "slow", hear "unusable".
rem // To make this faster, here's what I do:
rem // 1. fire up an archiver and create a SINGLE FILE archive of the directory hierarchy to copy
rem // 2. hand the file over to WSL2
rem // 3. unpack the archive in place - as it's much faster to migrate a single file than a folder hierarchy.
rem // The only prerequisite shall be that the archive format be *operable with the tools at hand on both systems*.
rem // By using the "usr\bin\tar.exe" utility from the QNX8 Win64 host SDP tools and piping the data through a WSL2 instance running
rem // the Linux version of /usr/bin/tar to untar it directly at its location, we achieve 10x faster transfer speeds than 9p.
rem // This is just ridiculous. I scoured the web for hours looking for an acceptable file transfer solution from Windows to WSL2
rem // and it looks just like nobody found that one yet. Ah well...
rem // see if the SDP needs to be copied at all. Only do this when the modification time of the SDP directory on Windows is more recent than the one on Linux.
rem // So test for the presence of the symlinks state file as it's the last file that's created (this ensures a complete copy), then check for the directory mtimes.
rem // NOTE: on WSL2, the QNX SDK will always be copied to $HOME, regardless of what xdg-user-dir DESKTOP says.
echo|set /p=Checking
if the
%QNXSDK_DIRNAME% directory is present
in WSL and up to date...
wsl --distribution %WSL_DISTR% -- test ! -f "${HOME}/%QNXSDK_DIRNAME%/.symlinks-state" -o "../%QNXSDK_DIRNAME%" -nt "${HOME}/%QNXSDK_DIRNAME%" && (
rem // copy needed - cleanup and create the directories we'll need in $HOME to accomodate the QNX SDK. We want the Linux host tools, and the QNX target sysroot files
echo|set /p=Preparing to
copy QNX8 SDK files to WSL2 ext4 filesystem...
wsl --distribution
%WSL_DISTR% -- rm -rf "${HOME}/
%QNXSDK_DIRNAME%";
mkdir -p "${HOME}/
%QNXSDK_DIRNAME%/host" "${HOME}/
%QNXSDK_DIRNAME%/target" 2
>nul
rem // transfer the QNX SDK host files: tar on Windows, pipe to WSL2, untar on Linux - or die
echo|set /p=Deploying QNX8 SDK Linux host files to WSL2 ext4 filesystem
^(this can take some time
^)...
"..\
%QNXSDK_DIRNAME%\host\win64\x86_64\usr\bin\tar.exe" -c --directory="..\
%QNXSDK_DIRNAME%\host" linux
| wsl --distribution
%WSL_DISTR% -- tar -x --directory="${HOME}/
%QNXSDK_DIRNAME%/host"
|| ( echo failed
& goto :
exit_error )
rem // Windows has absolutely no notion of UNIX permissions. The best we can do here is to arbitrarily set the executable flags on relevant parts of the migrated tree.
echo|set /p=Setting executable permissions to QNX8 SDK toolchain files...
wsl --distribution
%WSL_DISTR% -- chmod -R +x "${HOME}/
%QNXSDK_DIRNAME%/host/linux/x86_64/usr/bin" "${HOME}/
%QNXSDK_DIRNAME%/host/linux/x86_64/usr/lib/gcc"
|| goto :
exit_error
rem // transfer the QNX SDK target files: tar on Windows, pipe to WSL2, untar on Linux - or die
echo|set /p=Deploying QNX8 SDK target sysroot files to WSL2 ext4 filesystem
^(this WILL take some time
^)...
"..\
%QNXSDK_DIRNAME%\host\win64\x86_64\usr\bin\tar.exe" -c --directory="..\
%QNXSDK_DIRNAME%\target" qnx
| wsl --distribution
%WSL_DISTR% -- tar -x --directory="${HOME}/
%QNXSDK_DIRNAME%/target"
|| ( echo failed
& goto :
exit_error )
rem // setup the toolchain symlinks
echo Setting up the QNX SDP platform-specific symlinks...
"..\
%QNXSDK_DIRNAME%\host\win64\x86_64\usr\bin\tar.exe" -c --directory="..\
%QNXSDK_DIRNAME%" symlinks.sh
| wsl --distribution
%WSL_DISTR% -- tar -x --directory="${HOME}/
%QNXSDK_DIRNAME%"
|| ( echo failed
& goto :
exit_error )
wsl --distribution
%WSL_DISTR% --
cd "${HOME}/
%QNXSDK_DIRNAME%"
^&^& find . -name symlinks.lst -exec ./symlinks.sh {} create $1 \;
^&^& printf 'present-v2'
^> "${HOME}/
%QNXSDK_DIRNAME%/.symlinks-state"
|| goto :
exit_error
)
rem // now chain-call the POSIX Bourne shell build script
echo Running POSIX build script...
wsl --distribution
%WSL_DISTR% -- ./cross-build.sh
|| goto :
exit_error
rem // at this point, the story is supposed to have ended well.
rem // Keep calm, and I'll tell you another one tomorrow.
echo The build ended successfully.
:install_wsl
echo Installing the Windows Subsystem
for Linux...
echo __________________________________________________________________________
echo ^| NOTE: please define a *non-root* Linux user name and password when asked
^|
echo ^| ^(for example: 'utilisateur' / 'utilisateur'
^) ^|
echo ^| then type '
exit' at the Linux shell
^(green prompt
^) to continue.
^|
echo ^|__________________________________________________________________________
^|
wsl --install --distribution %WSL_DISTR% || (
echo Error: you need at least a version of Windows 10 which has the "wsl" command.
echo Please upgrade your Windows operating system to Windows 10 version 2004 build
echo 19041 or a later version.
)
echo The Windows Subsystem
for Linux was successfully installed with the
%WSL_DISTR% distribution.
echo Action required: please reboot your computer
if requested and run this script again.
:exit_error
rem // failure exit
:exit_success
rem // successful exit