-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathBoot.hs
1356 lines (1198 loc) · 50.2 KB
/
Boot.hs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# OPTIONS_GHC -O0 #-}
{- |
The ghcjs-boot program installs the libraries and runtime system for GHCJS
You can explicitly set the boot source location with the -s option. It
can either be a directory containing the boot.yaml file or a boot.tar
archive.
if the -s option is not set, the following locations are tried in order:
1. the current directory if it contains boot.yaml
2. the boot.tar file installed by cabal install in the GHCJS
data directory
ghcjs-boot installs the libraries into the GHCJS library directory,
which is set by the `ghcjs' and `ghcjs-pkg' scripts, which pass the
library path with the -B option to the underlying binary executable.
modify the scripts to change the installation location
-}
{-# LANGUAGE ExtendedDefaultRules,
OverloadedStrings,
ScopedTypeVariables,
TemplateHaskell,
LambdaCase,
FlexibleInstances,
DeriveDataTypeable,
GeneralizedNewtypeDeriving,
NoMonomorphismRestriction,
FlexibleContexts,
RankNTypes,
TupleSections
#-}
module Main (main) where
import Prelude
hiding (elem, mapM, mapM_, any, all, concat, concatMap)
-- import qualified Prelude
import qualified Distribution.Simple.Utils as Cabal
import qualified Distribution.Verbosity as Cabal
import qualified Codec.Archive.Tar as Tar
import qualified Codec.Archive.Tar.Entry as Tar
import Control.Applicative
import qualified Control.Exception as Ex
import Control.Lens hiding ((<.>))
import Control.Monad.Reader (MonadReader, ReaderT(..), ask )
import Control.Monad.State
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.Data
import Data.Data.Lens
import Data.Foldable
import qualified Data.HashMap.Strict as HM
import Data.List (intercalate, transpose)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Vector as V
import Data.Yaml ((.:))
import qualified Data.Yaml as Yaml
import GHC.IO.Encoding
(setLocaleEncoding, setForeignEncoding, utf8)
import Options.Applicative hiding (info)
import qualified Options.Applicative as O
import System.Directory
import System.Environment (lookupEnv, setEnv)
import System.Environment.Executable (getExecutablePath)
import System.Exit
(exitSuccess, exitFailure, ExitCode(..))
import qualified System.FilePath as FP
import System.FilePath -- hiding ((<.>), (</>))
import System.IO
(hPutStrLn, hSetBuffering, stderr, stdout, BufferMode(..), hClose)
import System.PosixCompat.Files (setFileMode)
import System.Process (readCreateProcess, createProcess, waitForProcess, {-CreateProcess(..),-} proc)
-- import Shelly ((<.>), fromText)
-- import qualified Shelly as Sh
import Compiler.GhcjsProgram (printVersion)
import qualified Compiler.Info as Info
import qualified Compiler.Utils as Utils
import Compiler.Settings (NodeSettings(..))
-- import qualified Paths_ghcjs as Paths
import System.IO.Error (isDoesNotExistError)
import Compiler.Platform (isWindows)
-- fixme force overwrite?
default (Text)
newtype Verbosity = Verbosity Int deriving (Eq, Ord, Data, Typeable)
trace, info, warn, err :: Verbosity
trace = Verbosity 3
info = Verbosity 2
warn = Verbosity 1
err = Verbosity 0
data BootSettings = BootSettings
{ _bsShowVersion :: Bool -- ^show the version and exit
, _bsDebug :: Bool {- ^build debug version of the libraries
(GHCJS records the STG in the object
files for easier inspection) -}
, _bsProf :: Bool -- ^build profiling version of the libraries
, _bsHaddock :: Bool -- ^build documentation
, _bsVerbosity :: Verbosity -- ^verbosity level 0..3, 2 is default
, _bsWithCabal :: Maybe Text {- ^location of cabal (cabal-install)
executable, must have GHCJS support -}
-- , _bsBindir :: Maybe Text -- ^ bin directory to install GHCJS programs
-- , _bsLibexecDir :: Maybe Text -- ^ location of GHCJS executables
-- , _bsGhcjsLibdir :: Maybe Text -- ^ location to install GHCJS boot libraries
, _bsWithEmsdk :: Maybe Text
, _bsWithGhc :: Maybe Text {- ^location of GHC compiler (must have a
GHCJS-compatible Cabal library
installed. ghcjs-boot copies some files
from this compiler) -}
, _bsWithNode :: Maybe Text -- ^location of the node.js program
, _bsWithNodePath :: Maybe Text -- ^ NODE_PATH to use when running node.js Template Haskell or REPL
-- (if unspecified, GHCJS uses bundled packages)
, _bsNodeExtraArgs :: Maybe Text -- ^ extra node arguments
, _bsSourceDir :: Maybe Text -- ^source directory (can be a tar file)
} deriving (Ord, Eq, Data, Typeable)
{- | Stage configuration file: packages to install in each stage
see boot.yaml for more information
-}
data BootStages = BootStages
{ _bstStage1a :: Stage
, _bstStage1b :: Stage
, _bstCabal :: Package {- ^installed between 1b and 2,
only when doing a full boot -}
, _bstGhcjsPrim :: Package -- ^installed between 1a and 1b
} deriving (Data, Typeable)
type Stage = [Package]
type Package = Text {- ^just the package name/location, unversioned
can be a directory name
(starting with ./ relative to the ghcjs-boot root),
a url or a plain package name
-}
data BootLocations = BootLocations
{ _blSourceDir :: FilePath
, _blBuildDir :: FilePath
, _blGhcjsTopDir :: FilePath
, _blGlobalDB :: FilePath -- ^global package database
-- , _blLibexecDir :: FilePath
, _blEmsdk :: FilePath
} deriving (Data, Typeable, Show)
data Program = Program
{ _pgmName :: Text -- ^program name for messages
, _pgmSearch :: Text {- ^name searched for when configuring the
program (from command line or config file)
-}
, _pgmVersion :: Maybe Text -- ^version if known
, _pgmLoc :: Maybe FilePath -- ^absolute path to the program
, _pgmArgs :: [Text] -- ^extra arguments to pass to the program
} deriving (Data, Typeable, Show)
-- | configured programs, fail early if any of the required programs is missing
data BootPrograms = BootPrograms { _bpGhc :: Program
, _bpCabal :: Program
, _bpNode :: Program
} deriving (Data, Typeable, Show)
data BootEnv = BootEnv { _beSettings :: BootSettings
, _beLocations :: BootLocations
, _bePrograms :: BootPrograms
, _beStages :: BootStages
}
data BootConfigFile = BootConfigFile BootStages BootPrograms
deriving (Data, Typeable)
makeLenses ''Program
makeLenses ''BootSettings
makeLenses ''BootLocations
makeLenses ''BootPrograms
makeLenses ''BootStages
makeLenses ''BootEnv
main :: IO ()
main = do
settings <- execParser optParser'
when (settings ^. bsShowVersion) (printVersion >> exitSuccess)
hSetBuffering stdout LineBuffering
setLocaleEncoding utf8
setForeignEncoding utf8
env <- initBootEnv settings
printBootEnvSummary False env
r <- runReaderT ((actions >> pure Nothing)
`catchAny` (pure . Just)) env
maybe exitSuccess Ex.throwIO r
where
actions :: B ()
actions = do
e <- ask
liftIO $ setEnv "GHC_CHARENC" "UTF-8"
cleanCache
prepareLibDir
let base = libDir (e ^. beLocations)
liftIO $ setEnv "CFLAGS" ("-I" <> (base </> "include"))
installStage1
installCabal
when (e ^. beSettings . bsHaddock) buildDocIndex
liftIO . printBootEnvSummary True =<< ask
instance Yaml.FromJSON BootPrograms where
parseJSON (Yaml.Object v) = BootPrograms
<$> v ..: "ghc"
<*> v ..: "cabal"
<*> v ..: "node"
where
o ..: p = ((\t -> Program p t Nothing Nothing []) <$> o .: p) <|>
(withArgs p =<< o .: p)
withArgs :: Text -> Yaml.Value -> Yaml.Parser Program
withArgs p (Yaml.Object o) | [(k,v)] <- HM.toList o =
Program p k Nothing Nothing <$> Yaml.parseJSON v
withArgs _ _ =
mempty
parseJSON _ = mempty
instance Yaml.FromJSON BootStages where
parseJSON (Yaml.Object v) = BootStages
<$> v ..: "stage1a"
<*> v ..: "stage1b"
<*> v .: "cabal"
<*> v .: "ghcjs-prim"
where
o ..: p = pkgs =<< o .: p
pkgs (Yaml.String t) =
pure [t]
pkgs (Yaml.Array v) =
concat <$> mapM pkgs (V.toList v)
pkgs __ =
mempty
parseJSON _ = mempty
instance Yaml.FromJSON BootConfigFile where
parseJSON (Yaml.Object v) = BootConfigFile
<$> v .: "packages" <*> v .: "programs"
parseJSON _ = mempty
optParser' :: ParserInfo BootSettings
optParser' =
O.info (helper <*> optParser)
(fullDesc <>
header "GHCJS booter, build base libraries for the compiler" <>
progDesc description)
description :: String
description = unlines
[ "ghcjs-boot builds an initial set of libraries for GHCJS."
]
optParser :: Parser BootSettings
optParser =
BootSettings
<$> switch
(long "version" <>
help "show the ghcjs-boot version")
<*> switch
(long "debug" <>
short 'd' <>
help "build debug libraries with extra checks")
<*> (fmap not . switch)
(long "no-prof" <>
help "don't generate profiling version of the libraries")
<*> (fmap not . switch)
(long "no-haddock" <>
help "don't generate documentation")
<*> (fmap Verbosity . option auto)
(long "verbosity" <>
short 'v' <>
value 2 <>
help "verbose output")
<*> (optional . fmap T.pack . strOption)
(long "with-cabal" <> metavar "PROGRAM" <>
help "cabal program to use")
<*> (optional . fmap T.pack . strOption)
(long "with-emsdk" <>
metavar "DIR" <>
help "location of Emscripten SDK")
<*> (optional . fmap T.pack . strOption)
(long "with-ghc" <>
metavar "PROGRAM" <>
help "ghc program to use")
<*> (optional . fmap T.pack . strOption)
(long "with-node" <>
metavar "PROGRAM" <>
help "node.js program to use")
<*> (optional . fmap T.pack . strOption)
(long "with-node-path" <> metavar "PATH" <>
help "value of NODE_PATH environment variable when running Template Haskell or GHCJSi")
<*> (optional . fmap T.pack . strOption)
(long "extra-node-args" <> metavar "ARGS" <>
help "extra arguments to pass to node.js")
<*> (optional . fmap T.pack . strOption)
(long "source-dir" <>
short 's' <>
metavar "DIR" <>
help "location of GHCJS library sources")
initPackageDB :: B ()
initPackageDB = do
msg info "creating package databases"
initDB "--global" <^> beLocations . blGlobalDB
where
initDB dbName db = do
rm_rf db
ghcjs_pkg_ ["init", T.pack db] `catchAny_` return ()
ghcjs_pkg_ ["recache", dbName]
cleanCache :: B ()
cleanCache =
liftIO Info.getUserCacheDir >>= \case
Just p -> rm_rf p `catchAny_` return ()
Nothing -> return ()
prepareLibDir :: B ()
prepareLibDir = do
msg info "preparing GHCJS library directory"
locs <- view beLocations
pgms <- view bePrograms
liftIO $ print (locs, pgms)
ver <- liftIO $ ghcjsVersion pgms
let globalDB = locs ^. blGlobalDB
ghcjsLib = libDir locs
ghcjsTop = locs ^. blGhcjsTopDir
rtsConfFile = "rts.conf"
wrapperSrcDir = (locs ^. blSourceDir) </> "input" </> "wrappers"
wenv = WrapperEnv { weTopDir = T.pack (libDir locs)
, weBinDir = T.pack ((locs ^. blGhcjsTopDir) </> "bin")
, weLibexecDir = T.pack (libDir locs </> "bin")
, weVersion = ver
, weGhcVersion = ver
, weEmsdk = T.pack (locs ^. blEmsdk)
}
rtsConf <- replacePlaceholders wenv <$>
liftIO (T.readFile ((locs ^. blSourceDir) </> "input" </> "rts" <.> "conf"))
mkdir_p (ghcjsLib </> "bin")
msg info "copying GHCJS executables"
-- copy the executables from the package to the libraries directory
-- (the executables in the package have been prefixed with `private-ghcjs-`
-- as a workaround for cabal-install incorrectly generating symlinks for
-- private executables)
let cpExe name = do
let srcName = ghcjsTop </> "bin" </> exe (T.unpack $ "private-ghcjs-" <> name)
tgtName = ghcjsLib </> "bin" </> exe (T.unpack name)
cp srcName tgtName
liftIO . Cabal.setFileExecutable $ tgtName
mapM_ cpExe [ "unlit", "run" ]
when isWindows (mapM_ cpExe ["touchy", "wrapper"])
msg info "creating emscripten wrapper"
liftIO $ copyWrapper "emcc"
Nothing
wenv
(ghcjsLib </> "bin")
wrapperSrcDir
(ghcjsLib </> "bin")
msg info "adding RTS package"
initPackageDB
liftIO $ T.writeFile (globalDB </> rtsConfFile) rtsConf
ghcjs_pkg_ ["recache", "--global", "--no-user-package-db"]
cp_r ((locs ^. blSourceDir) </> "shims") (ghcjsLib </> "shims")
cp_r ((locs ^. blSourceDir) </> "data") ghcjsLib
liftIO . T.writeFile (ghcjsLib </> "settings") =<< generateSettings
-- workaround for hardcoded check of mingw dir in ghc when building on Windows
when isWindows (mkdir_p $ ghcjsTop </> "mingw")
prepareNodeJs
unless isWindows $ do
let runSh = ghcjsLib </> "run" <.> "sh"
liftIO (T.writeFile runSh "#!/bin/sh\nCOMMAND=$1\nshift\n\"$COMMAND\" \"$@\"\n")
liftIO (Cabal.setFileExecutable =<< makeAbsolute runSh)
-- make an empty object file for
subLib $ do
liftIO (T.writeFile "empty.c" "")
ghc_ ["-c", "empty.c"]
msg info "RTS prepared"
generateSettings :: B Text
generateSettings = do
emsdk <- view (beLocations . blEmsdk)
-- do we also need emar, emranlib?
let opts :: [(String, String)]
opts = [("GCC extra via C opts", " -fwrapv -fno-builtin"),
("C compiler command", emsdk </> "upstream" </> "bin" </> "clang" {-"asmjs-none-gcc"-} {-T.unpack emccProgram-}),
("C compiler flags", ""),
("C++ compiler flags",""),
("C compiler link flags", " "),
("C compiler supports -no-pie", "NO"),
("Haskell CPP command", emsdk </> "upstream" </> "bin" </> "clang"),
("Haskell CPP flags","-E -undef -traditional -Wno-invalid-pp-token -Wno-unicode -Wno-trigraphs"),
("ld command", "ld"),
("ld flags", ""),
("ld supports compact unwind", "YES"),
("ld supports build-id", "NO"),
("ld supports filelist", "YES"),
("ld is GNU ld", "NO"),
("Merge objects command", "ld.gold"),
("Merge objects flags", "-r"),
("ar command", emsdk </> "upstream" </> "emscripten" </> "emar"),
("ar flags", "qcls"),
("ar supports at file", "NO"),
("ranlib command", emsdk </> "upstream" </> "emscripten" </> "emranlib"),
("otool command", "otool"),
("install_name_tool command", "install_name_tool"),
("touch command", if isWindows then "$topdir/bin/touchy.exe" else "touch"),
("dllwrap command", "/bin/false"),
("windres command", "/bin/false"),
("libtool command", "libtool"),
("unlit command", "$topdir/bin/unlit"),
("cross compiling", "YES"),
("target os", "OSUnknown"),
("target arch", "ArchJavaScript"),
("target platform string", T.unpack ghcjsTriple),
("Target platform", T.unpack ghcjsTriple),
("target word size", "4"),
("target has GNU nonexec stack", "NO"),
("target has .ident directive", "YES"),
("target has subsections via symbols", "YES"),
("target has RTS linker", "YES"),
("integer library", "integer-gmp"),
("Unregisterised", "YES"),
("LLVM target", T.unpack ghcjsTriple),
("LLVM llc command", "llc"),
("LLVM opt command", "opt"),
("LLVM clang command", "clang"),
("Tables next to code","YES"),
("Use interpreter","YES"),
("Use native code generator","YES"),
("Support SMP","NO"),
("RTS ways","l debug debug_p"),
("Leading underscore","NO"),
("Use LibFFI","NO"),
("Use Threads","YES"),
("Use Debugging","NO"),
("RTS expects libdw","NO"),
("Support parallel --make","YES"),
("Support reexported-modules","YES"),
("Support thinning and renaming package flags","YES"),
("Support Backpack","YES"),
("Requires unified installed package IDs","YES"),
("Uses package keys","YES"),
("Uses unit IDs","YES")
]
pure $ T.pack ("[" ++ intercalate ",\n" (map show opts) ++ "\n]")
prepareNodeJs :: B ()
prepareNodeJs = do
ghcjsLib <- libDir <$> view beLocations
buildDir <- view (beLocations . blBuildDir)
nodeProgram <- view (bePrograms . bpNode . pgmLoc . to (maybe "-" T.pack))
mbNodePath <- view (beSettings . bsWithNodePath)
extraArgs <- view (beSettings . bsNodeExtraArgs)
-- If no setting for NODE_PATH is specified, we use the libraries bundled
-- with the ghcjs-boot submodule. We must run "npm rebuild" to build
-- any sytem-specific components.
when (isNothing mbNodePath) $ do
subLib (mkdir_p "ghcjs-node")
liftIO $ unpackTar False
True
ghcjsLib
(buildDir </> "ghcjs-node.tar")
subLib' "ghcjs-node" $ npm_ ["rebuild"]
-- write nodeSettings.json file
let nodeSettings = NodeSettings
{ nodeProgram = T.unpack nodeProgram
, nodePath = mbNodePath
, nodeExtraArgs = maybeToList extraArgs
, nodeKeepAliveMaxMem = 536870912
}
liftIO $ BL.writeFile (T.unpack . T.pack $ ghcjsLib </> "nodeSettings.json")
(Aeson.encode $ Aeson.toJSON nodeSettings)
exe :: FilePath -> FilePath
exe = bool isWindows (<.>"exe") id
buildDocIndex :: B ()
buildDocIndex = subTop' "doc" $ do
haddockFiles <- findWhen (return . isExtensionOf "haddock") "."
haddock_ $ ["--gen-contents"
,"--gen-index"
, "-o"
, "html"
, "--title=GHCJS Libraries"
] ++
map (\p -> "--read-interface=../" <>
T.pack (takeDirectory p) <>
"," <>
T.pack p)
haddockFiles
installCabal :: B ()
installCabal = subBuild $ do
msg info "installing Cabal library"
preparePackage "Cabal"
buildCabalPackage "Cabal"
installGhcjsPrim :: B ()
installGhcjsPrim = do
msg info "installing ghcjs-prim"
preparePackage "ghcjs-prim"
buildCabalPackage "ghcjs-prim"
installGhcjsTh :: B ()
installGhcjsTh = do
msg info "installing ghcjs-th"
preparePackage "ghcjs-th"
buildCabalPackage "ghcjs-th"
installStage1 :: B ()
installStage1 = subBuild $ do
buildCabalPackage "ghc-prim"
mapM_ installPackage =<< stagePackages bstStage1a
installGhcjsPrim
mapM_ installPackage =<< stagePackages bstStage1b
installGhcjsTh
resolveWiredInPackages
where
installPackage pkg = do
msg info ("installing package " <> pkg)
preparePackage pkg
buildCabalPackage pkg
resolveWiredInPackages :: B ()
resolveWiredInPackages = subLib $ do
wips <- readBinary ("wiredinpkgs" <.> "yaml")
case Yaml.decodeEither' wips of
Left err -> failWith $
"error parsing wired-in packages file wiredinpkgs.yaml\n" <> T.pack (show err)
Right pkgs -> do
pkgs' <- forM pkgs $ \p ->
(p,) . T.strip <$> ghcjs_pkg [ "--simple-output"
, "field"
, p
, "key"
]
liftIO (T.writeFile ("wiredinkeys" <.> "yaml") (
T.unlines ("# resolved wired-in packages" :
map (\(p,k) -> p <> ": " <> k) pkgs')))
preparePackage :: Package -> B ()
preparePackage pkg
| "./" `T.isPrefixOf` pkg || "../" `T.isPrefixOf` pkg = sub $ do
msg trace ("preparing package " <> pkg)
cd (T.unpack pkg)
rm_rf "dist"
| otherwise = return ()
-- | subshell in path relative to build dir
subBuild :: B a -> B a
subBuild a = sub (view (beLocations . blBuildDir) >>= cd >> a)
-- | subshell in path relative to top installation dir
subTop' :: FilePath -> B a -> B a
subTop' p a = subTop (cd p >> a)
subTop :: B a -> B a
subTop a = sub (view (beLocations . blGhcjsTopDir) >>= cd >> a)
subLib' :: FilePath -> B a -> B a
subLib' p a = subLib (cd p >> a)
subLib :: B a -> B a
subLib a = sub ((libDir <$> view beLocations) >>= cd >> a)
{- |unpack a tar file (does not support compression)
only supports files, does not try to emulate symlinks -}
unpackTar :: Bool -- ^strip the first directory component?
-> Bool -- ^preserve symbolic links?
-> Prelude.FilePath -- ^destination to unpack to
-> Prelude.FilePath -- ^the tar file
-> IO ()
unpackTar stripFirst preserveSymlinks dest tarFile = do
createDirectoryIfMissing True dest
entries <- Tar.read . BL.fromStrict <$> B.readFile tarFile
void $ Tar.foldEntries (\e -> (>>=checkExtract e))
(return Nothing)
(\e -> failWith $ "error unpacking tar: " <> showT e)
entries
where
failSec e msg = failWith $ "tar security check, " <>
msg <>
": " <>
T.pack (Tar.entryPath e)
checkExtract e Nothing
| (p:_) <- FP.splitDirectories (Tar.entryPath e)
= checkExtract e (Just p)
| otherwise = failSec e $
"no path"
checkExtract e je@(Just expected)
| FP.isAbsolute ep = failSec e $
"absolute path"
| elem ".." epd = failSec e $
"'..' in path"
| listToMaybe epd /= je &&
isSupportedEntry (Tar.entryContent e) = failSec e $
"tar bomb, expected path component: " <> T.pack expected
| otherwise = do
(extractEntry e $ dest FP.</>
(FP.joinPath
(drop (if stripFirst then 1 else 0) epd)))
return je
where ep = Tar.entryPath e
epd = FP.splitDirectories ep
isSupportedEntry Tar.NormalFile{} = True
isSupportedEntry Tar.Directory{} = True
isSupportedEntry _ = False
extractEntry :: Tar.Entry -> Prelude.FilePath -> IO ()
extractEntry e tgt
| Tar.NormalFile bs _size <- Tar.entryContent e = do
createDirectoryIfMissing True (FP.dropFileName tgt)
BL.writeFile tgt bs
setPermissions (Tar.entryPermissions e) tgt
| Tar.Directory <- Tar.entryContent e = do
createDirectoryIfMissing True tgt
setPermissions (Tar.entryPermissions e) tgt
| Tar.SymbolicLink _linkTgt <- Tar.entryContent e
, preserveSymlinks =
createDirectoryIfMissing True (FP.dropFileName tgt)
| otherwise = hPutStrLn stderr $
"ignoring unexpected entry type in tar. " <>
"only normal files and directories (no links) " <>
"are supported:\n " <> tgt
setPermissions mode tgt = do
absTgt <- makeAbsolute tgt
setFileMode absTgt mode
run_inplace :: Text -> [Text] -> B Text
run_inplace pgm args = do
ghcjsTop <- view (beLocations . blGhcjsTopDir)
run'' (ghcjsTop </> "bin" </> T.unpack pgm) "" args
run_inplace_ :: Text -> [Text] -> B ()
run_inplace_ pgm args = do
ghcjsTop <- view (beLocations . blGhcjsTopDir)
run''_ (ghcjsTop </> "bin" </> T.unpack pgm) args
ghc_ :: [Text] -> B ()
ghc_ = runE_ bpGhc
ghcjs_pkg :: [Text] -> B Text
ghcjs_pkg = run_inplace "ghcjs-pkg"
ghcjs_pkg_ :: [Text] -> B ()
ghcjs_pkg_ = run_inplace_ "ghcjs-pkg"
haddock_ :: [Text] -> B ()
haddock_ = run_inplace_ "haddock"
npm_ :: [Text] -> B ()
npm_ args = do
nodePgm <- view (bePrograms . bpNode)
case nodePgm ^. pgmLoc of
Nothing -> failWith "nodejs program not configured when trying to run npm"
Just loc ->
if isWindows
then do
sysRoot <- fromMaybe "C:\\WINDOWS" <$> liftIO (Utils.getEnvMay "SYSTEMROOT")
let cmdLoc = sysRoot </> "system32" </> "cmd" <.> "exe"
npmLoc = FP.takeDirectory loc </> "npm" <.> "cmd"
run''_ cmdLoc ("/C" : T.pack npmLoc : args)
else do
let npmLoc = FP.takeDirectory loc </> "npm"
run''_ npmLoc args
runE_ :: ((Program -> Const Program Program)
-> BootPrograms -> Const Program BootPrograms)
-> [Text] -> B ()
runE_ g a = view (bePrograms . g) >>= \p -> run_ p a
customSetupPackages :: [Text]
customSetupPackages = [ "ghc-prim" ]
cabalConfigureFlags :: B [Text]
cabalConfigureFlags = do
debug <- view (beSettings . bsDebug)
v <- view (beSettings . bsVerbosity)
instDir <- libDir <$> view beLocations
prof <- view (beSettings . bsProf)
locs <- view beLocations
let binDir = (locs ^. blGhcjsTopDir) </> "bin"
privateBinDir = instDir </> "bin"
return $ [ "--global"
, "--ghcjs"
, "--builddir", "dist"
, "--with-compiler", (T.pack $ binDir </> "ghcjs")
, "--with-hc-pkg", (T.pack $ binDir </> "ghcjs-pkg")
, "--with-haddock", (T.pack $ binDir </> "haddock")
, "--with-gcc", (T.pack $ privateBinDir </> "emcc")
, "--prefix", T.pack (locs ^. blGhcjsTopDir)
, "--configure-option", "--host=js-unknown-ghcjs"
, "--ghcjs-options=-fwrite-ide-info"
, "--enable-debug-info"
, "--disable-library-stripping"
, "--disable-executable-stripping"
, "-fghci"
, bool prof
"--enable-profiling"
"--disable-profiling"
] ++
-- don't slow down Windows builds too much,
-- on other platforms we get this more
-- or less for free, thanks to dynamic-too
bool isWindows [] ["--enable-shared"] ++
catMaybes [ bj debug "--ghcjs-options=-debug"
, bj (v > info) "-v2"
]
buildCabalPackage :: Text -> B ()
buildCabalPackage pkg = subBuild $ do
cd ("pkg" </> T.unpack pkg) -- hopefully correct
-- build custom setup if necessary
msg info ("configuring package: " <> pkg)
configureFlags <- cabalConfigureFlags
haddock <- view (beSettings . bsHaddock)
-- configure / build
liftIO $ setEnv "GHCJS_BOOTING" "1"
case pkg `elem` customSetupPackages of
True -> do
{-
XXX there is only one custom setup package
we should probably just build the ghc-prim
Setup.hs as part of the ghcjs package, so
we can avoid having to know about GHC at all
(and have proper dependencies)
-}
ghc <- requirePgmLoc =<< view (bePrograms . bpGhc)
run''_ ghc ["Setup.hs"]
let setup args = run''_ "./Setup" args
setup ("configure" : configureFlags)
setup ["build"]
when haddock (setup ["haddock"])
setup ["copy"]
setup ["register"]
_ -> do
cmd <- requirePgmLoc =<< view (bePrograms . bpCabal)
let setup args = run''_ cmd args
setup ("v1-configure" : configureFlags)
setup ["v1-build"]
when haddock (setup ["v1-haddock"])
setup ["v1-copy"]
setup ["v1-register"]
stagePackages :: Getter BootStages Stage -> B [Package]
stagePackages l = view (beStages . l)
failWith :: MonadIO m => Text -> m a
failWith err = liftIO (T.putStrLn ("fatal: " <> err) >> exitFailure)
{- |initialize our boot environment by reading the configuration files,
finding all programs -}
initBootEnv :: BootSettings -> IO BootEnv
initBootEnv bs = do
BootConfigFile stgs pgms1 <- readBootConfigFile bs
pgms2 <- configureBootPrograms bs pgms1
locs <- configureBootLocations bs pgms2
-- installWrappers ((locs ^. blGhcjsTopDir) </> "bin") locs pgms2
return (BootEnv bs locs pgms2 stgs)
-- the platform triple that GHCJS reports
-- this is in practice mostly used for the toolchain
ghcjsTriple :: Text
ghcjsTriple = "js-unknown-ghcjs"
-- this assumes that the ghcjs-boot version is correct. perhaps we should query the ghcjs executable to verify?
ghcjsVersion :: BootPrograms -> IO Text
ghcjsVersion _ = pure (T.pack Info.getCompilerVersion)
libSubDir :: FilePath
libSubDir = "lib"
libDir :: BootLocations -> FilePath
libDir locs = (locs ^. blGhcjsTopDir) </> libSubDir
data WrapperEnv = WrapperEnv { weTopDir :: Text
, weBinDir :: Text
, weLibexecDir :: Text
, weVersion :: Text
, weGhcVersion :: Text
, weEmsdk :: Text
} deriving (Show)
--
copyWrapper :: Text
-> Maybe Text
-> WrapperEnv
-> FilePath
-> FilePath
-> FilePath
-> IO ()
copyWrapper = if isWindows then copyWrapperW else copyWrapperU
{- |
on Windows we can't run shell scripts, so we don't install wrappers
just copy program.exe to program-{version}-{ghcversion}.exe
the programs read a program-{version}-{ghcversion}.exe.options file from
the same directory, which contains the command line arguments to prepend
installation does not overwrite existing .options files
-}
copyWrapperW :: Text
-> Maybe Text
-> WrapperEnv
-> FilePath
-> FilePath
-> FilePath
-> IO ()
copyWrapperW programName mbVer env libexecDir wrapperSrcDir binDir = do
createDirectoryIfMissing False binDir
Cabal.installExecutableFile verbosity srcExe tgtExe
whenVer (Cabal.installExecutableFile verbosity srcExe tgtExeVersioned)
options <- replacePlaceholders env <$> T.readFile (wrapperSrcDir </> T.unpack programName <.> "exe" <.> "options")
createDirectoryIfMissing False binDir
Cabal.withTempFile binDir "ghcjs-options-XXXXXX.tmp" $ \tmp h -> do
T.hPutStr h options
hClose h
Cabal.installOrdinaryFile verbosity tmp tgtOptions
whenVer (Cabal.installOrdinaryFile verbosity tmp tgtOptionsVersioned)
where
verbosity = Cabal.verbose
whenVer x = when (isJust mbVer) x
srcExe = libexecDir </> "wrapper" <.> "exe"
tgtPrefix = binDir </> T.unpack programName
tgtPrefixVersioned = binDir </> T.unpack (programName <> "-" <> ver)
tgtExe = tgtPrefix <.> "exe"
tgtOptions = tgtExe <.> "options"
tgtExeVersioned = tgtPrefixVersioned <.> "exe"
tgtOptionsVersioned = tgtExeVersioned <.> "options"
ver = fromMaybe "0.0.0" mbVer
{- |
on non-Windows we copy shell scripts that pass the -B flag to ghcjs,
ghcjs-pkg etc
installation updates the symlink, but does not overwrite the wrapper
scripts if they already exist
if no wrapper is required, we simply symlink to the executable in the
libexec directory
-}
copyWrapperU :: Text
-> Maybe Text
-> WrapperEnv
-> FilePath
-> FilePath
-> FilePath
-> IO ()
copyWrapperU programName mbVer env libexecDir wrapperSrcDir binDir = do
putStrLn ("installing wrapper for: " ++ show (programName, mbVer, libexecDir, wrapperSrcDir, binDir))
createDirectoryIfMissing False binDir
wrapperScript <- replacePlaceholders env <$> T.readFile (wrapperSrcDir </> T.unpack programName <.> "sh")
Cabal.withTempFile binDir "ghcjs-wrapper-XXXXXX.tmp" $
(\tmp h -> do T.hPutStr h wrapperScript
hClose h
Cabal.installExecutableFile Cabal.normal tmp (binDir </> tgt))
when (isJust mbVer) (linkFileU Cabal.normal binDir tgt tgtPlain)
where
tgtPlain = T.unpack programName
tgt = maybe tgtPlain
(\ver -> T.unpack (programName <> "-" <> ver))
mbVer
{- |
create a symlink, overwriting the target. unix only.
it looks like the Cabal library does not have this functionality,
and since we shouldn't use system-specific libraries here,
we use a shell command instead.
the symlink is relative if both files are in the same directory
-}
linkFileU :: Cabal.Verbosity -> FilePath -> FilePath -> FilePath -> IO ()
linkFileU _v workingDir src dest = do
-- putStrLn ("link file: " ++ show (workingDir, src, dest))
let ignoreDoesNotExist :: IOError -> IO ()
ignoreDoesNotExist e | isDoesNotExistError e = return ()
| otherwise = Ex.throw e
removeFile (workingDir </> dest) `Ex.catch` ignoreDoesNotExist
wd <- getCurrentDirectory
setCurrentDirectory workingDir
createFileLink src dest
setCurrentDirectory wd
-- | replace placeholders in a wrapper script or options file
replacePlaceholders :: WrapperEnv -> Text -> Text
replacePlaceholders env xs =
foldl (\ys (p,r) -> T.replace p (r env) ys) xs
[ ("{topdir}", weTopDir)
, ("{bindir}", weBinDir)
, ("{libexecdir}", weLibexecDir)
, ("{version}", weVersion)
, ("{ghcversion}", weGhcVersion)
, ("{emsdk}", weEmsdk)
]
{- | try to find the emsdk directory
1. check --with-emsdk option
2. check EMSDK environment variable
3. find emcc executable in PATH
-}
configureEmsdkDir :: BootSettings
-> IO FilePath
configureEmsdkDir bs
| Just dir <- bs ^. bsWithEmsdk = pure (T.unpack dir)
| otherwise = do
e <- lookupEnv "EMSDK"
case e of
Just edir -> pure edir
_ -> do
mbEmccExe <- findExecutable "emcc"
case mbEmccExe of
Just emccExe ->
{- if we found the `emcc' executable, we just assume
that it's two directories deeper than the emsdk root
path, e.g. fastcomp/bin, fastcomp/emscripten, upstream/bin
perhaps we should change this to check for the emsdk python
script itself.
-}
let emccDirParts = FP.splitPath (FP.takeDirectory emccExe)
in pure (concat . reverse . drop 2 . reverse $ emccDirParts)
_ -> notFound
where
notFound = failWith "emscripten emsdk not found (use --with-emsdk)"
-- | configure the locations
configureBootLocations :: BootSettings
-> BootPrograms
-> IO BootLocations
configureBootLocations bs _pgms = do
-- take the location of the ghcjs-boot program and configure the
-- installation directory relative to that
bootPath <- getExecutablePath
ghcjsTopDir <- canonicalizePath (FP.dropFileName bootPath </> "..")
let ghcjsLibDir = ghcjsTopDir </> libSubDir
globalDB = ghcjsLibDir </> "package.conf.d"
-- the source directory is specified by the user
sourceDir <- bootSourceDir bs
buildDir <- prepareBuildDir sourceDir ghcjsLibDir
emsdkDir <- configureEmsdkDir bs
pure $ BootLocations sourceDir
buildDir
ghcjsTopDir
globalDB
emsdkDir
prepareBuildDir :: Prelude.FilePath
-> Prelude.FilePath
-> IO Prelude.FilePath
prepareBuildDir srcDir ghcjsLibDir = do
e <- doesFileExist srcDir
if e then do let bootDir = ghcjsLibDir FP.</> "boot"
bootExists <- doesDirectoryExist bootDir
when bootExists (removeDirectoryRecursive bootDir)
unpackTar False False ghcjsLibDir srcDir
pure bootDir
else do
d <- doesDirectoryExist srcDir
if d then do
e <- doesFileExist (srcDir FP.</> "boot" FP.<.> "yaml")