forked from openenclave/oeedger8r-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
169 lines (151 loc) · 4.76 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include <cstdlib>
#include <string>
#include <vector>
#include "args_h_emitter.h"
#include "c_emitter.h"
#include "h_emitter.h"
#include "parser.h"
#ifdef _WIN32
#include <windows.h>
#endif
static void _ensure_directory(const std::string& dir)
{
#if _WIN32
std::string::size_type pos = 0;
do
{
pos = dir.find_first_of("\\/", pos + 1);
std::string subdir = dir.substr(0, pos);
CreateDirectoryA(subdir.c_str(), NULL);
} while (pos != std::string::npos);
#else
system(("mkdir -p " + dir).c_str());
#endif
}
const char* usage =
"usage: oeedger8r [options] <file> ...\n"
"\n"
"[options]\n"
"--search-path <path> Specify the search path of EDL files\n"
"--use-prefix Prefix untrusted proxy with Enclave name\n"
"--header-only Only generate header files\n"
"--untrusted Generate untrusted proxy and bridge\n"
"--trusted Generate trusted proxy and bridge\n"
"--untrusted-dir <dir> Specify the directory for saving untrusted code\n"
"--trusted-dir <dir> Specify the directory for saving trusted code\n"
"-D<name> Define the name to be used by the C-style "
"preprocessor\n"
"--experimental Enable experimental features\n"
"--help Print this help message\n"
"\n"
"If neither `--untrusted' nor `--trusted' is specified, generate both.\n";
int main(int argc, char** argv)
{
std::vector<std::string> searchpaths;
bool use_prefix = false;
bool header_only = false;
bool gen_untrusted = false;
bool gen_trusted = false;
std::string untrusted_dir = ".";
std::string trusted_dir = ".";
std::vector<std::string> files;
std::vector<std::string> defines;
int i = 1;
if (argc == 1)
{
printf("%s\n", usage);
return 1;
}
auto get_dir = [argc, argv](int i) {
if (i == argc)
{
fprintf(
stderr,
"error: missing directory name after %s\n",
argv[i - 1]);
fprintf(stderr, "%s\n", usage);
exit(1);
}
return fix_path_separators(argv[i]);
};
while (i < argc)
{
std::string a = argv[i++];
if (a == "--search-path")
searchpaths.push_back(get_dir(i++));
else if (a == "--use-prefix")
use_prefix = true;
else if (a == "--header-only")
header_only = true;
else if (a == "--untrusted")
gen_untrusted = true;
else if (a == "--trusted")
gen_trusted = true;
else if (a == "--trusted-dir")
trusted_dir = get_dir(i++);
else if (a == "--untrusted-dir")
untrusted_dir = get_dir(i++);
else if (a == "--experimental")
;
else if (a.rfind("-D", 0) == 0)
{
std::string define = a.substr(2);
if (define.empty())
{
fprintf(stderr, "error: macro name missing after '-D'\n");
fprintf(stderr, "%s", usage);
return -1;
}
defines.push_back(define);
}
else if (a == "--help")
{
printf("%s\n", usage);
return 1;
}
else
files.push_back(fix_path_separators(a));
}
if (files.empty())
{
fprintf(stderr, "error: missing edl filename.\n");
fprintf(stderr, "%s", usage);
return -1;
}
printf("Generating edge routine, for the Open Enclave SDK.\n");
if (!gen_trusted && !gen_untrusted)
gen_trusted = gen_untrusted = true;
const char* sep = path_sep();
// Add separators. / works on both Linux and Windows.
if (trusted_dir.back() != sep[0])
trusted_dir += sep;
if (trusted_dir != std::string(".") + sep)
_ensure_directory(trusted_dir);
if (untrusted_dir.back() != sep[0])
untrusted_dir += sep;
if (untrusted_dir != std::string(".") + sep)
_ensure_directory(untrusted_dir);
for (std::string& file : files)
{
Parser p(file, searchpaths, defines);
Edl* edl = p.parse();
if (gen_trusted)
{
ArgsHEmitter(edl).emit(trusted_dir);
HEmitter(edl).emit_t_h(trusted_dir);
if (!header_only)
CEmitter(edl).emit_t_c(trusted_dir);
}
if (gen_untrusted)
{
std::string prefix = use_prefix ? (edl->name_ + "_") : "";
ArgsHEmitter(edl).emit(untrusted_dir);
HEmitter(edl).emit_u_h(untrusted_dir, prefix);
if (!header_only)
CEmitter(edl).emit_u_c(untrusted_dir, prefix);
}
}
printf("Success.\n");
}