Skip to content

Commit aa5eef1

Browse files
Using subprocess to run executable instead
1 parent 599e3b8 commit aa5eef1

File tree

1 file changed

+27
-95
lines changed

1 file changed

+27
-95
lines changed

Src/runcpp2/runcpp2.cpp

Lines changed: 27 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,42 @@ namespace
3636
return true;
3737
}
3838

39-
bool RunCompiledScript( const std::string& scriptDirectory,
40-
const std::string& scriptName,
41-
const std::string& exeExt,
39+
bool RunCompiledScript( const std::string& executable,
40+
const std::string& scriptDirectory,
41+
const std::string& scriptPath,
4242
const std::vector<std::string>& runArgs)
4343
{
4444
INTERNAL_RUNCPP2_SAFE_START();
4545
ssLOG_FUNC_DEBUG();
4646

4747
std::error_code _;
48-
49-
std::string runCommand = runcpp2::ProcessPath(scriptDirectory + "/" + scriptName + exeExt);
48+
std::string interpretedRunPath = runcpp2::ProcessPath(scriptPath);
49+
std::string processedScriptDir = runcpp2::ProcessPath(scriptDirectory);
50+
std::vector<const char*> args = { interpretedRunPath.c_str() };
5051

5152
if(!runArgs.empty())
5253
{
5354
for(int i = 0; i < runArgs.size(); ++i)
54-
runCommand += " \"" + runArgs[i] + "\"";
55+
args.push_back(runArgs[i].c_str());
5556
}
5657

57-
ssLOG_INFO("Running: " << runCommand);
58-
5958
System2CommandInfo runCommandInfo = {};
59+
runCommandInfo.RunDirectory = processedScriptDir.c_str();
6060
runCommandInfo.RedirectOutput = true;
61-
SYSTEM2_RESULT result = System2Run(runCommand.c_str(), &runCommandInfo);
61+
62+
SYSTEM2_RESULT result = System2RunSubprocess( executable.c_str(),
63+
args.data(),
64+
args.size(),
65+
&runCommandInfo);
66+
67+
ssLOG_INFO("Running: " << executable);
68+
ssLOG_INFO("At: " << processedScriptDir);
69+
for(int i = 0; i < runArgs.size(); ++i)
70+
ssLOG_INFO("- " << runArgs[i]);
6271

6372
if(result != SYSTEM2_RESULT_SUCCESS)
6473
{
6574
ssLOG_ERROR("System2Run failed with result: " << result);
66-
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
6775
return false;
6876
}
6977

@@ -107,7 +115,6 @@ namespace
107115
else
108116
{
109117
ssLOG_ERROR("Failed to read from output with result: " << result);
110-
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
111118
return false;
112119
}
113120
}
@@ -116,81 +123,19 @@ namespace
116123
if(result != SYSTEM2_RESULT_SUCCESS)
117124
{
118125
ssLOG_ERROR("System2GetCommandReturnValueASync failed with result: " << result);
119-
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
120126
return false;
121127
}
122128

123129
if(statusCode != 0)
124130
{
125131
ssLOG_ERROR("Run command returned with non-zero status code: " << statusCode);
126-
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
127132
return false;
128133
}
129134

130-
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
131135
return true;
132136
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
133137
}
134138

135-
bool CopyCompiledExecutable(const std::string& scriptDirectory,
136-
const std::string& scriptName,
137-
const std::string& target,
138-
const std::string& exeExt,
139-
const std::vector<std::string>& copiedBinariesPaths,
140-
const std::string& targetSharedLibExt)
141-
{
142-
ssLOG_FUNC_DEBUG();
143-
144-
std::error_code _;
145-
std::error_code copyErrorCode;
146-
ghc::filesystem::copy( target,
147-
scriptDirectory + "/" + scriptName +
148-
exeExt,
149-
copyErrorCode);
150-
151-
if(copyErrorCode)
152-
{
153-
ssLOG_ERROR("Failed to copy file from " << target << " to " << scriptDirectory);
154-
ssLOG_ERROR("Error code: " << copyErrorCode.message());
155-
return -1;
156-
}
157-
158-
//Copy library files as well
159-
for(int i = 0; i < copiedBinariesPaths.size(); ++i)
160-
{
161-
if(!ghc::filesystem::exists(copiedBinariesPaths[i], _))
162-
{
163-
ssLOG_ERROR("Failed to find copied file: " << copiedBinariesPaths[i]);
164-
return -1;
165-
}
166-
167-
size_t foundIndex = copiedBinariesPaths[i].find_last_of(".");
168-
if(foundIndex != std::string::npos)
169-
{
170-
std::string ext = copiedBinariesPaths[i].substr(foundIndex);
171-
172-
if(ext == targetSharedLibExt)
173-
{
174-
const std::string targetFileName =
175-
ghc::filesystem::path(copiedBinariesPaths[i]).filename().string();
176-
177-
ghc::filesystem::copy( copiedBinariesPaths[i],
178-
scriptDirectory + "/" + targetFileName,
179-
copyErrorCode);
180-
}
181-
}
182-
}
183-
184-
if(copyErrorCode)
185-
{
186-
ssLOG_ERROR("Failed to copy file from " << target << " to " << scriptDirectory);
187-
ssLOG_ERROR("Error code: " << copyErrorCode.message());
188-
return -1;
189-
}
190-
191-
return 0;
192-
}
193-
194139
int RunCompiledSharedLib( const std::string& scriptDirectory,
195140
const std::string& scriptPath,
196141
const std::string& compiledSharedLibPath,
@@ -316,7 +261,6 @@ namespace
316261
ghc::filesystem::file_time_type lastScriptWriteTime =
317262
ghc::filesystem::last_write_time(absoluteScriptPath, _);
318263

319-
//Check if the c/cpp file is newer than the compiled c/c++ file
320264
if(currentOptions.find(runcpp2::CmdOptions::RESET_CACHE) != currentOptions.end())
321265
return false;
322266

@@ -362,6 +306,7 @@ namespace
362306
std::string currentExtension = it.path().extension().string();
363307

364308
ssLOG_DEBUG("currentFileName: " << currentFileName);
309+
ssLOG_DEBUG("currentExtension: " << currentExtension);
365310

366311
if(currentExtension == *sharedLibExtToCopy)
367312
outCopiedBinariesPaths.push_back(it.path().string());
@@ -425,6 +370,8 @@ int runcpp2::RunScript( const std::string& scriptPath,
425370
const std::unordered_map<CmdOptions, std::string> currentOptions,
426371
const std::vector<std::string>& runArgs)
427372
{
373+
ssLOG_FUNC_DEBUG();
374+
428375
if(profiles.empty())
429376
{
430377
ssLOG_ERROR("No compiler profiles found");
@@ -566,9 +513,9 @@ int runcpp2::RunScript( const std::string& scriptPath,
566513
}
567514
}
568515

569-
//Copying the compiled file to script directory
516+
//Run the compiled file at script directory
570517
{
571-
ssLOG_INFO("Copying script...");
518+
ssLOG_INFO("Running script...");
572519

573520
std::error_code _;
574521
std::string target = scriptDirectory + "/.runcpp2/";
@@ -600,26 +547,11 @@ int runcpp2::RunScript( const std::string& scriptPath,
600547

601548
if(currentOptions.count(CmdOptions::EXECUTABLE) > 0)
602549
{
603-
if(!CopyCompiledExecutable( scriptDirectory,
604-
scriptName,
605-
target,
606-
exeExt,
607-
copiedBinariesPaths,
608-
*targetSharedLibExt))
609-
{
610-
ssLOG_ERROR("Failed to copy compiled executable: " << target);
611-
return -1;
612-
}
613-
614-
//Run it?
615-
if(false)
550+
//Running the script
551+
if(!RunCompiledScript(target, scriptDirectory, absoluteScriptPath, runArgs))
616552
{
617-
//Running the script
618-
if(!RunCompiledScript(scriptDirectory, scriptName, exeExt, runArgs))
619-
{
620-
ssLOG_ERROR("Failed to run script");
621-
return false;
622-
}
553+
ssLOG_ERROR("Failed to run script");
554+
return false;
623555
}
624556

625557
return 0;

0 commit comments

Comments
 (0)