1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
28 #include <sys/resource.h>
29 #include <mach/mach.h>
30 #include <mach/mach_time.h>
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <sys/resource.h>
54 #include <unordered_set>
64 #include "Diagnostics.h"
65 #include "DyldSharedCache.h"
66 #include "BuilderUtils.h"
67 #include "FileUtils.h"
68 #include "JSONReader.h"
69 #include "JSONWriter.h"
70 #include "StringUtils.h"
71 #include "mrm_shared_cache_builder.h"
73 #if !__has_feature(objc_arc)
74 #error The use of libdispatch in this files requires it to be compiled with ARC in order to avoid leaks
77 extern char** environ;
79 static dispatch_queue_t build_queue;
81 int runCommandAndWait(Diagnostics& diags, const char* args[])
85 int res = posix_spawn(&pid, args[0], nullptr, nullptr, (char**)args, environ);
87 diags.error("Failed to spawn %s: %s (%d)", args[0], strerror(res), res);
90 res = waitpid(pid, &status, 0);
91 } while (res == -1 && errno == EINTR);
93 if (WIFEXITED(status)) {
94 res = WEXITSTATUS(status);
103 void processRoots(Diagnostics& diags, std::set<std::string>& roots, const char *tempRootsDir)
105 std::set<std::string> processedRoots;
110 for (const auto& root : roots) {
111 res = stat(root.c_str(), &sb);
113 if (res == 0 && S_ISDIR(sb.st_mode)) {
114 processedRoots.insert(root);
118 char tempRootDir[MAXPATHLEN];
119 strlcpy(tempRootDir, tempRootsDir, MAXPATHLEN);
120 strlcat(tempRootDir, "/XXXXXXXX", MAXPATHLEN);
121 mkdtemp(tempRootDir);
123 if (endsWith(root, ".cpio") || endsWith(root, ".cpio.gz") || endsWith(root, ".cpgz") || endsWith(root, ".cpio.bz2") || endsWith(root, ".cpbz2") || endsWith(root, ".pax") || endsWith(root, ".pax.gz") || endsWith(root, ".pgz") || endsWith(root, ".pax.bz2") || endsWith(root, ".pbz2")) {
124 args[0] = (char*)"/usr/bin/ditto";
125 args[1] = (char*)"-x";
126 args[2] = (char*)root.c_str();
127 args[3] = tempRootDir;
129 } else if (endsWith(root, ".tar")) {
130 args[0] = (char*)"/usr/bin/tar";
131 args[1] = (char*)"xf";
132 args[2] = (char*)root.c_str();
133 args[3] = (char*)"-C";
134 args[4] = tempRootDir;
136 } else if (endsWith(root, ".tar.gz") || endsWith(root, ".tgz")) {
137 args[0] = (char*)"/usr/bin/tar";
138 args[1] = (char*)"xzf";
139 args[2] = (char*)root.c_str();
140 args[3] = (char*)"-C";
141 args[4] = tempRootDir;
143 } else if (endsWith(root, ".tar.bz2")
144 || endsWith(root, ".tbz2")
145 || endsWith(root, ".tbz")) {
146 args[0] = (char*)"/usr/bin/tar";
147 args[1] = (char*)"xjf";
148 args[2] = (char*)root.c_str();
149 args[3] = (char*)"-C";
150 args[4] = tempRootDir;
152 } else if (endsWith(root, ".xar")) {
153 args[0] = (char*)"/usr/bin/xar";
154 args[1] = (char*)"-xf";
155 args[2] = (char*)root.c_str();
156 args[3] = (char*)"-C";
157 args[4] = tempRootDir;
159 } else if (endsWith(root, ".zip")) {
160 args[0] = (char*)"/usr/bin/ditto";
161 args[1] = (char*)"-xk";
162 args[2] = (char*)root.c_str();
163 args[3] = tempRootDir;
166 diags.error("unknown archive type: %s", root.c_str());
170 if (res != runCommandAndWait(diags, args)) {
171 fprintf(stderr, "Could not expand archive %s: %s (%d)", root.c_str(), strerror(res), res);
174 for (auto& existingRoot : processedRoots) {
175 if (existingRoot == tempRootDir)
179 processedRoots.insert(tempRootDir);
182 roots = processedRoots;
185 void writeRootList(const std::string& dstRoot, const std::set<std::string>& roots)
187 if (roots.size() == 0)
190 std::string rootFile = dstRoot + "/roots.txt";
191 FILE* froots = ::fopen(rootFile.c_str(), "w");
195 for (auto& root : roots) {
196 fprintf(froots, "%s\n", root.c_str());
202 BOMCopierCopyOperation filteredCopyExcludingPaths(BOMCopier copier, const char* path, BOMFSObjType type, off_t size)
204 std::string absolutePath = &path[1];
205 void *userData = BOMCopierUserData(copier);
206 std::set<std::string> *cachePaths = (std::set<std::string>*)userData;
207 if (cachePaths->count(absolutePath)) {
208 return BOMCopierSkipFile;
210 return BOMCopierContinue;
213 BOMCopierCopyOperation filteredCopyIncludingPaths(BOMCopier copier, const char* path, BOMFSObjType type, off_t size)
215 std::string absolutePath = &path[1];
216 void *userData = BOMCopierUserData(copier);
217 std::set<std::string> *cachePaths = (std::set<std::string>*)userData;
218 for (const std::string& cachePath : *cachePaths) {
219 if (startsWith(cachePath, absolutePath))
220 return BOMCopierContinue;
222 if (cachePaths->count(absolutePath)) {
223 return BOMCopierContinue;
225 return BOMCopierSkipFile;
228 static std::string dispositionToString(Disposition disposition) {
229 switch (disposition) {
232 case InternalDevelopment:
233 return "InternalDevelopment";
236 case InternalMinDevelopment:
237 return "InternalMinDevelopment";
241 static Disposition stringToDisposition(Diagnostics& diags, const std::string& str) {
242 if (diags.hasError())
244 if (str == "Unknown")
246 if (str == "InternalDevelopment")
247 return InternalDevelopment;
248 if (str == "Customer")
250 if (str == "InternalMinDevelopment")
251 return InternalMinDevelopment;
255 static std::string platformToString(Platform platform) {
270 return "UIKitForMac";
272 return "iOS_simulator";
274 return "tvOS_simulator";
275 case watchOS_simulator:
276 return "watchOS_simulator";
280 static Platform stringToPlatform(Diagnostics& diags, const std::string& str) {
281 if (diags.hasError())
283 if (str == "unknown")
291 if (str == "watchOS")
293 if (str == "bridgeOS")
297 if (str == "UIKitForMac")
299 if (str == "iOS_simulator")
300 return iOS_simulator;
301 if (str == "tvOS_simulator")
302 return tvOS_simulator;
303 if (str == "watchOS_simulator")
304 return watchOS_simulator;
308 static std::string fileFlagsToString(FileFlags fileFlags) {
313 return "MustBeInCache";
314 case ShouldBeExcludedFromCacheIfUnusedLeaf:
315 return "ShouldBeExcludedFromCacheIfUnusedLeaf";
316 case RequiredClosure:
317 return "RequiredClosure";
319 return "DylibOrderFile";
320 case DirtyDataOrderFile:
321 return "DirtyDataOrderFile";
325 static FileFlags stringToFileFlags(Diagnostics& diags, const std::string& str) {
326 if (diags.hasError())
328 if (str == "NoFlags")
330 if (str == "MustBeInCache")
331 return MustBeInCache;
332 if (str == "ShouldBeExcludedFromCacheIfUnusedLeaf")
333 return ShouldBeExcludedFromCacheIfUnusedLeaf;
334 if (str == "RequiredClosure")
335 return RequiredClosure;
336 if (str == "DylibOrderFile")
337 return DylibOrderFile;
338 if (str == "DirtyDataOrderFile")
339 return DirtyDataOrderFile;
343 static dyld3::json::Node getBuildOptionsNode(BuildOptions_v1 buildOptions) {
344 dyld3::json::Node buildOptionsNode;
345 buildOptionsNode.map["version"].value = dyld3::json::decimal(buildOptions.version);
346 buildOptionsNode.map["updateName"].value = buildOptions.updateName;
347 buildOptionsNode.map["deviceName"].value = buildOptions.deviceName;
348 buildOptionsNode.map["disposition"].value = dispositionToString(buildOptions.disposition);
349 buildOptionsNode.map["platform"].value = platformToString(buildOptions.platform);
350 for (unsigned i = 0; i != buildOptions.numArchs; ++i) {
351 dyld3::json::Node archNode;
352 archNode.value = buildOptions.archs[i];
353 buildOptionsNode.map["archs"].array.push_back(archNode);
355 return buildOptionsNode;
358 struct SharedCacheBuilderOptions {
360 std::set<std::string> roots;
361 std::string dylibCacheDir;
362 std::string artifactDir;
364 bool emitDevCaches = true;
365 bool emitElidedDylibs = true;
366 bool listConfigs = false;
367 bool copyRoots = false;
371 std::string emitJSONPath;
372 std::string buildAllPath;
373 std::string configuration;
374 std::string resultPath;
375 std::string baselineDifferenceResultPath;
376 std::string baselineCacheMapPath;
377 bool baselineCopyRoots = false;
380 static void loadMRMFiles(Diagnostics& diags,
381 SharedCacheBuilder* sharedCacheBuilder,
382 const std::vector<std::tuple<std::string, std::string, FileFlags>>& inputFiles,
383 std::vector<std::pair<const void*, size_t>>& mappedFiles,
384 const std::set<std::string>& baselineCacheFiles) {
386 for (const std::tuple<std::string, std::string, FileFlags>& inputFile : inputFiles) {
387 const std::string& buildPath = std::get<0>(inputFile);
388 const std::string& runtimePath = std::get<1>(inputFile);
389 FileFlags fileFlags = std::get<2>(inputFile);
391 struct stat stat_buf;
392 int fd = ::open(buildPath.c_str(), O_RDONLY, 0);
394 if (baselineCacheFiles.count(runtimePath)) {
395 diags.error("can't open file '%s', errno=%d\n", buildPath.c_str(), errno);
398 diags.verbose("can't open file '%s', errno=%d\n", buildPath.c_str(), errno);
403 if (fstat(fd, &stat_buf) == -1) {
404 if (baselineCacheFiles.count(runtimePath)) {
405 diags.error("can't stat open file '%s', errno=%d\n", buildPath.c_str(), errno);
409 diags.verbose("can't stat open file '%s', errno=%d\n", buildPath.c_str(), errno);
415 const void* buffer = mmap(NULL, (size_t)stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
416 if (buffer == MAP_FAILED) {
417 diags.error("mmap() for file at %s failed, errno=%d\n", buildPath.c_str(), errno);
422 mappedFiles.emplace_back(buffer, (size_t)stat_buf.st_size);
424 addFile(sharedCacheBuilder, runtimePath.c_str(), (uint8_t*)buffer, (size_t)stat_buf.st_size, fileFlags);
428 static void unloadMRMFiles(std::vector<std::pair<const void*, size_t>>& mappedFiles) {
429 for (auto mappedFile : mappedFiles)
430 ::munmap((void*)mappedFile.first, mappedFile.second);
433 static void writeMRMResults(bool cacheBuildSuccess, SharedCacheBuilder* sharedCacheBuilder, const SharedCacheBuilderOptions& options) {
434 if (!cacheBuildSuccess) {
435 uint64_t errorCount = 0;
436 if (const char* const* errors = getErrors(sharedCacheBuilder, &errorCount)) {
437 for (uint64 i = 0, e = errorCount; i != e; ++i) {
438 const char* errorMessage = errors[i];
439 fprintf(stderr, "ERROR: %s\n", errorMessage);
444 // Now emit each cache we generated, or the errors for them.
445 uint64_t cacheResultCount = 0;
446 if (const CacheResult* const* cacheResults = getCacheResults(sharedCacheBuilder, &cacheResultCount)) {
447 for (uint64 i = 0, e = cacheResultCount; i != e; ++i) {
448 const CacheResult& result = *(cacheResults[i]);
449 // Always print the warnings if we have roots, even if there are errors
450 if ( (result.numErrors == 0) || !options.roots.empty() ) {
451 for (uint64_t warningIndex = 0; warningIndex != result.numWarnings; ++warningIndex) {
452 fprintf(stderr, "[%s] WARNING: %s\n", result.loggingPrefix, result.warnings[warningIndex]);
455 if (result.numErrors) {
456 for (uint64_t errorIndex = 0; errorIndex != result.numErrors; ++errorIndex) {
457 fprintf(stderr, "[%s] ERROR: %s\n", result.loggingPrefix, result.errors[errorIndex]);
459 cacheBuildSuccess = false;
464 if (!cacheBuildSuccess) {
468 // If we built caches, then write everything out.
469 // TODO: Decide if we should we write any good caches anyway?
470 if (cacheBuildSuccess && !options.dstRoot.empty()) {
471 uint64_t fileResultCount = 0;
472 if (const FileResult* const* fileResults = getFileResults(sharedCacheBuilder, &fileResultCount)) {
473 for (uint64 i = 0, e = fileResultCount; i != e; ++i) {
474 const FileResult& result = *(fileResults[i]);
476 switch (result.behavior) {
486 const std::string path = options.dstRoot + result.path;
487 std::string pathTemplate = path + "-XXXXXX";
488 size_t templateLen = strlen(pathTemplate.c_str())+2;
489 char pathTemplateSpace[templateLen];
490 strlcpy(pathTemplateSpace, pathTemplate.c_str(), templateLen);
491 int fd = mkstemp(pathTemplateSpace);
493 ::ftruncate(fd, result.size);
494 uint64_t writtenSize = pwrite(fd, result.data, result.size, 0);
495 if ( writtenSize == result.size ) {
496 ::fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); // mkstemp() makes file "rw-------", switch it to "rw-r--r--"
497 if ( ::rename(pathTemplateSpace, path.c_str()) == 0) {
503 fprintf(stderr, "ERROR: could not write file %s\n", pathTemplateSpace);
504 cacheBuildSuccess = false;
507 ::unlink(pathTemplateSpace);
510 fprintf(stderr, "ERROR: could not open file %s\n", pathTemplateSpace);
511 cacheBuildSuccess = false;
518 static void reportUnknownConfiguration(const std::string& configuration, dyld3::Manifest& manifest) {
519 fprintf(stderr, "** Unknown config '%s' for build %s.\n",
520 configuration.c_str(), manifest.build().c_str());
522 // Look for available configurations that the user might have meant.
523 // Substring match: print configs that contain the user's string.
524 // Regex match: if user wants "N61OS" then match .*N.*6.*1.*O.*S.*
526 std::string patternString = ".*";
527 for (auto c : configuration) {
528 if (isalnum(c)) { // filter regex special characters
530 patternString += ".*";
533 std::regex pattern(patternString);
535 std::vector<std::string> likelyConfigs{};
536 std::vector<std::string> allConfigs{};
537 manifest.forEachConfiguration([&](const std::string& configName) {
538 allConfigs.push_back(configName);
539 if (!configuration.empty()) {
540 if (configName.find(configuration) != std::string::npos || std::regex_match(configName, pattern)) {
541 likelyConfigs.push_back(configName);
546 if (!likelyConfigs.empty()) {
547 fprintf(stderr, "\nDid you mean:\n");
548 for (auto configName: likelyConfigs) {
549 fprintf(stderr, "%s\n", configName.c_str());
553 fprintf(stderr, "\nAvailable configurations:\n");
554 for (auto configName : allConfigs) {
555 fprintf(stderr, "%s\n", configName.c_str());
559 static void buildCacheFromPListManifest(Diagnostics& diags, const SharedCacheBuilderOptions& options) {
560 // Get the list of configurations, without fetching all of the files.
561 auto manifest = dyld3::Manifest(diags, options.dylibCacheDir + "/Manifest.plist", false);
563 if (manifest.build().empty()) {
564 fprintf(stderr, "No manifest found at '%s/Manifest.plist'\n", options.dylibCacheDir.c_str());
568 // List configurations if requested.
569 if (options.listConfigs) {
570 manifest.forEachConfiguration([](const std::string& configName) {
571 printf("%s\n", configName.c_str());
573 // If we weren't passed a configuration then exit
574 if (options.configuration.empty())
578 // Stop if the requested configuration is unavailable.
579 if (!manifest.filterForConfig(options.configuration)) {
580 reportUnknownConfiguration(options.configuration, manifest);
584 // Now finish initializing the manifest.
585 // This step is slow so we defer it until after the checks above.
586 fprintf(stderr, "Building Caches for %s\n", manifest.build().c_str());
587 manifest.populate(options.roots);
589 (void)mkpath_np((options.dstRoot + "/System/Library/Caches/com.apple.dyld/").c_str(), 0755);
590 bool cacheBuildSuccess = false;
591 if (options.useMRM) {
593 std::ofstream jsonFile;
594 if (!options.emitJSONPath.empty()) {
595 jsonFile.open(options.emitJSONPath, std::ofstream::out);
596 if (!jsonFile.is_open()) {
597 diags.verbose("can't open file '%s'\n", options.emitJSONPath.c_str());
601 dyld3::json::Node buildInvocationNode;
603 buildInvocationNode.map["version"].value = "1";
605 // Find the archs for the configuration we want.
606 __block std::set<std::string> validArchs;
607 manifest.configuration(options.configuration).forEachArchitecture(^(const std::string& path) {
608 validArchs.insert(path);
611 if (validArchs.size() != 1) {
612 fprintf(stderr, "MRM doesn't support more than one arch per configuration: %s\n",
613 options.configuration.c_str());
617 const char* archs[validArchs.size()];
618 uint64_t archIndex = 0;
619 for (const std::string& arch : validArchs) {
620 archs[archIndex++] = arch.c_str();
623 BuildOptions_v1 buildOptions;
624 buildOptions.version = 1;
625 buildOptions.updateName = manifest.build().c_str();
626 buildOptions.deviceName = options.configuration.c_str();
627 buildOptions.disposition = Disposition::Unknown;
628 buildOptions.platform = (Platform)manifest.platform();
629 buildOptions.archs = archs;
630 buildOptions.numArchs = validArchs.size();
631 buildOptions.verboseDiagnostics = options.debug;
632 buildOptions.isLocallyBuiltCache = true;
634 __block struct SharedCacheBuilder* sharedCacheBuilder = createSharedCacheBuilder(&buildOptions);
635 buildInvocationNode.map["buildOptions"] = getBuildOptionsNode(buildOptions);
637 std::set<std::string> requiredBinaries = {
638 "/usr/lib/libSystem.B.dylib"
641 // Get the file data for every MachO in the BOM.
642 __block dyld3::json::Node filesNode;
643 __block std::vector<std::pair<const void*, size_t>> mappedFiles;
644 manifest.forEachMachO(options.configuration, ^(const std::string &buildPath, const std::string &runtimePath, const std::string &arch, bool shouldBeExcludedIfLeaf) {
646 // Filter based on arch as the Manifest adds the file once for each UUID.
647 if (!validArchs.count(arch))
650 struct stat stat_buf;
651 int fd = ::open(buildPath.c_str(), O_RDONLY, 0);
653 diags.verbose("can't open file '%s', errno=%d\n", buildPath.c_str(), errno);
657 if (fstat(fd, &stat_buf) == -1) {
658 diags.verbose("can't stat open file '%s', errno=%d\n", buildPath.c_str(), errno);
663 const void* buffer = mmap(NULL, (size_t)stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
664 if (buffer == MAP_FAILED) {
665 diags.verbose("mmap() for file at %s failed, errno=%d\n", buildPath.c_str(), errno);
670 mappedFiles.emplace_back(buffer, (size_t)stat_buf.st_size);
671 FileFlags fileFlags = FileFlags::NoFlags;
672 if (requiredBinaries.count(runtimePath))
673 fileFlags = FileFlags::RequiredClosure;
674 addFile(sharedCacheBuilder, runtimePath.c_str(), (uint8_t*)buffer, (size_t)stat_buf.st_size, fileFlags);
676 dyld3::json::Node fileNode;
677 fileNode.map["path"].value = runtimePath;
678 fileNode.map["flags"].value = fileFlagsToString(fileFlags);
679 filesNode.array.push_back(fileNode);
682 __block dyld3::json::Node symlinksNode;
683 manifest.forEachSymlink(options.configuration, ^(const std::string &fromPath, const std::string &toPath) {
684 addSymlink(sharedCacheBuilder, fromPath.c_str(), toPath.c_str());
686 dyld3::json::Node symlinkNode;
687 symlinkNode.map["from-path"].value = fromPath;
688 symlinkNode.map["to-path"].value = toPath;
689 symlinksNode.array.push_back(symlinkNode);
692 buildInvocationNode.map["symlinks"] = symlinksNode;
694 std::string orderFileData;
695 if (!manifest.dylibOrderFile().empty()) {
696 orderFileData = loadOrderFile(manifest.dylibOrderFile());
697 if (!orderFileData.empty()) {
698 addFile(sharedCacheBuilder, "*order file data*", (uint8_t*)orderFileData.data(), orderFileData.size(), FileFlags::DylibOrderFile);
699 dyld3::json::Node fileNode;
700 fileNode.map["path"].value = manifest.dylibOrderFile();
701 fileNode.map["flags"].value = fileFlagsToString(FileFlags::DylibOrderFile);
702 filesNode.array.push_back(fileNode);
706 std::string dirtyDataOrderFileData;
707 if (!manifest.dirtyDataOrderFile().empty()) {
708 dirtyDataOrderFileData = loadOrderFile(manifest.dirtyDataOrderFile());
709 if (!dirtyDataOrderFileData.empty()) {
710 addFile(sharedCacheBuilder, "*dirty data order file data*", (uint8_t*)dirtyDataOrderFileData.data(), dirtyDataOrderFileData.size(), FileFlags::DirtyDataOrderFile);
711 dyld3::json::Node fileNode;
712 fileNode.map["path"].value = manifest.dirtyDataOrderFile();
713 fileNode.map["flags"].value = fileFlagsToString(FileFlags::DirtyDataOrderFile);
714 filesNode.array.push_back(fileNode);
718 buildInvocationNode.map["files"] = filesNode;
720 if (jsonFile.is_open()) {
721 dyld3::json::printJSON(buildInvocationNode, 0, jsonFile);
725 cacheBuildSuccess = runSharedCacheBuilder(sharedCacheBuilder);
727 writeMRMResults(cacheBuildSuccess, sharedCacheBuilder, options);
729 destroySharedCacheBuilder(sharedCacheBuilder);
731 for (auto mappedFile : mappedFiles)
732 ::munmap((void*)mappedFile.first, mappedFile.second);
734 manifest.calculateClosure();
736 cacheBuildSuccess = build(diags, manifest, options.dstRoot, false, options.debug, false, false, options.emitDevCaches, true);
739 if (!cacheBuildSuccess) {
743 // Compare this cache to the baseline cache and see if we have any roots to copy over
744 if (!options.baselineDifferenceResultPath.empty() || options.baselineCopyRoots) {
745 std::set<std::string> baselineDylibs = manifest.resultsForConfiguration(options.configuration);
747 std::set<std::string> newDylibs;
748 std::map<std::string, std::string> missingDylibReasons;
749 manifest.forEachConfiguration([&manifest, &newDylibs, &missingDylibReasons](const std::string& configName) {
750 for (auto& arch : manifest.configuration(configName).architectures) {
751 for (auto& dylib : arch.second.results.dylibs) {
752 if (dylib.second.included) {
753 newDylibs.insert(manifest.installNameForUUID(dylib.first));
755 missingDylibReasons[manifest.installNameForUUID(dylib.first)] = dylib.second.exclusionInfo;
761 // Work out the set of dylibs in the old cache but not the new one
762 std::map<std::string, std::string> dylibsMissingFromNewCache;
763 if (options.baselineCopyRoots || !options.baselineDifferenceResultPath.empty()) {
764 for (const std::string& baselineDylib : baselineDylibs) {
765 if (!newDylibs.count(baselineDylib)) {
766 auto reasonIt = missingDylibReasons.find(baselineDylib);
767 if (reasonIt != missingDylibReasons.end())
768 dylibsMissingFromNewCache[baselineDylib] = reasonIt->second;
770 dylibsMissingFromNewCache[baselineDylib] = "";
774 if (!dylibsMissingFromNewCache.empty()) {
775 // Work out which dylibs are missing from the new cache, but are not
776 // coming from the -root which already has them on disk
777 std::set<std::string> pathsNotInRoots;
778 for (std::pair<std::string, std::string> dylibMissingFromNewCache : dylibsMissingFromNewCache) {
779 const std::string& dylibInstallName = dylibMissingFromNewCache.first;
780 bool foundInRoot = false;
781 for (auto& root : options.roots) {
783 std::string filePath = root + "/" + dylibInstallName;
784 if (!stat(filePath.c_str(), &sb)) {
789 pathsNotInRoots.insert(dylibInstallName);
792 BOMCopier copier = BOMCopierNewWithSys(BomSys_default());
793 BOMCopierSetUserData(copier, (void*)&pathsNotInRoots);
794 BOMCopierSetCopyFileStartedHandler(copier, filteredCopyIncludingPaths);
795 std::string dylibCacheRootDir = realFilePath(options.dylibCacheDir + "/Root");
796 if (dylibCacheRootDir == "") {
797 fprintf(stderr, "Could not find dylib Root directory to copy baseline roots from\n");
800 BOMCopierCopy(copier, dylibCacheRootDir.c_str(), options.dstRoot.c_str());
801 BOMCopierFree(copier);
803 for (std::pair<std::string, std::string> dylibMissingFromNewCache : dylibsMissingFromNewCache) {
804 if (dylibMissingFromNewCache.second.empty())
805 diags.verbose("Dylib missing from new cache: '%s'\n", dylibMissingFromNewCache.first.c_str());
807 diags.verbose("Dylib missing from new cache: '%s' because '%s'\n",
808 dylibMissingFromNewCache.first.c_str(), dylibMissingFromNewCache.second.c_str());
813 if (!options.baselineDifferenceResultPath.empty()) {
814 auto cppToObjStr = [](const std::string& str) {
815 return [NSString stringWithUTF8String:str.c_str()];
818 // Work out the set of dylibs in the cache and taken from the -root
819 NSMutableArray<NSString*>* dylibsFromRoots = [NSMutableArray array];
820 for (auto& root : options.roots) {
821 for (const std::string& dylibInstallName : newDylibs) {
823 std::string filePath = root + "/" + dylibInstallName;
824 if (!stat(filePath.c_str(), &sb)) {
825 [dylibsFromRoots addObject:cppToObjStr(dylibInstallName)];
830 // Work out the set of dylibs in the new cache but not in the baseline cache.
831 NSMutableArray<NSString*>* dylibsMissingFromBaselineCache = [NSMutableArray array];
832 for (const std::string& newDylib : newDylibs) {
833 if (!baselineDylibs.count(newDylib))
834 [dylibsMissingFromBaselineCache addObject:cppToObjStr(newDylib)];
837 // If a dylib which was cached is no longer eligible, say why
838 NSMutableArray<NSDictionary*>* dylibsReasonsMissingFromNewCache = [NSMutableArray array];
839 for (std::pair<std::string, std::string> dylibMissingFromNewCache : dylibsMissingFromNewCache) {
840 NSMutableDictionary* reasonDict = [[NSMutableDictionary alloc] init];
841 reasonDict[@"path"] = cppToObjStr(dylibMissingFromNewCache.first);
842 reasonDict[@"reason"] = cppToObjStr(dylibMissingFromNewCache.second);
843 [dylibsReasonsMissingFromNewCache addObject:reasonDict];
846 NSMutableDictionary* cacheDict = [[NSMutableDictionary alloc] init];
847 cacheDict[@"root-paths-in-cache"] = dylibsFromRoots;
848 cacheDict[@"device-paths-to-delete"] = dylibsMissingFromBaselineCache;
849 cacheDict[@"baseline-paths-evicted-from-cache"] = dylibsReasonsMissingFromNewCache;
851 NSError* error = nil;
852 NSData* outData = [NSPropertyListSerialization dataWithPropertyList:cacheDict
853 format:NSPropertyListBinaryFormat_v1_0
856 (void)[outData writeToFile:cppToObjStr(options.baselineDifferenceResultPath) atomically:YES];
860 if (options.copyRoots) {
861 std::set<std::string> cachePaths;
862 manifest.forEachConfiguration([&manifest, &cachePaths](const std::string& configName) {
863 for (auto& arch : manifest.configuration(configName).architectures) {
864 for (auto& dylib : arch.second.results.dylibs) {
865 if (dylib.second.included) {
866 cachePaths.insert(manifest.installNameForUUID(dylib.first));
872 BOMCopier copier = BOMCopierNewWithSys(BomSys_default());
873 BOMCopierSetUserData(copier, (void*)&cachePaths);
874 BOMCopierSetCopyFileStartedHandler(copier, filteredCopyExcludingPaths);
875 for (auto& root : options.roots) {
876 BOMCopierCopy(copier, root.c_str(), options.dstRoot.c_str());
878 BOMCopierFree(copier);
881 int err = sync_volume_np(options.dstRoot.c_str(), SYNC_VOLUME_FULLSYNC | SYNC_VOLUME_WAIT);
883 fprintf(stderr, "Volume sync failed errnor=%d (%s)\n", err, strerror(err));
886 // Now that all the build commands have been issued lets put a barrier in after then which can tear down the app after
887 // everything is written.
889 if (!options.resultPath.empty()) {
890 manifest.write(options.resultPath);
894 static void buildCacheFromJSONManifest(Diagnostics& diags, const SharedCacheBuilderOptions& options,
895 const std::string& jsonManifestPath) {
896 dyld3::json::Node manifestNode = dyld3::json::readJSON(diags, jsonManifestPath.c_str());
897 if (diags.hasError())
900 // Top level node should be a map of the options, files, and symlinks.
901 if (manifestNode.map.empty()) {
902 diags.error("Expected map for JSON manifest node\n");
906 // Parse the nodes in the top level manifest node
907 const dyld3::json::Node& versionNode = dyld3::json::getRequiredValue(diags, manifestNode, "version");
908 uint64_t manifestVersion = dyld3::json::parseRequiredInt(diags, versionNode);
909 if (diags.hasError())
912 const uint64_t supportedManifestVersion = 1;
913 if (manifestVersion != supportedManifestVersion) {
914 diags.error("JSON manfiest version of %lld is unsupported. Supported version is %lld\n",
915 manifestVersion, supportedManifestVersion);
918 const dyld3::json::Node& buildOptionsNode = dyld3::json::getRequiredValue(diags, manifestNode, "buildOptions");
919 const dyld3::json::Node& filesNode = dyld3::json::getRequiredValue(diags, manifestNode, "files");
920 const dyld3::json::Node* symlinksNode = dyld3::json::getOptionalValue(diags, manifestNode, "symlinks");
923 const dyld3::json::Node& archsNode = dyld3::json::getRequiredValue(diags, buildOptionsNode, "archs");
924 if (diags.hasError())
926 if (archsNode.array.empty()) {
927 diags.error("Build options archs node is not an array\n");
930 const char* archs[archsNode.array.size()];
931 uint64_t archIndex = 0;
932 for (const dyld3::json::Node& archNode : archsNode.array) {
933 archs[archIndex++] = dyld3::json::parseRequiredString(diags, archNode).c_str();
936 // Parse the rest of the options node.
937 BuildOptions_v1 buildOptions;
938 buildOptions.version = dyld3::json::parseRequiredInt(diags, dyld3::json::getRequiredValue(diags, buildOptionsNode, "version"));
939 buildOptions.updateName = dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, buildOptionsNode, "updateName")).c_str();
940 buildOptions.deviceName = dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, buildOptionsNode, "deviceName")).c_str();
941 buildOptions.disposition = stringToDisposition(diags, dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, buildOptionsNode, "disposition")));
942 buildOptions.platform = stringToPlatform(diags, dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, buildOptionsNode, "platform")));
943 buildOptions.archs = archs;
944 buildOptions.numArchs = archsNode.array.size();
945 buildOptions.verboseDiagnostics = options.debug;
946 buildOptions.isLocallyBuiltCache = true;
948 if (diags.hasError())
951 // Override the disposition if we don't want certaion caches.
952 if (!options.emitDevCaches) {
953 switch (buildOptions.disposition) {
955 // Nothing we can do here as we can't assume what caches are built here.
957 case InternalDevelopment:
958 // This builds both caches, but we don't want dev
959 buildOptions.disposition = Customer;
962 // This is already only the customer cache
964 case InternalMinDevelopment:
965 diags.error("Cannot request no dev cache for InternalMinDevelopment as that is already only a dev cache\n");
970 if (diags.hasError())
973 struct SharedCacheBuilder* sharedCacheBuilder = createSharedCacheBuilder(&buildOptions);
976 if (filesNode.array.empty()) {
977 diags.error("Build options files node is not an array\n");
981 std::vector<std::tuple<std::string, std::string, FileFlags>> inputFiles;
982 for (const dyld3::json::Node& fileNode : filesNode.array) {
983 const std::string& path = dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, fileNode, "path")).c_str();
984 FileFlags fileFlags = stringToFileFlags(diags, dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, fileNode, "flags")));
986 // We can optionally have a sourcePath entry which is the path to get the source content from instead of the install path
987 std::string sourcePath;
988 const dyld3::json::Node* sourcePathNode = dyld3::json::getOptionalValue(diags, fileNode, "sourcePath");
989 if ( sourcePathNode != nullptr ) {
990 if (!sourcePathNode->array.empty()) {
991 diags.error("sourcePath node cannot be an array\n");
994 if (!sourcePathNode->map.empty()) {
995 diags.error("sourcePath node cannot be a map\n");
998 sourcePath = sourcePathNode->value;
1003 // Check if one of the -root's has this path
1004 bool foundInOverlay = false;
1005 for (const std::string& overlay : options.roots) {
1007 std::string filePath = overlay + path;
1008 if (!stat(filePath.c_str(), &sb)) {
1009 foundInOverlay = true;
1010 diags.verbose("Taking '%s' from overlay instead of dylib cache\n", path.c_str());
1011 inputFiles.push_back({ filePath, path, fileFlags });
1019 // Build paths are relative to the build artifact root directory.
1020 std::string buildPath;
1021 switch (fileFlags) {
1024 case ShouldBeExcludedFromCacheIfUnusedLeaf:
1025 case RequiredClosure:
1026 buildPath = "." + sourcePath;
1028 case DylibOrderFile:
1029 case DirtyDataOrderFile:
1030 buildPath = "." + sourcePath;
1033 inputFiles.push_back({ buildPath, path, fileFlags });
1036 if (diags.hasError())
1039 // Parse the baseline from the map if we have it
1040 std::set<std::string> baselineDylibs;
1041 if ( !options.baselineCacheMapPath.empty() ) {
1042 dyld3::json::Node mapNode = dyld3::json::readJSON(diags, options.baselineCacheMapPath.c_str());
1043 if (diags.hasError())
1046 // Top level node should be a map of the version and files
1047 if (mapNode.map.empty()) {
1048 diags.error("Expected map for JSON cache map node\n");
1052 // Parse the nodes in the top level manifest node
1053 const dyld3::json::Node& versionNode = dyld3::json::getRequiredValue(diags, mapNode, "version");
1054 uint64_t mapVersion = dyld3::json::parseRequiredInt(diags, versionNode);
1055 if (diags.hasError())
1058 const uint64_t supportedMapVersion = 1;
1059 if (mapVersion != supportedMapVersion) {
1060 diags.error("JSON map version of %lld is unsupported. Supported version is %lld\n",
1061 mapVersion, supportedMapVersion);
1066 const dyld3::json::Node& imagesNode = dyld3::json::getRequiredValue(diags, mapNode, "images");
1067 if (diags.hasError())
1069 if (imagesNode.array.empty()) {
1070 diags.error("Images node is not an array\n");
1074 for (const dyld3::json::Node& imageNode : imagesNode.array) {
1075 const dyld3::json::Node& pathNode = dyld3::json::getRequiredValue(diags, imageNode, "path");
1076 if (pathNode.value.empty()) {
1077 diags.error("Image path node is not a string\n");
1080 baselineDylibs.insert(pathNode.value);
1084 std::vector<std::pair<const void*, size_t>> mappedFiles;
1085 loadMRMFiles(diags, sharedCacheBuilder, inputFiles, mappedFiles, baselineDylibs);
1087 if (diags.hasError())
1090 // Parse the symlinks if we have them
1092 if (symlinksNode->array.empty()) {
1093 diags.error("Build options symlinks node is not an array\n");
1096 for (const dyld3::json::Node& symlinkNode : symlinksNode->array) {
1097 const std::string& fromPath = dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, symlinkNode, "path")).c_str();
1098 const std::string& toPath = dyld3::json::parseRequiredString(diags, dyld3::json::getRequiredValue(diags, symlinkNode, "target")).c_str();
1099 addSymlink(sharedCacheBuilder, fromPath.c_str(), toPath.c_str());
1103 if (diags.hasError())
1106 // Don't create a directory if we are skipping writes, which means we have no dstRoot set
1107 if (!options.dstRoot.empty())
1108 (void)mkpath_np((options.dstRoot + "/System/Library/Caches/com.apple.dyld/").c_str(), 0755);
1110 // Actually build the cache.
1111 bool cacheBuildSuccess = runSharedCacheBuilder(sharedCacheBuilder);
1113 // Compare this cache to the baseline cache and see if we have any roots to copy over
1114 if (!options.baselineDifferenceResultPath.empty() || options.baselineCopyRoots) {
1115 std::set<std::string> newDylibs;
1116 if (cacheBuildSuccess) {
1117 uint64_t fileResultCount = 0;
1118 if (const char* const* fileResults = getFilesToRemove(sharedCacheBuilder, &fileResultCount)) {
1119 for (uint64_t i = 0; i != fileResultCount; ++i)
1120 newDylibs.insert(fileResults[i]);
1124 if (options.baselineCopyRoots) {
1125 // Work out the set of dylibs in the old cache but not the new one
1126 std::set<std::string> dylibsMissingFromNewCache;
1127 for (const std::string& baselineDylib : baselineDylibs) {
1128 if (!newDylibs.count(baselineDylib))
1129 dylibsMissingFromNewCache.insert(baselineDylib);
1132 if (!dylibsMissingFromNewCache.empty()) {
1133 BOMCopier copier = BOMCopierNewWithSys(BomSys_default());
1134 BOMCopierSetUserData(copier, (void*)&dylibsMissingFromNewCache);
1135 BOMCopierSetCopyFileStartedHandler(copier, filteredCopyIncludingPaths);
1136 std::string dylibCacheRootDir = realFilePath(options.dylibCacheDir);
1137 if (dylibCacheRootDir == "") {
1138 fprintf(stderr, "Could not find dylib Root directory to copy baseline roots from\n");
1141 BOMCopierCopy(copier, dylibCacheRootDir.c_str(), options.dstRoot.c_str());
1142 BOMCopierFree(copier);
1144 for (const std::string& dylibMissingFromNewCache : dylibsMissingFromNewCache) {
1145 diags.verbose("Dylib missing from new cache: '%s'\n", dylibMissingFromNewCache.c_str());
1150 if (!options.baselineDifferenceResultPath.empty()) {
1151 auto cppToObjStr = [](const std::string& str) {
1152 return [NSString stringWithUTF8String:str.c_str()];
1155 // Work out the set of dylibs in the cache and taken from the -root
1156 NSMutableArray<NSString*>* dylibsFromRoots = [NSMutableArray array];
1157 for (auto& root : options.roots) {
1158 for (const std::string& dylibInstallName : newDylibs) {
1160 std::string filePath = root + "/" + dylibInstallName;
1161 if (!stat(filePath.c_str(), &sb)) {
1162 [dylibsFromRoots addObject:cppToObjStr(dylibInstallName)];
1167 // Work out the set of dylibs in the new cache but not in the baseline cache.
1168 NSMutableArray<NSString*>* dylibsMissingFromBaselineCache = [NSMutableArray array];
1169 for (const std::string& newDylib : newDylibs) {
1170 if (!baselineDylibs.count(newDylib))
1171 [dylibsMissingFromBaselineCache addObject:cppToObjStr(newDylib)];
1174 NSMutableDictionary* cacheDict = [[NSMutableDictionary alloc] init];
1175 cacheDict[@"root-paths-in-cache"] = dylibsFromRoots;
1176 cacheDict[@"device-paths-to-delete"] = dylibsMissingFromBaselineCache;
1178 NSError* error = nil;
1179 NSData* outData = [NSPropertyListSerialization dataWithPropertyList:cacheDict
1180 format:NSPropertyListBinaryFormat_v1_0
1183 (void)[outData writeToFile:cppToObjStr(options.baselineDifferenceResultPath) atomically:YES];
1187 writeMRMResults(cacheBuildSuccess, sharedCacheBuilder, options);
1189 destroySharedCacheBuilder(sharedCacheBuilder);
1191 unloadMRMFiles(mappedFiles);
1194 int main(int argc, const char* argv[])
1197 __block Diagnostics diags;
1198 SharedCacheBuilderOptions options;
1199 std::string jsonManifestPath;
1200 char* tempRootsDir = strdup("/tmp/dyld_shared_cache_builder.XXXXXX");
1202 mkdtemp(tempRootsDir);
1204 for (int i = 1; i < argc; ++i) {
1205 const char* arg = argv[i];
1206 if (arg[0] == '-') {
1207 if (strcmp(arg, "-debug") == 0) {
1208 diags = Diagnostics(true);
1209 options.debug = true;
1210 } else if (strcmp(arg, "-list_configs") == 0) {
1211 options.listConfigs = true;
1212 } else if (strcmp(arg, "-root") == 0) {
1213 std::string realpath = realPath(argv[++i]);
1214 if ( realpath.empty() || !fileExists(realpath) ) {
1215 fprintf(stderr, "-root path doesn't exist: %s\n", argv[i]);
1218 options.roots.insert(realpath);
1219 } else if (strcmp(arg, "-copy_roots") == 0) {
1220 options.copyRoots = true;
1221 } else if (strcmp(arg, "-dylib_cache") == 0) {
1222 options.dylibCacheDir = realPath(argv[++i]);
1223 } else if (strcmp(arg, "-artifact") == 0) {
1224 options.artifactDir = realPath(argv[++i]);
1225 } else if (strcmp(arg, "-no_development_cache") == 0) {
1226 options.emitDevCaches = false;
1227 } else if (strcmp(arg, "-no_overflow_dylibs") == 0) {
1228 options.emitElidedDylibs = false;
1229 } else if (strcmp(arg, "-development_cache") == 0) {
1230 options.emitDevCaches = true;
1231 } else if (strcmp(arg, "-overflow_dylibs") == 0) {
1232 options.emitElidedDylibs = true;
1233 } else if (strcmp(arg, "-mrm") == 0) {
1234 options.useMRM = true;
1235 } else if (strcmp(arg, "-emit_json") == 0) {
1236 options.emitJSONPath = realPath(argv[++i]);
1237 } else if (strcmp(arg, "-json_manifest") == 0) {
1238 jsonManifestPath = realPath(argv[++i]);
1239 } else if (strcmp(arg, "-build_all") == 0) {
1240 options.buildAllPath = realPath(argv[++i]);
1241 } else if (strcmp(arg, "-dst_root") == 0) {
1242 options.dstRoot = realPath(argv[++i]);
1243 } else if (strcmp(arg, "-release") == 0) {
1244 options.release = argv[++i];
1245 } else if (strcmp(arg, "-results") == 0) {
1246 options.resultPath = realPath(argv[++i]);
1247 } else if (strcmp(arg, "-baseline_diff_results") == 0) {
1248 options.baselineDifferenceResultPath = realPath(argv[++i]);
1249 } else if (strcmp(arg, "-baseline_copy_roots") == 0) {
1250 options.baselineCopyRoots = true;
1251 } else if (strcmp(arg, "-baseline_cache_map") == 0) {
1252 options.baselineCacheMapPath = realPath(argv[++i]);
1255 fprintf(stderr, "unknown option: %s\n", arg);
1259 if (!options.configuration.empty()) {
1260 fprintf(stderr, "You may only specify one configuration\n");
1263 options.configuration = argv[i];
1266 (void)options.emitElidedDylibs; // not implemented yet
1268 time_t mytime = time(0);
1269 fprintf(stderr, "Started: %s", asctime(localtime(&mytime)));
1270 processRoots(diags, options.roots, tempRootsDir);
1272 struct rlimit rl = { OPEN_MAX, OPEN_MAX };
1273 (void)setrlimit(RLIMIT_NOFILE, &rl);
1275 if (options.dylibCacheDir.empty() && options.artifactDir.empty() && options.release.empty()) {
1276 fprintf(stderr, "you must specify either -dylib_cache, -artifact or -release\n");
1278 } else if (!options.dylibCacheDir.empty() && !options.release.empty()) {
1279 fprintf(stderr, "you may not use -dylib_cache and -release at the same time\n");
1281 } else if (!options.dylibCacheDir.empty() && !options.artifactDir.empty()) {
1282 fprintf(stderr, "you may not use -dylib_cache and -artifact at the same time\n");
1286 if (options.configuration.empty() && jsonManifestPath.empty() && options.buildAllPath.empty()) {
1287 fprintf(stderr, "Must specify a configuration OR a -json_manifest path OR a -build_all path\n");
1291 if (!options.buildAllPath.empty()) {
1292 if (!options.dstRoot.empty()) {
1293 fprintf(stderr, "Cannot combine -dst_root and -build_all\n");
1296 if (!options.configuration.empty()) {
1297 fprintf(stderr, "Cannot combine configuration and -build_all\n");
1300 if (!jsonManifestPath.empty()) {
1301 fprintf(stderr, "Cannot combine -json_manifest and -build_all\n");
1304 if (!options.baselineDifferenceResultPath.empty()) {
1305 fprintf(stderr, "Cannot combine -baseline_diff_results and -build_all\n");
1308 if (options.baselineCopyRoots) {
1309 fprintf(stderr, "Cannot combine -baseline_copy_roots and -build_all\n");
1312 if (!options.baselineCacheMapPath.empty()) {
1313 fprintf(stderr, "Cannot combine -baseline_cache_map and -build_all\n");
1316 } else if (!options.listConfigs) {
1317 if (options.dstRoot.empty()) {
1318 fprintf(stderr, "Must specify a valid -dst_root OR -list_configs\n");
1322 if (options.configuration.empty() && jsonManifestPath.empty()) {
1323 fprintf(stderr, "Must specify a configuration OR -json_manifest path OR -list_configs\n");
1328 if (!options.baselineDifferenceResultPath.empty() && (options.roots.size() > 1)) {
1329 fprintf(stderr, "Cannot use -baseline_diff_results with more that one -root\n");
1333 // Some options don't work with a JSON manifest
1334 if (!jsonManifestPath.empty()) {
1335 if (!options.resultPath.empty()) {
1336 fprintf(stderr, "Cannot use -results with -json_manifest\n");
1339 if (!options.baselineDifferenceResultPath.empty() && options.baselineCacheMapPath.empty()) {
1340 fprintf(stderr, "Must use -baseline_cache_map with -baseline_diff_results when using -json_manifest\n");
1343 if (options.baselineCopyRoots && options.baselineCacheMapPath.empty()) {
1344 fprintf(stderr, "Must use -baseline_cache_map with -baseline_copy_roots when using -json_manifest\n");
1348 if (!options.baselineCacheMapPath.empty()) {
1349 fprintf(stderr, "Cannot use -baseline_cache_map without -json_manifest\n");
1354 if (!options.baselineCacheMapPath.empty()) {
1355 if (options.baselineDifferenceResultPath.empty() && options.baselineCopyRoots) {
1356 fprintf(stderr, "Must use -baseline_cache_map with -baseline_diff_results or -baseline_copy_roots\n");
1361 // Find all the JSON files if we use -build_all
1362 __block std::vector<std::string> jsonPaths;
1363 if (!options.buildAllPath.empty()) {
1364 struct stat stat_buf;
1365 if (stat(options.buildAllPath.c_str(), &stat_buf) != 0) {
1366 fprintf(stderr, "Could not find -build_all path '%s'\n", options.buildAllPath.c_str());
1370 if ( (stat_buf.st_mode & S_IFMT) != S_IFDIR ) {
1371 fprintf(stderr, "-build_all path is not a directory '%s'\n", options.buildAllPath.c_str());
1375 auto processFile = ^(const std::string& path, const struct stat& statBuf) {
1376 if ( !endsWith(path, ".json") )
1379 jsonPaths.push_back(path);
1382 iterateDirectoryTree("", options.buildAllPath,
1383 ^(const std::string& dirPath) { return false; },
1384 processFile, true /* process files */, false /* recurse */);
1386 if (jsonPaths.empty()) {
1387 fprintf(stderr, "Didn't find any .json files inside -build_all path: %s\n", options.buildAllPath.c_str());
1391 if (options.listConfigs) {
1392 for (const std::string& path : jsonPaths) {
1393 fprintf(stderr, "Found config: %s\n", path.c_str());
1399 if (!options.artifactDir.empty()) {
1400 // Find the dylib cache dir from inside the artifact dir
1401 struct stat stat_buf;
1402 if (stat(options.artifactDir.c_str(), &stat_buf) != 0) {
1403 fprintf(stderr, "Could not find artifact path '%s'\n", options.artifactDir.c_str());
1406 std::string dir = options.artifactDir + "/AppleInternal/Developer/DylibCaches";
1407 if (stat(dir.c_str(), &stat_buf) != 0) {
1408 fprintf(stderr, "Could not find artifact path '%s'\n", dir.c_str());
1412 if (!options.release.empty()) {
1413 // Use the given release
1414 options.dylibCacheDir = dir + "/" + options.release + ".dlc";
1416 // Find a release directory
1417 __block std::vector<std::string> subDirectories;
1418 iterateDirectoryTree("", dir, ^(const std::string& dirPath) {
1419 subDirectories.push_back(dirPath);
1421 }, nullptr, false, false);
1423 if (subDirectories.empty()) {
1424 fprintf(stderr, "Could not find dlc subdirectories inside '%s'\n", dir.c_str());
1428 if (subDirectories.size() > 1) {
1429 fprintf(stderr, "Found too many subdirectories inside artifact path '%s'. Use -release to select one\n", dir.c_str());
1433 options.dylibCacheDir = subDirectories.front();
1437 if (options.dylibCacheDir.empty()) {
1438 options.dylibCacheDir = std::string("/AppleInternal/Developer/DylibCaches/") + options.release + ".dlc";
1441 //Move into the dir so we can use relative path manifests
1442 chdir(options.dylibCacheDir.c_str());
1444 dispatch_async(dispatch_get_main_queue(), ^{
1445 if (!options.buildAllPath.empty()) {
1446 bool requiresConcurrencyLimit = false;
1447 dispatch_semaphore_t concurrencyLimit = NULL;
1448 // Try build 1 cache per 8GB of RAM
1449 uint64_t memSize = 0;
1450 size_t sz = sizeof(memSize);
1451 if ( sysctlbyname("hw.memsize", &memSize, &sz, NULL, 0) == 0 ) {
1452 uint64_t maxThreads = std::max(memSize / 0x200000000ULL, 1ULL);
1453 fprintf(stderr, "Detected %lldGb or less of memory, limiting concurrency to %lld threads\n",
1454 memSize / (1 << 30), maxThreads);
1455 requiresConcurrencyLimit = true;
1456 concurrencyLimit = dispatch_semaphore_create(maxThreads);
1459 dispatch_apply(jsonPaths.size(), DISPATCH_APPLY_AUTO, ^(size_t index) {
1460 // Horrible hack to limit concurrency in low spec build machines.
1461 if (requiresConcurrencyLimit) { dispatch_semaphore_wait(concurrencyLimit, DISPATCH_TIME_FOREVER); }
1463 const std::string& jsonPath = jsonPaths[index];
1464 buildCacheFromJSONManifest(diags, options, jsonPath);
1466 if (requiresConcurrencyLimit) { dispatch_semaphore_signal(concurrencyLimit); }
1468 } else if (!jsonManifestPath.empty()) {
1469 buildCacheFromJSONManifest(diags, options, jsonManifestPath);
1471 buildCacheFromPListManifest(diags, options);
1474 const char* args[8];
1475 args[0] = (char*)"/bin/rm";
1476 args[1] = (char*)"-rf";
1477 args[2] = (char*)tempRootsDir;
1479 (void)runCommandAndWait(diags, args);
1481 if (diags.hasError()) {
1482 fprintf(stderr, "dyld_shared_cache_builder: error: %s", diags.errorMessage().c_str());
1486 for (const std::string& warn : diags.warnings()) {
1487 fprintf(stderr, "dyld_shared_cache_builder: warning: %s\n", warn.c_str());
1490 // Finally, write the roots.txt to tell us which roots we pulled in
1491 if (!options.dstRoot.empty())
1492 writeRootList(options.dstRoot + "/System/Library/Caches/com.apple.dyld", options.roots);