Files
lean-sdl3/lakefile.lean
T

187 lines
5.8 KiB
Lean4
Raw Normal View History

2025-09-20 16:11:27 -07:00
import Lake
open System Lake DSL
package SDL3
-- pin to a specific commit to avoid breakages
2026-01-19 20:40:24 -08:00
structure GitDep where
repo : String
rev : String
2026-01-22 02:38:05 -08:00
name : String
def GitDep.dir (dep : GitDep) (pkg : Package) : FilePath :=
pkg.dir / "vendor" / dep.name
2026-01-19 20:40:24 -08:00
def sdlDep : GitDep := {
repo := "https://github.com/libsdl-org/SDL.git"
rev := "f3a9f66292d49322652be01ee93412d0e9b74f0b"
2026-01-22 02:38:05 -08:00
name := "SDL"
2026-01-19 20:40:24 -08:00
}
def sdlImageDep : GitDep := {
repo := "https://github.com/libsdl-org/SDL_image.git"
rev := "d354e3d5146117f8b2f14096800965e56f9f7bfc"
2026-01-22 02:38:05 -08:00
name := "SDL_image"
2026-01-19 20:40:24 -08:00
}
def sdlTtfDep : GitDep := {
repo := "https://github.com/libsdl-org/SDL_ttf.git"
rev := "6b6bd588e8646360b08f624fb601cc2ec75c6ada"
2026-01-22 02:38:05 -08:00
name := "SDL_ttf"
2026-01-19 20:40:24 -08:00
}
def sdlMixerDep : GitDep := {
repo := "https://github.com/libsdl-org/SDL_mixer.git"
rev := "5cdf029bae982df1d6c210f915fc151a616d982f"
2026-01-22 02:38:05 -08:00
name := "SDL_mixer"
2026-01-19 20:40:24 -08:00
}
2025-09-21 19:36:32 -07:00
-- TODO: at some point, we should figure out a better way to set the C compiler
def compiler := if Platform.isWindows then "gcc" else "cc"
2025-09-20 16:11:27 -07:00
2026-01-19 20:40:24 -08:00
def GitDep.clone (dep : GitDep) (dstDir : FilePath) : FetchM Unit := do
2026-01-20 20:20:27 -08:00
if (<- dstDir.pathExists) then
2025-09-24 19:20:17 -07:00
logInfo s!"Directory {dstDir} already exists, skipping clone"
2026-01-20 20:20:27 -08:00
else
logInfo s!"Cloning {dep.repo} into {dstDir}"
proc {
cmd := "git"
args := #["clone", "--revision", dep.rev,
"--single-branch", "--depth", "1", "--recursive", dep.repo, dstDir.toString
]
}
2025-09-24 19:20:17 -07:00
2026-01-23 04:34:32 -08:00
input_file sdlSrc where
path := "c" / "sdl.c"
text := true
2025-09-24 19:20:17 -07:00
2026-01-23 04:34:32 -08:00
target sdlObj pkg : FilePath := do
let srcJob ← sdlSrc.fetch
2025-09-21 21:40:01 -07:00
let oFile := pkg.buildDir / "c" / "sdl.o"
2026-01-20 20:20:27 -08:00
let leanInclude := <- getLeanIncludeDir
2025-09-21 21:40:01 -07:00
2026-01-22 02:38:05 -08:00
let includeDirs : List FilePath := [sdlDep, sdlImageDep, sdlTtfDep, sdlMixerDep].map
(fun dep => dep.dir pkg / "include/")
2025-09-21 21:40:01 -07:00
2026-01-21 12:15:43 -08:00
let flags: List String := ["-fPIC"] ++
includeDirs.map (fun dir => s!"-I{dir}") ++
["-D_REENTRANT", s!"-I{leanInclude}"]
let flags: Array String := flags.toArray
buildO oFile srcJob #[] flags compiler
2025-09-21 21:40:01 -07:00
2025-09-24 20:00:33 -07:00
def buildCMakeProject (repoDir : FilePath) (args : Array String): FetchM (Unit) := do
logInfo s!"Building {repoDir} with CMake with args {args}"
let buildDir := repoDir / "build"
let buildDirExists ← buildDir.pathExists
if !buildDirExists then
let configureBuild ← IO.Process.output {
cmd := "cmake",
args := #[
"-S", repoDir.toString,
"-B", buildDir.toString,
"-DBUILD_SHARED_LIBS=ON",
"-DCMAKE_BUILD_TYPE=Release",
s!"-DCMAKE_C_COMPILER={compiler}",] ++ args
}
if configureBuild.exitCode != 0 then
logError s!"Error configuring build: {configureBuild.stderr}"
logInfo "Build configured successfully"
2025-09-21 19:36:32 -07:00
else
2025-09-24 20:00:33 -07:00
logInfo "Build directory already exists, skipping configuration step"
let buildProject ← IO.Process.output { cmd := "cmake", args := #["--build", buildDir.toString, "--config", "Release"] }
if buildProject.exitCode != 0 then
logError s!"Error building project: {buildProject.exitCode}"
logError s!"Project build stderr: {buildProject.stderr}"
logInfo s!"{repoDir} built successfully"
2025-09-24 19:20:17 -07:00
2026-01-22 02:38:05 -08:00
target libSDL3 pkg : Dynlib := do
return .pure {
2025-09-21 19:36:32 -07:00
name := "SDL3"
2026-01-22 02:38:05 -08:00
path := sdlDep.dir pkg / "build" / nameToSharedLib "SDL3"
2025-09-21 19:36:32 -07:00
}
2025-09-20 16:11:27 -07:00
2026-01-22 02:38:05 -08:00
target libSDL3Image pkg : Dynlib := do
return .pure {
2025-09-21 19:36:32 -07:00
name := "SDL3_image"
2026-01-22 02:38:05 -08:00
path := sdlImageDep.dir pkg / "build" / nameToSharedLib "SDL3_image"
2025-09-21 19:36:32 -07:00
}
2026-01-22 02:38:05 -08:00
target libSDL3Ttf pkg : Dynlib := do
return .pure {
2025-09-23 20:54:45 -07:00
name := "SDL3_ttf"
2026-01-22 02:38:05 -08:00
path := sdlTtfDep.dir pkg / "build" / nameToSharedLib "SDL3_ttf"
2025-09-23 20:54:45 -07:00
}
2025-09-24 19:20:17 -07:00
2026-01-22 02:38:05 -08:00
target libSDL3Mixer pkg : Dynlib := do
return .pure {
name := "SDL3_mixer"
2026-01-22 02:38:05 -08:00
path := sdlMixerDep.dir pkg / "build" / nameToSharedLib "SDL3_mixer"
}
2025-09-23 20:54:45 -07:00
2025-09-21 20:06:29 -07:00
target libleansdl pkg : FilePath := do
2026-01-22 02:38:05 -08:00
let deps := [sdlDep, sdlImageDep, sdlTtfDep, sdlMixerDep]
2025-09-23 20:54:45 -07:00
2026-01-22 02:38:05 -08:00
-- clone the git repositories we need so we can build them later
for dep in deps do
dep.clone (dep.dir pkg)
2025-09-24 19:20:17 -07:00
-- build all the libraries we need
2026-01-23 04:34:32 -08:00
let sdlRepoBuildDir := (sdlDep.dir pkg / "build").toString
2026-01-22 02:38:05 -08:00
buildCMakeProject (sdlDep.dir pkg) #[]
2026-01-23 04:34:32 -08:00
buildCMakeProject (sdlImageDep.dir pkg) #["-DSDL3_DIR=" ++ sdlRepoBuildDir]
buildCMakeProject (sdlTtfDep.dir pkg) #["-DSDL3_DIR=" ++ sdlRepoBuildDir, "-DSDLTTF_VENDORED=true"]
buildCMakeProject (sdlMixerDep.dir pkg) #["-DSDL3_DIR=" ++ sdlRepoBuildDir, "-DSDLMIXER_VENDORED=true"]
2025-09-24 19:20:17 -07:00
logInfo "All libraries built successfully"
2026-01-23 04:34:32 -08:00
-- copy binaries
let binaryDstDir := ((<- getRootPackage).binDir)
IO.FS.createDirAll binaryDstDir
2025-09-21 20:06:29 -07:00
2026-01-23 04:34:32 -08:00
-- manually copy the DLLs we need to .lake/build/bin/ in the root directory for the game to work
for dep in deps do
let sourceDir := dep.dir pkg
logInfo s!"Copying binaries from {sourceDir} to {binaryDstDir}"
let buildDir := sourceDir / "build"
for entry in (← buildDir.readDir) do
if entry.path.extension != none then
copyFile entry.path (binaryDstDir / entry.path.fileName.get!)
let sdlO ← sdlObj.fetch
2025-09-21 20:06:29 -07:00
let name := nameToStaticLib "leansdl"
2025-09-20 16:11:27 -07:00
buildStaticLib (pkg.staticLibDir / name) #[sdlO]
def libList : TargetArray Dynlib := #[libSDL3, libSDL3Image, libSDL3Ttf, libSDL3Mixer]
2025-09-23 21:21:42 -07:00
2026-01-20 00:41:49 -08:00
-- make sure to copy these link args into whatever project is using this library in order for it to work
-- This is because without "-rpath=$ORIGIN", the Linux executable will not load dynlibs next to the executable (i.e., the SDL ones you've copied there).
def moreLinkArgs: Array String := if Platform.isWindows
then
#[]
else
#["-Wl,--allow-shlib-undefined", "-Wl,-rpath=$ORIGIN"]
2025-09-20 16:11:27 -07:00
@[default_target]
lean_lib SDL where
moreLinkObjs := #[libleansdl]
2025-09-23 21:21:42 -07:00
moreLinkLibs := libList
2026-01-20 00:41:49 -08:00
moreLinkArgs := moreLinkArgs
2025-09-23 20:06:23 -07:00
lean_exe «test-app» where
root := `TestApp
moreLinkObjs := #[libleansdl]
2025-09-23 21:21:42 -07:00
moreLinkLibs := libList
2026-01-20 00:41:49 -08:00
moreLinkArgs := moreLinkArgs
lean_exe «webcam-app» where
root := `WebcamApp
moreLinkObjs := #[libleansdl]
moreLinkLibs := libList
2026-01-20 00:41:49 -08:00
moreLinkArgs := moreLinkArgs