propellor/src/Utility/LinuxMkLibs.hs

64 lines
1.9 KiB
Haskell
Raw Normal View History

2014-04-04 01:22:37 +00:00
{- Linux library copier and binary shimmer
-
2015-04-29 18:26:13 +00:00
- Copyright 2013 Joey Hess <id@joeyh.name>
2014-04-04 01:22:37 +00:00
-
2014-05-10 14:05:28 +00:00
- License: BSD-2-clause
2014-04-04 01:22:37 +00:00
-}
module Utility.LinuxMkLibs where
2015-05-27 18:55:31 +00:00
import Utility.PartialPrelude
import Utility.Directory
import Utility.Process
import Utility.Monad
import Utility.Path
2014-04-04 01:22:37 +00:00
import Data.Maybe
import System.Directory
import System.FilePath
2014-04-04 01:22:37 +00:00
import Data.List.Utils
import System.Posix.Files
import Data.Char
import Control.Monad.IfElse
2015-05-27 18:55:31 +00:00
import Control.Applicative
import Prelude
2014-04-04 01:22:37 +00:00
{- Installs a library. If the library is a symlink to another file,
- install the file it links to, and update the symlink to be relative. -}
installLib :: (FilePath -> FilePath -> IO ()) -> FilePath -> FilePath -> IO (Maybe FilePath)
installLib installfile top lib = ifM (doesFileExist lib)
( do
installfile top lib
checksymlink lib
2015-04-29 18:26:13 +00:00
return $ Just $ parentDir lib
2014-04-04 01:22:37 +00:00
, return Nothing
)
where
checksymlink f = whenM (isSymbolicLink <$> getSymbolicLinkStatus (inTop top f)) $ do
l <- readSymbolicLink (inTop top f)
2015-04-29 18:26:13 +00:00
let absl = absPathFrom (parentDir f) l
target <- relPathDirToFile (takeDirectory f) absl
2014-04-04 01:22:37 +00:00
installfile top absl
nukeFile (top ++ f)
createSymbolicLink target (inTop top f)
checksymlink absl
-- Note that f is not relative, so cannot use </>
inTop :: FilePath -> FilePath -> FilePath
inTop top f = top ++ f
{- Parse ldd output, getting all the libraries that the input files
- link to. Note that some of the libraries may not exist
- (eg, linux-vdso.so) -}
parseLdd :: String -> [FilePath]
2014-05-10 13:46:36 +00:00
parseLdd = mapMaybe (getlib . dropWhile isSpace) . lines
2014-04-04 01:22:37 +00:00
where
getlib l = headMaybe . words =<< lastMaybe (split " => " l)
{- Get all glibc libs and other support files, including gconv files
-
- XXX Debian specific. -}
glibcLibs :: IO [FilePath]
glibcLibs = lines <$> readProcess "sh"
["-c", "dpkg -L libc6:$(dpkg --print-architecture) libgcc1:$(dpkg --print-architecture) | egrep '\\.so|gconv'"]