int include_mapping(const char *srcfile) { // read source file void *buffer; UINT32 length; file_error filerr = core_fload(srcfile, &buffer, &length); if (filerr != FILERR_NONE) { fprintf(stderr, "Unable to read source file '%s'\n", srcfile); return 1; } // rip through it to find all drivers char *srcptr = (char *)buffer; char *endptr = srcptr + length; while (srcptr < endptr) { char c = *srcptr++; // count newlines if (c == 13 || c == 10) { if (c == 13 && *srcptr == 10) srcptr++; continue; } // look for start of C comment if (c == '#' && *srcptr == '@') { srcptr++; //mapping char filename[256]; filename[0] = 0; for (int pos = 0; srcptr < endptr && pos < ARRAY_LENGTH(filename) - 1 && (*srcptr!=','); pos++) { filename[pos] = *srcptr++; filename[pos+1] = 0; } srcptr++; // skip comma char mapping[256]; mapping[0] = 0; for (int pos = 0; srcptr < endptr && pos < ARRAY_LENGTH(mapping) - 1 && (*srcptr!=10) && (*srcptr!=13); pos++) { mapping[pos] = *srcptr++; mapping[pos+1] = 0; } include_map.add(filename,core_strdup(mapping)); continue; } for (int pos = 0; srcptr < endptr && !isspace(*srcptr); pos++) { c = *srcptr++; } } osd_free(buffer); return 0; }
static file_entry &compute_dependencies(int srcrootlen, astring &srcfile) { // see if we already have an entry astring normalfile(srcfile); normalfile.replacechr(PATH_SEPARATOR[0], '/'); file_entry *foundfile = file_map.find(normalfile); if (foundfile != NULL) return *foundfile; // create a new header entry file_entry &file = *new file_entry; file.deplist = NULL; file.name = normalfile; file_map.add(file.name, &file); // read the source file UINT32 filelength; char *filedata; if (core_fload(srcfile, (void **)&filedata, &filelength) != FILERR_NONE) { fprintf(stderr, "Unable to read file '%s'\n", srcfile.cstr()); return file; } // find the #include directives in this file for (int index = 0; index < filelength; index++) if (filedata[index] == '#' && strncmp(&filedata[index + 1], "include", 7) == 0) { // first make sure we're not commented or quoted bool just_continue = false; for (int scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--) if ((filedata[scan] == '/' && filedata[scan - 1] == '/') || filedata[scan] == '"') { just_continue = true; break; } if (just_continue) continue; // scan forward to find the quotes or bracket index += 7; int scan; for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ; // ignore if not found or if it's bracketed if (scan >= filelength || filedata[scan] != '"') continue; int start = ++scan; // find the closing quote while (scan < filelength && filedata[scan] != '"') scan++; if (scan >= filelength) continue; // find the include file astring filename(&filedata[start], scan - start); astring target; // create a new dependency if (find_include_file(target, srcrootlen, srcfile, filename)) { dependency *dep = new dependency; dep->next = file.deplist; file.deplist = dep; dep->file = &compute_dependencies(srcrootlen, target); } } osd_free(filedata); return file; }
static int recurse_dir(astring &srcdir) { int result = 0; // iterate through each file for (librarylist_entry *lib = librarylist; lib != NULL; lib = lib->next) { for (list_entry *src = lib->sourcefiles; src != NULL; src = src->next) { astring srcfile; // build the source filename srcfile.printf("%s%s.c", srcdir.cstr(), src->name.cstr()); dependency_map depend_map; // find dependencies file_entry &file = compute_dependencies(srcfile); recurse_dependencies(file, depend_map); for (dependency_map::entry_t *entry = depend_map.first(); entry != NULL; entry = depend_map.next(entry)) { astring t(entry->tag()); if (core_filename_ends_with(t, ".h")) { char *foundfile = include_map.find(t); if (foundfile != NULL) { printf("%s\n", foundfile); // we add things just once when needed include_map.remove(t); } } } } } // iterate through each file for (librarylist_entry *lib = librarylist; lib != NULL; lib = lib->next) { // convert the target from source to object (makes assumptions about rules) astring target("$(OBJ)/target/",lib->name.cstr()); target.cat(".a"); printf("\n%s : \\\n", target.cstr()); for (list_entry *src = lib->sourcefiles; src != NULL; src = src->next) { astring srcfile; // build the source filename srcfile.printf("%s%s.c", srcdir.cstr(), src->name.cstr()); dependency_map depend_map; // find dependencies file_entry &file = compute_dependencies(srcfile); recurse_dependencies(file, depend_map); // iterate over the hashed dependencies and output them as well for (dependency_map::entry_t *entry = depend_map.first(); entry != NULL; entry = depend_map.next(entry)) { astring t(entry->tag()); t.replace(0, "src/", "$(OBJ)/"); t.replace(0, ".c", ".o"); if (core_filename_ends_with(t, ".o")) { printf("\t%s \\\n", t.cstr()); } } } printf("\n"); for (list_entry *src = lib->sourcefiles; src != NULL; src = src->next) { astring srcfile; // build the source filename srcfile.printf("%s%s.c", srcdir.cstr(), src->name.cstr()); dependency_map depend_map; // find dependencies file_entry &file = compute_dependencies(srcfile); recurse_dependencies(file, depend_map); for (dependency_map::entry_t *entry = depend_map.first(); entry != NULL; entry = depend_map.next(entry)) { astring t(entry->tag()); if (core_filename_ends_with(t, ".lay")) { astring target2(file.name); target2.replace(0, "src/", "$(OBJ)/"); target2.replace(0, ".c", ".o"); t.replace(0, "src/", "$(OBJ)/"); t.replace(0, ".lay", ".lh"); printf("%s: %s\n", target2.cstr(), t.cstr()); } if (core_filename_ends_with(t, ".inc")) { astring target2(file.name); target2.replace(0, "src/", "$(OBJ)/"); target2.replace(0, ".c", ".o"); printf("%s: %s\n", target2.cstr(), t.cstr()); } } } } return result; }