[sdks] add .gitignore python __pycache__ (#17414)
[mono-project.git] / netcore / corerun / corerun.cpp
blob4e5c9d9839e67daffa625f9c34e7837c49bf06d9
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.
5 #include <coreruncommon.h>
6 #include <string>
7 #include <string.h>
8 #include <sys/stat.h>
10 // Display the command line options
11 void DisplayUsage()
13 fprintf(
14 stderr,
15 "Usage: corerun [OPTIONS] assembly [ARGUMENTS]\n"
16 "Execute the specified managed assembly with the passed in arguments\n\n"
17 "Options:\n"
18 "-c, --clr-path path to the libcoreclr.so and the managed CLR assemblies\n");
21 // Parse the command line arguments
22 bool ParseArguments(
23 const int argc,
24 const char* argv[],
25 const char** clrFilesPath,
26 const char** managedAssemblyPath,
27 int* managedAssemblyArgc,
28 const char*** managedAssemblyArgv)
30 bool success = false;
32 *clrFilesPath = nullptr;
33 *managedAssemblyPath = nullptr;
34 *managedAssemblyArgv = nullptr;
35 *managedAssemblyArgc = 0;
37 // The command line must contain at least the current exe name and the managed assembly path
38 if (argc >= 2)
40 for (int i = 1; i < argc; i++)
42 // Check for an option
43 if (argv[i][0] == '-')
45 // Path to the libcoreclr.so and the managed CLR assemblies
46 if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--clr-path") == 0)
48 i++;
49 if (i < argc)
51 *clrFilesPath = argv[i];
53 else
55 fprintf(stderr, "Option %s: missing path\n", argv[i - 1]);
56 break;
59 else if (strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
61 DisplayUsage();
62 break;
64 else
66 fprintf(stderr, "Unknown option %s\n", argv[i]);
67 break;
70 else
72 // First argument that is not an option is the managed assembly to execute
73 *managedAssemblyPath = argv[i];
75 int managedArgvOffset = (i + 1);
76 *managedAssemblyArgc = argc - managedArgvOffset;
77 if (*managedAssemblyArgc != 0)
79 *managedAssemblyArgv = &argv[managedArgvOffset];
81 success = true;
82 break;
86 else
88 DisplayUsage();
91 return success;
94 int corerun(const int argc, const char* argv[])
96 const char* clrFilesPath;
97 const char* managedAssemblyPath;
98 const char** managedAssemblyArgv;
99 int managedAssemblyArgc;
101 if (!ParseArguments(
102 argc,
103 argv,
104 &clrFilesPath,
105 &managedAssemblyPath,
106 &managedAssemblyArgc,
107 &managedAssemblyArgv))
109 // Invalid command line
110 return -1;
113 // Check if the specified managed assembly file exists
114 struct stat sb;
115 if (stat(managedAssemblyPath, &sb) == -1)
117 perror("Managed assembly not found");
118 return -1;
121 // Verify that the managed assembly path points to a file
122 if (!S_ISREG(sb.st_mode))
124 fprintf(stderr, "The specified managed assembly is not a file\n");
125 return -1;
128 // Make sure we have a full path for argv[0].
129 std::string argv0AbsolutePath;
130 if (!GetEntrypointExecutableAbsolutePath(argv0AbsolutePath))
132 perror("Could not get full path");
133 return -1;
136 std::string clrFilesAbsolutePath;
137 if(!GetClrFilesAbsolutePath(argv0AbsolutePath.c_str(), clrFilesPath, clrFilesAbsolutePath))
139 return -1;
142 std::string managedAssemblyAbsolutePath;
143 if (!GetAbsolutePath(managedAssemblyPath, managedAssemblyAbsolutePath))
145 perror("Failed to convert managed assembly path to absolute path");
146 return -1;
149 int exitCode = ExecuteManagedAssembly(
150 argv0AbsolutePath.c_str(),
151 clrFilesAbsolutePath.c_str(),
152 managedAssemblyAbsolutePath.c_str(),
153 managedAssemblyArgc,
154 managedAssemblyArgv);
156 return exitCode;
159 int main(const int argc, const char* argv[])
161 return corerun(argc, argv);