packages feed

MicroHs-0.17.4.0: Tools/Install.hs

module Install where
import Control.Monad
import qualified Data.ByteString as BS
import Data.Char
import Data.List
import Data.Maybe
import Data.Version
import Data.Word
import Foreign.Storable
import Foreign.Marshal.Utils
import Foreign.Ptr
import System.Directory
import System.Environment
import System.FilePath
import System.Process
import System.IO.Serialize
import System.IO.TimeMilli
import Text.Printf
import MicroHs.Config
import MicroHs.Ident(showIdent)
import MicroHs.Package
import MicroHs.TypeCheck(tModuleName)

type CConf = [(Key, Value)]


main :: IO ()
main = do
  start <- getTimeMilli
  args <- getArgs
  let flags = decodeArgs defaultFlags args
  confText <- macroExpand flags <$> readFile (confFile flags)
  home <- getHomeDirectory
  let conf = either (\ s -> error $ "cannot parse config " ++ s) id $
                    parseConfig (confFile flags) confText
      cc = fromMaybe (error $ "Cannot locate section " ++ target flags) $
              lookup (target flags) conf
      exe | target flags == "windows" = "exe"
          | otherwise                 =  ""
      inst = fromMaybe (home </> ".mcabal") (instDirM flags)
      flags' = flags { exeSuffix = exe, cconf = cc, instDir = inst, conf = confText
                     , libraries = getLibraries conf }

  install flags'

  end <- getTimeMilli
  let delta = (end - start) `quot` 100
      (sec, tenths) = delta `quotRem` 10
  putStrLn $ "Install time: " ++ show sec ++ "." ++ show tenths ++ "s"

install :: Flags -> IO ()
install flags = do
  time "instBin" $ instBin flags
  vers <- init <$> mhsOut flags ["--numeric-version"]
  let flags' = flags { version = vers }
  time "mkMachdep" $ mkMachdep flags'
  time "mkConf" $ mkConf flags'
  time "copyRTS" $ copyRTS flags'
  time "copyBase" $ copyBase flags'
  time "registerBase" $ registerBase flags'
  time "checkPath" $ checkPath flags'
  time "pgo" $ optMhs flags'

instBin :: Flags -> IO ()
instBin flags = do
  let mCabal = instDir flags
      mcabalBin = mCabal </> "bin"
      exes = ["mhs", "cpphs", "mcabal", "tester"]
  mkdir flags "bin"
  mapM_ (buildBin flags) exes
  mkdir flags mcabalBin
  mapM_ (cpbin flags mcabalBin) exes

cpbin :: Flags -> FilePath -> String -> IO FilePath
cpbin flags mcabalBin pgm = do
  let exe = pgm <.> exeSuffix flags
      dst = mcabalBin </> exe
  copy flags ("bin" </> exe) dst
  return dst

mkMachdep :: Flags -> IO ()
mkMachdep flags =
  machdep flags $ "src" </> "runtime" </> "MachDeps.h"

mkConf :: Flags -> IO ()
mkConf flags = do
  msg flags "create mhs.conf"
  unless (dryRun flags) $
    writeFile "mhs.conf" (conf flags)

copyRTS :: Flags -> IO ()
copyRTS flags = do
  let mCabalMhs = instDir flags </> ("mhs-" ++ version flags)
  let mData = mCabalMhs </> "packages" </> ("MicroHs-" ++ version flags) </> "data"
      rts = "src" </> "runtime"
  time "mkdir eData" $ mkdir flags mData
  time "copy mhs.conf" $ copy flags "mhs.conf" (mData </> "mhs.conf")
  time "copyDir" $ copyDir flags rts (mData </> rts)

copyBase :: Flags -> IO ()
copyBase flags = do
  let mCabalMhs = instDir flags </> ("mhs-" ++ version flags)
  mkdir flags mCabalMhs
  out <- mhsOut flags ["-Q", "generated" </> "base.pkg", mCabalMhs]
  msg flags out

-- Packages from Hackage depend on base-4.x, and on bytestring, text, etc.,
-- whose modules are part of the base of mhs.  Say so in Cabal registration
-- files next to the installed packages: one for base, and one for each
-- library bundled with it, which depends on base and has no package file
-- of its own.  Tools like cabal can then read them instead of keeping a
-- table of their own.  The libraries are given by the [library] sections
-- of the config file.
registerBase :: Flags -> IO ()
registerBase flags = do
  pkg <- readSerialized ("generated" </> "base.pkg") :: IO Package
  let mCabalMhs = instDir flags </> ("mhs-" ++ version flags)
      pkgDir = mCabalMhs </> "packages"
      baseName = showIdent (pkgName pkg)
      baseId = baseName ++ "-" ++ showVersion (pkgVersion pkg)
      (bases, libs) = partition ((== baseName) . libName) (libraries flags)
      base =
        case bases of
          [b] | isNothing (libModules b) -> b
              | otherwise -> error $ "The [library] for " ++ baseName ++ " cannot have modules"
          _ -> error $ "There must be exactly one [library] for " ++ baseName
      prefixes l = fromMaybe (error $ "The [library] for " ++ libName l ++ " has no modules") (libModules l)
      modules = map (showIdent . tModuleName)
      exported = modules (pkgExported pkg)
      bundles ps m = any (\ p -> m == p || (p ++ ".") `isPrefixOf` m) ps
      isBundled m = any (\ l -> bundles (prefixes l) m) libs
      register pid flds = do
        let file = mCabalMhs </> pid <.> "conf"
        msg flags $ "create " ++ file
        unless (dryRun flags) $
          writeFile file $ unlines [ showField f | f@(_, vs) <- flds, not (null vs) ]
  register baseId
    [ ("name", [baseName]), ("version", [libVersion base]), ("id", [baseId]), ("key", [baseId])
    , ("exposed", ["True"])
    , ("exposed-modules", filter (not . isBundled) exported)
    , ("hidden-modules", modules (pkgOther pkg))
    , ("library-dirs", [pkgDir])
    , ("ld-options", pkgOptl pkg)
    ]
  forM_ libs $ \ l -> do
    let pid = libName l ++ "-" ++ libVersion l
    register pid
      [ ("name", [libName l]), ("version", [libVersion l]), ("id", [pid]), ("key", [pid])
      , ("exposed", ["True"])
      , ("exposed-modules", filter (bundles (prefixes l)) exported)
      , ("depends", [baseId])
      ]
  where
    showField (k, [v]) = k ++ ": " ++ v
    showField (k, vs)  = k ++ ":" ++ concatMap ("\n  " ++) vs

-- A [library] section of the config file.
data Library = Library
  { libName    :: String
  , libVersion :: String
  , libModules :: Maybe [String]    -- module prefixes, none for base
  }
  deriving (Show)

getLibraries :: Config -> [Library]
getLibraries cnf = [ library kvs | ("library", kvs) <- cnf ]
  where
    library kvs =
      case [ k | (k, _) <- kvs, k `notElem` ["name", "version", "modules"] ] of
        [] -> Library { libName = field "name", libVersion = field "version"
                      , libModules = words <$> lookup "modules" kvs }
        ks -> error $ "Unknown keys in [library]: " ++ unwords ks
      where field k = fromMaybe (error $ "Missing key in [library]: " ++ k) $ lookup k kvs

checkPath :: Flags -> IO ()
checkPath flags = do
  path <- fromMaybe "" <$> lookupEnv "PATH"
  let paths = map norm (splitOn pathSep path)
      pathSep | target flags == "windows" = ";"
              | otherwise = ":"
      bin = instDir flags </> "bin"
      norm = map (\ c -> if c == '\\' then '/' else c)
  unless (norm bin `elem` paths) $
    putStrLn $ "Please add " ++ bin ++ " to your PATH"

buildBin :: Flags -> String -> IO ()
buildBin flags pgm = do
  let src = "generated" </> pgm <.> ".c"
      dst = "bin" </> pgm <.> exeSuffix flags
      ccf = cconf flags
      rts = "src" </> "runtime"
  time ("buildBin " ++ pgm) $
   cc flags [xflags flags, get ccf "ccflags", "-I" ++ rts, "-I" ++ (rts </> get ccf "conf"),
             rts </> "main.c", rts </> "eval.c",
             src, get ccf "cclibs", getD ccf "-o" "cout" ++ dst]

mhsOut :: Flags -> [String] -> IO String
mhsOut flags args = do
  let exe = "bin" </> "mhs" <.> exeSuffix flags
  msg flags $ unwords (exe:args)
  if dryRun flags then
    return "MHSOUT\n"
   else
    readProcess exe args ""

-----

getD :: CConf -> Value -> Key -> Value
getD cconf def key = fromMaybe def $ lookup key cconf

get :: CConf -> Key -> Value
get cconf key = getD cconf (error $ "Cannot find " ++ key) key

-----

cc :: Flags -> [String] -> IO ()
cc flags args = do
  let c = get (cconf flags) "cc"
  msg flags $ unwords (c : args)
  unless (dryRun flags) $
    callProcess c args

msg :: Flags -> String -> IO ()
msg flags s | not (quiet flags) = putStrLn s
            | otherwise         = return ()

-----

mkdir :: Flags -> FilePath -> IO ()
mkdir flags dir = do
  msg flags $ "mkdir " ++ dir
  unless (dryRun flags) $ do
    _ <- system $ "mkdir -p " ++ dir
    return ()
--    createDirectoryIfMissing True dir

machdep :: Flags -> FilePath -> IO ()
machdep flags name = do
  msg flags $ "create " ++ name
  big <- isBigEndian
  unless (dryRun flags) $
    writeFile name $ unlines
      [ "#define WORD_SIZE_IN_BITS " ++ show _wordSize
      , (if big then "#define" else "#undef") ++ " WORDS_BIGENDIAN"
      ]

isBigEndian :: IO Bool
isBigEndian = do
  let w :: Word
      w = if _wordSize == 32 then 0x01000002 else 0x0100000000000002
  p <- new w
  b <- peek (castPtr p :: Ptr Word8)
  return (b == 1)

copy :: Flags -> FilePath -> FilePath -> IO ()
copy flags src dst = do
  msg flags $ unwords ["cp", src, dst]
  unless (dryRun flags) $ do
    copyFileBS src dst
    copyPermissions src dst

copyDir :: Flags -> FilePath -> FilePath -> IO ()
copyDir flags src dst = do
  msg flags $ unwords ["cp -r", src, dst]
  let flags' = flags{ quiet = not (verbose flags) }
  mkdir flags dst
  let one file = do
        d <- doesDirectoryExist (src </> file)
        (if d then copyDir else copy) flags' (src </> file) (dst </> file)
  mapM_ one =<< listDirectory src

splitOn :: String -> String -> [String]
splitOn d = loop []
  where loop [] [] = []
        loop r [] = [reverse r]
        loop r s@(c:cs) | Just s' <- stripPrefix d s = reverse r : loop [] s'
                        | otherwise = loop (c:r) cs

macroExpand :: Flags -> String -> String
macroExpand flags = loop
  where loop [] = []
        loop ('%':cs) =
          let (mn, rest) = span nameChar cs
              nameChar c = isAlphaNum c || c == '_'
          in  fromMaybe "" (lookup mn (macros flags)) ++ loop rest
        loop (c:cs) = c : loop cs

-----

data Flags = Flags
  { target    :: String
  , dryRun    :: Bool
  , verbose   :: Bool
  , quiet     :: Bool
  , pgo       :: Bool
  , macros    :: [(String, String)]
  , goals     :: [String]
  , confFile  :: FilePath
  , instDirM  :: Maybe FilePath
  -- The rest are for internal use
  , exeSuffix :: String
  , instDir   :: FilePath
  , cconf     :: CConf
  , version   :: String
  , conf      :: String
  , libraries :: [Library]
  , xflags    :: String
  }
  deriving (Show)

defaultFlags :: Flags
defaultFlags =
  Flags
    { target    = if _isWindows then "windows" else "unix"
    , dryRun    = False
    , verbose   = False
    , quiet     = False
    , pgo       = False
    , macros    = []
    , goals     = []
    , confFile  = "mhs.conf.in"
    , instDirM  = Nothing
    --
    , exeSuffix = undefined
    , instDir   = undefined
    , cconf     = undefined
    , version   = undefined
    , conf      = undefined
    , libraries = undefined
    , xflags    = ""
    }

decodeArgs :: Flags -> [String] -> Flags
decodeArgs f [] = f
decodeArgs f (arg:args) =
  case arg of
    "--help"           -> error usage
    "-v"               -> decodeArgs f{verbose = True} args
    "-q"               -> decodeArgs f{quiet = True} args
    "--dryrun"         -> decodeArgs f{dryRun = True} args
    "--pgo"            -> decodeArgs f{pgo = True} args
    '-':'t':s          -> decodeArgs f{target = s} args
    '-':'i':s          -> decodeArgs f{instDirM = Just s} args
    '-':_              -> error $ "Unknown flag: " ++ arg ++ "\n" ++ usage
    _ | (m, '=':e) <- span (/= '=') arg
                       -> decodeArgs f{macros = macros f ++ [(m, e)]} args
      | otherwise      -> decodeArgs f{goals = goals f ++ [arg]} args

usage :: String
usage = "\ninstall [--help] [-v] [--dryrun] [--pgo] [-tTARGET] [-iDIR] [NAME=MACRO] [GOAL]\n"

copyFileBS :: FilePath -> FilePath -> IO ()
copyFileBS src dst = BS.readFile src >>= BS.writeFile dst

time :: String -> IO a -> IO a
time _ ioa | 0==0 = ioa
-- Time individual operations
time s ioa = do
  t1 <- getTimeMilli
  a <- ioa
  t2 <- getTimeMilli
  printf "***** %s %fs\n" s (fromIntegral (t2 - t1) / 1000)
  return a

-- Do Profile Guided Optimization
optMhs :: Flags -> IO ()
optMhs flags | not (pgo flags) = return ()
             | otherwise = do
  let mCabal = instDir flags
      mcabalBin = mCabal </> "bin"
  mpf <- pgoFlags flags
  case mpf of
    Nothing -> putStrLn "Not clang nor gcc, so no PGO"
    Just (genFlag, useFlag, merge) -> do
      putStrLn "*** Doing Profile Guided Optimization..."
      -- Build with flag to generate profile info
      buildBin flags{xflags = genFlag} "mhs"
      -- Copy to target location
      mhs <- cpbin flags mcabalBin "mhs"
      -- Invoke mhs, compiling itself
      let mhsArgs = ["-i", "-imhs", "-isrc", "MicroHs.Main"]
      msg flags $ unwords (mhs : mhsArgs)
      callProcess mhs mhsArgs
      -- Extra profile processing
      merge
      -- Compile again with profiling info
      buildBin flags{xflags = useFlag} "mhs"
      -- And install it again
      _ <- cpbin flags mcabalBin "mhs"
      return ()

pgoFlags :: Flags -> IO (Maybe (String, String, IO ()))
pgoFlags flags = do
  -- figure out which C compiler this is
  let c = get (cconf flags) "cc"
  out <- readProcess c ["--version"] ""
  if "gcc" `isInfixOf` out then
    return $ Just ("-fprofile-generate -flto=auto", "-fprofile-use -flto=auto", return ())
   else if "clang" `isInfixOf` out then
    return $ Just ("-fprofile-generate -flto=full", "-fprofile-use=mhs.profdata -flto=full",
                   callProcess "llvm-profdata" ["merge", "-output=mhs.profdata", "*.profraw"])
   else
    return Nothing