{-# LANGUAGE LambdaCase #-}
module Distribution.Nixpkgs.Haskell.Platform
( parsePlatformLenient
, parsePlatformFromSystemLenient
) where
import Data.List ( isPrefixOf, intercalate )
import Data.List.Split ( splitOn )
import Data.Maybe ( fromMaybe, listToMaybe )
import Distribution.System
ghcConvertArch :: String -> String
ghcConvertArch :: String -> String
ghcConvertArch String
arch = case String
arch of
String
"i486" -> String
"i386"
String
"i586" -> String
"i386"
String
"i686" -> String
"i386"
String
"amd64" -> String
"x86_64"
String
_ -> String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
arch (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ [String] -> Maybe String
forall a. [a] -> Maybe a
listToMaybe
[String
prefix | String
prefix <- [String]
archPrefixes, String
prefix String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
arch]
where archPrefixes :: [String]
archPrefixes =
[ String
"aarch64", String
"alpha", String
"arm", String
"hppa1_1", String
"hppa", String
"m68k", String
"mipseb"
, String
"mipsel", String
"mips", String
"powerpc64le", String
"powerpc64", String
"powerpc", String
"s390x"
, String
"sparc64", String
"sparc"
]
ghcConvertOS :: String -> String
ghcConvertOS :: String -> String
ghcConvertOS String
os = case String
os of
String
"watchos" -> String
"ios"
String
"tvos" -> String
"ios"
String
"linux-android" -> String
"linux-android"
String
"linux-androideabi" -> String
"linux-androideabi"
String
_ | String
"linux-" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
os -> String
"linux"
String
_ -> String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
os (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ [String] -> Maybe String
forall a. [a] -> Maybe a
listToMaybe
[String
prefix | String
prefix <- [String]
osPrefixes, String
prefix String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
os]
where osPrefixes :: [String]
osPrefixes =
[ String
"gnu", String
"openbsd", String
"aix", String
"darwin", String
"solaris2", String
"freebsd", String
"nto-qnx"]
parseArch :: String -> Arch
parseArch :: String -> Arch
parseArch = ClassificationStrictness -> String -> Arch
classifyArch ClassificationStrictness
Permissive (String -> Arch) -> (String -> String) -> String -> Arch
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
ghcConvertArch
parseOS :: String -> OS
parseOS :: String -> OS
parseOS = ClassificationStrictness -> String -> OS
classifyOS ClassificationStrictness
Permissive (String -> OS) -> (String -> String) -> String -> OS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
ghcConvertOS
parsePlatformParts :: [String] -> Maybe Platform
parsePlatformParts :: [String] -> Maybe Platform
parsePlatformParts = \case
[String
arch, String
os] ->
Platform -> Maybe Platform
forall a. a -> Maybe a
Just (Platform -> Maybe Platform) -> Platform -> Maybe Platform
forall a b. (a -> b) -> a -> b
$ Arch -> OS -> Platform
Platform (String -> Arch
parseArch String
arch) (String -> OS
parseOS String
os)
(String
arch : String
_ : [String]
osParts) ->
Platform -> Maybe Platform
forall a. a -> Maybe a
Just (Platform -> Maybe Platform) -> Platform -> Maybe Platform
forall a b. (a -> b) -> a -> b
$ Arch -> OS -> Platform
Platform (String -> Arch
parseArch String
arch) (OS -> Platform) -> OS -> Platform
forall a b. (a -> b) -> a -> b
$ String -> OS
parseOS (String -> OS) -> String -> OS
forall a b. (a -> b) -> a -> b
$ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"-" [String]
osParts
[String]
_ -> Maybe Platform
forall a. Maybe a
Nothing
parsePlatformLenient :: String -> Maybe Platform
parsePlatformLenient :: String -> Maybe Platform
parsePlatformLenient = [String] -> Maybe Platform
parsePlatformParts ([String] -> Maybe Platform)
-> (String -> [String]) -> String -> Maybe Platform
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> [String]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn String
"-"
parsePlatformFromSystemLenient :: String -> Maybe Platform
parsePlatformFromSystemLenient :: String -> Maybe Platform
parsePlatformFromSystemLenient String
s =
case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-') String
s of
(String
arch, Char
'-':String
os) ->
if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
arch Bool -> Bool -> Bool
|| String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
os
then Maybe Platform
forall a. Maybe a
Nothing
else [String] -> Maybe Platform
parsePlatformParts [String
arch, String
os]
(String, String)
_ -> Maybe Platform
forall a. Maybe a
Nothing