-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathSetup.hs
More file actions
executable file
·141 lines (116 loc) · 5.7 KB
/
Setup.hs
File metadata and controls
executable file
·141 lines (116 loc) · 5.7 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
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiWayIf #-}
{-# OPTIONS -Wall #-}
module Main where
import Distribution.Extra.Doctest
import Distribution.PackageDescription hiding (Flag)
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import Distribution.System
import Distribution.Verbosity
#if MIN_VERSION_Cabal(3,14,0)
-- Note [Cabal 3.14]
--
-- If you change any path stuff, either test that the package still works with
-- Cabal 3.12 or stop declaring support for it in accelerate.cabal. (If you do
-- the latter, also remove all of the other conditionals in this file.)
-- Note that supporting old versions of Cabal is useful for being able to run
-- e.g. Accelerate on old GPU clusters, which is nice.
import Distribution.Utils.Path (SymbolicPath, FileOrDir(Dir), Pkg, CWD, interpretSymbolicPath)
#endif
import Control.Monad
import Data.Maybe
import System.Directory
import System.FilePath
import Text.Printf
main :: IO ()
main =
defaultMainWithHooks (doctestsUserHooks "doctest")
{ preConf = preConfHook
, postBuild = postBuildHook
}
preConfHook :: Args -> ConfigFlags -> IO HookedBuildInfo
preConfHook args config_flags = do
let verbosity = fromFlagOrDefault normal $ configVerbosity config_flags
when (tracyMode config_flags) $ do
yes <- doesFileExist "cbits/tracy/public/TracyClient.cpp"
if yes
then
-- Nix (and apparently future versions of stack) automatically update
-- submodules, so there is no need to do so again.
return ()
else do
-- Stack and cabal based builds require updating the submodules
git <- doesDirectoryExist ".git"
if git
then rawSystemExit' verbosity "git" ["submodule", "update", "--init", "--recursive"]
else do
-- XXX: This must be kept up to date with the git submodule revision
let archive = "v0.11.1.tar.gz"
createDirectoryIfMissing True "cbits/tracy"
rawSystemExit' verbosity "curl" ["-LO", "https://github.com/wolfpld/tracy/archive/refs/tags/" ++ archive]
rawSystemExit' verbosity "tar" ["-xzf", archive, "-C", "cbits/tracy", "--strip-components", "1"]
removeFile archive
preConf simpleUserHooks args config_flags
-- TODO: only build the executables which are enabled
--
postBuildHook :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
postBuildHook args build_flags pkg_desc lbi = do
let Platform _ os = hostPlatform lbi
verbosity = fromFlagOrDefault normal $ buildVerbosity build_flags
targets = [ ("tracy-capture", "capture", "tracy-capture")
, ("tracy", "profiler", "tracy-profiler") ]
when (tracyMode (configFlags lbi)) $ do
case os of
Windows -> return () -- XXX TODO: Windows users get the dummy executable that just throws an error
_ ->
forM_ targets $ \(hs_exe, c_dir, c_exe) -> do
let cwd = flagToMaybe (workingDirFlag build_flags)
c_projdir = "cbits/tracy" </> c_dir
hs_builddir = interpretSymbolicPath cwd (buildDir lbi) </> hs_exe
hs_tmpdir = hs_builddir </> hs_exe ++ "-tmp"
-- TODO: This creates a separate build directory for each tracy
-- executable (of which we build two, at the time of writing). This
-- means that some duplicate work is done (building capstone twice).
-- Could we share a build directory between the two?
-- Existence of the hs_exe doesn't mean anything because before we
-- overwrite it it's just a dummy executable. So check existence (and
-- mtime) of the c_exe instead.
c_exe_exists <- doesFileExist (hs_tmpdir </> c_exe)
tracy_newer <- if c_exe_exists
then do c_exe_modtime <- getModificationTime (hs_tmpdir </> c_exe)
tracy_modtime <- getModificationTime "cbits/tracy"
return (tracy_modtime > c_exe_modtime)
else return True
when tracy_newer $ do
setupMessage verbosity (printf "Building executable '%s' from Tracy C++ sources for" hs_exe) (package pkg_desc)
-- We set LEGACY=1 so that tracy builds with X11 instead of Wayland.
rawSystemExit' verbosity "cmake" ["-B", hs_tmpdir, "-S", c_projdir, "-DCMAKE_BUILD_TYPE=Release", "-DLEGACY=1"]
-- Build in parallel with 2 jobs because likely, accelerate is one of
-- the last dependencies in a build, so we aren't stealing CPU time
-- from other packages, and tracy takes way too long to build
rawSystemExit' verbosity "cmake" ["--build", hs_tmpdir, "--config", "Release", "-j", "2"]
-- Copy, not rename, to prevent cmake from linking again on the next
-- reconfigure
copyFile (hs_tmpdir </> c_exe) (hs_builddir </> hs_exe)
postBuild simpleUserHooks args build_flags pkg_desc lbi
tracyMode :: ConfigFlags -> Bool
tracyMode config_flags =
fromMaybe False $
lookupFlagAssignment (mkFlagName "tracy") (configConfigurationsFlags config_flags)
rawSystemExit' :: Verbosity -> FilePath -> [String] -> IO ()
#if MIN_VERSION_Cabal(3,14,0)
rawSystemExit' verb = rawSystemExit verb Nothing
workingDirFlag :: BuildFlags -> Flag (SymbolicPath CWD ('Dir Pkg))
workingDirFlag = setupWorkingDir . buildCommonFlags
-- interpretSymbolicPath is an actual useful function in Cabal-3.14
#else
rawSystemExit' = rawSystemExit
workingDirFlag :: BuildFlags -> Flag ()
workingDirFlag _ = NoFlag
interpretSymbolicPath :: Maybe () -> FilePath -> FilePath
interpretSymbolicPath _ = id
#endif