Skip to content

Commit

Permalink
Use zig cc for SwiftShader on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey committed Oct 29, 2024
1 parent 6f709a9 commit a99bba0
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 26 deletions.
29 changes: 3 additions & 26 deletions .github/workflows/swiftshader.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,6 @@ jobs:
- os: ubuntu-22.04
name: Linux
nuke_invoke: ./build.sh
extras: |
# We need to adjust APT sources for multiarch. Use the ones corresponding to
# Ubuntu 22.04 with appropriate `arch` values. ports.ubuntu.com is required
# for armhf and arm64.
sudo tee /etc/apt/sources.list << EOF
deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy main multiverse restricted universe
deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy-backports main multiverse restricted universe
deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy-security main multiverse restricted universe
deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy-updates main multiverse restricted universe
deb [arch=arm64,armhf] http://ports.ubuntu.com jammy main multiverse restricted universe
deb [arch=arm64,armhf] http://ports.ubuntu.com jammy-backports main multiverse restricted universe
deb [arch=arm64,armhf] http://ports.ubuntu.com jammy-security main multiverse restricted universe
deb [arch=arm64,armhf] http://ports.ubuntu.com jammy-updates main multiverse restricted universe
deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy main multiverse restricted universe
deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-backports main multiverse restricted universe
deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-security main multiverse restricted universe
deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-updates main multiverse restricted universe
EOF
sudo dpkg --add-architecture arm64
sudo dpkg --add-architecture armhf
sudo apt update
sudo apt install -y gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf
sudo apt install -y g++-aarch64-linux-gnu g++-arm-linux-gnueabihf
for arch in amd64 arm64 armhf; do
sudo apt install -y libx11-xcb-dev:$arch
done
- os: windows-2022
name: Windows
nuke_invoke: ./build.cmd
Expand Down Expand Up @@ -83,6 +57,9 @@ jobs:
dotnet-version: |
6.0.201
7.0.*
- name: Install Zig
if: ${{ matrix.env.name == 'Linux' }}
uses: goto-bus-stop/setup-zig@v2
- name: Build SwiftShader
run: ${{ matrix.env.nuke_invoke }} SwiftShader
env:
Expand Down
36 changes: 36 additions & 0 deletions build/cmake/fudge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Origignal script from https://github.com/ziglang/zig/issues/4911
# Modified to add SILKDOTNET_ paths in response to some issues with SwiftShader

args=""
for arg in "$@"
do
parg="$arg"

option=${arg%=*}
target=${arg#*=}
if [[ $option == "-march" || $option == "-mcpu" || $option == "-mtune" ]]; then
moveon=0
for replace in $SILKDOTNET_ReplaceArchitectureZigCcFlags
do
replacetarget=${replace%=*}
replacement=${replace#*=}
# echo $replacetarget A $replacement B $target END
if [[ $replacetarget == $target ]]; then
if [[ "$replacement" == "" ]]; then
moveon=1
else
target="$replacement"
fi
fi
done
if [[ $moveon == 1 || "$SILKDOTNET_RemoveAllPotentiallyProblematicZigCcFlags" == "1" ]]; then
continue
else
fixedTarget=${target//-/_}
parg="$option=$fixedTarget"
fi
fi
args="$args $parg"
done
2 changes: 2 additions & 0 deletions build/cmake/zig-ar.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
zig ar %*
2 changes: 2 additions & 0 deletions build/cmake/zig-ar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
zig ar "$@"
3 changes: 3 additions & 0 deletions build/cmake/zig-c++.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
: TODO add -march/-mtune handling if necessary
zig c++ %*
3 changes: 3 additions & 0 deletions build/cmake/zig-c++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
. "$(dirname ${BASH_SOURCE[0]})/fudge.sh" $args
zig c++ $args
3 changes: 3 additions & 0 deletions build/cmake/zig-cc.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
: TODO add -march/-mtune handling if necessary
zig cc %*
3 changes: 3 additions & 0 deletions build/cmake/zig-cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
. "$(dirname ${BASH_SOURCE[0]})/fudge.sh" $args
zig cc $args
2 changes: 2 additions & 0 deletions build/cmake/zig-ranlib.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
zig ranlib %*
2 changes: 2 additions & 0 deletions build/cmake/zig-ranlib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
zig ranlib "$@"
2 changes: 2 additions & 0 deletions build/cmake/zig-rc.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
zig rc %*
2 changes: 2 additions & 0 deletions build/cmake/zig-rc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
zig rc "$@"
2 changes: 2 additions & 0 deletions build/cmake/zig-toolchain-aarch64-linux-gnu.2.17.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(ZIG_TARGET "aarch64-linux-gnu.2.17")
include(${CMAKE_CURRENT_LIST_DIR}/zig-toolchain.cmake)
2 changes: 2 additions & 0 deletions build/cmake/zig-toolchain-arm-linux-gnueabihf.2.17.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(ZIG_TARGET "arm-linux-gnueabihf.2.17")
include(${CMAKE_CURRENT_LIST_DIR}/zig-toolchain.cmake)
2 changes: 2 additions & 0 deletions build/cmake/zig-toolchain-x86_64-linux-gnu.2.17.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(ZIG_TARGET "x86_64-linux-gnu.2.17")
include(${CMAKE_CURRENT_LIST_DIR}/zig-toolchain.cmake)
40 changes: 40 additions & 0 deletions build/cmake/zig-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include_guard()

if(CMAKE_GENERATOR MATCHES "Visual Studio")
message(FATAL_ERROR "Visual Studio generator not supported, use: cmake -G Ninja")
endif()

if(NOT ZIG_TARGET MATCHES "^([a-zZ-Z0-9_]+)-([a-zZ-Z0-9_]+)-([a-zZ-Z0-9_.]+)$")
message(FATAL_ERROR "Expected -DZIG_TARGET=<arch>-<os>-<abi>")
endif()

set(ZIG_ARCH ${CMAKE_MATCH_1})
set(ZIG_OS ${CMAKE_MATCH_2})
set(ZIG_ABI ${CMAKE_MATCH_3})

if(ZIG_OS STREQUAL "linux")
set(CMAKE_SYSTEM_NAME "Linux")
elseif(ZIG_OS STREQUAL "windows")
set(CMAKE_SYSTEM_NAME "Windows")
elseif(ZIG_OS STREQUAL "macos")
set(CMAKE_SYSTEM_NAME "Darwin")
else()
message(WARNING "Unknown OS: ${ZIG_OS}")
endif()

set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR ${ZIG_ARCH})

if(WIN32)
set(SCRIPT_SUFFIX ".cmd")
else()
set(SCRIPT_SUFFIX ".sh")
endif()

# This is working (thanks to Simon for finding this trick)
set(CMAKE_AR "${CMAKE_CURRENT_LIST_DIR}/zig-ar${SCRIPT_SUFFIX}")
set(CMAKE_RANLIB "${CMAKE_CURRENT_LIST_DIR}/zig-ranlib${SCRIPT_SUFFIX}")
set(CMAKE_RC_COMPILER "${CMAKE_CURRENT_LIST_DIR}/zig-rc${SCRIPT_SUFFIX}")
set(CMAKE_ASM_COMPILER "${CMAKE_CURRENT_LIST_DIR}/zig-cc${SCRIPT_SUFFIX}" -target ${ZIG_TARGET})
set(CMAKE_C_COMPILER "${CMAKE_CURRENT_LIST_DIR}/zig-cc${SCRIPT_SUFFIX}" -target ${ZIG_TARGET})
set(CMAKE_CXX_COMPILER "${CMAKE_CURRENT_LIST_DIR}/zig-c++${SCRIPT_SUFFIX}" -target ${ZIG_TARGET})

0 comments on commit a99bba0

Please sign in to comment.