cabal-flatpak-1.0.0: src/domain/Distribution/Flatpak/Generate.hs
module Distribution.Flatpak.Generate
( RevisedUnit (..)
, ghcModule
, cabalInstallModule
, packageURLs
, haskellSources
, setupCommands
) where
import Data.Map qualified as Map
import Data.Maybe
import Data.Text (Text)
import Data.Text qualified as Text
import Shell.Utility.Quote qualified as Quote
import Cabal.Plan qualified as Plan
import Distribution.Flatpak qualified as Flatpak
import Distribution.Flatpak.GHC
ghcPkgCleanup :: Plan.PlanJson -> Flatpak.Arch -> [Text]
ghcPkgCleanup plan arch =
let archLinux =
Text.pack (Flatpak.archString arch)
<> "-linux-"
<> Plan.dispPkgId (Plan.pjCompilerId plan)
in map (<> archLinux) $
"/lib/"
: "/share/doc/"
: []
ghcCleanup :: Plan.PlanJson -> [Text]
ghcCleanup plan =
let (Plan.PkgId _ ghcVer) = Plan.pjCompilerId plan
withVersion prg = prg <> "-" <> Plan.dispVer ghcVer
in map ("/" <>) $
concat
[ map ("bin/" <>) $
withVersion "*"
: "ghc"
: "ghc-pkg"
: "ghci"
: "haddock"
: "runghc"
: "runhaskell"
: "hp2ps"
: "hpc"
: "hsc2hs"
: []
, map ("lib/" <>) $
withVersion "ghc"
: []
, map ("share/" <>) $
"man"
: ("doc/" <> withVersion "ghc")
: []
]
-- | Generate the module to download and install the GHC binary distribution.
ghcModule :: Plan.PlanJson -> Flatpak.Arch -> Plan.Sha256 -> Flatpak.Module
ghcModule plan arch hash =
let compiler = Plan.pjCompilerId plan
in Flatpak.Module
{ Flatpak.name =
Plan.dispPkgId compiler
<> "-"
<> Text.pack (Flatpak.archString arch)
, Flatpak.onlyArches = [arch]
, Flatpak.buildsystem = Flatpak.Simple
, Flatpak.builddir = False
, Flatpak.configOpts = []
, Flatpak.buildCommands =
"./configure --prefix=/app"
: "make install"
: []
, Flatpak.postInstall = []
, Flatpak.cleanupModule = ghcCleanup plan
, Flatpak.sources =
[ Flatpak.Source
{ Flatpak.typ = Flatpak.archive
, Flatpak.url =
Just $
Text.pack (ghcDirUrl compiler ++ ghcArchive compiler arch)
, Flatpak.path = Nothing
, Flatpak.destFilename = Nothing
, Flatpak.dest = Nothing
, Flatpak.sha256 = Just hash
}
]
, Flatpak.modules = []
}
-- | A build plan unit together with the Hackage revision of its .cabal file.
data RevisedUnit
= RevisedUnit
{ unit :: Plan.Unit
, revision :: Plan.Rev
}
deriving stock (Show)
packageURLs :: RevisedUnit -> (Text, Text, Text)
packageURLs revisedUnit =
let pkgId@(Plan.PkgId (Plan.PkgName name) _version) = Plan.uPId revisedUnit.unit
pkgNameVer = Plan.dispPkgId pkgId
packageUrl = "https://hackage.haskell.org/package/" <> pkgNameVer
in ( packageUrl <> "/" <> pkgNameVer <> ".tar.gz"
, packageUrl <> "/revision/" <> Plan.dispRev revisedUnit.revision <> ".cabal"
, name <> ".cabal"
)
packageSources :: RevisedUnit -> [Flatpak.Source]
packageSources revisedUnit =
let pkg = revisedUnit.unit
(pkgUrl, cabalUrl, cabalPath) = packageURLs revisedUnit
in Flatpak.Source
{ Flatpak.typ = Flatpak.archive
, Flatpak.url = Just pkgUrl
, Flatpak.path = Nothing
, Flatpak.destFilename = Nothing
, Flatpak.dest = Nothing
, Flatpak.sha256 = Plan.uSha256 pkg
}
: Flatpak.Source
{ Flatpak.typ = Flatpak.File
, Flatpak.url = Just cabalUrl
, Flatpak.path = Nothing
, Flatpak.destFilename = Just cabalPath
, Flatpak.dest = Nothing
, Flatpak.sha256 = Plan.uCabalSha256 pkg
}
: []
unitFlags :: Plan.Unit -> [Text]
unitFlags unit =
case Map.toList (Plan.uFlags unit) of
[] -> []
flags -> [Text.pack (Quote.always (Text.unpack ("--flags=" <> Text.unwords (map flagString flags))))]
where
flagString (Plan.FlagName name, enabled) =
Text.cons (if enabled then '+' else '-') name
-- |
-- Generate a module to download, build and install a package from Hackage.
-- Due to the Cabal package "revision" not being part of a package,
-- we have to download the package,
-- extract and then overwrite the .cabal file with the latest revision.
-- The first argument lists the files that the module cleans up at the end of the build.
modul
:: [Text]
-> RevisedUnit
-> Flatpak.Module
modul cleanup revisedUnit =
let (Plan.PkgId (Plan.PkgName name) _version) = Plan.uPId revisedUnit.unit
in Flatpak.Module
{ Flatpak.name = name
, Flatpak.onlyArches = []
, Flatpak.buildsystem = Flatpak.Simple
, Flatpak.builddir = False
, Flatpak.configOpts = []
, Flatpak.buildCommands = setupCommands (unitFlags revisedUnit.unit)
, Flatpak.postInstall = []
, Flatpak.cleanupModule = cleanup
, Flatpak.sources = packageSources revisedUnit
, Flatpak.modules = []
}
setupCommands :: [Text] -> [Text]
setupCommands configureFlags =
[ "echo '#! /usr/bin/env runhaskell' >Setup.txt"
, "echo '> import Distribution.Simple' >>Setup.txt"
, "echo '> main = defaultMain' >>Setup.txt"
, "test -e Setup.hs || test -e Setup.lhs || ln -s Setup.txt Setup.lhs"
, Text.unwords ("runhaskell Setup configure --prefix=/app" : configureFlags)
, "runhaskell Setup build --jobs=$FLATPAK_BUILDER_N_JOBS"
, "runhaskell Setup install"
]
-- | Generate the module that builds GHC and the given packages.
haskellSources
:: Plan.PlanJson
-> [(Flatpak.Arch, Plan.Sha256)]
-> [RevisedUnit]
-> Flatpak.Module
haskellSources plan archs units =
let pkgCleanup = concatMap (ghcPkgCleanup plan . fst) archs
in Flatpak.Module
{ Flatpak.name = "haskell-sources"
, Flatpak.onlyArches = []
, Flatpak.buildsystem = Flatpak.Simple
, Flatpak.builddir = False
, Flatpak.configOpts = []
, -- flatpak-builder needs at least one command for a simple module.
Flatpak.buildCommands = ["true"]
, Flatpak.postInstall = []
, Flatpak.cleanupModule = []
, Flatpak.sources = []
, Flatpak.modules =
map (\(arch, ghcHash) -> ghcModule plan arch ghcHash) archs
++ map (modul pkgCleanup) units
}
cabalInstallVersion :: Text
cabalInstallVersion = "3.18.1.0"
-- | SHA256 of the static Linux cabal-install binary distribution, from its
-- SHA256SUMS file.
cabalInstallHash :: Flatpak.Arch -> Plan.Sha256
cabalInstallHash arch =
let hash =
case arch of
Flatpak.X86_64 ->
"c6385c155ff61f792dfed0e8c101004db63f8d8b556dd6c9838f81a012bf28a6"
Flatpak.AArch64 ->
"b8333291622c0354d72adec64ccb19e13563264bae2c15c9e6657e7f9c1486e8"
in fromMaybe (error $ "invalid cabal-install SHA256: " ++ Text.unpack hash) $
Plan.parseSha256 hash
cabalInstallModule :: Flatpak.Arch -> Flatpak.Module
cabalInstallModule arch =
let cabalVerName = "cabal-install-" <> cabalInstallVersion
archName = Text.pack (Flatpak.archString arch)
url =
"https://downloads.haskell.org/cabal/"
<> cabalVerName
<> "/"
<> cabalVerName
<> "-"
<> archName
<> "-linux-unknown.tar.xz"
in Flatpak.Module
{ Flatpak.name = cabalVerName <> "-" <> archName
, Flatpak.onlyArches = [arch]
, Flatpak.buildsystem = Flatpak.Simple
, Flatpak.builddir = False
, Flatpak.configOpts = []
, Flatpak.buildCommands = ["install cabal /app/bin"]
, Flatpak.postInstall = []
, Flatpak.cleanupModule = ["/bin/cabal"]
, Flatpak.sources =
[ Flatpak.Source
{ Flatpak.typ = Flatpak.Archive 0
, Flatpak.url = Just url
, Flatpak.path = Nothing
, Flatpak.destFilename = Nothing
, Flatpak.dest = Nothing
, Flatpak.sha256 = Just $ cabalInstallHash arch
}
]
, Flatpak.modules = []
}