| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Generate a snapshot file after loading all the scripts specified on the | 5 // Generate a snapshot file after loading all the scripts specified on the |
| 6 // command line. | 6 // command line. |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 | 11 |
| 12 #include <cstdarg> | 12 #include <cstdarg> |
| 13 | 13 |
| 14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
| 15 #include "bin/dartutils.h" | 15 #include "bin/dartutils.h" |
| 16 #include "bin/eventhandler.h" | 16 #include "bin/eventhandler.h" |
| 17 #include "bin/file.h" | 17 #include "bin/file.h" |
| 18 #include "bin/loader.h" | 18 #include "bin/loader.h" |
| 19 #include "bin/log.h" | 19 #include "bin/log.h" |
| 20 #include "bin/thread.h" | 20 #include "bin/thread.h" |
| 21 #include "bin/utils.h" | 21 #include "bin/utils.h" |
| 22 #include "bin/vmservice_impl.h" | 22 #include "bin/vmservice_impl.h" |
| 23 | 23 |
| 24 #include "include/dart_api.h" | 24 #include "include/dart_api.h" |
| 25 #include "include/dart_tools_api.h" | 25 #include "include/dart_tools_api.h" |
| 26 | 26 |
| 27 #include "platform/hashmap.h" | 27 #include "platform/hashmap.h" |
| 28 #include "platform/globals.h" | 28 #include "platform/globals.h" |
| 29 #include "platform/growable_array.h" |
| 29 | 30 |
| 30 namespace dart { | 31 namespace dart { |
| 31 namespace bin { | 32 namespace bin { |
| 32 | 33 |
| 33 // Exit code indicating an API error. | 34 // Exit code indicating an API error. |
| 34 static const int kApiErrorExitCode = 253; | 35 static const int kApiErrorExitCode = 253; |
| 35 // Exit code indicating a compilation error. | 36 // Exit code indicating a compilation error. |
| 36 static const int kCompilationErrorExitCode = 254; | 37 static const int kCompilationErrorExitCode = 254; |
| 37 // Exit code indicating an unhandled error that is not a compilation error. | 38 // Exit code indicating an unhandled error that is not a compilation error. |
| 38 static const int kErrorExitCode = 255; | 39 static const int kErrorExitCode = 255; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 59 | 60 |
| 60 | 61 |
| 61 // The core snapshot to use when creating isolates. Normally NULL, but loaded | 62 // The core snapshot to use when creating isolates. Normally NULL, but loaded |
| 62 // from a file when creating script snapshots. | 63 // from a file when creating script snapshots. |
| 63 const uint8_t* isolate_snapshot_data = NULL; | 64 const uint8_t* isolate_snapshot_data = NULL; |
| 64 | 65 |
| 65 | 66 |
| 66 // Global state that indicates whether a snapshot is to be created and | 67 // Global state that indicates whether a snapshot is to be created and |
| 67 // if so which file to write the snapshot into. | 68 // if so which file to write the snapshot into. |
| 68 enum SnapshotKind { | 69 enum SnapshotKind { |
| 70 kNone, |
| 69 kCore, | 71 kCore, |
| 70 kScript, | 72 kScript, |
| 71 kAppAOTBlobs, | 73 kAppAOTBlobs, |
| 72 kAppAOTAssembly, | 74 kAppAOTAssembly, |
| 73 }; | 75 }; |
| 74 static SnapshotKind snapshot_kind = kCore; | 76 static SnapshotKind snapshot_kind = kCore; |
| 75 static const char* vm_snapshot_data_filename = NULL; | 77 static const char* vm_snapshot_data_filename = NULL; |
| 76 static const char* vm_snapshot_instructions_filename = NULL; | 78 static const char* vm_snapshot_instructions_filename = NULL; |
| 77 static const char* isolate_snapshot_data_filename = NULL; | 79 static const char* isolate_snapshot_data_filename = NULL; |
| 78 static const char* isolate_snapshot_instructions_filename = NULL; | 80 static const char* isolate_snapshot_instructions_filename = NULL; |
| 79 static const char* assembly_filename = NULL; | 81 static const char* assembly_filename = NULL; |
| 80 static const char* script_snapshot_filename = NULL; | 82 static const char* script_snapshot_filename = NULL; |
| 83 static const char* dependencies_filename = NULL; |
| 81 | 84 |
| 82 | 85 |
| 83 // Value of the --package-root flag. | 86 // Value of the --package-root flag. |
| 84 // (This pointer points into an argv buffer and does not need to be | 87 // (This pointer points into an argv buffer and does not need to be |
| 85 // free'd.) | 88 // free'd.) |
| 86 static const char* commandline_package_root = NULL; | 89 static const char* commandline_package_root = NULL; |
| 87 | 90 |
| 88 // Value of the --packages flag. | 91 // Value of the --packages flag. |
| 89 // (This pointer points into an argv buffer and does not need to be | 92 // (This pointer points into an argv buffer and does not need to be |
| 90 // free'd.) | 93 // free'd.) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 203 } |
| 201 return NULL; | 204 return NULL; |
| 202 } | 205 } |
| 203 | 206 |
| 204 | 207 |
| 205 static bool ProcessSnapshotKindOption(const char* option) { | 208 static bool ProcessSnapshotKindOption(const char* option) { |
| 206 const char* kind = ProcessOption(option, "--snapshot_kind="); | 209 const char* kind = ProcessOption(option, "--snapshot_kind="); |
| 207 if (kind == NULL) { | 210 if (kind == NULL) { |
| 208 return false; | 211 return false; |
| 209 } | 212 } |
| 210 if (strcmp(kind, "core") == 0) { | 213 if (strcmp(kind, "none") == 0) { |
| 214 snapshot_kind = kNone; |
| 215 return true; |
| 216 } else if (strcmp(kind, "core") == 0) { |
| 211 snapshot_kind = kCore; | 217 snapshot_kind = kCore; |
| 212 return true; | 218 return true; |
| 213 } else if (strcmp(kind, "script") == 0) { | 219 } else if (strcmp(kind, "script") == 0) { |
| 214 snapshot_kind = kScript; | 220 snapshot_kind = kScript; |
| 215 return true; | 221 return true; |
| 216 } else if (strcmp(kind, "app-aot-blobs") == 0) { | 222 } else if (strcmp(kind, "app-aot-blobs") == 0) { |
| 217 snapshot_kind = kAppAOTBlobs; | 223 snapshot_kind = kAppAOTBlobs; |
| 218 return true; | 224 return true; |
| 219 } else if (strcmp(kind, "app-aot-assembly") == 0) { | 225 } else if (strcmp(kind, "app-aot-assembly") == 0) { |
| 220 snapshot_kind = kAppAOTAssembly; | 226 snapshot_kind = kAppAOTAssembly; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 static bool ProcessScriptSnapshotOption(const char* option) { | 287 static bool ProcessScriptSnapshotOption(const char* option) { |
| 282 const char* name = ProcessOption(option, "--script_snapshot="); | 288 const char* name = ProcessOption(option, "--script_snapshot="); |
| 283 if (name != NULL) { | 289 if (name != NULL) { |
| 284 script_snapshot_filename = name; | 290 script_snapshot_filename = name; |
| 285 return true; | 291 return true; |
| 286 } | 292 } |
| 287 return false; | 293 return false; |
| 288 } | 294 } |
| 289 | 295 |
| 290 | 296 |
| 297 static bool ProcessDependenciesOption(const char* option) { |
| 298 const char* name = ProcessOption(option, "--dependencies="); |
| 299 if (name != NULL) { |
| 300 dependencies_filename = name; |
| 301 return true; |
| 302 } |
| 303 return false; |
| 304 } |
| 305 |
| 306 |
| 291 static bool ProcessEmbedderEntryPointsManifestOption(const char* option) { | 307 static bool ProcessEmbedderEntryPointsManifestOption(const char* option) { |
| 292 const char* name = ProcessOption(option, "--embedder_entry_points_manifest="); | 308 const char* name = ProcessOption(option, "--embedder_entry_points_manifest="); |
| 293 if (name != NULL) { | 309 if (name != NULL) { |
| 294 entry_points_files->AddArgument(name); | 310 entry_points_files->AddArgument(name); |
| 295 return true; | 311 return true; |
| 296 } | 312 } |
| 297 return false; | 313 return false; |
| 298 } | 314 } |
| 299 | 315 |
| 300 | 316 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 | 369 |
| 354 // Parse out the vm options. | 370 // Parse out the vm options. |
| 355 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { | 371 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { |
| 356 if (ProcessSnapshotKindOption(argv[i]) || | 372 if (ProcessSnapshotKindOption(argv[i]) || |
| 357 ProcessVmSnapshotDataOption(argv[i]) || | 373 ProcessVmSnapshotDataOption(argv[i]) || |
| 358 ProcessVmSnapshotInstructionsOption(argv[i]) || | 374 ProcessVmSnapshotInstructionsOption(argv[i]) || |
| 359 ProcessIsolateSnapshotDataOption(argv[i]) || | 375 ProcessIsolateSnapshotDataOption(argv[i]) || |
| 360 ProcessIsolateSnapshotInstructionsOption(argv[i]) || | 376 ProcessIsolateSnapshotInstructionsOption(argv[i]) || |
| 361 ProcessAssemblyOption(argv[i]) || | 377 ProcessAssemblyOption(argv[i]) || |
| 362 ProcessScriptSnapshotOption(argv[i]) || | 378 ProcessScriptSnapshotOption(argv[i]) || |
| 379 ProcessDependenciesOption(argv[i]) || |
| 363 ProcessEmbedderEntryPointsManifestOption(argv[i]) || | 380 ProcessEmbedderEntryPointsManifestOption(argv[i]) || |
| 364 ProcessURLmappingOption(argv[i]) || ProcessPackageRootOption(argv[i]) || | 381 ProcessURLmappingOption(argv[i]) || ProcessPackageRootOption(argv[i]) || |
| 365 ProcessPackagesOption(argv[i]) || ProcessEnvironmentOption(argv[i])) { | 382 ProcessPackagesOption(argv[i]) || ProcessEnvironmentOption(argv[i])) { |
| 366 i += 1; | 383 i += 1; |
| 367 continue; | 384 continue; |
| 368 } | 385 } |
| 369 vm_options->AddArgument(argv[i]); | 386 vm_options->AddArgument(argv[i]); |
| 370 i += 1; | 387 i += 1; |
| 371 } | 388 } |
| 372 | 389 |
| 373 // Get the script name. | 390 // Get the script name. |
| 374 if (i < argc) { | 391 if (i < argc) { |
| 375 *script_name = argv[i]; | 392 *script_name = argv[i]; |
| 376 i += 1; | 393 i += 1; |
| 377 } else { | 394 } else { |
| 378 *script_name = NULL; | 395 *script_name = NULL; |
| 379 } | 396 } |
| 380 | 397 |
| 381 // Verify consistency of arguments. | 398 // Verify consistency of arguments. |
| 382 if ((commandline_package_root != NULL) && | 399 if ((commandline_package_root != NULL) && |
| 383 (commandline_packages_file != NULL)) { | 400 (commandline_packages_file != NULL)) { |
| 384 Log::PrintErr( | 401 Log::PrintErr( |
| 385 "Specifying both a packages directory and a packages " | 402 "Specifying both a packages directory and a packages " |
| 386 "file is invalid.\n\n"); | 403 "file is invalid.\n\n"); |
| 387 return -1; | 404 return -1; |
| 388 } | 405 } |
| 389 | 406 |
| 390 switch (snapshot_kind) { | 407 switch (snapshot_kind) { |
| 408 case kNone: { |
| 409 if (*script_name == NULL) { |
| 410 Log::PrintErr( |
| 411 "Building without snapshot generation requires a Dart " |
| 412 "script.\n\n"); |
| 413 return -1; |
| 414 } |
| 415 break; |
| 416 } |
| 391 case kCore: { | 417 case kCore: { |
| 392 if ((vm_snapshot_data_filename == NULL) || | 418 if ((vm_snapshot_data_filename == NULL) || |
| 393 (isolate_snapshot_data_filename == NULL)) { | 419 (isolate_snapshot_data_filename == NULL)) { |
| 394 Log::PrintErr( | 420 Log::PrintErr( |
| 395 "Building a core snapshot requires specifying output files for " | 421 "Building a core snapshot requires specifying output files for " |
| 396 "--vm_snapshot_data and --isolate_snapshot_data.\n\n"); | 422 "--vm_snapshot_data and --isolate_snapshot_data.\n\n"); |
| 397 return -1; | 423 return -1; |
| 398 } | 424 } |
| 399 break; | 425 break; |
| 400 } | 426 } |
| 401 case kScript: { | 427 case kScript: { |
| 402 if ((vm_snapshot_data_filename == NULL) || | 428 if ((vm_snapshot_data_filename == NULL) || |
| 403 (isolate_snapshot_data_filename == NULL) || | 429 (isolate_snapshot_data_filename == NULL) || |
| 404 (script_snapshot_filename == NULL) || (script_name == NULL)) { | 430 (script_snapshot_filename == NULL) || (*script_name == NULL)) { |
| 405 Log::PrintErr( | 431 Log::PrintErr( |
| 406 "Building a script snapshot requires specifying input files for " | 432 "Building a script snapshot requires specifying input files for " |
| 407 "--vm_snapshot_data and --isolate_snapshot_data, an output file " | 433 "--vm_snapshot_data and --isolate_snapshot_data, an output file " |
| 408 "for --script-snapshot, and a Dart script.\n\n"); | 434 "for --script-snapshot, and a Dart script.\n\n"); |
| 409 return -1; | 435 return -1; |
| 410 } | 436 } |
| 411 break; | 437 break; |
| 412 } | 438 } |
| 413 case kAppAOTBlobs: { | 439 case kAppAOTBlobs: { |
| 414 if ((vm_snapshot_data_filename == NULL) || | 440 if ((vm_snapshot_data_filename == NULL) || |
| 415 (vm_snapshot_instructions_filename == NULL) || | 441 (vm_snapshot_instructions_filename == NULL) || |
| 416 (isolate_snapshot_data_filename == NULL) || | 442 (isolate_snapshot_data_filename == NULL) || |
| 417 (isolate_snapshot_instructions_filename == NULL) || | 443 (isolate_snapshot_instructions_filename == NULL) || |
| 418 (script_name == NULL)) { | 444 (*script_name == NULL)) { |
| 419 Log::PrintErr( | 445 Log::PrintErr( |
| 420 "Building an AOT snapshot as blobs requires specifying output " | 446 "Building an AOT snapshot as blobs requires specifying output " |
| 421 "files for --vm_snapshot_data, --vm_snapshot_instructions, " | 447 "files for --vm_snapshot_data, --vm_snapshot_instructions, " |
| 422 "--isolate_snapshot_data and --isolate_snapshot_instructions and a " | 448 "--isolate_snapshot_data and --isolate_snapshot_instructions and a " |
| 423 "Dart script.\n\n"); | 449 "Dart script.\n\n"); |
| 424 return -1; | 450 return -1; |
| 425 } | 451 } |
| 426 break; | 452 break; |
| 427 } | 453 } |
| 428 case kAppAOTAssembly: { | 454 case kAppAOTAssembly: { |
| 429 if ((assembly_filename == NULL) || (script_name == NULL)) { | 455 if ((assembly_filename == NULL) || (*script_name == NULL)) { |
| 430 Log::PrintErr( | 456 Log::PrintErr( |
| 431 "Building an AOT snapshot as assembly requires specifying " | 457 "Building an AOT snapshot as assembly requires specifying " |
| 432 "an output file for --assembly and a Dart script.\n\n"); | 458 "an output file for --assembly and a Dart script.\n\n"); |
| 433 return -1; | 459 return -1; |
| 434 } | 460 } |
| 435 break; | 461 break; |
| 436 } | 462 } |
| 437 } | 463 } |
| 438 | 464 |
| 439 if (IsSnapshottingForPrecompilation() && (entry_points_files->count() == 0)) { | 465 if (IsSnapshottingForPrecompilation() && (entry_points_files->count() == 0)) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 private: | 520 private: |
| 495 Dart_Isolate snapshotted_isolate_; | 521 Dart_Isolate snapshotted_isolate_; |
| 496 | 522 |
| 497 DISALLOW_COPY_AND_ASSIGN(UriResolverIsolateScope); | 523 DISALLOW_COPY_AND_ASSIGN(UriResolverIsolateScope); |
| 498 }; | 524 }; |
| 499 | 525 |
| 500 | 526 |
| 501 Dart_Isolate UriResolverIsolateScope::isolate = NULL; | 527 Dart_Isolate UriResolverIsolateScope::isolate = NULL; |
| 502 | 528 |
| 503 | 529 |
| 530 static char* ResolveAsFilePath(const char* uri_string) { |
| 531 UriResolverIsolateScope scope; |
| 532 uint8_t* scoped_file_path = NULL; |
| 533 intptr_t scoped_file_path_length = -1; |
| 534 Dart_Handle uri = Dart_NewStringFromCString(uri_string); |
| 535 ASSERT(!Dart_IsError(uri)); |
| 536 Dart_Handle result = Loader::ResolveAsFilePath(uri, &scoped_file_path, |
| 537 &scoped_file_path_length); |
| 538 if (Dart_IsError(result)) { |
| 539 Log::Print("Error resolving dependency: %s\n", Dart_GetError(result)); |
| 540 exit(kErrorExitCode); |
| 541 } |
| 542 return StringUtils::StrNDup(reinterpret_cast<const char*>(scoped_file_path), |
| 543 scoped_file_path_length); |
| 544 } |
| 545 |
| 546 |
| 547 static void AddDependency(const char* uri_string) { |
| 548 IsolateData* isolate_data = |
| 549 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
| 550 MallocGrowableArray<char*>* dependencies = isolate_data->dependencies(); |
| 551 if (dependencies != NULL) { |
| 552 dependencies->Add(ResolveAsFilePath(uri_string)); |
| 553 } |
| 554 } |
| 555 |
| 556 |
| 504 static Dart_Handle LoadUrlContents(const char* uri_string) { | 557 static Dart_Handle LoadUrlContents(const char* uri_string) { |
| 505 bool failed = false; | 558 bool failed = false; |
| 506 char* result_string = NULL; | 559 char* error_string = NULL; |
| 507 uint8_t* payload = NULL; | 560 uint8_t* payload = NULL; |
| 508 intptr_t payload_length = 0; | 561 intptr_t payload_length = 0; |
| 509 // Switch to the UriResolver Isolate and load the script. | 562 // Switch to the UriResolver Isolate and load the script. |
| 510 { | 563 { |
| 511 UriResolverIsolateScope scope; | 564 UriResolverIsolateScope scope; |
| 512 | 565 |
| 513 Dart_Handle resolved_uri = Dart_NewStringFromCString(uri_string); | 566 Dart_Handle resolved_uri = Dart_NewStringFromCString(uri_string); |
| 514 Dart_Handle result = | 567 Dart_Handle result = |
| 515 Loader::LoadUrlContents(resolved_uri, &payload, &payload_length); | 568 Loader::LoadUrlContents(resolved_uri, &payload, &payload_length); |
| 516 if (Dart_IsError(result)) { | 569 if (Dart_IsError(result)) { |
| 517 failed = true; | 570 failed = true; |
| 518 result_string = strdup(Dart_GetError(result)); | 571 error_string = strdup(Dart_GetError(result)); |
| 519 } | 572 } |
| 520 } | 573 } |
| 574 AddDependency(uri_string); |
| 521 // Switch back to the isolate from which we generate the snapshot and | 575 // Switch back to the isolate from which we generate the snapshot and |
| 522 // create the source string for the specified uri. | 576 // create the source string for the specified uri. |
| 523 Dart_Handle result; | 577 Dart_Handle result; |
| 524 if (!failed) { | 578 if (!failed) { |
| 525 result = Dart_NewStringFromUTF8(payload, payload_length); | 579 result = Dart_NewStringFromUTF8(payload, payload_length); |
| 526 free(payload); | 580 free(payload); |
| 527 } else { | 581 } else { |
| 528 result = Dart_NewApiError(result_string); | 582 result = Dart_NewApiError(error_string); |
| 529 free(result_string); | 583 free(error_string); |
| 530 } | 584 } |
| 531 return result; | 585 return result; |
| 532 } | 586 } |
| 533 | 587 |
| 534 | 588 |
| 535 static Dart_Handle ResolveUriInWorkingDirectory(const char* script_uri) { | 589 static Dart_Handle ResolveUriInWorkingDirectory(const char* script_uri) { |
| 536 bool failed = false; | 590 bool failed = false; |
| 537 char* result_string = NULL; | 591 char* result_string = NULL; |
| 538 | 592 |
| 539 { | 593 { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 if (DartUtils::IsDartBuiltinLibURL(url)) { | 638 if (DartUtils::IsDartBuiltinLibURL(url)) { |
| 585 return Builtin::kBuiltinLibrary; | 639 return Builtin::kBuiltinLibrary; |
| 586 } | 640 } |
| 587 if (DartUtils::IsDartIOLibURL(url)) { | 641 if (DartUtils::IsDartIOLibURL(url)) { |
| 588 return Builtin::kIOLibrary; | 642 return Builtin::kIOLibrary; |
| 589 } | 643 } |
| 590 return Builtin::kInvalidLibrary; | 644 return Builtin::kInvalidLibrary; |
| 591 } | 645 } |
| 592 | 646 |
| 593 | 647 |
| 648 static void CreateAndWriteDependenciesFile() { |
| 649 IsolateData* isolate_data = |
| 650 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
| 651 MallocGrowableArray<char*>* dependencies = isolate_data->dependencies(); |
| 652 if (dependencies == NULL) { |
| 653 return; |
| 654 } |
| 655 |
| 656 ASSERT(dependencies_filename != NULL); |
| 657 File* file = File::Open(dependencies_filename, File::kWriteTruncate); |
| 658 if (file == NULL) { |
| 659 Log::PrintErr("Error: Unable to open dependencies file: %s\n\n", |
| 660 dependencies_filename); |
| 661 exit(kErrorExitCode); |
| 662 } |
| 663 bool success = true; |
| 664 |
| 665 // Targets: |
| 666 if (vm_snapshot_data_filename != NULL) { |
| 667 success &= |
| 668 file->Print("%s ", File::GetCanonicalPath(vm_snapshot_data_filename)); |
| 669 } |
| 670 if (vm_snapshot_instructions_filename != NULL) { |
| 671 success &= file->Print( |
| 672 "%s ", File::GetCanonicalPath(vm_snapshot_instructions_filename)); |
| 673 } |
| 674 if (isolate_snapshot_data_filename != NULL) { |
| 675 success &= file->Print( |
| 676 "%s ", File::GetCanonicalPath(isolate_snapshot_data_filename)); |
| 677 } |
| 678 if (isolate_snapshot_instructions_filename != NULL) { |
| 679 success &= file->Print( |
| 680 "%s ", File::GetCanonicalPath(isolate_snapshot_instructions_filename)); |
| 681 } |
| 682 if (assembly_filename != NULL) { |
| 683 success &= file->Print("%s ", File::GetCanonicalPath(assembly_filename)); |
| 684 } |
| 685 if (script_snapshot_filename != NULL) { |
| 686 success &= |
| 687 file->Print("%s ", File::GetCanonicalPath(script_snapshot_filename)); |
| 688 } |
| 689 |
| 690 success &= file->Print(": "); |
| 691 |
| 692 // Sources: |
| 693 for (intptr_t i = 0; i < dependencies->length(); i++) { |
| 694 char* dep = dependencies->At(i); |
| 695 success &= file->Print("%s ", dep); |
| 696 free(dep); |
| 697 } |
| 698 success &= file->Print("\n"); |
| 699 |
| 700 if (!success) { |
| 701 Log::PrintErr("Error: Unable to write dependencies file: %s\n\n", |
| 702 dependencies_filename); |
| 703 exit(kErrorExitCode); |
| 704 } |
| 705 file->Release(); |
| 706 delete dependencies; |
| 707 isolate_data->set_dependencies(NULL); |
| 708 } |
| 709 |
| 710 |
| 594 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, | 711 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 595 Dart_Handle library, | 712 Dart_Handle library, |
| 596 Dart_Handle url) { | 713 Dart_Handle url) { |
| 597 if (!Dart_IsLibrary(library)) { | 714 if (!Dart_IsLibrary(library)) { |
| 598 return Dart_NewApiError("not a library"); | 715 return Dart_NewApiError("not a library"); |
| 599 } | 716 } |
| 600 Dart_Handle library_url = Dart_LibraryUrl(library); | 717 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 601 if (Dart_IsError(library_url)) { | 718 if (Dart_IsError(library_url)) { |
| 602 return Dart_NewApiError("accessing library url failed"); | 719 return Dart_NewApiError("accessing library url failed"); |
| 603 } | 720 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 } | 802 } |
| 686 | 803 |
| 687 | 804 |
| 688 // clang-format off | 805 // clang-format off |
| 689 static void PrintUsage() { | 806 static void PrintUsage() { |
| 690 Log::PrintErr( | 807 Log::PrintErr( |
| 691 "Usage: \n" | 808 "Usage: \n" |
| 692 " gen_snapshot [<vm-flags>] [<options>] [<dart-script-file>] \n" | 809 " gen_snapshot [<vm-flags>] [<options>] [<dart-script-file>] \n" |
| 693 " \n" | 810 " \n" |
| 694 " Global options: \n" | 811 " Global options: \n" |
| 695 " --package_root=<path> Where to find packages, that is, \n" | 812 " --package_root=<path> Where to find packages, that is, \n" |
| 696 " package:... imports. \n" | 813 " package:... imports. \n" |
| 697 " \n" | 814 " \n" |
| 698 " --packages=<packages_file> Where to find a package spec file \n" | 815 " --packages=<packages_file> Where to find a package spec file \n" |
| 699 " \n" | 816 " \n" |
| 700 " --url_mapping=<mapping> Uses the URL mapping(s) specified on \n" | 817 " --url_mapping=<mapping> Uses the URL mapping(s) specified on \n" |
| 701 " the command line to load the \n" | 818 " the command line to load the \n" |
| 702 " libraries. \n" | 819 " libraries. \n" |
| 820 " --dependencies=<output-file> Generates a Makefile with snapshot output \n" |
| 821 " files as targets and all transitive imports\n" |
| 822 " as sources. \n" |
| 823 " \n" |
| 824 " To discover dependencies without generating a snapshot: \n" |
| 825 " --snapshot_kind=none \n" |
| 826 " --dependencies=<output-file> \n" |
| 827 " <dart-script-file> \n" |
| 703 " \n" | 828 " \n" |
| 704 " To create a core snapshot: \n" | 829 " To create a core snapshot: \n" |
| 705 " --snapshot_kind=core \n" | 830 " --snapshot_kind=core \n" |
| 706 " --vm_snapshot_data=<output-file> \n" | 831 " --vm_snapshot_data=<output-file> \n" |
| 707 " --isolate_snapshot_data=<output-file> \n" | 832 " --isolate_snapshot_data=<output-file> \n" |
| 708 " [<dart-script-file>] \n" | 833 " [<dart-script-file>] \n" |
| 709 " \n" | 834 " \n" |
| 710 " Writes a snapshot of <dart-script-file> to the specified snapshot files. \n" | 835 " Writes a snapshot of <dart-script-file> to the specified snapshot files. \n" |
| 711 " If no <dart-script-file> is passed, a generic snapshot of all the corelibs \n" | 836 " If no <dart-script-file> is passed, a generic snapshot of all the corelibs \n" |
| 712 " is created. \n" | 837 " is created. \n" |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1336 File* file = File::Open(vm_snapshot_data_filename, File::kRead); | 1461 File* file = File::Open(vm_snapshot_data_filename, File::kRead); |
| 1337 if (file == NULL) { | 1462 if (file == NULL) { |
| 1338 Log::PrintErr("Failed to open: %s\n", vm_snapshot_data_filename); | 1463 Log::PrintErr("Failed to open: %s\n", vm_snapshot_data_filename); |
| 1339 return kErrorExitCode; | 1464 return kErrorExitCode; |
| 1340 } | 1465 } |
| 1341 mapped_vm_snapshot_data = file->Map(File::kReadOnly, 0, file->Length()); | 1466 mapped_vm_snapshot_data = file->Map(File::kReadOnly, 0, file->Length()); |
| 1342 if (mapped_vm_snapshot_data == NULL) { | 1467 if (mapped_vm_snapshot_data == NULL) { |
| 1343 Log::PrintErr("Failed to read: %s\n", vm_snapshot_data_filename); | 1468 Log::PrintErr("Failed to read: %s\n", vm_snapshot_data_filename); |
| 1344 return kErrorExitCode; | 1469 return kErrorExitCode; |
| 1345 } | 1470 } |
| 1346 file->Close(); | 1471 file->Release(); |
| 1347 init_params.vm_snapshot_data = | 1472 init_params.vm_snapshot_data = |
| 1348 reinterpret_cast<const uint8_t*>(mapped_vm_snapshot_data->address()); | 1473 reinterpret_cast<const uint8_t*>(mapped_vm_snapshot_data->address()); |
| 1349 | 1474 |
| 1350 file = File::Open(isolate_snapshot_data_filename, File::kRead); | 1475 file = File::Open(isolate_snapshot_data_filename, File::kRead); |
| 1351 if (file == NULL) { | 1476 if (file == NULL) { |
| 1352 Log::PrintErr("Failed to open: %s\n", isolate_snapshot_data_filename); | 1477 Log::PrintErr("Failed to open: %s\n", isolate_snapshot_data_filename); |
| 1353 return kErrorExitCode; | 1478 return kErrorExitCode; |
| 1354 } | 1479 } |
| 1355 mapped_isolate_snapshot_data = | 1480 mapped_isolate_snapshot_data = |
| 1356 file->Map(File::kReadOnly, 0, file->Length()); | 1481 file->Map(File::kReadOnly, 0, file->Length()); |
| 1357 if (mapped_isolate_snapshot_data == NULL) { | 1482 if (mapped_isolate_snapshot_data == NULL) { |
| 1358 Log::PrintErr("Failed to read: %s\n", isolate_snapshot_data_filename); | 1483 Log::PrintErr("Failed to read: %s\n", isolate_snapshot_data_filename); |
| 1359 return kErrorExitCode; | 1484 return kErrorExitCode; |
| 1360 } | 1485 } |
| 1361 file->Close(); | 1486 file->Release(); |
| 1362 isolate_snapshot_data = reinterpret_cast<const uint8_t*>( | 1487 isolate_snapshot_data = reinterpret_cast<const uint8_t*>( |
| 1363 mapped_isolate_snapshot_data->address()); | 1488 mapped_isolate_snapshot_data->address()); |
| 1364 } | 1489 } |
| 1365 | 1490 |
| 1366 char* error = Dart_Initialize(&init_params); | 1491 char* error = Dart_Initialize(&init_params); |
| 1367 if (error != NULL) { | 1492 if (error != NULL) { |
| 1368 Log::PrintErr("VM initialization failed: %s\n", error); | 1493 Log::PrintErr("VM initialization failed: %s\n", error); |
| 1369 free(error); | 1494 free(error); |
| 1370 return kErrorExitCode; | 1495 return kErrorExitCode; |
| 1371 } | 1496 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1418 Dart_ExitIsolate(); | 1543 Dart_ExitIsolate(); |
| 1419 | 1544 |
| 1420 // Now we create an isolate into which we load all the code that needs to | 1545 // Now we create an isolate into which we load all the code that needs to |
| 1421 // be in the snapshot. | 1546 // be in the snapshot. |
| 1422 isolate_data = new IsolateData(NULL, NULL, NULL, NULL); | 1547 isolate_data = new IsolateData(NULL, NULL, NULL, NULL); |
| 1423 const uint8_t* kernel = NULL; | 1548 const uint8_t* kernel = NULL; |
| 1424 intptr_t kernel_length = 0; | 1549 intptr_t kernel_length = 0; |
| 1425 const bool is_kernel_file = | 1550 const bool is_kernel_file = |
| 1426 TryReadKernel(app_script_name, &kernel, &kernel_length); | 1551 TryReadKernel(app_script_name, &kernel, &kernel_length); |
| 1427 | 1552 |
| 1553 if (dependencies_filename != NULL) { |
| 1554 isolate_data->set_dependencies(new MallocGrowableArray<char*>()); |
| 1555 } |
| 1556 |
| 1428 void* kernel_program = NULL; | 1557 void* kernel_program = NULL; |
| 1429 if (is_kernel_file) { | 1558 if (is_kernel_file) { |
| 1430 kernel_program = Dart_ReadKernelBinary(kernel, kernel_length); | 1559 kernel_program = Dart_ReadKernelBinary(kernel, kernel_length); |
| 1431 free(const_cast<uint8_t*>(kernel)); | 1560 free(const_cast<uint8_t*>(kernel)); |
| 1432 } | 1561 } |
| 1433 | 1562 |
| 1434 Dart_Isolate isolate = | 1563 Dart_Isolate isolate = |
| 1435 is_kernel_file | 1564 is_kernel_file |
| 1436 ? Dart_CreateIsolateFromKernel(NULL, NULL, kernel_program, NULL, | 1565 ? Dart_CreateIsolateFromKernel(NULL, NULL, kernel_program, NULL, |
| 1437 isolate_data, &error) | 1566 isolate_data, &error) |
| 1438 : Dart_CreateIsolate(NULL, NULL, isolate_snapshot_data, NULL, NULL, | 1567 : Dart_CreateIsolate(NULL, NULL, isolate_snapshot_data, NULL, NULL, |
| 1439 isolate_data, &error); | 1568 isolate_data, &error); |
| 1440 if (isolate == NULL) { | 1569 if (isolate == NULL) { |
| 1441 Log::PrintErr("%s\n", error); | 1570 Log::PrintErr("%s\n", error); |
| 1442 free(error); | 1571 free(error); |
| 1443 exit(kErrorExitCode); | 1572 exit(kErrorExitCode); |
| 1444 } | 1573 } |
| 1445 Dart_EnterScope(); | 1574 Dart_EnterScope(); |
| 1446 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 1575 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
| 1447 CHECK_RESULT(result); | 1576 CHECK_RESULT(result); |
| 1448 | 1577 |
| 1449 // Set up the library tag handler in such a manner that it will use the | 1578 // Set up the library tag handler in such a manner that it will use the |
| 1450 // URL mapping specified on the command line to load the libraries. | 1579 // URL mapping specified on the command line to load the libraries. |
| 1451 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); | 1580 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); |
| 1452 CHECK_RESULT(result); | 1581 CHECK_RESULT(result); |
| 1453 | 1582 |
| 1583 if (commandline_packages_file != NULL) { |
| 1584 AddDependency(commandline_packages_file); |
| 1585 } |
| 1586 |
| 1454 Dart_QualifiedFunctionName* entry_points = | 1587 Dart_QualifiedFunctionName* entry_points = |
| 1455 ParseEntryPointsManifestIfPresent(); | 1588 ParseEntryPointsManifestIfPresent(); |
| 1456 | 1589 |
| 1457 if (is_kernel_file) { | 1590 if (is_kernel_file) { |
| 1458 Dart_Handle library = Dart_LoadKernel(kernel_program); | 1591 Dart_Handle library = Dart_LoadKernel(kernel_program); |
| 1459 if (Dart_IsError(library)) FATAL("Failed to load app from Kernel IR"); | 1592 if (Dart_IsError(library)) FATAL("Failed to load app from Kernel IR"); |
| 1460 } else { | 1593 } else { |
| 1461 // Set up the library tag handler in such a manner that it will use the | 1594 // Set up the library tag handler in such a manner that it will use the |
| 1462 // URL mapping specified on the command line to load the libraries. | 1595 // URL mapping specified on the command line to load the libraries. |
| 1463 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); | 1596 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1474 CHECK_RESULT(library); | 1607 CHECK_RESULT(library); |
| 1475 | 1608 |
| 1476 ImportNativeEntryPointLibrariesIntoRoot(entry_points); | 1609 ImportNativeEntryPointLibrariesIntoRoot(entry_points); |
| 1477 } | 1610 } |
| 1478 | 1611 |
| 1479 // Ensure that we mark all libraries as loaded. | 1612 // Ensure that we mark all libraries as loaded. |
| 1480 result = Dart_FinalizeLoading(false); | 1613 result = Dart_FinalizeLoading(false); |
| 1481 CHECK_RESULT(result); | 1614 CHECK_RESULT(result); |
| 1482 | 1615 |
| 1483 switch (snapshot_kind) { | 1616 switch (snapshot_kind) { |
| 1617 case kNone: |
| 1618 break; |
| 1484 case kCore: | 1619 case kCore: |
| 1485 CreateAndWriteCoreSnapshot(); | 1620 CreateAndWriteCoreSnapshot(); |
| 1486 break; | 1621 break; |
| 1487 case kScript: | 1622 case kScript: |
| 1488 CreateAndWriteScriptSnapshot(); | 1623 CreateAndWriteScriptSnapshot(); |
| 1489 break; | 1624 break; |
| 1490 case kAppAOTBlobs: | 1625 case kAppAOTBlobs: |
| 1491 case kAppAOTAssembly: | 1626 case kAppAOTAssembly: |
| 1492 CreateAndWritePrecompiledSnapshot(entry_points); | 1627 CreateAndWritePrecompiledSnapshot(entry_points); |
| 1493 break; | 1628 break; |
| 1494 default: | 1629 default: |
| 1495 UNREACHABLE(); | 1630 UNREACHABLE(); |
| 1496 } | 1631 } |
| 1497 | 1632 |
| 1633 CreateAndWriteDependenciesFile(); |
| 1634 |
| 1498 Dart_ExitScope(); | 1635 Dart_ExitScope(); |
| 1499 Dart_ShutdownIsolate(); | 1636 Dart_ShutdownIsolate(); |
| 1500 | 1637 |
| 1501 CleanupEntryPointsCollection(entry_points); | 1638 CleanupEntryPointsCollection(entry_points); |
| 1502 | 1639 |
| 1503 Dart_EnterIsolate(UriResolverIsolateScope::isolate); | 1640 Dart_EnterIsolate(UriResolverIsolateScope::isolate); |
| 1504 Dart_ShutdownIsolate(); | 1641 Dart_ShutdownIsolate(); |
| 1505 } else { | 1642 } else { |
| 1506 SetupForGenericSnapshotCreation(); | 1643 SetupForGenericSnapshotCreation(); |
| 1507 CreateAndWriteCoreSnapshot(); | 1644 CreateAndWriteCoreSnapshot(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1519 delete mapped_isolate_snapshot_data; | 1656 delete mapped_isolate_snapshot_data; |
| 1520 return 0; | 1657 return 0; |
| 1521 } | 1658 } |
| 1522 | 1659 |
| 1523 } // namespace bin | 1660 } // namespace bin |
| 1524 } // namespace dart | 1661 } // namespace dart |
| 1525 | 1662 |
| 1526 int main(int argc, char** argv) { | 1663 int main(int argc, char** argv) { |
| 1527 return dart::bin::main(argc, argv); | 1664 return dart::bin::main(argc, argv); |
| 1528 } | 1665 } |
| OLD | NEW |