123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- #include <ctype.h>
- #include <arpa/inet.h>
- char *target;
- char *depfile;
- char *cmdline;
- void usage(void)
- {
- fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
- exit(1);
- }
- void print_cmdline(void)
- {
- printf("cmd_%s := %s\n\n", target, cmdline);
- }
- char * str_config = NULL;
- int size_config = 0;
- int len_config = 0;
- void grow_config(int len)
- {
- while (len_config + len > size_config) {
- if (size_config == 0)
- size_config = 2048;
- str_config = realloc(str_config, size_config *= 2);
- if (str_config == NULL)
- { perror("fixdep:malloc"); exit(1); }
- }
- }
- int is_defined_config(const char * name, int len)
- {
- const char * pconfig;
- const char * plast = str_config + len_config - len;
- for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
- if (pconfig[ -1] == '\n'
- && pconfig[len] == '\n'
- && !memcmp(pconfig, name, len))
- return 1;
- }
- return 0;
- }
- void define_config(const char * name, int len)
- {
- grow_config(len + 1);
- memcpy(str_config+len_config, name, len);
- len_config += len;
- str_config[len_config++] = '\n';
- }
- void clear_config(void)
- {
- len_config = 0;
- define_config("", 0);
- }
- void use_config(char *m, int slen)
- {
- char *s = alloca(slen+1);
- char *p;
- if (is_defined_config(m, slen))
- return;
- define_config(m, slen);
- memcpy(s, m, slen); s[slen] = 0;
- for (p = s; p < s + slen; p++) {
- if (*p == '_')
- *p = '/';
- else
- *p = tolower((int)*p);
- }
- printf(" $(wildcard include/config/%s.h) \\\n", s);
- }
- void parse_config_file(char *map, size_t len)
- {
-
- char *end_3 = map + len - 3;
- char *end_7 = map + len - 7;
- char *p = map;
- char *q;
- int off;
- for (; p <= end_3; p++) {
-
- if (!(isalnum(*p) || *p == '_'))
- continue;
-
- if (p < end_7 && p[6] == '_') {
- if (!memcmp(p, "CONFIG", 6)) goto conf7;
- if (!memcmp(p, "ENABLE", 6)) goto conf7;
- if (!memcmp(p, "IF_NOT", 6)) goto conf7;
- }
-
-
- if (p[0] == 'I' && p[1] == 'F' && p[2] == '_') {
- off = 3;
- goto conf;
- }
-
- while (p <= end_3 && (isalnum(*p) || *p == '_'))
- p++;
- continue;
- conf7: off = 7;
- conf:
- p += off;
- for (q = p; q < end_3+3; q++) {
- if (!(isalnum(*q) || *q == '_'))
- break;
- }
- if (q != p) {
- use_config(p, q-p);
- }
- }
- }
- int strrcmp(char *s, char *sub)
- {
- int slen = strlen(s);
- int sublen = strlen(sub);
- if (sublen > slen)
- return 1;
- return memcmp(s + slen - sublen, sub, sublen);
- }
- void do_config_file(char *filename)
- {
- struct stat st;
- int fd;
- void *map;
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "fixdep: ");
- perror(filename);
- exit(2);
- }
- fstat(fd, &st);
- if (st.st_size == 0) {
- close(fd);
- return;
- }
- map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if ((long) map == -1) {
- perror("fixdep: mmap");
- close(fd);
- return;
- }
- parse_config_file(map, st.st_size);
- munmap(map, st.st_size);
- close(fd);
- }
- void parse_dep_file(void *map, size_t len)
- {
- char *m = map;
- char *end = m + len;
- char *p;
- char *s = alloca(len);
- p = memchr(m, ':', len);
- if (!p) {
- fprintf(stderr, "fixdep: parse error\n");
- exit(1);
- }
- memcpy(s, m, p-m); s[p-m] = 0;
- printf("deps_%s := \\\n", target);
- m = p+1;
- clear_config();
- while (m < end) {
- while (m < end && (*m == ' ' || *m == '\\' || *m == '\n' || *m == '\r'))
- m++;
- p = m;
- while (p < end && *p != ' ') p++;
- if (p == end) {
- do p--; while (!isalnum(*p));
- p++;
- }
- memcpy(s, m, p-m); s[p-m] = 0;
- if (strrcmp(s, "include/autoconf.h") &&
- strrcmp(s, "arch/um/include/uml-config.h") &&
- strrcmp(s, ".ver")) {
- printf(" %s \\\n", s);
- do_config_file(s);
- }
- m = p + 1;
- }
- printf("\n%s: $(deps_%s)\n\n", target, target);
- printf("$(deps_%s):\n", target);
- }
- void print_deps(void)
- {
- struct stat st;
- int fd;
- void *map;
- fd = open(depfile, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "fixdep: ");
- perror(depfile);
- exit(2);
- }
- fstat(fd, &st);
- if (st.st_size == 0) {
- fprintf(stderr,"fixdep: %s is empty\n",depfile);
- close(fd);
- return;
- }
- map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if ((long) map == -1) {
- perror("fixdep: mmap");
- close(fd);
- return;
- }
- parse_dep_file(map, st.st_size);
- munmap(map, st.st_size);
- close(fd);
- }
- void traps(void)
- {
- }
- int main(int argc, char **argv)
- {
- traps();
- if (argc != 4)
- usage();
- depfile = argv[1];
- target = argv[2];
- cmdline = argv[3];
- print_cmdline();
- print_deps();
- return 0;
- }
|