00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #if ! (defined(linux) || defined(BUILD_OS_DARWIN))
00090 #include <bstring.h>
00091 #endif
00092 #include <ctype.h>
00093 #include <errno.h>
00094 #include <fcntl.h>
00095 #include <stdio.h>
00096 #include <stdlib.h>
00097 #include <string.h>
00098 #include <unistd.h>
00099 #include <sys/mman.h>
00100 #include <sys/stat.h>
00101 #include "make_depend.h"
00102
00103
00104
00105
00106 #if MD_DEBUG
00107 #define ENTER(s) printf("ENTER: %s\n",s);
00108 #define EXIT(s) printf("EXIT : %s\n",s);
00109 #define LOG(s) printf s ;
00110 #else
00111 #define ENTER(s)
00112 #define EXIT(s)
00113 #define LOG(s)
00114 #endif
00115
00116
00117
00118
00119
00120
00121 #define FREECOUNT(x) ((x) - (x) / 4)
00122 #define TABLESIZE(n) ((n) + (n) / 3)
00123 #define INITIALSIZE (1 << 8)
00124 #define INITSTABSIZE (0x40000)
00125
00126
00127
00128
00129
00130
00131
00132 #define MAXSTRINGLEN 512
00133
00134 char scratch[80];
00135
00136 typedef struct dependency Dependency;
00137
00138 struct dependency {
00139 char *name;
00140 Dependency *next;
00141 };
00142
00143 struct mdrule {
00144 char *toolname;
00145 char *filename;
00146 char *target;
00147 void (*error)(const char*,...);
00148 unsigned int entries;
00149 unsigned int freecount;
00150 unsigned int hashmask;
00151 Dependency *table;
00152 char *stab;
00153 char *stabcur;
00154 char *stabmax;
00155 };
00156
00157
00158 typedef struct mdfile {
00159 int f;
00160 const char* filename;
00161 char* base;
00162 char* limit;
00163 int size;
00164 struct stat stb;
00165 } MDfile_t;
00166
00167
00168
00169
00170
00171
00172 static
00173 void
00174 MDfile_init ( MDfile_t *m ) {
00175 m->f = 0;
00176 m->filename = "";
00177 m->base = 0;
00178 m->limit = 0;
00179 m->size = 0;
00180 m->stb.st_size = 0;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 static
00193 int
00194 MDtoolcmp ( const char *tool, const char *s, int slen ) {
00195 char *t;
00196 int toollen;
00197
00198 t = strpbrk(tool," \n");
00199 toollen = t-tool;
00200
00201 if ( (t == 0) || (toollen != slen) ) {return -1;}
00202
00203 return strncmp(tool,s,slen);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 static
00216 int
00217 MDtargcmp ( const char *targ, const char *s, int slen ) {
00218 char *t;
00219 int targlen;
00220
00221 t = strpbrk(targ,": ");
00222 targlen = t-targ;
00223
00224 if ( (t == 0) || (targlen != slen) ) {return -1;}
00225
00226 return strncmp(targ,s,slen);
00227 }
00228
00229 #if 0
00230
00231
00232
00233
00234
00235
00236
00237 static
00238 char * MDstrcatdup ( const char *s1, const char *s2 ) {
00239 int s1_len, s2_len, targ_len;
00240 char *targ;
00241 s1_len = strlen(s1);
00242 s2_len = strlen(s2);
00243 targ_len= s1_len + s2_len;
00244 targ = (char*)malloc(targ_len);
00245 bcopy(s1,targ,s1_len);
00246 bcopy(s2,&targ[s1_len], s2_len);
00247 targ[targ_len]='\0';
00248 return targ;
00249 }
00250 #endif
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 void
00266 MDnewstab(MDhandle h)
00267 {
00268 h->stab = (char *) malloc(INITSTABSIZE);
00269 h->stabcur = h->stab;
00270 h->stabmax = h->stab + INITSTABSIZE;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 char *
00285 MDstrcpy(MDhandle h, const char * src )
00286 {
00287 char *dest;
00288 int freespace;
00289
00290 if ( src == NULL ) { return NULL; }
00291
00292
00293
00294
00295
00296
00297
00298 freespace = (h->stabmax - h->stabcur);
00299 if (freespace < MAXSTRINGLEN) {
00300 MDnewstab(h);
00301 freespace=(h->stabmax - h->stabcur);
00302 }
00303
00304
00305
00306
00307 dest = h->stabcur;
00308 h->stabcur = (char *) memccpy(h->stabcur, src, '\0', freespace );
00309 if ( h->stabcur == NULL ) {
00310
00311
00312
00313
00314 h->error("String table overflow in MDupdate\n", h->filename);
00315 exit(1);
00316 }
00317
00318 return dest;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327 static Dependency *
00328 MDhash(MDhandle h, char *s)
00329 {
00330 int len;
00331 char *t;
00332 unsigned int v, c;
00333
00334 len = strlen(s);
00335 t = s + len - 2;
00336 if (len > 2 && *t == '.' && (t[1] == 'h' || t[1] == 'a' || t[1] == 'o'))
00337 *t = '\0';
00338 else
00339 t = 0;
00340 for (v = 0; (c = *s) != 0; s++)
00341 v += (v << 1) ^ c;
00342 if (t)
00343 *t = '.';
00344 return &h->table[v & h->hashmask];
00345 }
00346
00347
00348
00349
00350
00351
00352 static Dependency *
00353 MDlookup(MDhandle h, char *dependency)
00354 {
00355 Dependency *d, *prevd;
00356
00357 for (d = MDhash(h, dependency); d->name; d = d->next) {
00358 if (!strcmp(d->name, dependency))
00359 break;
00360 if (d->next == 0) {
00361 prevd = d;
00362 do {
00363 if (++d > &h->table[h->hashmask])
00364 d = &h->table[0];
00365 } while (d->name);
00366 prevd->next = d;
00367 break;
00368 }
00369 }
00370 return d;
00371 }
00372
00373
00374
00375
00376 static void
00377 MDgrow(MDhandle h)
00378 {
00379 int n, m;
00380 Dependency *table, *oldtable;
00381 Dependency *d, *e;
00382
00383 n = h->hashmask + 1;
00384 m = 2 * n;
00385 table = (Dependency *) calloc(m, sizeof *h->table);
00386 if (table == 0) {
00387 h->error("can't reallocate memory for %s update", h->filename);
00388 exit(1);
00389 }
00390
00391 h->freecount += FREECOUNT(n);
00392 h->hashmask = m - 1;
00393 oldtable = h->table;
00394 h->table = table;
00395 for (d = oldtable; --n >= 0; d++) {
00396 if (d->name == 0)
00397 continue;
00398 e = MDlookup(h, d->name);
00399 e->name = d->name;
00400 }
00401 free(oldtable);
00402 }
00403
00404
00405
00406
00407 MDhandle
00408 MDopen(const char *toolname, const char *filename, const char *target, void (*error)(const char*,...))
00409 {
00410 MDhandle h;
00411 Dependency *table;
00412
00413 h = (mdrule *) malloc(sizeof *h);
00414 MDnewstab(h);
00415 table = (Dependency *) calloc(INITIALSIZE, sizeof *h->table);
00416 if (h == 0 || table == 0) {
00417 error("can't allocate memory for %s update", filename);
00418 if (h)
00419 free(h);
00420 if (table)
00421 free(table);
00422 return 0;
00423 }
00424
00425 h->toolname = MDstrcpy(h,toolname);
00426 h->filename = MDstrcpy(h,filename);
00427 h->target = MDstrcpy(h,target );
00428 h->error = error;
00429 h->entries = 0;
00430 h->freecount = FREECOUNT(INITIALSIZE);
00431 h->hashmask = INITIALSIZE - 1;
00432 h->table = table;
00433
00434 return h;
00435 }
00436
00437
00438
00439
00440 void
00441 MDupdate(MDhandle h, char *dependency)
00442 {
00443 Dependency *d;
00444
00445 if (h == 0)
00446 return;
00447 d = MDlookup(h, dependency);
00448 if (d->name)
00449 return;
00450 if (h->freecount == 0) {
00451 MDgrow(h);
00452 d = MDlookup(h, dependency);
00453 }
00454
00455 d->name = MDstrcpy(h,dependency);
00456 --h->freecount;
00457 h->entries++;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 #define SKIP_BLANKS while ( next<limit && *next==' ' ) {next++;}
00517 #define SKIP_TO_EOL while ( next<limit && *next!='\n') {next++;}
00518 #define IS_EOL (*next=='\n')
00519 #define IS_EOF (next>=limit)
00520 #define SCAN_FOR(c) while ( next<limit && *next!=c && *next!='\n') {next++;}
00521
00522 static
00523 char *
00524 MDparseline ( char* line, char *limit, char **targ, char **tool ) {
00525 char *next;
00526
00527
00528 *targ = 0;
00529 *tool = 0;
00530 next = line;
00531
00532
00533
00534
00535
00536
00537
00538
00539 SKIP_BLANKS; if ( IS_EOF ) { return 0; }
00540
00541
00542 if ( IS_EOL ) { return ++next; }
00543
00544
00545 if ( *next == '#') {
00546 SKIP_TO_EOL;
00547 if ( IS_EOF ) { return 0; }
00548 if ( IS_EOL ) { return ++next; }
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558 *targ = next;
00559
00560
00561 SCAN_FOR(':'); if ( *next != ':' ) return 0;
00562
00563
00564 next++; SKIP_BLANKS;
00565
00566
00567 SCAN_FOR('#'); if ( IS_EOF ) { return 0; }
00568
00569
00570 if ( IS_EOL ) { return ++next;}
00571
00572
00573 if ( *++next != ':' ) return 0;
00574
00575
00576 next++;
00577 SKIP_BLANKS; if ( IS_EOF ) { return 0; }
00578
00579
00580
00581
00582
00583 if ( IS_EOL ) { return ++next; }
00584
00585
00586 *tool = next;
00587
00588
00589 SKIP_TO_EOL;
00590 if ( IS_EOF ) { return 0; }
00591
00592
00593 return ++next;
00594
00595 }
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 #define ERR(x) { h->error x ; goto cleanup; }
00624 void
00625 MDclose(MDhandle h, char *target)
00626 {
00627 struct flock l;
00628 MDfile_t md;
00629 FILE *file;
00630
00631 char *line;
00632 char *nextline;
00633 char *targ;
00634 char *tool;
00635 Dependency *d;
00636 int targetlen;
00637 int toolnamelen;
00638 int found=0;
00639 int linelen;
00640 int remaininglen;
00641
00642
00643 ENTER("MDclose");
00644
00645
00646
00647
00648
00649
00650
00651 if (h == 0) return;
00652 if (h->entries == 0) goto relspace;
00653
00654
00655
00656
00657
00658
00659 if (target == 0) target = h->target;
00660 targetlen = strlen(target);
00661
00662
00663
00664
00665
00666
00667 MDfile_init ( &md ); md.filename = strdup ( h->filename );
00668
00669
00670
00671
00672
00673
00674 LOG(("Opening %s\n",md.filename));
00675 md.f = open(md.filename, O_CREAT|O_RDWR, 0666);
00676 if ( md.f < 0 ) ERR(("open (%s): %s", md.filename, strerror(errno)));
00677
00678 l.l_type = F_WRLCK;
00679 l.l_whence = SEEK_SET;
00680 l.l_start = l.l_len = 0;
00681 if ( fcntl(md.f, F_SETLKW, &l) < 0 )
00682 ERR(("fcntl (%s) : %s", md.filename, strerror(errno)));
00683 if ( fstat(md.f,&md.stb) < 0 )
00684 ERR(("fstat (%s) : %s", md.filename, strerror(errno)));
00685 LOG(("Opening %s successful\n",md.filename));
00686
00687
00688
00689
00690
00691
00692 LOG(("Mapping %s\n",md.filename));
00693 md.base = md.limit = 0;
00694 if ( 0 < md.stb.st_size ) {
00695 md.base = (char *) mmap ( 0, md.stb.st_size, PROT_READ|PROT_WRITE
00696 , MAP_SHARED, md.f, 0);
00697 if ((long) md.base < 0)
00698 ERR(("mmap (%s): %s", md.filename, strerror(errno)));
00699 md.size = md.stb.st_size;
00700 md.limit = md.base + md.size;
00701 }
00702 LOG(("Mapping %s successful \n",md.filename));
00703
00704
00705
00706
00707
00708
00709
00710 nextline=0;
00711 found=0;
00712 toolnamelen = strlen(h->toolname);
00713 for ( line=md.base; line<md.limit; line=nextline ) {
00714 nextline = MDparseline(line, md.limit, &targ, &tool);
00715 if ( 0 == nextline ) ERR(("%s: damaged file", md.filename));
00716
00717
00718 if ( 0 == targ ) continue;
00719
00720
00721 if ( 0 == MDtargcmp(targ,target,targetlen) ) {
00722
00723
00724 if ( 0 == tool ) { continue; }
00725
00726
00727 if (0 == MDtoolcmp(tool,h->toolname,toolnamelen)) {
00728 found = 1;
00729 break;
00730 }
00731 }
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 if ( found ) {
00747 LOG(("Found target=%s\n",target));
00748
00749 linelen = nextline - line;
00750 remaininglen = md.limit - nextline;
00751 memmove ( line, nextline, remaininglen );
00752
00753 LOG(("Truncating %s by 0x%08x bytes\n",md.filename,linelen));
00754
00755 if ( 0 != ftruncate(md.f, (md.size-linelen) ) )
00756 ERR(("ftruncate (%s): %s", md.filename, strerror(errno)));
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 LOG(("Write final record to %s\n",md.filename));
00768 if ( 0 == (file = fdopen(md.f,"a")))
00769 ERR(("fdopen(%s): %s", md.filename, strerror(errno)));
00770
00771 fprintf ( file, "%s:", target );
00772 for (d=&h->table[0]; d<= &h->table[h->hashmask]; d++) {
00773 if (d->name) { fprintf( file, " %s", d->name ); }
00774 }
00775 if ( 0 > fprintf( file, " #:%s\n", h->toolname ) )
00776 ERR(("fprintf(%s): %s", md.filename, strerror(errno)));
00777
00778 cleanup:
00779 if ( file != 0 ) { fclose(file); }
00780 if ( md.base > (char*)0 ) { munmap(md.base, md.stb.st_size ); }
00781 if ( md.f > 0 ) { close(md.f); }
00782
00783 relspace:
00784 free(h->table);
00785 free(h->stab);
00786 free(h);
00787 EXIT("MDclose");
00788 }
00789