Chromium Code Reviews| Index: sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
| diff --git a/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart b/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
| index c323fcf9d1b1d60f4e1ecaba59b454a757c2e426..d6a3cc77337b0bdf817d32b1a7a4be035fd5b41a 100644 |
| --- a/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
| +++ b/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
| @@ -25,6 +25,10 @@ class DartBackend extends Backend { |
| final bool outputAst = false; |
| final Map<ClassNode, List<Node>> memberNodes; |
| + // Should the output go to one file per input library, or everything in one |
| + // file. |
|
Johnni Winther
2014/08/15 08:07:31
Paraphrase to something like "If `true`, libraries
sigurdm
2014/08/15 13:31:42
Done.
|
| + final bool multiFile; |
| + |
| PlaceholderRenamer placeholderRenamer; |
| // TODO(zarah) Maybe change this to a command-line option. |
| @@ -102,7 +106,7 @@ class DartBackend extends Backend { |
| return true; |
| } |
| - DartBackend(Compiler compiler, List<String> strips) |
| + DartBackend(Compiler compiler, List<String> strips, {this.multiFile}) |
| : tasks = <CompilerTask>[], |
| memberNodes = new Map<ClassNode, List<Node>>(), |
| forceStripTypes = strips.indexOf('types') != -1, |
| @@ -264,6 +268,9 @@ class DartBackend extends Backend { |
| } |
| } |
| + List<LibraryElement> userLibraries = |
| + compiler.libraryLoader.libraries.where(isUserLibrary).toList(); |
| + |
| Set<Element> topLevelElements = new Set<Element>(); |
| Map<ClassElement, Set<Element>> classMembers = |
| new Map<ClassElement, Set<Element>>(); |
| @@ -451,17 +458,65 @@ class DartBackend extends Backend { |
| topLevelNodes, collector); |
| } |
| - final unparser = new EmitterUnparser(placeholderRenamer.renames, |
| - stripTypes: forceStripTypes, |
| - minify: compiler.enableMinification); |
| - for(LibraryElement library in placeholderRenamer.platformImports) { |
| - if (library.isPlatformLibrary && !library.isInternalLibrary) { |
| - unparser.unparseImportTag(library.canonicalUri.toString()); |
| + Map<LibraryElement, String> outputUris = new Map<LibraryElement, String>(); |
| + Map<LibraryElement, EmitterUnparser> unparsers = |
| + new Map<LibraryElement, EmitterUnparser>(); |
| + |
| + // The single unparser used if we collect all the output in one file |
| + EmitterUnparser mainUnparser = multiFile |
| + ? null |
| + : new EmitterUnparser(placeholderRenamer.renames, |
| + stripTypes: forceStripTypes, |
| + minify: compiler.enableMinification); |
| + |
| + if (multiFile) { |
| + Set<String> usedLibraryNames = new Set<String>(); |
| + for (LibraryElement library in userLibraries) { |
| + List<String> names = library.canonicalUri.pathSegments.last.split("."); |
| + if (names.last == "dart") { |
| + names = names.sublist(0, names.length - 1); |
|
floitsch
2014/08/15 11:59:36
names is only used when the library is not the mai
sigurdm
2014/08/15 13:31:42
Done.
|
| + } |
| + String mainName = compiler.outputUri.path.split('/').last; |
|
jgruber1
2014/08/15 06:20:53
Does this work on windows?
Johnni Winther
2014/08/15 08:07:31
Use [Uri.pathSegments] instead of `path.split('/')
floitsch
2014/08/15 11:59:36
You should be able to compute the mainName/mmainBa
sigurdm
2014/08/15 13:31:42
I think so - Uri's should normalize path segment s
sigurdm
2014/08/15 13:31:42
Thanks
sigurdm
2014/08/15 13:31:42
Done.
|
| + String mainBaseName = mainName.endsWith(".dart") |
| + ? mainName.substring(0, mainName.length - 5) |
| + : mainName; |
| + String uri = library == compiler.mainApp |
| + ? mainBaseName |
| + : "$mainBaseName.${makeUnique(names.join("."), usedLibraryNames)}"; |
|
floitsch
2014/08/15 11:59:36
usedLibraryNames does not contain the "mainBaseNam
sigurdm
2014/08/15 13:31:42
But this is only a suffix.
So we generate
mainBase
|
| + outputUris[library] = uri; |
| + unparsers[library] = new EmitterUnparser(placeholderRenamer.renames, |
| + stripTypes: forceStripTypes, minify: compiler.enableMinification); |
| + } |
| + |
| + for(LibraryElement outputLibrary in userLibraries) { |
|
jgruber1
2014/08/15 06:20:53
Can we merge this into the previous for loop?
Johnni Winther
2014/08/15 08:07:31
No. This loop requires [outputUris] to be fully co
sigurdm
2014/08/15 13:31:42
Acknowledged.
sigurdm
2014/08/15 13:31:42
We want to set up all the ouputUris before using t
|
| + EmitterUnparser unparser = unparsers[outputLibrary]; |
| + for (LibraryTag tag in outputLibrary.tags) { |
| + if (tag is! LibraryDependency) continue; |
| + LibraryDependency dependency = tag; |
| + LibraryElement libraryElement = |
| + outputLibrary.getLibraryFromTag(dependency); |
| + String uri = outputUris.containsKey(libraryElement) |
| + ? outputUris[libraryElement] + ".dart" |
|
Johnni Winther
2014/08/15 08:07:31
Use '${outputUri[LibraryElement]}.dart' instead of
sigurdm
2014/08/15 13:31:42
Done.
|
| + : libraryElement.canonicalUri.toString(); |
| + if (dependency is Import) { |
| + unparser.unparseImportTag('$uri'); |
|
jgruber1
2014/08/15 06:20:53
unparseImportTag(uri)?
sigurdm
2014/08/15 13:31:42
Done.
|
| + } else { |
| + unparser.unparseExportTag('$uri'); |
| + } |
| + } |
| + } |
| + } else { |
| + for(LibraryElement library in placeholderRenamer.platformImports) { |
| + if (library.isPlatformLibrary && !library.isInternalLibrary) { |
| + mainUnparser.unparseImportTag(library.canonicalUri.toString()); |
| + } |
| } |
| } |
| + |
| for (int i = 0; i < sortedTopLevels.length; i++) { |
| Element element = sortedTopLevels[i]; |
| Node node = topLevelNodes[i]; |
| + Unparser unparser = multiFile ? unparsers[element.library] : mainUnparser; |
| if (node is ClassNode) { |
| // TODO(smok): Filter out default constructors here. |
| unparser.unparseClassWithBody(node, memberNodes[node]); |
| @@ -471,13 +526,25 @@ class DartBackend extends Backend { |
| unparser.newline(); |
| } |
| - compiler.assembledCode = unparser.result; |
| - compiler.outputProvider("", "dart") |
| - ..add(compiler.assembledCode) |
| - ..close(); |
| - // Output verbose info about size ratio of resulting bundle to all |
| - // referenced non-platform sources. |
| - logResultBundleSizeInfo(topLevelElements); |
| + if (multiFile) { |
| + for(LibraryElement outputLibrary in userLibraries) { |
| + compiler.outputProvider(outputUris[outputLibrary], "dart") |
| + ..add(unparsers[outputLibrary].result) |
|
Johnni Winther
2014/08/15 08:07:31
Add a TODO to enable direct output, i.e. avoid cac
sigurdm
2014/08/15 13:31:42
Done.
|
| + ..close(); |
| + } |
| + // TODO(sigurdm): What to do here? Probably we should get rid of |
| + // compiler.assembledCode |
| + compiler.assembledCode = ""; |
| + } else { |
| + compiler.assembledCode = mainUnparser.result; |
| + compiler.outputProvider("", "dart") |
| + ..add(compiler.assembledCode) |
| + ..close(); |
| + |
| + // Output verbose info about size ratio of resulting bundle to all |
| + // referenced non-platform sources. |
| + logResultBundleSizeInfo(topLevelElements); |
| + } |
| } |
| void logResultBundleSizeInfo(Set<Element> topLevelElements) { |