Files
lean-sdl3/lakefile.lean
T

181 lines
7.9 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"
def sdlImageGitRepo : String := "https://github.com/libsdl-org/SDL_image.git"
-- clone from a stable branch to avoid breakages
def sdlBranch : String := "release-3.2.x"
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-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
let sdlInclude := sdlRepoDir / "include/"
let sdlImageInclude := sdlImageDir / "include/"
buildO oFile srcJob #[] #["-fPIC", s!"-I{sdlInclude}", s!"-I{sdlImageInclude}", "-D_REENTRANT", s!"-I{leanInclude}"] compiler
2025-09-21 19:36:32 -07:00
target libSDL3 pkg : Dynlib := Job.async do
2025-09-21 20:06:29 -07:00
let sdlRepoDir : FilePath ← (← sdlDir.fetch).await
2025-09-20 16:11:27 -07:00
let sdlExists ← System.FilePath.pathExists sdlRepoDir
if !sdlExists then
2025-09-22 12:39:18 -07:00
logInfo "Cloning SDL"
2025-09-21 17:05:02 -07:00
let sdlClone ← IO.Process.output { cmd := "git", args := #["clone", "-b", sdlBranch, "--single-branch", "--depth", "1", "--recursive", sdlGitRepo, sdlRepoDir.toString] }
2025-09-20 16:11:27 -07:00
if sdlClone.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error cloning SDL: {sdlClone.stderr}"
2025-09-20 16:11:27 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL cloned successfully"
logInfo sdlClone.stdout
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-21 19:36:32 -07:00
-- Return built dynlib
return {
name := "SDL3"
path := pkg.dir / "vendor" / "SDL" / "build" / nameToSharedLib "SDL3"
}
2025-09-20 16:11:27 -07:00
2025-09-21 19:36:32 -07:00
target libSDL3Image pkg : Dynlib := Job.async do
let sdlRepoDir : FilePath ← (<- sdlDir.fetch).await
let sdlImageRepoDir : FilePath ← (<- sdlImageDir.fetch).await
2025-09-20 16:11:27 -07:00
let sdlImageExists ← System.FilePath.pathExists sdlImageRepoDir
if !sdlImageExists then
2025-09-22 12:39:18 -07:00
logInfo "Cloning SDL_image"
2025-09-21 17:05:02 -07:00
let sdlImageClone ← IO.Process.output { cmd := "git", args := #["clone", "-b", sdlBranch, "--single-branch", "--depth", "1", "--recursive", sdlImageGitRepo, sdlImageRepoDir.toString] }
2025-09-20 16:11:27 -07:00
if sdlImageClone.exitCode != 0 then
2025-09-22 12:39:18 -07:00
logError s!"Error cloning SDL_image: {sdlImageClone.stderr}"
2025-09-20 16:11:27 -07:00
else
2025-09-22 12:39:18 -07:00
logInfo "SDL_image cloned successfully"
logInfo sdlImageClone.stdout
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
return {
name := "SDL3_image"
path := pkg.dir / "vendor" / "SDL_image" / "build" / nameToSharedLib "SDL3_image"
}
2025-09-21 20:06:29 -07:00
target commonCopy : FilePath := do
-- manually copy the DLLs we need to .lake/build/bin/ in the root directory for the game to work
2025-09-21 20:18:38 -07:00
let dstDir := ((<- getRootPackage).binDir)
2025-09-21 20:06:29 -07:00
IO.FS.createDirAll dstDir
return .pure dstDir
2025-09-21 19:36:32 -07:00
2025-09-21 20:06:29 -07:00
target copySdl : Unit := do
let dstDir : FilePath := (←(← commonCopy.fetch).await)
let sdlDirPath ← (← sdlDir.fetch).await
let sdlBinariesDir : FilePath := sdlDirPath / "build"
2025-09-20 16:11:27 -07:00
for entry in (← sdlBinariesDir.readDir) do
if entry.path.extension != none then
copyFile entry.path (dstDir / entry.path.fileName.get!)
2025-09-21 20:06:29 -07:00
return pure ()
target copySdlImage : Unit := do
let dstDir : FilePath := (←(← commonCopy.fetch).await)
let sdlImageDirPath ← (← sdlImageDir.fetch).await
let sdlImageBinariesDir : FilePath := sdlImageDirPath / "build"
2025-09-20 16:11:27 -07:00
for entry in (← sdlImageBinariesDir.readDir) do
if entry.path.extension != none then
copyFile entry.path (dstDir / entry.path.fileName.get!)
2025-09-21 20:06:29 -07:00
return pure ()
target copyLeanRuntime : Unit := do
2025-09-21 20:18:38 -07:00
let dstDir : FilePath := (←(← commonCopy.fetch).await)
2025-09-20 16:11:27 -07:00
if Platform.isWindows then
-- binaries for Lean/Lake itself for the executable to run standalone
let lakeBinariesDir := (← IO.appPath).parent.get!
2025-09-22 12:39:18 -07:00
logInfo s!"Copying Lake DLLs from {lakeBinariesDir}"
2025-09-20 16:11:27 -07:00
for entry in (← lakeBinariesDir.readDir) do
if entry.path.extension == some "dll" then
2025-09-21 20:18:38 -07:00
copyFile entry.path (dstDir / entry.path.fileName.get!)
2025-09-20 16:11:27 -07:00
else
-- binaries for Lean/Lake itself, like libgmp are on a different place on Linux
let lakeBinariesDir := (← IO.appPath).parent.get!.parent.get! / "lib"
2025-09-22 12:39:18 -07:00
logInfo s!"Copying Lake binaries from {lakeBinariesDir}"
2025-09-20 16:11:27 -07:00
for entry in (← lakeBinariesDir.readDir) do
if entry.path.extension != none then
2025-09-21 20:18:38 -07:00
copyFile entry.path (dstDir / entry.path.fileName.get!)
2025-09-21 20:06:29 -07:00
return pure ()
2025-09-20 16:11:27 -07:00
2025-09-21 20:06:29 -07:00
target libleansdl pkg : FilePath := do
discard (← libSDL3.fetch).await
discard (← libSDL3Image.fetch).await
discard (← copySdl.fetch).await
discard (← copySdlImage.fetch).await
2025-09-21 20:18:38 -07:00
-- We shouldn't need to copy the Lean runtime every time we build the library
-- because the Lean Runtime is supposed to get statically linked already by default
--discard (← copyLeanRuntime.fetch).await
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]
@[default_target]
lean_lib SDL where
moreLinkObjs := #[libleansdl]
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 #[]
moreLinkLibs := #[libSDL3, libSDL3Image]
2025-09-23 20:06:23 -07:00
lean_exe «test-app» where
root := `TestApp
moreLinkObjs := #[libleansdl]
moreLinkLibs := #[libSDL3, libSDL3Image]
moreLinkArgs := if !Platform.isWindows then #["-Wl,--allow-shlib-undefined", "-Wl,-rpath=$ORIGIN"] else #[]