]> git.saurik.com Git - apt.git/blob - apt-private/private-source.cc
default to --no-check for dpkg-source call
[apt.git] / apt-private / private-source.cc
1 // Include Files /*{{{*/
2 #include <config.h>
3
4 #include <apt-pkg/acquire-item.h>
5 #include <apt-pkg/acquire.h>
6 #include <apt-pkg/algorithms.h>
7 #include <apt-pkg/aptconfiguration.h>
8 #include <apt-pkg/cachefile.h>
9 #include <apt-pkg/cacheiterators.h>
10 #include <apt-pkg/cacheset.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/configuration.h>
13 #include <apt-pkg/depcache.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/hashes.h>
17 #include <apt-pkg/indexfile.h>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/srcrecords.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/version.h>
24 #include <apt-pkg/policy.h>
25
26 #include <apt-private/private-cachefile.h>
27 #include <apt-private/private-cacheset.h>
28 #include <apt-private/private-download.h>
29 #include <apt-private/private-install.h>
30 #include <apt-private/private-source.h>
31
32 #include <apt-pkg/debindexfile.h>
33
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include <iostream>
42 #include <sstream>
43 #include <set>
44 #include <string>
45 #include <vector>
46
47 #include <apti18n.h>
48 /*}}}*/
49
50 // GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
51 static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
52 pkgSourceList const * const SrcList, pkgSrcRecords::Parser const * const Parse)
53 {
54 // try to find release
55 const pkgIndexFile& CurrentIndexFile = Parse->Index();
56
57 for (pkgSourceList::const_iterator S = SrcList->begin();
58 S != SrcList->end(); ++S)
59 {
60 std::vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
61 for (std::vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
62 IF != Indexes->end(); ++IF)
63 {
64 if (&CurrentIndexFile == (*IF))
65 return (*S)->FindInCache(CacheFile, false);
66 }
67 }
68 return pkgCache::RlsFileIterator(CacheFile);
69 }
70 /*}}}*/
71 // FindSrc - Find a source record /*{{{*/
72 static pkgSrcRecords::Parser *FindSrc(const char *Name,
73 pkgSrcRecords &SrcRecs,std::string &Src,
74 CacheFile &Cache)
75 {
76 std::string VerTag, UserRequestedVerTag;
77 std::string ArchTag = "";
78 std::string RelTag = _config->Find("APT::Default-Release");
79 std::string TmpSrc = Name;
80
81 // extract release
82 size_t found = TmpSrc.find_last_of("/");
83 if (found != std::string::npos)
84 {
85 RelTag = TmpSrc.substr(found+1);
86 TmpSrc = TmpSrc.substr(0,found);
87 }
88 // extract the version
89 found = TmpSrc.find_last_of("=");
90 if (found != std::string::npos)
91 {
92 VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
93 TmpSrc = TmpSrc.substr(0,found);
94 }
95 // extract arch
96 found = TmpSrc.find_last_of(":");
97 if (found != std::string::npos)
98 {
99 ArchTag = TmpSrc.substr(found+1);
100 TmpSrc = TmpSrc.substr(0,found);
101 }
102
103 /* Lookup the version of the package we would install if we were to
104 install a version and determine the source package name, then look
105 in the archive for a source package of the same name. */
106 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
107 pkgCache::PkgIterator Pkg;
108 if (ArchTag != "")
109 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc, ArchTag);
110 else
111 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc);
112
113 // if we can't find a package but the user qualified with a arch,
114 // error out here
115 if (Pkg.end() && ArchTag != "")
116 {
117 Src = Name;
118 _error->Error(_("Can not find a package for architecture '%s'"),
119 ArchTag.c_str());
120 return 0;
121 }
122
123 if (MatchSrcOnly == false && Pkg.end() == false)
124 {
125 if(VerTag != "" || RelTag != "" || ArchTag != "")
126 {
127 bool fuzzy = false;
128 // we have a default release, try to locate the pkg. we do it like
129 // this because GetCandidateVer() will not "downgrade", that means
130 // "apt-get source -t stable apt" won't work on a unstable system
131 for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
132 {
133 // try first only exact matches, later fuzzy matches
134 if (Ver.end() == true)
135 {
136 if (fuzzy == true)
137 break;
138 fuzzy = true;
139 Ver = Pkg.VersionList();
140 // exit right away from the Pkg.VersionList() loop if we
141 // don't have any versions
142 if (Ver.end() == true)
143 break;
144 }
145
146 // ignore arches that are not for us
147 if (ArchTag != "" && Ver.Arch() != ArchTag)
148 continue;
149
150 // pick highest version for the arch unless the user wants
151 // something else
152 if (ArchTag != "" && VerTag == "" && RelTag == "")
153 if(Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver.VerStr()) < 0)
154 VerTag = Ver.VerStr();
155
156 // We match against a concrete version (or a part of this version)
157 if (VerTag.empty() == false &&
158 (fuzzy == true || Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
159 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
160 continue;
161
162 for (pkgCache::VerFileIterator VF = Ver.FileList();
163 VF.end() == false; ++VF)
164 {
165 /* If this is the status file, and the current version is not the
166 version in the status file (ie it is not installed, or somesuch)
167 then it is not a candidate for installation, ever. This weeds
168 out bogus entries that may be due to config-file states, or
169 other. */
170 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
171 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
172 continue;
173
174 // or we match against a release
175 if(VerTag.empty() == false ||
176 (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
177 (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
178 {
179 // the Version we have is possibly fuzzy or includes binUploads,
180 // so we use the Version of the SourcePkg (empty if same as package)
181 Src = Ver.SourcePkgName();
182 VerTag = Ver.SourceVerStr();
183 break;
184 }
185 }
186 if (Src.empty() == false)
187 break;
188 }
189 }
190
191 if (Src.empty() == true && ArchTag.empty() == false)
192 {
193 if (VerTag.empty() == false)
194 _error->Error(_("Can not find a package '%s' with version '%s'"),
195 Pkg.FullName().c_str(), VerTag.c_str());
196 if (RelTag.empty() == false)
197 _error->Error(_("Can not find a package '%s' with release '%s'"),
198 Pkg.FullName().c_str(), RelTag.c_str());
199 Src = Name;
200 return 0;
201 }
202
203
204 if (Src.empty() == true)
205 {
206 // if we don't have found a fitting package yet so we will
207 // choose a good candidate and proceed with that.
208 // Maybe we will find a source later on with the right VerTag
209 // or RelTag
210 if (Cache.BuildPolicy() == false)
211 return nullptr;
212 pkgPolicy * Policy = dynamic_cast<pkgPolicy*>(Cache.GetPolicy());
213 if (Policy == nullptr)
214 {
215 _error->Fatal("Implementation error: dynamic up-casting policy engine failed in FindSrc!");
216 return nullptr;
217 }
218 pkgCache::VerIterator const Ver = Policy->GetCandidateVer(Pkg);
219 if (Ver.end() == false)
220 {
221 if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
222 Src = Ver.SourcePkgName();
223 if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
224 VerTag = Ver.SourceVerStr();
225 }
226 }
227 }
228
229 if (Src.empty() == true)
230 {
231 Src = TmpSrc;
232 }
233 else
234 {
235 /* if we have a source pkg name, make sure to only search
236 for srcpkg names, otherwise apt gets confused if there
237 is a binary package "pkg1" and a source package "pkg1"
238 with the same name but that comes from different packages */
239 MatchSrcOnly = true;
240 if (Src != TmpSrc)
241 {
242 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
243 }
244 }
245
246 // The best hit
247 pkgSrcRecords::Parser *Last = 0;
248 unsigned long Offset = 0;
249 std::string Version;
250 pkgSourceList const * const SrcList = Cache.GetSourceList();
251
252 /* Iterate over all of the hits, which includes the resulting
253 binary packages in the search */
254 pkgSrcRecords::Parser *Parse;
255 while (true)
256 {
257 SrcRecs.Restart();
258 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
259 {
260 const std::string Ver = Parse->Version();
261
262 // See if we need to look for a specific release tag
263 if (RelTag.empty() == false && UserRequestedVerTag.empty() == true)
264 {
265 pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(Cache, SrcList, Parse);
266 if (Rls.end() == false)
267 {
268 if ((Rls->Archive != 0 && RelTag != Rls.Archive()) &&
269 (Rls->Codename != 0 && RelTag != Rls.Codename()))
270 continue;
271 }
272 }
273
274 // Ignore all versions which doesn't fit
275 if (VerTag.empty() == false &&
276 Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver) != 0) // exact match
277 continue;
278
279 // Newer version or an exact match? Save the hit
280 if (Last == 0 || Cache.GetPkgCache()->VS->CmpVersion(Version,Ver) < 0) {
281 Last = Parse;
282 Offset = Parse->Offset();
283 Version = Ver;
284 }
285
286 // was the version check above an exact match?
287 // If so, we don't need to look further
288 if (VerTag.empty() == false && (VerTag == Ver))
289 break;
290 }
291 if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
292 ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
293 Version.c_str(), RelTag.c_str(), Src.c_str());
294
295 if (Last != 0 || VerTag.empty() == true)
296 break;
297 _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
298 return 0;
299 }
300
301 if (Last == 0 || Last->Jump(Offset) == false)
302 return 0;
303
304 return Last;
305 }
306 /*}}}*/
307 // DoSource - Fetch a source archive /*{{{*/
308 // ---------------------------------------------------------------------
309 /* Fetch souce packages */
310 struct DscFile
311 {
312 std::string Package;
313 std::string Version;
314 std::string Dsc;
315 };
316 bool DoSource(CommandLine &CmdL)
317 {
318 if (CmdL.FileSize() <= 1)
319 return _error->Error(_("Must specify at least one package to fetch source for"));
320
321 CacheFile Cache;
322 if (Cache.BuildCaches(false) == false)
323 return false;
324
325 // Create the text record parsers
326 pkgSourceList * const List = Cache.GetSourceList();
327 pkgSrcRecords SrcRecs(*List);
328 if (_error->PendingError() == true)
329 return false;
330
331 std::vector<DscFile> Dsc;
332 Dsc.reserve(CmdL.FileSize());
333
334 // insert all downloaded uris into this set to avoid downloading them
335 // twice
336 std::set<std::string> queued;
337
338 // Diff only mode only fetches .diff files
339 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
340 // Tar only mode only fetches .tar files
341 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
342 // Dsc only mode only fetches .dsc files
343 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
344
345 // Load the requestd sources into the fetcher
346 aptAcquireWithTextStatus Fetcher;
347 std::vector<std::string> UntrustedList;
348 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
349 {
350 std::string Src;
351 pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
352 if (Last == 0) {
353 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
354 }
355
356 if (Last->Index().IsTrusted() == false)
357 UntrustedList.push_back(Src);
358
359 std::string srec = Last->AsStr();
360 std::string::size_type pos = srec.find("\nVcs-");
361 while (pos != std::string::npos)
362 {
363 pos += strlen("\nVcs-");
364 std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
365 if(vcs == "Browser")
366 {
367 pos = srec.find("\nVcs-", pos);
368 continue;
369 }
370 pos += vcs.length()+2;
371 std::string::size_type epos = srec.find("\n", pos);
372 std::string const uri = srec.substr(pos,epos-pos);
373 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
374 "the '%s' version control system at:\n"
375 "%s\n"),
376 Src.c_str(), vcs.c_str(), uri.c_str());
377 std::string vcscmd;
378 if (vcs == "Bzr")
379 vcscmd = "bzr branch " + uri;
380 else if (vcs == "Git")
381 vcscmd = "git clone " + uri;
382
383 if (vcscmd.empty() == false)
384 ioprintf(c1out,_("Please use:\n%s\n"
385 "to retrieve the latest (possibly unreleased) "
386 "updates to the package.\n"),
387 vcscmd.c_str());
388 break;
389 }
390
391 // Back track
392 std::vector<pkgSrcRecords::File2> Lst;
393 if (Last->Files2(Lst) == false) {
394 return false;
395 }
396
397 DscFile curDsc;
398 // Load them into the fetcher
399 for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
400 I != Lst.end(); ++I)
401 {
402 // Try to guess what sort of file it is we are getting.
403 if (I->Type == "dsc")
404 {
405 curDsc.Package = Last->Package();
406 curDsc.Version = Last->Version();
407 curDsc.Dsc = flNotDir(I->Path);
408 }
409
410 // Handle the only options so that multiple can be used at once
411 if (diffOnly == true || tarOnly == true || dscOnly == true)
412 {
413 if ((diffOnly == true && I->Type == "diff") ||
414 (tarOnly == true && I->Type == "tar") ||
415 (dscOnly == true && I->Type == "dsc"))
416 ; // Fine, we want this file downloaded
417 else
418 continue;
419 }
420
421 // don't download the same uri twice (should this be moved to
422 // the fetcher interface itself?)
423 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
424 continue;
425 queued.insert(Last->Index().ArchiveURI(I->Path));
426
427 // check if we have a file with that md5 sum already localy
428 std::string localFile = flNotDir(I->Path);
429 if (FileExists(localFile) == true)
430 if(I->Hashes.VerifyFile(localFile) == true)
431 {
432 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
433 localFile.c_str());
434 continue;
435 }
436
437 // see if we have a hash (Acquire::ForceHash is the only way to have none)
438 if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
439 {
440 ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
441 localFile.c_str());
442 curDsc.Dsc.clear();
443 continue;
444 }
445
446 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
447 I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
448 }
449 Dsc.push_back(std::move(curDsc));
450 }
451
452 // Display statistics
453 unsigned long long FetchBytes = Fetcher.FetchNeeded();
454 unsigned long long FetchPBytes = Fetcher.PartialPresent();
455 unsigned long long DebBytes = Fetcher.TotalNeeded();
456
457 if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
458 return false;
459
460 // Number of bytes
461 if (DebBytes != FetchBytes)
462 //TRANSLATOR: The required space between number and unit is already included
463 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
464 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
465 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
466 else
467 //TRANSLATOR: The required space between number and unit is already included
468 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
469 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
470 SizeToStr(DebBytes).c_str());
471
472 if (_config->FindB("APT::Get::Simulate",false) == true)
473 {
474 for (auto const &D: Dsc)
475 ioprintf(std::cout, _("Fetch source %s\n"), D.Package.c_str());
476 return true;
477 }
478
479 // Just print out the uris an exit if the --print-uris flag was used
480 if (_config->FindB("APT::Get::Print-URIs") == true)
481 {
482 pkgAcquire::UriIterator I = Fetcher.UriBegin();
483 for (; I != Fetcher.UriEnd(); ++I)
484 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
485 std::to_string(I->Owner->FileSize) << ' ' << I->Owner->HashSum() << std::endl;
486 return true;
487 }
488
489 // check authentication status of the source as well
490 if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
491 return false;
492
493 // Run it
494 bool Failed = false;
495 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
496 return _error->Error(_("Failed to fetch some archives."));
497
498 if (diffOnly || tarOnly || dscOnly || _config->FindB("APT::Get::Download-only",false) == true)
499 {
500 c1out << _("Download complete and in download only mode") << std::endl;
501 return true;
502 }
503
504 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
505 bool SaidCheckIfDpkgDev = false;
506 for (auto const &D: Dsc)
507 {
508 if (unlikely(D.Dsc.empty() == true))
509 continue;
510 std::string const Dir = D.Package + '-' + Cache.GetPkgCache()->VS->UpstreamVersion(D.Version.c_str());
511
512 // See if the package is already unpacked
513 struct stat Stat;
514 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
515 S_ISDIR(Stat.st_mode) != 0)
516 {
517 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
518 Dir.c_str());
519 }
520 else
521 {
522 // Call dpkg-source
523 std::string const sourceopts = _config->Find("DPkg::Source-Options", "--no-check -x");
524 std::string S;
525 strprintf(S, "%s %s %s",
526 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
527 sourceopts.c_str(), D.Dsc.c_str());
528 if (system(S.c_str()) != 0)
529 {
530 _error->Error(_("Unpack command '%s' failed.\n"), S.c_str());
531 if (SaidCheckIfDpkgDev == false)
532 {
533 _error->Notice(_("Check if the 'dpkg-dev' package is installed.\n"));
534 SaidCheckIfDpkgDev = true;
535 }
536 continue;
537 }
538 }
539
540 // Try to compile it with dpkg-buildpackage
541 if (_config->FindB("APT::Get::Compile",false) == true)
542 {
543 std::string buildopts = _config->Find("APT::Get::Host-Architecture");
544 if (buildopts.empty() == false)
545 buildopts = "-a" + buildopts + " ";
546
547 // get all active build profiles
548 std::string const profiles = APT::Configuration::getBuildProfilesString();
549 if (profiles.empty() == false)
550 buildopts.append(" -P").append(profiles).append(" ");
551
552 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
553
554 // Call dpkg-buildpackage
555 std::string S;
556 strprintf(S, "cd %s && %s %s",
557 Dir.c_str(),
558 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
559 buildopts.c_str());
560
561 if (system(S.c_str()) != 0)
562 {
563 _error->Error(_("Build command '%s' failed.\n"), S.c_str());
564 continue;
565 }
566 }
567 }
568 return true;
569 }
570 /*}}}*/
571 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
572 // ---------------------------------------------------------------------
573 /* This function will look at the build depends list of the given source
574 package and install the necessary packages to make it true, or fail. */
575 static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
576 char const * const Src, bool const StripMultiArch, std::string const &hostArch)
577 {
578 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
579 // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
580 if (hostArch.empty() == false)
581 {
582 std::string nativeArch = _config->Find("APT::Architecture");
583 _config->Set("APT::Architecture", hostArch);
584 bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
585 _config->Set("APT::Architecture", nativeArch);
586 if (Success == false)
587 {
588 _error->Error(_("Unable to get build-dependency information for %s"), Src);
589 return {};
590 }
591 }
592 else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
593 {
594 _error->Error(_("Unable to get build-dependency information for %s"), Src);
595 return {};
596 }
597
598 if (BuildDeps.empty() == true)
599 ioprintf(c1out,_("%s has no build depends.\n"), Src);
600
601 return BuildDeps;
602 }
603 static void WriteBuildDependencyPackage(std::ostringstream &buildDepsPkgFile,
604 std::string const &PkgName, std::string const &Arch,
605 std::vector<pkgSrcRecords::Parser::BuildDepRec> const &Dependencies)
606 {
607 buildDepsPkgFile << "Package: " << PkgName << "\n"
608 << "Architecture: " << Arch << "\n"
609 << "Version: 1\n";
610
611 std::string depends, conflicts;
612 for (auto const &dep: Dependencies)
613 {
614 std::string * type;
615 if (dep.Type == pkgSrcRecords::Parser::BuildConflict ||
616 dep.Type == pkgSrcRecords::Parser::BuildConflictIndep ||
617 dep.Type == pkgSrcRecords::Parser::BuildConflictArch)
618 type = &conflicts;
619 else
620 type = &depends;
621
622 type->append(" ").append(dep.Package);
623 if (dep.Version.empty() == false)
624 type->append(" (").append(pkgCache::CompTypeDeb(dep.Op)).append(" ").append(dep.Version).append(")");
625 if ((dep.Op & pkgCache::Dep::Or) == pkgCache::Dep::Or)
626 {
627 type->append("\n |");
628 }
629 else
630 type->append(",\n");
631 }
632 if (depends.empty() == false)
633 buildDepsPkgFile << "Depends:\n" << depends;
634 if (conflicts.empty() == false)
635 buildDepsPkgFile << "Conflicts:\n" << conflicts;
636 buildDepsPkgFile << "\n";
637 }
638 bool DoBuildDep(CommandLine &CmdL)
639 {
640 CacheFile Cache;
641 std::vector<std::string> VolatileCmdL;
642 Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
643
644 _config->Set("APT::Install-Recommends", false);
645
646 if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
647 return _error->Error(_("Must specify at least one package to check builddeps for"));
648
649 bool StripMultiArch;
650 std::string hostArch = _config->Find("APT::Get::Host-Architecture");
651 if (hostArch.empty() == false)
652 {
653 std::vector<std::string> archs = APT::Configuration::getArchitectures();
654 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
655 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
656 StripMultiArch = false;
657 }
658 else
659 StripMultiArch = true;
660
661 std::ostringstream buildDepsPkgFile;
662 std::vector<std::pair<std::string,std::string>> pseudoPkgs;
663 // deal with the build essentials first
664 {
665 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
666 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
667 if (Opts)
668 Opts = Opts->Child;
669 for (; Opts; Opts = Opts->Next)
670 {
671 if (Opts->Value.empty() == true)
672 continue;
673
674 pkgSrcRecords::Parser::BuildDepRec rec;
675 rec.Package = Opts->Value;
676 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
677 rec.Op = 0;
678 BuildDeps.push_back(rec);
679 }
680 std::string const pseudo = "builddeps:essentials";
681 std::string const nativeArch = _config->Find("APT::Architecture");
682 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, nativeArch, BuildDeps);
683 pseudoPkgs.emplace_back(pseudo, nativeArch);
684 }
685
686 // Read the source list
687 if (Cache.BuildSourceList() == false)
688 return false;
689 pkgSourceList *List = Cache.GetSourceList();
690 std::string const pseudoArch = hostArch.empty() ? _config->Find("APT::Architecture") : hostArch;
691
692 // FIXME: Avoid volatile sources == cmdline assumption
693 {
694 auto const VolatileSources = List->GetVolatileFiles();
695 if (VolatileSources.size() == VolatileCmdL.size())
696 {
697 for (size_t i = 0; i < VolatileSources.size(); ++i)
698 {
699 auto const Src = VolatileCmdL[i];
700 if (DirectoryExists(Src))
701 ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src.c_str());
702 else
703 ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src.c_str());
704 std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
705 if (Last == nullptr)
706 return _error->Error(_("Unable to find a source package for %s"), Src.c_str());
707
708 std::string const pseudo = std::string("builddeps:") + Src;
709 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
710 GetBuildDeps(Last.get(), Src.c_str(), StripMultiArch, hostArch));
711 pseudoPkgs.emplace_back(pseudo, pseudoArch);
712 }
713 }
714 else
715 return _error->Error("Implementation error: Volatile sources (%lu) and"
716 "commandline elements (%lu) do not match!", VolatileSources.size(),
717 VolatileCmdL.size());
718 }
719
720 bool const WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
721 if (CmdL.FileList[1] != 0)
722 {
723 if (Cache.BuildCaches(WantLock) == false)
724 return false;
725 // Create the text record parsers
726 pkgSrcRecords SrcRecs(*List);
727 if (_error->PendingError() == true)
728 return false;
729 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
730 {
731 std::string Src;
732 pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
733 if (Last == nullptr)
734 return _error->Error(_("Unable to find a source package for %s"), *I);
735
736 std::string const pseudo = std::string("builddeps:") + Src;
737 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
738 GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch));
739 pseudoPkgs.emplace_back(pseudo, pseudoArch);
740 }
741 }
742
743 Cache.AddIndexFile(new debStringPackageIndex(buildDepsPkgFile.str()));
744
745 if (Cache.Open(WantLock) == false)
746 return false;
747 pkgProblemResolver Fix(Cache.GetDepCache());
748
749 APT::PackageVector removeAgain;
750 {
751 pkgDepCache::ActionGroup group(Cache);
752 TryToInstall InstallAction(Cache, &Fix, false);
753 for (auto const &pkg: pseudoPkgs)
754 {
755 pkgCache::PkgIterator const Pkg = Cache->FindPkg(pkg.first, pkg.second);
756 if (Pkg.end())
757 continue;
758 Cache->SetCandidateVersion(Pkg.VersionList());
759 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
760 removeAgain.push_back(Pkg);
761 }
762 InstallAction.doAutoInstall();
763
764 OpTextProgress Progress(*_config);
765 bool const resolver_fail = Fix.Resolve(true, &Progress);
766 if (resolver_fail == false && Cache->BrokenCount() == 0)
767 return false;
768 if (CheckNothingBroken(Cache) == false)
769 return false;
770 }
771 if (DoAutomaticRemove(Cache) == false)
772 return false;
773
774 {
775 pkgDepCache::ActionGroup group(Cache);
776 if (_config->FindB("APT::Get::Build-Dep-Automatic", false) == false)
777 {
778 for (auto const &pkg: removeAgain)
779 {
780 auto const instVer = Cache[pkg].InstVerIter(Cache);
781 if (unlikely(instVer.end() == true))
782 continue;
783 for (auto D = instVer.DependsList(); D.end() != true; ++D)
784 {
785 if (D->Type != pkgCache::Dep::Depends || D.IsMultiArchImplicit())
786 continue;
787 APT::VersionList verlist = APT::VersionList::FromDependency(Cache, D, APT::CacheSetHelper::CANDIDATE);
788 for (auto const &V : verlist)
789 {
790 auto const P = V.ParentPkg();
791 if (Cache[P].InstallVer != V)
792 continue;
793 Cache->MarkAuto(P, false);
794 }
795 }
796 }
797 }
798 for (auto const &pkg: removeAgain)
799 Cache->MarkDelete(pkg, false, 0, true);
800 }
801
802 pseudoPkgs.clear();
803 if (_error->PendingError() || InstallPackages(Cache, false, true) == false)
804 return _error->Error(_("Failed to process build dependencies"));
805 return true;
806 }
807 /*}}}*/