|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
|
|
|
#include <sys/stat.h> |
|
|
#include <unistd.h> |
|
|
#include <unistd.h> |
|
|
|
|
|
#include <dirent.h> |
|
|
|
|
|
#include <stdexcept> |
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
#include <errno.h> |
|
|
#include <fstream> |
|
|
#include <fstream> |
|
|
|
|
|
|
|
|
#include "SourceFile.h" |
|
|
#include "SourceFile.h" |
|
|
#include "BBBParser.h" |
|
|
#include "BBBParser.h" |
|
|
|
|
|
|
|
|
void readDir(std::string dir, std::vector<SourceFile> sources) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void readDir(std::string dir, std::vector<SourceFile> &sources, BBBParser::Variables vars) { |
|
|
|
|
|
std::ifstream buildFile(dir + "/build.bbb"); |
|
|
|
|
|
std::vector<std::string> files; |
|
|
|
|
|
bool hasFiles = false; |
|
|
|
|
|
|
|
|
|
|
|
// Parse $dir/build.bbb, specifies a 'files' value |
|
|
|
|
|
if (buildFile.good()) { |
|
|
|
|
|
BBBParser parser(buildFile, BBBParser::FLAG_INSECURE); |
|
|
|
|
|
parser.parse(vars); |
|
|
|
|
|
|
|
|
|
|
|
auto it = vars.find("files"); |
|
|
|
|
|
if (it != vars.end()) { |
|
|
|
|
|
files = it->second; |
|
|
|
|
|
hasFiles = true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If build.bbb didn't specify 'files', we have to readdir |
|
|
|
|
|
if (!hasFiles) { |
|
|
|
|
|
DIR *d = opendir(dir.c_str()); |
|
|
|
|
|
if (d == NULL) { |
|
|
|
|
|
throw std::runtime_error("opendir '" + dir + "': " + strerror(errno)); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct dirent *ent; |
|
|
|
|
|
while ((ent = readdir(d)) != NULL) { |
|
|
|
|
|
files.emplace_back(ent->d_name); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
closedir(d); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Go through files |
|
|
|
|
|
for (auto &ent: files) { |
|
|
|
|
|
std::string path = dir + "/" + ent; |
|
|
|
|
|
struct stat st; |
|
|
|
|
|
if (stat(path.c_str(), &st) < 0) { |
|
|
|
|
|
throw std::runtime_error("stat '" + path + "': " + strerror(errno)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) { |
|
|
|
|
|
readDir(path, sources, vars); |
|
|
|
|
|
} else { |
|
|
|
|
|
sources.emplace_back(dir, ent); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int main(int argc, char **argv) { |
|
|
int main(int argc, char **argv) { |