1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 
  7 #include <cpp2cm/cpp2cm/System.hpp>
  8 #include <cpp2cm/cpp2cm/Project.hpp>
  9 #include <cpp2cm/cpp2cm/Converter.hpp>
 10 #include <sngcpp/pp/InitDone.hpp>
 11 #include <sngcpp/ast/InitDone.hpp>
 12 #include <sngcm/ast/InitDone.hpp>
 13 #include <sngxml/xpath/InitDone.hpp>
 14 #include <soulng/util/InitDone.hpp>
 15 #include <soulng/util/TextUtils.hpp>
 16 #include <soulng/util/Path.hpp>
 17 #include <soulng/util/System.hpp>
 18 #include <iostream>
 19 #include <stdexcept>
 20 #include <stdlib.h>
 21 
 22 using namespace soulng::util;
 23 
 24 void PrintHelp()
 25 {
 26     std::cout << "C++ to Cmajor converter version " << cpp2cm::Cpp2CmVersion() << std::endl;
 27     std::cout << "usage: cpp2cm [options] [ [PATH/]PROJECT.xml ]" << std::endl;
 28     std::cout << "options:" << std::endl;
 29     std::cout << "--help | -h" << std::endl;
 30     std::cout << "  Print help and exit." << std::endl;
 31     std::cout << "--verbose | -v" << std::endl;
 32     std::cout << "  Be verbose." << std::endl;
 33     std::cout << "--install | -i" << std::endl;
 34     std::cout << "  Install to installation directory defined in the PROJECT.xml." << std::endl;
 35     std::cout << "--system | -s" << std::endl;
 36     std::cout << "  Build system XML's." << std::endl;
 37     std::cout << "--nothrow-info | -n" << std::endl;
 38     std::cout << "  Print nothrow status for each file, class and function." << std::endl;
 39 }
 40 
 41 std::string SystemDirectoryPath()
 42 {
 43     std::string soulngRootDir;
 44     const char* soulngRootEnv = std::getenv("SOULNG_ROOT");
 45     if (soulngRootEnv)
 46     {
 47         soulngRootDir = soulngRootEnv;
 48     }
 49     if (soulngRootDir.empty())
 50     {
 51         throw std::runtime_error("please set SOULNG_ROOT environment variable to point to /path/to/soulng-2.0.0 directory");
 52     }
 53     return GetFullPath(Path::Combine(soulngRootDir"tools/cpp2cm/system"));
 54 }
 55 
 56 struct Initializer 
 57 {
 58     Initializer()
 59     {
 60         soulng::util::Init();
 61         sngxml::xpath::Init();
 62         sngcpp::pp::Init();
 63         sngcpp::ast::Init();
 64         sngcm::ast::Init();
 65     }
 66     ~Initializer()
 67     {
 68         sngcm::ast::Done();
 69         sngcpp::ast::Done();
 70         sngcpp::pp::Done();
 71         sngxml::xpath::Done();
 72         soulng::util::Done();
 73     }
 74 };
 75 
 76 int main(int argcconst char** argv)
 77 {
 78     Initializer initializer;
 79     try
 80     {
 81         bool verbose = false;
 82         bool system = false;
 83         cpp2cm::ProcessType processType = cpp2cm::ProcessType::stage;
 84         std::string projectXmlFilePath;
 85         bool nothrowStatus = false;
 86         for (int i = 1; i < argc; ++i)
 87         {
 88             std::string arg = argv[i];
 89             if (StartsWith(arg"--"))
 90             {
 91                 if (arg == "--help")
 92                 {
 93                     PrintHelp();
 94                     return 1;
 95                 }
 96                 else if (arg == "--verbose")
 97                 {
 98                     verbose = true;
 99                 }
100                 else if (arg == "--system")
101                 {
102                     system = true;
103                 }
104                 else if (arg == "--install")
105                 {
106                     processType = cpp2cm::ProcessType::install;
107                 }
108                 else if (arg == "--nothrow-info")
109                 {
110                     nothrowStatus = true;
111                 }
112                 else
113                 {
114                     throw std::runtime_error("unknown option '" + arg + "'");
115                 }
116             }
117             else if (StartsWith(arg"-"))
118             {
119                 std::string options = arg.substr(1);
120                 for (char o : options)
121                 {
122                     if (o == 'h')
123                     {
124                         PrintHelp();
125                         return 1;
126                     }
127                     else if (o == 'v')
128                     {
129                         verbose = true;
130                     }
131                     else if (o == 's')
132                     {
133                         system = true;
134                     }
135                     else if (o == 'i')
136                     {
137                         processType = cpp2cm::ProcessType::install;
138                     }
139                     else if (o == 'n')
140                     {
141                         nothrowStatus = true;
142                     }
143                     else
144                     {
145                         throw std::runtime_error("unknown option '-" + std::string(1o) + "'");
146                     }
147                 }
148             }
149             else
150             {
151                 projectXmlFilePath = GetFullPath(arg);
152             }
153         }
154         std::string systemXmlFilePath = GetFullPath(Path::Combine(SystemDirectoryPath()"system.xml"));
155         if (system)
156         {
157             cpp2cm::ProcessSystemXml(systemXmlFilePathverbose);
158         }
159         else
160         {
161             cpp2cm::Project project(systemXmlFilePathprojectXmlFilePathnothrowStatus);
162             project.Process(verboseprocessType);
163         }
164     }
165     catch (const std::exception&  ex;)
166     {
167         std::cerr << ex.what() << std::endl;
168         return 1;
169     }
170     return 0;
171 }