<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[58672] users/toby/objcports/misc/depstree.c</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.macports.org/changeset/58672">58672</a></dd>
<dt>Author</dt> <dd>toby@macports.org</dd>
<dt>Date</dt> <dd>2009-10-02 10:46:36 -0700 (Fri, 02 Oct 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>in progress</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#userstobyobjcportsmiscdepstreec">users/toby/objcports/misc/depstree.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="userstobyobjcportsmiscdepstreec"></a>
<div class="modfile"><h4>Modified: users/toby/objcports/misc/depstree.c (58671 => 58672)</h4>
<pre class="diff"><span>
<span class="info">--- users/toby/objcports/misc/depstree.c        2009-10-02 17:22:41 UTC (rev 58671)
+++ users/toby/objcports/misc/depstree.c        2009-10-02 17:46:36 UTC (rev 58672)
</span><span class="lines">@@ -28,13 +28,22 @@
</span><span class="cx"> char *
</span><span class="cx"> strdup_cf(CFStringRef str)
</span><span class="cx"> {
</span><ins>+        char *result;
</ins><span class="cx">         CFIndex length, size;
</span><del>-        char *result = NULL;
</del><span class="cx">
</span><del>-        length = CFStringGetLength(str);
-        size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
-        result = malloc(size);
-        CFStringGetCString(str, result, size, kCFStringEncodingUTF8);
</del><ins>+        if ((result = CFStringGetCStringPtr(str, kCFStringEncodingUTF8))) {
+                result = strdup(result);
+        } else {
+                length = CFStringGetLength(str);
+                size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
+                result = malloc(size);
+                if (result) {
+                        if (!CFStringGetCString(str, result, size, kCFStringEncodingUTF8)) {
+                                free(result);
+                                result = NULL;
+                        }
+                }
+        }
</ins><span class="cx">
</span><span class="cx">         return result;
</span><span class="cx"> }
</span><span class="lines">@@ -194,6 +203,7 @@
</span><span class="cx">         context.info = (void *)port;
</span><span class="cx">         context.retain = CFRetain;
</span><span class="cx">         context.release = CFRelease;
</span><ins>+        context.copyDescription = CFCopyDescription;
</ins><span class="cx">         tree = CFTreeCreate(NULL, &context);
</span><span class="cx">
</span><span class="cx">         deps = find_deps(port);
</span><span class="lines">@@ -208,27 +218,37 @@
</span><span class="cx">         return tree;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static void
-dump_tree(CFTreeRef tree, CFIndex indent)
</del><ins>+// percentage of nodes to visit before the root
+typedef enum {
+        TRAVERSE_PREORDER = 0,
+        TRAVERSE_INORDER = 50,
+        TRAVERSE_POSTORDER = 100,
+} traverse_method_t;
+
+typedef void (^traverse_handler_t)(CFTreeRef tree, CFIndex level, Boolean *stop);
+
+Boolean
+traverse_tree(CFTreeRef tree, traverse_method_t method, CFIndex level, traverse_handler_t handler)
</ins><span class="cx"> {
</span><del>-        CFIndex i, count;
-        CFMutableStringRef output;
-        CFTreeContext context;
</del><ins>+        CFIndex i, count = CFTreeGetChildCount(tree), cutoff = (count * method) / 100;
+        CFTreeRef children[count];
+        Boolean stop = FALSE;
</ins><span class="cx">
</span><del>-        output = CFStringCreateMutable(NULL, 0);
-        for (i = 0; i < indent; i++) {
-                CFStringAppend(output, CFSTR(" "));
</del><ins>+        CFTreeGetChildren(tree, children);
+
+        for (i = 0; i < cutoff; i++) {
+                stop = traverse_tree(children[i], method, level + 1, handler);
+                if (stop) break;
</ins><span class="cx">         }
</span><ins>+        if (!stop) handler(tree, level, &stop);
+        if (!stop) {
+                for (i = cutoff; i < count; i++) {
+                        stop = traverse_tree(children[i], method, level + 1, handler);
+                        if (stop) break;
+                }
+        }
</ins><span class="cx">
</span><del>-        CFTreeGetContext(tree, &context);
-        CFStringAppend(output, context.info);
-        fprintf_cf(stdout, indent ? CFSTR("%@\n") : CFSTR("Dependencies of %@:\n"), output);
-        CFRelease(output);
-
-        count = CFTreeGetChildCount(tree);
-        for (i = 0; i < count; i++) {
-                dump_tree(CFTreeGetChildAtIndex(tree, i), indent + 1);
-        }
</del><ins>+        return stop;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static void __dead2
</span><span class="lines">@@ -238,12 +258,110 @@
</span><span class="cx">         exit(1);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+void
+print_deps(CFTreeRef root)
+{
+        traverse_tree(root, TRAVERSE_PREORDER, 0,
+                ^(CFTreeRef tree, CFIndex level, Boolean *stop __unused) {
+                        CFIndex i;
+                        CFMutableStringRef output;
+                        CFTreeContext context;
+
+                        output = CFStringCreateMutable(NULL, 0);
+                        for (i = 0; i < level; i++) {
+                                CFStringAppend(output, CFSTR(" "));
+                        }
+
+                        CFTreeGetContext(tree, &context);
+                        CFStringAppend(output, context.info);
+                        fprintf_cf(stdout, level ? CFSTR("%@\n") : CFSTR("Dependencies of %@:\n"), output);
+                        CFRelease(output);
+                }
+        );
+}
+
+void
+build_port(CFTreeRef root, long jobs)
+{
+        dispatch_semaphore_t sema;
+        dispatch_queue_t queue;
+        dispatch_queue_t print_queue;
+        CFMutableArrayRef working;
+        __block int done = 0;
+        __block CFStringRef port;
+        CFStringRef tmp;
+
+        // TODO: synchronize access to this
+        working = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
+
+        sema = dispatch_semaphore_create(jobs);
+        queue = dispatch_queue_create("lame queue", NULL);
+        print_queue = dispatch_queue_create("CFShow", NULL);
+
+        for (;;) {
+                dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
+
+                port = NULL;
+                fprintf(stderr, "... find next\n");
+                traverse_tree(root, TRAVERSE_POSTORDER, 0,
+                        ^(CFTreeRef tree, CFIndex level __unused, Boolean *stop) {
+                                CFTreeContext context;
+                                __block int skip = 0;
+
+                                CFTreeGetContext(tree, &context);
+
+                                dispatch_sync(queue, ^{
+                                        if (CFArrayContainsValue(working, CFRangeMake(0, CFArrayGetCount(working)), context.info)) {
+                                                fprintf_cf(stderr, CFSTR("skip %@ (in progress / already built)\n"), context.info);
+                                                skip = 1;
+                                        }
+                                        if (CFTreeGetChildCount(tree)) {
+                                                fprintf_cf(stderr, CFSTR("skip %@ (blocked)\n"), context.info);
+                                                skip = 1;
+                                        }
+                                        if (!skip) {
+                                                fprintf_cf(stderr, CFSTR("+++ build %@\n"), context.info);
+                                                CFArrayAppendValue(working, context.info);
+                                                port = CFStringCreateCopy(NULL, context.info);
+                                                *stop = 1;
+                                        }
+                                });
+                        }
+                );
+
+                if (port) {
+                        tmp = CFStringCreateCopy(NULL, port);
+                        dispatch_async(dispatch_get_global_queue(0, 0), ^{
+                                fprintf_cf(stderr, CFSTR("start %@\n"), tmp);
+                                sleep(2);
+                                fprintf_cf(stderr, CFSTR("done %@\n"), tmp);
+                                dispatch_sync(queue, ^{
+                                        traverse_tree(root, TRAVERSE_POSTORDER, 0,
+                                                ^(CFTreeRef xtree, CFIndex xlevel __unused, Boolean *xstop __unused) {
+                                                        CFTreeContext xcontext;
+                                                        CFTreeGetContext(xtree, &xcontext);
+                                                        if (CFStringCompare(xcontext.info, tmp, 0) == kCFCompareEqualTo) {
+                                                                CFTreeRemove(xtree);
+                                                        }
+                                                }
+                                        );
+                                });
+                                CFRelease(tmp);
+                                dispatch_semaphore_signal(sema);
+                        });
+                } else {
+                        fprintf(stderr, "lost\n");
+                }
+        }
+}
+
</ins><span class="cx"> int
</span><span class="cx"> main(int argc, char *argv[])
</span><span class="cx"> {
</span><span class="cx">         int ch;
</span><span class="cx">         CFStringRef port;
</span><span class="cx">         CFTreeRef tree;
</span><ins>+        long jobs;
</ins><span class="cx">
</span><span class="cx">         while ((ch = getopt(argc, argv, "")) != -1) {
</span><span class="cx">                 switch (ch) {
</span><span class="lines">@@ -256,14 +374,21 @@
</span><span class="cx">         argc -= optind;
</span><span class="cx">         argv += optind;
</span><span class="cx">
</span><del>-        if (argc != 1) {
</del><ins>+        if (argc < 1) {
</ins><span class="cx">                 usage();
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         port = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
</span><span class="cx">         tree = create_port_tree(port);
</span><span class="cx">         CFRelease(port);
</span><del>-        dump_tree(tree, 0);
</del><ins>+
+        if (argc < 2) {
+                print_deps(tree);
+        } else {
+                jobs = strtol(argv[1], NULL, 0);
+                if (!jobs) jobs = 2;
+                build_port(tree, jobs);
+        }
</ins><span class="cx">         CFRelease(tree);
</span><span class="cx">
</span><span class="cx">         return 0;
</span></span></pre>
</div>
</div>
</body>
</html>