Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sokol #10381

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
79 changes: 79 additions & 0 deletions S/Sokol/build_tarballs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using BinaryBuilder
import Pkg: PackageSpec

name = "Sokol"
version = v"2025.28.1" # Use the commit date or tag of Sokol you're targeting

# Use the latest commit or a specific tag from the Sokol repository
sources = [
GitSource("https://github.com/floooh/sokol.git", "db9ebdf24243572c190affde269b92725942ddd0"),
ArchiveSource("https://github.com/phracker/MacOSX-SDKs/releases/download/10.15/MacOSX10.14.sdk.tar.xz",
"0f03869f72df8705b832910517b47dd5b79eb4e160512602f593ed243b28715f"),
DirectorySource("./bundled"),
]

# Build script
script = raw"""
cd $WORKSPACE/srcdir/sokol*
export CFLAGS="-I${includedir}"
cp ${WORKSPACE}/srcdir/files/CMakeLists.txt ./CMakeLists.txt
cp ${WORKSPACE}/srcdir/files/sokol.c ./sokol.c

if [[ "${bb_full_target}" == x86_64-apple-darwin* ]]; then
# LLVM 15+ requires macOS SDK 10.14.
pushd $WORKSPACE/srcdir/MacOSX10.*.sdk
rm -rf /opt/${target}/${target}/sys-root/System
cp -ra usr/* "/opt/${target}/${target}/sys-root/usr/."
cp -ra System "/opt/${target}/${target}/sys-root/."
export MACOSX_DEPLOYMENT_TARGET=10.14
popd
fi

if [[ ${target} == aarch64-apple-* ]]; then
# `libclang_rt.osx.a` from LLVM compiler-rt.
FLAGS+=(-DCMAKE_SHARED_LINKER_FLAGS="-L${libdir}/darwin -lclang_rt.osx"
-DCMAKE_EXE_LINKER_FLAGS="-L${libdir}/darwin -lclang_rt.osx"
-DUSE_METAL=ON
)
fi

cmake -B build \
-DCMAKE_INSTALL_PREFIX=$prefix \
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
-DCMAKE_BUILD_TYPE=Release \
"${FLAGS[@]}"

cmake --build build

install -Dvm 755 "build/libsokol.${dlext}" "${libdir}/libsokol.${dlext}"
for file in sokol_*.h; do
install -Dvm 644 "${file}" -t "${includedir}"
done
install_license LICENSE
"""

# Supported platforms
platforms = supported_platforms(exclude=p->arch(p)=="armv6l"||Sys.isfreebsd(p)||arch(p)=="riscv64")
llvm_version = v"13.0.1+1"

# Platform-specific dependencies
dependencies = [
# Linux dependencies
Dependency("Xorg_libX11_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("Xorg_libXrandr_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("Xorg_libXi_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("Xorg_libXcursor_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("Xorg_libXinerama_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("Libglvnd_jll"; platforms=filter(Sys.islinux, platforms)),
Dependency("alsa_jll"; platforms=filter(Sys.islinux, platforms)),
BuildDependency("Xorg_xorgproto_jll"; platforms=filter(Sys.islinux, platforms)),
BuildDependency(PackageSpec(name="LLVMCompilerRT_jll", uuid="4e17d02c-6bf5-513e-be62-445f41c75a11", version=llvm_version);
platforms=[Platform("aarch64", "macos")])
]

# Library products
products = [
LibraryProduct("libsokol", :libsokol)
]

build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies; julia_compat="1.6", preferred_gcc_version=v"7", preferred_llvm_version=llvm_version)
25 changes: 25 additions & 0 deletions S/Sokol/bundled/files/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.0)
project(libsokol)
set(CMAKE_C_STANDARD 11)
option(USE_METAL "Enable/disable Metal" OFF)
# Linux -pthread shenanigans
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
endif()
add_library(sokol SHARED sokol.c)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
# compile sokol.c as Objective-C
target_compile_options(sokol PRIVATE -x objective-c)
if(USE_METAL)
target_link_libraries(sokol "-framework QuartzCore" "-framework Cocoa" "-framework MetalKit" "-framework Metal" "-framework AudioToolbox")
else()
target_link_libraries(sokol "-framework OpenGL" "-framework QuartzCore" "-framework Cocoa" "-framework AudioToolbox")
endif()
else()
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
target_link_libraries(sokol INTERFACE X11 Xi Xcursor GL asound dl m)
target_link_libraries(sokol PUBLIC Threads::Threads)
endif()
endif()
target_include_directories(sokol INTERFACE sokol)
24 changes: 24 additions & 0 deletions S/Sokol/bundled/files/sokol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define SOKOL_NO_ENTRY
#define SOKOL_IMPL
#if defined(_MSC_VER)
#define SOKOL_D3D11
#define SOKOL_LOG(str) OutputDebugStringA(str)
#elif defined(__APPLE__) && defined(__arm64__)
// NOTE: on macOS, sokol.c is compiled explicitly as ObjC
#include <TargetConditionals.h>
#define SOKOL_METAL
#elif defined(__APPLE__) && defined(__x86_64__)
#include <TargetConditionals.h>
#define SOKOL_GLCORE
#else
#define SOKOL_GLCORE
#endif
#define SOKOL_WIN32_FORCE_MAIN
#include "sokol_audio.h"
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_time.h"
#include "sokol_glue.h"
#include "sokol_fetch.h"
#include "sokol_log.h"
#include "sokol_args.h"