/* Copyright (C) 2015 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include #include #include #include #include #include #include FILE *mf, *oi, *rf, *rfo; char starting_dir[2000]; char top_dir[2000]; char path[2000]; int do_oh_files = 0; void process_makefile(char *path_end, const char *mk, FILE *rf) { FILE *oh; int last_was_nl = 1; int ch; strcpy(path_end, mk); oh = fopen(path, "r"); if (oh == 0) return; *path_end = 0; while ((ch = fgetc(oh)) != EOF) { if (ch == '\n' && last_was_nl) continue; if (ch != '\n' && last_was_nl) fprintf(oi, "OBJS += "); last_was_nl = (ch == '\n'); if (ch == '&') { fprintf(oi, "%s", path+2); fprintf(rf, "%s", path+2); } else { fputc(ch, oi); fputc(ch, rf); } } fclose(oh); } int sort_alpha(const void *va, const void *vb) { char *a = *(char **)va; char *b = *(char **)vb; return strcmp(a, b); } void find(void) { char *path_end; DIR *d; struct dirent *de; char **subs; int nsubs=0, msubs=10, i; struct stat s; path_end = path+strlen(path); /* Find all subdirectories and any makefile */ d = opendir(path); if (d == 0) { fprintf(stderr, "Error: cannot open %s\n", path); exit(1); } *path_end = '/'; subs = (char **)malloc(msubs*sizeof(char *)); while ((de = readdir(d)) != NULL) { /* Skip . and .. */ if (de->d_name[0] == '.') continue; /* optimization: no djgpp src directories have dots, so skip stat() for them */ if (strchr(de->d_name, '.')) continue; /* See if what we found is a directory */ strcpy(path_end+1, de->d_name); stat(path, &s); if (S_ISDIR(s.st_mode)) { if (nsubs >= msubs) { msubs += 10; subs = (char **)realloc(subs, msubs * sizeof(char *)); } subs[nsubs] = (char *)malloc(strlen(de->d_name)+1); strcpy(subs[nsubs], de->d_name); nsubs++; } /* Don't check the top directory! */ if (strcmp(path, "./makefile") == 0) continue; /* See if the current directory has a makefile */ if (strcmp(de->d_name, "makefile") == 0) { if (!do_oh_files) { *path_end = 0; if (path[1]) { /* fprintf(mf, "\t@echo =---------- make $(SUBARGS) in %s\n", path+2); */ fprintf(mf, "\t$(MAKE) -C %s $(SUBARGS)\n", path+2); } } else { process_makefile(path_end, "/makefile.oh", rf); process_makefile(path_end, "/makefile.oho", rfo); } *path_end = '/'; } } closedir(d); /* Now, descend into subdirectories, in asciibetical order */ qsort(subs, nsubs, sizeof(char *), sort_alpha); for (i=0; i 2 && strcmp(argv[1], "-C") == 0) { chdir(argv[2]); argc -= 2; argv += 2; } getcwd(top_dir, 200); notepwd = strrchr(top_dir, '/'); if (notepwd) notepwd++; else notepwd = top_dir; if (argc > 1 && strcmp(argv[1], "-2") == 0) { do_oh_files = 1; printf("makemake: scanning %s for object files\n", notepwd); } else printf("makemake: scanning %s for makefiles\n", notepwd); if (!do_oh_files) mf = fopen("makefile.sub", "w"); else { oi = fopen("makefile.oi", "w"); rf = fopen("makefile.rf2", "w"); rfo = fopen("makefile.rf3", "w"); } if (!do_oh_files) fprintf(mf, "all_subs:\n"); strcpy(path, "."); find(); chdir(starting_dir); if (!do_oh_files) fclose(mf); else { fclose(oi); fclose(rf); fclose(rfo); move_if_change("makefile.rf2", "makefile.rf"); move_if_change("makefile.rf3", "makefile.rfo"); } return 0; }