Skip to content

Commit 2ebcbe2

Browse files
authored
Support for alternative GHC backends: extra-compilation-artifacts directory installation (#8662)
* Cbits archive for c, cxx, cmm, asm sources Build and install extra archive file for non-Haskell code. It allows to build alternative backends and compilation pipelines for GHC. GHC plugins can observe and process Haskell related code, while the 'cbits' archive supply the foreign code parts. * Install extra module related (modpak) files when available Support '.modpak' file installation. Modpak is an optional file, it is generated by GHC plugins (or other tools) for Haskell modules. The modpak file format is not set, it could be a zip file or other format. In modpak files GHC plugins can store additional data for Haskell modules. With modpak files Cabal can support alternative GHC backends out of the box. * add changelog entry for cbits and modpak feature * Support for extra compilation artifacts GHC plugins can store custom data in the 'extra-compilation-artifacts' directory which gets installed with the package. This is a generalization of the previous design. See the discussion in PR #8662. * documentation for extra-compilation-artifacts installation
1 parent 6c68d87 commit 2ebcbe2

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

Cabal/src/Distribution/Simple/GHC.hs

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ import System.Directory
118118
( doesFileExist, doesDirectoryExist
119119
, getAppUserDataDirectory, createDirectoryIfMissing
120120
, canonicalizePath, removeFile, renameFile, getDirectoryContents
121-
, makeRelativeToCurrentDirectory )
121+
, makeRelativeToCurrentDirectory, doesDirectoryExist )
122122
import System.FilePath ( (</>), (<.>), takeExtension
123123
, takeDirectory, replaceExtension
124124
,isRelative )
@@ -2005,6 +2005,9 @@ installLib verbosity lbi targetDir dynlibTargetDir _builtDir pkg lib clbi = do
20052005
whenProf $ copyModuleFiles "p_hi"
20062006
whenShared $ copyModuleFiles "dyn_hi"
20072007

2008+
-- copy extra compilation artifacts that ghc plugins may produce
2009+
copyDirectoryIfExists "extra-compilation-artifacts"
2010+
20082011
-- copy the built library files over:
20092012
whenHasCode $ do
20102013
whenVanilla $ do
@@ -2079,6 +2082,12 @@ installLib verbosity lbi targetDir dynlibTargetDir _builtDir pkg lib clbi = do
20792082
findModuleFilesEx verbosity [builtDir] [ext] (allLibModules lib clbi)
20802083
>>= installOrdinaryFiles verbosity targetDir
20812084

2085+
copyDirectoryIfExists dirName = do
2086+
let src = builtDir </> dirName
2087+
dst = targetDir </> dirName
2088+
dirExists <- doesDirectoryExist src
2089+
when dirExists $ copyDirectoryRecursive verbosity src dst
2090+
20822091
compiler_id = compilerId (compiler lbi)
20832092
platform = hostPlatform lbi
20842093
uid = componentUnitId clbi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib where
2+
3+
bar = ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cabal-version: 2.4
2+
name: lib
3+
version: 0.1.0.0
4+
library
5+
exposed-modules: Lib
6+
build-depends: base
7+
default-language: Haskell2010
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Distribution.Simple.LocalBuildInfo
2+
import Test.Cabal.Prelude
3+
import System.Directory
4+
import System.FilePath
5+
6+
-- Test if extra-compilation-artifacts are installed
7+
main = setupAndCabalTest . recordMode DoNotRecord $ do
8+
withPackageDb $ do
9+
setup "configure" []
10+
setup "build" []
11+
generateExtraCompArtifactsToBuildDir
12+
setup "copy" []
13+
14+
lbi <- getLocalBuildInfoM
15+
let installedLibPath = libdir $ absoluteInstallDirs (localPkgDescr lbi) lbi NoCopyDest
16+
17+
shouldExist $ installedLibPath </> "extra-compilation-artifacts" </> "ghc-plugin-X" </> "data-dir" </> "content-A.txt"
18+
shouldExist $ installedLibPath </> "extra-compilation-artifacts" </> "ghc-plugin-X" </> "data-dir" </> "content-B.txt"
19+
shouldExist $ installedLibPath </> "extra-compilation-artifacts" </> "ghc-plugin-Y" </> "content-Y.txt"
20+
21+
generateExtraCompArtifactsToBuildDir :: TestM ()
22+
generateExtraCompArtifactsToBuildDir = do
23+
-- extra compilation artifacts can be generated optionally by ghc plugins
24+
dist_dir <- fmap testDistDir getTestEnv
25+
let genArtifact fname = liftIO $ do
26+
let dst = dist_dir </> "build" </> "extra-compilation-artifacts" </> fname
27+
createDirectoryIfMissing True (takeDirectory dst)
28+
writeFile dst ""
29+
genArtifact $ "ghc-plugin-X" </> "data-dir" </> "content-A.txt"
30+
genArtifact $ "ghc-plugin-X" </> "data-dir" </> "content-B.txt"
31+
genArtifact $ "ghc-plugin-Y" </> "content-Y.txt"
32+

changelog.d/pr-8662

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
synopsis: Installation of extra-compilation-artifacts directory
2+
packages: Cabal
3+
prs: #8662
4+
issues:
5+
description: {
6+
7+
- GHC plugins now can store custom data in the 'extra-compilation-artifacts' directory which gets installed with the package.
8+
9+
}

doc/setup-commands.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ GHC provides the commands ``runhaskell`` and ``runghc`` (they are equivalent)
99
to allow you to run Haskell programs without first having to compile them
1010
(scripts). The low-level Cabal interface is implemented using ``Setup.hs``
1111
scripts. You should prefer using higher level interface provided by nix-style
12-
builds.
12+
builds. However, the documentation of the low level interface below may be helpful
13+
to high level interface users as well, because it delves into internal details
14+
common to both and omitted elsewhere.
1315

1416
::
1517

@@ -1104,6 +1106,10 @@ Copy the files into the install locations and (for library packages)
11041106
register the package with the compiler, i.e. make the modules it
11051107
contains available to programs.
11061108

1109+
Additionally for GHC the ``extra-compilation-artifacts`` directory is copied if present.
1110+
GHC plugins can store extra data in subfolders.
1111+
(e.g. *extra-compilation-artifacts/PLUGIN_NAME/HS_MODULE.txt*)
1112+
11071113
The `install locations <#installation-paths>`__ are determined by
11081114
options to `runhaskell Setup.hs configure`_.
11091115

0 commit comments

Comments
 (0)