From f131186dc108726432d66782bffcd1d6edbb2d1e Mon Sep 17 00:00:00 2001 From: Yuto Takano Date: Sat, 10 Aug 2024 18:23:03 +0100 Subject: [PATCH] Create a setup script for Windows that creates cabal.project.local --- .github/workflows/windows-ci.yaml | 4 +- .gitignore | 1 + HaskellGameJamTemplate.cabal | 6 --- README.md | 4 +- Setup.hs | 78 ------------------------------- win_setup.bat | 11 +++++ 6 files changed, 17 insertions(+), 87 deletions(-) delete mode 100644 Setup.hs create mode 100644 win_setup.bat diff --git a/.github/workflows/windows-ci.yaml b/.github/workflows/windows-ci.yaml index 9a2eb26..6706716 100644 --- a/.github/workflows/windows-ci.yaml +++ b/.github/workflows/windows-ci.yaml @@ -23,7 +23,9 @@ jobs: run: cabal update - name: Build project - run: cabal build + run: | + win_setup.bat + cabal build - name: Create a zip with exe file and assets run: | diff --git a/.gitignore b/.gitignore index f388f96..33320cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist-newstyle/ .cabal .DS_Store +cabal.project.local diff --git a/HaskellGameJamTemplate.cabal b/HaskellGameJamTemplate.cabal index db05fd1..e462b6d 100644 --- a/HaskellGameJamTemplate.cabal +++ b/HaskellGameJamTemplate.cabal @@ -31,12 +31,6 @@ maintainer: changeme@ed.ac.uk -- copyright: category: Game -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 diff --git a/README.md b/README.md index c8b3948..c0d29a5 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ 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. +For Windows, SDL2 development libraries are provided within the repository. +Run `win_setup.bat` to create a file that specifies where to find these libraries. diff --git a/Setup.hs b/Setup.hs deleted file mode 100644 index 967e0ee..0000000 --- a/Setup.hs +++ /dev/null @@ -1,78 +0,0 @@ -import Distribution.PackageDescription - ( GenericPackageDescription - , HookedBuildInfo - , extraLibDirs - , includeDirs - , libBuildInfo - , library - ) -import Distribution.Simple - ( Args - , UserHooks - , confHook - , defaultMain - , defaultMainWithHooks - , simpleUserHooks - ) -import Distribution.Simple.LocalBuildInfo - ( LocalBuildInfo - , localPkgDescr - ) -import Distribution.Simple.Setup - ( ConfigFlags - ) -import Distribution.System - ( buildOS - , OS (Windows) - ) -import Data.Maybe - ( fromJust - ) -import System.Directory - ( getCurrentDirectory - ) - -main :: IO () -main = case buildOS of - Windows -> defaultMainWithHooks simpleUserHooks { confHook = sdlConfHook } - _ -> defaultMain - --- | 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 - putStrLn "\ESC[34mRunning custom SDL configuration hook for Windows\ESC[m" - putStrLn "\ESC[34mGetting local build info\ESC[m" - localBuildInfo <- confHook simpleUserHooks (description, buildInfo) flags - let packageDescription = localPkgDescr localBuildInfo - library = fromJust $ library packageDescription - libraryBuildInfo = libBuildInfo library - putStrLn "\ESC[34mGetting current directory\ESC[m" - dir <- getCurrentDirectory - putStrLn "\ESC[34mAdding SDL include and lib directories: " ++ dir ++ "\ESC[m" - 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) - } - } - } - } diff --git a/win_setup.bat b/win_setup.bat new file mode 100644 index 0000000..f3af616 --- /dev/null +++ b/win_setup.bat @@ -0,0 +1,11 @@ +@echo off + +rem Create a cabal.project.local file containing the path to the local SDL2 libraries + +( +echo.package sdl2 +echo. extra-lib-dirs: "%cd:\=\\%\\sdl2_win_mingw\\lib" +echo. extra-include-dirs: "%cd:\=\\%\\sdl2_win_mingw\\include\\SDL2" +)>"cabal.project.local" +echo Setup complete! Try running 'cabal run' to build the project. +echo If you encounter problems, try asking Discord.