]> git.saurik.com Git - apt.git/blobdiff - apt-private/private-source.cc
FildFd: Introduce a Flush() function and call it from Close()
[apt.git] / apt-private / private-source.cc
index c7e5e425720512cf3985b27d884d10f7bac3af6d..52487248f0c8a97f61315199240bc9e7041d8cb8 100644 (file)
@@ -245,8 +245,8 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name,
         // choose a good candidate and proceed with that.
         // Maybe we will find a source later on with the right VerTag
         // or RelTag
         // choose a good candidate and proceed with that.
         // Maybe we will find a source later on with the right VerTag
         // or RelTag
-        pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
-        if (Ver.end() == false) 
+        pkgCache::VerIterator const Ver = Cache->GetCandidateVersion(Pkg);
+        if (Ver.end() == false)
         {
            if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
               Src = Ver.SourcePkgName();
         {
            if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
               Src = Ver.SourcePkgName();
@@ -288,6 +288,7 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name,
                  while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0) 
                  {
                     const std::string Ver = Parse->Version();
                  while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0) 
                  {
                     const std::string Ver = Parse->Version();
+                    bool CorrectRelTag = false;
 
                     // See if we need to look for a specific release tag
                     if (RelTag != "" && UserRequestedVerTag == "")
 
                     // See if we need to look for a specific release tag
                     if (RelTag != "" && UserRequestedVerTag == "")
@@ -297,13 +298,10 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name,
                        {
                           if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
                                 (Rls->Codename != 0 && RelTag == Rls.Codename()))
                        {
                           if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
                                 (Rls->Codename != 0 && RelTag == Rls.Codename()))
-                          {
-                             Last = Parse;
-                             Offset = Parse->Offset();
-                             Version = Ver;
-                          }
+                             CorrectRelTag = true;
                        }
                        }
-                    }
+                    } else
+                       CorrectRelTag = true;
 
                     // Ignore all versions which doesn't fit
                     if (VerTag.empty() == false &&
 
                     // Ignore all versions which doesn't fit
                     if (VerTag.empty() == false &&
@@ -311,7 +309,7 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name,
                        continue;
 
                     // Newer version or an exact match? Save the hit
                        continue;
 
                     // Newer version or an exact match? Save the hit
-                    if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
+                    if (CorrectRelTag && (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0)) {
                        Last = Parse;
                        Offset = Parse->Offset();
                        Version = Ver;
                        Last = Parse;
                        Offset = Parse->Offset();
                        Version = Ver;
@@ -613,412 +611,434 @@ bool DoSource(CommandLine &CmdL)
    return ExecWait(Process, "dpkg-source");
 }
                                                                        /*}}}*/
    return ExecWait(Process, "dpkg-source");
 }
                                                                        /*}}}*/
-// DoBuildDep - Install/removes packages to satisfy build dependencies  /*{{{*/
-// ---------------------------------------------------------------------
-/* This function will look at the build depends list of the given source 
-   package and install the necessary packages to make it true, or fail. */
-bool DoBuildDep(CommandLine &CmdL)
+// InstallBuildDepsLoop                                                        /*{{{*/
+static bool InstallBuildDepsLoop(CacheFile &Cache, std::string const &Src,
+      std::vector<pkgSrcRecords::Parser::BuildDepRec> const &BuildDeps,
+      bool const StripMultiArch, std::string const &hostArch)
 {
 {
-   CacheFile Cache;
-
-   _config->Set("APT::Install-Recommends", false);
-
-   bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
-
-   if (Cache.Open(WantLock) == false)
-      return false;
-
-   if (CmdL.FileSize() <= 1)
-      return _error->Error(_("Must specify at least one package to check builddeps for"));
-
-   // Read the source list
-   if (Cache.BuildSourceList() == false)
-      return false;
-   pkgSourceList *List = Cache.GetSourceList();
-
-   // Create the text record parsers
-   pkgSrcRecords SrcRecs(*List);
-   if (_error->PendingError() == true)
-      return false;
-
-   bool StripMultiArch;
-   std::string hostArch = _config->Find("APT::Get::Host-Architecture");
-   if (hostArch.empty() == false)
-   {
-      std::vector<std::string> archs = APT::Configuration::getArchitectures();
-      if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
-        return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
-      StripMultiArch = false;
-   }
-   else
-      StripMultiArch = true;
-
-   unsigned J = 0;
-   for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
+   // Install the requested packages
+   std::vector <pkgSrcRecords::Parser::BuildDepRec>::const_iterator D;
+   pkgProblemResolver Fix(Cache);
+   bool skipAlternatives = false; // skip remaining alternatives in an or group
+   for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
    {
    {
-      std::string Src;
-      pkgSrcRecords::Parser *Last = 0;
-      std::unique_ptr<pkgSrcRecords::Parser> LastOwner;
-
-      // an unpacked debian source tree
-      using APT::String::Startswith;
-      if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
-           DirectoryExists(*I))
-      {
-        ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
-        // FIXME: how can we make this more elegant?
-        std::string TypeName = "Debian control file";
-        pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
-        if(Type != NULL)
-           LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
-      }
-      // if its a local file (e.g. .dsc) use this
-      else if (FileExists(*I))
-      {
-        ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
-
-        // see if we can get a parser for this pkgIndexFile type
-        std::string TypeName = "Debian " + flExtension(*I) + " file";
-        pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
-        if(Type != NULL)
-           LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
-      } else {
-        // normal case, search the cache for the source file
-        Last = FindSrc(*I,SrcRecs,Src,Cache);
-      }
+      bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
 
 
-      if (Last == 0)
-        return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
-
-      // Process the build-dependencies
-      std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
-      // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
-      if (hostArch.empty() == false)
+      if (skipAlternatives == true)
       {
       {
-        std::string nativeArch = _config->Find("APT::Architecture");
-        _config->Set("APT::Architecture", hostArch);
-        bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
-        _config->Set("APT::Architecture", nativeArch);
-        if (Success == false)
-           return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
-      }
-      else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
-        return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
-
-      // Also ensure that build-essential packages are present
-      Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
-      if (Opts) 
-        Opts = Opts->Child;
-      for (; Opts; Opts = Opts->Next)
-      {
-        if (Opts->Value.empty() == true)
-           continue;
-
-        pkgSrcRecords::Parser::BuildDepRec rec;
-        rec.Package = Opts->Value;
-        rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
-        rec.Op = 0;
-        BuildDeps.push_back(rec);
-      }
-
-      if (BuildDeps.empty() == true)
-      {
-        ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
+        /*
+         * if there are alternatives, we've already picked one, so skip
+         * the rest
+         *
+         * TODO: this means that if there's a build-dep on A|B and B is
+         * installed, we'll still try to install A; more importantly,
+         * if A is currently broken, we cannot go back and try B. To fix
+         * this would require we do a Resolve cycle for each package we
+         * add to the install list. Ugh
+         */
+        if (!hasAlternatives)
+           skipAlternatives = false; // end of or group
         continue;
       }
 
         continue;
       }
 
-      // Install the requested packages
-      std::vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
-      pkgProblemResolver Fix(Cache);
-      bool skipAlternatives = false; // skip remaining alternatives in an or group
-      for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
+      if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
+           (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
       {
       {
-        bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
+        pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
+        // Build-conflicts on unknown packages are silently ignored
+        if (Grp.end() == true)
+           continue;
 
 
-        if (skipAlternatives == true)
+        for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
         {
         {
+           pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
            /*
            /*
-            * if there are alternatives, we've already picked one, so skip
-            * the rest
-            *
-            * TODO: this means that if there's a build-dep on A|B and B is
-            * installed, we'll still try to install A; more importantly,
-            * if A is currently broken, we cannot go back and try B. To fix 
-            * this would require we do a Resolve cycle for each package we 
-            * add to the install list. Ugh
+            * Remove if we have an installed version that satisfies the
+            * version criteria
             */
             */
-           if (!hasAlternatives)
-              skipAlternatives = false; // end of or group
-           continue;
+           if (IV.end() == false &&
+                 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+              TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
         }
         }
+      }
+      else // BuildDep || BuildDepIndep
+      {
+        if (_config->FindB("Debug::BuildDeps",false) == true)
+           std::cout << "Looking for " << (*D).Package << "...\n";
 
 
-        if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
-              (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
-        {
-           pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
-           // Build-conflicts on unknown packages are silently ignored
-           if (Grp.end() == true)
-              continue;
+        pkgCache::PkgIterator Pkg;
 
 
-           for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
-           {
-              pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
-              /*
-               * Remove if we have an installed version that satisfies the
-               * version criteria
-               */
-              if (IV.end() == false &&
-                    Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
-                 TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
-           }
-        }
-        else // BuildDep || BuildDepIndep
+        // Cross-Building?
+        if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
         {
         {
-           if (_config->FindB("Debug::BuildDeps",false) == true)
-              std::cout << "Looking for " << (*D).Package << "...\n";
-
-           pkgCache::PkgIterator Pkg;
-
-           // Cross-Building?
-           if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
+           size_t const colon = D->Package.find(":");
+           if (colon != std::string::npos)
            {
            {
-              size_t const colon = D->Package.find(":");
-              if (colon != std::string::npos)
-              {
-                 if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
-                    Pkg = Cache->FindPkg(D->Package.substr(0,colon));
-                 else
-                    Pkg = Cache->FindPkg(D->Package);
-              }
+              if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
+                 Pkg = Cache->FindPkg(D->Package.substr(0,colon));
               else
               else
-                 Pkg = Cache->FindPkg(D->Package, hostArch);
+                 Pkg = Cache->FindPkg(D->Package);
+           }
+           else
+              Pkg = Cache->FindPkg(D->Package, hostArch);
 
 
-              // a bad version either is invalid or doesn't satify dependency
+           // a bad version either is invalid or doesn't satify dependency
 #define BADVER(Ver) (Ver.end() == true || \
       (D->Version.empty() == false && \
        Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
 
 #define BADVER(Ver) (Ver.end() == true || \
       (D->Version.empty() == false && \
        Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
 
-              APT::VersionList verlist;
-              if (Pkg.end() == false)
+           APT::VersionList verlist;
+           if (Pkg.end() == false)
+           {
+              pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
+              if (BADVER(Ver) == false)
+                 verlist.insert(Ver);
+              Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
+              if (BADVER(Ver) == false)
+                 verlist.insert(Ver);
+           }
+           if (verlist.empty() == true)
+           {
+              pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
+              if (BuildPkg.end() == false && Pkg != BuildPkg)
               {
               {
-                 pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
+                 pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
                  if (BADVER(Ver) == false)
                     verlist.insert(Ver);
                  if (BADVER(Ver) == false)
                     verlist.insert(Ver);
-                 Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
+                 Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
                  if (BADVER(Ver) == false)
                     verlist.insert(Ver);
               }
                  if (BADVER(Ver) == false)
                     verlist.insert(Ver);
               }
-              if (verlist.empty() == true)
-              {
-                 pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
-                 if (BuildPkg.end() == false && Pkg != BuildPkg)
-                 {
-                    pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
-                    if (BADVER(Ver) == false)
-                       verlist.insert(Ver);
-                    Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
-                    if (BADVER(Ver) == false)
-                       verlist.insert(Ver);
-                 }
-              }
+           }
 #undef BADVER
 
 #undef BADVER
 
-              std::string forbidden;
-              // We need to decide if host or build arch, so find a version we can look at
-              APT::VersionList::const_iterator Ver = verlist.begin();
-              for (; Ver != verlist.end(); ++Ver)
+           std::string forbidden;
+           // We need to decide if host or build arch, so find a version we can look at
+           APT::VersionList::const_iterator Ver = verlist.begin();
+           for (; Ver != verlist.end(); ++Ver)
+           {
+              forbidden.clear();
+              if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
               {
               {
-                 forbidden.clear();
-                 if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
-                 {
-                    if (colon == std::string::npos)
-                       Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
-                    else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
-                       forbidden = "Multi-Arch: no";
-                    else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
-                       Pkg = Ver.ParentPkg().Group().FindPkg("native");
-                 }
-                 else if (Ver->MultiArch == pkgCache::Version::Same)
-                 {
-                    if (colon == std::string::npos)
-                       Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
-                    else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
-                       forbidden = "Multi-Arch: same";
-                    else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
-                       Pkg = Ver.ParentPkg().Group().FindPkg("native");
-                 }
-                 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
-                 {
-                    if (colon == std::string::npos)
-                       Pkg = Ver.ParentPkg().Group().FindPkg("native");
-                    else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
-                          strcmp(D->Package.c_str() + colon, ":native") == 0)
-                       forbidden = "Multi-Arch: foreign";
-                 }
-                 else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
-                 {
-                    if (colon == std::string::npos)
-                       Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
-                    else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
-                    {
-                       // prefer any installed over preferred non-installed architectures
-                       pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
-                       // we don't check for version here as we are better of with upgrading than remove and install
-                       for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
-                          if (Pkg.CurrentVer().end() == false)
-                             break;
-                       if (Pkg.end() == true)
-                          Pkg = Grp.FindPreferredPkg(true);
-                    }
-                    else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
-                       Pkg = Ver.ParentPkg().Group().FindPkg("native");
-                 }
-
-                 if (forbidden.empty() == false)
-                 {
-                    if (_config->FindB("Debug::BuildDeps",false) == true)
-                       std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
-                    continue;
-                 }
-
-                 //we found a good version
-                 break;
+                 if (colon == std::string::npos)
+                    Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+                 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+                    forbidden = "Multi-Arch: no";
+                 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+                    Pkg = Ver.ParentPkg().Group().FindPkg("native");
               }
               }
-              if (Ver == verlist.end())
+              else if (Ver->MultiArch == pkgCache::Version::Same)
               {
               {
-                 if (_config->FindB("Debug::BuildDeps",false) == true)
-                    std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
-
-                 if (forbidden.empty() == false)
-                 {
-                    if (hasAlternatives)
-                       continue;
-                    return _error->Error(_("%s dependency for %s can't be satisfied "
-                             "because %s is not allowed on '%s' packages"),
-                          Last->BuildDepType(D->Type), Src.c_str(),
-                          D->Package.c_str(), forbidden.c_str());
-                 }
+                 if (colon == std::string::npos)
+                    Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+                 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+                    forbidden = "Multi-Arch: same";
+                 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+                    Pkg = Ver.ParentPkg().Group().FindPkg("native");
               }
               }
-           }
-           else
-              Pkg = Cache->FindPkg(D->Package);
-
-           if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
-           {
-              if (_config->FindB("Debug::BuildDeps",false) == true)
-                 std::cout << " (not found)" << (*D).Package << std::endl;
-
-              if (hasAlternatives)
-                 continue;
-
-              return _error->Error(_("%s dependency for %s cannot be satisfied "
-                       "because the package %s cannot be found"),
-                    Last->BuildDepType((*D).Type),Src.c_str(),
-                    (*D).Package.c_str());
-           }
-
-           pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
-           if (IV.end() == false)
-           {
-              if (_config->FindB("Debug::BuildDeps",false) == true)
-                 std::cout << "  Is installed\n";
-
-              if (D->Version.empty() == true ||
-                    Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+              else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
               {
               {
-                 skipAlternatives = hasAlternatives;
-                 continue;
+                 if (colon == std::string::npos)
+                    Pkg = Ver.ParentPkg().Group().FindPkg("native");
+                 else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
+                       strcmp(D->Package.c_str() + colon, ":native") == 0)
+                    forbidden = "Multi-Arch: foreign";
               }
               }
-
-              if (_config->FindB("Debug::BuildDeps",false) == true)
-                 std::cout << "    ...but the installed version doesn't meet the version requirement\n";
-
-              if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
-                 return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
-                       Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
-           }
-
-           // Only consider virtual packages if there is no versioned dependency
-           if ((*D).Version.empty() == true)
-           {
-              /*
-               * If this is a virtual package, we need to check the list of
-               * packages that provide it and see if any of those are
-               * installed
-               */
-              pkgCache::PrvIterator Prv = Pkg.ProvidesList();
-              for (; Prv.end() != true; ++Prv)
+              else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
               {
               {
-                 if (_config->FindB("Debug::BuildDeps",false) == true)
-                    std::cout << "  Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
-
-                 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
-                    break;
+                 if (colon == std::string::npos)
+                    Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+                 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+                 {
+                    // prefer any installed over preferred non-installed architectures
+                    pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
+                    // we don't check for version here as we are better of with upgrading than remove and install
+                    for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+                       if (Pkg.CurrentVer().end() == false)
+                          break;
+                    if (Pkg.end() == true)
+                       Pkg = Grp.FindPreferredPkg(true);
+                 }
+                 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+                    Pkg = Ver.ParentPkg().Group().FindPkg("native");
               }
 
               }
 
-              if (Prv.end() == false)
+              if (forbidden.empty() == false)
               {
                  if (_config->FindB("Debug::BuildDeps",false) == true)
               {
                  if (_config->FindB("Debug::BuildDeps",false) == true)
-                    std::cout << "  Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
-                 skipAlternatives = hasAlternatives;
+                    std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
                  continue;
               }
                  continue;
               }
+
+              //we found a good version
+              break;
            }
            }
-           else // versioned dependency
+           if (Ver == verlist.end())
            {
            {
-              pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
-              if (CV.end() == true ||
-                    Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
+              if (_config->FindB("Debug::BuildDeps",false) == true)
+                 std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
+
+              if (forbidden.empty() == false)
               {
                  if (hasAlternatives)
                     continue;
               {
                  if (hasAlternatives)
                     continue;
-                 else if (CV.end() == false)
-                    return _error->Error(_("%s dependency for %s cannot be satisfied "
-                             "because candidate version of package %s "
-                             "can't satisfy version requirements"),
-                          Last->BuildDepType(D->Type), Src.c_str(),
-                          D->Package.c_str());
-                 else
-                    return _error->Error(_("%s dependency for %s cannot be satisfied "
-                             "because package %s has no candidate version"),
-                          Last->BuildDepType(D->Type), Src.c_str(),
-                          D->Package.c_str());
+                 return _error->Error(_("%s dependency for %s can't be satisfied "
+                          "because %s is not allowed on '%s' packages"),
+                       pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
+                       D->Package.c_str(), forbidden.c_str());
               }
            }
               }
            }
+        }
+        else
+           Pkg = Cache->FindPkg(D->Package);
 
 
-           if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
+        if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
+        {
+           if (_config->FindB("Debug::BuildDeps",false) == true)
+              std::cout << " (not found)" << (*D).Package << std::endl;
+
+           if (hasAlternatives)
+              continue;
+
+           return _error->Error(_("%s dependency for %s cannot be satisfied "
+                    "because the package %s cannot be found"),
+                 pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
+                 (*D).Package.c_str());
+        }
+
+        pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
+        if (IV.end() == false)
+        {
+           if (_config->FindB("Debug::BuildDeps",false) == true)
+              std::cout << "  Is installed\n";
+
+           if (D->Version.empty() == true ||
+                 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
            {
            {
-              // We successfully installed something; skip remaining alternatives
               skipAlternatives = hasAlternatives;
               skipAlternatives = hasAlternatives;
-              if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
-                 Cache->MarkAuto(Pkg, true);
               continue;
            }
               continue;
            }
-           else if (hasAlternatives)
+
+           if (_config->FindB("Debug::BuildDeps",false) == true)
+              std::cout << "    ...but the installed version doesn't meet the version requirement\n";
+
+           if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
+              return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
+                    pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(), Pkg.FullName(true).c_str());
+        }
+
+        // Only consider virtual packages if there is no versioned dependency
+        if ((*D).Version.empty() == true)
+        {
+           /*
+            * If this is a virtual package, we need to check the list of
+            * packages that provide it and see if any of those are
+            * installed
+            */
+           pkgCache::PrvIterator Prv = Pkg.ProvidesList();
+           for (; Prv.end() != true; ++Prv)
            {
               if (_config->FindB("Debug::BuildDeps",false) == true)
            {
               if (_config->FindB("Debug::BuildDeps",false) == true)
-                 std::cout << "  Unsatisfiable, trying alternatives\n";
+                 std::cout << "  Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
+
+              if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
+                 break;
+           }
+
+           if (Prv.end() == false)
+           {
+              if (_config->FindB("Debug::BuildDeps",false) == true)
+                 std::cout << "  Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
+              skipAlternatives = hasAlternatives;
               continue;
            }
               continue;
            }
-           else
+        }
+        else // versioned dependency
+        {
+           pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
+           if (CV.end() == true ||
+                 Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
            {
            {
-              return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
-                    Last->BuildDepType((*D).Type),
-                    Src.c_str(),
-                    (*D).Package.c_str());
+              if (hasAlternatives)
+                 continue;
+              else if (CV.end() == false)
+                 return _error->Error(_("%s dependency for %s cannot be satisfied "
+                          "because candidate version of package %s "
+                          "can't satisfy version requirements"),
+                       pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
+                       D->Package.c_str());
+              else
+                 return _error->Error(_("%s dependency for %s cannot be satisfied "
+                          "because package %s has no candidate version"),
+                       pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
+                       D->Package.c_str());
            }
         }
            }
         }
+
+        if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
+        {
+           // We successfully installed something; skip remaining alternatives
+           skipAlternatives = hasAlternatives;
+           if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
+              Cache->MarkAuto(Pkg, true);
+           continue;
+        }
+        else if (hasAlternatives)
+        {
+           if (_config->FindB("Debug::BuildDeps",false) == true)
+              std::cout << "  Unsatisfiable, trying alternatives\n";
+           continue;
+        }
+        else
+        {
+           return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
+                 pkgSrcRecords::Parser::BuildDepType(D->Type),
+                 Src.c_str(),
+                 (*D).Package.c_str());
+        }
+      }
+   }
+
+   if (Fix.Resolve(true) == false)
+      _error->Discard();
+
+   // Now we check the state of the packages,
+   if (Cache->BrokenCount() != 0)
+   {
+      ShowBroken(std::cout, Cache, false);
+      return _error->Error(_("Build-dependencies for %s could not be satisfied."), Src.c_str());
+   }
+   return true;
+}
+                                                                       /*}}}*/
+// DoBuildDep - Install/removes packages to satisfy build dependencies  /*{{{*/
+// ---------------------------------------------------------------------
+/* This function will look at the build depends list of the given source 
+   package and install the necessary packages to make it true, or fail. */
+static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
+      char const * const Src, bool const StripMultiArch, std::string const &hostArch)
+{
+   std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
+   // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
+   if (hostArch.empty() == false)
+   {
+      std::string nativeArch = _config->Find("APT::Architecture");
+      _config->Set("APT::Architecture", hostArch);
+      bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
+      _config->Set("APT::Architecture", nativeArch);
+      if (Success == false)
+      {
+        _error->Error(_("Unable to get build-dependency information for %s"), Src);
+        return {};
+      }
+   }
+   else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
+   {
+      _error->Error(_("Unable to get build-dependency information for %s"), Src);
+      return {};
+   }
+
+   if (BuildDeps.empty() == true)
+      ioprintf(c1out,_("%s has no build depends.\n"), Src);
+
+   return BuildDeps;
+}
+bool DoBuildDep(CommandLine &CmdL)
+{
+   CacheFile Cache;
+   std::vector<char const *> VolatileCmdL;
+   Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
+
+   _config->Set("APT::Install-Recommends", false);
+
+   bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
+
+   if (Cache.Open(WantLock) == false)
+      return false;
+
+   if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
+      return _error->Error(_("Must specify at least one package to check builddeps for"));
+
+   // Read the source list
+   if (Cache.BuildSourceList() == false)
+      return false;
+   pkgSourceList *List = Cache.GetSourceList();
+
+   // Create the text record parsers
+   pkgSrcRecords SrcRecs(*List);
+   if (_error->PendingError() == true)
+      return false;
+
+   bool StripMultiArch;
+   std::string hostArch = _config->Find("APT::Get::Host-Architecture");
+   if (hostArch.empty() == false)
+   {
+      std::vector<std::string> archs = APT::Configuration::getArchitectures();
+      if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
+        return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
+      StripMultiArch = false;
+   }
+   else
+      StripMultiArch = true;
+
+   // deal with the build essentials first
+   {
+      std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
+
+      Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
+      if (Opts)
+        Opts = Opts->Child;
+      for (; Opts; Opts = Opts->Next)
+      {
+        if (Opts->Value.empty() == true)
+           continue;
+
+        pkgSrcRecords::Parser::BuildDepRec rec;
+        rec.Package = Opts->Value;
+        rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
+        rec.Op = 0;
+        BuildDeps.push_back(rec);
       }
 
       }
 
-      if (Fix.Resolve(true) == false)
-        _error->Discard();
+      if (InstallBuildDepsLoop(Cache, "APT::Build-Essential", BuildDeps, StripMultiArch, hostArch) == false)
+        return false;
+   }
 
 
-      // Now we check the state of the packages,
-      if (Cache->BrokenCount() != 0)
+   // FIXME: Avoid volatile sources == cmdline assumption
+   {
+      auto const VolatileSources = List->GetVolatileFiles();
+      if (VolatileSources.size() == VolatileCmdL.size())
       {
       {
-        ShowBroken(std::cout, Cache, false);
-        return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
+        for (size_t i = 0; i < VolatileSources.size(); ++i)
+        {
+           char const * const Src = VolatileCmdL[i];
+           if (DirectoryExists(Src))
+              ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src);
+           else
+              ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src);
+           std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
+           if (Last == nullptr)
+              return _error->Error(_("Unable to find a source package for %s"), Src);
+
+           auto const BuildDeps = GetBuildDeps(Last.get(), Src, StripMultiArch, hostArch);
+           if (InstallBuildDepsLoop(Cache, Src, BuildDeps, StripMultiArch, hostArch) == false)
+              return false;
+        }
       }
       }
+      else
+        _error->Error("Implementation error: Volatile sources (%lu) and commandline elements (%lu) do not match!", VolatileSources.size(), VolatileCmdL.size());
+   }
+
+   for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
+   {
+      std::string Src;
+      pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
+      if (Last == nullptr)
+        return _error->Error(_("Unable to find a source package for %s"), *I);
+
+      auto const BuildDeps = GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch);
+      if (InstallBuildDepsLoop(Cache, Src, BuildDeps, StripMultiArch, hostArch) == false)
+        return false;
    }
 
    if (InstallPackages(Cache, false, true) == false)
    }
 
    if (InstallPackages(Cache, false, true) == false)