From 10c0566121cb923b7f4e6c1a97a1df8ad70cb327 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 19 Jan 2021 11:43:50 -0500 Subject: [PATCH] Don't keep temporary C build files by default. --- Makefile | 10 ++++++++ tools/preproc/c_file.cpp | 54 +++++++++++++++++++++++++++++---------- tools/preproc/c_file.h | 5 +++- tools/preproc/preproc.cpp | 22 +++++++++++----- 4 files changed, 70 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 3bb5161e3..3383cb14d 100644 --- a/Makefile +++ b/Makefile @@ -283,10 +283,15 @@ override CFLAGS += -g endif $(C_BUILDDIR)/%.o : $(C_SUBDIR)/%.c $$(c_dep) +ifeq (,$(KEEP_TEMPS)) + @echo "$(CC1) -o $@ $<" + @$(CPP) $(CPPFLAGS) $< | $(PREPROC) $< charmap.txt -i | { $(CC1) $(CFLAGS) -o - -; echo -e ".text\n\t.align\t2, 0"; } | $(AS) $(ASFLAGS) -o $@ - +else @$(CPP) $(CPPFLAGS) $< -o $(C_BUILDDIR)/$*.i @$(PREPROC) $(C_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s @echo -e ".text\n\t.align\t2, 0\n" >> $(C_BUILDDIR)/$*.s $(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s +endif ifeq ($(NODEP),1) $(GFLIB_BUILDDIR)/%.o: c_dep := @@ -295,10 +300,15 @@ $(GFLIB_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(GFLIB_SUBDIR)/$*.c ]] && $(SCANIN endif $(GFLIB_BUILDDIR)/%.o : $(GFLIB_SUBDIR)/%.c $$(c_dep) +ifeq (,$(KEEP_TEMPS)) + @echo "$(CC1) -o $@ $<" + @$(CPP) $(CPPFLAGS) $< | $(PREPROC) $< charmap.txt -i | { $(CC1) $(CFLAGS) -o - -; echo -e ".text\n\t.align\t2, 0"; } | $(AS) $(ASFLAGS) -o $@ - +else @$(CPP) $(CPPFLAGS) $< -o $(GFLIB_BUILDDIR)/$*.i @$(PREPROC) $(GFLIB_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(GFLIB_BUILDDIR)/$*.s @echo -e ".text\n\t.align\t2, 0\n" >> $(GFLIB_BUILDDIR)/$*.s $(AS) $(ASFLAGS) -o $@ $(GFLIB_BUILDDIR)/$*.s +endif ifeq ($(NODEP),1) $(C_BUILDDIR)/%.o: c_asm_dep := diff --git a/tools/preproc/c_file.cpp b/tools/preproc/c_file.cpp index b996a048c..f7ca4365c 100644 --- a/tools/preproc/c_file.cpp +++ b/tools/preproc/c_file.cpp @@ -23,32 +23,58 @@ #include #include #include +#include #include "preproc.h" #include "c_file.h" #include "char_util.h" #include "utf8.h" #include "string_parser.h" -CFile::CFile(std::string filename) : m_filename(filename) +CFile::CFile(const char * filenameCStr, bool isStdin) { - FILE *fp = std::fopen(filename.c_str(), "rb"); + FILE *fp; + + if (isStdin) { + fp = stdin; + m_filename = std::string{"/"}.append(filenameCStr); + } else { + fp = std::fopen(filenameCStr, "rb"); + m_filename = std::string(filenameCStr); + } + + std::string& filename = m_filename; if (fp == NULL) FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str()); - std::fseek(fp, 0, SEEK_END); + m_size = 0; + m_buffer = (char *)malloc(CHUNK_SIZE + 1); + if (m_buffer == NULL) { + FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str()); + } - m_size = std::ftell(fp); + std::size_t numAllocatedBytes = CHUNK_SIZE + 1; + std::size_t bufferOffset = 0; + std::size_t count; - if (m_size < 0) - FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str()); + while ((count = std::fread(m_buffer + bufferOffset, 1, CHUNK_SIZE, fp)) != 0) { + if (!std::ferror(fp)) { + m_size += count; - m_buffer = new char[m_size + 1]; + if (std::feof(fp)) { + break; + } - std::rewind(fp); - - if (std::fread(m_buffer, m_size, 1, fp) != 1) - FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str()); + numAllocatedBytes += CHUNK_SIZE; + bufferOffset += CHUNK_SIZE; + m_buffer = (char *)realloc(m_buffer, numAllocatedBytes); + if (m_buffer == NULL) { + FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str()); + } + } else { + FATAL_ERROR("Failed to read \"%s\". (error: %s)", filename.c_str(), std::strerror(errno)); + } + } m_buffer[m_size] = 0; @@ -56,6 +82,7 @@ CFile::CFile(std::string filename) : m_filename(filename) m_pos = 0; m_lineNum = 1; + m_isStdin = isStdin; } CFile::CFile(CFile&& other) : m_filename(std::move(other.m_filename)) @@ -64,13 +91,14 @@ CFile::CFile(CFile&& other) : m_filename(std::move(other.m_filename)) m_pos = other.m_pos; m_size = other.m_size; m_lineNum = other.m_lineNum; + m_isStdin = other.m_isStdin; - other.m_buffer = nullptr; + other.m_buffer = NULL; } CFile::~CFile() { - delete[] m_buffer; + free(m_buffer); } void CFile::Preproc() diff --git a/tools/preproc/c_file.h b/tools/preproc/c_file.h index 7369aba85..49e633a18 100644 --- a/tools/preproc/c_file.h +++ b/tools/preproc/c_file.h @@ -30,7 +30,7 @@ class CFile { public: - CFile(std::string filename); + CFile(const char * filenameCStr, bool isStdin); CFile(CFile&& other); CFile(const CFile&) = delete; ~CFile(); @@ -42,6 +42,7 @@ private: long m_size; long m_lineNum; std::string m_filename; + bool m_isStdin; bool ConsumeHorizontalWhitespace(); bool ConsumeNewline(); @@ -55,4 +56,6 @@ private: void RaiseWarning(const char* format, ...); }; +#define CHUNK_SIZE 4096 + #endif // C_FILE_H diff --git a/tools/preproc/preproc.cpp b/tools/preproc/preproc.cpp index c9c6042df..02950a296 100644 --- a/tools/preproc/preproc.cpp +++ b/tools/preproc/preproc.cpp @@ -103,9 +103,9 @@ void PreprocAsmFile(std::string filename) } } -void PreprocCFile(std::string filename) +void PreprocCFile(const char * filename, bool isStdin) { - CFile cFile(filename); + CFile cFile(filename, isStdin); cFile.Preproc(); } @@ -132,9 +132,9 @@ char* GetFileExtension(char* filename) int main(int argc, char **argv) { - if (argc != 3) + if (argc < 3 || argc > 4) { - std::fprintf(stderr, "Usage: %s SRC_FILE CHARMAP_FILE", argv[0]); + std::fprintf(stderr, "Usage: %s SRC_FILE CHARMAP_FILE [-i]\nwhere -i denotes if input is from stdin", argv[0]); return 1; } @@ -147,9 +147,17 @@ int main(int argc, char **argv) if ((extension[0] == 's') && extension[1] == 0) PreprocAsmFile(argv[1]); - else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) - PreprocCFile(argv[1]); - else + else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) { + if (argc == 4) { + if (argv[3][0] == '-' && argv[3][1] == 'i' && argv[3][2] == '\0') { + PreprocCFile(argv[1], true); + } else { + FATAL_ERROR("unknown argument flag \"%s\".\n", argv[3]); + } + } else { + PreprocCFile(argv[1], false); + } + } else FATAL_ERROR("\"%s\" has an unknown file extension of \"%s\".\n", argv[1], extension); return 0;