TDME2  1.9.200
nmakefilegenerator-main.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 
4 #include <tdme/tdme.h>
6 #include <tdme/engine/Version.h>
10 #include <tdme/utilities/Console.h>
13 
14 using std::string;
15 using std::to_string;
16 using std::vector;
17 
26 
27 void scanDir(const string& folder, vector<string>& sourceFiles, vector<string>& mainSourceFiles) {
28  class SourceFilesFilter : public virtual FileNameFilter {
29  public:
30  virtual ~SourceFilesFilter() {}
31 
32  bool accept(const string& pathName, const string& fileName) override {
33  if (fileName == ".") return false;
34  if (fileName == "..") return false;
35  if (FileSystem::getInstance()->isPath(pathName + "/" + fileName) == true) return true;
36  // skip on CPP files that gets #include ed
37  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".incl.cpp") == true) return false;
38  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".include.cpp") == true) return false;
39  // CPP hit, yes
40  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".cpp") == true) return true;
41  return false;
42  }
43  };
44 
45  SourceFilesFilter sourceFilesFilter;
46  vector<string> files;
47 
48  FileSystem::getInstance()->list(folder, files, &sourceFilesFilter);
49 
50  for (const auto& fileName: files) {
51  if (StringTools::endsWith(fileName, "-main.cpp") == true) {
52  mainSourceFiles.push_back(folder + "/" + fileName);
53  } else
54  if (StringTools::endsWith(fileName, ".cpp") == true) {
55  sourceFiles.push_back(folder + "/" + fileName);
56  } else {
57  scanDir(folder + "/" + fileName, sourceFiles, mainSourceFiles);
58  }
59  }
60 }
61 
62 int main(int argc, char** argv)
63 {
64  Console::println(string("nmakefilegenerator ") + Version::getVersion());
65  Console::println(Version::getCopyright());
66  Console::println();
67 
68  if (argc != 2) {
69  Console::println("Usage: nmakefilegenerator path_to_source");
70  Application::exit(Application::EXITCODE_FAILURE);
71  }
72 
73  auto pathToSource = string(argv[1]);
74 
75  try {
76  Console::println("Scanning source files");
77  vector<string> sourceFiles;
78  vector<string> mainSourceFiles;
79  scanDir(pathToSource, sourceFiles, mainSourceFiles);
80 
81  //
82  string sourceFilesVariable = "\\\n";
83  for (const auto& file: sourceFiles) sourceFilesVariable+= "\t" + file + "\\\n";
84  sourceFilesVariable+= "\n";
85 
86  //
87  string mainTargets;
88  for (const auto& file: mainSourceFiles) {
89  if (mainTargets.empty() == false) mainTargets+= " ";
90  mainTargets+= StringTools::substring(file, file.rfind('/') + 1, file.find("-main.cpp"));
91  }
92 
93  Console::println("Generating Makefile");
94 
95  //
96  auto executableFolder = StringTools::replace(argv[0], "\\", "/");
97  auto tdme2Folder = StringTools::substring(executableFolder, 0, StringTools::toLowerCase(executableFolder).rfind("/tdme2/") + string("/tdme2/").length());
98 
99  //
100  auto makefileSource = FileSystem::getInstance()->getContentAsString(tdme2Folder + "/resources/engine/templates/makefiles", "Makefile.nmake");
101  auto makefileMainSourceTemplate = FileSystem::getInstance()->getContentAsString(tdme2Folder + "/resources/engine/templates/makefiles", "Makefile.nmake.main");
102  makefileSource = StringTools::replace(makefileSource, "{$source-files}", sourceFilesVariable);
103  makefileSource = StringTools::replace(makefileSource, "{$main-targets}", mainTargets);
104  makefileSource+= "\n";
105 
106  //
107  for (const auto& file: mainSourceFiles) {
108  auto makefileMainSource = makefileMainSourceTemplate;
109  auto mainTarget = StringTools::substring(file, file.rfind('/') + 1, file.find("-main.cpp"));
110  auto mainTargetSource = file;
111  auto mainTargetExecutable = mainTarget + ".exe";
112  makefileMainSource = StringTools::replace(makefileMainSource, "{$main-target}", mainTarget);
113  makefileMainSource = StringTools::replace(makefileMainSource, "{$main-target-source}", mainTargetSource);
114  makefileMainSource = StringTools::replace(makefileMainSource, "{$main-target-executable}", mainTargetExecutable);
115  makefileSource+= makefileMainSource + "\n";
116  }
117 
118  //
119  FileSystem::getInstance()->setContentFromString(".", "Makefile.nmake", makefileSource);
120  } catch (Exception& exception) {
121  Console::println("An error occurred: " + string(exception.what()));
122  }
123 
124  //
125  Application::exit(Application::EXITCODE_SUCCESS);
126 }
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:41
File system singleton class.
Definition: FileSystem.h:17
Console class.
Definition: Console.h:29
String tools class.
Definition: StringTools.h:22
std::exception Exception
Exception base class.
Definition: Exception.h:18
int main(int argc, char **argv)
void scanDir(const string &folder, vector< string > &sourceFiles, vector< string > &mainSourceFiles)
File system file name filter interface.
virtual bool accept(const string &path, const string &file)=0
Accept a file.