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"
2025-09-23 20:54:45 -07:00
def sdlTtfGitRepo : String : = "https://github.com/libsdl-org/SDL_ttf.git"
2025-09-24 00:18:56 -07:00
def sdlMixerGitRepo : String : = "https://github.com/libsdl-org/SDL_mixer.git"
2025-09-20 16:11:27 -07:00
-- 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-23 20:54:45 -07:00
target sdlTtfDir pkg : FilePath : = do
return . pure ( pkg . dir / "vendor" / "SDL_ttf" )
2025-09-24 00:18:56 -07:00
target sdlMixerDir pkg : FilePath : = do
return . pure ( pkg . dir / "vendor" / "SDL_mixer" )
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
2025-09-24 00:18:56 -07:00
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/"
2025-09-24 00:18:56 -07:00
let sdlMixerInclude : = sdlMixerDir / "include/"
2025-09-21 21:40:01 -07:00
2025-09-24 00:18:56 -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-23 20:54:45 -07:00
target libSDL3 : 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"
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-23 20:56:06 -07:00
target libSDL3Image : Dynlib : = Job . async do
2025-09-21 19:36:32 -07:00
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"
2025-09-23 20:54:45 -07:00
path : = sdlImageRepoDir / "build" / nameToSharedLib "SDL3_image"
2025-09-21 19:36:32 -07:00
}
2025-09-23 20:56:06 -07:00
target libSDL3Ttf : Dynlib : = Job . async do
2025-09-23 20:54:45 -07:00
let sdlRepoDir : FilePath ← ( <- sdlDir . fetch ) . await
let sdlTtfRepoDir : FilePath ← ( <- sdlTtfDir . fetch ) . await
let sdlTtfExists ← System . FilePath . pathExists sdlTtfRepoDir
if ! sdlTtfExists then
logInfo "Cloning SDL_ttf"
let sdlTtfClone ← IO . Process . output { cmd : = "git" , args : = # [ "clone" , "-b" , sdlBranch , "--single-branch" , "--depth" , "1" , "--recursive" , sdlTtfGitRepo , sdlTtfRepoDir . toString ] }
if sdlTtfClone . exitCode != 0 then
logError s! "Error cloning SDL_ttf: {sdlTtfClone.stderr}"
else
logInfo "SDL_ttf cloned successfully"
logInfo sdlTtfClone . stdout
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
2025-09-24 00:18:56 -07:00
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
return {
name : = "SDL3_ttf"
path : = sdlTtfRepoDir / "build" / nameToSharedLib "SDL3_ttf"
}
2025-09-24 00:18:56 -07:00
target libSDL3Mixer : Dynlib : = Job . async do
let sdlRepoDir : FilePath ← ( ← sdlDir . fetch ) . await
let sdlMixerRepoDir : FilePath ← ( ← sdlMixerDir . fetch ) . await
logInfo s! "SDL repo dir: {sdlRepoDir}"
logInfo "Building SDL_mixer"
let sdlMixerExists ← System . FilePath . pathExists sdlMixerRepoDir
if ! sdlMixerExists then
logInfo "Cloning SDL_mixer"
let sdlMixerClone ← IO . Process . output { cmd : = "git" , args : = # [ "clone" , "--single-branch" , "--depth" , "1" , "--recursive" , sdlMixerGitRepo , sdlMixerRepoDir . toString ] }
if sdlMixerClone . exitCode != 0 then
logError s! "Error cloning SDL_mixer: {sdlMixerClone.stderr}"
else
logInfo "SDL_mixer cloned successfully"
logInfo sdlMixerClone . stdout
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"
return {
name : = "SDL3_mixer"
path : = sdlMixerRepoDir / "build" / nameToSharedLib "SDL3_mixer"
}
2025-09-23 20:54:45 -07:00
2025-09-23 21:21:42 -07:00
def copyBinaries ( sourceDir : FilePath ) : FetchM ( Job Unit ) : = do
2025-09-21 20:06:29 -07:00
-- 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
2025-09-23 21:21:42 -07:00
let binariesDir : FilePath : = sourceDir / "build"
for entry in ( ← binariesDir . readDir ) do
2025-09-23 20:54:45 -07:00
if entry . path . extension != none then
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
2025-09-23 20:54:45 -07:00
discard ( ← libSDL3Ttf . fetch ) . await
2025-09-24 00:18:56 -07:00
discard ( ← libSDL3Mixer . fetch ) . await
2025-09-23 20:54:45 -07:00
2025-09-23 21:21:42 -07:00
-- copy binaries to the bin directory
let sdlDirPath ← ( ← sdlDir . fetch ) . await
let sdlImageDirPath ← ( ← sdlImageDir . fetch ) . await
let sdlTtfDirPath ← ( ← sdlTtfDir . fetch ) . await
2025-09-24 00:18:56 -07:00
let sdlMixerDirPath ← ( ← sdlMixerDir . fetch ) . await
2025-09-23 21:21:42 -07:00
discard ( <- copyBinaries sdlDirPath ) . await
discard ( <- copyBinaries sdlImageDirPath ) . await
discard ( <- copyBinaries sdlTtfDirPath ) . await
2025-09-24 00:18:56 -07:00
discard ( <- copyBinaries sdlMixerDirPath ) . 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 ]
2025-09-24 00:18:56 -07:00
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 # []