+ (void)mkpath_np((dstRoot + "/System/Library/Caches/com.apple.dyld/").c_str(), 0755);
+ bool cacheBuildSuccess = false;
+ if (useMRM) {
+
+ FILE* jsonFile = nullptr;
+ if (!emitJSONPath.empty()) {
+ jsonFile = fopen(emitJSONPath.c_str(), "w");
+ if (!jsonFile) {
+ diags.verbose("can't open file '%s', errno=%d\n", emitJSONPath.c_str(), errno);
+ return;
+ }
+ }
+ dyld3::json::Node buildInvocationNode;
+
+ // Find the archs for the configuration we want.
+ __block std::set<std::string> validArchs;
+ manifest.configuration(configuration).forEachArchitecture(^(const std::string& path) {
+ validArchs.insert(path);
+ });
+
+ if (validArchs.size() != 1) {
+ fprintf(stderr, "MRM doesn't support more than one arch per configuration: %s\n",
+ configuration.c_str());
+ exit(-1);
+ }
+
+ const char* archs[validArchs.size()];
+ uint64_t archIndex = 0;
+ for (const std::string& arch : validArchs) {
+ archs[archIndex++] = arch.c_str();
+ }
+
+ BuildOptions_v1 buildOptions;
+ buildOptions.version = 1;
+ buildOptions.updateName = manifest.build().c_str();
+ buildOptions.deviceName = configuration.c_str();
+ buildOptions.disposition = Disposition::Unknown;
+ buildOptions.platform = (Platform)manifest.platform();
+ buildOptions.archs = archs;
+ buildOptions.numArchs = validArchs.size();
+ buildOptions.verboseDiagnostics = debug;
+ buildOptions.isLocallyBuiltCache = true;
+
+ __block struct SharedCacheBuilder* sharedCacheBuilder = createSharedCacheBuilder(&buildOptions);
+ buildInvocationNode.map["build-options"] = getBuildOptionsNode(buildOptions);
+
+ std::set<std::string> requiredBinaries = {
+ "/usr/lib/libSystem.B.dylib"
+ };
+
+ // Get the file data for every MachO in the BOM.
+ __block dyld3::json::Node filesNode;
+ __block std::vector<std::pair<const void*, size_t>> mappedFiles;
+ manifest.forEachMachO(configuration, ^(const std::string &buildPath, const std::string &runtimePath, const std::string &arch, bool shouldBeExcludedIfLeaf) {
+
+ // Filter based on arch as the Manifest adds the file once for each UUID.
+ if (!validArchs.count(arch))
+ return;
+
+ struct stat stat_buf;
+ int fd = ::open(buildPath.c_str(), O_RDONLY, 0);
+ if (fd == -1) {
+ diags.verbose("can't open file '%s', errno=%d\n", buildPath.c_str(), errno);
+ return;
+ }
+
+ if (fstat(fd, &stat_buf) == -1) {
+ diags.verbose("can't stat open file '%s', errno=%d\n", buildPath.c_str(), errno);
+ ::close(fd);
+ return;
+ }
+
+ const void* buffer = mmap(NULL, (size_t)stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (buffer == MAP_FAILED) {
+ diags.verbose("mmap() for file at %s failed, errno=%d\n", buildPath.c_str(), errno);
+ ::close(fd);
+ }
+ ::close(fd);
+
+ mappedFiles.emplace_back(buffer, (size_t)stat_buf.st_size);
+ FileFlags fileFlags = FileFlags::NoFlags;
+ if (requiredBinaries.count(runtimePath))
+ fileFlags = FileFlags::RequiredClosure;
+ addFile(sharedCacheBuilder, runtimePath.c_str(), (uint8_t*)buffer, (size_t)stat_buf.st_size, fileFlags);
+
+ dyld3::json::Node fileNode;
+ fileNode.map["path"].value = runtimePath;
+ fileNode.map["flags"].value = "NoFlags";
+ filesNode.array.push_back(fileNode);
+ });
+
+ __block dyld3::json::Node symlinksNode;
+ manifest.forEachSymlink(configuration, ^(const std::string &fromPath, const std::string &toPath) {
+ addSymlink(sharedCacheBuilder, fromPath.c_str(), toPath.c_str());
+
+ dyld3::json::Node symlinkNode;
+ symlinkNode.map["from-path"].value = fromPath;
+ symlinkNode.map["to-path"].value = toPath;
+ symlinksNode.array.push_back(symlinkNode);
+ });
+
+ buildInvocationNode.map["symlinks"] = symlinksNode;
+
+ std::string orderFileData;
+ if (!manifest.dylibOrderFile().empty()) {
+ orderFileData = loadOrderFile(manifest.dylibOrderFile());
+ if (!orderFileData.empty()) {
+ addFile(sharedCacheBuilder, "*order file data*", (uint8_t*)orderFileData.data(), orderFileData.size(), FileFlags::DylibOrderFile);
+ dyld3::json::Node fileNode;
+ fileNode.map["path"].value = manifest.dylibOrderFile();
+ fileNode.map["flags"].value = "DylibOrderFile";
+ filesNode.array.push_back(fileNode);
+ }
+ }
+
+ std::string dirtyDataOrderFileData;
+ if (!manifest.dirtyDataOrderFile().empty()) {
+ dirtyDataOrderFileData = loadOrderFile(manifest.dirtyDataOrderFile());
+ if (!dirtyDataOrderFileData.empty()) {
+ addFile(sharedCacheBuilder, "*dirty data order file data*", (uint8_t*)dirtyDataOrderFileData.data(), dirtyDataOrderFileData.size(), FileFlags::DirtyDataOrderFile);
+ dyld3::json::Node fileNode;
+ fileNode.map["path"].value = manifest.dirtyDataOrderFile();
+ fileNode.map["flags"].value = "DirtyDataOrderFile";
+ filesNode.array.push_back(fileNode);
+ }
+ }
+
+ buildInvocationNode.map["files"] = filesNode;
+
+ if (jsonFile) {
+ dyld3::json::printJSON(buildInvocationNode, 0, jsonFile);
+ fclose(jsonFile);
+ jsonFile = nullptr;
+ }
+
+ cacheBuildSuccess = runSharedCacheBuilder(sharedCacheBuilder);
+
+ if (!cacheBuildSuccess) {
+ for (uint64 i = 0, e = getErrorCount(sharedCacheBuilder); i != e; ++i) {
+ const char* errorMessage = getError(sharedCacheBuilder, i);
+ fprintf(stderr, "ERROR: %s\n", errorMessage);
+ }
+ }
+
+ // Now emit each cache we generated, or the errors for them.
+ for (uint64 i = 0, e = getCacheResultCount(sharedCacheBuilder); i != e; ++i) {
+ BuildResult result;
+ getCacheResult(sharedCacheBuilder, i, &result);
+ if (result.numErrors) {
+ for (uint64_t errorIndex = 0; errorIndex != result.numErrors; ++errorIndex) {
+ fprintf(stderr, "[%s] ERROR: %s\n", result.loggingPrefix, result.errors[errorIndex]);
+ }
+ cacheBuildSuccess = false;
+ continue;
+ }
+ if (result.numWarnings) {
+ for (uint64_t warningIndex = 0; warningIndex != result.numWarnings; ++warningIndex) {
+ fprintf(stderr, "[%s] WARNING: %s\n", result.loggingPrefix, result.warnings[warningIndex]);
+ }
+ }
+ }
+
+ // If we built caches, then write everything out.
+ // TODO: Decide if we should we write any good caches anyway?
+ if (cacheBuildSuccess) {
+ for (uint64 i = 0, e = getFileResultCount(sharedCacheBuilder); i != e; ++i) {
+ FileResult result;
+ getFileResult(sharedCacheBuilder, i, &result);
+
+ if (!result.data)
+ continue;
+
+ const std::string path = dstRoot + result.path;
+ std::string pathTemplate = path + "-XXXXXX";
+ size_t templateLen = strlen(pathTemplate.c_str())+2;
+ char pathTemplateSpace[templateLen];
+ strlcpy(pathTemplateSpace, pathTemplate.c_str(), templateLen);
+ int fd = mkstemp(pathTemplateSpace);
+ if ( fd != -1 ) {
+ ::ftruncate(fd, result.size);
+ uint64_t writtenSize = pwrite(fd, result.data, result.size, 0);
+ if ( writtenSize == result.size ) {
+ ::fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); // mkstemp() makes file "rw-------", switch it to "rw-r--r--"
+ if ( ::rename(pathTemplateSpace, path.c_str()) == 0) {
+ ::close(fd);
+ continue; // success
+ }
+ }
+ else {
+ fprintf(stderr, "ERROR: could not write file %s\n", pathTemplateSpace);
+ cacheBuildSuccess = false;
+ }
+ ::close(fd);
+ ::unlink(pathTemplateSpace);
+ }
+ else {
+ fprintf(stderr, "ERROR: could not open file %s\n", pathTemplateSpace);
+ cacheBuildSuccess = false;
+ }
+ }
+ }
+
+ destroySharedCacheBuilder(sharedCacheBuilder);