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

@@ -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

+ 2
- 1
build.bx View File

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

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


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

@@ -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;

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

@@ -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);

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

@@ -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());

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

@@ -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());

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


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

@@ -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;

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

@@ -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);
}


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

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

}

+ 36
- 0
lib/logger.h View File

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

}

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

@@ -1,7 +0,0 @@
#include "logger.h"

namespace logger {

std::mutex mut;

}

+ 0
- 17
src/logger.h View File

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

}

Loading…
Cancel
Save