movie.get_last_movie()
[lsnes.git] / buildaux / mkdeps.cpp
blobcbb106bedaa83705611e39cbdbeb3876f292b5af
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 bool is_cmdhelp_file(const std::string& filename)
14 std::string _filename = filename;
15 return (_filename.length() > 8 && _filename.substr(0, 8) == "cmdhelp/");
18 std::string search_include(const std::list<std::string>& searchpath, const std::string& _filename,
19 const std::string& ref_by)
21 std::string filename = _filename;
22 //Hack: process cmdhelp includes internally as the date were for the JSON include.
23 if(is_cmdhelp_file(filename)) {
24 if(filename != "cmdhelp/inverselist.hpp") {
25 filename = "../src/" + filename;
26 //Replace the extension with .json.
27 size_t split = filename.find_last_of("./\\");
28 if(split < filename.length() && filename[split] == '.') {
29 filename = filename.substr(0, split) + ".json";
33 size_t p = ref_by.find_last_of("/");
34 if(p < ref_by.length()) {
35 std::string i = ref_by;
36 i = i.substr(0, p);
37 std::string real_fn = i + "/" + filename;
38 boost_fs::path p(real_fn);
39 if(boost_fs::exists(p) && boost_fs::is_regular_file(p))
40 return real_fn;
42 for(auto& i : searchpath) {
43 std::string real_fn = i + "/" + filename;
44 boost_fs::path p(real_fn);
45 if(boost_fs::exists(p) && boost_fs::is_regular_file(p))
46 return real_fn;
48 std::cerr << "WARNING: Include file '" << filename << "' not found." << std::endl;
49 return "";
52 time_t get_timestamp(const std::string& path)
54 boost_fs::path p(path);
55 if(!boost_fs::exists(p)) return 0;
56 return boost_fs::last_write_time(p);
59 time_t recursive_scan(const std::list<std::string>& searchpath, const std::string& filename,
60 std::map<std::string, time_t>& scanned)
62 if(filename == "")
63 return 0;
64 if(scanned.count(filename))
65 return 0;
66 std::ifstream fp(filename);
67 if(!fp) {
68 std::cerr << "WARNING: File '" << filename << "' can't be opened." << std::endl;
69 return 0;
71 time_t newest = get_timestamp(filename);
72 scanned[filename] = newest;
73 std::string tmp;
74 while(std::getline(fp, tmp)) {
75 if(tmp.length() > 0 && tmp[0] == '#') {
76 //Possibly include.
77 std::string included;
78 if(strncmp(tmp.c_str(), "#include", 8))
79 continue;
80 size_t ptr = 8;
81 while(ptr < tmp.length() && isspace((unsigned char)tmp[ptr]))
82 ptr++;
83 if(ptr == tmp.length())
84 continue;
85 if(tmp[ptr] != '\"')
86 continue;
87 size_t iptr = ++ptr;
88 while(ptr < tmp.length() && tmp[ptr] != '\"')
89 ptr++;
90 if(ptr == tmp.length())
91 continue;
92 included = tmp.substr(iptr, ptr - iptr);
93 newest = std::max(newest, recursive_scan(searchpath, search_include(searchpath, included,
94 filename), scanned));
97 return newest;
100 int main(int argc, char** argv)
102 std::list<std::string> searchpath;
103 std::list<std::string> files;
104 bool step = false;
105 for(int i = 1; i < argc; i++) {
106 if(!step && !strcmp(argv[i], "--"))
107 step = true;
108 else if(!step)
109 searchpath.push_back(argv[i]);
110 else
111 files.push_back(argv[i]);
113 searchpath.push_back(".");
114 for(auto& i : files) {
115 std::map<std::string, time_t> x;
116 time_t t = recursive_scan(searchpath, i, x);
117 if(get_timestamp(i + ".dep") < t) {
118 std::ofstream y(i + ".dep");
119 for(auto& j : x)
120 y << j.second << " " << j.first << std::endl;
123 return 0;