| @@ -1,11 +1,11 @@ | |||
| SRCS = \ | |||
| src/BXParser.cc src/build.cc src/compdb.cc src/CompileStep.cc src/DepNode.cc \ | |||
| src/globals.cc src/LinkStep.cc src/logger.cc src/parallel.cc src/sys.cc \ | |||
| src/toolchain.cc src/main.cc | |||
| lib/BXParser.cc lib/build.cc lib/compdb.cc lib/CompileStep.cc lib/DepNode.cc \ | |||
| lib/globals.cc lib/LinkStep.cc lib/logger.cc lib/parallel.cc lib/sys.cc \ | |||
| lib/toolchain.cc cmd/main.cc | |||
| HDRS = \ | |||
| src/BXParser.h src/build.h src/compdb.h src/CompileStep.h src/DepNode.h \ | |||
| src/globals.h src/LinkStep.h src/logger.h src/parallel.h src/sys.h \ | |||
| src/toolchain.h src/bufio.h | |||
| lib/BXParser.h lib/build.h lib/compdb.h lib/CompileStep.h lib/DepNode.h \ | |||
| lib/globals.h lib/LinkStep.h lib/logger.h lib/parallel.h lib/sys.h \ | |||
| lib/toolchain.h lib/bufio.h | |||
| BUILD = build | |||
| OBJS = $(patsubst %,$(BUILD)/%.o,$(SRCS)) | |||
| CFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -O3 -g | |||
| @@ -1,5 +1,6 @@ | |||
| target := box | |||
| files := src | |||
| files := lib cmd | |||
| includes := lib | |||
| warnings := all extra no-unused-parameter | |||
| std := c++14 | |||
| optimize := 3 | |||
| @@ -30,7 +30,7 @@ BXParser::Operator BXParser::readOperator() { | |||
| skip(); // '=' | |||
| skip(); // '+' | |||
| return Operator::EQUALS_PLUS; | |||
| } else if (peek() == '|' && ch2 == '|') { | |||
| } else if (peek() == '|' && ch2 == '=') { | |||
| skip(); // '|' | |||
| skip(); // '=' | |||
| return Operator::BAR_EQUALS; | |||
| @@ -24,7 +24,7 @@ public: | |||
| static const int FLAG_NONE = 0; | |||
| static const int FLAG_ONE_LINE = 1 << 0; | |||
| BXParser(bufio::IStream &stream, int flags, int line = 1, int ch = 1): | |||
| BXParser(bufio::IStream &stream, int flags = FLAG_NONE, int line = 1, int ch = 1): | |||
| flags_(flags), line_(line), ch_(ch), buf_(stream) {} | |||
| void parse(BXVariables &vars); | |||
| @@ -37,7 +37,7 @@ bool CompileStep::checkHasChanged(const std::string &outDir) { | |||
| BXVariables cachedVariables; | |||
| try { | |||
| bufio::IFStream f(confPath); | |||
| BXParser parser(f, BXParser::FLAG_NONE); | |||
| BXParser parser(f); | |||
| parser.parse(cachedVariables); | |||
| } catch (BXParseError &err) { | |||
| logger::log(confPath + ": " + err.what()); | |||
| @@ -22,7 +22,7 @@ bool LinkStep::checkHasChanged(const std::string &outDir) { | |||
| BXVariables cachedVariables; | |||
| try { | |||
| bufio::IFStream f(confPath); | |||
| BXParser parser(f, BXParser::FLAG_NONE); | |||
| BXParser parser(f); | |||
| parser.parse(cachedVariables); | |||
| } catch (BXParseError &err) { | |||
| logger::log(confPath + ": " + err.what()); | |||
| @@ -25,13 +25,13 @@ private: | |||
| class ISStream: public IStream { | |||
| public: | |||
| ISStream(const std::string &str): str_(str) {} | |||
| ISStream(std::string str): str_(std::move(str)) {} | |||
| size_t read(char *buf, size_t maxlen) override; | |||
| private: | |||
| size_t idx_ = 0; | |||
| const std::string &str_; | |||
| const std::string str_; | |||
| }; | |||
| class OStream { | |||
| @@ -50,25 +50,25 @@ private: | |||
| std::ofstream os_; | |||
| }; | |||
| template<size_t bufsiz = 1024> | |||
| template<typename IS = IStream, size_t bufsiz = 1024> | |||
| class IBuf { | |||
| public: | |||
| IBuf(IStream &is): is_(is) {} | |||
| IBuf(IS &is): is_(is) {} | |||
| char get(); | |||
| int peek(size_t count = 1); | |||
| private: | |||
| IStream &is_; | |||
| IS &is_; | |||
| char buf_[bufsiz]; | |||
| size_t idx_ = 0; | |||
| size_t len_ = 0; | |||
| }; | |||
| template<size_t bufsiz = 1024> | |||
| template<typename OS = OStream, size_t bufsiz = 1024> | |||
| class OBuf { | |||
| public: | |||
| OBuf(OStream &os): os_(os) {} | |||
| OBuf(OS &os): os_(os) {} | |||
| ~OBuf(); | |||
| void put(char ch); | |||
| @@ -77,7 +77,7 @@ public: | |||
| void put(const std::string &str) { put(str.c_str(), str.size()); } | |||
| private: | |||
| OStream &os_; | |||
| OS &os_; | |||
| char buf_[bufsiz]; | |||
| size_t idx_ = 0; | |||
| }; | |||
| @@ -131,8 +131,8 @@ inline void OFStream::write(const char *buf, size_t len) { | |||
| * IBuf | |||
| */ | |||
| template<size_t bufsiz> | |||
| inline char IBuf<bufsiz>::get() { | |||
| template<typename IS, size_t bufsiz> | |||
| inline char IBuf<IS, bufsiz>::get() { | |||
| if (idx_ < len_) { | |||
| return buf_[idx_++]; | |||
| } | |||
| @@ -146,8 +146,8 @@ inline char IBuf<bufsiz>::get() { | |||
| return buf_[idx_++]; | |||
| } | |||
| template<size_t bufsiz> | |||
| inline int IBuf<bufsiz>::peek(size_t count) { | |||
| template<typename IS, size_t bufsiz> | |||
| inline int IBuf<IS, bufsiz>::peek(size_t count) { | |||
| size_t offset = count - 1; | |||
| if (idx_ + offset < len_) { | |||
| return buf_[idx_ + offset]; | |||
| @@ -168,15 +168,15 @@ inline int IBuf<bufsiz>::peek(size_t count) { | |||
| * OBuf | |||
| */ | |||
| template<size_t bufsiz> | |||
| inline OBuf<bufsiz>::~OBuf() { | |||
| template<typename OS, size_t bufsiz> | |||
| inline OBuf<OS, bufsiz>::~OBuf() { | |||
| if (idx_ > 0) { | |||
| os_.write(buf_, idx_); | |||
| } | |||
| } | |||
| template<size_t bufsiz> | |||
| inline void OBuf<bufsiz>::put(char ch) { | |||
| template<typename OS, size_t bufsiz> | |||
| inline void OBuf<OS, bufsiz>::put(char ch) { | |||
| buf_[idx_++] = ch; | |||
| if (idx_ == sizeof(buf_)) { | |||
| @@ -185,8 +185,8 @@ inline void OBuf<bufsiz>::put(char ch) { | |||
| } | |||
| } | |||
| template<size_t bufsiz> | |||
| inline void OBuf<bufsiz>::put(const char *str, size_t len) { | |||
| template<typename OS, size_t bufsiz> | |||
| inline void OBuf<OS, bufsiz>::put(const char *str, size_t len) { | |||
| size_t w = sizeof(buf_) - idx_ - 1; | |||
| if (w > len) { | |||
| w = len; | |||
| @@ -64,7 +64,7 @@ static void findDeps( | |||
| varsptr = &subvars; | |||
| bufio::IFStream stream("build.bx"); | |||
| BXParser parser(stream, BXParser::FLAG_NONE); | |||
| BXParser parser(stream); | |||
| parser.parse(subvars); | |||
| auto it = subvars.find("files"); | |||
| @@ -102,7 +102,7 @@ std::unique_ptr<DepNode> buildDepTree(const std::string &outDir, BXVariables var | |||
| // Read config from file | |||
| if (sys::fileExists("build.bx")) { | |||
| bufio::IFStream stream("build.bx"); | |||
| BXParser parser(stream, BXParser::FLAG_NONE); | |||
| BXParser parser(stream); | |||
| parser.parse(variables); | |||
| } | |||
| @@ -0,0 +1,37 @@ | |||
| #include "logger.h" | |||
| #include <chrono> | |||
| namespace logger { | |||
| std::mutex mut; | |||
| Timer::Timer() { | |||
| restart(); | |||
| } | |||
| void Timer::restart() { | |||
| std::chrono::duration<double>(std::chrono::steady_clock::now() | |||
| .time_since_epoch()).count(); | |||
| } | |||
| void Timer::print(std::ostream &os) const { | |||
| double now = std::chrono::duration<double>(std::chrono::steady_clock::now() | |||
| .time_since_epoch()).count(); | |||
| int deltaMs = (int)((now - start_) * 1000); | |||
| int delta = deltaMs; | |||
| if (deltaMs > 60 * 1000) { | |||
| os << (deltaMs / (60 * 1000)) << "m "; | |||
| delta %= 60 * 1000; | |||
| } | |||
| if (deltaMs > 1000) { | |||
| os << (deltaMs / 1000) << "s "; | |||
| delta %= 1000; | |||
| } | |||
| os << delta << "ms"; | |||
| } | |||
| } | |||
| @@ -0,0 +1,36 @@ | |||
| #pragma once | |||
| #include <string> | |||
| #include <mutex> | |||
| #include <iostream> | |||
| namespace logger { | |||
| extern std::mutex mut; | |||
| class Timer { | |||
| public: | |||
| Timer(); | |||
| void restart(); | |||
| void print(std::ostream &os) const; | |||
| private: | |||
| double start_; | |||
| }; | |||
| template<typename T> | |||
| inline void log(const T &msg) { | |||
| std::unique_lock<std::mutex> lock(mut); | |||
| std::cerr << msg << '\n'; | |||
| } | |||
| template<typename T> | |||
| inline void log(const T &msg, const Timer &t) { | |||
| std::unique_lock<std::mutex> lock(mut); | |||
| std::cerr << msg; | |||
| t.print(std::cerr); | |||
| std::cerr << '\n'; | |||
| } | |||
| } | |||
| @@ -1,7 +0,0 @@ | |||
| #include "logger.h" | |||
| namespace logger { | |||
| std::mutex mut; | |||
| } | |||
| @@ -1,17 +0,0 @@ | |||
| #pragma once | |||
| #include <string> | |||
| #include <mutex> | |||
| #include <iostream> | |||
| namespace logger { | |||
| extern std::mutex mut; | |||
| template<typename T> | |||
| inline void log(const T &msg) { | |||
| std::unique_lock<std::mutex> lock(mut); | |||
| std::cerr << msg << '\n'; | |||
| } | |||
| } | |||