Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The cabal parser does not see common stanzas #9617

Closed
Kleidukos opened this issue Jan 16, 2024 · 10 comments
Closed

The cabal parser does not see common stanzas #9617

Kleidukos opened this issue Jan 16, 2024 · 10 comments
Labels
Cabal: common stanza Concerning `common` stanzas in `.cabal` files Cabal: parser type: user-question

Comments

@Kleidukos
Copy link
Member

Using parseGenericPackageDescription, on a cabal file, it seems that common stanzas are not "seen" by the parser. By that I mean they are not expanded in the PackageDescription result.

Example:

Cabal file (truncated)
cabal-version:      3.0
name:               libsodium-bindings
version:            0.0.1.1
license:            BSD-3-Clause
build-type:         Simple
tested-with:
  GHC ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3

extra-source-files:
  LICENSE
  README.md

extra-doc-files:    CHANGELOG.md

flag use-pkg-config
  description: Use pkg-config to find Libsodium. Used by default on macOS.
  default:     False
  manual:      True

common common
  build-depends:    base >=4.14 && <5

  if (os(osx) || flag(use-pkg-config))
    pkgconfig-depends: libsodium ==1.0.18

  extra-libraries: sodium
  default-language: Haskell2010

common common-rts-options
  ghc-options: -rtsopts -threaded -with-rtsopts=-N

library
  import:          common
  hs-source-dirs:  src
Result
PackageDescription
    { specVersion = CabalSpecV3_0
    , package = PackageIdentifier{pkgName = PackageName "libsodium-bindings", pkgVersion = mkVersion [0, 0, 1, 1]}
    , licenseRaw = Left (License (ELicense (ELicenseId BSD_3_Clause) Nothing))
    , licenseFiles = []
    , copyright = ""
    , maintainer = "The Haskell Cryptography contributors"
    , author = "H\233cate Moonlight, Koz Ross"
    , stability = ""
    , testedWith =
        [
            ( GHC
            , UnionVersionRanges
                (ThisVersion (mkVersion [8, 10, 7]))
                ( UnionVersionRanges
                    (ThisVersion (mkVersion [9, 0, 2]))
                    ( UnionVersionRanges
                        (ThisVersion (mkVersion [9, 2, 8]))
                        (UnionVersionRanges (ThisVersion (mkVersion [9, 4, 8])) (ThisVersion (mkVersion [9, 6, 3])))
                    )
                )
            )
        ]
    , homepage = "https://github.com/haskell-cryptography/libsodium-bindings"
    , pkgUrl = ""
    , bugReports = "https://github.com/haskell-cryptography/libsodium-bindings/issues"
    , sourceRepos =
        [ SourceRepo
            { repoKind = RepoHead
            , repoType = Just (KnownRepoType Git)
            , repoLocation = Just "https://github.com/haskell-cryptography/libsodium-bindings"
            , repoModule = Nothing
            , repoBranch = Nothing
            , repoTag = Nothing
            , repoSubdir = Nothing
            }
        ]
    , synopsis = "FFI bindings to libsodium"
    , description =
        "This library embeds FFI bindings to the stable version of libsodium 1.0.18.\nThe interface exposed by this library is kept close to the C library."
    , category = "Cryptography"
    , customFieldsPD = []
    , buildTypeRaw = Just Simple
    , setupBuildInfo = Nothing
    , library = Nothing
    , subLibraries = []
    , executables = []
    , foreignLibs = []
    , testSuites = []
    , benchmarks = []
    , dataFiles = []
    , dataDir = "."
    , extraSrcFiles = ["LICENSE", "README.md"]
    , extraTmpFiles = []
    , extraDocFiles = ["CHANGELOG.md"]
    }

We can see that there is no information whatsoever that is contained in the common stanza.

My question is the following: Which function do I need to call to take common stanzas into account in my PackageDescription?

@Kleidukos Kleidukos added type: user-question Cabal: parser Cabal: common stanza Concerning `common` stanzas in `.cabal` files labels Jan 16, 2024
@jasagredo
Copy link
Collaborator

jasagredo commented Jan 16, 2024

Could this be a degenerate case because there are no source files at all in the library?

Have you tried moving the common stanzas before the flags? Does cabal complain in verbose mode about "ignoring section blah"?

@Kleidukos
Copy link
Member Author

Could this be a degenerate case because there are no source files at all in the library?

Apologies, I elided the files to make the example more concise, the actual cabal file has modules: https://github.com/haskell-cryptography/libsodium-bindings/blob/98fa256e355e1037337f1d9a0aa439dca5e48ffd/libsodium-bindings/libsodium-bindings.cabal

Have you tried moving the common stanzas before the flags? Does cabal complain in verbose mode about "ignoring section blah"?

I will try this, thank you.

@Kleidukos
Copy link
Member Author

Update:

With the following file:

libsodium-bindings.cabal
cabal-version:      3.0
name:               libsodium-bindings
version:            0.0.1.1
category:           Cryptography
synopsis:           FFI bindings to libsodium
description:
  This library embeds FFI bindings to the stable version of libsodium 1.0.18.
  The interface exposed by this library is kept close to the C library.

homepage:           https://github.com/haskell-cryptography/libsodium-bindings
bug-reports:
  https://github.com/haskell-cryptography/libsodium-bindings/issues

author:             Hécate Moonlight, Koz Ross
maintainer:         The Haskell Cryptography contributors
license:            BSD-3-Clause
build-type:         Simple
tested-with:
  GHC ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3

extra-source-files:
  LICENSE
  README.md

extra-doc-files:    CHANGELOG.md

common common
  build-depends:    base >=4.14 && <5
  ghc-options:
    -Wall -Wcompat -Widentities -Wincomplete-record-updates
    -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints
    -fhide-source-paths -Wno-unused-do-bind -haddock

  if (os(osx) || flag(use-pkg-config))
    pkgconfig-depends: libsodium ==1.0.18

  extra-libraries: sodium
  default-language: Haskell2010

common common-rts-options
  ghc-options: -rtsopts -threaded -with-rtsopts=-N

flag use-pkg-config
  description: Use pkg-config to find Libsodium. Used by default on macOS.
  default:     False
  manual:      True

source-repository head
  type:     git
  location: https://github.com/haskell-cryptography/libsodium-bindings

library
  import:          common
  hs-source-dirs:  src

  -- cabal-fmt: expand src/
  exposed-modules:
    LibSodium.Bindings
    LibSodium.Bindings.AEAD
    LibSodium.Bindings.Comparison
    LibSodium.Bindings.CryptoAuth
    LibSodium.Bindings.CryptoBox
    LibSodium.Bindings.CryptoSign
    LibSodium.Bindings.GenericHashing
    LibSodium.Bindings.KeyDerivation
    LibSodium.Bindings.KeyExchange
    LibSodium.Bindings.Main
    LibSodium.Bindings.PasswordHashing
    LibSodium.Bindings.Random
    LibSodium.Bindings.Scrypt
    LibSodium.Bindings.SealedBoxes
    LibSodium.Bindings.Secretbox
    LibSodium.Bindings.SecretStream
    LibSodium.Bindings.SecureMemory
    LibSodium.Bindings.SHA2
    LibSodium.Bindings.ShortHashing
    LibSodium.Bindings.Utils
    LibSodium.Bindings.XChaCha20

I get the following result

Package Description
PackageDescription
    { specVersion = CabalSpecV3_0
    , package = PackageIdentifier{pkgName = PackageName "libsodium-bindings", pkgVersion = mkVersion [0, 0, 1, 1]}
    , licenseRaw = Left (License (ELicense (ELicenseId BSD_3_Clause) Nothing))
    , licenseFiles = []
    , copyright = ""
    , maintainer = "The Haskell Cryptography contributors"
    , author = "H\233cate Moonlight, Koz Ross"
    , stability = ""
    , testedWith =
        [
            ( GHC
            , UnionVersionRanges
                (ThisVersion (mkVersion [8, 10, 7]))
                ( UnionVersionRanges
                    (ThisVersion (mkVersion [9, 0, 2]))
                    ( UnionVersionRanges
                        (ThisVersion (mkVersion [9, 2, 8]))
                        (UnionVersionRanges (ThisVersion (mkVersion [9, 4, 8])) (ThisVersion (mkVersion [9, 6, 3])))
                    )
                )
            )
        ]
    , homepage = "https://github.com/haskell-cryptography/libsodium-bindings"
    , pkgUrl = ""
    , bugReports = "https://github.com/haskell-cryptography/libsodium-bindings/issues"
    , sourceRepos =
        [ SourceRepo
            { repoKind = RepoHead
            , repoType = Just (KnownRepoType Git)
            , repoLocation = Just "https://github.com/haskell-cryptography/libsodium-bindings"
            , repoModule = Nothing
            , repoBranch = Nothing
            , repoTag = Nothing
            , repoSubdir = Nothing
            }
        ]
    , synopsis = "FFI bindings to libsodium"
    , description =
        "This library embeds FFI bindings to the stable version of libsodium 1.0.18.\nThe interface exposed by this library is kept close to the C library."
    , category = "Cryptography"
    , customFieldsPD = []
    , buildTypeRaw = Just Simple
    , setupBuildInfo = Nothing
    , library = Nothing
    , subLibraries = []
    , executables = []
    , foreignLibs = []
    , testSuites = []
    , benchmarks = []
    , dataFiles = []
    , dataDir = "."
    , extraSrcFiles = ["LICENSE", "README.md"]
    , extraTmpFiles = []
    , extraDocFiles = ["CHANGELOG.md"]
    }

@jasagredo
Copy link
Collaborator

What is the issue you are facing later on? The common stanzas might be expanded later on in the process (I think this is the case), so is there a specific problem you are facing?

@fgaz
Copy link
Member

fgaz commented Jan 16, 2024

How did you turn the GenericPackageDescription into a PackageDescription?

@Kleidukos
Copy link
Member Author

@jasagredo

There is no "later on" for me, I use the PackageDescription and I don't know how to get the stanzas (expanded or not).

@fgaz I'm just accessing the field packageDescription: https://hackage.haskell.org/package/Cabal-syntax-3.10.2.0/docs/Distribution-Types-GenericPackageDescription.html#t:GenericPackageDescription

@fgaz
Copy link
Member

fgaz commented Jan 16, 2024

The packageDescription field of GenericPackageDescription is only partially filled. The rest is in the CondTrees, still unresolved. To get a filled PackageDescription you have to use finalizePD or flattenPackageDescription. I think there's a comment about it where PackageDescription or finalizePD is defined.

@Kleidukos
Copy link
Member Author

@Kleidukos
Copy link
Member Author

Okay, indeed it was all in the generic package description:

GenericPackageDescription
GenericPackageDescription
    { packageDescription =
        PackageDescription
            { specVersion = CabalSpecV3_0
            , package =
                PackageIdentifier
                    { pkgName = PackageName "libsodium-bindings"
                    , pkgVersion = mkVersion [0, 0, 1, 1]
                    }
            , licenseRaw = Left (License (ELicense (ELicenseId BSD_3_Clause) Nothing))
            , licenseFiles = []
            , copyright = ""
            , maintainer = "The Haskell Cryptography contributors"
            , author = "H\233cate Moonlight, Koz Ross"
            , stability = ""
            , testedWith =
                [
                    ( GHC
                    , UnionVersionRanges
                        (ThisVersion (mkVersion [8, 10, 7]))
                        ( UnionVersionRanges
                            (ThisVersion (mkVersion [9, 0, 2]))
                            ( UnionVersionRanges
                                (ThisVersion (mkVersion [9, 2, 8]))
                                ( UnionVersionRanges
                                    (ThisVersion (mkVersion [9, 4, 8]))
                                    (ThisVersion (mkVersion [9, 6, 3]))
                                )
                            )
                        )
                    )
                ]
            , homepage = "https://github.com/haskell-cryptography/libsodium-bindings"
            , pkgUrl = ""
            , bugReports = "https://github.com/haskell-cryptography/libsodium-bindings/issues"
            , sourceRepos =
                [ SourceRepo
                    { repoKind = RepoHead
                    , repoType = Just (KnownRepoType Git)
                    , repoLocation = Just "https://github.com/haskell-cryptography/libsodium-bindings"
                    , repoModule = Nothing
                    , repoBranch = Nothing
                    , repoTag = Nothing
                    , repoSubdir = Nothing
                    }
                ]
            , synopsis = "FFI bindings to libsodium"
            , description =
                "This library embeds FFI bindings to the stable version of libsodium 1.0.18.\nThe interface exposed by this library is kept close to the C library."
            , category = "Cryptography"
            , customFieldsPD = []
            , buildTypeRaw = Just Simple
            , setupBuildInfo = Nothing
            , library = Nothing
            , subLibraries = []
            , executables = []
            , foreignLibs = []
            , testSuites = []
            , benchmarks = []
            , dataFiles = []
            , dataDir = "."
            , extraSrcFiles = ["LICENSE", "README.md"]
            , extraTmpFiles = []
            , extraDocFiles = ["CHANGELOG.md"]
            }
    , gpdScannedVersion = Nothing
    , genPackageFlags =
        [ MkPackageFlag
            { flagName = FlagName "use-pkg-config"
            , flagDescription = "Use pkg-config to find Libsodium. Used by default on macOS."
            , flagDefault = False
            , flagManual = True
            }
        ]
    , condLibrary =
        Just
            ( CondNode
                { condTreeData =
                    Library
                        { libName = LMainLibName
                        , exposedModules =
                            [ ModuleName "LibSodium.Bindings"
                            , ModuleName "LibSodium.Bindings.AEAD"
                            , ModuleName "LibSodium.Bindings.Comparison"
                            , ModuleName "LibSodium.Bindings.CryptoAuth"
                            , ModuleName "LibSodium.Bindings.CryptoBox"
                            , ModuleName "LibSodium.Bindings.CryptoSign"
                            , ModuleName "LibSodium.Bindings.GenericHashing"
                            , ModuleName "LibSodium.Bindings.KeyDerivation"
                            , ModuleName "LibSodium.Bindings.KeyExchange"
                            , ModuleName "LibSodium.Bindings.Main"
                            , ModuleName "LibSodium.Bindings.PasswordHashing"
                            , ModuleName "LibSodium.Bindings.Random"
                            , ModuleName "LibSodium.Bindings.Scrypt"
                            , ModuleName "LibSodium.Bindings.SealedBoxes"
                            , ModuleName "LibSodium.Bindings.Secretbox"
                            , ModuleName "LibSodium.Bindings.SecretStream"
                            , ModuleName "LibSodium.Bindings.SecureMemory"
                            , ModuleName "LibSodium.Bindings.SHA2"
                            , ModuleName "LibSodium.Bindings.ShortHashing"
                            , ModuleName "LibSodium.Bindings.Utils"
                            , ModuleName "LibSodium.Bindings.XChaCha20"
                            ]
                        , reexportedModules = []
                        , signatures = []
                        , libExposed = True
                        , libVisibility = LibraryVisibilityPublic
                        , libBuildInfo =
                            BuildInfo
                                { buildable = True
                                , buildTools = []
                                , buildToolDepends = []
                                , cppOptions = []
                                , asmOptions = []
                                , cmmOptions = []
                                , ccOptions = []
                                , cxxOptions = []
                                , ldOptions = []
                                , hsc2hsOptions = []
                                , pkgconfigDepends = []
                                , frameworks = []
                                , extraFrameworkDirs = []
                                , asmSources = []
                                , cmmSources = []
                                , cSources = []
                                , cxxSources = []
                                , jsSources = []
                                , hsSourceDirs = [SymbolicPath "src"]
                                , otherModules = []
                                , virtualModules = []
                                , autogenModules = []
                                , defaultLanguage = Just Haskell2010
                                , otherLanguages = []
                                , defaultExtensions = []
                                , otherExtensions = []
                                , oldExtensions = []
                                , extraLibs = ["sodium"]
                                , extraLibsStatic = []
                                , extraGHCiLibs = []
                                , extraBundledLibs = []
                                , extraLibFlavours = []
                                , extraDynLibFlavours = []
                                , extraLibDirs = []
                                , extraLibDirsStatic = []
                                , includeDirs = []
                                , includes = []
                                , autogenIncludes = []
                                , installIncludes = []
                                , options =
                                    PerCompilerFlavor
                                        [ "-Wall"
                                        , "-Wcompat"
                                        , "-Widentities"
                                        , "-Wincomplete-record-updates"
                                        , "-Wincomplete-uni-patterns"
                                        , "-Wpartial-fields"
                                        , "-Wredundant-constraints"
                                        , "-fhide-source-paths"
                                        , "-Wno-unused-do-bind"
                                        , "-haddock"
                                        ]
                                        []
                                , profOptions = PerCompilerFlavor [] []
                                , sharedOptions = PerCompilerFlavor [] []
                                , staticOptions = PerCompilerFlavor [] []
                                , customFieldsBI = []
                                , targetBuildDepends =
                                    [ Dependency
                                        (PackageName "base")
                                        ( IntersectVersionRanges
                                            (OrLaterVersion (mkVersion [4, 14]))
                                            (EarlierVersion (mkVersion [5]))
                                        )
                                        (fromNonEmpty (LMainLibName :| []))
                                    ]
                                , mixins = []
                                }
                        }
                , condTreeConstraints =
                    [ Dependency
                        (PackageName "base")
                        ( IntersectVersionRanges
                            (OrLaterVersion (mkVersion [4, 14]))
                            (EarlierVersion (mkVersion [5]))
                        )
                        (fromNonEmpty (LMainLibName :| []))
                    ]
                , condTreeComponents =
                    [ CondBranch
                        { condBranchCondition =
                            COr (Var (OS OSX)) (Var (PackageFlag (FlagName "use-pkg-config")))
                        , condBranchIfTrue =
                            CondNode
                                { condTreeData =
                                    Library
                                        { libName = LMainLibName
                                        , exposedModules = []
                                        , reexportedModules = []
                                        , signatures = []
                                        , libExposed = True
                                        , libVisibility = LibraryVisibilityPublic
                                        , libBuildInfo =
                                            BuildInfo
                                                { buildable = True
                                                , buildTools = []
                                                , buildToolDepends = []
                                                , cppOptions = []
                                                , asmOptions = []
                                                , cmmOptions = []
                                                , ccOptions = []
                                                , cxxOptions = []
                                                , ldOptions = []
                                                , hsc2hsOptions = []
                                                , pkgconfigDepends =
                                                    [ PkgconfigDependency
                                                        (PkgconfigName "libsodium")
                                                        (PcThisVersion (PkgconfigVersion "1.0.18"))
                                                    ]
                                                , frameworks = []
                                                , extraFrameworkDirs = []
                                                , asmSources = []
                                                , cmmSources = []
                                                , cSources = []
                                                , cxxSources = []
                                                , jsSources = []
                                                , hsSourceDirs = []
                                                , otherModules = []
                                                , virtualModules = []
                                                , autogenModules = []
                                                , defaultLanguage = Nothing
                                                , otherLanguages = []
                                                , defaultExtensions = []
                                                , otherExtensions = []
                                                , oldExtensions = []
                                                , extraLibs = []
                                                , extraLibsStatic = []
                                                , extraGHCiLibs = []
                                                , extraBundledLibs = []
                                                , extraLibFlavours = []
                                                , extraDynLibFlavours = []
                                                , extraLibDirs = []
                                                , extraLibDirsStatic = []
                                                , includeDirs = []
                                                , includes = []
                                                , autogenIncludes = []
                                                , installIncludes = []
                                                , options = PerCompilerFlavor [] []
                                                , profOptions = PerCompilerFlavor [] []
                                                , sharedOptions = PerCompilerFlavor [] []
                                                , staticOptions = PerCompilerFlavor [] []
                                                , customFieldsBI = []
                                                , targetBuildDepends = []
                                                , mixins = []
                                                }
                                        }
                                , condTreeConstraints = []
                                , condTreeComponents = []
                                }
                        , condBranchIfFalse = Nothing
                        }
                    ]
                }
            )
    , condSubLibraries = []
    , condForeignLibs = []
    , condExecutables = []
    , condTestSuites = []
    , condBenchmarks = []
    }

@fgaz
Copy link
Member

fgaz commented Jan 16, 2024

Great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cabal: common stanza Concerning `common` stanzas in `.cabal` files Cabal: parser type: user-question
Projects
None yet
Development

No branches or pull requests

3 participants