implement MultiarchCross for build-dep and source (Closes: #632221)
[ntk/apt.git] / cmdline / apt-get.cc
index b3ce54b..8eda293 100644 (file)
@@ -2620,12 +2620,17 @@ bool DoSource(CommandLine &CmdL)
         // Try to compile it with dpkg-buildpackage
         if (_config->FindB("APT::Get::Compile",false) == true)
         {
+           string buildopts = _config->Find("APT::Get::Host-Architecture");
+           if (buildopts.empty() == false)
+              buildopts = "-a " + buildopts + " ";
+           buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
+
            // Call dpkg-buildpackage
            char S[500];
            snprintf(S,sizeof(S),"cd %s && %s %s",
                     Dir.c_str(),
                     _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
-                    _config->Find("DPkg::Build-Options","-b -uc").c_str());
+                    buildopts.c_str());
            
            if (system(S) != 0)
            {
@@ -2687,8 +2692,19 @@ bool DoBuildDep(CommandLine &CmdL)
    if (Fetcher.Setup(&Stat) == false)
       return false;
 
+   bool StripMultiArch;
+   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;
-   bool const StripMultiArch = APT::Configuration::getArchitectures().size() <= 1;
    for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
    {
       string Src;
@@ -2722,7 +2738,7 @@ bool DoBuildDep(CommandLine &CmdL)
         ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
         continue;
       }
-      
+
       // Install the requested packages
       vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
       pkgProblemResolver Fix(Cache);
@@ -2773,7 +2789,95 @@ bool DoBuildDep(CommandLine &CmdL)
             if (_config->FindB("Debug::BuildDeps",false) == true)
                  cout << "Looking for " << (*D).Package << "...\n";
 
-           pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
+           pkgCache::PkgIterator Pkg;
+
+           // Cross-Building?
+           if (StripMultiArch == false)
+           {
+              size_t const colon = D->Package.find(":");
+              if (colon != string::npos &&
+                  (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);
+
+              // We need to decide if host or build arch, so find a version we can look at
+              pkgCache::VerIterator Ver;
+
+              // a bad version either is invalid or doesn't satify dependency
+              #define BADVER(Ver) Ver.end() == true || \
+                                  (Ver.end() == false && D->Version.empty() == false && \
+                                   Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false)
+
+              if (Pkg.end() == false)
+              {
+                 Ver = (*Cache)[Pkg].InstVerIter(*Cache);
+                 if (BADVER(Ver))
+                    Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
+              }
+              if (BADVER(Ver))
+              {
+                 pkgCache::PkgIterator HostPkg = Cache->FindPkg(D->Package, hostArch);
+                 if (HostPkg.end() == false)
+                 {
+                    Ver = (*Cache)[HostPkg].InstVerIter(*Cache);
+                    if (BADVER(Ver))
+                       Ver = (*Cache)[HostPkg].CandidateVerIter(*Cache);
+                 }
+              }
+              if ((BADVER(Ver)) == false)
+              {
+                 string forbidden;
+                 if (Ver->MultiArch == pkgCache::Version::None || Ver->MultiArch == pkgCache::Version::All);
+                 else if (Ver->MultiArch == pkgCache::Version::Same)
+                 {
+                    if (colon != string::npos)
+                       Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+                    else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+                       forbidden = "Multi-Arch: same";
+                    // :native gets the buildArch
+                 }
+                 else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
+                 {
+                    if (colon != string::npos)
+                       forbidden = "Multi-Arch: foreign";
+                 }
+                 else if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
+                 {
+                    if (colon == 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);
+                    }
+                    // native gets buildArch
+                 }
+                 if (forbidden.empty() == false)
+                 {
+                    if (_config->FindB("Debug::BuildDeps",false) == true)
+                       cout << " :any is not allowed from M-A: same package " << (*D).Package << endl;
+                    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(), "Multi-Arch: same");
+                 }
+              }
+              else if (_config->FindB("Debug::BuildDeps",false) == true)
+                 cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
+              #undef BADVER
+           }
+           else
+              Pkg = Cache->FindPkg(D->Package);
+
            if (Pkg.end() == true)
             {
                if (_config->FindB("Debug::BuildDeps",false) == true)
@@ -3241,6 +3345,7 @@ int main(int argc,const char *argv[])                                     /*{{{*/
       {'m',"ignore-missing","APT::Get::Fix-Missing",0},
       {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
       {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
+      {'a',"host-architecture","APT::Get::Host-Architecture",CommandLine::HasArg},
       {0,"download","APT::Get::Download",0},
       {0,"fix-missing","APT::Get::Fix-Missing",0},
       {0,"ignore-hold","APT::Ignore-Hold",0},