Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 209 lines (163 sloc) 7.62 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
-- | This module contains routines for making mac .pkg files.
------------------------------------------------------------------------
module Program.MakePackage ( runMakePackage ) where

import Control.Monad

import Data.Char
import Data.Function
import Data.List
import Data.Maybe
import Data.Version

import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Configuration
import Distribution.PackageDescription.Parse
import Distribution.Simple.Utils hiding (intercalate)
import Distribution.Verbosity as Verbosity

import System.Directory
import System.FilePath
import System.IO

import Text.Regex


------------------------------------------------------------------------
-- local imports
import Distribution.OSX.PackageInfo
import Program.Options
import Program.Util


------------------------------------------------------------------------
-- | The program driver. Given the command-line options and a temp
-- directory path, searches the current working directory for a .cabal
-- file and builds an OSX package file based on its contents.
------------------------------------------------------------------------
runMakePackage :: Options -- ^ command-line options
               -> FilePath -- ^ temp directory path
               -> IO ()
runMakePackage opts tmpdir = do
  cabalFile <- findPackageDesc "."
  pkgDesc <- flattenPackageDescription `liftM`
                 readPackageDescription Verbosity.normal cabalFile

  makeMacPkg opts tmpdir pkgDesc


------------------------------------------------------------------------
-- "the guts"
------------------------------------------------------------------------

------------------------------------------------------------------------
-- | the 'makeMacPkg' function does (or will do) all of the dirty work
-- of building the .pkg files
makeMacPkg :: Options -- ^ command-line options
           -> FilePath -- ^ path to temp directory
           -> PackageDescription -- ^ a parsed .cabal file
           -> IO ()
makeMacPkg opts tmpdir pkgDesc = do
    -- some portions of package building process require root
    -- privileges (thanks, Apple!)
    checkRootPrivileges
    createDirectories

    --------------------------------------------------------------------
    hasPostFlight <- buildPackageContents
    setRootPrivileges
    mkInfoFiles hasPostFlight
    runPackageMaker

  where
    --------------------------------------------------------------------
    -- variables
    --------------------------------------------------------------------

    -- package metadata
    pkgDescription = synopsis pkgDesc
    pkgTitle = unPackageName . packageName $ pkgDesc
    pkgVersionString = showVersion . packageVersion $ pkgDesc
    pkgBaseName = subRegex (mkRegex "[[:space:]]+") pkgTitle "_"

    -- directories
    cabalBuildDir = tmpdir </> "dist"
    contentsDir = stagingDir </> "Contents"
    scriptsDir = tmpdir </> "Scripts"
    stagingDir = tmpdir </> "stage"

    -- config options
    packageMakerCmd = fromJust $ packageMakerPath opts
    prefix = fromJust $ installPrefix opts

    -- output files
    temporaryPkgConfig = tmpdir </> "temp.pkgconfig"
    infoPath = tmpdir </> "PackageInfo"
    postflightScriptFile = scriptsDir </> "postflight"


    outputPackageDir = fromMaybe "." (packageOutputDir opts)
    computedPackageFile = (pkgBaseName ++ "-" ++ pkgVersionString ++ ".pkg")
    outputPackagePath = fromMaybe (outputPackageDir </> computedPackageFile)
                                     (packageOutputFile opts)


    --------------------------------------------------------------------
    -- helper I/O actions
    --------------------------------------------------------------------


    --------------------------------------------------------------------
    -- creates necessary directories inside the work area
    createDirectories =
      (createDirectoryIfMissing True)
        `mapM_` [stagingDir, scriptsDir, contentsDir]


    --------------------------------------------------------------------
    -- uses cabal to build the package into the work area
    buildPackageContents = do
        runSetup "configure" [ "--global", "--prefix="++prefix, "-p"
                               , "--ld-options=-macosx_version_min 10.5" ]
        runSetup "build" []
        runSetup "haddock" ["--hyperlink-source"]
        runSetup "copy" ["--destdir=" ++ contentsDir]
        runSetup "register" ["--gen-pkg-config=" ++ temporaryPkgConfig]

        makePostFlightScriptFile temporaryPkgConfig postflightScriptFile


    --------------------------------------------------------------------
    -- FIXME: make this stuff relocatable
    makePostFlightScriptFile src dest = do
        fe <- doesFileExist src
        if not fe then
            return False
          else do
            contents <- readFile src
            let output = "#!/bin/sh\n\
\/usr/bin/ghc-pkg --global update - <<EOF\n"
                         ++ contents ++ "\nEOF\n"
            writeFile dest output
            return True


    --------------------------------------------------------------------
    -- populate the packageinfo file in the resource directory
    mkInfoFiles :: Bool -> IO ()
    mkInfoFiles hasPf = do
        nf <- getNumFiles contentsDir
        kb <- getFileSizesInKB contentsDir

        let pf = if hasPf then Just "postflight" else Nothing

        let pinfo = PackageInfo kb nf ("haskell."++pkgTitle)
                                Nothing pf

        writePackageInfo infoPath pinfo


    --------------------------------------------------------------------
    -- make sure files are owned by root and have correct permissions
    setRootPrivileges :: IO ()
    setRootPrivileges = do
        runCmd "chmod" ["-R", "g+r,g-w,o+r,o-w", tmpdir]
        runCmd "chown" ["-R", "root:wheel", tmpdir]
        runCmd "sh" ["-c", "find " ++ tmpdir
                            ++ " -type d -print0 | xargs -0 chmod a+x"]


    --------------------------------------------------------------------
    -- build the package
    runPackageMaker :: IO ()
    runPackageMaker = do
        putStrLn $ "building " ++ outputPackagePath
        hFlush stdout

        runCmd packageMakerCmd [ "-o"
                               , outputPackagePath
                               , "--root"
                               , contentsDir
                               , "--id"
                               , ("haskell." ++ pkgTitle)
                               , "--scripts"
                               , scriptsDir
                               , "--target"
                               , "10.5"
                               , "--root-volume-only"
                               , "--info"
                               , infoPath ]



    --------------------------------------------------------------------
    -- helper functions
    --------------------------------------------------------------------

    -- | runs Setup.[l]hs with the given subcommand and arguments
    runSetup :: String -- ^ subcommand of Setup.hs,
                                -- i.e. "configure"/"build"/etc
             -> [String] -- ^ additional arguments
             -> IO ()
    runSetup cmd args =
        runCmd "cabal" $ [cmd] ++ mkOpts args
      where
        mkOpts s = s ++ ["--builddir=" ++ cabalBuildDir]


    unPackageName (PackageName s) = s
Something went wrong with that request. Please try again.