Skip to content

Commit

Permalink
Use Custom build type on all OSes but only act on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
yutotakano committed Aug 9, 2024
1 parent a63f253 commit f9b71b0
Show file tree
Hide file tree
Showing 94 changed files with 52 additions and 20 deletions.
7 changes: 6 additions & 1 deletion HaskellGameJamTemplate.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ maintainer: changeme@ed.ac.uk
-- copyright:

category: Game
build-type: Custom
build-type: Custom

custom-setup
setup-depends: base >= 4.10.0.0,
Cabal >= 3.6,
directory ^>= 1.3.0.0

executable Game
ghc-options: -Wall
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
On Linux/OSX, SDL2 is found in the system package manager.
On Linux, run: apt-get install libsdl2-dev
On OSX, run: brew install sdl2

For Windows, SDL2 development libraries are provided within the repository, and
Setup.hs (run automatically during the build process) should find them.
54 changes: 40 additions & 14 deletions Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
( ConfigFlags
)
import Distribution.System
( buildOS
, OS (Windows)
)
import Data.Maybe
( fromJust
)
Expand All @@ -30,20 +34,42 @@ import System.Directory
main :: IO ()
main = defaultMainWithHooks simpleUserHooks { confHook = sdlConfHook }

-- | A hook that adds the SDL include and lib directories to the build info on
-- Windows. This is necessary to link against the SDL library during compilation.
--
-- On MacOS and Linux, the SDL library can be installed using package managers,
-- which puts them in a standard system-wide location that GHC will find
-- automatically. However, on Windows, due to Haskell being distributed not
-- natively but on top of MinGW and MSYS2, installing and using external
-- libraries is a much more manual and error-prone process.
--
-- Instead, in this Game Jam Template repository, we've bundled the Windows
-- build of the SDL development libraries with the project, so that we can avoid
-- a system-wide installation of SDL. But because it's now in a non-standard
-- location, we need to tell GHC where to find those libraries. We could specify
-- the path in the .cabal or cabal.project files, but they only accept absolute
-- paths and not relative ones. Which means it's unsuited for a template repo
-- that might be built on many different machines. Thus, we chose to use a
-- custom Setup.hs file, which runs during the build process and basically
-- dynamically modifies the .cabal file to add absolute paths before GHC reads it.
sdlConfHook :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags -> IO LocalBuildInfo
sdlConfHook (description, buildInfo) flags = do
localBuildInfo <- confHook simpleUserHooks (description, buildInfo) flags
let packageDescription = localPkgDescr localBuildInfo
library = fromJust $ library packageDescription
libraryBuildInfo = libBuildInfo library
dir <- getCurrentDirectory
return localBuildInfo {
localPkgDescr = packageDescription {
library = Just $ library {
libBuildInfo = libraryBuildInfo {
includeDirs = (dir ++ "/sdl/include"):(includeDirs libraryBuildInfo),
extraLibDirs = (dir ++ "/sdl/lib"):(extraLibDirs libraryBuildInfo)
sdlConfHook (description, buildInfo) flags =
-- Only add the SDL include and lib directories on Windows
if buildOS flags /= Windows
then pure $ fromJust $ confHook simpleUserHooks (description, buildInfo) flags
else do
localBuildInfo <- confHook simpleUserHooks (description, buildInfo) flags
let packageDescription = localPkgDescr localBuildInfo
library = fromJust $ library packageDescription
libraryBuildInfo = libBuildInfo library
dir <- getCurrentDirectory
pure localBuildInfo {
localPkgDescr = packageDescription {
library = Just $ library {
libBuildInfo = libraryBuildInfo {
includeDirs = (dir ++ "/sdl2_win_mingw/include"):(includeDirs libraryBuildInfo),
extraLibDirs = (dir ++ "/sdl2_win_mingw/lib"):(extraLibDirs libraryBuildInfo)
}
}
}
}
}
}
5 changes: 0 additions & 5 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ if os(windows)
-- Disable searching with pkgconfig on Win, as a DLL is supplied instead
flags: -pkgconfig

-- Extra libraries needed for the Windows platform. On Linux/OSX, SDL2
-- is found in the system package manager.
-- On Linux, run: apt-get install libsdl2-dev
-- On OSX, run: brew install sdl2

package text
-- Disable use of SIMD, which makes text fails to build on Win + GHC 8.8.4
flags: -simdutf
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f9b71b0

Please sign in to comment.