diff options
author | yenatch <yenatch@gmail.com> | 2017-07-22 18:49:38 -0400 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2017-07-22 18:49:38 -0400 |
commit | 0faa6c9c93804c4ce0e6fc8e5a6d5cfbc85f01da (patch) | |
tree | 7e42c1ad480d5d0c49e8fd45f2f8766bb0280922 | |
parent | 5e80a12ec9fcf2201c070712d8e2dcefe2a0e364 (diff) |
scaninc: read c includes
Now editing .h files triggers a rebuild.
Also allow .h and .inc files to be passed as a main argument.
src/ and include/ are temporarily hardcoded.
-rw-r--r-- | tools/scaninc/c_file.cpp | 57 | ||||
-rw-r--r-- | tools/scaninc/c_file.h | 4 | ||||
-rw-r--r-- | tools/scaninc/scaninc.cpp | 41 |
3 files changed, 85 insertions, 17 deletions
diff --git a/tools/scaninc/c_file.cpp b/tools/scaninc/c_file.cpp index b82276dd6..2dd400967 100644 --- a/tools/scaninc/c_file.cpp +++ b/tools/scaninc/c_file.cpp @@ -145,6 +145,7 @@ void CFile::FindIncbins() } else { + CheckInclude(); CheckIncbin(); if (m_pos >= m_size) @@ -211,6 +212,29 @@ bool CFile::CheckIdentifier(const std::string& ident) return (i == ident.length()); } +void CFile::CheckInclude() +{ + if (m_buffer[m_pos] != '#') + return; + + std::string ident = "#include"; + + if (!CheckIdentifier(ident)) + { + return; + } + + m_pos += ident.length(); + + ConsumeHorizontalWhitespace(); + + std::string path = ReadPath(); + + if (!path.empty()) { + m_includes.emplace(path); + } +} + void CFile::CheckIncbin() { std::string idents[6] = { "INCBIN_S8", "INCBIN_U8", "INCBIN_S16", "INCBIN_U16", "INCBIN_S32", "INCBIN_U32" }; @@ -246,8 +270,28 @@ void CFile::CheckIncbin() SkipWhitespace(); + std::string path = ReadPath(); + + SkipWhitespace(); + + if (m_buffer[m_pos] != ')') + FATAL_INPUT_ERROR("expected ')'"); + + m_pos++; + + m_incbins.emplace(path); +} + +std::string CFile::ReadPath() +{ if (m_buffer[m_pos] != '"') - FATAL_INPUT_ERROR("expected double quote"); + { + if (m_buffer[m_pos] == '<') + { + return std::string(); + } + FATAL_INPUT_ERROR("expected '\"' or '<'"); + } m_pos++; @@ -272,16 +316,7 @@ void CFile::CheckIncbin() m_pos++; } - std::string path(&m_buffer[startPos], m_pos - startPos); - m_pos++; - SkipWhitespace(); - - if (m_buffer[m_pos] != ')') - FATAL_INPUT_ERROR("expected ')'"); - - m_pos++; - - m_incbins.emplace(path); + return std::string(m_buffer, startPos, m_pos - 1 - startPos); } diff --git a/tools/scaninc/c_file.h b/tools/scaninc/c_file.h index 922cb4639..38b10f04e 100644 --- a/tools/scaninc/c_file.h +++ b/tools/scaninc/c_file.h @@ -33,6 +33,7 @@ public: ~CFile(); void FindIncbins(); const std::set<std::string>& GetIncbins() { return m_incbins; } + const std::set<std::string>& GetIncludes() { return m_includes; } private: char *m_buffer; @@ -41,13 +42,16 @@ private: int m_lineNum; std::string m_path; std::set<std::string> m_incbins; + std::set<std::string> m_includes; void RemoveComments(); bool ConsumeHorizontalWhitespace(); bool ConsumeNewline(); void SkipWhitespace(); bool CheckIdentifier(const std::string& ident); + void CheckInclude(); void CheckIncbin(); + std::string ReadPath(); }; #endif // C_FILE_H diff --git a/tools/scaninc/scaninc.cpp b/tools/scaninc/scaninc.cpp index b6f7ba767..10cb06eee 100644 --- a/tools/scaninc/scaninc.cpp +++ b/tools/scaninc/scaninc.cpp @@ -55,16 +55,45 @@ int main(int argc, char **argv) std::string extension = initialPath.substr(pos + 1); - if (extension == "c") + std::string srcDir("src/"); + std::string includeDir("include/"); + + if (extension == "c" || extension == "h") { - CFile file(initialPath); + filesToProcess.push(initialPath); + + while (!filesToProcess.empty()) + { + CFile file(filesToProcess.top()); + filesToProcess.pop(); - file.FindIncbins(); - dependencies = file.GetIncbins(); + file.FindIncbins(); + for (auto incbin : file.GetIncbins()) + { + dependencies.insert(incbin); + } + for (auto include : file.GetIncludes()) + { + std::string path(srcDir + include); + if (!CanOpenFile(path)) + { + path = includeDir + include; + } + + if (CanOpenFile(path)) + { + bool inserted = dependencies.insert(path).second; + if (inserted) + { + filesToProcess.push(path); + } + } + } + } } - else if (extension == "s") + else if (extension == "s" || extension == "inc") { - filesToProcess.push(std::string(argv[1])); + filesToProcess.push(initialPath); while (!filesToProcess.empty()) { |