module GHC.Linker.Static.Utils where

import GHC.Prelude
import GHC.Platform
import System.FilePath

-- | Compute the output file name of a program.
--
-- StaticLink boolean is used to indicate if the program is actually a static library
-- (e.g., on iOS).
--
-- Use the provided filename (if any), otherwise use "main.exe" (Windows),
-- "a.out (otherwise without StaticLink set), "liba.a". In every case, add the
-- extension if it is missing.
exeFileName :: Platform -> Bool -> Maybe FilePath -> FilePath
exeFileName :: Platform -> Bool -> Maybe FilePath -> FilePath
exeFileName Platform
platform Bool
staticLink Maybe FilePath
output_fn
  | Just FilePath
s <- Maybe FilePath
output_fn =
      case Platform -> OS
platformOS Platform
platform of
          OS
OSMinGW32 -> FilePath
s FilePath -> FilePath -> FilePath
<?.> FilePath
"exe"
          OS
_         -> if Bool
staticLink
                         then FilePath
s FilePath -> FilePath -> FilePath
<?.> FilePath
"a"
                         else FilePath
s
  | Bool
otherwise =
      if Platform -> OS
platformOS Platform
platform OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSMinGW32
      then FilePath
"main.exe"
      else if Bool
staticLink
           then FilePath
"liba.a"
           else FilePath
"a.out"
 where FilePath
s <?.> :: FilePath -> FilePath -> FilePath
<?.> FilePath
ext | FilePath -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (FilePath -> FilePath
takeExtension FilePath
s) = FilePath
s FilePath -> FilePath -> FilePath
<.> FilePath
ext
                  | Bool
otherwise              = FilePath
s