Skip to content

Commit

Permalink
Add macos aarch64 build support to Shakefile system
Browse files Browse the repository at this point in the history
  • Loading branch information
supermario committed May 5, 2022
1 parent 1557892 commit 173d7cd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
8 changes: 5 additions & 3 deletions Shakefiles/Dependencies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Development.Shake
import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Util
import Shakefiles.Extra (addMacOSGhcOptionFFI)


localBinDir :: String
Expand All @@ -12,7 +13,8 @@ localBinDir = "bin"

cabalInstallExe :: String -> Action ()
cabalInstallExe package =
cmd_ "cabal"
cmd_ "cabal" $
addMacOSGhcOptionFFI
[ "v2-install"
, package
, "--installdir", localBinDir
Expand Down Expand Up @@ -41,7 +43,7 @@ rules = do
, "cabal.project"
, "cabal.project.freeze"
]
cmd_ "cabal" [ "v2-build", "--only-dependencies" ]
cmd_ "cabal" $ addMacOSGhcOptionFFI [ "v2-build", "--only-dependencies" ]
writeFile' out ""

"_build/cabal-test-dependencies.ok" %> \out -> do
Expand All @@ -50,7 +52,7 @@ rules = do
, "cabal.project"
, "cabal.project.freeze"
]
cmd_ "cabal" [ "v2-build", "--only-dependencies", "--enable-tests" ]
cmd_ "cabal" $ addMacOSGhcOptionFFI [ "v2-build", "--only-dependencies", "--enable-tests" ]
writeFile' out ""

shellcheck %> \out -> do
Expand Down
12 changes: 11 additions & 1 deletion Shakefiles/Extra.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Shakefiles.Extra (phonyPrefix, forEach) where
module Shakefiles.Extra (phonyPrefix, forEach, addMacOSGhcOptionFFI) where

import Development.Shake
import Data.List (stripPrefix)
import Shakefiles.Platform (platform, Platform(..))


phonyPrefix :: String -> (String -> Action ()) -> Rules ()
Expand All @@ -15,3 +16,12 @@ phonyPrefix prefix action =
forEach :: Monad m => [a] -> (a -> m ()) -> m ()
forEach list f =
mapM_ f list


--ghc-option required because of https://gitlab.haskell.org/ghc/ghc/-/issues/20592
addMacOSGhcOptionFFI :: [String] -> [String]
addMacOSGhcOptionFFI options =
case platform of
Mac -> ["--ghc-option=-I/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/usr/include/ffi"] ++ options
MacArm64 -> ["--ghc-option=-I/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/usr/include/ffi"] ++ options
_ -> options
8 changes: 4 additions & 4 deletions Shakefiles/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ cabalProject name sourceFiles sourcePatterns deps testPatterns testDeps =
do
"_build/cabal/" </> name </> "build.ok" %> \out -> do
hash <- needProjectFiles
cmd_ "cabal" "v2-build" "-O0" (name ++ ":libs") "--enable-tests"
cmd_ "cabal" $ addMacOSGhcOptionFFI ["v2-build", "-O0", (name ++ ":libs"), "--enable-tests"]
writeFile' out hash

cabalBinPath name "noopt" %> \out -> do
_ <- needProjectFiles
cmd_ "cabal" "v2-build" "-O0" (name ++ ":exes") "--enable-tests"
cmd_ "cabal" $ addMacOSGhcOptionFFI ["v2-build", "-O0", (name ++ ":exes"), "--enable-tests"]

cabalBinPath name "opt" %> \out -> do
_ <- needProjectFiles
cmd_ "cabal" "v2-build" "-O2" (name ++ ":exes")
cmd_ "cabal" $ addMacOSGhcOptionFFI ["v2-build", "-O2", (name ++ ":exes")]

"_build/cabal/" </> name </> "test.ok" %> \out -> do
need globalConfig
Expand All @@ -55,7 +55,7 @@ cabalProject name sourceFiles sourcePatterns deps testPatterns testDeps =
need sourceFilesFromPatterns
testFiles <- getDirectoryFiles "" testPatterns
need testFiles
cmd_ "cabal" "v2-test" "-O0" (name ++ ":tests") "--test-show-details=streaming"
cmd_ "cabal" $ addMacOSGhcOptionFFI ["v2-test", "-O0", (name ++ ":tests"), "--test-show-details=streaming"]
writeFile' out ""


Expand Down
8 changes: 7 additions & 1 deletion Shakefiles/Platform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ module Shakefiles.Platform (Platform(..), Shakefiles.Platform.all, platform, zip
import qualified System.Info


data Platform = Linux | Mac | Windows
data Platform = Linux | Mac | MacArm64 | Windows

instance Show Platform where
show Linux = "linux-x64"
show Mac = "mac-x64"
show MacArm64 = "mac-arm64"
show Windows = "win-x64"


all :: [Platform]
all =
[ Linux
, Mac
, MacArm64
, Windows
]

Expand All @@ -24,6 +26,7 @@ platform =
case (System.Info.os, System.Info.arch) of
("linux", "x86_64") -> Linux
("darwin", "x86_64") -> Mac
("darwin", "aarch64") -> MacArm64
("osx", "x86_64") -> Mac
("mingw32", "x86_64") -> Windows
("win32", "x86_64") -> Windows
Expand All @@ -34,13 +37,15 @@ zipFormatFor :: Platform -> String
zipFormatFor = \case
Linux -> "tgz"
Mac -> "tgz"
MacArm64 -> "tgz"
Windows -> "zip"


binExt :: Platform -> String
binExt = \case
Linux -> ""
Mac -> ""
MacArm64 -> ""
Windows -> ".exe"


Expand All @@ -49,6 +54,7 @@ githubRunnerOs = \case
Linux -> "Linux"
Windows -> "Windows"
Mac -> "macOS"
MacArm64 -> "macOS"


cabalInstallOs :: String
Expand Down
7 changes: 6 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ mkdir -p _build
# install shake if it's not installed
if ! grep -qs '^package-id \(shake\|shk\)-' "$PKG_ENV_FILE"; then
echo "$0: installing shake"
cabal v2-install --package-env "$PKG_ENV_FILE" --lib shake
if [[ $OSTYPE == darwin* ]]; then
# --ghc-option required because of https://gitlab.haskell.org/ghc/ghc/-/issues/20592
cabal v2-install --package-env "$PKG_ENV_FILE" --ghc-option="`pkg-config --cflags libffi`" --lib shake
else
cabal v2-install --package-env "$PKG_ENV_FILE" --lib shake
fi
fi

# compile the build script
Expand Down

0 comments on commit 173d7cd

Please sign in to comment.