1111#include < set>
1212#include < string>
1313#include < unordered_map>
14+ #include < unordered_set>
1415#include < vector>
1516
1617using namespace metagen ;
1718
1819#define UMBRELLA_HEADER " umbrella.h"
1920
2021int main (int argc, char **argv) {
21- char *target = argv[1 ];
22- char *outputFile = argv[2 ];
23- char *outputTypesDir = argv[3 ];
24- std::string sdk = argv[4 ];
25- std::string frameworksDir = sdk + " /System/Library/Frameworks" ;
22+ std::string outputFile;
23+ std::string outputTypesDir;
24+ std::string sdk;
25+ std::string frameworksDir;
2626
27- std::set<std::string> frameworks;
28-
29- for (int i = 5 ; i < argc; i++) {
30- frameworks.insert (argv[i]);
31- }
27+ std::string code = " " ;
3228
3329 std::vector<std::string> args = {" -Xclang" ,
3430 " -isysroot" ,
35- sdk ,
31+ " " ,
3632 " -x" ,
3733 " objective-c" ,
3834 " -fno-objc-arc" ,
@@ -44,18 +40,54 @@ int main(int argc, char **argv) {
4440 " -Wno-expansion-to-defined" ,
4541 " -Wno-deprecated-declarations" ,
4642 " -Wno-objc-property-no-attribute" ,
47- " -std=gnu11" ,
48- " -I" + sdk + " /usr/include" ,
49- " -F" + frameworksDir,
50- " -target" ,
51- target};
52-
53- std::string code = " " ;
54-
55- for (auto &framework : frameworks) {
56- args.emplace_back (" -I" + frameworksDir + " /" + framework +
57- " .framework/Headers" );
58- code += " #import <" + framework + " /" + framework + " .h>\n " ;
43+ " -std=gnu11" };
44+
45+ std::unordered_set<std::string> includePaths;
46+
47+ for (int i = 1 ; i < argc; i++) {
48+ std::string arg = argv[i];
49+ if (arg.find (" framework=" ) == 0 ) {
50+ if (frameworksDir.empty ()) {
51+ std::cout << " framework= argument must be specified after sdk="
52+ << std::endl;
53+ std::exit (1 );
54+ }
55+
56+ std::string framework = arg.substr (10 );
57+ std::string includePath = frameworksDir + " /" + framework + " .framework" ;
58+ includePaths.emplace (includePath);
59+ args.emplace_back (" -I" + includePath + " /Headers" );
60+ code += " #import <" + framework + " /" + framework + " .h>\n " ;
61+ } else if (arg.find (" include=" ) == 0 ) {
62+ std::string includeDir = arg.substr (8 );
63+ includePaths.emplace (includeDir);
64+ } else if (arg.find (" headers=" ) == 0 ) {
65+ std::string includeDir = arg.substr (8 );
66+ args.emplace_back (" -I" + includeDir);
67+ } else if (arg.find (" import=" ) == 0 ) {
68+ std::string import = arg.substr (7 );
69+ code += " #import " + import + " \n " ;
70+ } else if (arg.find (" sdk=" ) == 0 ) {
71+ sdk = arg.substr (4 );
72+ args[2 ] = sdk;
73+ args.emplace_back (" -I" + sdk + " /usr/include" );
74+ frameworksDir = sdk + " /System/Library/Frameworks" ;
75+ args.emplace_back (" -F" + frameworksDir);
76+ } else if (arg.find (" target=" ) == 0 ) {
77+ std::string target = arg.substr (7 );
78+ args.emplace_back (" -target" );
79+ args.emplace_back (target);
80+ } else if (arg.find (" arg=" ) == 0 ) {
81+ std::string argval = arg.substr (4 );
82+ args.emplace_back (argval);
83+ } else if (arg.find (" output=" ) == 0 ) {
84+ outputFile = arg.substr (7 );
85+ } else if (arg.find (" types=" ) == 0 ) {
86+ outputTypesDir = arg.substr (6 );
87+ } else {
88+ std::cout << " Unknown argument: " << arg << std::endl;
89+ std::exit (1 );
90+ }
5991 }
6092
6193 std::vector<const char *> argsC (args.size ());
@@ -72,13 +104,13 @@ int main(int argc, char **argv) {
72104 index, UMBRELLA_HEADER , argsC.data (), args.size (), nullptr , 0 ,
73105 CXTranslationUnit_None);
74106
107+ std::remove (UMBRELLA_HEADER );
108+
75109 if (unit == nullptr ) {
76110 std::cout << " Failed to parse translation unit" << std::endl;
77111 std::exit (1 );
78112 }
79113
80- std::remove (UMBRELLA_HEADER );
81-
82114 // print diagnostics
83115 unsigned numDiagnostics = clang_getNumDiagnostics (unit);
84116
@@ -100,9 +132,7 @@ int main(int argc, char **argv) {
100132 CXCursor cursor = clang_getTranslationUnitCursor (unit);
101133
102134 MetadataFactory factory;
103- for (auto fw : frameworks) {
104- factory.frameworks .emplace (fw);
105- }
135+ factory.includePaths = includePaths;
106136 factory.process (cursor);
107137 factory.postProcess ();
108138
@@ -121,7 +151,6 @@ int main(int argc, char **argv) {
121151 std::cout << " total size: " << result.second / 1024 . / 1024 .
122152 << " MB" << std::endl;
123153 std::cout << " total sections: " << MD_NUM_SECTIONS << std::endl;
124- std::cout << " num frameworks: " << frameworks.size () << std::endl;
125154 std::cout << " strings (n, size): " << writer.strings .size () << " , "
126155 << writer.strings .section_size / 1024 . << " KB" << std::endl;
127156 std::cout << " consts (n, size): " << writer.constants .size () << " ,"
@@ -141,7 +170,7 @@ int main(int argc, char **argv) {
141170 std::cout << " unions (n, size): " << writer.unions .size () << " , "
142171 << writer.unions .section_size / 1024 . << " KB" << std::endl;
143172
144- auto file = std::fopen (outputFile, " w" );
173+ auto file = std::fopen (outputFile. c_str () , " w" );
145174 std::fwrite (result.first , 1 , result.second , file);
146175 std::fclose (file);
147176
0 commit comments