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
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-12-24 17:13:17 +00:00
|
|
|
-}
|
|
|
|
|
2020-08-03 17:43:21 +00:00
|
|
|
module Build.LinuxMkLibs (mklibs) where
|
2013-12-24 17:13:17 +00:00
|
|
|
|
|
|
|
import Data.Maybe
|
|
|
|
import System.FilePath
|
|
|
|
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
|
2022-08-30 19:20:04 +00:00
|
|
|
import qualified System.Info
|
2015-12-28 16:41:36 +00:00
|
|
|
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
|
2020-11-09 16:06:53 +00:00
|
|
|
import Utility.Path.AbsRel
|
2013-12-24 17:13:17 +00:00
|
|
|
import Utility.FileMode
|
|
|
|
import Utility.CopyFile
|
2020-11-09 16:06:53 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2020-08-03 18:34:15 +00:00
|
|
|
mklibs :: FilePath -> a -> IO Bool
|
2020-08-03 17:43:21 +00:00
|
|
|
mklibs top _installedbins = do
|
2013-12-24 17:13:17 +00:00
|
|
|
fs <- dirContentsRecursive top
|
|
|
|
exes <- filterM checkExe fs
|
2023-01-28 17:50:33 +00:00
|
|
|
libs <- runLdd exes
|
2020-07-31 18:42:03 +00:00
|
|
|
|
2013-12-24 17:13:17 +00:00
|
|
|
glibclibs <- glibcLibs
|
|
|
|
let libs' = nub $ libs ++ glibclibs
|
2020-07-31 18:42:03 +00:00
|
|
|
let (linkers, otherlibs) = partition ("ld-linux" `isInfixOf`) libs'
|
|
|
|
libdirs <- nub . catMaybes <$> mapM (installLib installFile top) otherlibs
|
|
|
|
libdirs' <- consolidateUsrLib top libdirs
|
|
|
|
|
|
|
|
gconvlibs <- gconvLibs
|
|
|
|
mapM_ (installLib installFile top) gconvlibs
|
2013-12-24 17:25:02 +00:00
|
|
|
|
|
|
|
-- Various files used by runshell to set up env vars used by the
|
|
|
|
-- linker shims.
|
2020-07-31 18:42:03 +00:00
|
|
|
writeFile (top </> "libdirs") (unlines libdirs')
|
2020-11-09 16:06:53 +00:00
|
|
|
writeFile (top </> "gconvdir") (fromRawFilePath $ parentDir $ toRawFilePath $ Prelude.head gconvlibs)
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2020-07-31 18:42:03 +00:00
|
|
|
mapM_ (installLib installFile top) linkers
|
|
|
|
let linker = Prelude.head linkers
|
2015-02-16 23:36:26 +00:00
|
|
|
mapM_ (installLinkerShim top linker) exes
|
2020-07-31 18:42:03 +00:00
|
|
|
|
2020-08-03 18:34:15 +00:00
|
|
|
return (any hwcaplibdir libdirs)
|
|
|
|
where
|
|
|
|
-- hwcap lib dirs are things like foo/tls and foo/x86.
|
|
|
|
-- Hard to know if a directory is, so this is a heuristic
|
2023-03-14 02:39:16 +00:00
|
|
|
-- looking for things that are certainly not. If this heuristic
|
2020-08-03 18:34:15 +00:00
|
|
|
-- fails, a minor optimisation will not happen, but there will be
|
|
|
|
-- no bad results.
|
|
|
|
hwcaplibdir d = not $ or
|
|
|
|
[ "lib" == takeFileName d
|
|
|
|
-- eg, "lib/x86_64-linux-gnu"
|
|
|
|
, "-linux-" `isInfixOf` takeFileName d
|
|
|
|
]
|
|
|
|
|
2020-07-31 18:42:03 +00:00
|
|
|
{- If there are two libdirs that are the same except one is in
|
|
|
|
- usr/lib and the other is in lib/, move the contents of the usr/lib one
|
|
|
|
- into the lib/ one. This reduces the number of directories the linker
|
|
|
|
- needs to look in, and so reduces the number of failed stats
|
|
|
|
- and improves startup time.
|
|
|
|
-}
|
|
|
|
consolidateUsrLib :: FilePath -> [FilePath] -> IO [FilePath]
|
2020-08-10 16:20:32 +00:00
|
|
|
consolidateUsrLib top libdirs = go [] libdirs
|
2020-07-31 18:42:03 +00:00
|
|
|
where
|
|
|
|
go c [] = return c
|
2020-08-10 16:20:32 +00:00
|
|
|
go c (x:rest) = case filter (\d -> ("/usr" ++ d) == x) libdirs of
|
|
|
|
(d:_) -> do
|
|
|
|
fs <- getDirectoryContents (inTop top x)
|
2020-08-10 20:49:09 +00:00
|
|
|
forM_ fs $ \f -> do
|
|
|
|
let src = inTop top (x </> f)
|
|
|
|
let dst = inTop top (d </> f)
|
2020-07-31 18:42:03 +00:00
|
|
|
unless (dirCruft f) $
|
2020-08-10 20:49:09 +00:00
|
|
|
unlessM (doesDirectoryExist src) $
|
|
|
|
renameFile src dst
|
2022-08-30 19:20:04 +00:00
|
|
|
symlinkHwCapDirs top d
|
2020-08-10 16:20:32 +00:00
|
|
|
go c rest
|
2023-09-07 16:59:20 +00:00
|
|
|
_ -> do
|
|
|
|
symlinkHwCapDirs top x
|
|
|
|
go (x:c) rest
|
2013-12-24 17:13:17 +00:00
|
|
|
|
2022-08-30 19:20:04 +00:00
|
|
|
{- The linker looks for optimised versions of libraries depending on the
|
|
|
|
- hardware capabilities. That causes a lot of extra work searching for
|
|
|
|
- libraries, so to avoid it, make symlinks from the hwcap directories
|
|
|
|
- to the libdir. This way, the linker will find a library the first place
|
|
|
|
- it happens to look for it.
|
|
|
|
-}
|
|
|
|
symlinkHwCapDirs :: FilePath -> FilePath -> IO ()
|
|
|
|
symlinkHwCapDirs top libdir = forM_ hwcapdirs $ \d ->
|
|
|
|
unlessM (doesDirectoryExist (top ++ libdir </> d)) $ do
|
|
|
|
createDirectoryIfMissing True (top ++ libdir </> takeDirectory d)
|
|
|
|
link <- relPathDirToFile
|
|
|
|
(toRawFilePath (top ++ takeDirectory (libdir </> d)))
|
|
|
|
(toRawFilePath (top ++ libdir))
|
|
|
|
let link' = case fromRawFilePath link of
|
|
|
|
"" -> "."
|
|
|
|
l -> l
|
|
|
|
createSymbolicLink link' (top ++ libdir </> d)
|
|
|
|
where
|
|
|
|
hwcapdirs = case System.Info.arch of
|
|
|
|
"x86_64" ->
|
|
|
|
-- See glibc's sysdeps/x86_64/dl-hwcaps-subdirs.c
|
|
|
|
-- for list of subarchitecture directories.
|
|
|
|
[ "glibc-hwcaps/x86-64-v2"
|
|
|
|
, "glibc-hwcaps/x86-64-v3"
|
|
|
|
, "glibc-hwcaps/x86-64-v4"
|
|
|
|
-- The linker later checks these, and will check
|
|
|
|
-- them when none of the above subarchitectures
|
|
|
|
-- are supported by the processor, so make them
|
|
|
|
-- just in case.
|
|
|
|
, "tls/x86_64"
|
|
|
|
, "x86_64"
|
|
|
|
]
|
|
|
|
"i386" ->
|
|
|
|
[ "tls/i686"
|
2022-09-05 17:20:23 +00:00
|
|
|
, "tls/i586"
|
2022-08-30 19:20:04 +00:00
|
|
|
, "i686"
|
2022-09-05 17:20:23 +00:00
|
|
|
, "i586"
|
2022-08-30 19:20:04 +00:00
|
|
|
]
|
|
|
|
"arm" ->
|
|
|
|
-- Probably not complete, only what I have
|
|
|
|
-- observed.
|
|
|
|
[ "tls/v7l"
|
|
|
|
, "v7l"
|
|
|
|
]
|
|
|
|
_ -> []
|
|
|
|
|
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)
|
2016-11-10 19:12:08 +00:00
|
|
|
ifM (isSymbolicLink <$> getSymbolicLinkStatus exe)
|
|
|
|
( do
|
|
|
|
sl <- readSymbolicLink exe
|
2020-10-29 14:33:12 +00:00
|
|
|
removeWhenExistsWith removeLink exe
|
|
|
|
removeWhenExistsWith removeLink exedest
|
2016-11-10 19:12:08 +00:00
|
|
|
-- Assume that for a symlink, the destination
|
|
|
|
-- will also be shimmed.
|
|
|
|
let sl' = ".." </> takeFileName sl </> takeFileName sl
|
|
|
|
createSymbolicLink sl' exedest
|
|
|
|
, renameFile exe exedest
|
|
|
|
)
|
2020-11-09 16:06:53 +00:00
|
|
|
link <- relPathDirToFile
|
|
|
|
(toRawFilePath (top </> exedir))
|
|
|
|
(toRawFilePath (top ++ linker))
|
2015-02-16 23:36:26 +00:00
|
|
|
unlessM (doesFileExist (top </> exelink)) $
|
2020-11-09 16:06:53 +00:00
|
|
|
createSymbolicLink (fromRawFilePath link) (top </> exelink)
|
2013-12-24 17:13:17 +00:00
|
|
|
writeFile exe $ unlines
|
|
|
|
[ "#!/bin/sh"
|
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
|
|
|
]
|
2020-11-09 16:06:53 +00:00
|
|
|
modifyFileMode (toRawFilePath exe) $ addModes executeModes
|
2013-12-24 17:13:17 +00:00
|
|
|
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
|
|
|
|
|
|
|
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
|
2020-11-09 16:06:53 +00:00
|
|
|
destdir = inTop top $ fromRawFilePath $ parentDir $ toRawFilePath 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)
|
2016-11-10 19:12:08 +00:00
|
|
|
( checkFileExe <$> readProcess "file" ["-L", f]
|
2013-12-24 17:13:17 +00:00
|
|
|
, 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
|
|
|
|
]
|