corerun.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #include <coreruncommon.h>
  5. #include <string>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. // Display the command line options
  9. void DisplayUsage()
  10. {
  11. fprintf(
  12. stderr,
  13. "Usage: corerun [OPTIONS] assembly [ARGUMENTS]\n"
  14. "Execute the specified managed assembly with the passed in arguments\n\n"
  15. "Options:\n"
  16. "-c, --clr-path path to the libcoreclr.so and the managed CLR assemblies\n");
  17. }
  18. // Parse the command line arguments
  19. bool ParseArguments(
  20. const int argc,
  21. const char* argv[],
  22. const char** clrFilesPath,
  23. const char** managedAssemblyPath,
  24. int* managedAssemblyArgc,
  25. const char*** managedAssemblyArgv)
  26. {
  27. bool success = false;
  28. *clrFilesPath = nullptr;
  29. *managedAssemblyPath = nullptr;
  30. *managedAssemblyArgv = nullptr;
  31. *managedAssemblyArgc = 0;
  32. // The command line must contain at least the current exe name and the managed assembly path
  33. if (argc >= 2)
  34. {
  35. for (int i = 1; i < argc; i++)
  36. {
  37. // Check for an option
  38. if (argv[i][0] == '-')
  39. {
  40. // Path to the libcoreclr.so and the managed CLR assemblies
  41. if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--clr-path") == 0)
  42. {
  43. i++;
  44. if (i < argc)
  45. {
  46. *clrFilesPath = argv[i];
  47. }
  48. else
  49. {
  50. fprintf(stderr, "Option %s: missing path\n", argv[i - 1]);
  51. break;
  52. }
  53. }
  54. else if (strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  55. {
  56. DisplayUsage();
  57. break;
  58. }
  59. else
  60. {
  61. fprintf(stderr, "Unknown option %s\n", argv[i]);
  62. break;
  63. }
  64. }
  65. else
  66. {
  67. // First argument that is not an option is the managed assembly to execute
  68. *managedAssemblyPath = argv[i];
  69. int managedArgvOffset = (i + 1);
  70. *managedAssemblyArgc = argc - managedArgvOffset;
  71. if (*managedAssemblyArgc != 0)
  72. {
  73. *managedAssemblyArgv = &argv[managedArgvOffset];
  74. }
  75. success = true;
  76. break;
  77. }
  78. }
  79. }
  80. else
  81. {
  82. DisplayUsage();
  83. }
  84. return success;
  85. }
  86. int corerun(const int argc, const char* argv[])
  87. {
  88. const char* clrFilesPath;
  89. const char* managedAssemblyPath;
  90. const char** managedAssemblyArgv;
  91. int managedAssemblyArgc;
  92. if (!ParseArguments(
  93. argc,
  94. argv,
  95. &clrFilesPath,
  96. &managedAssemblyPath,
  97. &managedAssemblyArgc,
  98. &managedAssemblyArgv))
  99. {
  100. // Invalid command line
  101. return -1;
  102. }
  103. // Check if the specified managed assembly file exists
  104. struct stat sb;
  105. if (stat(managedAssemblyPath, &sb) == -1)
  106. {
  107. perror("Managed assembly not found");
  108. return -1;
  109. }
  110. // Verify that the managed assembly path points to a file
  111. if (!S_ISREG(sb.st_mode))
  112. {
  113. fprintf(stderr, "The specified managed assembly is not a file\n");
  114. return -1;
  115. }
  116. // Make sure we have a full path for argv[0].
  117. std::string argv0AbsolutePath;
  118. if (!GetEntrypointExecutableAbsolutePath(argv0AbsolutePath))
  119. {
  120. perror("Could not get full path");
  121. return -1;
  122. }
  123. std::string clrFilesAbsolutePath;
  124. if(!GetClrFilesAbsolutePath(argv0AbsolutePath.c_str(), clrFilesPath, clrFilesAbsolutePath))
  125. {
  126. return -1;
  127. }
  128. std::string managedAssemblyAbsolutePath;
  129. if (!GetAbsolutePath(managedAssemblyPath, managedAssemblyAbsolutePath))
  130. {
  131. perror("Failed to convert managed assembly path to absolute path");
  132. return -1;
  133. }
  134. int exitCode = ExecuteManagedAssembly(
  135. argv0AbsolutePath.c_str(),
  136. clrFilesAbsolutePath.c_str(),
  137. managedAssemblyAbsolutePath.c_str(),
  138. managedAssemblyArgc,
  139. managedAssemblyArgv);
  140. return exitCode;
  141. }
  142. int main(const int argc, const char* argv[])
  143. {
  144. return corerun(argc, argv);
  145. }