From 986edc54cd8fae8f9b05f5d3ad3b670f6547bbbc Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 23 Nov 2021 06:36:40 +0100 Subject: moved scaninc --- tools/scaninc/.gitignore | 1 - tools/scaninc/LICENSE | 19 --- tools/scaninc/Makefile | 18 --- tools/scaninc/asm_file.cpp | 192 ------------------------ tools/scaninc/asm_file.h | 119 --------------- tools/scaninc/c_file.cpp | 307 -------------------------------------- tools/scaninc/c_file.h | 57 ------- tools/scaninc/scaninc.cpp | 130 ---------------- tools/scaninc/scaninc.h | 59 -------- tools/scaninc/source_file.cpp | 125 ---------------- tools/scaninc/source_file.h | 71 --------- tools/src/CMakeLists.txt | 1 + tools/src/scaninc/CMakeLists.txt | 6 + tools/src/scaninc/LICENSE | 19 +++ tools/src/scaninc/asm_file.cpp | 192 ++++++++++++++++++++++++ tools/src/scaninc/asm_file.h | 119 +++++++++++++++ tools/src/scaninc/c_file.cpp | 307 ++++++++++++++++++++++++++++++++++++++ tools/src/scaninc/c_file.h | 57 +++++++ tools/src/scaninc/scaninc.cpp | 130 ++++++++++++++++ tools/src/scaninc/scaninc.h | 59 ++++++++ tools/src/scaninc/source_file.cpp | 125 ++++++++++++++++ tools/src/scaninc/source_file.h | 71 +++++++++ 22 files changed, 1086 insertions(+), 1098 deletions(-) delete mode 100755 tools/scaninc/.gitignore delete mode 100755 tools/scaninc/LICENSE delete mode 100755 tools/scaninc/Makefile delete mode 100755 tools/scaninc/asm_file.cpp delete mode 100755 tools/scaninc/asm_file.h delete mode 100755 tools/scaninc/c_file.cpp delete mode 100755 tools/scaninc/c_file.h delete mode 100755 tools/scaninc/scaninc.cpp delete mode 100755 tools/scaninc/scaninc.h delete mode 100755 tools/scaninc/source_file.cpp delete mode 100755 tools/scaninc/source_file.h create mode 100644 tools/src/scaninc/CMakeLists.txt create mode 100755 tools/src/scaninc/LICENSE create mode 100755 tools/src/scaninc/asm_file.cpp create mode 100755 tools/src/scaninc/asm_file.h create mode 100755 tools/src/scaninc/c_file.cpp create mode 100755 tools/src/scaninc/c_file.h create mode 100755 tools/src/scaninc/scaninc.cpp create mode 100755 tools/src/scaninc/scaninc.h create mode 100755 tools/src/scaninc/source_file.cpp create mode 100755 tools/src/scaninc/source_file.h (limited to 'tools') diff --git a/tools/scaninc/.gitignore b/tools/scaninc/.gitignore deleted file mode 100755 index 94bfbf98..00000000 --- a/tools/scaninc/.gitignore +++ /dev/null @@ -1 +0,0 @@ -scaninc diff --git a/tools/scaninc/LICENSE b/tools/scaninc/LICENSE deleted file mode 100755 index b66bf81c..00000000 --- a/tools/scaninc/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2015 YamaArashi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/tools/scaninc/Makefile b/tools/scaninc/Makefile deleted file mode 100755 index 1516f159..00000000 --- a/tools/scaninc/Makefile +++ /dev/null @@ -1,18 +0,0 @@ -CXX = g++ - -CXXFLAGS = -Wall -Werror -std=c++11 -O2 - -SRCS = scaninc.cpp c_file.cpp asm_file.cpp source_file.cpp - -HEADERS := scaninc.h asm_file.h c_file.h source_file.h - -.PHONY: all clean - -all: scaninc - @: - -scaninc: $(SRCS) $(HEADERS) - $(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS) - -clean: - $(RM) scaninc scaninc.exe diff --git a/tools/scaninc/asm_file.cpp b/tools/scaninc/asm_file.cpp deleted file mode 100755 index 109e604a..00000000 --- a/tools/scaninc/asm_file.cpp +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright(c) 2015-2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include -#include -#include "scaninc.h" -#include "asm_file.h" - -AsmFile::AsmFile(std::string path) -{ - m_path = path; - - FILE *fp = std::fopen(path.c_str(), "rb"); - - if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); - - std::fseek(fp, 0, SEEK_END); - - m_size = std::ftell(fp); - - m_buffer = new char[m_size]; - - std::rewind(fp); - - if (std::fread(m_buffer, m_size, 1, fp) != 1) - FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); - - std::fclose(fp); - - m_pos = 0; - m_lineNum = 1; -} - -AsmFile::~AsmFile() -{ - delete[] m_buffer; -} - -IncDirectiveType AsmFile::ReadUntilIncDirective(std::string &path) -{ - // At the beginning of each loop iteration, the current file position - // should be at the start of a line or at the end of the file. - for (;;) - { - SkipTabsAndSpaces(); - - IncDirectiveType incDirectiveType = IncDirectiveType::None; - - char c = PeekChar(); - if (c == '.' || c == '#') - { - m_pos++; - - if (MatchIncDirective("incbin", path)) - incDirectiveType = IncDirectiveType::Incbin; - else if (MatchIncDirective("include", path)) - incDirectiveType = IncDirectiveType::Include; - } - - for (;;) - { - int c = GetChar(); - - if (c == -1) - return incDirectiveType; - - if (c == ';') - { - SkipEndOfLineComment(); - break; - } - else if (c == '/' && PeekChar() == '*') - { - m_pos++; - SkipMultiLineComment(); - } - else if (c == '"') - { - SkipString(); - } - else if (c == '\n') - { - break; - } - } - - if (incDirectiveType != IncDirectiveType::None) - return incDirectiveType; - } -} - -std::string AsmFile::ReadPath() -{ - int length = 0; - int startPos = m_pos; - - for (;;) - { - int c = GetChar(); - - if (c == '"') - break; - - if (c == -1) - FATAL_INPUT_ERROR("unexpected EOF in include string\n"); - - if (c == 0) - FATAL_INPUT_ERROR("unexpected NUL character in include string\n"); - - if (c == '\n') - FATAL_INPUT_ERROR("unexpected end of line character in include string\n"); - - // Don't bother allowing any escape sequences. - if (c == '\\') - FATAL_INPUT_ERROR("unexpected escape in include string\n"); - - length++; - - if (length > SCANINC_MAX_PATH) - FATAL_INPUT_ERROR("path is too long"); - } - - return std::string(m_buffer + startPos, length); -} - -void AsmFile::SkipEndOfLineComment() -{ - int c; - - do - { - c = GetChar(); - } while (c != -1 && c != '\n'); -} - -void AsmFile::SkipMultiLineComment() -{ - for (;;) - { - int c = GetChar(); - - if (c == '*') - { - if (PeekChar() == '/') - { - m_pos++; - return; - } - } - else if (c == -1) - { - return; - } - } -} - -void AsmFile::SkipString() -{ - for (;;) - { - int c = GetChar(); - - if (c == '"') - break; - - if (c == -1) - FATAL_INPUT_ERROR("unexpected EOF in string\n"); - - if (c == '\\') - { - c = GetChar(); - } - } -} diff --git a/tools/scaninc/asm_file.h b/tools/scaninc/asm_file.h deleted file mode 100755 index ad99b757..00000000 --- a/tools/scaninc/asm_file.h +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright(c) 2015-2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#ifndef ASM_FILE_H -#define ASM_FILE_H - -#include -#include "scaninc.h" - -enum class IncDirectiveType -{ - None, - Include, - Incbin -}; - -class AsmFile -{ -public: - AsmFile(std::string path); - ~AsmFile(); - IncDirectiveType ReadUntilIncDirective(std::string& path); - -private: - char *m_buffer; - int m_pos; - int m_size; - int m_lineNum; - std::string m_path; - - int GetChar() - { - if (m_pos >= m_size) - return -1; - - int c = m_buffer[m_pos++]; - - if (c == '\r') - { - if (m_pos < m_size && m_buffer[m_pos++] == '\n') - { - m_lineNum++; - return '\n'; - } - else - { - FATAL_INPUT_ERROR("CR line endings are not supported\n"); - } - } - - if (c == '\n') - m_lineNum++; - - return c; - } - - // No newline translation because it's not needed for any use of this function. - int PeekChar() - { - if (m_pos >= m_size) - return -1; - - return m_buffer[m_pos]; - } - - void SkipTabsAndSpaces() - { - while (m_pos < m_size && (m_buffer[m_pos] == '\t' || m_buffer[m_pos] == ' ')) - m_pos++; - } - - bool MatchIncDirective(std::string directiveName, std::string& path) - { - int length = directiveName.length(); - int i; - - for (i = 0; i < length && m_pos + i < m_size; i++) - if (directiveName[i] != m_buffer[m_pos + i]) - return false; - - if (i < length) - return false; - - m_pos += length; - - SkipTabsAndSpaces(); - - if (GetChar() != '"') - FATAL_INPUT_ERROR("no path after \".%s\" directive\n", directiveName.c_str()); - - path = ReadPath(); - - return true; - } - - std::string ReadPath(); - void SkipEndOfLineComment(); - void SkipMultiLineComment(); - void SkipString(); -}; - -#endif // ASM_FILE_H diff --git a/tools/scaninc/c_file.cpp b/tools/scaninc/c_file.cpp deleted file mode 100755 index 595f366c..00000000 --- a/tools/scaninc/c_file.cpp +++ /dev/null @@ -1,307 +0,0 @@ -// Copyright(c) 2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "c_file.h" - -CFile::CFile(std::string path) -{ - m_path = path; - - FILE *fp = std::fopen(path.c_str(), "rb"); - - if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); - - std::fseek(fp, 0, SEEK_END); - - m_size = std::ftell(fp); - - m_buffer = new char[m_size + 1]; - m_buffer[m_size] = 0; - - std::rewind(fp); - - if (std::fread(m_buffer, m_size, 1, fp) != 1) - FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); - - std::fclose(fp); - - m_pos = 0; - m_lineNum = 1; -} - -CFile::~CFile() -{ - delete[] m_buffer; -} - -void CFile::FindIncbins() -{ - char stringChar = 0; - - while (m_pos < m_size) - { - if (stringChar) - { - if (m_buffer[m_pos] == stringChar) - { - m_pos++; - stringChar = 0; - } - else if (m_buffer[m_pos] == '\\' && m_buffer[m_pos + 1] == stringChar) - { - m_pos += 2; - } - else - { - if (m_buffer[m_pos] == '\n') - m_lineNum++; - m_pos++; - } - } - else - { - SkipWhitespace(); - CheckInclude(); - CheckIncbin(); - - if (m_pos >= m_size) - break; - - char c = m_buffer[m_pos++]; - - if (c == '\n') - m_lineNum++; - else if (c == '"') - stringChar = '"'; - else if (c == '\'') - stringChar = '\''; - else if (c == 0) - FATAL_INPUT_ERROR("unexpected null character"); - } - } -} - -bool CFile::ConsumeHorizontalWhitespace() -{ - if (m_buffer[m_pos] == '\t' || m_buffer[m_pos] == ' ') - { - m_pos++; - return true; - } - - return false; -} - -bool CFile::ConsumeNewline() -{ - if (m_buffer[m_pos] == '\n') - { - m_pos++; - m_lineNum++; - return true; - } - - if (m_buffer[m_pos] == '\r' && m_buffer[m_pos + 1] == '\n') - { - m_pos += 2; - m_lineNum++; - return true; - } - - return false; -} - -bool CFile::ConsumeComment() -{ - if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '*') - { - m_pos += 2; - while (m_buffer[m_pos] != '*' || m_buffer[m_pos + 1] != '/') - { - if (m_buffer[m_pos] == 0) - return false; - if (!ConsumeNewline()) - m_pos++; - } - m_pos += 2; - return true; - } - else if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '/') - { - m_pos += 2; - while (!ConsumeNewline()) - { - if (m_buffer[m_pos] == 0) - return false; - m_pos++; - } - return true; - } - - return false; -} - -void CFile::SkipWhitespace() -{ - while (ConsumeHorizontalWhitespace() || ConsumeNewline() || ConsumeComment()) - ; -} - -bool CFile::CheckIdentifier(const std::string& ident) -{ - unsigned int i; - - for (i = 0; i < ident.length() && m_pos + i < (unsigned)m_size; i++) - if (ident[i] != m_buffer[m_pos + i]) - return false; - - 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() -{ - // Optimization: assume most lines are not incbins - if (!(m_buffer[m_pos+0] == 'I' - && m_buffer[m_pos+1] == 'N' - && m_buffer[m_pos+2] == 'C' - && m_buffer[m_pos+3] == 'B' - && m_buffer[m_pos+4] == 'I' - && m_buffer[m_pos+5] == 'N' - && m_buffer[m_pos+6] == '_')) - { - return; - } - - std::string idents[6] = { "INCBIN_S8", "INCBIN_U8", "INCBIN_S16", "INCBIN_U16", "INCBIN_S32", "INCBIN_U32" }; - int incbinType = -1; - - for (int i = 0; i < 6; i++) - { - if (CheckIdentifier(idents[i])) - { - incbinType = i; - break; - } - } - - if (incbinType == -1) - return; - - long oldPos = m_pos; - long oldLineNum = m_lineNum; - - m_pos += idents[incbinType].length(); - - SkipWhitespace(); - - if (m_buffer[m_pos] != '(') - { - m_pos = oldPos; - m_lineNum = oldLineNum; - return; - } - - m_pos++; - - while (true) - { - SkipWhitespace(); - - std::string path = ReadPath(); - - SkipWhitespace(); - - m_incbins.emplace(path); - - if (m_buffer[m_pos] != ',') - break; - - m_pos++; - } - - if (m_buffer[m_pos] != ')') - FATAL_INPUT_ERROR("expected ')'"); - - m_pos++; - -} - -std::string CFile::ReadPath() -{ - if (m_buffer[m_pos] != '"') - { - if (m_buffer[m_pos] == '<') - { - return std::string(); - } - FATAL_INPUT_ERROR("expected '\"' or '<'"); - } - - m_pos++; - - int startPos = m_pos; - - while (m_buffer[m_pos] != '"') - { - if (m_buffer[m_pos] == 0) - { - if (m_pos >= m_size) - FATAL_INPUT_ERROR("unexpected EOF in path string"); - else - FATAL_INPUT_ERROR("unexpected null character in path string"); - } - - if (m_buffer[m_pos] == '\r' || m_buffer[m_pos] == '\n') - FATAL_INPUT_ERROR("unexpected end of line character in path string"); - - if (m_buffer[m_pos] == '\\') - FATAL_INPUT_ERROR("unexpected escape in path string"); - - m_pos++; - } - - m_pos++; - - return std::string(m_buffer + startPos, m_pos - 1 - startPos); -} diff --git a/tools/scaninc/c_file.h b/tools/scaninc/c_file.h deleted file mode 100755 index 618901b8..00000000 --- a/tools/scaninc/c_file.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright(c) 2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#ifndef C_FILE_H -#define C_FILE_H - -#include -#include -#include -#include "scaninc.h" - -class CFile -{ -public: - CFile(std::string path); - ~CFile(); - void FindIncbins(); - const std::set& GetIncbins() { return m_incbins; } - const std::set& GetIncludes() { return m_includes; } - -private: - char *m_buffer; - int m_pos; - int m_size; - int m_lineNum; - std::string m_path; - std::set m_incbins; - std::set m_includes; - - bool ConsumeHorizontalWhitespace(); - bool ConsumeNewline(); - bool ConsumeComment(); - 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 deleted file mode 100755 index 1bf7feb5..00000000 --- a/tools/scaninc/scaninc.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright(c) 2015-2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include -#include -#include -#include -#include -#include -#include "scaninc.h" -#include "source_file.h" - -bool CanOpenFile(std::string path) -{ - FILE *fp = std::fopen(path.c_str(), "rb"); - - if (fp == NULL) - return false; - - std::fclose(fp); - return true; -} - -const char *const USAGE = "Usage: scaninc [-I INCLUDE_PATH] FILE_PATH\n"; - -int main(int argc, char **argv) -{ - std::queue filesToProcess; - std::set dependencies; - - std::vector includeDirs; - - argc--; - argv++; - - while (argc > 1) - { - std::string arg(argv[0]); - if (arg.substr(0, 2) == "-I") - { - std::string includeDir = arg.substr(2); - if (includeDir.empty()) - { - argc--; - argv++; - includeDir = std::string(argv[0]); - } - if (!includeDir.empty() && includeDir.back() != '/') - { - includeDir += '/'; - } - includeDirs.push_back(includeDir); - } - else - { - FATAL_ERROR(USAGE); - } - argc--; - argv++; - } - - if (argc != 1) { - FATAL_ERROR(USAGE); - } - - std::string initialPath(argv[0]); - - filesToProcess.push(initialPath); - - while (!filesToProcess.empty()) - { - std::string filePath = filesToProcess.front(); - SourceFile file(filePath); - filesToProcess.pop(); - - includeDirs.push_back(file.GetSrcDir()); - for (auto incbin : file.GetIncbins()) - { - // Search for the incbin in the include directories as well. - for (auto includeDir : includeDirs) - { - std::string path(includeDir + incbin); - if (CanOpenFile(path)) - { - dependencies.insert(path); - break; - } - } - } - for (auto include : file.GetIncludes()) - { - for (auto includeDir : includeDirs) - { - std::string path(includeDir + include); - if (CanOpenFile(path)) - { - bool inserted = dependencies.insert(path).second; - if (inserted) - { - filesToProcess.push(path); - } - break; - } - } - } - includeDirs.pop_back(); - } - - for (const std::string &path : dependencies) - { - std::printf("%s\n", path.c_str()); - } -} diff --git a/tools/scaninc/scaninc.h b/tools/scaninc/scaninc.h deleted file mode 100755 index 30cc9611..00000000 --- a/tools/scaninc/scaninc.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright(c) 2015-2017 YamaArashi -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#ifndef SCANINC_H -#define SCANINC_H - -#include -#include - -#ifdef _MSC_VER - -#define FATAL_INPUT_ERROR(format, ...) \ -do { \ - fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, __VA_ARGS__); \ - exit(1); \ -} while (0) - -#define FATAL_ERROR(format, ...) \ -do { \ - fprintf(stderr, format, __VA_ARGS__); \ - exit(1); \ -} while (0) - -#else - -#define FATAL_INPUT_ERROR(format, ...) \ -do { \ - fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, ##__VA_ARGS__); \ - exit(1); \ -} while (0) - -#define FATAL_ERROR(format, ...) \ -do { \ - fprintf(stderr, format, ##__VA_ARGS__); \ - exit(1); \ -} while (0) - -#endif // _MSC_VER - -#define SCANINC_MAX_PATH 255 - -#endif // SCANINC_H diff --git a/tools/scaninc/source_file.cpp b/tools/scaninc/source_file.cpp deleted file mode 100755 index f23ff6db..00000000 --- a/tools/scaninc/source_file.cpp +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright(c) 2019 Phlosioneer -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include -#include "source_file.h" - - -SourceFileType GetFileType(std::string& path) -{ - std::size_t pos = path.find_last_of('.'); - - if (pos == std::string::npos) - FATAL_ERROR("no file extension in path \"%s\"\n", path.c_str()); - - std::string extension = path.substr(pos + 1); - - if (extension == "c") - return SourceFileType::Cpp; - else if (extension == "s") - return SourceFileType::Asm; - else if (extension == "h") - return SourceFileType::Header; - else if (extension == "inc") - return SourceFileType::Inc; - else - FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); - - // Unreachable - return SourceFileType::Cpp; -} - -std::string GetDir(std::string& path) -{ - std::size_t slash = path.rfind('/'); - - if (slash != std::string::npos) - return path.substr(0, slash + 1); - else - return std::string(""); -} - -SourceFile::SourceFile(std::string path) -{ - m_file_type = GetFileType(path); - - m_src_dir = GetDir(path); - - if (m_file_type == SourceFileType::Cpp - || m_file_type == SourceFileType::Header) - { - new (&m_source_file.c_file) CFile(path); - m_source_file.c_file.FindIncbins(); - } - else - { - AsmFile file(path); - std::set incbins; - std::set includes; - - IncDirectiveType incDirectiveType; - std::string outputPath; - - while ((incDirectiveType = file.ReadUntilIncDirective(outputPath)) != IncDirectiveType::None) - { - if (incDirectiveType == IncDirectiveType::Include) - includes.insert(outputPath); - else - incbins.insert(outputPath); - } - - new (&m_source_file.asm_wrapper) SourceFile::InnerUnion::AsmWrapper{incbins, includes}; - } -} - -SourceFile::~SourceFile() -{ - if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) - { - m_source_file.c_file.~CFile(); - } - else - { - m_source_file.asm_wrapper.asm_incbins.~set(); - m_source_file.asm_wrapper.asm_includes.~set(); - } -} - -const std::set& SourceFile::GetIncbins() -{ - if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) - return m_source_file.c_file.GetIncbins(); - else - return m_source_file.asm_wrapper.asm_incbins; -} - -const std::set& SourceFile::GetIncludes() -{ - if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) - return m_source_file.c_file.GetIncludes(); - else - return m_source_file.asm_wrapper.asm_includes; -} - -std::string& SourceFile::GetSrcDir() -{ - return m_src_dir; -} - diff --git a/tools/scaninc/source_file.h b/tools/scaninc/source_file.h deleted file mode 100755 index f7b6412b..00000000 --- a/tools/scaninc/source_file.h +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright(c) 2019 Phlosioneer -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#ifndef SOURCE_FILE_H -#define SOURCE_FILE_H - -#include -#include "scaninc.h" -#include "asm_file.h" -#include "c_file.h" - -enum class SourceFileType -{ - Cpp, - Header, - Asm, - Inc -}; - -SourceFileType GetFileType(std::string& path); - -class SourceFile -{ -public: - - SourceFile(std::string path); - ~SourceFile(); - SourceFile(SourceFile const&) = delete; - SourceFile(SourceFile&&) = delete; - SourceFile& operator =(SourceFile const&) = delete; - SourceFile& operator =(SourceFile&&) = delete; - bool HasIncbins(); - const std::set& GetIncbins(); - const std::set& GetIncludes(); - std::string& GetSrcDir(); - -private: - union InnerUnion { - CFile c_file; - struct AsmWrapper { - std::set asm_incbins; - std::set asm_includes; - } asm_wrapper; - - // Construction and destruction handled by SourceFile. - InnerUnion() {}; - ~InnerUnion() {}; - } m_source_file; - SourceFileType m_file_type; - std::string m_src_dir; -}; - -#endif // SOURCE_FILE_H - diff --git a/tools/src/CMakeLists.txt b/tools/src/CMakeLists.txt index 5d77a206..875f672f 100644 --- a/tools/src/CMakeLists.txt +++ b/tools/src/CMakeLists.txt @@ -6,3 +6,4 @@ add_subdirectory(gbafix) add_subdirectory(gbagfx) add_subdirectory(mid2agb) add_subdirectory(preproc) +add_subdirectory(scaninc) diff --git a/tools/src/scaninc/CMakeLists.txt b/tools/src/scaninc/CMakeLists.txt new file mode 100644 index 00000000..ec335c3a --- /dev/null +++ b/tools/src/scaninc/CMakeLists.txt @@ -0,0 +1,6 @@ +file(GLOB_RECURSE sources *.cpp) + +add_executable(scaninc ${sources}) +target_include_directories(scaninc PRIVATE .) + +install(TARGETS scaninc RUNTIME DESTINATION bin) diff --git a/tools/src/scaninc/LICENSE b/tools/src/scaninc/LICENSE new file mode 100755 index 00000000..b66bf81c --- /dev/null +++ b/tools/src/scaninc/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 YamaArashi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/tools/src/scaninc/asm_file.cpp b/tools/src/scaninc/asm_file.cpp new file mode 100755 index 00000000..109e604a --- /dev/null +++ b/tools/src/scaninc/asm_file.cpp @@ -0,0 +1,192 @@ +// Copyright(c) 2015-2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include +#include "scaninc.h" +#include "asm_file.h" + +AsmFile::AsmFile(std::string path) +{ + m_path = path; + + FILE *fp = std::fopen(path.c_str(), "rb"); + + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); + + std::fseek(fp, 0, SEEK_END); + + m_size = std::ftell(fp); + + m_buffer = new char[m_size]; + + std::rewind(fp); + + if (std::fread(m_buffer, m_size, 1, fp) != 1) + FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); + + std::fclose(fp); + + m_pos = 0; + m_lineNum = 1; +} + +AsmFile::~AsmFile() +{ + delete[] m_buffer; +} + +IncDirectiveType AsmFile::ReadUntilIncDirective(std::string &path) +{ + // At the beginning of each loop iteration, the current file position + // should be at the start of a line or at the end of the file. + for (;;) + { + SkipTabsAndSpaces(); + + IncDirectiveType incDirectiveType = IncDirectiveType::None; + + char c = PeekChar(); + if (c == '.' || c == '#') + { + m_pos++; + + if (MatchIncDirective("incbin", path)) + incDirectiveType = IncDirectiveType::Incbin; + else if (MatchIncDirective("include", path)) + incDirectiveType = IncDirectiveType::Include; + } + + for (;;) + { + int c = GetChar(); + + if (c == -1) + return incDirectiveType; + + if (c == ';') + { + SkipEndOfLineComment(); + break; + } + else if (c == '/' && PeekChar() == '*') + { + m_pos++; + SkipMultiLineComment(); + } + else if (c == '"') + { + SkipString(); + } + else if (c == '\n') + { + break; + } + } + + if (incDirectiveType != IncDirectiveType::None) + return incDirectiveType; + } +} + +std::string AsmFile::ReadPath() +{ + int length = 0; + int startPos = m_pos; + + for (;;) + { + int c = GetChar(); + + if (c == '"') + break; + + if (c == -1) + FATAL_INPUT_ERROR("unexpected EOF in include string\n"); + + if (c == 0) + FATAL_INPUT_ERROR("unexpected NUL character in include string\n"); + + if (c == '\n') + FATAL_INPUT_ERROR("unexpected end of line character in include string\n"); + + // Don't bother allowing any escape sequences. + if (c == '\\') + FATAL_INPUT_ERROR("unexpected escape in include string\n"); + + length++; + + if (length > SCANINC_MAX_PATH) + FATAL_INPUT_ERROR("path is too long"); + } + + return std::string(m_buffer + startPos, length); +} + +void AsmFile::SkipEndOfLineComment() +{ + int c; + + do + { + c = GetChar(); + } while (c != -1 && c != '\n'); +} + +void AsmFile::SkipMultiLineComment() +{ + for (;;) + { + int c = GetChar(); + + if (c == '*') + { + if (PeekChar() == '/') + { + m_pos++; + return; + } + } + else if (c == -1) + { + return; + } + } +} + +void AsmFile::SkipString() +{ + for (;;) + { + int c = GetChar(); + + if (c == '"') + break; + + if (c == -1) + FATAL_INPUT_ERROR("unexpected EOF in string\n"); + + if (c == '\\') + { + c = GetChar(); + } + } +} diff --git a/tools/src/scaninc/asm_file.h b/tools/src/scaninc/asm_file.h new file mode 100755 index 00000000..ad99b757 --- /dev/null +++ b/tools/src/scaninc/asm_file.h @@ -0,0 +1,119 @@ +// Copyright(c) 2015-2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASM_FILE_H +#define ASM_FILE_H + +#include +#include "scaninc.h" + +enum class IncDirectiveType +{ + None, + Include, + Incbin +}; + +class AsmFile +{ +public: + AsmFile(std::string path); + ~AsmFile(); + IncDirectiveType ReadUntilIncDirective(std::string& path); + +private: + char *m_buffer; + int m_pos; + int m_size; + int m_lineNum; + std::string m_path; + + int GetChar() + { + if (m_pos >= m_size) + return -1; + + int c = m_buffer[m_pos++]; + + if (c == '\r') + { + if (m_pos < m_size && m_buffer[m_pos++] == '\n') + { + m_lineNum++; + return '\n'; + } + else + { + FATAL_INPUT_ERROR("CR line endings are not supported\n"); + } + } + + if (c == '\n') + m_lineNum++; + + return c; + } + + // No newline translation because it's not needed for any use of this function. + int PeekChar() + { + if (m_pos >= m_size) + return -1; + + return m_buffer[m_pos]; + } + + void SkipTabsAndSpaces() + { + while (m_pos < m_size && (m_buffer[m_pos] == '\t' || m_buffer[m_pos] == ' ')) + m_pos++; + } + + bool MatchIncDirective(std::string directiveName, std::string& path) + { + int length = directiveName.length(); + int i; + + for (i = 0; i < length && m_pos + i < m_size; i++) + if (directiveName[i] != m_buffer[m_pos + i]) + return false; + + if (i < length) + return false; + + m_pos += length; + + SkipTabsAndSpaces(); + + if (GetChar() != '"') + FATAL_INPUT_ERROR("no path after \".%s\" directive\n", directiveName.c_str()); + + path = ReadPath(); + + return true; + } + + std::string ReadPath(); + void SkipEndOfLineComment(); + void SkipMultiLineComment(); + void SkipString(); +}; + +#endif // ASM_FILE_H diff --git a/tools/src/scaninc/c_file.cpp b/tools/src/scaninc/c_file.cpp new file mode 100755 index 00000000..595f366c --- /dev/null +++ b/tools/src/scaninc/c_file.cpp @@ -0,0 +1,307 @@ +// Copyright(c) 2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "c_file.h" + +CFile::CFile(std::string path) +{ + m_path = path; + + FILE *fp = std::fopen(path.c_str(), "rb"); + + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); + + std::fseek(fp, 0, SEEK_END); + + m_size = std::ftell(fp); + + m_buffer = new char[m_size + 1]; + m_buffer[m_size] = 0; + + std::rewind(fp); + + if (std::fread(m_buffer, m_size, 1, fp) != 1) + FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); + + std::fclose(fp); + + m_pos = 0; + m_lineNum = 1; +} + +CFile::~CFile() +{ + delete[] m_buffer; +} + +void CFile::FindIncbins() +{ + char stringChar = 0; + + while (m_pos < m_size) + { + if (stringChar) + { + if (m_buffer[m_pos] == stringChar) + { + m_pos++; + stringChar = 0; + } + else if (m_buffer[m_pos] == '\\' && m_buffer[m_pos + 1] == stringChar) + { + m_pos += 2; + } + else + { + if (m_buffer[m_pos] == '\n') + m_lineNum++; + m_pos++; + } + } + else + { + SkipWhitespace(); + CheckInclude(); + CheckIncbin(); + + if (m_pos >= m_size) + break; + + char c = m_buffer[m_pos++]; + + if (c == '\n') + m_lineNum++; + else if (c == '"') + stringChar = '"'; + else if (c == '\'') + stringChar = '\''; + else if (c == 0) + FATAL_INPUT_ERROR("unexpected null character"); + } + } +} + +bool CFile::ConsumeHorizontalWhitespace() +{ + if (m_buffer[m_pos] == '\t' || m_buffer[m_pos] == ' ') + { + m_pos++; + return true; + } + + return false; +} + +bool CFile::ConsumeNewline() +{ + if (m_buffer[m_pos] == '\n') + { + m_pos++; + m_lineNum++; + return true; + } + + if (m_buffer[m_pos] == '\r' && m_buffer[m_pos + 1] == '\n') + { + m_pos += 2; + m_lineNum++; + return true; + } + + return false; +} + +bool CFile::ConsumeComment() +{ + if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '*') + { + m_pos += 2; + while (m_buffer[m_pos] != '*' || m_buffer[m_pos + 1] != '/') + { + if (m_buffer[m_pos] == 0) + return false; + if (!ConsumeNewline()) + m_pos++; + } + m_pos += 2; + return true; + } + else if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '/') + { + m_pos += 2; + while (!ConsumeNewline()) + { + if (m_buffer[m_pos] == 0) + return false; + m_pos++; + } + return true; + } + + return false; +} + +void CFile::SkipWhitespace() +{ + while (ConsumeHorizontalWhitespace() || ConsumeNewline() || ConsumeComment()) + ; +} + +bool CFile::CheckIdentifier(const std::string& ident) +{ + unsigned int i; + + for (i = 0; i < ident.length() && m_pos + i < (unsigned)m_size; i++) + if (ident[i] != m_buffer[m_pos + i]) + return false; + + 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() +{ + // Optimization: assume most lines are not incbins + if (!(m_buffer[m_pos+0] == 'I' + && m_buffer[m_pos+1] == 'N' + && m_buffer[m_pos+2] == 'C' + && m_buffer[m_pos+3] == 'B' + && m_buffer[m_pos+4] == 'I' + && m_buffer[m_pos+5] == 'N' + && m_buffer[m_pos+6] == '_')) + { + return; + } + + std::string idents[6] = { "INCBIN_S8", "INCBIN_U8", "INCBIN_S16", "INCBIN_U16", "INCBIN_S32", "INCBIN_U32" }; + int incbinType = -1; + + for (int i = 0; i < 6; i++) + { + if (CheckIdentifier(idents[i])) + { + incbinType = i; + break; + } + } + + if (incbinType == -1) + return; + + long oldPos = m_pos; + long oldLineNum = m_lineNum; + + m_pos += idents[incbinType].length(); + + SkipWhitespace(); + + if (m_buffer[m_pos] != '(') + { + m_pos = oldPos; + m_lineNum = oldLineNum; + return; + } + + m_pos++; + + while (true) + { + SkipWhitespace(); + + std::string path = ReadPath(); + + SkipWhitespace(); + + m_incbins.emplace(path); + + if (m_buffer[m_pos] != ',') + break; + + m_pos++; + } + + if (m_buffer[m_pos] != ')') + FATAL_INPUT_ERROR("expected ')'"); + + m_pos++; + +} + +std::string CFile::ReadPath() +{ + if (m_buffer[m_pos] != '"') + { + if (m_buffer[m_pos] == '<') + { + return std::string(); + } + FATAL_INPUT_ERROR("expected '\"' or '<'"); + } + + m_pos++; + + int startPos = m_pos; + + while (m_buffer[m_pos] != '"') + { + if (m_buffer[m_pos] == 0) + { + if (m_pos >= m_size) + FATAL_INPUT_ERROR("unexpected EOF in path string"); + else + FATAL_INPUT_ERROR("unexpected null character in path string"); + } + + if (m_buffer[m_pos] == '\r' || m_buffer[m_pos] == '\n') + FATAL_INPUT_ERROR("unexpected end of line character in path string"); + + if (m_buffer[m_pos] == '\\') + FATAL_INPUT_ERROR("unexpected escape in path string"); + + m_pos++; + } + + m_pos++; + + return std::string(m_buffer + startPos, m_pos - 1 - startPos); +} diff --git a/tools/src/scaninc/c_file.h b/tools/src/scaninc/c_file.h new file mode 100755 index 00000000..618901b8 --- /dev/null +++ b/tools/src/scaninc/c_file.h @@ -0,0 +1,57 @@ +// Copyright(c) 2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef C_FILE_H +#define C_FILE_H + +#include +#include +#include +#include "scaninc.h" + +class CFile +{ +public: + CFile(std::string path); + ~CFile(); + void FindIncbins(); + const std::set& GetIncbins() { return m_incbins; } + const std::set& GetIncludes() { return m_includes; } + +private: + char *m_buffer; + int m_pos; + int m_size; + int m_lineNum; + std::string m_path; + std::set m_incbins; + std::set m_includes; + + bool ConsumeHorizontalWhitespace(); + bool ConsumeNewline(); + bool ConsumeComment(); + void SkipWhitespace(); + bool CheckIdentifier(const std::string& ident); + void CheckInclude(); + void CheckIncbin(); + std::string ReadPath(); +}; + +#endif // C_FILE_H diff --git a/tools/src/scaninc/scaninc.cpp b/tools/src/scaninc/scaninc.cpp new file mode 100755 index 00000000..1bf7feb5 --- /dev/null +++ b/tools/src/scaninc/scaninc.cpp @@ -0,0 +1,130 @@ +// Copyright(c) 2015-2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include +#include +#include +#include +#include +#include "scaninc.h" +#include "source_file.h" + +bool CanOpenFile(std::string path) +{ + FILE *fp = std::fopen(path.c_str(), "rb"); + + if (fp == NULL) + return false; + + std::fclose(fp); + return true; +} + +const char *const USAGE = "Usage: scaninc [-I INCLUDE_PATH] FILE_PATH\n"; + +int main(int argc, char **argv) +{ + std::queue filesToProcess; + std::set dependencies; + + std::vector includeDirs; + + argc--; + argv++; + + while (argc > 1) + { + std::string arg(argv[0]); + if (arg.substr(0, 2) == "-I") + { + std::string includeDir = arg.substr(2); + if (includeDir.empty()) + { + argc--; + argv++; + includeDir = std::string(argv[0]); + } + if (!includeDir.empty() && includeDir.back() != '/') + { + includeDir += '/'; + } + includeDirs.push_back(includeDir); + } + else + { + FATAL_ERROR(USAGE); + } + argc--; + argv++; + } + + if (argc != 1) { + FATAL_ERROR(USAGE); + } + + std::string initialPath(argv[0]); + + filesToProcess.push(initialPath); + + while (!filesToProcess.empty()) + { + std::string filePath = filesToProcess.front(); + SourceFile file(filePath); + filesToProcess.pop(); + + includeDirs.push_back(file.GetSrcDir()); + for (auto incbin : file.GetIncbins()) + { + // Search for the incbin in the include directories as well. + for (auto includeDir : includeDirs) + { + std::string path(includeDir + incbin); + if (CanOpenFile(path)) + { + dependencies.insert(path); + break; + } + } + } + for (auto include : file.GetIncludes()) + { + for (auto includeDir : includeDirs) + { + std::string path(includeDir + include); + if (CanOpenFile(path)) + { + bool inserted = dependencies.insert(path).second; + if (inserted) + { + filesToProcess.push(path); + } + break; + } + } + } + includeDirs.pop_back(); + } + + for (const std::string &path : dependencies) + { + std::printf("%s\n", path.c_str()); + } +} diff --git a/tools/src/scaninc/scaninc.h b/tools/src/scaninc/scaninc.h new file mode 100755 index 00000000..30cc9611 --- /dev/null +++ b/tools/src/scaninc/scaninc.h @@ -0,0 +1,59 @@ +// Copyright(c) 2015-2017 YamaArashi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef SCANINC_H +#define SCANINC_H + +#include +#include + +#ifdef _MSC_VER + +#define FATAL_INPUT_ERROR(format, ...) \ +do { \ + fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, __VA_ARGS__); \ + exit(1); \ +} while (0) + +#define FATAL_ERROR(format, ...) \ +do { \ + fprintf(stderr, format, __VA_ARGS__); \ + exit(1); \ +} while (0) + +#else + +#define FATAL_INPUT_ERROR(format, ...) \ +do { \ + fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, ##__VA_ARGS__); \ + exit(1); \ +} while (0) + +#define FATAL_ERROR(format, ...) \ +do { \ + fprintf(stderr, format, ##__VA_ARGS__); \ + exit(1); \ +} while (0) + +#endif // _MSC_VER + +#define SCANINC_MAX_PATH 255 + +#endif // SCANINC_H diff --git a/tools/src/scaninc/source_file.cpp b/tools/src/scaninc/source_file.cpp new file mode 100755 index 00000000..f23ff6db --- /dev/null +++ b/tools/src/scaninc/source_file.cpp @@ -0,0 +1,125 @@ +// Copyright(c) 2019 Phlosioneer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include "source_file.h" + + +SourceFileType GetFileType(std::string& path) +{ + std::size_t pos = path.find_last_of('.'); + + if (pos == std::string::npos) + FATAL_ERROR("no file extension in path \"%s\"\n", path.c_str()); + + std::string extension = path.substr(pos + 1); + + if (extension == "c") + return SourceFileType::Cpp; + else if (extension == "s") + return SourceFileType::Asm; + else if (extension == "h") + return SourceFileType::Header; + else if (extension == "inc") + return SourceFileType::Inc; + else + FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); + + // Unreachable + return SourceFileType::Cpp; +} + +std::string GetDir(std::string& path) +{ + std::size_t slash = path.rfind('/'); + + if (slash != std::string::npos) + return path.substr(0, slash + 1); + else + return std::string(""); +} + +SourceFile::SourceFile(std::string path) +{ + m_file_type = GetFileType(path); + + m_src_dir = GetDir(path); + + if (m_file_type == SourceFileType::Cpp + || m_file_type == SourceFileType::Header) + { + new (&m_source_file.c_file) CFile(path); + m_source_file.c_file.FindIncbins(); + } + else + { + AsmFile file(path); + std::set incbins; + std::set includes; + + IncDirectiveType incDirectiveType; + std::string outputPath; + + while ((incDirectiveType = file.ReadUntilIncDirective(outputPath)) != IncDirectiveType::None) + { + if (incDirectiveType == IncDirectiveType::Include) + includes.insert(outputPath); + else + incbins.insert(outputPath); + } + + new (&m_source_file.asm_wrapper) SourceFile::InnerUnion::AsmWrapper{incbins, includes}; + } +} + +SourceFile::~SourceFile() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + { + m_source_file.c_file.~CFile(); + } + else + { + m_source_file.asm_wrapper.asm_incbins.~set(); + m_source_file.asm_wrapper.asm_includes.~set(); + } +} + +const std::set& SourceFile::GetIncbins() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + return m_source_file.c_file.GetIncbins(); + else + return m_source_file.asm_wrapper.asm_incbins; +} + +const std::set& SourceFile::GetIncludes() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + return m_source_file.c_file.GetIncludes(); + else + return m_source_file.asm_wrapper.asm_includes; +} + +std::string& SourceFile::GetSrcDir() +{ + return m_src_dir; +} + diff --git a/tools/src/scaninc/source_file.h b/tools/src/scaninc/source_file.h new file mode 100755 index 00000000..f7b6412b --- /dev/null +++ b/tools/src/scaninc/source_file.h @@ -0,0 +1,71 @@ +// Copyright(c) 2019 Phlosioneer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef SOURCE_FILE_H +#define SOURCE_FILE_H + +#include +#include "scaninc.h" +#include "asm_file.h" +#include "c_file.h" + +enum class SourceFileType +{ + Cpp, + Header, + Asm, + Inc +}; + +SourceFileType GetFileType(std::string& path); + +class SourceFile +{ +public: + + SourceFile(std::string path); + ~SourceFile(); + SourceFile(SourceFile const&) = delete; + SourceFile(SourceFile&&) = delete; + SourceFile& operator =(SourceFile const&) = delete; + SourceFile& operator =(SourceFile&&) = delete; + bool HasIncbins(); + const std::set& GetIncbins(); + const std::set& GetIncludes(); + std::string& GetSrcDir(); + +private: + union InnerUnion { + CFile c_file; + struct AsmWrapper { + std::set asm_incbins; + std::set asm_includes; + } asm_wrapper; + + // Construction and destruction handled by SourceFile. + InnerUnion() {}; + ~InnerUnion() {}; + } m_source_file; + SourceFileType m_file_type; + std::string m_src_dir; +}; + +#endif // SOURCE_FILE_H + -- cgit v1.2.3