Skip to content

Commit 2c58506

Browse files
Allow option to pass script path
1 parent 11e082a commit 2c58506

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

DefaultYAMLs/DefaultScriptInfo.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# NOTE: All the options here are defaulted to empty.
55
# You can uncomment different sections to set what you want.
66

7+
# (Optional) Whether to pass the script path as the second parameter when running
8+
PassScriptPath: false
9+
710
# # (Optional) Language of the script
811
Language: "c++"
912

Examples/test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* runcpp2
22
3+
PassScriptPath: true
4+
35
RequiredProfiles:
46
Windows: ["msvc"]
57
Unix: ["g++"]

Include/runcpp2/Data/ScriptInfo.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace runcpp2
1919
{
2020
public:
2121
std::string Language;
22+
bool PassScriptPath = false;
2223
std::unordered_map<PlatformName, std::vector<ProfileName>> RequiredProfiles;
2324

2425
std::unordered_map<PlatformName, ProfilesFlagsOverride> OverrideCompileFlags;

Src/runcpp2/Data/ScriptInfo.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bool runcpp2::Data::ScriptInfo::ParseYAML_Node(ryml::ConstNodeRef& node)
1111

1212
std::vector<NodeRequirement> requirements =
1313
{
14+
NodeRequirement("PassScriptPath", ryml::NodeType_e::KEYVAL, false, true),
1415
NodeRequirement("Language", ryml::NodeType_e::KEYVAL, false, true),
1516
NodeRequirement("RequiredProfiles", ryml::NodeType_e::MAP, false, true),
1617
NodeRequirement("OverrideCompileFlags", ryml::NodeType_e::MAP, false, true),
@@ -26,6 +27,24 @@ bool runcpp2::Data::ScriptInfo::ParseYAML_Node(ryml::ConstNodeRef& node)
2627
return false;
2728
}
2829

30+
if(ExistAndHasChild(node, "PassScriptPath"))
31+
{
32+
std::string passScriptPathStr = GetValue(node["PassScriptPath"]);
33+
for(size_t i = 0; i < passScriptPathStr.length(); ++i)
34+
passScriptPathStr[i] = std::tolower(passScriptPathStr[i]);
35+
36+
if(passScriptPathStr == "true" || passScriptPathStr == "1")
37+
PassScriptPath = true;
38+
else if(passScriptPathStr == "false" || passScriptPathStr == "0")
39+
PassScriptPath = false;
40+
else
41+
{
42+
ssLOG_ERROR("ScriptInfo: Invalid value for PassScriptPath: " << passScriptPathStr);
43+
ssLOG_ERROR("Expected true/false or 1/0");
44+
return false;
45+
}
46+
}
47+
2948
if(ExistAndHasChild(node, "Language"))
3049
node["Language"] >> Language;
3150

@@ -149,6 +168,8 @@ std::string runcpp2::Data::ScriptInfo::ToString(std::string indentation) const
149168
{
150169
std::string out = indentation + "ScriptInfo:\n";
151170

171+
out += indentation + " PassScriptPath: " + (PassScriptPath ? "true" : "false") + "\n";
172+
152173
if(!Language.empty())
153174
out += indentation + " Language: " + Language + "\n";
154175

Src/runcpp2/runcpp2.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,9 @@ namespace
8585
INTERNAL_RUNCPP2_SAFE_START();
8686
ssLOG_FUNC_DEBUG();
8787

88-
std::error_code _;
89-
std::string interpretedRunPath = runcpp2::ProcessPath(scriptPath);
90-
std::vector<const char*> args = { interpretedRunPath.c_str() };
91-
92-
if(!runArgs.empty())
93-
{
94-
for(int i = 0; i < runArgs.size(); ++i)
95-
args.push_back(runArgs[i].c_str());
96-
}
88+
std::vector<const char*> args;
89+
for(size_t i = 0; i < runArgs.size(); ++i)
90+
args.push_back(runArgs[i].c_str());
9791

9892
System2CommandInfo runCommandInfo = {};
9993
SYSTEM2_RESULT result = System2RunSubprocess( executable.c_str(),
@@ -102,7 +96,7 @@ namespace
10296
&runCommandInfo);
10397

10498
ssLOG_INFO("Running: " << executable.string());
105-
for(int i = 0; i < runArgs.size(); ++i)
99+
for(size_t i = 0; i < runArgs.size(); ++i)
106100
ssLOG_INFO("- " << runArgs[i]);
107101

108102
if(result != SYSTEM2_RESULT_SUCCESS)
@@ -172,12 +166,12 @@ namespace
172166
return false;
173167
}
174168

175-
int (*scriptFullMain)(int, char**) = nullptr;
169+
int (*scriptFullMain)(int, const char**) = nullptr;
176170
int (*scriptMain)() = nullptr;
177171

178172
try
179173
{
180-
scriptFullMain = sharedLib->get_function<int(int, char**)>("main");
174+
scriptFullMain = sharedLib->get_function<int(int, const char**)>("main");
181175
}
182176
catch(const dylib::exception& ex)
183177
{
@@ -217,13 +211,9 @@ namespace
217211
{
218212
if(scriptFullMain != nullptr)
219213
{
220-
std::vector<std::string> runArgsCopy = runArgs;
221-
runArgsCopy.insert( runArgsCopy.begin(),
222-
runcpp2::ProcessPath(compiledSharedLibPath.string()));
223-
224-
std::vector<char*> runArgsCStr(runArgsCopy.size());
225-
for(int i = 0; i < runArgsCopy.size(); ++i)
226-
runArgsCStr.at(i) = &runArgsCopy.at(i).at(0);
214+
std::vector<const char*> runArgsCStr(runArgs.size());
215+
for(size_t i = 0; i < runArgs.size(); ++i)
216+
runArgsCStr.at(i) = &runArgs.at(i).at(0);
227217

228218
returnStatus = scriptFullMain(runArgsCStr.size(), runArgsCStr.data());
229219
}
@@ -636,6 +626,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
636626

637627
//Parsing the script, setting up dependencies, compiling and linking
638628
std::vector<std::string> filesToCopyPaths;
629+
Data::ScriptInfo scriptInfo;
639630
{
640631
//Check if there's script info as yaml file instead
641632
std::error_code e;
@@ -672,7 +663,6 @@ runcpp2::StartPipeline( const std::string& scriptPath,
672663
}
673664

674665
//Try to parse the runcpp2 info
675-
Data::ScriptInfo scriptInfo;
676666
if(!ParseScriptInfo(parsableInfo, scriptInfo))
677667
{
678668
ssLOG_ERROR("Failed to parse info");
@@ -1037,21 +1027,31 @@ runcpp2::StartPipeline( const std::string& scriptPath,
10371027
return PipelineResult::UNEXPECTED_FAILURE;
10381028
}
10391029

1030+
//Prepare run arguments
1031+
std::vector<std::string> finalRunArgs;
1032+
finalRunArgs.push_back(target.string());
1033+
if(scriptInfo.PassScriptPath)
1034+
finalRunArgs.push_back(absoluteScriptPath);
1035+
1036+
//Add user provided arguments
1037+
for(size_t i = 0; i < runArgs.size(); ++i)
1038+
finalRunArgs.push_back(runArgs[i]);
1039+
10401040
if(currentOptions.count(CmdOptions::EXECUTABLE) > 0)
10411041
{
1042-
//Running the script
1043-
if(!RunCompiledScript(target, absoluteScriptPath, runArgs, returnStatus))
1042+
//Running the script with modified args
1043+
if(!RunCompiledScript(target, absoluteScriptPath, finalRunArgs, returnStatus))
10441044
{
10451045
ssLOG_ERROR("Failed to run script");
10461046
return PipelineResult::RUN_SCRIPT_FAILED;
10471047
}
10481048

10491049
return PipelineResult::SUCCESS;
10501050
}
1051-
//Load the shared library and run it
1051+
//Load the shared library and run it with modified args
10521052
else
10531053
{
1054-
if(!RunCompiledSharedLib(absoluteScriptPath, target, runArgs, returnStatus))
1054+
if(!RunCompiledSharedLib(absoluteScriptPath, target, finalRunArgs, returnStatus))
10551055
{
10561056
ssLOG_ERROR("Failed to run script");
10571057
return PipelineResult::RUN_SCRIPT_FAILED;

TODO.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
TODO:
2-
- Track include dependencies for source files
2+
- Track include files for compiling
33
- Allow runcpp2 to be library
44
- Ability to compile runcpp2 as single cpp
55

6+
- Separate git and local source options
7+
- Migrate to libyaml
8+
- Track script info changes properly for either script change or dependency change
69
- Add wildcard support for filenames and extensions
710
- Add tests and examples (On Windows as well)
811
- Make SearchLibraryNames and SearchDirectories optional (?)

0 commit comments

Comments
 (0)