2013-12-24 17:13:17 +00:00
|
|
|
{- Linux library copier and binary shimmer
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-12-24 17:13:17 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2016-04-20 16:40:11 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2013-12-24 17:13:17 +00:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import System.Environment
|
|
|
|
import Data.Maybe
|
|
|
|
import System.FilePath
|
|
|
|
import System.Directory
|
|
|
|
import Control.Monad
|
|
|
|
import Data.List
|
|
|
|
import System.Posix.Files
|
2013-12-24 17:25:02 +00:00
|
|
|
import Control.Monad.IfElse
|
2015-12-28 16:41:36 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2014-04-04 01:25:59 +00:00
|
|
|
import Utility.LinuxMkLibs
|
2013-12-24 17:13:17 +00:00
|
|
|
import Utility.Directory
|
|
|
|
import Utility.Process
|
|
|
|
import Utility.Monad
|
|
|
|
import Utility.Path
|
|
|
|
import Utility.FileMode
|
|
|
|
import Utility.CopyFile
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = getArgs >>= go
|
|
|
|
where
|
|
|
|
go [] = error "specify LINUXSTANDALONE_DIST"
|
|
|
|
go (top:_) = mklibs top
|
|
|
|
|
|
|
|
mklibs :: FilePath -> IO ()
|
|
|
|
mklibs top = do
|
|
|
|
fs <- dirContentsRecursive top
|
2013-12-24 19:42:49 +00:00
|
|
|
mapM_ symToHardLink fs
|
2013-12-24 17:13:17 +00:00
|
|
|
exes <- filterM checkExe fs
|
|
|
|
libs <- parseLdd <$> readProcess "ldd" exes
|
|
|
|
glibclibs <- glibcLibs
|
|
|
|
let libs' = nub $ libs ++ glibclibs
|
2014-04-04 01:25:59 +00:00
|
|
|
libdirs <- nub . catMaybes <$> mapM (installLib installFile top) libs'
|
2013-12-24 17:25:02 +00:00
|
|
|
|
|
|
|
-- Various files used by runshell to set up env vars used by the
|
|
|
|
-- linker shims.
|
2013-12-24 17:13:17 +00:00
|
|
|
writeFile (top </> "libdirs") (unlines libdirs)
|
|
|
|
writeFile (top </> "gconvdir")
|
2015-01-09 17:11:56 +00:00
|
|
|
(parentDir $ Prelude.head $ filter ("/gconv/" `isInfixOf`) glibclibs)
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2015-02-16 23:36:26 +00:00
|
|
|
let linker = Prelude.head $ filter ("ld-linux" `isInfixOf`) libs'
|
|
|
|
mapM_ (installLinkerShim top linker) exes
|
2013-12-24 17:13:17 +00:00
|
|
|
|
|
|
|
{- Installs a linker shim script around a binary.
|
|
|
|
-
|
|
|
|
- Note that each binary is put into its own separate directory,
|
|
|
|
- to avoid eg git looking for binaries in its directory rather
|
2015-02-16 23:36:26 +00:00
|
|
|
- than in PATH.
|
|
|
|
-
|
|
|
|
- The linker is symlinked to a file with the same basename as the binary,
|
|
|
|
- since that looks better in ps than "ld-linux.so".
|
|
|
|
-}
|
|
|
|
installLinkerShim :: FilePath -> FilePath -> FilePath -> IO ()
|
|
|
|
installLinkerShim top linker exe = do
|
|
|
|
createDirectoryIfMissing True (top </> shimdir)
|
|
|
|
createDirectoryIfMissing True (top </> exedir)
|
2013-12-24 17:13:17 +00:00
|
|
|
renameFile exe exedest
|
2015-02-16 23:36:26 +00:00
|
|
|
link <- relPathDirToFile (top </> exedir) (top ++ linker)
|
|
|
|
unlessM (doesFileExist (top </> exelink)) $
|
|
|
|
createSymbolicLink link (top </> exelink)
|
2013-12-24 17:13:17 +00:00
|
|
|
writeFile exe $ unlines
|
|
|
|
[ "#!/bin/sh"
|
2015-03-27 20:06:50 +00:00
|
|
|
, "GIT_ANNEX_PROGRAMPATH=\"$0\""
|
|
|
|
, "export GIT_ANNEX_PROGRAMPATH"
|
2016-04-27 18:32:05 +00:00
|
|
|
#ifdef MIN_VERSION_GLASGOW_HASKELL
|
2016-04-20 16:40:11 +00:00
|
|
|
#if ! MIN_VERSION_GLASGOW_HASKELL(7,10,0,0)
|
2016-04-27 18:32:05 +00:00
|
|
|
#define NEED_LOCPATH_WORKAROUND
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define NEED_LOCPATH_WORKAROUND
|
|
|
|
#endif
|
|
|
|
#ifdef NEED_LOCPATH_WORKAROUND
|
2016-04-20 16:40:11 +00:00
|
|
|
-- workaround for https://ghc.haskell.org/trac/ghc/ticket/7695
|
|
|
|
, "LOCPATH=/dev/null"
|
|
|
|
, "export LOCPATH"
|
|
|
|
#endif
|
2015-02-16 23:36:26 +00:00
|
|
|
, "exec \"$GIT_ANNEX_DIR/" ++ exelink ++ "\" --library-path \"$GIT_ANNEX_LD_LIBRARY_PATH\" \"$GIT_ANNEX_DIR/shimmed/" ++ base ++ "/" ++ base ++ "\" \"$@\""
|
2013-12-24 17:13:17 +00:00
|
|
|
]
|
|
|
|
modifyFileMode exe $ addModes executeModes
|
|
|
|
where
|
|
|
|
base = takeFileName exe
|
2015-02-16 23:36:26 +00:00
|
|
|
shimdir = "shimmed" </> base
|
|
|
|
exedir = "exe"
|
|
|
|
exedest = top </> shimdir </> base
|
|
|
|
exelink = exedir </> base
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2013-12-24 19:42:49 +00:00
|
|
|
{- Converting symlinks to hard links simplifies the binary shimming
|
|
|
|
- process. -}
|
|
|
|
symToHardLink :: FilePath -> IO ()
|
|
|
|
symToHardLink f = whenM (isSymbolicLink <$> getSymbolicLinkStatus f) $ do
|
|
|
|
l <- readSymbolicLink f
|
2015-01-09 17:11:56 +00:00
|
|
|
let absl = absPathFrom (parentDir f) l
|
2013-12-24 19:42:49 +00:00
|
|
|
nukeFile f
|
|
|
|
createLink absl f
|
|
|
|
|
2013-12-24 17:13:17 +00:00
|
|
|
installFile :: FilePath -> FilePath -> IO ()
|
|
|
|
installFile top f = do
|
|
|
|
createDirectoryIfMissing True destdir
|
2014-08-31 14:57:07 +00:00
|
|
|
void $ copyFileExternal CopyTimeStamps f destdir
|
2013-12-24 17:13:17 +00:00
|
|
|
where
|
2015-01-09 17:11:56 +00:00
|
|
|
destdir = inTop top $ parentDir f
|
2013-12-24 17:13:17 +00:00
|
|
|
|
|
|
|
checkExe :: FilePath -> IO Bool
|
|
|
|
checkExe f
|
|
|
|
| ".so" `isSuffixOf` f = return False
|
|
|
|
| otherwise = ifM (isExecutable . fileMode <$> getFileStatus f)
|
|
|
|
( checkFileExe <$> readProcess "file" [f]
|
|
|
|
, return False
|
|
|
|
)
|
|
|
|
|
|
|
|
{- Check that file(1) thinks it's a Linux ELF executable, or possibly
|
|
|
|
- a shared library (a few executables like ssh appear as shared libraries). -}
|
|
|
|
checkFileExe :: String -> Bool
|
|
|
|
checkFileExe s = and
|
|
|
|
[ "ELF" `isInfixOf` s
|
|
|
|
, "executable" `isInfixOf` s || "shared object" `isInfixOf` s
|
|
|
|
]
|