lsnes_vsetc -> lsnes_instance.setcache
[lsnes.git] / buildaux / mkdeps.cpp
blobf69cd561768f89e125dfaf32747e4001b4d588ec
1 #include <boost/filesystem.hpp>
2 #include <sys/time.h>
3 #include <fstream>
4 #include <cctype>
5 #include <set>
6 #include <map>
7 #include <iostream>
8 #include <string>
10 namespace boost_fs = boost::filesystem;
12 std::string search_include(const std::list<std::string>& searchpath, const std::string& filename,
13 const std::string& ref_by)
15 size_t p = ref_by.find_last_of("/");
16 if(p < ref_by.length()) {
17 std::string i = ref_by;
18 i = i.substr(0, p);
19 std::string real_fn = i + "/" + filename;
20 boost_fs::path p(real_fn);
21 if(boost_fs::exists(p) && boost_fs::is_regular_file(p))
22 return real_fn;
24 for(auto& i : searchpath) {
25 std::string real_fn = i + "/" + filename;
26 boost_fs::path p(real_fn);
27 if(boost_fs::exists(p) && boost_fs::is_regular_file(p))
28 return real_fn;
30 std::cerr << "WARNING: Include file '" << filename << "' not found." << std::endl;
31 return "";
34 time_t get_timestamp(const std::string& path)
36 boost_fs::path p(path);
37 if(!boost_fs::exists(p)) return 0;
38 return boost_fs::last_write_time(p);
41 time_t recursive_scan(const std::list<std::string>& searchpath, const std::string& filename,
42 std::map<std::string, time_t>& scanned)
44 if(filename == "")
45 return 0;
46 if(scanned.count(filename))
47 return 0;
48 std::ifstream fp(filename);
49 if(!fp) {
50 std::cerr << "WARNING: File '" << filename << "' can't be opened." << std::endl;
51 return 0;
53 time_t newest = get_timestamp(filename);
54 scanned[filename] = newest;
55 std::string tmp;
56 while(std::getline(fp, tmp)) {
57 if(tmp.length() > 0 && tmp[0] == '#') {
58 //Possibly include.
59 std::string included;
60 if(strncmp(tmp.c_str(), "#include", 8))
61 continue;
62 size_t ptr = 8;
63 while(ptr < tmp.length() && isspace((unsigned char)tmp[ptr]))
64 ptr++;
65 if(ptr == tmp.length())
66 continue;
67 if(tmp[ptr] != '\"')
68 continue;
69 size_t iptr = ++ptr;
70 while(ptr < tmp.length() && tmp[ptr] != '\"')
71 ptr++;
72 if(ptr == tmp.length())
73 continue;
74 included = tmp.substr(iptr, ptr - iptr);
75 newest = std::max(newest, recursive_scan(searchpath, search_include(searchpath, included,
76 filename), scanned));
79 return newest;
82 int main(int argc, char** argv)
84 std::list<std::string> searchpath;
85 std::list<std::string> files;
86 bool step = false;
87 for(int i = 1; i < argc; i++) {
88 if(!step && !strcmp(argv[i], "--"))
89 step = true;
90 else if(!step)
91 searchpath.push_back(argv[i]);
92 else
93 files.push_back(argv[i]);
95 searchpath.push_back(".");
96 for(auto& i : files) {
97 std::map<std::string, time_t> x;
98 time_t t = recursive_scan(searchpath, i, x);
99 if(get_timestamp(i + ".dep") < t) {
100 std::ofstream y(i + ".dep");
101 for(auto& j : x)
102 y << j.second << " " << j.first << std::endl;
105 return 0;