-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completes the prototype implementation of Bazel persistent worker mod…
…e support in rules_haskell Started in 132d0af. This commit imports worker's code and sets everything to make the persistent worker mode available to a regular `rules_haskell` user. The worker code lives in `tools/worker`. The mode is supported in ghc_nixpkgs-toolchain only, at this point.
- Loading branch information
1 parent
21c277b
commit ee5eafe
Showing
20 changed files
with
953 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Workspace rules (tools/repositories)""" | ||
|
||
load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot") | ||
|
||
def rules_haskell_tools_dependencies(): | ||
"""Provide all repositories that are necessary for `rules_haskell`'s tools to | ||
function. | ||
""" | ||
excludes = native.existing_rules().keys() | ||
|
||
if "rules_haskell_worker_dependencies" not in excludes: | ||
stack_snapshot( | ||
name = "rules_haskell_worker_dependencies", | ||
packages = [ | ||
"base", | ||
"bytestring", | ||
"filepath", | ||
"ghc", | ||
"ghc-paths", | ||
"microlens", | ||
"process", | ||
"proto-lens", | ||
"proto-lens-runtime", | ||
"text", | ||
"vector", | ||
], | ||
snapshot = "lts-14.1", | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
load("@rules_haskell//haskell:cabal.bzl", "haskell_cabal_binary") | ||
|
||
haskell_cabal_binary( | ||
name = "bin", | ||
srcs = glob(["**"]), | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@rules_haskell_worker_dependencies//:base", | ||
"@rules_haskell_worker_dependencies//:bytestring", | ||
"@rules_haskell_worker_dependencies//:filepath", | ||
"@rules_haskell_worker_dependencies//:ghc", | ||
"@rules_haskell_worker_dependencies//:ghc-paths", | ||
"@rules_haskell_worker_dependencies//:microlens", | ||
"@rules_haskell_worker_dependencies//:process", | ||
"@rules_haskell_worker_dependencies//:proto-lens", | ||
"@rules_haskell_worker_dependencies//:proto-lens-runtime", | ||
"@rules_haskell_worker_dependencies//:text", | ||
"@rules_haskell_worker_dependencies//:vector", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE NondecreasingIndentation #-} | ||
|
||
module Compile (compile) where | ||
|
||
import Control.Monad | ||
import Control.Monad.IO.Class (liftIO) | ||
import Data.List | ||
import System.FilePath | ||
import System.Exit | ||
|
||
import GHC | ||
import GHC.Paths ( libdir ) | ||
import DynFlags ( defaultFatalMessager, defaultFlushOut, Option(..) ) | ||
import DriverPhases | ||
import DriverPipeline ( compileFile, oneShot ) | ||
import Util | ||
|
||
compile :: [String] -> IO () | ||
compile flags = | ||
defaultErrorHandler defaultFatalMessager defaultFlushOut $ do | ||
runGhc (Just libdir) $ do | ||
|
||
-- Parse flags | ||
dflags <- getSessionDynFlags | ||
(dflags2, fileish_args, _warns) <- | ||
parseDynamicFlags dflags (map noLoc flags) | ||
|
||
-- Normilize paths | ||
let | ||
normalise_hyp fp | ||
| strt_dot_sl && "-" `isPrefixOf` nfp = cur_dir ++ nfp | ||
| otherwise = nfp | ||
where | ||
-- # if defined(mingw32_HOST_OS) -- TODO: do we need Win support at this point? | ||
-- strt_dot_sl = "./" `isPrefixOf` fp || ".\\" `isPrefixOf` fp | ||
-- # else | ||
strt_dot_sl = "./" `isPrefixOf` fp | ||
-- # endif | ||
cur_dir = '.' : [pathSeparator] | ||
nfp = normalise fp | ||
normal_fileish_paths = map (normalise_hyp . unLoc) fileish_args | ||
(srcs, objs) = partition_args normal_fileish_paths [] [] | ||
|
||
-- Update flags with normalized | ||
dflags3 = dflags2 { ldInputs = map (FileOption "") objs | ||
++ ldInputs dflags2 } | ||
|
||
_ <- setSessionDynFlags dflags3 | ||
|
||
doMake srcs | ||
|
||
doMake :: [(String,Maybe Phase)] -> Ghc () | ||
doMake srcs = do | ||
let (hs_srcs, non_hs_srcs) = partition isHaskellishTarget srcs | ||
|
||
hsc_env <- GHC.getSession | ||
|
||
-- if we have no haskell sources from which to do a dependency | ||
-- analysis, then just do one-shot compilation and/or linking. | ||
-- This means that "ghc Foo.o Bar.o -o baz" links the program as | ||
-- we expect. | ||
if (null hs_srcs) | ||
then liftIO (oneShot hsc_env StopLn srcs) | ||
else do | ||
|
||
o_files <- mapM (\x -> liftIO $ compileFile hsc_env StopLn x) | ||
non_hs_srcs | ||
dflags <- GHC.getSessionDynFlags | ||
let dflags' = dflags { ldInputs = map (FileOption "") o_files | ||
++ ldInputs dflags } | ||
_ <- GHC.setSessionDynFlags dflags' | ||
|
||
targets <- mapM (uncurry GHC.guessTarget) hs_srcs | ||
GHC.setTargets targets | ||
ok_flag <- GHC.load LoadAllTargets | ||
|
||
when (failed ok_flag) (liftIO $ exitWith (ExitFailure 1)) | ||
return () | ||
|
||
partition_args :: [String] -> [(String, Maybe Phase)] -> [String] | ||
-> ([(String, Maybe Phase)], [String]) | ||
partition_args [] srcs objs = (reverse srcs, reverse objs) | ||
partition_args ("-x":suff:args) srcs objs | ||
| "none" <- suff = partition_args args srcs objs | ||
| StopLn <- phase = partition_args args srcs (slurp ++ objs) | ||
| otherwise = partition_args rest (these_srcs ++ srcs) objs | ||
where phase = startPhase suff | ||
(slurp,rest) = break (== "-x") args | ||
these_srcs = zip slurp (repeat (Just phase)) | ||
partition_args (arg:args) srcs objs | ||
| looks_like_an_input arg = partition_args args ((arg,Nothing):srcs) objs | ||
| otherwise = partition_args args srcs (arg:objs) | ||
|
||
looks_like_an_input :: String -> Bool | ||
looks_like_an_input m = isSourceFilename m | ||
|| looksLikeModuleName m | ||
|| "-" `isPrefixOf` m | ||
|| not (hasExtension m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Main where | ||
|
||
import Server (server) | ||
|
||
import System.Environment (getArgs) | ||
import System.Exit (exitFailure) | ||
import System.IO | ||
|
||
--import GHC.IO.Handle (hDuplicate, hDuplicateTo) | ||
|
||
pwFlag :: String | ||
pwFlag = "--persistent_worker" | ||
|
||
main :: IO () | ||
main = do | ||
|
||
-- redirect stdout to stderr | ||
-- stdout_dup <- hDuplicate stdout | ||
-- hDuplicateTo stderr stdout | ||
let stdout_dup = stdout | ||
|
||
args <- getArgs | ||
hPutStrLn stderr $ "Args taken: " ++ show args | ||
if pwFlag `elem` args | ||
then server stdin stdout_dup $ filter (/= pwFlag) args | ||
else | ||
print "Worker should be called with --persistent_worker" | ||
>> exitFailure |
Oops, something went wrong.