summaryrefslogtreecommitdiff
path: root/Biz/Bild.hs
blob: d1d3d78a5473dae9a71ceb33c148aeb54a6cbf79 (plain)
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
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | A general purpose build tool.
--
-- : out bild
--
-- == Design constraints
--
--    * only input is one or more a namespaces. no subcommands, no packages
--
--    * no need to write specific build rules, one rule for hs, one for rs, one
--    for scm, and so on
--
--    * no need to distinguish between exe and lib, just have a single output,
--    or figure it out automatically
--
--    * never concerned with deployment/packaging - leave that to another tool
--      (scp? tar?)
--
--    * local dev builds should be preserved, while remote nix builds are used
--    for the final package
--
-- == Features
--
--    * namespace maps to filesystem
--
--        * no need for `bild -l` for listing available targets.
--          Use `ls` or `tree`
--
--        * you build namespaces, not files/modules/packages/etc
--
--    * namespace maps to language modules
--
--        * build settings can be set in the file comments
--
--    * pwd is always considered the the source directory,
--      no `src` vs `doc` etc.
--
--    * build methods automaticatly detected with file extensions
--
--    * flags modify the way to interact with the build, some ideas:
--
--        * -p = turn on profiling
--
--        * -o = optimize level
--
-- == Example Commands
--
-- > bild [opts] <target..>
--
-- The general scheme is to build the things described by the targets. A target
-- is a namespace. You can list as many as you want, but you must list at least
-- one. It could just be `:!bild %` in vim to build whatever you're working on,
-- or `bild **/*` to build everything.
--
-- Build outputs will go into the `_` directory in the root of the project.
--
-- > bild A/B.hs
--
-- This will build the file at ./A/B.hs, which translates to something like
-- `ghc --make A.B`.
--
-- == Build Metadata
--
-- Metadata is set in the comments with a special syntax. For system-level deps,
-- we list the deps in comments in the target file, like:
--
-- > -- : sys cmark
--
-- The name is used to lookup the package in `nixpkgs.pkgs.<name>`.
-- Language-level deps can automatically determined by passing parsed import
-- statements to a package database, eg `ghc-pkg find-module`.
--
-- The output executable is named with:
--
-- > -- : out my-program
--
-- or
--
-- > -- : out my-app.js
--
-- When multiple compilers are possible we use the @out@ extension to determine
-- target platform. If @out@ does not have an extension, each build type falls
-- back to a default, namely an executable binary.
--
-- This method of setting metadata in the module comments works pretty well,
-- and really only needs to be done in the entrypoint module anyway.
--
-- Local module deps are included by just giving the repo root to the underlying
-- compiler for the target, and the compiler does work of walking the source
-- tree.
module Biz.Bild where

import Alpha hiding (sym, (<.>))
import qualified Biz.Cli as Cli
import qualified Biz.Log as Log
import Biz.Namespace (Namespace (..))
import qualified Biz.Namespace as Namespace
import qualified Biz.Test as Test
import qualified Control.Concurrent.Async as Async
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as Char8
import qualified Data.ByteString.Lazy as ByteString
import qualified Data.Char as Char
import Data.Conduit ((.|))
import qualified Data.Conduit as Conduit
import qualified Data.Conduit.List as Conduit
import qualified Data.Conduit.Process as Conduit
import qualified Data.List as List
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import qualified Data.String as String
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import qualified System.Directory as Dir
import qualified System.Environment as Env
import qualified System.Exit as Exit
import System.FilePath (replaceExtension, (</>))
import qualified System.IO as IO
import qualified System.Process as Process
import qualified Text.Regex.Applicative as Regex

main :: IO ()
main = Cli.main <| Cli.Plan help move test_ pure
  where
    test_ =
      Test.group
        "Biz.Bild"
        [ Test.unit "can bild bild" <| do
            analyze "Biz/Bild.hs"
              /> Maybe.fromJust
              +> build False False
              +> \case
                Exit.ExitFailure _ ->
                  Test.assertFailure "can't bild bild"
                _ ->
                  pure ()
        ]

move :: Cli.Arguments -> IO ()
move args =
  IO.hSetBuffering stdout IO.NoBuffering
    >> pure (Cli.getAllArgs args (Cli.argument "target"))
    /> filter (not <. Namespace.isCab)
    +> filterM Dir.doesFileExist
    +> traverse analyze
    /> filter isJust
    /> map Maybe.fromJust
    /> filter (namespace .> isBuildableNs)
    +> printOrBuild
    +> exitSummary
  where
    printOrBuild :: [Target] -> IO [ExitCode]
    printOrBuild targets
      | args `Cli.has` Cli.longOption "json" =
        traverse_ putJSON targets >> pure [Exit.ExitSuccess]
      | otherwise = do
        root <- Env.getEnv "BIZ_ROOT"
        createHier root
        traverse (build isTest isLoud) targets
    isTest = args `Cli.has` Cli.longOption "test"
    isLoud = args `Cli.has` Cli.longOption "loud"
    putJSON = Aeson.encode .> ByteString.toStrict .> Char8.putStrLn

nixStore :: String
nixStore = "/nix/store/00000000000000000000000000000000-"

help :: Cli.Docopt
help =
  [Cli.docopt|
bild

Usage:
  bild test
  bild [options] <target>...

Options:
  --test      Run tests on a target after building
  --loud      Show all output from compiler
  --json      Only analyze and print as JSON, don't build
  -h, --help  Print this info
|]

exitSummary :: [Exit.ExitCode] -> IO ()
exitSummary exits =
  if failures > 0
    then Exit.die <| show failures
    else Exit.exitSuccess
  where
    failures = length <| filter isFailure exits

type Dep = String

type Out = String

data Compiler
  = Copy
  | GhcLib
  | GhcExe
  | Guile
  | NixBuild
  | Rustc
  | Sbcl
  deriving (Eq, Show, Generic, Aeson.ToJSON)

data Target = Target
  { -- | Output name
    out :: Maybe Out,
    -- | Fully qualified namespace partitioned by '.'
    namespace :: Namespace,
    -- | Absolute path to file
    path :: FilePath,
    -- | Language-specific dependencies
    langdeps :: Set Dep,
    -- | System-level dependencies
    sysdeps :: Set Dep,
    -- | Which compiler should we use?
    compiler :: Compiler,
    -- | Where is this machine being built? Schema: user@location
    builder :: Text
  }
  deriving (Show, Generic, Aeson.ToJSON)

-- | We can't build everything yet...
isBuildableNs :: Namespace -> Bool
isBuildableNs (Namespace _ Namespace.Hs) = True
isBuildableNs (Namespace _ Namespace.Lisp) = True
isBuildableNs (Namespace _ Namespace.Scm) = True
isBuildableNs (Namespace _ Namespace.Rs) = True
isBuildableNs ns
  | ns `elem` nixTargets = True
  | otherwise = False
  where
    nixTargets =
      [ Namespace ["Biz", "Pie"] Namespace.Nix,
        Namespace ["Biz", "Que"] Namespace.Nix,
        Namespace ["Biz", "Cloud"] Namespace.Nix,
        Namespace ["Biz", "Dev"] Namespace.Nix,
        Namespace ["Biz", "Dragons", "Analysis"] Namespace.Nix
      ]

-- | Emulate the *nix hierarchy in the cabdir.
bindir, intdir, nixdir, vardir :: String
bindir = "_/bin"
intdir = "_/int"
nixdir = "_/nix"
vardir = "_/var"

createHier :: String -> IO ()
createHier root =
  traverse_
    (Dir.createDirectoryIfMissing True)
    [ root </> bindir,
      root </> intdir,
      root </> nixdir,
      root </> vardir
    ]

-- >>> removeVersion "array-0.5.4.0-DFLKGIjfsadi"
-- "array"
removeVersion :: String -> String
removeVersion = takeWhile (/= '.') .> butlast2
  where
    butlast2 s = take (length s - 2) s

detectImports :: Namespace -> [Text] -> IO (Set Dep)
detectImports (Namespace _ Namespace.Hs) contentLines = do
  let imports =
        contentLines
          /> Text.unpack
          /> Regex.match haskellImports
          |> catMaybes
  pkgs <- foldM ghcPkgFindModule Set.empty imports
  transitivePkgs <-
    imports
      |> map (Namespace.fromHaskellModule .> Namespace.toPath)
      |> traverse Dir.makeAbsolute
      +> filterM Dir.doesFileExist
      +> traverse analyze -- surely this is a bottleneck ripe for caching
      /> catMaybes
      /> map langdeps
      /> mconcat
  pure <| pkgs <> transitivePkgs
detectImports (Namespace _ Namespace.Lisp) contentLines = do
  let requires = contentLines /> Text.unpack /> Regex.match lispRequires |> catMaybes
  pure <| Set.fromList requires
detectImports _ _ = Exit.die "can only detectImports for Haskell"

-- | TODO: globally cache analyses, so I'm not re-analyzing modules all the
-- time. This is important as it would speed up building by a lot.
analyze :: FilePath -> IO (Maybe Target)
analyze path = do
  content <-
    withFile path ReadMode <| \h ->
      IO.hSetEncoding h IO.utf8_bom
        >> Text.IO.hGetContents h
  let contentLines = Text.lines content
  root <- Env.getEnv "BIZ_ROOT"
  absPath <- Dir.makeAbsolute path
  Log.info ["bild", "analyze", str path]
  let ns =
        if "hs" `List.isSuffixOf` path
          then Namespace.fromHaskellContent <| Text.unpack content
          else Namespace.fromPath root absPath
  case ns of
    Nothing ->
      Log.warn ["bild", "analyze", str path, "could not find namespace"]
        >> Log.br
        >> pure Nothing
    Just namespace@(Namespace _ ext) ->
      Just </ do
        user <- Env.getEnv "USER" /> Text.pack
        host <- Text.pack </ fromMaybe "interactive" </ Env.lookupEnv "HOSTNAME"
        case ext of
          Namespace.Hs -> do
            langdeps <- detectImports namespace contentLines
            let out =
                  contentLines
                    /> Text.unpack
                    /> Regex.match (metaOut "--")
                    |> catMaybes
                    |> head
            pure
              Target
                { builder = user <> "@localhost",
                  compiler = detectGhcCompiler out,
                  sysdeps =
                    contentLines
                      /> Text.unpack
                      /> Regex.match (metaSys "--")
                      |> catMaybes
                      |> Set.fromList,
                  ..
                }
          Namespace.Lisp -> do
            langdeps <- detectImports namespace contentLines
            pure
              Target
                { sysdeps = Set.empty,
                  compiler = Sbcl,
                  out =
                    contentLines
                      /> Text.unpack
                      /> Regex.match (metaOut ";;")
                      |> catMaybes
                      |> head,
                  builder = user <> "@localhost",
                  ..
                }
          Namespace.Nix ->
            pure
              Target
                { langdeps = Set.empty,
                  sysdeps = Set.empty,
                  compiler = NixBuild,
                  out = Nothing,
                  builder =
                    if host == "lithium"
                      then mempty
                      else
                        Text.concat
                          [ "ssh://",
                            user,
                            "@dev.simatime.com?ssh-key=/home/",
                            user,
                            "/.ssh/id_rsa"
                          ],
                  ..
                }
          Namespace.Scm -> do
            pure
              Target
                { langdeps = Set.empty,
                  sysdeps = Set.empty,
                  compiler = Guile,
                  out =
                    contentLines
                      /> Text.unpack
                      /> Regex.match (metaOut ";;")
                      |> catMaybes
                      |> head,
                  builder = user <> "@localhost",
                  ..
                }
          Namespace.Rs -> do
            pure
              Target
                { langdeps = Set.empty,
                  sysdeps = Set.empty,
                  compiler = Rustc,
                  out =
                    contentLines
                      /> Text.unpack
                      /> Regex.match (metaOut "//")
                      |> catMaybes
                      |> head,
                  builder = user <> "@localhost",
                  ..
                }
          _ ->
            pure
              Target
                { langdeps = Set.empty,
                  sysdeps = Set.empty,
                  compiler = Copy,
                  out = Nothing,
                  builder = user <> "@localhost",
                  ..
                }

ghcPkgFindModule :: Set String -> String -> IO (Set String)
ghcPkgFindModule acc m = do
  packageDb <- Env.getEnv "GHC_PACKAGE_PATH"
  Process.readProcess
    "ghc-pkg"
    ["--package-db", packageDb, "--names-only", "--simple-output", "find-module", m]
    ""
    /> String.lines
    /> Set.fromList
    /> Set.union acc

-- | Some rules for detecting the how to compile a ghc module. If there is an
-- out, then we know it's some Exe, otherwise it's a Lib.
detectGhcCompiler :: Maybe Out -> Compiler
detectGhcCompiler = \case
  Just _ -> GhcExe
  Nothing -> GhcLib

isFailure :: Exit.ExitCode -> Bool
isFailure (Exit.ExitFailure _) = True
isFailure Exit.ExitSuccess = False

isSuccess :: Exit.ExitCode -> Bool
isSuccess Exit.ExitSuccess = True
isSuccess _ = False

test :: Bool -> Target -> IO Exit.ExitCode
test loud Target {..} = case compiler of
  GhcExe -> do
    root <- Env.getEnv "BIZ_ROOT"
    run
      <| Proc
        { loud = loud,
          cmd = root </> bindir </> Maybe.fromJust out,
          args = ["test"],
          ns = namespace,
          onFailure = Log.fail ["test", nschunk namespace] >> Log.br,
          onSuccess = Log.pass ["test", nschunk namespace] >> Log.br
        }
  _ ->
    Log.warn ["test", nschunk namespace, "unavailable"]
      >> Log.br
      >> pure Exit.ExitSuccess

build :: Bool -> Bool -> Target -> IO Exit.ExitCode
build andTest loud target@Target {..} = do
  root <- Env.getEnv "BIZ_ROOT"
  case compiler of
    GhcExe -> do
      Log.info ["bild", "dev", "ghc-exe", nschunk namespace]
      exitcode <-
        proc
          loud
          namespace
          "ghc"
          [ "-Werror",
            "-i" <> root,
            "-odir",
            root </> intdir,
            "-hidir",
            root </> intdir,
            "--make",
            path,
            "-main-is",
            Namespace.toHaskellModule namespace,
            "-o",
            root </> bindir </> Maybe.fromJust out
          ]
      if andTest && isSuccess exitcode
        then test loud target
        else pure exitcode
    GhcLib -> do
      Log.info ["bild", "dev", "ghc-lib", nschunk namespace]
      proc
        loud
        namespace
        "ghc"
        [ "-Werror",
          "-i" <> root,
          "-odir",
          root </> intdir,
          "-hidir",
          root </> intdir,
          "--make",
          path
        ]
    Guile -> do
      Log.info ["bild", "dev", "guile", nschunk namespace]
      _ <-
        proc
          loud
          namespace
          "guild"
          [ "compile",
            "--r7rs",
            "--load-path=" ++ root,
            "--output=" ++ root </> intdir </> replaceExtension path ".scm.go",
            path
          ]
      when (isJust out) <| do
        let o = Maybe.fromJust out
        writeFile
          (root </> bindir </> o)
          <| Text.pack
          <| joinWith
            "\n"
            [ "#!/usr/bin/env bash",
              "guile -C \""
                <> root </> intdir
                <> "\" -c \"(use-modules "
                <> Namespace.toSchemeModule namespace
                <> ") (main (command-line))\""
                <> " \"$@\""
            ]
        p <- Dir.getPermissions <| root </> bindir </> o
        Dir.setPermissions (root </> bindir </> o) (Dir.setOwnerExecutable True p)
      pure Exit.ExitSuccess
    NixBuild -> do
      Log.info
        [ "bild",
          "nix",
          if Text.null builder
            then "local"
            else builder,
          nschunk namespace
        ]
      proc
        loud
        namespace
        "nix-build"
        [ path,
          "--out-link",
          root </> nixdir </> Namespace.toPath namespace,
          "--builders",
          Text.unpack builder
        ]
    Copy -> do
      Log.warn ["bild", "copy", "TODO", nschunk namespace]
      pure Exit.ExitSuccess
    Rustc -> do
      Log.info ["bild", "dev", "rust", nschunk namespace]
      proc
        loud
        namespace
        "rustc"
        [ path,
          "-o",
          root </> bindir </> Maybe.fromJust out
        ]
    Sbcl -> do
      Log.info ["bild", "dev", "lisp", nschunk namespace]
      proc
        loud
        namespace
        "sbcl"
        [ "--load",
          path,
          "--eval",
          "(require :asdf)",
          "--eval",
          "(sb-ext:save-lisp-and-die #p\"" <> (root </> bindir </> Maybe.fromJust out) <> "\" :toplevel #'main :executable t)"
        ]

data Proc = Proc
  { loud :: Bool,
    cmd :: String,
    args :: [String],
    ns :: Namespace,
    onFailure :: IO (),
    onSuccess :: IO ()
  }

-- | Run a subprocess, streaming output if --loud is set.
run :: Proc -> IO Exit.ExitCode
run Proc {..} = do
  (Conduit.Inherited, stdout_, stderr_, cph) <- Conduit.streamingProcess <| Conduit.proc cmd args
  exitcode <-
    if loud
      then
        Async.runConcurrently
          <| Async.Concurrently (puts stdout_)
          *> (Async.Concurrently <| Conduit.waitForStreamingProcess cph)
      else
        Async.runConcurrently
          <| Async.Concurrently
          <| Conduit.waitForStreamingProcess cph
  if isFailure exitcode
    then puts stderr_ >> onFailure >> pure exitcode
    else onSuccess >> pure exitcode

-- | Helper for running a standard bild subprocess.
proc :: Bool -> Namespace -> String -> [String] -> IO Exit.ExitCode
proc loud namespace cmd args =
  run
    <| Proc
      { loud = loud,
        ns = namespace,
        cmd = cmd,
        args = args,
        onFailure = Log.fail ["bild", nschunk namespace] >> Log.br,
        onSuccess = Log.good ["bild", nschunk namespace] >> Log.br
      }

-- | Helper for printing during a subprocess
puts :: Conduit.ConduitM () ByteString IO () -> IO ()
puts thing = Conduit.runConduit <| thing .| Conduit.mapM_ putStr

nschunk :: Namespace -> Text
nschunk = Namespace.toPath .> Text.pack

metaDep :: Regex.RE Char Dep
metaDep = Regex.string "-- : dep " *> Regex.many (Regex.psym Char.isAlpha)

metaSys :: [Char] -> Regex.RE Char Dep
metaSys comment = Regex.string (comment ++ " : sys ") *> Regex.many (Regex.psym Char.isAlpha)

metaOut :: [Char] -> Regex.RE Char Out
metaOut comment = Regex.string (comment ++ " : out ") *> Regex.many (Regex.psym (/= ' '))

haskellImports :: Regex.RE Char String
haskellImports =
  Regex.string "import"
    *> Regex.some (Regex.psym Char.isSpace)
    *> Regex.many (Regex.psym Char.isLower)
    *> Regex.many (Regex.psym Char.isSpace)
    *> Regex.some (Regex.psym isModuleChar)
    <* Regex.many Regex.anySym

isModuleChar :: Char -> Bool
isModuleChar c =
  elem c <| concat [['A' .. 'Z'], ['a' .. 'z'], ['.', '_'], ['0' .. '9']]

-- Matches on `(require :package)` forms and returns `package`. The `require`
-- function is technically deprecated in Common Lisp, but no new spec has been
-- published with a replacement, and I don't wanna use asdf, so this is what we
-- use for Lisp imports.
lispRequires :: Regex.RE Char String
lispRequires =
  Regex.string "(require"
    *> Regex.some (Regex.psym Char.isSpace)
    *> Regex.many (Regex.psym isQuote)
    *> Regex.many (Regex.psym isModuleChar)
    <* Regex.many (Regex.psym (== ')'))
  where
    isQuote :: Char -> Bool
    isQuote c = c `elem` ['\'', ':']