Browse Source

move things around

master
Martin Dørum 3 years ago
parent
commit
5738eb48ea
28 changed files with 105 additions and 55 deletions
  1. 6
    6
      Makefile
  2. 2
    1
      build.bx
  3. 0
    0
      cmd/main.cc
  4. 1
    1
      lib/BXParser.cc
  5. 1
    1
      lib/BXParser.h
  6. 1
    1
      lib/CompileStep.cc
  7. 0
    0
      lib/CompileStep.h
  8. 0
    0
      lib/DepNode.cc
  9. 0
    0
      lib/DepNode.h
  10. 1
    1
      lib/LinkStep.cc
  11. 0
    0
      lib/LinkStep.h
  12. 18
    18
      lib/bufio.h
  13. 2
    2
      lib/build.cc
  14. 0
    0
      lib/build.h
  15. 0
    0
      lib/compdb.cc
  16. 0
    0
      lib/compdb.h
  17. 0
    0
      lib/globals.cc
  18. 0
    0
      lib/globals.h
  19. 37
    0
      lib/logger.cc
  20. 36
    0
      lib/logger.h
  21. 0
    0
      lib/parallel.cc
  22. 0
    0
      lib/parallel.h
  23. 0
    0
      lib/sys.cc
  24. 0
    0
      lib/sys.h
  25. 0
    0
      lib/toolchain.cc
  26. 0
    0
      lib/toolchain.h
  27. 0
    7
      src/logger.cc
  28. 0
    17
      src/logger.h

+ 6
- 6
Makefile View File

SRCS = \ 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 = \ 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 BUILD = build
OBJS = $(patsubst %,$(BUILD)/%.o,$(SRCS)) OBJS = $(patsubst %,$(BUILD)/%.o,$(SRCS))
CFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -O3 -g CFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -O3 -g

+ 2
- 1
build.bx View File

target := box target := box
files := src
files := lib cmd
includes := lib
warnings := all extra no-unused-parameter warnings := all extra no-unused-parameter
std := c++14 std := c++14
optimize := 3 optimize := 3

src/main.cc → cmd/main.cc View File


src/BXParser.cc → lib/BXParser.cc View File

skip(); // '=' skip(); // '='
skip(); // '+' skip(); // '+'
return Operator::EQUALS_PLUS; return Operator::EQUALS_PLUS;
} else if (peek() == '|' && ch2 == '|') {
} else if (peek() == '|' && ch2 == '=') {
skip(); // '|' skip(); // '|'
skip(); // '=' skip(); // '='
return Operator::BAR_EQUALS; return Operator::BAR_EQUALS;

src/BXParser.h → lib/BXParser.h View File

static const int FLAG_NONE = 0; static const int FLAG_NONE = 0;
static const int FLAG_ONE_LINE = 1 << 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) {} flags_(flags), line_(line), ch_(ch), buf_(stream) {}


void parse(BXVariables &vars); void parse(BXVariables &vars);

src/CompileStep.cc → lib/CompileStep.cc View File

BXVariables cachedVariables; BXVariables cachedVariables;
try { try {
bufio::IFStream f(confPath); bufio::IFStream f(confPath);
BXParser parser(f, BXParser::FLAG_NONE);
BXParser parser(f);
parser.parse(cachedVariables); parser.parse(cachedVariables);
} catch (BXParseError &err) { } catch (BXParseError &err) {
logger::log(confPath + ": " + err.what()); logger::log(confPath + ": " + err.what());

src/CompileStep.h → lib/CompileStep.h View File


src/DepNode.cc → lib/DepNode.cc View File


src/DepNode.h → lib/DepNode.h View File


src/LinkStep.cc → lib/LinkStep.cc View File

BXVariables cachedVariables; BXVariables cachedVariables;
try { try {
bufio::IFStream f(confPath); bufio::IFStream f(confPath);
BXParser parser(f, BXParser::FLAG_NONE);
BXParser parser(f);
parser.parse(cachedVariables); parser.parse(cachedVariables);
} catch (BXParseError &err) { } catch (BXParseError &err) {
logger::log(confPath + ": " + err.what()); logger::log(confPath + ": " + err.what());

src/LinkStep.h → lib/LinkStep.h View File


src/bufio.h → lib/bufio.h View File



class ISStream: public IStream { class ISStream: public IStream {
public: 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; size_t read(char *buf, size_t maxlen) override;


private: private:
size_t idx_ = 0; size_t idx_ = 0;
const std::string &str_;
const std::string str_;
}; };


class OStream { class OStream {
std::ofstream os_; std::ofstream os_;
}; };


template<size_t bufsiz = 1024>
template<typename IS = IStream, size_t bufsiz = 1024>
class IBuf { class IBuf {
public: public:
IBuf(IStream &is): is_(is) {}
IBuf(IS &is): is_(is) {}


char get(); char get();
int peek(size_t count = 1); int peek(size_t count = 1);


private: private:
IStream &is_;
IS &is_;
char buf_[bufsiz]; char buf_[bufsiz];
size_t idx_ = 0; size_t idx_ = 0;
size_t len_ = 0; size_t len_ = 0;
}; };


template<size_t bufsiz = 1024>
template<typename OS = OStream, size_t bufsiz = 1024>
class OBuf { class OBuf {
public: public:
OBuf(OStream &os): os_(os) {}
OBuf(OS &os): os_(os) {}
~OBuf(); ~OBuf();


void put(char ch); void put(char ch);
void put(const std::string &str) { put(str.c_str(), str.size()); } void put(const std::string &str) { put(str.c_str(), str.size()); }


private: private:
OStream &os_;
OS &os_;
char buf_[bufsiz]; char buf_[bufsiz];
size_t idx_ = 0; size_t idx_ = 0;
}; };
* IBuf * 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_) { if (idx_ < len_) {
return buf_[idx_++]; return buf_[idx_++];
} }
return buf_[idx_++]; 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; size_t offset = count - 1;
if (idx_ + offset < len_) { if (idx_ + offset < len_) {
return buf_[idx_ + offset]; return buf_[idx_ + offset];
* OBuf * OBuf
*/ */


template<size_t bufsiz>
inline OBuf<bufsiz>::~OBuf() {
template<typename OS, size_t bufsiz>
inline OBuf<OS, bufsiz>::~OBuf() {
if (idx_ > 0) { if (idx_ > 0) {
os_.write(buf_, idx_); 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; buf_[idx_++] = ch;


if (idx_ == sizeof(buf_)) { if (idx_ == sizeof(buf_)) {
} }
} }


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; size_t w = sizeof(buf_) - idx_ - 1;
if (w > len) { if (w > len) {
w = len; w = len;

src/build.cc → lib/build.cc View File

varsptr = &subvars; varsptr = &subvars;


bufio::IFStream stream("build.bx"); bufio::IFStream stream("build.bx");
BXParser parser(stream, BXParser::FLAG_NONE);
BXParser parser(stream);
parser.parse(subvars); parser.parse(subvars);


auto it = subvars.find("files"); auto it = subvars.find("files");
// Read config from file // Read config from file
if (sys::fileExists("build.bx")) { if (sys::fileExists("build.bx")) {
bufio::IFStream stream("build.bx"); bufio::IFStream stream("build.bx");
BXParser parser(stream, BXParser::FLAG_NONE);
BXParser parser(stream);
parser.parse(variables); parser.parse(variables);
} }



src/build.h → lib/build.h View File


src/compdb.cc → lib/compdb.cc View File


src/compdb.h → lib/compdb.h View File


src/globals.cc → lib/globals.cc View File


src/globals.h → lib/globals.h View File


+ 37
- 0
lib/logger.cc View File

#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";
}

}

+ 36
- 0
lib/logger.h View File

#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';
}

}

src/parallel.cc → lib/parallel.cc View File


src/parallel.h → lib/parallel.h View File


src/sys.cc → lib/sys.cc View File


src/sys.h → lib/sys.h View File


src/toolchain.cc → lib/toolchain.cc View File


src/toolchain.h → lib/toolchain.h View File


+ 0
- 7
src/logger.cc View File

#include "logger.h"

namespace logger {

std::mutex mut;

}

+ 0
- 17
src/logger.h View File

#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';
}

}

Loading…
Cancel
Save