TDME2 1.9.121
makefilegenerator-main.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <string>
3#include <vector>
4
5#include <tdme/tdme.h>
14
15using std::string;
16using std::to_string;
17using std::vector;
18
27
28void scanDir(const string& folder, vector<string>& sourceFiles, vector<string>& mainSourceFiles) {
29 class SourceFilesFilter : public virtual FileNameFilter {
30 public:
31 virtual ~SourceFilesFilter() {}
32
33 bool accept(const string& pathName, const string& fileName) override {
34 if (fileName == ".") return false;
35 if (fileName == "..") return false;
36 if (FileSystem::getInstance()->isPath(pathName + "/" + fileName) == true) return true;
37 if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".cpp") == true) return true;
38 return false;
39 }
40 };
41
42 SourceFilesFilter sourceFilesFilter;
43 vector<string> files;
44
45 FileSystem::getInstance()->list(folder, files, &sourceFilesFilter);
46
47 for (auto fileName: files) {
48 if (StringTools::endsWith(fileName, "-main.cpp") == true) {
49 mainSourceFiles.push_back(folder + "/" + fileName);
50 } else
51 if (StringTools::endsWith(fileName, ".cpp") == true) {
52 sourceFiles.push_back(folder + "/" + fileName);
53 } else {
54 scanDir(folder + "/" + fileName, sourceFiles, mainSourceFiles);
55 }
56 }
57}
58
59int main(int argc, char** argv)
60{
61 Console::println(string("makefilegenerator ") + Version::getVersion());
62 Console::println(Version::getCopyright());
63 Console::println();
64
65 if (argc != 2) {
66 Console::println("Usage: makefilegenerator path_to_source");
67 Application::exit(1);
68 }
69
70 auto pathToSource = string(argv[1]);
71
72 try {
73 Console::println("Scanning source files");
74 vector<string> sourceFiles;
75 vector<string> mainSourceFiles;
76 scanDir(pathToSource, sourceFiles, mainSourceFiles);
77
78 string sourceFilesVariable = "\\\n";
79 for (auto& file: sourceFiles) sourceFilesVariable+= "\t" + file + "\\\n";
80 sourceFilesVariable+= "\n";
81
82 string mainSourceFilesVariable = "\\\n";
83 for (auto& file: mainSourceFiles) mainSourceFilesVariable+= "\t" + file + "\\\n";
84 mainSourceFilesVariable+= "\n";
85
86 Console::println("Generating Makefile");
87
88 auto executableFolder = StringTools::replace(argv[0], "\\", "/");
89 auto tdme2Folder = StringTools::substring(executableFolder, 0, executableFolder.rfind("/tdme2/") + string("/tdme2/").length());
90
91 auto makefileSource = FileSystem::getInstance()->getContentAsString(tdme2Folder + "/resources/engine/templates/makefiles", "Makefile");
92 makefileSource = StringTools::replace(makefileSource, "{$source-files}", sourceFilesVariable);
93 makefileSource = StringTools::replace(makefileSource, "{$main-source-files}", mainSourceFilesVariable);
94 FileSystem::getInstance()->setContentFromString(".", "Makefile", makefileSource);
95 } catch (Exception& exception) {
96 Console::println("An error occurred: " + string(exception.what()));
97 }
98}
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:37
File system singleton class.
Definition: FileSystem.h:14
Console class.
Definition: Console.h:26
String tools class.
Definition: StringTools.h:20
int main(int argc, char **argv)
void scanDir(const string &folder, vector< string > &sourceFiles, vector< string > &mainSourceFiles)
std::exception Exception
Exception base class.
Definition: Exception.h:19
File system file name filter interface.
virtual bool accept(const string &path, const string &file)=0
Accept a file.