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 <dispatch/dispatch.h>
26 #include <Security/Security.h>
27 #include <Security/SecCodeSigner.h>
29 #include <sys/types.h>
32 #include <sys/resource.h>
33 #include <mach/mach.h>
34 #include <mach/mach_time.h>
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/resource.h>
65 #include "MultiCacheBuilder.h"
67 #include "mega-dylib-utils.h"
70 #if !__has_feature(objc_arc)
71 #error The use of libdispatch in this files requires it to be compiled with ARC in order to avoid leaks
74 extern char** environ;
76 static dispatch_queue_t build_queue;
78 inline bool has_suffix(std::string const & value, std::string const & ending)
80 if (ending.size() > value.size()) return false;
81 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
84 static const char *tempRootDirTemplate = "/tmp/dyld_shared_cache_builder.XXXXXX";
85 static char *tempRootDir = nullptr;
87 void processRoot( const std::string &root, std::set<std::string> &roots ) {
94 res = stat(root.c_str(), &sb);
96 if (res == 0 && S_ISDIR(sb.st_mode)) {
99 } else if ( has_suffix( root, ".cpio" ) || has_suffix( root, ".cpio.gz" ) || has_suffix( root, ".cpgz" ) ||
100 has_suffix( root, ".cpio.bz2" ) || has_suffix( root, ".cpbz2" ) || has_suffix( root, ".pax" ) ||
101 has_suffix( root, ".pax.gz" ) || has_suffix( root, ".pgz" ) || has_suffix( root, ".pax.bz2" ) ||
102 has_suffix( root, ".pbz2" ) ) {
103 args[0] = (char *)"/usr/bin/ditto";
104 args[1] = (char *)"-x";
105 args[2] = (char *)root.c_str();
106 args[3] = tempRootDir;
108 } else if (has_suffix(root, ".tar")) {
109 args[0] = (char *)"/usr/bin/tar";
110 args[1] = (char *)"xf";
111 args[2] = (char *)root.c_str();
112 args[3] = (char *)"-C";
113 args[4] = tempRootDir;
115 } else if (has_suffix(root, ".tar.gz") || has_suffix(root, ".tgz")) {
116 args[0] = (char *)"/usr/bin/tar";
117 args[1] = (char *)"xzf";
118 args[2] = (char *)root.c_str();
119 args[3] = (char *)"-C";
120 args[4] = tempRootDir;
122 } else if (has_suffix(root, ".tar.bz2")
123 || has_suffix(root, ".tbz2")
124 || has_suffix(root, ".tbz")) {
125 args[0] = (char *)"/usr/bin/tar";
126 args[1] = (char *)"xjf";
127 args[2] = (char *)root.c_str();
128 args[3] = (char *)"-C";
129 args[4] = tempRootDir;
131 } else if (has_suffix(root, ".xar")) {
132 args[0] = (char *)"/usr/bin/xar";
133 args[1] = (char *)"-xf";
134 args[2] = (char *)root.c_str();
135 args[3] = (char *)"-C";
136 args[4] = tempRootDir;
138 } else if (has_suffix(root, ".zip")) {
139 args[0] = (char *)"/usr/bin/ditto";
140 args[1] = (char *)"-xk";
141 args[2] = (char *)root.c_str();
142 args[3] = tempRootDir;
145 terminate("unknown archive type: %s", root.c_str());
148 res = posix_spawn(&pid, args[0], nullptr, nullptr, (char**)args, environ);
149 if (res != 0) terminate("Failed to spawn %s: %s (%d)", args[0], strerror(res), res);
152 res = waitpid(pid, &status, 0);
153 } while (res == -1 && errno == EINTR);
155 if (WIFEXITED(status)) {
156 res = WEXITSTATUS(status);
162 if (res != 0) terminate("Could not expand archive %s: %s (%d)", root.c_str(), strerror(res), res);
164 for (auto &existingRoot : roots) {
165 if (existingRoot == tempRootDir)
169 roots.insert( tempRootDir );
172 bool writeRootList( const std::string &dstRoot, const std::set<std::string> &roots ) {
173 if ( roots.size() == 0 ) return false;
175 std::string rootFile = dstRoot + "/roots.txt";
176 FILE* froots = ::fopen(rootFile.c_str(), "w");
177 if ( froots == NULL )
180 for (auto &root : roots) {
181 fprintf(froots, "%s\n", root.c_str());
188 std::set<std::string> cachePaths;
190 BOMCopierCopyOperation filteredCopy(BOMCopier copier, const char *path, BOMFSObjType type, off_t size) {
191 std::string absolutePath = &path[1];
192 if (cachePaths.count(absolutePath)) {
193 return BOMCopierSkipFile;
195 return BOMCopierContinue;
198 int main (int argc, const char * argv[]) {
200 std::set<std::string> roots;
201 std::vector<std::string> inputRoots;
202 std::string dylibCacheDir;
204 bool emitDevCaches = true;
205 bool emitElidedDylibs = true;
206 bool listConfigs = false;
207 bool copyRoots = false;
209 std::string configuration;
210 std::string resultPath;
212 time_t mytime = time(0);
213 log("Started: %s", asctime(localtime(&mytime)));
215 tempRootDir = strdup(tempRootDirTemplate);
216 mkdtemp(tempRootDir);
218 for (int i = 1; i < argc; ++i) {
219 const char* arg = argv[i];
221 if (strcmp(arg, "-debug") == 0) {
223 } else if (strcmp(arg, "-list_configs") == 0) {
225 } else if (strcmp(arg, "-root") == 0) {
226 std::string root = argv[++i];
227 inputRoots.push_back(root);
228 processRoot(root, roots);
229 } else if (strcmp(arg, "-copy_roots") == 0) {
231 } else if (strcmp(arg, "-dylib_cache") == 0) {
232 dylibCacheDir = argv[++i];
233 } else if (strcmp(arg, "-no_development_cache") == 0) {
234 emitDevCaches = false;
235 } else if (strcmp(arg, "-no_overflow_dylibs") == 0) {
236 emitElidedDylibs = false;
237 } else if (strcmp(arg, "-development_cache") == 0) {
238 emitDevCaches = true;
239 } else if (strcmp(arg, "-overflow_dylibs") == 0) {
240 emitElidedDylibs = true;
241 } else if (strcmp(arg, "-dst_root") == 0) {
243 } else if (strcmp(arg, "-release") == 0) {
245 } else if (strcmp(arg, "-results") == 0) {
246 resultPath = argv[++i];
249 terminate("unknown option: %s\n", arg);
252 if (!configuration.empty()) {
253 terminate("You may only specify one configruation");
255 configuration = argv[i];
259 struct rlimit rl = {OPEN_MAX, OPEN_MAX};
260 (void)setrlimit(RLIMIT_NOFILE, &rl);
262 if (dylibCacheDir.empty() && release.empty()) {
263 terminate("you must specify either -dylib_cache or -release");
264 } else if (!dylibCacheDir.empty() && !release.empty()) {
265 terminate("you may not use -dylib_cache and -release at the same time");
268 if ((configuration.empty() || dstRoot.empty()) && !listConfigs) {
269 terminate("Must specify a configuration and a -dst_root OR -list_configs\n");
273 if (dylibCacheDir.empty()) {
274 dylibCacheDir = std::string("/AppleInternal/Developer/DylibCaches/") + release + ".dlc";
277 //Move into the dir so we can use relative path manifests
278 chdir(dylibCacheDir.c_str());
280 auto manifest = Manifest(dylibCacheDir + "/Manifest.plist", roots);
282 if (manifest.build.empty()) {
283 terminate("No manifest found at '%s/Manifest.plist'\n", dylibCacheDir.c_str());
285 log("Building Caches for %s", manifest.build.c_str());
288 for (auto& config : manifest.configurations) {
289 printf("%s\n", config.first.c_str());
294 std::map<std::string, Manifest::Configuration> filteredConfigs;
296 for (auto& config : manifest.configurations) {
297 if (config.first == configuration) {
298 filteredConfigs[config.first] = config.second;
300 for (auto& arch : filteredConfigs[config.first].architectures) {
301 arch.second.results = Manifest::Results();
306 if ( filteredConfigs.empty() ) {
307 terminate( "No config %s. Please run with -list_configs to see configurations available for this %s.\n",
308 configuration.c_str(), manifest.build.c_str() );
311 manifest.configurations = filteredConfigs;
312 manifest.calculateClosure(false);
314 // FIXME: Plumb through no_development
316 std::shared_ptr<MultiCacheBuilder> builder = std::make_shared<MultiCacheBuilder>( manifest, false, false, true );
317 builder->buildCaches(dstRoot);
318 writeRootList(dstRoot, roots);
321 for (auto& config : manifest.configurations) {
322 for (auto& arch : config.second.architectures) {
323 for (auto& dylib : arch.second.results.dylibs) {
324 if (dylib.second.included) {
325 MachOProxy *proxy = manifest.dylibProxy( dylib.first, arch.first );
326 cachePaths.insert( proxy->installName );
327 for ( auto &alias : proxy->installNameAliases ) {
328 cachePaths.insert(alias);
335 BOMCopier copier = BOMCopierNewWithSys(BomSys_default());
336 BOMCopierSetCopyFileStartedHandler(copier, filteredCopy);
337 for (auto& root : roots) {
338 BOMCopierCopy(copier, root.c_str(), dstRoot.c_str());
340 BOMCopierFree(copier);
343 // Create an empty FIPS data in the root
344 (void)mkpath_np((dstRoot + "/private/var/db/FIPS/").c_str(), 0755);
345 int fd = open((dstRoot + "/private/var/db/FIPS/fips_data").c_str(), O_CREAT | O_TRUNC, 0644);
348 // Now that all the build commands have been issued lets put a barrier in after then which can tear down the app after
349 // everything is written.
352 if (!resultPath.empty()) {
353 manifest.write(resultPath);
361 args[0] = (char*)"/bin/rm";
362 args[1] = (char*)"-rf";
363 args[2] = (char*)tempRootDir;
366 res = posix_spawn(&pid, args[0], nullptr, nullptr, (char**)args, environ);
368 terminate("Failed to spawn %s: %s (%d)", args[0], strerror(res), res);
371 res = waitpid(pid, &status, 0);
372 } while (res == -1 && errno == EINTR);
374 if (WIFEXITED(status)) {
375 res = WEXITSTATUS(status);