Skip to content

Commit 362c932

Browse files
Allowed multiple search directories and libs, refactoring & cmdline opts
1 parent 67d731f commit 362c932

File tree

13 files changed

+493
-383
lines changed

13 files changed

+493
-383
lines changed

DefaultYAMLs/DefaultScriptInfo.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ Dependencies:
5656
# Properties for searching the library binary for the profile
5757
"g++":
5858
# The library name to be searched for when linking against the script
59-
SearchLibraryName: lib1
59+
SearchLibraryNames: ["lib1"]
6060

6161
# The path (relative to the dependency folder) to be searched for the dependency binaries
62-
# TODO: Maybe rename this to SearchDirectories
63-
# TODO: Make this a list
64-
SearchPath: ./build
62+
SearchDirectories: ["./build"]
6563

6664
# (Optional) List of setup commands for the supported platforms
6765
Setup:

DefaultYAMLs/ScriptInfoSchema.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,20 @@
147147
"type": "object",
148148
"properties":
149149
{
150-
"SearchLibraryName":
150+
"SearchLibraryNames":
151151
{
152-
"type": "string",
152+
"type": "array",
153+
"items": { "type":"string" },
153154
"description": "The library name to be searched for when linking against the script"
154155
},
155-
"SearchPath":
156+
"SearchDirectories":
156157
{
157-
"type": "string",
158+
"type": "array",
159+
"items": { "type":"string" },
158160
"description": "The path to be searched for the dependency. {BuildType} will be replaced accordingly"
159161
}
160162
},
161-
"required": ["SearchLibraryName", "SearchPath"],
163+
"required": ["SearchLibraryNames", "SearchDirectories"],
162164
"additionalProperties": false,
163165
"description": "Properties for searching the library binary for the profile"
164166
}

Include/runcpp2/CompilingLinking.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@
77

88
namespace runcpp2
99
{
10+
bool CompileScript( const std::string& scriptPath,
11+
const ScriptInfo& scriptInfo,
12+
const CompilerProfile& profile,
13+
std::string& outScriptObjectFilePath);
14+
15+
bool LinkScript( const std::string& scriptPath,
16+
const ScriptInfo& scriptInfo,
17+
const CompilerProfile& profile,
18+
const std::string& scriptObjectFilePath,
19+
const std::vector<std::string>& copiedDependenciesBinariesNames);
20+
21+
1022
bool CompileAndLinkScript( const std::string& scriptPath,
1123
const ScriptInfo& scriptInfo,
12-
const CompilerProfile& profile);
24+
const CompilerProfile& profile,
25+
const std::vector<std::string>& copiedDependenciesBinariesNames);
1326
}
1427

1528
#endif

Include/runcpp2/Data/DependencySearchProperty.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace runcpp2
99
class DependencySearchProperty
1010
{
1111
public:
12-
std::string SearchLibraryName;
13-
std::string SearchPath;
12+
std::vector<std::string> SearchLibraryNames;
13+
std::vector<std::string> SearchDirectories;
1414

1515
bool ParseYAML_Node(YAML::Node& node);
1616
std::string ToString(std::string indentation) const;

Include/runcpp2/DependenciesSetupHelper.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
#include "runcpp2/Data/DependencyInfo.hpp"
55
#include "runcpp2/Data/ScriptInfo.hpp"
6+
#include "runcpp2/Data/CompilerProfile.hpp"
67
#include <vector>
8+
79
namespace runcpp2
810
{
911
bool IsDependencyAvailableForThisPlatform(const DependencyInfo& dependency);
@@ -32,6 +34,16 @@ namespace runcpp2
3234
bool resetDependencies,
3335
std::vector<std::string>& outDependenciesLocalCopiesPaths,
3436
std::vector<std::string>& outDependenciesSourcePaths);
37+
38+
bool GetDependencyBinariesExtensionsToCopy( const DependencyInfo& dependencyInfo,
39+
const CompilerProfile& profile,
40+
std::vector<std::string>& outExtensionsToCopy);
41+
42+
bool CopyDependenciesBinaries( const std::string& scriptPath,
43+
const ScriptInfo& scriptInfo,
44+
const std::vector<std::string>& dependenciesCopiesPaths,
45+
const CompilerProfile& profile,
46+
std::vector<std::string>& outCopiedBinariesNames);
3547
}
3648

3749
#endif

Include/runcpp2/runcpp2.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99

1010
namespace runcpp2
1111
{
12-
bool CreateRuncpp2ScriptDirectory(const std::string& scriptPath);
12+
enum class CmdOptions
13+
{
14+
NONE,
15+
SETUP,
16+
COUNT
17+
};
1318

14-
bool CopyDependenciesBinaries( const std::string& scriptPath,
15-
const ScriptInfo& scriptInfo,
16-
const std::vector<std::string>& dependenciesCopiesPaths,
17-
const CompilerProfile& profile);
19+
bool CreateRuncpp2ScriptDirectory(const std::string& scriptPath);
1820

1921
//--------------------------------------------
2022
//Running
2123
//--------------------------------------------
2224
bool RunScript( const std::string& scriptPath,
2325
const std::vector<CompilerProfile>& profiles,
2426
const std::string& configPreferredProfile,
27+
const std::unordered_map<CmdOptions, std::string> currentOptions,
2528
const std::vector<std::string>& runArgs);
2629

2730

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 🏃‍♂️ runcpp2
22

3-
A cross-platform tool that can let you run any cpp file as a script, just like python!
3+
A cross-platform tool that can let you run any c++ file as a script, just like python!
44

55
Run c++ files anytime, anywhere.
66

Src/runcpp2/CompilingLinking.cpp

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include "runcpp2/StringUtil.hpp"
77
#include "ssLogger/ssLog.hpp"
88

9-
bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
10-
const ScriptInfo& scriptInfo,
11-
const CompilerProfile& profile)
9+
10+
bool runcpp2::CompileScript(const std::string& scriptPath,
11+
const ScriptInfo& scriptInfo,
12+
const CompilerProfile& profile,
13+
std::string& outScriptObjectFilePath)
1214
{
1315
std::string scriptDirectory = ghc::filesystem::path(scriptPath).parent_path().string();
1416
std::string scriptName = ghc::filesystem::path(scriptPath).stem().string();
@@ -115,10 +117,10 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
115117
return false;
116118
}
117119

118-
std::string objectFileName = Internal::ProcessPath( runcpp2ScriptDir + "/" +
120+
std::string objectFilePath = Internal::ProcessPath( runcpp2ScriptDir + "/" +
119121
scriptName + "." + objectFileExt);
120122

121-
compileCommand.replace(foundIndex, objectFileSubstitution.size(), objectFileName);
123+
compileCommand.replace(foundIndex, objectFileSubstitution.size(), objectFilePath);
122124

123125
//Compile the script
124126
ssLOG_INFO("running compile command: " << compileCommand);
@@ -172,11 +174,24 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
172174
return false;
173175
}
174176

177+
outScriptObjectFilePath = objectFilePath;
178+
return true;
179+
}
180+
181+
bool runcpp2::LinkScript( const std::string& scriptPath,
182+
const ScriptInfo& scriptInfo,
183+
const CompilerProfile& profile,
184+
const std::string& scriptObjectFilePath,
185+
const std::vector<std::string>& copiedDependenciesBinariesNames)
186+
{
187+
std::string scriptName = ghc::filesystem::path(scriptPath).stem().string();
188+
175189
//Link the script to the dependencies
176190
std::string linkCommand = profile.Linker.Executable + " ";
177191
std::string currentOutputPart = profile.Linker.LinkerArgs.OutputPart;
178192
const std::string linkFlagsSubstitution = "{LinkFlags}";
179193
const std::string outputFileSubstitution = "{OutputFile}";
194+
const std::string objectFileSubstitution = "{ObjectFile}";
180195

181196
std::string linkFlags = profile.Linker.DefaultLinkFlags;
182197
std::string outputName = scriptName;
@@ -208,7 +223,7 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
208223
}
209224

210225
//Replace for {LinkFlags} for output part
211-
foundIndex = currentOutputPart.find(linkFlagsSubstitution);
226+
std::size_t foundIndex = currentOutputPart.find(linkFlagsSubstitution);
212227
if(foundIndex == std::string::npos)
213228
{
214229
ssLOG_ERROR("'" + linkFlagsSubstitution + "' missing in LinkerArgs");
@@ -233,7 +248,7 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
233248
return false;
234249
}
235250

236-
currentOutputPart.replace(foundIndex, objectFileSubstitution.size(), objectFileName);
251+
currentOutputPart.replace(foundIndex, objectFileSubstitution.size(), scriptObjectFilePath);
237252
Internal::Trim(currentOutputPart);
238253
linkCommand += currentOutputPart;
239254

@@ -248,27 +263,23 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
248263
return false;
249264
}
250265

251-
for(int i = 0; i < scriptInfo.Dependencies.size(); ++i)
266+
for(int i = 0; i < copiedDependenciesBinariesNames.size(); ++i)
252267
{
253-
if(scriptInfo.Dependencies.at(i).LibraryType == DependencyLibraryType::HEADER)
254-
continue;
255-
256-
std::string dependencyName = scriptInfo .Dependencies
257-
.at(i)
258-
.SearchProperties
259-
.at(profile.Name)
260-
.SearchLibraryName;
261-
262268
std::string currentDependencyPart = dependencyPart;
263-
currentDependencyPart.replace(foundIndex, dependencySubstitution.size(), dependencyName);
269+
currentDependencyPart.replace( foundIndex,
270+
dependencySubstitution.size(),
271+
copiedDependenciesBinariesNames[i]);
272+
264273
linkCommand += " " + currentDependencyPart;
265274
}
266275

276+
std::string scriptDirectory = ghc::filesystem::path(scriptPath).parent_path().string();
277+
std::string runcpp2ScriptDir = Internal::ProcessPath(scriptDirectory + "/.runcpp2");
267278
linkCommand = "cd " + runcpp2ScriptDir + " && " + linkCommand;
268279

269280
//Do Linking
270281
System2CommandInfo linkCommandInfo;
271-
result = System2Run(linkCommand.c_str(), &linkCommandInfo);
282+
SYSTEM2_RESULT result = System2Run(linkCommand.c_str(), &linkCommandInfo);
272283

273284
ssLOG_INFO("running link command: " << linkCommand);
274285

@@ -278,20 +289,21 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
278289
return false;
279290
}
280291

281-
output.clear();
292+
std::string output;
293+
294+
//output.clear();
282295
do
283296
{
284297
uint32_t byteRead = 0;
285-
286-
output.resize(output.size() + 4096);
298+
char tempBuffer[4096];
287299

288300
result = System2ReadFromOutput( &linkCommandInfo,
289-
output.data() + output.size() - 4096,
301+
tempBuffer,
290302
4096 - 1,
291303
&byteRead);
292304

293-
output.resize(output.size() - 4096 + byteRead + 1);
294-
output.back() = '\0';
305+
tempBuffer[byteRead] = '\0';
306+
output += tempBuffer;
295307
}
296308
while(result == SYSTEM2_RESULT_READ_NOT_FINISHED);
297309

@@ -303,7 +315,7 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
303315

304316
ssLOG_DEBUG("Link Output: \n" << output.data());
305317

306-
statusCode = 0;
318+
int statusCode = 0;
307319
result = System2GetCommandReturnValueSync(&linkCommandInfo, &statusCode);
308320

309321
if(result != SYSTEM2_RESULT_SUCCESS)
@@ -318,6 +330,32 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
318330
return false;
319331
}
320332

333+
return true;
334+
}
335+
336+
337+
bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
338+
const ScriptInfo& scriptInfo,
339+
const CompilerProfile& profile,
340+
const std::vector<std::string>& copiedDependenciesBinariesNames)
341+
{
342+
std::string scriptObjectFilePath;
343+
344+
if(!CompileScript(scriptPath, scriptInfo, profile, scriptObjectFilePath))
345+
{
346+
ssLOG_ERROR("CompileScript failed");
347+
return false;
348+
}
349+
350+
if(!LinkScript( scriptPath,
351+
scriptInfo,
352+
profile,
353+
scriptObjectFilePath,
354+
copiedDependenciesBinariesNames))
355+
{
356+
ssLOG_ERROR("LinkScript failed");
357+
return false;
358+
}
321359

322360
return true;
323361
}

Src/runcpp2/Data/DependencySearchProperty.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace runcpp2
1717

1818
std::vector<Internal::NodeRequirement> requirements =
1919
{
20-
Internal::NodeRequirement("SearchLibraryName", YAML::NodeType::Scalar, true, false),
21-
Internal::NodeRequirement("SearchPath", YAML::NodeType::Scalar, true, false)
20+
Internal::NodeRequirement("SearchLibraryNames", YAML::NodeType::Sequence, true, false),
21+
Internal::NodeRequirement("SearchDirectories", YAML::NodeType::Sequence, true, false)
2222
};
2323

2424
if(!Internal::CheckNodeRequirements(node, requirements))
@@ -27,8 +27,12 @@ namespace runcpp2
2727
return false;
2828
}
2929

30-
SearchLibraryName = node["SearchLibraryName"].as<std::string>();
31-
SearchPath = node["SearchPath"].as<std::string>();
30+
for(int i = 0; i < node["SearchLibraryNames"].size(); ++i)
31+
SearchLibraryNames.push_back(node["SearchLibraryNames"][i].as<std::string>());
32+
33+
for(int i = 0; i < node["SearchDirectories"].size(); ++i)
34+
SearchDirectories.push_back(node["SearchDirectories"][i].as<std::string>());
35+
3236
return true;
3337

3438
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
@@ -37,8 +41,15 @@ namespace runcpp2
3741
std::string DependencySearchProperty::ToString(std::string indentation) const
3842
{
3943
std::string out;
40-
out += indentation + "SearchLibraryName: " + SearchLibraryName + "\n";
41-
out += indentation + "SearchPath: " + SearchPath + "\n";
44+
out += indentation + "SearchLibraryName: \n";
45+
46+
for(int i = 0; i < SearchLibraryNames.size(); ++i)
47+
out += indentation + "- " + SearchLibraryNames[i] + "\n";
48+
49+
out += indentation + "SearchDirectories: \n";
50+
51+
for(int i = 0; i < SearchDirectories.size(); ++i)
52+
out += indentation + "- " + SearchDirectories[i] + "\n";
4253

4354
return out;
4455
}

0 commit comments

Comments
 (0)