Add jobs as default behaviour, Add run to makefile

This commit is contained in:
Thraix
2019-10-04 16:48:22 +02:00
parent 5a103442f8
commit 8315fd0897
5 changed files with 183 additions and 65 deletions
+5 -4
View File
@@ -1,4 +1,4 @@
# This Makefile was generated using MakeGen v1.1.3 made by Tim Håkansson # This Makefile was generated using MakeGen v1.1.4 made by Tim Håkansson
# and is licensed under MIT. Full source of the project can be found at # and is licensed under MIT. Full source of the project can be found at
# https://github.com/Thraix/MakeGen # https://github.com/Thraix/MakeGen
CC=@g++ CC=@g++
@@ -13,15 +13,16 @@ LIBDIR=
LDFLAGS= LDFLAGS=
LIBS=$(LIBDIR) LIBS=$(LIBDIR)
OUTPUT=$(BIN)makegen OUTPUT=$(BIN)makegen
.PHONY: all directories rebuild clean dependencies .PHONY: all directories rebuild clean run
all: dependencies directories $(OUTPUT) all: directories $(OUTPUT)
dependencies:
directories: $(BIN) $(OBJPATH) directories: $(BIN) $(OBJPATH)
$(BIN): $(BIN):
$(info Creating output directories) $(info Creating output directories)
@$(MKDIR_P) $(BIN) @$(MKDIR_P) $(BIN)
$(OBJPATH): $(OBJPATH):
@$(MKDIR_P) $(OBJPATH) @$(MKDIR_P) $(OBJPATH)
run: all
@./$(OUTPUT)
rebuild: clean all rebuild: clean all
clean: clean:
$(info Removing intermediates) $(info Removing intermediates)
+8 -1
View File
@@ -13,12 +13,19 @@
// Release , should be backwards compatible with any minor version // Release , should be backwards compatible with any minor version
#define MAKEGEN_VERSION_RELEASE 1 #define MAKEGEN_VERSION_RELEASE 1
// Minor changes, should be compatible with any other minor version with same major and release. // Minor changes, should be compatible with any other minor version with same major and release.
#define MAKEGEN_VERSION_MINOR 3 #define MAKEGEN_VERSION_MINOR 4
#define MAKEGEN_VERSION ("v" STR(MAKEGEN_VERSION_MAJOR) "." STR(MAKEGEN_VERSION_RELEASE) "." STR(MAKEGEN_VERSION_MINOR)) #define MAKEGEN_VERSION ("v" STR(MAKEGEN_VERSION_MAJOR) "." STR(MAKEGEN_VERSION_RELEASE) "." STR(MAKEGEN_VERSION_MINOR))
const static unsigned int FLAG_HELP = BIT(0); const static unsigned int FLAG_HELP = BIT(0);
const static unsigned int FLAG_GEN = BIT(1); const static unsigned int FLAG_GEN = BIT(1);
const static unsigned int FLAG_VERSION = BIT(2); const static unsigned int FLAG_VERSION = BIT(2);
const static unsigned int FLAG_CLEAN = BIT(3);
const static unsigned int FLAG_MAKE = BIT(4);
const static unsigned int FLAG_RUN = BIT(5);
const static unsigned int FLAG_INSTALL = BIT(6);
const static unsigned int FLAG_REBUILD = BIT(7);
const static unsigned int FLAG_SINGLE_THREAD = BIT(8);
const static unsigned int FLAG_DEPENDENCY = BIT(9);
#define LOG_INFO(...) Log(__VA_ARGS__); std::cout << std::endl #define LOG_INFO(...) Log(__VA_ARGS__); std::cout << std::endl
+21 -5
View File
@@ -1,16 +1,32 @@
#pragma once #pragma once
#include <string>
#include <fstream>
#include <dirent.h>
#include "Common.h" #include "Common.h"
#include <cstring>
#include <vector>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <vector>
struct FileUtils struct FileUtils
{ {
static bool HasPath(const std::string& path)
{
struct stat info;
if(stat(path.c_str(), &info) != 0)
return false;
else
return true;
}
static bool CreateDirectory(const std::string& path)
{
return mkdir(path.c_str(), 0777);
}
static std::string GetRealPath(const std::string& filename) static std::string GetRealPath(const std::string& filename)
{ {
#if defined(__linux__) #if defined(__linux__)
+26 -13
View File
@@ -94,39 +94,51 @@ void Makefile::Save(const ConfigFile& conf)
} }
} }
outputFile << "OUTPUT=$(BIN)" << conf.outputname << std::endl; outputFile << "OUTPUT=$(BIN)" << conf.outputname << std::endl;
outputFile << ".PHONY: all directories rebuild clean dependencies" << std::endl; outputFile << ".PHONY: all directories rebuild clean run" << std::endl;
outputFile << "all: dependencies directories $(OUTPUT)" << std::endl;
//outputFile << "\t$(info ------------------------)" << std::endl;
//outputFile << "\t$(info ---- Done Compiling ----)" << std::endl;
//outputFile << "\t$(info ------------------------)" << std::endl;
outputFile << "dependencies:" << std::endl; // All
if(!conf.dependencies.empty()) outputFile << "all: directories $(OUTPUT)" << std::endl;
{
//outputFile << "\t$(info Building dependencies)" << std::endl; // Directories
//outputFile << "\t@for dep in $(DEPENDENCIES); do\\" << std::endl;
//outputFile << "\t\tmakegen -C $$dep;\\" << std::endl;
//outputFile << "\tdone" << std::endl;
}
outputFile << "directories: $(BIN) $(OBJPATH)" << std::endl; outputFile << "directories: $(BIN) $(OBJPATH)" << std::endl;
// Bin path
outputFile << "$(BIN):" << std::endl; outputFile << "$(BIN):" << std::endl;
outputFile << "\t$(info Creating output directories)" << std::endl; outputFile << "\t$(info Creating output directories)" << std::endl;
outputFile << "\t@$(MKDIR_P) $(BIN)" << std::endl; outputFile << "\t@$(MKDIR_P) $(BIN)" << std::endl;
// Object path
outputFile << "$(OBJPATH):" << std::endl; outputFile << "$(OBJPATH):" << std::endl;
outputFile << "\t@$(MKDIR_P) $(OBJPATH)" << std::endl; outputFile << "\t@$(MKDIR_P) $(OBJPATH)" << std::endl;
// Run
outputFile << "run: all" << std::endl;
if(conf.executable)
{
outputFile << "\t@./$(OUTPUT)" << std::endl;
}
// Rebuild
outputFile << "rebuild: clean all" << std::endl; outputFile << "rebuild: clean all" << std::endl;
// Clean
outputFile << "clean:" << std::endl; outputFile << "clean:" << std::endl;
outputFile << "\t$(info Removing intermediates)" << std::endl; outputFile << "\t$(info Removing intermediates)" << std::endl;
outputFile << "\trm -rf $(OBJPATH)/*.o" << std::endl; outputFile << "\trm -rf $(OBJPATH)/*.o" << std::endl;
// Output file
outputFile << "$(OUTPUT): $(OBJECTS)" << std::endl; outputFile << "$(OUTPUT): $(OBJECTS)" << std::endl;
outputFile << "\t$(info Generating output file)" << std::endl; outputFile << "\t$(info Generating output file)" << std::endl;
if(conf.executable) if(conf.executable)
outputFile << "\t$(CO) $(OUTPUT) $(OBJECTS) $(LDFLAGS) $(LIBS)" << std::endl; outputFile << "\t$(CO) $(OUTPUT) $(OBJECTS) $(LDFLAGS) $(LIBS)" << std::endl;
else else
outputFile << "\t$(CO) $(OUTPUT) $(OBJECTS)" << std::endl; outputFile << "\t$(CO) $(OUTPUT) $(OBJECTS)" << std::endl;
// Install
outputFile << "install: all" << std::endl; outputFile << "install: all" << std::endl;
outputFile << "\t$(info Installing " << conf.projectname <<" to /usr/bin/)" << std::endl; outputFile << "\t$(info Installing " << conf.projectname <<" to /usr/bin/)" << std::endl;
outputFile << "\t@cp $(OUTPUT) /usr/bin/" << conf.outputname << std::endl; outputFile << "\t@cp $(OUTPUT) /usr/bin/" << conf.outputname << std::endl;
std::map<std::string, IncludeDeps*> dependencies; std::map<std::string, IncludeDeps*> dependencies;
size_t i = 0; size_t i = 0;
for(auto it = cppFiles.begin(); it!=cppFiles.end();++it) for(auto it = cppFiles.begin(); it!=cppFiles.end();++it)
@@ -139,6 +151,7 @@ void Makefile::Save(const ConfigFile& conf)
size_t extensionPos = it->find_last_of("."); size_t extensionPos = it->find_last_of(".");
size_t slash = it->find_last_of("/")+1; size_t slash = it->find_last_of("/")+1;
std::string oFile = it->substr(slash, extensionPos - slash)+".o "; std::string oFile = it->substr(slash, extensionPos - slash)+".o ";
outputFile << "$(OBJPATH)/" << oFile << ": "; outputFile << "$(OBJPATH)/" << oFile << ": ";
deps->Output(outputFile, conf); deps->Output(outputFile, conf);
outputFile << std::endl; outputFile << std::endl;
+112 -31
View File
@@ -1,28 +1,55 @@
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <fstream>
#include <cmath>
#include "Common.h" #include "Common.h"
#include "IncludeDeps.h"
#include "ConfigFile.h" #include "ConfigFile.h"
#include "Makefile.h" #include "FileUtils.h"
#include "HFileGen.h" #include "HFileGen.h"
#include "IncludeDeps.h"
#include "Makefile.h"
#include "Timer.h" #include "Timer.h"
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <sys/sysinfo.h>
#include <vector>
#include <thread>
#define RETURN_IF(x, b) \
if(x)\
return b;
void PrintHelp() void PrintHelp()
{ {
LOG_INFO("MakeGen ", MAKEGEN_VERSION);
LOG_INFO("");
LOG_INFO("MakeGen is a utility tool to generate and run Makefiles in a simple manner.\nBy default it always compiles code with parallell jobs.");
LOG_INFO("");
LOG_INFO("Usage: makegen [options]"); LOG_INFO("Usage: makegen [options]");
LOG_INFO("");
LOG_INFO(" Options:"); LOG_INFO(" Options:");
LOG_INFO(" --help\tDisplays this information"); LOG_INFO(" -h, --help Displays this information");
LOG_INFO(" --conf\tGenerate a config file for the project"); LOG_INFO(" --conf Prompt the user to enter information to create config");
LOG_INFO(" --version\tDisplays the version of this program"); LOG_INFO(" file");
LOG_INFO(" install\tGenerates a Makefile and runs make install"); LOG_INFO(" -v, --version Displays the version of this program");
LOG_INFO(" clean\t\tGenerates a Makefile and runs make clean"); LOG_INFO(" -m,-a, make,all Generates a Makefile and runs");
LOG_INFO(" rebuild\tGenerates a Makefile and runs make rebuild"); LOG_INFO(" make all");
LOG_INFO(" If no option is given it will run default make"); LOG_INFO(" -i, install Generates a Makefile and runs");
LOG_INFO(" make all && make install");
LOG_INFO(" -c, clean Generates a Makefile and runs");
LOG_INFO(" make clean");
LOG_INFO(" -r, rebuild Generates a Makefile and runs");
LOG_INFO(" make clean && make all");
LOG_INFO(" -e, run, execute Generates a Makefile and runs");
LOG_INFO(" make all && make run");
LOG_INFO(" -s, single Runs additional makegen options as single thread");
LOG_INFO(" (no --jobs=X flag)");
LOG_INFO("");
LOG_INFO(" If no option is given it will run \"make all\"");
LOG_INFO("");
LOG_INFO(" If multiple make options are given it will run in the following order:");
LOG_INFO(" clean all install run, rebuild will be translated to \"clean make\"");
} }
std::optional<ConfigFile> GetConfigFile(const std::string& filepath) std::optional<ConfigFile> GetConfigFile(const std::string& filepath)
@@ -47,14 +74,13 @@ void GenMakefile(const ConfigFile& conf)
unsigned int ReadFlags(int argc, char** argv) unsigned int ReadFlags(int argc, char** argv)
{ {
unsigned int flags = 0; unsigned int flags = 0;
bool make = true;
for(int i = 1;i<argc;i++) for(int i = 1;i<argc;i++)
{ {
if(strlen(argv[i]) > 1) if(strlen(argv[i]) > 1)
{
if(argv[i][0] == '-' && argv[i][1] == '-')
{ {
std::string flag(argv[i]); std::string flag(argv[i]);
if(flag == "--help") if(flag == "-h" || flag == "--help")
{ {
flags |= FLAG_HELP; flags |= FLAG_HELP;
} }
@@ -62,21 +88,77 @@ unsigned int ReadFlags(int argc, char** argv)
{ {
flags |= FLAG_GEN; flags |= FLAG_GEN;
} }
else if(flag == "--version") else if(flag == "-v" || flag == "--version")
{ {
flags |= FLAG_VERSION; flags |= FLAG_VERSION;
} }
else if(flag == "make" || flag == "-m" || flag == "all" || flag == "-a")
{
flags |= FLAG_MAKE;
}
else if(flag == "clean" || flag == "-c")
{
make = false;
flags |= FLAG_CLEAN;
}
else if(flag == "run" || flag == "-e" || flag == "execute")
{
flags |= FLAG_RUN;
}
else if(flag == "install" || flag == "-i")
{
flags |= FLAG_INSTALL;
}
else if(flag == "rebuild" || flag == "-r")
{
flags |= FLAG_CLEAN;
flags |= FLAG_MAKE;
}
else if(flag == "single" || flag == "-s")
{
flags |= FLAG_SINGLE_THREAD;
}
else if(flag != "")
{
LOG_ERROR("Unknown argument ", flag);
return FLAG_HELP;
} }
} }
} }
if(make)
flags |= FLAG_MAKE;
return flags; return flags;
} }
bool MakeGen(const std::string& filepath, const std::string& args, const ConfigFile& conf) bool RunMake(const std::string& filepath, unsigned int flags, const ConfigFile& conf)
{
std::string make = "make --no-print-directory -C " + filepath;
if(!(flags & FLAG_SINGLE_THREAD))
make += " -j" + std::to_string(std::thread::hardware_concurrency()) + " ";
if(flags & FLAG_CLEAN)
{
RETURN_IF(system(std::string(make + " clean").c_str()) != 0, false);
}
if(flags & FLAG_MAKE)
{
RETURN_IF(system(std::string(make + " all").c_str()) != 0, false);
}
if(flags & FLAG_INSTALL)
{
RETURN_IF(system(std::string(make + " install").c_str()) != 0, false);
}
if(flags & FLAG_RUN && conf.executable)
{
RETURN_IF(system(std::string(make + " run").c_str()) != 0, false);
}
return true;
}
bool MakeGen(const std::string& filepath, unsigned int flags, const ConfigFile& conf)
{ {
for(size_t i = 0;i<conf.dependencies.size();++i) for(size_t i = 0;i<conf.dependencies.size();++i)
{ {
bool success = MakeGen(conf.dependencies[i], args, conf.dependencyConfigs[i]); bool success = MakeGen(conf.dependencies[i], flags, conf.dependencyConfigs[i]);
if(!success) if(!success)
return success; return success;
} }
@@ -88,10 +170,14 @@ bool MakeGen(const std::string& filepath, const std::string& args, const ConfigF
LOG_INFO("Took ", round(timer.Elapsed()*1000.0)/1000.0,"s"); LOG_INFO("Took ", round(timer.Elapsed()*1000.0)/1000.0,"s");
LOG_INFO("Running Makefile..."); LOG_INFO("Running Makefile...");
return system(std::string("make --no-print-directory -C " + filepath + " " + args).c_str()) == 0; if(!FileUtils::HasPath(conf.configPath + conf.outputdir))
{
FileUtils::CreateDirectory(conf.configPath + conf.outputdir);
FileUtils::CreateDirectory(conf.configPath + conf.outputdir + "intermediates");
}
LOG_INFO(conf.configPath + conf.outputdir);
return RunMake(filepath, flags, conf);
} }
#include "FileUtils.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@@ -112,15 +198,10 @@ int main(int argc, char** argv)
return 0; return 0;
} }
std::string args = std::string("");
for(int i = 1;i<argc;i++)
{
args += " " + std::string(argv[i]);
}
auto conf = GetConfigFile("./"); auto conf = GetConfigFile("./");
if(conf) if(conf)
{ {
bool success = MakeGen("./", args, *conf); bool success = MakeGen("./", flags, *conf);
return success ? 0 : 1; return success ? 0 : 1;
} }
else else