graph.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "mk.h"
  10. static Node *applyrules(char *, char *);
  11. static void togo(Node *);
  12. static int vacuous(Node *);
  13. static Node *newnode(char *);
  14. static void trace(char *, Arc *);
  15. static void cyclechk(Node *);
  16. static void ambiguous(Node *);
  17. static void attribute(Node *);
  18. Node *
  19. graph(char *target)
  20. {
  21. Node *node;
  22. char *cnt;
  23. cnt = rulecnt();
  24. node = applyrules(target, cnt);
  25. free(cnt);
  26. cyclechk(node);
  27. node->flags |= PROBABLE; /* make sure it doesn't get deleted */
  28. vacuous(node);
  29. ambiguous(node);
  30. attribute(node);
  31. return(node);
  32. }
  33. static Node *
  34. applyrules(char *target, char *cnt)
  35. {
  36. Symtab *sym;
  37. Node *node;
  38. Rule *r;
  39. Arc head, *a = &head;
  40. Word *w;
  41. char stem[NAMEBLOCK], buf[NAMEBLOCK];
  42. Resub rmatch[NREGEXP];
  43. /* print("applyrules(%lx='%s')\n", target, target);/**/
  44. sym = symlook(target, S_NODE, 0);
  45. if(sym)
  46. return sym->u.ptr;
  47. target = strdup(target);
  48. node = newnode(target);
  49. head.n = 0;
  50. head.next = 0;
  51. sym = symlook(target, S_TARGET, 0);
  52. memset((char*)rmatch, 0, sizeof(rmatch));
  53. for(r = sym? sym->u.ptr:0; r; r = r->chain){
  54. if(r->attr&META) continue;
  55. if(strcmp(target, r->target)) continue;
  56. if((!r->recipe || !*r->recipe) && (!r->tail || !r->tail->s || !*r->tail->s)) continue; /* no effect; ignore */
  57. if(cnt[r->rule] >= nreps) continue;
  58. cnt[r->rule]++;
  59. node->flags |= PROBABLE;
  60. /* if(r->attr&VIR)
  61. * node->flags |= VIRTUAL;
  62. * if(r->attr&NOREC)
  63. * node->flags |= NORECIPE;
  64. * if(r->attr&DEL)
  65. * node->flags |= DELETE;
  66. */
  67. if(!r->tail || !r->tail->s || !*r->tail->s) {
  68. a->next = newarc((Node *)0, r, "", rmatch);
  69. a = a->next;
  70. } else
  71. for(w = r->tail; w; w = w->next){
  72. a->next = newarc(applyrules(w->s, cnt), r, "", rmatch);
  73. a = a->next;
  74. }
  75. cnt[r->rule]--;
  76. head.n = node;
  77. }
  78. for(r = metarules; r; r = r->next){
  79. if((!r->recipe || !*r->recipe) && (!r->tail || !r->tail->s || !*r->tail->s)) continue; /* no effect; ignore */
  80. if ((r->attr&NOVIRT) && a != &head && (a->r->attr&VIR))
  81. continue;
  82. if(r->attr&REGEXP){
  83. stem[0] = 0;
  84. patrule = r;
  85. memset((char*)rmatch, 0, sizeof(rmatch));
  86. if(regexec(r->pat, node->name, rmatch, NREGEXP) == 0)
  87. continue;
  88. } else {
  89. if(!match(node->name, r->target, stem)) continue;
  90. }
  91. if(cnt[r->rule] >= nreps) continue;
  92. cnt[r->rule]++;
  93. /* if(r->attr&VIR)
  94. * node->flags |= VIRTUAL;
  95. * if(r->attr&NOREC)
  96. * node->flags |= NORECIPE;
  97. * if(r->attr&DEL)
  98. * node->flags |= DELETE;
  99. */
  100. if(!r->tail || !r->tail->s || !*r->tail->s) {
  101. a->next = newarc((Node *)0, r, stem, rmatch);
  102. a = a->next;
  103. } else
  104. for(w = r->tail; w; w = w->next){
  105. if(r->attr&REGEXP)
  106. regsub(w->s, buf, sizeof(buf), rmatch, NREGEXP);
  107. else
  108. subst(stem, w->s, buf, sizeof(buf));
  109. a->next = newarc(applyrules(buf, cnt), r, stem, rmatch);
  110. a = a->next;
  111. }
  112. cnt[r->rule]--;
  113. }
  114. a->next = node->prereqs;
  115. node->prereqs = head.next;
  116. return(node);
  117. }
  118. static void
  119. togo(Node *node)
  120. {
  121. Arc *la, *a;
  122. /* delete them now */
  123. la = 0;
  124. for(a = node->prereqs; a; la = a, a = a->next)
  125. if(a->flag&TOGO){
  126. if(a == node->prereqs)
  127. node->prereqs = a->next;
  128. else
  129. la->next = a->next, a = la;
  130. }
  131. }
  132. static
  133. vacuous(Node *node)
  134. {
  135. Arc *la, *a;
  136. int vac = !(node->flags&PROBABLE);
  137. if(node->flags&READY)
  138. return(node->flags&VACUOUS);
  139. node->flags |= READY;
  140. for(a = node->prereqs; a; a = a->next)
  141. if(a->n && vacuous(a->n) && (a->r->attr&META))
  142. a->flag |= TOGO;
  143. else
  144. vac = 0;
  145. /* if a rule generated arcs that DON'T go; no others from that rule go */
  146. for(a = node->prereqs; a; a = a->next)
  147. if((a->flag&TOGO) == 0)
  148. for(la = node->prereqs; la; la = la->next)
  149. if((la->flag&TOGO) && (la->r == a->r)){
  150. la->flag &= ~TOGO;
  151. }
  152. togo(node);
  153. if(vac)
  154. node->flags |= VACUOUS;
  155. return(vac);
  156. }
  157. static Node *
  158. newnode(char *name)
  159. {
  160. register Node *node;
  161. node = (Node *)Malloc(sizeof(Node));
  162. symlook(name, S_NODE, (void *)node);
  163. node->name = name;
  164. node->time = timeof(name, 0);
  165. node->prereqs = 0;
  166. node->flags = node->time? PROBABLE : 0;
  167. node->next = 0;
  168. return(node);
  169. }
  170. void
  171. dumpn(char *s, Node *n)
  172. {
  173. char buf[1024];
  174. Arc *a;
  175. Bprint(&bout, "%s%s@%p: time=%ld flags=0x%x next=%p\n",
  176. s, n->name, n, n->time, n->flags, n->next);
  177. for(a = n->prereqs; a; a = a->next){
  178. snprint(buf, sizeof buf, "%s ", (*s == ' ')? s:"");
  179. dumpa(buf, a);
  180. }
  181. }
  182. static void
  183. trace(char *s, Arc *a)
  184. {
  185. fprint(2, "\t%s", s);
  186. while(a){
  187. fprint(2, " <-(%s:%d)- %s", a->r->file, a->r->line,
  188. a->n? a->n->name:"");
  189. if(a->n){
  190. for(a = a->n->prereqs; a; a = a->next)
  191. if(*a->r->recipe) break;
  192. } else
  193. a = 0;
  194. }
  195. fprint(2, "\n");
  196. }
  197. static void
  198. cyclechk(Node *n)
  199. {
  200. Arc *a;
  201. if((n->flags&CYCLE) && n->prereqs){
  202. fprint(2, "mk: cycle in graph detected at target %s\n", n->name);
  203. Exit();
  204. }
  205. n->flags |= CYCLE;
  206. for(a = n->prereqs; a; a = a->next)
  207. if(a->n)
  208. cyclechk(a->n);
  209. n->flags &= ~CYCLE;
  210. }
  211. static void
  212. ambiguous(Node *n)
  213. {
  214. Arc *a;
  215. Rule *r = 0;
  216. Arc *la;
  217. int bad = 0;
  218. la = 0;
  219. for(a = n->prereqs; a; a = a->next){
  220. if(a->n)
  221. ambiguous(a->n);
  222. if(*a->r->recipe == 0) continue;
  223. if(r == 0)
  224. r = a->r, la = a;
  225. else{
  226. if(r->recipe != a->r->recipe){
  227. if((r->attr&META) && !(a->r->attr&META)){
  228. la->flag |= TOGO;
  229. r = a->r, la = a;
  230. } else if(!(r->attr&META) && (a->r->attr&META)){
  231. a->flag |= TOGO;
  232. continue;
  233. }
  234. }
  235. if(r->recipe != a->r->recipe){
  236. if(bad == 0){
  237. fprint(2, "mk: ambiguous recipes for %s:\n", n->name);
  238. bad = 1;
  239. trace(n->name, la);
  240. }
  241. trace(n->name, a);
  242. }
  243. }
  244. }
  245. if(bad)
  246. Exit();
  247. togo(n);
  248. }
  249. static void
  250. attribute(Node *n)
  251. {
  252. register Arc *a;
  253. for(a = n->prereqs; a; a = a->next){
  254. if(a->r->attr&VIR)
  255. n->flags |= VIRTUAL;
  256. if(a->r->attr&NOREC)
  257. n->flags |= NORECIPE;
  258. if(a->r->attr&DEL)
  259. n->flags |= DELETE;
  260. if(a->n)
  261. attribute(a->n);
  262. }
  263. if(n->flags&VIRTUAL)
  264. n->time = 0;
  265. }