Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: dart/lib/compiler/implementation/dart2js.dart

Issue 10407092: Add --package-root option to the compiler. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('dart2js'); 5 #library('dart2js');
6 6
7 #import('dart:io'); 7 #import('dart:io');
8 #import('dart:uri'); 8 #import('dart:uri');
9 #import('dart:utf'); 9 #import('dart:utf');
10 10
(...skipping 11 matching lines...) Expand all
22 String pattern; 22 String pattern;
23 HandleOption handle; 23 HandleOption handle;
24 24
25 OptionHandler(this.pattern, this.handle); 25 OptionHandler(this.pattern, this.handle);
26 } 26 }
27 27
28 String extractParameter(String argument) { 28 String extractParameter(String argument) {
29 return argument.substring(argument.indexOf('=') + 1); 29 return argument.substring(argument.indexOf('=') + 1);
30 } 30 }
31 31
32 String extractPath(String argument) {
33 String path = nativeToUriPath(extractParameter(argument));
34 return path.endsWith("/") ? path : "$path/";
35 }
36
32 void parseCommandLine(List<OptionHandler> handlers, List<String> argv) { 37 void parseCommandLine(List<OptionHandler> handlers, List<String> argv) {
33 // TODO(ahe): Use ../../args/args.dart for parsing options instead. 38 // TODO(ahe): Use ../../args/args.dart for parsing options instead.
34 var patterns = <String>[]; 39 var patterns = <String>[];
35 for (OptionHandler handler in handlers) { 40 for (OptionHandler handler in handlers) {
36 patterns.add(handler.pattern); 41 patterns.add(handler.pattern);
37 } 42 }
38 var pattern = new RegExp('^(${Strings.join(patterns, ")\$|(")})\$'); 43 var pattern = new RegExp('^(${Strings.join(patterns, ")\$|(")})\$');
39 OUTER: for (String argument in argv) { 44 OUTER: for (String argument in argv) {
40 Match match = pattern.firstMatch(argument); 45 Match match = pattern.firstMatch(argument);
41 assert(match.groupCount() == handlers.length); 46 assert(match.groupCount() == handlers.length);
42 for (int i = 0; i < handlers.length; i++) { 47 for (int i = 0; i < handlers.length; i++) {
43 if (match[i + 1] !== null) { 48 if (match[i + 1] !== null) {
44 handlers[i].handle(argument); 49 handlers[i].handle(argument);
45 continue OUTER; 50 continue OUTER;
46 } 51 }
47 } 52 }
48 throw 'Internal error: "$argument" did not match'; 53 throw 'Internal error: "$argument" did not match';
49 } 54 }
50 } 55 }
51 56
52 void compile(List<String> argv) { 57 void compile(List<String> argv) {
53 Uri cwd = getCurrentDirectory(); 58 Uri cwd = getCurrentDirectory();
54 bool throwOnError = false; 59 bool throwOnError = false;
55 bool showWarnings = true; 60 bool showWarnings = true;
56 bool verbose = false; 61 bool verbose = false;
57 Uri libraryRoot = cwd; 62 Uri libraryRoot = cwd;
58 Uri out = cwd.resolve('out.js'); 63 Uri out = cwd.resolve('out.js');
64 Uri packageRoot = null;
59 List<String> options = new List<String>(); 65 List<String> options = new List<String>();
60 bool explicitOut = false; 66 bool explicitOut = false;
61 bool wantHelp = false; 67 bool wantHelp = false;
62 68
63 passThrough(String argument) => options.add(argument); 69 passThrough(String argument) => options.add(argument);
64 70
71 setLibraryRoot(String argument) {
72 libraryRoot = cwd.resolve(extractPath(argument));
73 }
74
75 setPackageRoot(String argument) {
76 packageRoot = cwd.resolve(extractPath(argument));
77 }
78
79 setOutput(String argument) {
80 explicitOut = true;
81 out = cwd.resolve(nativeToUriPath(extractParameter(argument)));
82 }
83
65 List<String> arguments = <String>[]; 84 List<String> arguments = <String>[];
66 List<OptionHandler> handlers = <OptionHandler>[ 85 List<OptionHandler> handlers = <OptionHandler>[
67 new OptionHandler('--throw-on-error', (_) => throwOnError = true), 86 new OptionHandler('--throw-on-error', (_) => throwOnError = true),
68 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), 87 new OptionHandler('--suppress-warnings', (_) => showWarnings = false),
69 new OptionHandler('--output-type=dart|--output-type=js', passThrough), 88 new OptionHandler('--output-type=dart|--output-type=js', passThrough),
70 new OptionHandler('--verbose|-v', (_) => verbose = true), 89 new OptionHandler('--verbose|-v', (_) => verbose = true),
71 new OptionHandler('--library-root=.+', (String argument) { 90 new OptionHandler('--library-root=.+', setLibraryRoot),
72 String path = nativeToUriPath(extractParameter(argument)); 91 new OptionHandler('--out=.+|-o.+', setOutput),
73 if (!path.endsWith("/")) path = "$path/";
74 libraryRoot = cwd.resolve(path);
75 }),
76 new OptionHandler('--out=.+|-o.+', (String argument) {
77 explicitOut = true;
78 out = cwd.resolve(nativeToUriPath(extractParameter(argument)));
79 }),
80 new OptionHandler('--allow-mock-compilation', passThrough), 92 new OptionHandler('--allow-mock-compilation', passThrough),
81 new OptionHandler('--no-colors', (_) => colors.enabled = false), 93 new OptionHandler('--no-colors', (_) => colors.enabled = false),
82 new OptionHandler('--enable[_-]checked[_-]mode|--checked|-c', 94 new OptionHandler('--enable[_-]checked[_-]mode|--checked|-c',
83 (_) => passThrough('--enable-checked-mode')), 95 (_) => passThrough('--enable-checked-mode')),
84 new OptionHandler(@'--help|-h|/\?|/h', (_) => wantHelp = true), 96 new OptionHandler(@'--help|-h|/\?|/h', (_) => wantHelp = true),
97 new OptionHandler(@'--package-root=.+|-p.+', setPackageRoot),
85 // The following two options must come last. 98 // The following two options must come last.
86 new OptionHandler('-.*', (String argument) { 99 new OptionHandler('-.*', (String argument) {
87 helpAndFail('Error: Unknown option "$argument".'); 100 helpAndFail('Error: Unknown option "$argument".');
88 }), 101 }),
89 new OptionHandler('.*', (String argument) { 102 new OptionHandler('.*', (String argument) {
90 arguments.add(nativeToUriPath(argument)); 103 arguments.add(nativeToUriPath(argument));
91 }) 104 })
92 ]; 105 ];
93 106
94 parseCommandLine(handlers, argv); 107 parseCommandLine(handlers, argv);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 SourceFile file = sourceFiles[uri.toString()]; 156 SourceFile file = sourceFiles[uri.toString()];
144 print(file.getLocationMessage(message, begin, end, true)); 157 print(file.getLocationMessage(message, begin, end, true));
145 } 158 }
146 if (fatal && throwOnError) { 159 if (fatal && throwOnError) {
147 isAborting = true; 160 isAborting = true;
148 throw new AbortLeg(message); 161 throw new AbortLeg(message);
149 } 162 }
150 } 163 }
151 164
152 Uri uri = cwd.resolve(arguments[0]); 165 Uri uri = cwd.resolve(arguments[0]);
166 if (packageRoot === null) {
167 packageRoot = uri.resolve('./packages/');
168 }
169
153 info('compiling $uri'); 170 info('compiling $uri');
171 info('package root is $packageRoot');
154 172
155 // TODO(ahe): We expect the future to be complete and call value 173 // TODO(ahe): We expect the future to be complete and call value
156 // directly. In effect, we don't support truly asynchronous API. 174 // directly. In effect, we don't support truly asynchronous API.
157 String code = api.compile(uri, libraryRoot, provider, handler, options).value; 175 String code = api.compile(uri, libraryRoot, packageRoot, provider, handler,
176 options).value;
158 if (code === null) { 177 if (code === null) {
159 fail('Error: Compilation failed.'); 178 fail('Error: Compilation failed.');
160 } 179 }
161 writeString(out, code); 180 writeString(out, code);
162 int jsBytesWritten = code.length; 181 int jsBytesWritten = code.length;
163 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' 182 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS '
164 'in ${relativize(cwd, out)}'); 183 'in ${relativize(cwd, out)}');
165 if (!explicitOut) { 184 if (!explicitOut) {
166 String input = uriPathToNative(arguments[0]); 185 String input = uriPathToNative(arguments[0]);
167 String output = relativize(cwd, out); 186 String output = relativize(cwd, out);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 251
233 -c, --enable-checked-mode, --checked 252 -c, --enable-checked-mode, --checked
234 Insert runtime type checks and enable assertions (checked mode). 253 Insert runtime type checks and enable assertions (checked mode).
235 254
236 -h, /h, /?, --help 255 -h, /h, /?, --help
237 Display this message (add -v for information about all options). 256 Display this message (add -v for information about all options).
238 257
239 -v, --verbose 258 -v, --verbose
240 Display verbose information. 259 Display verbose information.
241 260
242 --output-type=<value> 261 -p<path>, --package-root=<path>
243 Output either JavaScript code (if value is 'js') or Dart code (if value is ' dart'). 262 Where to find packages, that is, "package:..." imports.
244 Default value is 'js'. 263
264 --output-type=dart
ahe 2012/05/22 11:42:49 @anton: I decided to simplify this. We only recogn
265 Output Dart code instead of JavaScript.
245 266
246 --suppress-warnings 267 --suppress-warnings
247 Do not display any warnings. 268 Do not display any warnings.
248 269
249 --no-colors 270 --no-colors
250 Do not add colors to diagnostic messages. 271 Do not add colors to diagnostic messages.
251 272
252 The following options are only used for compiler development and may 273 The following options are only used for compiler development and may
253 be removed in a future version: 274 be removed in a future version:
254 --throw-on-error 275 --throw-on-error
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 } catch (var ignored) { 307 } catch (var ignored) {
287 print('Internal error: error while printing exception'); 308 print('Internal error: error while printing exception');
288 } 309 }
289 try { 310 try {
290 print(trace); 311 print(trace);
291 } finally { 312 } finally {
292 exit(253); // 253 is recognized as a crash by our test scripts. 313 exit(253); // 253 is recognized as a crash by our test scripts.
293 } 314 }
294 } 315 }
295 } 316 }
OLDNEW
« no previous file with comments | « dart/lib/compiler/implementation/apiimpl.dart ('k') | dart/tests/utils/dummy_compiler_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698