| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Test generating and running a simple precompiled snapshot of this script. | 5 // Test generating and running a simple precompiled snapshot of this script. |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 main(List args) { | 9 main(List args) { |
| 10 if (!Platform.isLinux) { | |
| 11 print("Linux only test"); | |
| 12 return; | |
| 13 } | |
| 14 if (args.length > 0 && args[0] == "--hello") { | 10 if (args.length > 0 && args[0] == "--hello") { |
| 15 print("Hello"); | 11 print("Hello"); |
| 16 return; | 12 return; |
| 17 } | 13 } |
| 14 |
| 15 var cc, cc_flags, shared, libname; |
| 16 if (Platform.isLinux) { |
| 17 cc = 'gcc'; |
| 18 shared = '-shared'; |
| 19 libname = 'libprecompiled.so'; |
| 20 } else if (Platform.isMacOS) { |
| 21 cc = 'clang'; |
| 22 shared = '-dynamiclib'; |
| 23 libname = 'libprecompiled.dylib'; |
| 24 } else { |
| 25 print("Test only supports Linux and Mac"); |
| 26 return; |
| 27 } |
| 28 |
| 29 if (Platform.version.contains("x64")) { |
| 30 cc_flags = "-m64"; |
| 31 } else if (Platform.version.contains("simarm64")) { |
| 32 cc_flags = "-m64"; |
| 33 } else if (Platform.version.contains("simarm")) { |
| 34 cc_flags = "-m32"; |
| 35 } else if (Platform.version.contains("simmips")) { |
| 36 cc_flags = "-m32"; |
| 37 } else if (Platform.version.contains("arm")) { |
| 38 cc_flags = ""; |
| 39 } else if (Platform.version.contains("mips")) { |
| 40 cc_flags = "-EL"; |
| 41 } else { |
| 42 print("Architecture not supported: ${Platform.version}"); |
| 43 return; |
| 44 } |
| 45 |
| 18 var dart_executable = | 46 var dart_executable = |
| 19 Directory.current.path + Platform.pathSeparator + Platform.executable; | 47 Directory.current.path + Platform.pathSeparator + Platform.executable; |
| 20 Directory tmp; | 48 Directory tmp; |
| 21 try { | 49 try { |
| 22 tmp = Directory.current.createTempSync("temp_precompilation_test"); | 50 tmp = Directory.current.createTempSync("temp_precompilation_test"); |
| 23 var result = Process.runSync( | 51 var result = Process.runSync( |
| 24 "${dart_executable}_no_snapshot", | 52 "${dart_executable}_no_snapshot", |
| 25 ["--gen-precompiled-snapshot", Platform.script.path], | 53 ["--gen-precompiled-snapshot", Platform.script.path], |
| 26 workingDirectory: tmp.path); | 54 workingDirectory: tmp.path); |
| 27 if (result.exitCode != 0) { | 55 if (result.exitCode != 0) { |
| 28 print(result.stdout); | 56 print(result.stdout); |
| 29 print(result.stderr); | 57 print(result.stderr); |
| 30 throw "Snapshot generation failed."; | 58 throw "Snapshot generation failed."; |
| 31 } | 59 } |
| 32 | 60 |
| 33 // Check if gcc is present, and skip test if it is not. | 61 // Check if gcc is present, and skip test if it is not. |
| 34 try { | 62 try { |
| 35 result = Process.runSync( | 63 result = Process.runSync( |
| 36 "gcc", | 64 cc, |
| 37 ["--version"], | 65 ["--version"], |
| 38 workingDirectory: tmp.path); | 66 workingDirectory: tmp.path); |
| 39 if (result.exitCode != 0) { | 67 if (result.exitCode != 0) { |
| 40 throw "gcc --version failed."; | 68 throw "$cc --version failed."; |
| 41 } | 69 } |
| 42 } catch(e) { | 70 } catch(e) { |
| 43 print("Skipping test because gcc is not present: $e"); | 71 print("Skipping test because $cc is not present: $e"); |
| 44 return; | 72 return; |
| 45 } | 73 } |
| 46 | 74 |
| 47 // Detect if we're running a 32- or 64-bit VM. | |
| 48 result = Process.runSync( "file", [dart_executable]); | |
| 49 if (result.exitCode != 0) { | |
| 50 print(result.stdout); | |
| 51 print(result.stderr); | |
| 52 throw "'file $dart_executable' failed."; | |
| 53 } | |
| 54 var m_option = result.stdout.contains("32-bit") ? "-m32" : "-m64"; | |
| 55 | |
| 56 result = Process.runSync( | 75 result = Process.runSync( |
| 57 "gcc", | 76 cc, |
| 58 ["-shared", m_option, "-o", "libprecompiled.so", "precompiled.S"], | 77 [shared, cc_flags, "-o", libname, "precompiled.S"], |
| 59 workingDirectory: tmp.path); | 78 workingDirectory: tmp.path); |
| 60 if (result.exitCode != 0) { | 79 if (result.exitCode != 0) { |
| 61 print(result.stdout); | 80 print(result.stdout); |
| 62 print(result.stderr); | 81 print(result.stderr); |
| 63 throw "Shared library creation failed!"; | 82 throw "Shared library creation failed!"; |
| 64 } | 83 } |
| 84 |
| 85 var ld_library_path = new String.fromEnvironment("LD_LIBRARY_PATH"); |
| 86 ld_library_path = "${ld_library_path}:${tmp.path}"; |
| 87 |
| 65 result = Process.runSync( | 88 result = Process.runSync( |
| 66 "${dart_executable}", | 89 "${dart_executable}", |
| 67 ["--run-precompiled-snapshot", "ignored_script", "--hello"], | 90 ["--run-precompiled-snapshot", "ignored_script", "--hello"], |
| 68 workingDirectory: tmp.path); | 91 workingDirectory: tmp.path, |
| 92 environment: {"LD_LIBRARY_PATH": ld_library_path}); |
| 69 if (result.exitCode != 0) { | 93 if (result.exitCode != 0) { |
| 70 print(result.stdout); | 94 print(result.stdout); |
| 71 print(result.stderr); | 95 print(result.stderr); |
| 72 throw "Precompiled binary failed."; | 96 throw "Precompiled binary failed."; |
| 73 } | 97 } |
| 74 print(result.stdout); | 98 print(result.stdout); |
| 75 if (result.stdout != "Hello\n") { | 99 if (result.stdout != "Hello\n") { |
| 76 throw "Precompiled binary output mismatch."; | 100 throw "Precompiled binary output mismatch."; |
| 77 } | 101 } |
| 78 } finally { | 102 } finally { |
| 79 tmp?.deleteSync(recursive: true); | 103 tmp?.deleteSync(recursive: true); |
| 80 } | 104 } |
| 81 } | 105 } |
| OLD | NEW |