123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- #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>
- #define INT_CONF ntohl(0x434f4e46)
- #define INT_ONFI ntohl(0x4f4e4649)
- #define INT_NFIG ntohl(0x4e464947)
- #define INT_FIG_ ntohl(0x4649475f)
- char *target;
- char *depfile;
- char *cmdline;
- int is_spl_build = 0;
- static void usage(void)
- {
- fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
- exit(1);
- }
- static void print_cmdline(void)
- {
- printf("cmd_%s := %s\n\n", target, cmdline);
- }
- struct item {
- struct item *next;
- unsigned int len;
- unsigned int hash;
- char name[0];
- };
- #define HASHSZ 256
- static struct item *hashtab[HASHSZ];
- static unsigned int strhash(const char *str, unsigned int sz)
- {
-
- unsigned int i, hash = 2166136261U;
- for (i = 0; i < sz; i++)
- hash = (hash ^ str[i]) * 0x01000193;
- return hash;
- }
- static int is_defined_config(const char *name, int len, unsigned int hash)
- {
- struct item *aux;
- for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
- if (aux->hash == hash && aux->len == len &&
- memcmp(aux->name, name, len) == 0)
- return 1;
- }
- return 0;
- }
- static void define_config(const char *name, int len, unsigned int hash)
- {
- struct item *aux = malloc(sizeof(*aux) + len);
- if (!aux) {
- perror("fixdep:malloc");
- exit(1);
- }
- memcpy(aux->name, name, len);
- aux->len = len;
- aux->hash = hash;
- aux->next = hashtab[hash % HASHSZ];
- hashtab[hash % HASHSZ] = aux;
- }
- static void use_config(const char *m, int slen)
- {
- unsigned int hash = strhash(m, slen);
- int c, i;
- if (is_defined_config(m, slen, hash))
- return;
- define_config(m, slen, hash);
- printf(" $(wildcard include/config/");
- for (i = 0; i < slen; i++) {
- c = m[i];
- if (c == '_')
- c = '/';
- else
- c = tolower(c);
- putchar(c);
- }
- printf(".h) \\\n");
- }
- static void parse_config_file(const char *map, size_t len)
- {
- const int *end = (const int *) (map + len);
-
- const int *m = (const int *) map + 1;
- const char *p, *q;
- char tmp_buf[256] = "SPL_";
- for (; m < end; m++) {
- if (*m == INT_CONF) { p = (char *) m ; goto conf; }
- if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
- if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
- if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
- continue;
- conf:
- if (p > map + len - 7)
- continue;
- if (memcmp(p, "CONFIG_", 7))
- continue;
- p += 7;
- for (q = p; q < map + len; q++) {
- if (!(isalnum(*q) || *q == '_'))
- goto found;
- }
- continue;
- found:
- if (!memcmp(q - 7, "_MODULE", 7))
- q -= 7;
- if (q - p < 0)
- continue;
-
- if ((q - p == 10 && !memcmp(p, "IS_ENABLED(", 11)) ||
- (q - p == 10 && !memcmp(p, "IS_BUILTIN(", 11)) ||
- (q - p == 9 && !memcmp(p, "IS_MODULE(", 10))) {
- p = q + 1;
- for (q = p; q < map + len; q++)
- if (*q == ')')
- goto found2;
- continue;
- found2:
- if (is_spl_build) {
- memcpy(tmp_buf + 4, p, q - p);
- q = tmp_buf + 4 + (q - p);
- p = tmp_buf;
- }
- }
-
- use_config(p, q - p);
- }
- }
- static 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);
- }
- static void do_config_file(const char *filename)
- {
- struct stat st;
- int fd;
- void *map;
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "fixdep: error opening config file: ");
- perror(filename);
- exit(2);
- }
- if (fstat(fd, &st) < 0) {
- fprintf(stderr, "fixdep: error fstat'ing config file: ");
- perror(filename);
- exit(2);
- }
- 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);
- }
- static void parse_dep_file(void *map, size_t len)
- {
- char *m = map;
- char *end = m + len;
- char *p;
- char s[PATH_MAX];
- int is_target;
- int saw_any_target = 0;
- int is_first_dep = 0;
- while (m < end) {
-
- while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
- m++;
-
- p = m;
- while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
- p++;
-
- is_target = (*(p-1) == ':');
-
- if (is_target) {
-
- is_first_dep = 1;
- } else {
-
- memcpy(s, m, p-m);
- s[p - m] = 0;
-
- if (strrcmp(s, "include/generated/autoconf.h") &&
- strrcmp(s, "arch/um/include/uml-config.h") &&
- strrcmp(s, "include/linux/kconfig.h") &&
- strrcmp(s, ".ver")) {
-
- if (is_first_dep) {
-
- if (!saw_any_target) {
- saw_any_target = 1;
- printf("source_%s := %s\n\n",
- target, s);
- printf("deps_%s := \\\n",
- target);
- }
- is_first_dep = 0;
- } else
- printf(" %s \\\n", s);
- do_config_file(s);
- }
- }
-
- m = p + 1;
- }
- if (!saw_any_target) {
- fprintf(stderr, "fixdep: parse error; no targets found\n");
- exit(1);
- }
- printf("\n%s: $(deps_%s)\n\n", target, target);
- printf("$(deps_%s):\n", target);
- }
- static void print_deps(void)
- {
- struct stat st;
- int fd;
- void *map;
- fd = open(depfile, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "fixdep: error opening depfile: ");
- perror(depfile);
- exit(2);
- }
- if (fstat(fd, &st) < 0) {
- fprintf(stderr, "fixdep: error fstat'ing depfile: ");
- perror(depfile);
- exit(2);
- }
- 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);
- }
- static void traps(void)
- {
- static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
- int *p = (int *)test;
- if (*p != INT_CONF) {
- fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
- *p);
- exit(2);
- }
- }
- int main(int argc, char *argv[])
- {
- traps();
- if (argc != 4)
- usage();
- depfile = argv[1];
- target = argv[2];
- cmdline = argv[3];
-
- if (!strncmp(target, "spl/", 4) || !strncmp(target, "tpl/", 4))
- is_spl_build = 1;
- print_cmdline();
- print_deps();
- return 0;
- }
|