The GHC API, decoupled from GHC versions


Keywords
development, library, Propose Tags, Index, Quick Jump, GHC, GHC.Builtin.Names.TH, GHC.Builtin.PrimOps.Casts, GHC.Builtin.Types.Literals, GHC.Builtin.Utils, GHC.ByteCode.Asm, GHC.ByteCode.InfoTable, GHC.ByteCode.Instr, GHC.ByteCode.Linker, GHC.Cmm.CallConv, GHC.Cmm.CommonBlockElim, GHC.Cmm.Config, GHC.Cmm.ContFlowOpt, GHC.Cmm.Dataflow, GHC.Cmm.DebugBlock, GHC.Cmm.Dominators, GHC.Cmm.Graph, GHC.Cmm.Info, GHC.Cmm.Info.Build, GHC.Cmm.InitFini, GHC.Cmm.LRegSet, GHC.Cmm.LayoutStack, GHC.Cmm.Lexer, GHC.Cmm.Lint, GHC.Cmm.Liveness, GHC.Cmm.Opt, GHC.Cmm.Parser, GHC.Cmm.Parser.Config, GHC.Cmm.Parser.Monad, GHC.Cmm.Pipeline, GHC.Cmm.ProcPoint, GHC.Cmm.Reducibility, GHC.Cmm.Sink, GHC.Cmm.Switch.Implement, GHC.Cmm.ThreadSanitizer, GHC.Cmm.Utils, GHC.CmmToAsm, GHC.CmmToAsm.AArch64, GHC.CmmToAsm.AArch64.CodeGen, GHC.CmmToAsm.AArch64.Cond, GHC.CmmToAsm.AArch64.Instr, GHC.CmmToAsm.AArch64.Ppr, GHC.CmmToAsm.AArch64.RegInfo, GHC.CmmToAsm.AArch64.Regs, GHC.CmmToAsm.BlockLayout, GHC.CmmToAsm.CFG, GHC.CmmToAsm.CFG.Dominators, GHC.CmmToAsm.CPrim, GHC.CmmToAsm.Config, GHC.CmmToAsm.Dwarf, GHC.CmmToAsm.Dwarf.Constants, GHC.CmmToAsm.Dwarf.Types, GHC.CmmToAsm.Format, GHC.CmmToAsm.Instr, GHC.CmmToAsm.Monad, GHC.CmmToAsm.PIC, GHC.CmmToAsm.PPC, GHC.CmmToAsm.PPC.CodeGen, GHC.CmmToAsm.PPC.Cond, GHC.CmmToAsm.PPC.Instr, GHC.CmmToAsm.PPC.Ppr, GHC.CmmToAsm.PPC.RegInfo, GHC.CmmToAsm.PPC.Regs, GHC.CmmToAsm.Ppr, GHC.CmmToAsm.Reg.Graph, GHC.CmmToAsm.Reg.Graph.Spill, GHC.CmmToAsm.Reg.Graph.SpillClean, GHC.CmmToAsm.Reg.Graph.SpillCost, GHC.CmmToAsm.Reg.Graph.Stats, GHC.CmmToAsm.Reg.Graph.TrivColorable, GHC.CmmToAsm.Reg.Linear, GHC.CmmToAsm.Reg.Linear.AArch64, GHC.CmmToAsm.Reg.Linear.Base, GHC.CmmToAsm.Reg.Linear.FreeRegs, GHC.CmmToAsm.Reg.Linear.JoinToTargets, GHC.CmmToAsm.Reg.Linear.PPC, GHC.CmmToAsm.Reg.Linear.StackMap, GHC.CmmToAsm.Reg.Linear.State, GHC.CmmToAsm.Reg.Linear.Stats, GHC.CmmToAsm.Reg.Linear.X86, GHC.CmmToAsm.Reg.Linear.X86_64, GHC.CmmToAsm.Reg.Liveness, GHC.CmmToAsm.Reg.Target, GHC.CmmToAsm.Reg.Utils, GHC.CmmToAsm.Types, GHC.CmmToAsm.Utils, GHC.CmmToAsm.Wasm, GHC.CmmToAsm.Wasm.Asm, GHC.CmmToAsm.Wasm.FromCmm, GHC.CmmToAsm.Wasm.Types, GHC.CmmToAsm.Wasm.Utils, GHC.CmmToAsm.X86, GHC.CmmToAsm.X86.CodeGen, GHC.CmmToAsm.X86.Cond, GHC.CmmToAsm.X86.Instr, GHC.CmmToAsm.X86.Ppr, GHC.CmmToAsm.X86.RegInfo, GHC.CmmToAsm.X86.Regs, GHC.CmmToC, GHC.CmmToLlvm, GHC.CmmToLlvm.Base, GHC.CmmToLlvm.CodeGen, GHC.CmmToLlvm.Data, GHC.CmmToLlvm.Mangler, GHC.CmmToLlvm.Ppr, GHC.CmmToLlvm.Regs, GHC.Core.LateCC, GHC.Core.Opt.CSE, GHC.Core.Opt.CallArity, GHC.Core.Opt.CprAnal, GHC.Core.Opt.DmdAnal, GHC.Core.Opt.Exitify, GHC.Core.Opt.FloatIn, GHC.Core.Opt.FloatOut, GHC.Core.Opt.LiberateCase, GHC.Core.Opt.Pipeline, GHC.Core.Opt.SetLevels, GHC.Core.Opt.SpecConstr, GHC.Core.Opt.Specialise, GHC.Core.Opt.StaticArgs, GHC.Core.Opt.WorkWrap, GHC.Core.Opt.WorkWrap.Utils, GHC.Core.TyCon.Set, GHC.CoreToStg, GHC.CoreToStg.Prep, GHC.Data.Bitmap, GHC.Data.Graph.Base, GHC.Data.Graph.Collapse, GHC.Data.Graph.Color, GHC.Data.Graph.Inductive.Graph, GHC.Data.Graph.Inductive.PatriciaTree, GHC.Data.Graph.Ops, GHC.Data.Graph.Ppr, GHC.Data.UnionFind, GHC.Driver.CodeOutput, GHC.Driver.Config.Cmm, GHC.Driver.Config.Cmm.Parser, GHC.Driver.Config.CmmToAsm, GHC.Driver.Config.CmmToLlvm, GHC.Driver.Config.Core.Lint.Interactive, GHC.Driver.Config.Core.Opt.Arity, GHC.Driver.Config.Core.Opt.LiberateCase, GHC.Driver.Config.Core.Opt.Simplify, GHC.Driver.Config.Core.Opt.WorkWrap, GHC.Driver.Config.Core.Rules, GHC.Driver.Config.CoreToStg, GHC.Driver.Config.CoreToStg.Prep, GHC.Driver.Config.Finder, GHC.Driver.Config.HsToCore, GHC.Driver.Config.HsToCore.Ticks, GHC.Driver.Config.HsToCore.Usage, GHC.Driver.Config.Linker, GHC.Driver.Config.Stg.Debug, GHC.Driver.Config.Stg.Lift, GHC.Driver.Config.Stg.Pipeline, GHC.Driver.Config.Stg.Ppr, GHC.Driver.Config.StgToCmm, GHC.Driver.Config.StgToJS, GHC.Driver.Config.Tidy, GHC.Driver.GenerateCgIPEStub, GHC.Driver.Main, GHC.Driver.Make, GHC.Driver.MakeSem, GHC.Driver.Pipeline, GHC.Driver.Pipeline.Execute, GHC.Driver.Pipeline.LogQueue, GHC.Hs.Stats, GHC.Hs.Syn.Type, GHC.HsToCore, GHC.HsToCore.Arrows, GHC.HsToCore.Binds, GHC.HsToCore.Breakpoints, GHC.HsToCore.Coverage, GHC.HsToCore.Docs, GHC.HsToCore.Expr, GHC.HsToCore.Foreign.C, GHC.HsToCore.Foreign.Call, GHC.HsToCore.Foreign.Decl, GHC.HsToCore.Foreign.JavaScript, GHC.HsToCore.Foreign.Prim, GHC.HsToCore.Foreign.Utils, GHC.HsToCore.GuardedRHSs, GHC.HsToCore.ListComp, GHC.HsToCore.Match, GHC.HsToCore.Match.Constructor, GHC.HsToCore.Match.Literal, GHC.HsToCore.Monad, GHC.HsToCore.Pmc, GHC.HsToCore.Pmc.Check, GHC.HsToCore.Pmc.Desugar, GHC.HsToCore.Pmc.Solver, GHC.HsToCore.Pmc.Utils, GHC.HsToCore.Quote, GHC.HsToCore.Ticks, GHC.HsToCore.Types, GHC.HsToCore.Usage, GHC.HsToCore.Utils, GHC.Iface.Binary, GHC.Iface.Env, GHC.Iface.Errors, GHC.Iface.Ext.Ast, GHC.Iface.Ext.Binary, GHC.Iface.Ext.Debug, GHC.Iface.Ext.Types, GHC.Iface.Ext.Utils, GHC.Iface.Load, GHC.Iface.Make, GHC.Iface.Recomp, GHC.Iface.Recomp.Flags, GHC.Iface.Rename, GHC.Iface.Tidy, GHC.Iface.Tidy.StaticPtrTable, GHC.IfaceToCore, GHC.JS.Optimizer, GHC.Linker.Config, GHC.Linker.Deps, GHC.Linker.Dynamic, GHC.Linker.ExtraObj, GHC.Linker.Loader, GHC.Linker.MacOS, GHC.Linker.Static, GHC.Linker.Unit, GHC.Linker.Windows, GHC.Llvm, GHC.Llvm.MetaData, GHC.Llvm.Ppr, GHC.Llvm.Syntax, GHC.Llvm.Types, GHC.Parser.Utils, GHC.Platform.Host, GHC.Plugins, GHC.Rename.Bind, GHC.Rename.Doc, GHC.Rename.Env, GHC.Rename.Expr, GHC.Rename.Fixity, GHC.Rename.HsType, GHC.Rename.Module, GHC.Rename.Names, GHC.Rename.Pat, GHC.Rename.Splice, GHC.Rename.Unbound, GHC.Rename.Utils, GHC.Runtime.Eval, GHC.Runtime.Heap.Inspect, GHC.Runtime.Interpreter, GHC.Runtime.Interpreter.JS, GHC.Runtime.Interpreter.Process, GHC.Runtime.Loader, GHC.Runtime.Utils, GHC.Settings.IO, GHC.Stg.BcPrep, GHC.Stg.CSE, GHC.Stg.Debug, GHC.Stg.FVs, GHC.Stg.InferTags, GHC.Stg.InferTags.Rewrite, GHC.Stg.InferTags.Types, GHC.Stg.Lift, GHC.Stg.Lift.Analysis, GHC.Stg.Lift.Config, GHC.Stg.Lift.Monad, GHC.Stg.Lint, GHC.Stg.Pipeline, GHC.Stg.Stats, GHC.Stg.Subst, GHC.Stg.Unarise, GHC.Stg.Utils, GHC.StgToByteCode, GHC.StgToCmm, GHC.StgToCmm.ArgRep, GHC.StgToCmm.Bind, GHC.StgToCmm.CgUtils, GHC.StgToCmm.Closure, GHC.StgToCmm.DataCon, GHC.StgToCmm.Env, GHC.StgToCmm.Expr, GHC.StgToCmm.ExtCode, GHC.StgToCmm.Foreign, GHC.StgToCmm.Heap, GHC.StgToCmm.Hpc, GHC.StgToCmm.InfoTableProv, GHC.StgToCmm.Layout, GHC.StgToCmm.Lit, GHC.StgToCmm.Monad, GHC.StgToCmm.Prim, GHC.StgToCmm.Prof, GHC.StgToCmm.Sequel, GHC.StgToCmm.TagCheck, GHC.StgToCmm.Ticky, GHC.StgToCmm.Utils, GHC.StgToJS, GHC.StgToJS.Apply, GHC.StgToJS.Arg, GHC.StgToJS.Closure, GHC.StgToJS.CodeGen, GHC.StgToJS.DataCon, GHC.StgToJS.Deps, GHC.StgToJS.Expr, GHC.StgToJS.ExprCtx, GHC.StgToJS.FFI, GHC.StgToJS.Heap, GHC.StgToJS.Ids, GHC.StgToJS.Linker.Linker, GHC.StgToJS.Linker.Opt, GHC.StgToJS.Linker.Utils, GHC.StgToJS.Literal, GHC.StgToJS.Monad, GHC.StgToJS.Prim, GHC.StgToJS.Profiling, GHC.StgToJS.Regs, GHC.StgToJS.Rts.Rts, GHC.StgToJS.Rts.Types, GHC.StgToJS.Sinker, GHC.StgToJS.Stack, GHC.StgToJS.StaticPtr, GHC.StgToJS.Symbols, GHC.StgToJS.Utils, GHC.SysTools, GHC.SysTools.Ar, GHC.SysTools.Cpp, GHC.SysTools.Elf, GHC.SysTools.Info, GHC.SysTools.Process, GHC.SysTools.Tasks, GHC.Tc.Deriv, GHC.Tc.Deriv.Functor, GHC.Tc.Deriv.Generate, GHC.Tc.Deriv.Generics, GHC.Tc.Deriv.Infer, GHC.Tc.Deriv.Utils, GHC.Tc.Errors, GHC.Tc.Errors.Hole, GHC.Tc.Gen.Annotation, GHC.Tc.Gen.App, GHC.Tc.Gen.Arrow, GHC.Tc.Gen.Bind, GHC.Tc.Gen.Default, GHC.Tc.Gen.Export, GHC.Tc.Gen.Expr, GHC.Tc.Gen.Foreign, GHC.Tc.Gen.Head, GHC.Tc.Gen.HsType, GHC.Tc.Gen.Match, GHC.Tc.Gen.Pat, GHC.Tc.Gen.Rule, GHC.Tc.Gen.Sig, GHC.Tc.Gen.Splice, GHC.Tc.Instance.Class, GHC.Tc.Instance.Family, GHC.Tc.Instance.FunDeps, GHC.Tc.Instance.Typeable, GHC.Tc.Module, GHC.Tc.Solver, GHC.Tc.Solver.Dict, GHC.Tc.Solver.Equality, GHC.Tc.Solver.Irred, GHC.Tc.Solver.Monad, GHC.Tc.Solver.Rewrite, GHC.Tc.Solver.Solve, GHC.Tc.TyCl, GHC.Tc.TyCl.Build, GHC.Tc.TyCl.Class, GHC.Tc.TyCl.Instance, GHC.Tc.TyCl.PatSyn, GHC.Tc.TyCl.Utils, GHC.Tc.Types.EvTerm, GHC.Tc.Utils.Backpack, GHC.Tc.Utils.Concrete, GHC.Tc.Utils.Env, GHC.Tc.Utils.Instantiate, GHC.Tc.Utils.Monad, GHC.Tc.Utils.TcMType, GHC.Tc.Utils.Unify, GHC.Tc.Validity, GHC.Tc.Zonk.Env, GHC.Tc.Zonk.TcType, GHC.Tc.Zonk.Type, GHC.ThToHs, GHC.Types.Name.Shape, GHC.Types.Unique.MemoFun, GHC.Unit.Finder, GHC.Utils.Asm, GHC.Utils.Monad.Codensity, GHC.Wasm.ControlFlow, GHC.Wasm.ControlFlow.FromCmm, GHCi.BinaryArray, GHCi.ResolvedBCO, Paths_ghc_lib, More info, ghc-lib-9.8.2.20240223.tar.gz, browse, Package description, Package maintainers, cocreature, shayne_fletcher, edit package information , 8.8.1.20191204, ghc-api
License
BSD-3-Clause
Install
cabal install ghc-lib-9.6.5.20240423

Documentation

ghc-lib Hackage version Stackage version Build Status

Copyright © 2019-2023, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. SPDX-License-Identifier: (Apache-2.0 OR BSD-3-Clause)

The GHC API allows you to use the GHC compiler as a library, so you can parse, analyze and compile Haskell code. The GHC API comes preinstalled with GHC, and is tied to that GHC version - if you are using GHC 8.6.3, you get version 8.6.3 of the API, and can't change it. The ghc-lib project solves that problem, letting you mix and match versions of the GHC compiler and GHC API. Why might you want that?

  • Imagine you are writing a tool to work with several versions of the GHC compiler. The GHC API changes significantly between each version, so doing this would require writing a lot of C preprocessor code to support it. An alternative is to use one version of ghc-lib which works across multiple versions of GHC.

More precisely, building ghc-lib is the same as bootstrapping GHC. For a given ghc-lib 'flavor' (like --ghc-flavor=ghc-9.6.1), a build GHC can be at most "two versions behind & not newer" (e.g. --ghc-flavor=ghc-9.6.1[ghc-9.2.1, ghc-9.6.2)1. ghc-lib cabal files will not produce build plans for configurations outside of these bounds. Clients assume the same build constraints through transitivity e.g. HLint.

  • Imagine you are modifying the GHC API or want features from GHC HEAD. With ghc-lib you can depend on the revised GHC API, without upgrading the compiler used to build everything, speeding up iteration.

The ghc-lib project provides two packages : ghc-lib-parser and ghc-lib. The ghc-lib-parser package is that subset of the GHC API that is just enough to parse Haskell code. The ghc-lib package extends (and re-exports) ghc-lib-parser with the rest. While ghc-lib provides the full GHC API, it doesn't contain a runtime system, nor does it create a package database. That means you can't run code produced by ghc-lib (no runtime), and compiling off-the-shelf code is very hard (no package database containing the base library). What you can do:

There are some downsides to ghc-lib:

  • The lack of runtime means you can't run code, which includes running code at compile time, e.g. TemplateHaskell.
  • While ghc-lib isn't tied to any specific GHC versions, it can only read package databases and .hi files for one particular version of GHC. That means your existing package database probably can't be consumed by ghc-lib (unless you happen to perfectly match the GHC version, in which case you could just have used the GHC API), and it doesn't ship with a package database so you'd have to painfully build your own.
  • Compilation times for the ghc-lib packages are not small, taking approximately 5 minutes for each on our CI machines.

Using ghc-lib

The packages ghc-lib-parser and ghc-lib are available on Hackage, and can be used like any normal packages, e.g. cabal install ghc-lib. Since ghc-lib-parser and ghc-lib conflict perfectly with the GHC API and template-haskell, the packages are not exposed by default so if you use GHC directly, you need to pass -package ghc-lib, the GHC user guide has more information : use the language extension PackageImports to do import "ghc-lib" ... or import "ghc-lib-parser" ... as approriate. There are two release streams within the ghc-lib name:

  • Version 8.8.1 will be the version of ghc-lib produced against the released GHC 8.8.1, once it comes out;
  • Version 0.20190204 is the version of ghc-lib using GHC HEAD on the date 2019-02-04.

The Hackage packages are licensed under the BSD-3-Clause license, just like GHC itself. This repo, including the examples and the script that generates ghc-lib, are licensed under the BSD-3-Clause OR Apache-2.0 license.

Creating ghc-lib

We create the packages by taking a checkout of GHC, and combining the ghc package with the various dependencies it is tightly tied to (e.g. template-haskell) in two new cabal files ghc-lib-parser.cabal and ghc-lib.cabal. These new packages depend on a few generated outputs (which we build using the GHC build system) and some Cmm files. The ghc-lib-gen directory contains a script that puts all the pieces together. Because GHC itself is capable of being bootstrapped with older GHC versions (> 8.4.4) (its Stage0 build), the generated ghc-lib also compiles with multiple GHC versions.

To build ghc-lib-parser and ghc-lib you need clones of this repository and the GHC repository.

Warning : ghc-lib-parser and ghc-lib are known to work on all of MacOS, Linux and Windows. Distributions produced with cabal sdist on Linux/MacOS build on Windows, but a cabal sdist produced on Windows does not build on MacOS/Linux.

Building ghc-lib

By far the easist way to produce ghc-lib-parser and ghc-lib packages is to execute the CI script which incidentally builds and executes the examples (this procedure makes versioned packages based on the current date and expresses the version constraint between ghc-lib and ghc-lib-parser accordingly).

# Setup
git clone git@github.com:digital-asset/ghc-lib.git
cd ghc-lib
stack runhaskell --package extra --package optparse-applicative CI.hs -- --ghc-flavor=ghc-8.8.1

Releasing ghc-lib (notes for maintainers)

Build ghc-lib using the above instructions and upload the resulting .tar.gz files to Hackage.

FAQ

How do I use the ghc-lib/ghc-lib-parser version macros?

Read the Standard CPP macros section of the GHC users guide for the semantics of the MIN_VERSION_pkgname(x, y, z) macros and keep in mind that builds of ghc-lib-parser/ghc-lib from GHC head are ascribed version numbers of the form 0.α.

Building ghc-lib for DAML

The CI.hs script has special support for building custom versions of ghc-lib specifically tailored to the DAML compiler, which requires a handful of patches to be applied on top of GHC. The syntax is slightly different from the general case: the --ghc-flavor flag is replaced with an "enabling" flag --da and three more specific flags. A full call example would be:

stack runhaskell --package extra \
                 --package optparse-applicative \
                 CI.hs -- --da \
                          --merge-base-sha=ghc-8.8.1-release \
                          --patch=upstream/da-master-8.8.1 \
                          --patch=upstream/da-unit-ids-8.8.1 \
                          --gen-flavor=da-ghc-8.8.1 \
                          --upstream=https://github.com/digital-asset/ghc.git

The DAML-specific process only differs from the normal one in that it patches GHC with the given patches. More specifically, it will:

  • Clone GHC. (This is also done by the normal workflow.)
  • Add the DA fork of GHC as a remote named upstream; this can be overridden with the --upstream flag. For example, in local development, --upstream=$PWD/../ghc-fork/. Note that if you want to specify a local path (as in this example), it should be an absolute one, as the command will be run from the folder into which GHC has been cloned.
  • Checkout the commit provided as merge-base-sha.
  • Create a new commit by merging in all of the commits specified through the --patch flags.
  • Proceed as normal for the rest of the workflow.

At some later stage, the workflow calls out to the ghc-lib-gen program, and at that point it needs to pass in a "flavor" argument; it will use the value of the --gen-flavor option for that.

Note that deployment for the DAML version is handled from within the DAML CI.

Footnotes

  1. The set ghc-9.2.1, ghc-9.2.2, ..., ghc-9.4.1, ghc-9.4.2, ..., ghc-9.6.1