From 82fa2293853661d746c65bea1b1099eade3bb6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Ackerman?= <6054733+akrmn@users.noreply.github.com> Date: Fri, 15 Oct 2021 17:52:29 +0200 Subject: [PATCH] Add daml-util-ghc lib (#11260) * Extract UtilGHC module from daml-lf-conversion to its own library * Drop daml-preprocessor dependency on daml-lf-conversion changelog_begin changelog_end --- .hlint.yaml | 2 +- compiler/damlc/daml-compiler/BUILD.bazel | 1 + .../src/DA/Daml/Compiler/Repl.hs | 2 +- compiler/damlc/daml-ghc-util/BUILD.bazel | 27 +++++++++++++++++++ .../src/DA/Daml}/UtilGHC.hs | 8 ++++-- compiler/damlc/daml-lf-conversion/BUILD.bazel | 1 + .../src/DA/Daml/LFConversion.hs | 7 +---- compiler/damlc/daml-preprocessor/BUILD.bazel | 2 +- .../src/DA/Daml/Preprocessor.hs | 2 +- 9 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 compiler/damlc/daml-ghc-util/BUILD.bazel rename compiler/damlc/{daml-lf-conversion/src/DA/Daml/LFConversion => daml-ghc-util/src/DA/Daml}/UtilGHC.hs (98%) diff --git a/.hlint.yaml b/.hlint.yaml index 2fa9f558f32..73e36beab43 100644 --- a/.hlint.yaml +++ b/.hlint.yaml @@ -115,7 +115,7 @@ # - {name: Control.Arrow, within: []} # Certain modules are banned entirely # - functions: - - {name: unsafePerformIO, within: [DA.Daml.LFConversion.UtilGHC]} + - {name: unsafePerformIO, within: [DA.Daml.UtilGHC]} - {name: unsafeInterleaveIO, within: []} - {name: unsafeDupablePerformIO, within: []} - {name: setCurrentDirectory, within: [DA.Daml.Assistant.Tests, Main]} diff --git a/compiler/damlc/daml-compiler/BUILD.bazel b/compiler/damlc/daml-compiler/BUILD.bazel index 1ae03d84131..b7be5b2acf4 100644 --- a/compiler/damlc/daml-compiler/BUILD.bazel +++ b/compiler/damlc/daml-compiler/BUILD.bazel @@ -51,6 +51,7 @@ da_haskell_library( "//compiler/daml-lf-reader", "//compiler/daml-lf-tools", "//compiler/damlc/daml-doctest", + "//compiler/damlc/daml-ghc-util", "//compiler/damlc/daml-ide-core", "//compiler/damlc/daml-lf-conversion", "//compiler/damlc/daml-opts", diff --git a/compiler/damlc/daml-compiler/src/DA/Daml/Compiler/Repl.hs b/compiler/damlc/daml-compiler/src/DA/Daml/Compiler/Repl.hs index 217a14d4014..1cff2794b2c 100644 --- a/compiler/damlc/daml-compiler/src/DA/Daml/Compiler/Repl.hs +++ b/compiler/damlc/daml-compiler/src/DA/Daml/Compiler/Repl.hs @@ -34,9 +34,9 @@ import DA.Daml.LF.Ast.Optics (packageRefs) import qualified DA.Daml.LF.ReplClient as ReplClient import DA.Daml.LFConversion (convertModule) import DA.Daml.LFConversion.UtilLF (buildPackage) -import DA.Daml.LFConversion.UtilGHC import DA.Daml.Options.Types import qualified DA.Daml.Preprocessor.Records as Preprocessor +import DA.Daml.UtilGHC import Data.Bifunctor (first) import Data.Functor.Alt import Data.Functor.Bind diff --git a/compiler/damlc/daml-ghc-util/BUILD.bazel b/compiler/damlc/daml-ghc-util/BUILD.bazel new file mode 100644 index 00000000000..3e43efe66c3 --- /dev/null +++ b/compiler/damlc/daml-ghc-util/BUILD.bazel @@ -0,0 +1,27 @@ +# Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +load( + "//bazel_tools:haskell.bzl", + "da_haskell_library", +) + +da_haskell_library( + name = "daml-ghc-util", + srcs = glob(["src/**/*.hs"]), + hackage_deps = [ + "base", + "bytestring", + "containers", + "ghc-lib-parser", + "ghc-lib", + "text", + "uniplate", + ], + repl_ghci_args = ["-hide-package=ghc"], + src_strip_prefix = "src", + visibility = ["//visibility:public"], + deps = [ + "//compiler/daml-lf-ast", + ], +) diff --git a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion/UtilGHC.hs b/compiler/damlc/daml-ghc-util/src/DA/Daml/UtilGHC.hs similarity index 98% rename from compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion/UtilGHC.hs rename to compiler/damlc/daml-ghc-util/src/DA/Daml/UtilGHC.hs index 3787a951a60..85e9a32ac8d 100644 --- a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion/UtilGHC.hs +++ b/compiler/damlc/daml-ghc-util/src/DA/Daml/UtilGHC.hs @@ -12,8 +12,8 @@ -- * Call runGhc, use runGhcFast instead. It's faster and doesn't require config we don't have. -- -- * Call setSessionDynFlags, use modifyDynFlags instead. It's faster and avoids loading packages. -module DA.Daml.LFConversion.UtilGHC( - module DA.Daml.LFConversion.UtilGHC +module DA.Daml.UtilGHC ( + module DA.Daml.UtilGHC ) where import "ghc-lib" GHC hiding (convertLit) @@ -327,3 +327,7 @@ collectNonRecLets = \case makeNonRecLets :: [(GHC.Var, GHC.Expr Var)] -> GHC.Expr Var -> GHC.Expr Var makeNonRecLets lets body = foldr (\(x,y) b -> Let (NonRec x y) b) body lets + +convertModuleName :: GHC.ModuleName -> LF.ModuleName +convertModuleName = + LF.ModuleName . T.split (== '.') . fsToText . moduleNameFS diff --git a/compiler/damlc/daml-lf-conversion/BUILD.bazel b/compiler/damlc/daml-lf-conversion/BUILD.bazel index de4ad331edb..8de1f36b0bd 100644 --- a/compiler/damlc/daml-lf-conversion/BUILD.bazel +++ b/compiler/damlc/daml-lf-conversion/BUILD.bazel @@ -38,6 +38,7 @@ da_haskell_library( "//compiler/daml-lf-ast", "//compiler/daml-lf-proto", "//compiler/daml-lf-tools", + "//compiler/damlc/daml-ghc-util", "//libs-haskell/da-hs-base", ], ) diff --git a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs b/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs index 48cf0607ed2..8cc4d5db250 100644 --- a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs +++ b/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs @@ -74,7 +74,6 @@ module DA.Daml.LFConversion ( convertModule - , convertModuleName , sourceLocToRange , convertRationalBigNumeric -- exposed for festing , runConvertM -- exposed for testing @@ -82,9 +81,9 @@ module DA.Daml.LFConversion ) where import DA.Daml.LFConversion.Primitives -import DA.Daml.LFConversion.UtilGHC import DA.Daml.LFConversion.UtilLF import DA.Daml.LFConversion.MetadataEncoding +import DA.Daml.UtilGHC import Development.IDE.Types.Diagnostics import Development.IDE.Types.Location @@ -1894,10 +1893,6 @@ convertCoercion env co = evalStateT (go env co) 0 flv = tyConFlavour t isSatNewTyCon _ _ = Nothing -convertModuleName :: GHC.ModuleName -> LF.ModuleName -convertModuleName = - ModuleName . T.split (== '.') . fsToText . moduleNameFS - qualify :: Env -> GHC.Module -> a -> ConvertM (Qualified a) qualify env m x = do unitId <- convertUnitId (envModuleUnitId env) (envPkgMap env) $ GHC.moduleUnitId m diff --git a/compiler/damlc/daml-preprocessor/BUILD.bazel b/compiler/damlc/daml-preprocessor/BUILD.bazel index 3d897af3481..48f9a45d5a0 100644 --- a/compiler/damlc/daml-preprocessor/BUILD.bazel +++ b/compiler/damlc/daml-preprocessor/BUILD.bazel @@ -35,7 +35,7 @@ da_haskell_library( "//compiler/daml-lf-ast", "//compiler/daml-lf-proto", "//compiler/daml-lf-tools", - "//compiler/damlc/daml-lf-conversion", + "//compiler/damlc/daml-ghc-util", "//compiler/damlc/stable-packages:stable-packages-lib", "//libs-haskell/bazel-runfiles", "//libs-haskell/da-hs-base", diff --git a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs index 9c97621fba7..d799f653d71 100644 --- a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs +++ b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs @@ -8,11 +8,11 @@ module DA.Daml.Preprocessor , noPreprocessor ) where -import DA.Daml.LFConversion (convertModuleName) import DA.Daml.Preprocessor.Records import DA.Daml.Preprocessor.Generics import DA.Daml.Preprocessor.EnumType import DA.Daml.StablePackages (stablePackageByModuleName) +import DA.Daml.UtilGHC (convertModuleName) import Development.IDE.Types.Options import qualified "ghc-lib" GHC