Add support for h file generation

This commit is contained in:
Thraix
2019-01-30 16:58:52 +01:00
parent 048e3d050d
commit aecbe5c501
13 changed files with 213 additions and 128 deletions
Executable → Regular
+10 -7
View File
@@ -1,4 +1,4 @@
# This Makefile was generated using MakeGen v1.0.9 made by Tim Håkansson
# This Makefile was generated using MakeGen v1.1.0 made by Tim Håkansson
# and is licensed under MIT. Full source of the project can be found at
# https://github.com/Thraix/MakeGen
CC=@g++
@@ -6,7 +6,7 @@ CO=@g++ -o
BIN=bin/
OBJPATH=$(BIN)intermediates
INCLUDES=
OBJECTS=$(OBJPATH)/ConfigFile.o $(OBJPATH)/IncludeDeps.o $(OBJPATH)/Makefile.o $(OBJPATH)/main.o
OBJECTS=$(OBJPATH)/ConfigFile.o $(OBJPATH)/HFileGen.o $(OBJPATH)/IncludeDeps.o $(OBJPATH)/Makefile.o $(OBJPATH)/main.o
CFLAGS=$(INCLUDES) -std=c++17 -c -w -g3 -D_DEBUG
LIBDIR=
LDFLAGS=
@@ -24,14 +24,17 @@ install: all
$(info Installing MakeGen to /usr/bin/)
@cp $(OUTPUT) /usr/bin/makegen
$(OBJPATH)/ConfigFile.o : src/ConfigFile.cpp src/Common.h src/ConfigFile.h
$(info -[25%]- $<)
$(info -[20%]- $<)
$(CC) $(CFLAGS) -o $@ $<
$(OBJPATH)/HFileGen.o : src/HFileGen.cpp src/FileUtils.h src/Common.h src/HFileGen.h src/ConfigFile.h
$(info -[40%]- $<)
$(CC) $(CFLAGS) -o $@ $<
$(OBJPATH)/IncludeDeps.o : src/IncludeDeps.cpp src/IncludeDeps.h
$(info -[50%]- $<)
$(info -[60%]- $<)
$(CC) $(CFLAGS) -o $@ $<
$(OBJPATH)/Makefile.o : src/Makefile.cpp src/Common.h src/IncludeDeps.h src/Makefile.h src/ConfigFile.h
$(info -[75%]- $<)
$(OBJPATH)/Makefile.o : src/Makefile.cpp src/Common.h src/FileUtils.h src/IncludeDeps.h src/Makefile.h src/ConfigFile.h
$(info -[80%]- $<)
$(CC) $(CFLAGS) -o $@ $<
$(OBJPATH)/main.o : src/main.cpp src/Common.h src/ConfigFile.h src/IncludeDeps.h src/Makefile.h src/Timer.h
$(OBJPATH)/main.o : src/main.cpp src/Common.h src/ConfigFile.h src/HFileGen.h src/IncludeDeps.h src/Makefile.h src/Timer.h
$(info -[100%]- $<)
$(CC) $(CFLAGS) -o $@ $<
Executable → Regular
+2 -1
View File
@@ -1,10 +1,11 @@
#libs
#libdirs
#includedirs
#srcdirs
#srcdir
src/
#defines
_DEBUG
#compileflags
#outputdir
bin/
#projectname
+2 -2
View File
@@ -10,9 +10,9 @@
// Major changes, probably not be backwards compatible
#define MAKEGEN_VERSION_MAJOR 1
// Release , should be backwards compatible with any minor version
#define MAKEGEN_VERSION_RELEASE 0
#define MAKEGEN_VERSION_RELEASE 1
// Minor changes, should be compatible with any other minor version with same major and release.
#define MAKEGEN_VERSION_MINOR 9
#define MAKEGEN_VERSION_MINOR 0
#define MAKEGEN_VERSION ("v" STR(MAKEGEN_VERSION_MAJOR) "." STR(MAKEGEN_VERSION_RELEASE) "." STR(MAKEGEN_VERSION_MINOR))
const static unsigned int FLAG_HELP = BIT(0);
+73 -46
View File
@@ -8,9 +8,8 @@
#define FLAG_STRING 2
#define FLAG_BOOL 3
ConfigFile::ConfigFile()
: outputdir("bin"), outputname("out.a"),executable(true), shared(true)
: outputdir("bin"), outputname("out.a"), hFile(""),executable(true), shared(true), generateHFile(false)
{
}
ConfigFile ConfigFile::Load(const std::string& filename)
@@ -51,16 +50,16 @@ ConfigFile ConfigFile::Load(const std::string& filename)
vec = &conf.flags;
loadFlag = FLAG_VECTOR;
}
else if(line == "#srcdirs")
{
vec = &conf.srcdirs;
loadFlag = FLAG_VECTOR;
}
else if(line == "#defines")
{
vec = &conf.defines;
loadFlag = FLAG_VECTOR;
}
else if(line == "#srcdir")
{
s = &conf.srcdir;
loadFlag = FLAG_STRING;
}
else if(line == "#outputdir")
{
s = &conf.outputdir;
@@ -76,6 +75,11 @@ ConfigFile ConfigFile::Load(const std::string& filename)
s = &conf.projectname;
loadFlag = FLAG_STRING;
}
else if(line == "#hfile")
{
s = &conf.hFile;
loadFlag = FLAG_STRING;
}
else if(line == "#executable")
{
b = &conf.executable;
@@ -86,6 +90,11 @@ ConfigFile ConfigFile::Load(const std::string& filename)
b = &conf.shared;
loadFlag = FLAG_BOOL;
}
else if(line == "#generatehfile")
{
b = &conf.generateHFile;
loadFlag = FLAG_BOOL;
}
else
{
LOG_ERROR("Invalid flag: ", line);
@@ -111,8 +120,43 @@ ConfigFile ConfigFile::Load(const std::string& filename)
}
}
}
}
if(conf.hFile == "")
conf.hFile = conf.projectname+".h";
return conf;
}
void ConfigFile::InputBoolean(const std::string& inputText, bool& b)
{
std::string input;
while(true)
{
LOG_INFO(inputText);
std::getline(std::cin, input);
if(input.length() > 0)
{
if(input[0] == 'y' || input[0] == 'n')
{
b = input[0] == 'y';
return;
}
}
}
}
void ConfigFile::InputString(const std::string& inputText, std::string& str, bool needEnding, bool allowEmpty)
{
str = "";
while(true)
{
LOG_INFO(inputText);
std::getline(std::cin, str);
if(needEnding && str[str.length()-1] != '/' && !str.empty())
str += '/';
if(allowEmpty || !str.empty())
return;
}
}
void ConfigFile::InputMultiple(const std::string& inputText, std::vector<std::string>& vec, bool needEnding)
@@ -120,12 +164,9 @@ void ConfigFile::InputMultiple(const std::string& inputText, std::vector<std::st
std::string input;
while(true)
{
LOG_INFO(inputText);
std::getline(std::cin, input);
InputString(inputText, input, needEnding, true);
if(input == "")
break;
if(needEnding && input[input.length()-1] != '/')
input+='/';
vec.push_back(input);
}
}
@@ -133,15 +174,7 @@ void ConfigFile::InputMultiple(const std::string& inputText, std::vector<std::st
ConfigFile ConfigFile::Gen()
{
ConfigFile conf;
std::string input = "";
while(input == "")
{
LOG_INFO("Should it be compiled as an executable (y/n):");
std::getline(std::cin, input);
if(input[0] != 'y' && input[0] != 'n')
input = "";
}
conf.executable = input[0] == 'y';
InputBoolean("Should it be compiled as an executable? (y/n)", conf.executable);
// If it isn't an executable there is not need to have libraries
if(conf.executable)
{
@@ -150,35 +183,22 @@ ConfigFile ConfigFile::Gen()
}
else
{
while(input == "")
InputBoolean("Should it be compiled as a shared library? (y/n)", conf.shared);
InputBoolean("Should it compile a single h file? (y/n):", conf.generateHFile);
if(conf.generateHFile)
{
LOG_INFO("Should it be compiled as a shared library (y/n):");
std::getline(std::cin, input);
if(input[0] != 'y' && input[0] != 'n')
input = "";
InputString("Enter the h file name (and path): ", conf.hFile, false, false);
}
conf.shared = input[0] == 'y';
}
InputMultiple("Enter include directory:", conf.includedirs, true);
InputMultiple("Enter source directories:", conf.srcdirs,true);
InputString("Enter source directories:", conf.srcdir, true, false);
InputMultiple("Enter preprocessor definitions:", conf.defines, false);
InputMultiple("Enter compile flags:", conf.flags, false);
LOG_INFO("Enter output directory (default: bin):");
std::getline(std::cin, conf.outputdir);
InputString("Enter output directory (default: bin):", conf.outputdir, true, true);
if(conf.outputdir == "")
conf.outputdir = "bin/";
conf.outputname = "";
while(conf.projectname == "")
{
LOG_INFO("Enter a name for the project:");
std::getline(std::cin, conf.projectname);
}
while(conf.outputname == "")
{
LOG_INFO("Enter a name for the output file:");
std::getline(std::cin, conf.outputname);
}
InputString("Enter a name for the project:", conf.projectname, false, false);
InputString("Enter a name for the output file:", conf.outputname, false, false);
return conf;
}
@@ -200,11 +220,6 @@ void ConfigFile::Save() const
{
file << *it << std::endl;
}
file << "#srcdirs" << std::endl;
for(auto it = srcdirs.begin();it!=srcdirs.end();++it)
{
file << *it << std::endl;
}
file << "#defines" << std::endl;
for(auto it = defines.begin();it!=defines.end();++it)
{
@@ -215,6 +230,8 @@ void ConfigFile::Save() const
{
file << *it << std::endl;
}
file << "#srcdir" << std::endl;
file << srcdir << std::endl;
file << "#outputdir" << std::endl;
file << outputdir << std::endl;
file << "#projectname" << std::endl;
@@ -223,7 +240,17 @@ void ConfigFile::Save() const
file << outputname << std::endl;
file << "#executable" << std::endl;
file << (executable ? "true" : "false") << std::endl;
file << "#generatehfile" << std::endl;
file << (generateHFile ? "true" : "false") << std::endl;
if(generateHFile)
{
file << "#hfile" << std::endl;
file << hFile << std::endl;
}
if(!executable)
{
file << "#shared" << std::endl;
file << (shared ? "true" : "false") << std::endl;
}
file.close();
}
+5 -1
View File
@@ -9,19 +9,23 @@ class ConfigFile
std::vector<std::string> libs;
std::vector<std::string> libdirs;
std::vector<std::string> includedirs;
std::vector<std::string> srcdirs;
std::vector<std::string> defines;
std::vector<std::string> flags;
std::string outputdir;
std::string srcdir;
std::string outputname;
std::string projectname;
std::string hFile;
bool executable;
bool shared;
bool generateHFile;
public:
ConfigFile();
void Save() const;
static ConfigFile Gen();
static ConfigFile Load(const std::string& filename);
private:
static void InputBoolean(const std::string& inputText, bool& b);
static void InputMultiple(const std::string& inputText, std::vector<std::string>& vec, bool needEnding);
static void InputString(const std::string& inputText, std::string& vec, bool needEnding, bool allowEmpty);
};
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include <string>
#include <fstream>
#include <dirent.h>
#include "Common.h"
#include <cstring>
#include <vector>
struct FileUtils
{
static void GetAllFiles(const std::string& folder, std::vector<std::string>& files)
{
DIR* dp;
struct dirent *dirp;
if((dp = opendir(folder.c_str())) == NULL){
LOG_ERROR(errno);
return;
}
while((dirp = readdir(dp)) != NULL)
{
if(dirp->d_type == DT_DIR)
{
if(strcmp(dirp->d_name,".") == 0)
continue;
if(strcmp(dirp->d_name,"..") == 0)
continue;
GetAllFiles(folder+dirp->d_name+"/", files);
}
else
{
files.push_back(folder+dirp->d_name);
}
}
closedir(dp);
}
};
+31
View File
@@ -0,0 +1,31 @@
#include "HFileGen.h"
#include "FileUtils.h"
#include <set>
void HFileGen::Create(const ConfigFile& conf)
{
std::set<std::string> hFiles;
std::vector<std::string> files;
FileUtils::GetAllFiles(conf.srcdir,files);
// include paramenter with the path of the file
// For example src/graphics/Window.h -> graphics/Window.h if src is a src folder
for(auto it = files.begin(); it!=files.end();++it)
{
size_t extensionPos = it->find_last_of(".");
if(extensionPos != std::string::npos)
{
std::string filename = it->substr(conf.srcdir.length());
if(it->substr(extensionPos+1) == "h" && filename != conf.hFile)
{
// Make files sorted in alphabetical order
hFiles.emplace(filename);
}
}
}
std::ofstream os(conf.srcdir+"/"+conf.hFile);
os << "#pragma once" << std::endl << std::endl;
for(auto&& hFile : hFiles)
os << "#include <" << hFile << ">" << std::endl;
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "ConfigFile.h"
#include <map>
class HFileGen
{
public:
static void Create(const ConfigFile& conf);
};
+4 -4
View File
@@ -2,7 +2,7 @@
std::set<std::string> IncludeDeps::printSet;
int IncludeDeps::printCounter = 0;
IncludeDeps::IncludeDeps(const std::string& filename, const std::string& dir, const std::map<std::string, std::string>& files, std::map<std::string, IncludeDeps*>& allDeps)
IncludeDeps::IncludeDeps(const std::string& filename, const std::string& dir, const std::set<std::string>& files, std::map<std::string, IncludeDeps*>& allDeps)
: filepath(dir+filename)
{
if(filename[filename.length() - 1] =='h')
@@ -20,11 +20,11 @@ IncludeDeps::IncludeDeps(const std::string& filename, const std::string& dir, co
auto it = files.find(include);
if(it != files.end())
{
auto itD = allDeps.find(it->second + it->first);
auto itD = allDeps.find(dir + *it);
if(itD == allDeps.end())
{
IncludeDeps* inc = new IncludeDeps(it->first, it->second,files,allDeps);
dependencies.emplace(it->second+ it->first, inc);
IncludeDeps* inc = new IncludeDeps(*it, dir,files,allDeps);
dependencies.emplace(dir + *it, inc);
}else{
dependencies.emplace(itD->first, itD->second);
}
+1 -1
View File
@@ -17,7 +17,7 @@ class IncludeDeps
static std::set<std::string> printSet;
static int printCounter;
IncludeDeps(const std::string& filename, const std::string& dir, const std::map<std::string, std::string>& files, std::map<std::string, IncludeDeps*>& allDeps);
IncludeDeps(const std::string& filename, const std::string& dir, const std::set<std::string>& files, std::map<std::string, IncludeDeps*>& allDeps);
std::string GetIncludeFile(const std::string& line, size_t pos, const std::string& filename);
+15 -45
View File
@@ -6,37 +6,12 @@
#include <fstream>
#include "IncludeDeps.h"
#include "Common.h"
void Makefile::GetAllFiles(const std::string& folder, std::vector<std::string>& files)
{
DIR* dp;
struct dirent *dirp;
if((dp = opendir(folder.c_str())) == NULL){
LOG_ERROR(errno);
return;
}
while((dirp = readdir(dp)) != NULL)
{
if(dirp->d_type == DT_DIR)
{
if(strcmp(dirp->d_name,".") == 0)
continue;
if(strcmp(dirp->d_name,"..") == 0)
continue;
GetAllFiles(folder+dirp->d_name+"/", files);
}
else
{
files.push_back(folder+dirp->d_name);
}
}
closedir(dp);
}
#include "FileUtils.h"
void Makefile::Save(const ConfigFile& conf)
{
std::map<std::string, std::string> hFiles;
std::map<std::string, std::string> cppFiles;
std::set<std::string> hFiles;
std::set<std::string> cppFiles;
PreSave(conf,hFiles,cppFiles);
std::ofstream outputFile("Makefile");
@@ -65,9 +40,9 @@ void Makefile::Save(const ConfigFile& conf)
outputFile << "OBJECTS=";
for(auto it = cppFiles.begin();it!=cppFiles.end();++it)
{
size_t extensionPos = it->first.find_last_of(".");
size_t slash = it->first.find_last_of("/")+1;
outputFile << "$(OBJPATH)/" << it->first.substr(slash, extensionPos - slash) << ".o ";
size_t extensionPos = it->find_last_of(".");
size_t slash = it->find_last_of("/")+1;
outputFile << "$(OBJPATH)/" << it->substr(slash, extensionPos - slash) << ".o ";
}
outputFile << std::endl;
if(conf.executable || !conf.shared)
@@ -131,13 +106,13 @@ void Makefile::Save(const ConfigFile& conf)
for(auto it = cppFiles.begin(); it!=cppFiles.end();++it)
{
i++;
auto itD = dependencies.find(it->first+it->second);
auto itD = dependencies.find(conf.srcdir + *it);
if(itD == dependencies.end())
{
IncludeDeps* deps = new IncludeDeps(it->first, it->second,hFiles,dependencies);
size_t extensionPos = it->first.find_last_of(".");
size_t slash = it->first.find_last_of("/")+1;
std::string oFile = it->first.substr(slash, extensionPos - slash)+".o ";
IncludeDeps* deps = new IncludeDeps(*it, conf.srcdir,hFiles,dependencies);
size_t extensionPos = it->find_last_of(".");
size_t slash = it->find_last_of("/")+1;
std::string oFile = it->substr(slash, extensionPos - slash)+".o ";
outputFile << "$(OBJPATH)/" << oFile << ": " << *deps << std::endl;
outputFile << "\t$(info -[" << (int)(i / (float)cppFiles.size() * 100) << "%]- $<)" << std::endl;
outputFile << "\t$(CC) $(CFLAGS) -o $@ $<" << std::endl;
@@ -146,13 +121,10 @@ void Makefile::Save(const ConfigFile& conf)
}
}
void Makefile::PreSave(const ConfigFile& conf, std::map<std::string, std::string>& hFiles,
std::map<std::string, std::string>& cppFiles)
{
for(auto itSrc = conf.srcdirs.begin();itSrc != conf.srcdirs.end();++itSrc)
void Makefile::PreSave(const ConfigFile& conf, std::set<std::string>& hFiles, std::set<std::string>& cppFiles)
{
std::vector<std::string> files;
GetAllFiles(*itSrc,files);
FileUtils::GetAllFiles(conf.srcdir,files);
// include paramenter with the path of the file
// For example src/graphics/Window.h -> graphics/Window.h if src is a src folder
for(auto it = files.begin(); it!=files.end();++it)
@@ -162,14 +134,12 @@ void Makefile::PreSave(const ConfigFile& conf, std::map<std::string, std::string
{
if(it->substr(extensionPos+1) == "cpp")
{
cppFiles.emplace(it->substr(itSrc->length()), *itSrc);
cppFiles.emplace(it->substr(conf.srcdir.length()));
}
else
{
hFiles.emplace(it->substr(itSrc->length()), *itSrc);
hFiles.emplace(it->substr(conf.srcdir.length()));
}
}
}
}
}
+2 -3
View File
@@ -1,13 +1,12 @@
#pragma once
#include "ConfigFile.h"
#include <map>
#include <set>
class Makefile
{
public:
static void Save(const ConfigFile& conf);
private:
static void PreSave(const ConfigFile& conf, std::map<std::string, std::string>& hFiles, std::map<std::string, std::string>& cppFiles);
static void GetAllFiles(const std::string& folder, std::vector<std::string>& files);
static void PreSave(const ConfigFile& conf, std::set<std::string>& hFiles, std::set<std::string>& cppFiles);
};
+3
View File
@@ -9,6 +9,7 @@
#include "IncludeDeps.h"
#include "ConfigFile.h"
#include "Makefile.h"
#include "HFileGen.h"
#include "Timer.h"
void PrintHelp()
@@ -30,6 +31,8 @@ bool GenMakefile()
if(f.good())
{
ConfigFile conf = ConfigFile::Load("makegen.conf");
if(conf.generateHFile)
HFileGen::Create(conf);
Makefile::Save(conf);
return true;
}