From 3afd3a3920b999f795ec73e6c9a9740b1eff132b Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Wed, 19 Jul 2023 13:22:36 +0200 Subject: [PATCH 1/2] Try each pkg-config query separatedly if returned list doesn't match query length MinGW's pkg-config returns only one version even if queried for multiple libraries. --- .../Distribution/Solver/Types/PkgConfigDb.hs | 20 ++++++++++++------- changelog.d/pkgconfig-once | 8 ++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 changelog.d/pkgconfig-once diff --git a/cabal-install-solver/src/Distribution/Solver/Types/PkgConfigDb.hs b/cabal-install-solver/src/Distribution/Solver/Types/PkgConfigDb.hs index 11e6da847f0..00bc38c28c3 100644 --- a/cabal-install-solver/src/Distribution/Solver/Types/PkgConfigDb.hs +++ b/cabal-install-solver/src/Distribution/Solver/Types/PkgConfigDb.hs @@ -70,12 +70,18 @@ readPkgConfigDb verbosity progdb = handle ioErrorHandler $ do (pkgVersions, _errs, exitCode) <- getProgramInvocationOutputAndErrors verbosity (programInvocation pkgConfig ("--modversion" : pkgNames)) - case exitCode of - ExitSuccess -> (return . pkgConfigDbFromList . zip pkgNames) (lines pkgVersions) - -- if there's a single broken pc file the above fails, so we fall back into calling it individually - _ -> do - info verbosity ("call to pkg-config --modversion on all packages failed. Falling back to querying pkg-config individually on each package") - pkgConfigDbFromList . catMaybes <$> mapM (getIndividualVersion pkgConfig) pkgNames + if exitCode == ExitSuccess && length pkgNames == length pkgList + then (return . pkgConfigDbFromList . zip pkgNames) (lines pkgVersions) + else + -- if there's a single broken pc file the above fails, so we fall back + -- into calling it individually + -- + -- Also some implementations of @pkg-config@ do not provide more than + -- one package version, so if the returned list is shorter than the + -- requested one, we fall back to querying one by one. + do + info verbosity ("call to pkg-config --modversion on all packages failed. Falling back to querying pkg-config individually on each package") + pkgConfigDbFromList . catMaybes <$> mapM (getIndividualVersion pkgConfig) pkgNames where -- For when pkg-config invocation fails (possibly because of a -- too long command line). @@ -92,7 +98,7 @@ readPkgConfigDb verbosity progdb = handle ioErrorHandler $ do getIndividualVersion pkgConfig pkg = do (pkgVersion, _errs, exitCode) <- getProgramInvocationOutputAndErrors verbosity - (programInvocation pkgConfig ["--modversion",pkg]) + (programInvocation pkgConfig ["--modversion", pkg]) return $ case exitCode of ExitSuccess -> Just (pkg, pkgVersion) _ -> Nothing diff --git a/changelog.d/pkgconfig-once b/changelog.d/pkgconfig-once new file mode 100644 index 00000000000..c3ac3ac47e0 --- /dev/null +++ b/changelog.d/pkgconfig-once @@ -0,0 +1,8 @@ +synopsis: PkgConfig individual calls +prs: #9134 + +description: { + +- `cabal` invokes `pkg-config` individually for each lib if querying for all doesn't return the expected result + +} From 9f1504590d4e9fa533265ae9035668a3ef4dbc9c Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 18 Sep 2023 17:08:08 +0200 Subject: [PATCH 2/2] Set `PKG_CONFIG_ALLOW_SYSTEM_(CFLAGS|LIBS)` by default --- Cabal/src/Distribution/Simple/Program/Builtin.hs | 7 +++++++ changelog.d/pkgconfig-envvars | 8 ++++++++ 2 files changed, 15 insertions(+) create mode 100644 changelog.d/pkgconfig-envvars diff --git a/Cabal/src/Distribution/Simple/Program/Builtin.hs b/Cabal/src/Distribution/Simple/Program/Builtin.hs index 1f3db0740f3..e604dbbe962 100644 --- a/Cabal/src/Distribution/Simple/Program/Builtin.hs +++ b/Cabal/src/Distribution/Simple/Program/Builtin.hs @@ -400,4 +400,11 @@ pkgConfigProgram :: Program pkgConfigProgram = (simpleProgram "pkg-config") { programFindVersion = findProgramVersion "--version" id + , programPostConf = \_ pkgConfProg -> + let programOverrideEnv' = + programOverrideEnv pkgConfProg + ++ [ ("PKG_CONFIG_ALLOW_SYSTEM_CFLAGS", Just "1") + , ("PKG_CONFIG_ALLOW_SYSTEM_LIBS", Just "1") + ] + in pure $ pkgConfProg{programOverrideEnv = programOverrideEnv'} } diff --git a/changelog.d/pkgconfig-envvars b/changelog.d/pkgconfig-envvars new file mode 100644 index 00000000000..9b570693a44 --- /dev/null +++ b/changelog.d/pkgconfig-envvars @@ -0,0 +1,8 @@ +synopsis: PkgConfig environment variables +prs: #9134 + +description: { + +- `cabal` invokes `pkg-config` with `PKG_CONFIG_ALLOW_SYSTEM_CFLAGS` and `PKG_CONFIG_ALLOW_SYSTEM_LIBS` set + +}