Skip to content

Instantly share code, notes, and snippets.

@acrylic-origami
Created January 5, 2020 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrylic-origami/a3ad7b89419c4a5284e3232be1f4b436 to your computer and use it in GitHub Desktop.
Save acrylic-origami/a3ad7b89419c4a5284e3232be1f4b436 to your computer and use it in GitHub Desktop.
Minimal example of frontend plugin vs. regular GHC API usage
-- In subdirectory `target/A.hs`
module A where
#!/usr/bin/env bash
cabal build
# compare:
cabal run sandbox
# with:
ghc --frontend Plugin -itarget -package-db dist-newstyle/packagedb/ghc-8.6.5 Plugin -plugin-package sandbox -hide-all-packages
module Main where
import GHC
import GhcPlugins
import DynFlags
import GHC.Paths ( libdir )
main :: IO ()
main = runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags $ dflags {
importPaths = "target":(importPaths dflags)
}
target <- guessTarget "A" Nothing
setTargets [target]
() <$ load LoadAllTargets
module Plugin where
import GHC
import GhcPlugins
import DynFlags
frontendPlugin :: FrontendPlugin
frontendPlugin = defaultFrontendPlugin {
frontend = fe
}
fe :: [String] -> [(String, Maybe Phase)] -> Ghc ()
fe _ _ = do
target <- guessTarget "A" Nothing
setTargets [target]
() <$ load LoadAllTargets
cabal-version: >=1.10
name: sandbox
version: 0.1.0.0
license-file: LICENSE
author: Derek Lam
maintainer: derek@lam.io
build-type: Simple
extra-source-files: CHANGELOG.md
library
exposed-modules: Plugin
build-depends: base >=4.12 && <4.13, ghc
default-language: Haskell2010
executable sandbox
main-is: Main.hs
build-depends: base >=4.12 && <4.13, ghc-paths, ghc
default-language: Haskell2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment