Files
lean-sdl3/lakefile.lean
T

276 lines
11 KiB
Lean4
Raw Normal View History

2025-09-20 16:11:27 -07:00
import Lake
open System Lake DSL
package SDL3
def sdlGitRepo : String := "https://github.com/libsdl-org/SDL.git"
-- pin to a specific commit to avoid breakages
def sdlGitRev : String := "f3a9f66292d49322652be01ee93412d0e9b74f0b"
2025-09-20 16:11:27 -07:00
def sdlImageGitRepo : String := "https://github.com/libsdl-org/SDL_image.git"
def sdlImageGitRev : String := "d354e3d5146117f8b2f14096800965e56f9f7bfc"
2025-09-23 20:54:45 -07:00
def sdlTtfGitRepo : String := "https://github.com/libsdl-org/SDL_ttf.git"
def sdlTtfGitRev : String := "6b6bd588e8646360b08f624fb601cc2ec75c6ada"
def sdlMixerGitRepo : String := "https://github.com/libsdl-org/SDL_mixer.git"
def sdlMixerGitRev : String := "5cdf029bae982df1d6c210f915fc151a616d982f"
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
input_file sdl.c where
path := "c" / "sdl.c"
text := true
2025-09-21 19:36:32 -07:00
target sdlDir pkg : FilePath := do
return .pure (pkg.dir / "vendor" / "SDL")
target sdlImageDir pkg : FilePath := do
return .pure (pkg.dir / "vendor" / "SDL_image")
2025-09-23 20:54:45 -07:00
target sdlTtfDir pkg : FilePath := do
return .pure (pkg.dir / "vendor" / "SDL_ttf")
target sdlMixerDir pkg : FilePath := do
return .pure (pkg.dir / "vendor" / "SDL_mixer")
2025-09-24 19:20:17 -07:00
def cloneGitRepo (repo : String) (rev : String) (dstDir : FilePath) : FetchM (Unit) := do
let doesExist ← dstDir.pathExists
if !doesExist then
logInfo s!"Cloning {repo} into {dstDir}"
let clone ← IO.Process.output { cmd := "git", args := #["clone", "--revision", rev, "--single-branch", "--depth", "1", "--recursive", repo, dstDir.toString] }
if clone.exitCode != 0 then
logError s!"Error cloning {repo}: {clone.stderr}"
else
logInfo s!"{repo} cloned successfully"
logInfo clone.stdout
else
logInfo s!"Directory {dstDir} already exists, skipping clone"
pure ()
def copyBinaries (sourceDir : FilePath) : FetchM (Unit) := do
-- manually copy the DLLs we need to .lake/build/bin/ in the root directory for the game to work
let dstDir := ((<- getRootPackage).binDir)
IO.FS.createDirAll dstDir
logInfo s!"Copying binaries from {sourceDir} to {dstDir}"
let binariesDir : FilePath := sourceDir / "build"
for entry in (← binariesDir.readDir) do
if entry.path.extension != none then
copyFile entry.path (dstDir / entry.path.fileName.get!)
pure ()
2025-09-21 21:40:01 -07:00
target sdl.o pkg : FilePath := do
let srcJob ← sdl.c.fetch
let oFile := pkg.buildDir / "c" / "sdl.o"
let leanInclude := (<- getLeanIncludeDir).toString
let sdlRepoDir : FilePath ← (← sdlDir.fetch).await
let sdlImageDir : FilePath ← (← sdlImageDir.fetch).await
2025-09-23 21:21:42 -07:00
let sdlTtfDir : FilePath ← (← sdlTtfDir.fetch).await
let sdlMixerDir : FilePath ← (← sdlMixerDir.fetch).await
2025-09-21 21:40:01 -07:00
let sdlInclude := sdlRepoDir / "include/"
let sdlImageInclude := sdlImageDir / "include/"
2025-09-23 21:21:42 -07:00
let sdlTtfInclude := sdlTtfDir / "include/"
let sdlMixerInclude := sdlMixerDir / "include/"
2025-09-21 21:40:01 -07:00
buildO oFile srcJob #[] #["-fPIC", s!"-I{sdlInclude}", s!"-I{sdlImageInclude}", s!"-I{sdlTtfInclude}", s!"-I{sdlMixerInclude}", "-D_REENTRANT", s!"-I{leanInclude}"] compiler
2025-09-21 21:40:01 -07:00
2025-09-24 19:20:17 -07:00
def buildSDL3 : FetchM (Unit) := do
2025-09-21 20:06:29 -07:00
let sdlRepoDir : FilePath ← (← sdlDir.fetch).await
2025-09-22 12:39:18 -07:00
logInfo "Building SDL"
2025-09-21 19:36:32 -07:00
-- Create build directory if it doesn't exist
let sdlBuildDirExists ← System.FilePath.pathExists (sdlRepoDir / "build")
if !sdlBuildDirExists then
let configureSdlBuild ← IO.Process.output { cmd := "cmake", args := #["-S", sdlRepoDir.toString, "-B", (sdlRepoDir / "build").toString, "-DBUILD_SHARED_LIBS=ON", "-DCMAKE_BUILD_TYPE=Release", s!"-DCMAKE_C_COMPILER={compiler}"] }
if configureSdlBuild.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error configuring SDL: {configureSdlBuild.stderr}"
2025-09-21 19:36:32 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL configured successfully"
logInfo configureSdlBuild.stdout
2025-09-21 19:36:32 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL build directory already exists, skipping configuration step"
2025-09-21 19:36:32 -07:00
-- now actually build SDL once we've configured it
let buildSdl ← IO.Process.output { cmd := "cmake", args := #["--build", (sdlRepoDir / "build").toString, "--config", "Release"] }
if buildSdl.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error building SDL: {buildSdl.exitCode}"
logError buildSdl.stderr
2025-09-21 19:36:32 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL built successfully"
logInfo buildSdl.stdout
2025-09-24 19:20:17 -07:00
logInfo "SDL built successfully, copying binaries"
pure ()
target libSDL3 : Dynlib := Job.async do
let sdlRepoDir : FilePath ← (← sdlDir.fetch).await
2025-09-21 19:36:32 -07:00
return {
name := "SDL3"
2025-09-23 20:54:45 -07:00
path := sdlRepoDir / "build" / nameToSharedLib "SDL3"
2025-09-21 19:36:32 -07:00
}
2025-09-20 16:11:27 -07:00
2025-09-24 19:20:17 -07:00
def buildSDL3Image : FetchM (Unit) := do
2025-09-21 19:36:32 -07:00
let sdlRepoDir : FilePath ← (<- sdlDir.fetch).await
let sdlImageRepoDir : FilePath ← (<- sdlImageDir.fetch).await
2025-09-22 12:39:18 -07:00
logInfo "Building SDL_image"
2025-09-21 19:36:32 -07:00
-- Create build directory if it doesn't exist
2025-09-21 17:05:02 -07:00
let sdlImageBuildDirExists ← System.FilePath.pathExists (sdlImageRepoDir / "build")
2025-09-20 16:11:27 -07:00
if !sdlImageBuildDirExists then
2025-09-21 19:36:32 -07:00
let configureSdlImageBuild ← IO.Process.output { cmd := "cmake", args := #["-S", sdlImageRepoDir.toString, "-B", (sdlImageRepoDir / "build").toString, s!"-DSDL3_DIR={sdlRepoDir / "build"}", "-DBUILD_SHARED_LIBS=ON", "-DCMAKE_BUILD_TYPE=Release", s!"-DCMAKE_C_COMPILER={compiler}"] }
2025-09-20 16:11:27 -07:00
if configureSdlImageBuild.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error configuring SDL_image: {configureSdlImageBuild.stderr}"
2025-09-20 16:11:27 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL_image configured successfully"
logInfo configureSdlImageBuild.stdout
2025-09-20 16:11:27 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL_image build directory already exists, skipping configuration step"
2025-09-21 19:36:32 -07:00
-- now actually build SDL_image once we've configured it
let buildSdlImage ← IO.Process.output { cmd := "cmake", args := #["--build", (sdlImageRepoDir / "build").toString, "--config", "Release"] }
2025-09-20 16:11:27 -07:00
if buildSdlImage.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error building SDL_image: {buildSdlImage.exitCode}"
logError buildSdlImage.stderr
2025-09-20 16:11:27 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL_image built successfully"
logInfo buildSdlImage.stdout
2025-09-21 19:36:32 -07:00
-- Return built dynlib
2025-09-24 19:20:17 -07:00
logInfo "SDL_image built successfully"
target libSDL3Image : Dynlib := Job.async do
let sdlImageRepoDir : FilePath ← (← sdlImageDir.fetch).await
2025-09-21 19:36:32 -07:00
return {
name := "SDL3_image"
2025-09-23 20:54:45 -07:00
path := sdlImageRepoDir / "build" / nameToSharedLib "SDL3_image"
2025-09-21 19:36:32 -07:00
}
2025-09-24 19:20:17 -07:00
def buildSDL3Ttf : FetchM (Unit) := do
2025-09-23 20:54:45 -07:00
let sdlRepoDir : FilePath ← (<- sdlDir.fetch).await
let sdlTtfRepoDir : FilePath ← (<- sdlTtfDir.fetch).await
logInfo "Building SDL_ttf"
-- Create build directory if it doesn't exist
let sdlTtfBuildDirExists ← System.FilePath.pathExists (sdlTtfRepoDir / "build")
if !sdlTtfBuildDirExists then
-- tell SDL_ttf to vendor its own dependencies
let configureSdlTtfBuild ← IO.Process.output {
cmd := "cmake",
args := #[
"-S",
sdlTtfRepoDir.toString,
"-B",
(sdlTtfRepoDir / "build").toString,
s!"-DSDL3_DIR={sdlRepoDir / "build"}",
"-DBUILD_SHARED_LIBS=ON",
"-DCMAKE_BUILD_TYPE=Release",
s!"-DCMAKE_C_COMPILER={compiler}",
s!"-DSDLTTF_VENDORED=true"
]
}
2025-09-23 20:54:45 -07:00
if configureSdlTtfBuild.exitCode != 0 then
logError s!"Error configuring SDL_ttf: {configureSdlTtfBuild.stderr}"
else
logInfo "SDL_ttf configured successfully"
logInfo configureSdlTtfBuild.stdout
else
logInfo "SDL_ttf build directory already exists, skipping configuration step"
-- now actually build SDL_ttf once we've configured it
let buildSdlTtf ← IO.Process.output { cmd := "cmake", args := #["--build", (sdlTtfRepoDir / "build").toString, "--config", "Release"] }
if buildSdlTtf.exitCode != 0 then
logError s!"Error building SDL_ttf: {buildSdlTtf.exitCode}"
logError buildSdlTtf.stderr
else
logInfo "SDL_ttf built successfully"
logInfo buildSdlTtf.stdout
-- Return built dynlib
2025-09-24 19:20:17 -07:00
logInfo "SDL_ttf built successfully"
target libSDL3Ttf : Dynlib := Job.async do
let sdlTtfRepoDir : FilePath ← (← sdlTtfDir.fetch).await
2025-09-23 20:54:45 -07:00
return {
name := "SDL3_ttf"
path := sdlTtfRepoDir / "build" / nameToSharedLib "SDL3_ttf"
}
2025-09-24 19:20:17 -07:00
def buildSDL3Mixer : FetchM (Unit) := do
let sdlRepoDir : FilePath ← (← sdlDir.fetch).await
let sdlMixerRepoDir : FilePath ← (← sdlMixerDir.fetch).await
logInfo "Building SDL_mixer"
let sdlMixerBuildDir := sdlMixerRepoDir / "build"
let sdlMixerBuildDirExists ← sdlMixerBuildDir.pathExists
if !sdlMixerBuildDirExists then
let configureSdlMixerBuild ← IO.Process.output {
cmd := "cmake",
args := #[
"-S", sdlMixerRepoDir.toString,
"-B", sdlMixerBuildDir.toString,
s!"-DSDL3_DIR={sdlRepoDir / "build"}",
"-DBUILD_SHARED_LIBS=ON",
"-DCMAKE_BUILD_TYPE=Release",
s!"-DCMAKE_C_COMPILER={compiler}",
s!"-DCMAKE_PREFIX_PATH={sdlRepoDir / "build"}",
s!"-DSDLTTF_VENDORED=true"
]
}
if configureSdlMixerBuild.exitCode != 0 then
logError s!"Error configuring SDL_mixer: {configureSdlMixerBuild.stderr}"
logInfo "SDL_mixer configured successfully"
else
logInfo "SDL_mixer build directory already exists, skipping configuration step"
let buildSdlMixer ← IO.Process.output { cmd := "cmake", args := #["--build", sdlMixerBuildDir.toString, "--config", "Release"] }
if buildSdlMixer.exitCode != 0 then
logError s!"Error building SDL_mixer: {buildSdlMixer.exitCode}"
logError s!"SDL_mixer build stderr: {buildSdlMixer.stderr}"
logInfo "SDL_mixer built successfully"
2025-09-24 19:20:17 -07:00
target libSDL3Mixer : Dynlib := Job.async do
let sdlMixerRepoDir : FilePath ← (← sdlMixerDir.fetch).await
return {
name := "SDL3_mixer"
path := sdlMixerRepoDir / "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
2025-09-24 19:20:17 -07:00
-- clone the git repositories we need so we can build them later
let sdlRepoDir ← (← sdlDir.fetch).await
let sdlImageRepoDir ← (← sdlImageDir.fetch).await
let sdlTtfRepoDir ← (← sdlTtfDir.fetch).await
let sdlMixerRepoDir ← (← sdlMixerDir.fetch).await
2025-09-23 20:54:45 -07:00
2025-09-24 19:20:17 -07:00
cloneGitRepo sdlGitRepo sdlGitRev sdlRepoDir
cloneGitRepo sdlImageGitRepo sdlImageGitRev sdlImageRepoDir
cloneGitRepo sdlTtfGitRepo sdlTtfGitRev sdlTtfRepoDir
cloneGitRepo sdlMixerGitRepo sdlMixerGitRev sdlMixerRepoDir
-- build all the libraries we need
buildSDL3
buildSDL3Image
buildSDL3Ttf
buildSDL3Mixer
logInfo "All libraries built successfully"
copyBinaries sdlRepoDir
copyBinaries sdlImageRepoDir
copyBinaries sdlTtfRepoDir
copyBinaries sdlMixerRepoDir
2025-09-21 20:06:29 -07:00
let sdlO ← sdl.o.fetch
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
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
2025-09-21 21:28:09 -07:00
-- make sure to copy these link args into whatever project is using this library in order for it to work
2025-09-21 21:40:01 -07:00
-- 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).
moreLinkArgs := if !Platform.isWindows then #["-Wl,--allow-shlib-undefined", "-Wl,-rpath=$ORIGIN"] else #[]
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
2025-09-23 20:06:23 -07:00
moreLinkArgs := if !Platform.isWindows then #["-Wl,--allow-shlib-undefined", "-Wl,-rpath=$ORIGIN"] else #[]