Skip to content

Commit 8dbd2df

Browse files
Exposing thread option from command line
1 parent d951cfe commit 8dbd2df

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

Include/runcpp2/Data/CmdOptions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace runcpp2
2121
CONFIG_FILE,
2222
CLEANUP,
2323
BUILD_SOURCE_ONLY,
24+
THREADS,
2425
COUNT
2526
};
2627
}

Include/runcpp2/runcpp2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ namespace runcpp2
3939
const std::unordered_map<CmdOptions, std::string> currentOptions,
4040
const std::vector<std::string>& runArgs,
4141
const Data::ScriptInfo* lastScriptInfo,
42-
Data::ScriptInfo& outScriptInfo,
4342
const std::string& buildOutputDir,
43+
Data::ScriptInfo& outScriptInfo,
4444
int& returnStatus);
4545

4646
std::string PipelineResultToString(PipelineResult result);

Src/runcpp2/CompilingLinking.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ namespace
431431
if(actions.size() >= maxThreads || i == sourceFiles.size() - 1)
432432
{
433433
std::chrono::system_clock::time_point deadline =
434-
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads);
434+
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads < 8 ?
435+
8 :
436+
maxThreads);
435437

436438
for(int j = 0; j < actions.size(); ++j)
437439
{

Src/runcpp2/DependenciesHelper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ bool runcpp2::SetupDependenciesIfNeeded(const runcpp2::Data::Profile& profile,
556556
if(actions.size() >= maxThreads || i == availableDependencies.size() - 1)
557557
{
558558
std::chrono::system_clock::time_point deadline =
559-
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads);
559+
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads < 8 ?
560+
8 :
561+
maxThreads);
560562

561563
for(int j = 0; j < actions.size(); ++j)
562564
{
@@ -632,7 +634,9 @@ bool runcpp2::BuildDependencies(const runcpp2::Data::Profile& profile,
632634
if(actions.size() >= maxThreads || i == availableDependencies.size() - 1)
633635
{
634636
std::chrono::system_clock::time_point deadline =
635-
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads);
637+
std::chrono::system_clock::now() + std::chrono::seconds(maxThreads < 8 ?
638+
8 :
639+
maxThreads);
636640

637641
for(int j = 0; j < actions.size(); ++j)
638642
{

Src/runcpp2/main.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int main(int argc, char* argv[])
174174
int currentArgIndex = 0;
175175
std::unordered_map<runcpp2::CmdOptions, std::string> currentOptions;
176176
{
177-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 16, "Update this");
177+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
178178
std::unordered_map<std::string, runcpp2::OptionInfo> longOptionsMap =
179179
{
180180
{
@@ -236,10 +236,14 @@ int main(int argc, char* argv[])
236236
{
237237
"--build-source-only",
238238
runcpp2::OptionInfo(runcpp2::CmdOptions::BUILD_SOURCE_ONLY, false)
239-
}
239+
},
240+
{
241+
"--jobs",
242+
runcpp2::OptionInfo(runcpp2::CmdOptions::THREADS, true)
243+
},
240244
};
241245

242-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 16, "Update this");
246+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
243247
std::unordered_map<std::string, const runcpp2::OptionInfo&> shortOptionsMap =
244248
{
245249
{"-rc", longOptionsMap.at("--reset-cache")},
@@ -256,6 +260,7 @@ int main(int argc, char* argv[])
256260
{"-c", longOptionsMap.at("--config")},
257261
{"-cu", longOptionsMap.at("--cleanup")},
258262
{"-s", longOptionsMap.at("--build-source-only")},
263+
{"-j", longOptionsMap.at("--jobs")},
259264
};
260265

261266
currentArgIndex = ParseArgs(longOptionsMap, shortOptionsMap, currentOptions, argc, argv);
@@ -272,7 +277,7 @@ int main(int argc, char* argv[])
272277
//Help message
273278
if(currentOptions.count(runcpp2::CmdOptions::HELP))
274279
{
275-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 16, "Update this");
280+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
276281
ssLOG_BASE("Usage: runcpp2 [options] [input_file]");
277282
ssLOG_BASE("Options:");
278283
ssLOG_BASE(" Run/Build:");
@@ -285,6 +290,7 @@ int main(int argc, char* argv[])
285290
ssLOG_BASE(" -s, --build-[s]ource-only (Re)Builds source files only without building dependencies.");
286291
ssLOG_BASE(" The previous built binaries will be used for dependencies.");
287292
ssLOG_BASE(" Requires dependencies to be built already.");
293+
ssLOG_BASE(" -j, --[j]obs Maximum number of threads running. Defaults to 8");
288294
ssLOG_BASE(" Reset/Cleanup:");
289295
ssLOG_BASE(" -rc, --[r]eset-[c]ache Deletes compiled source files cache only");
290296
ssLOG_BASE(" -ru, --[r]eset-[u]ser-config Replace current user config with the default one");
@@ -469,8 +475,8 @@ int main(int argc, char* argv[])
469475
currentOptions,
470476
scriptArgs,
471477
lastParsedScriptInfo,
472-
parsedScriptInfo,
473478
"",
479+
parsedScriptInfo,
474480
result);
475481

476482
static_assert(static_cast<int>(runcpp2::PipelineResult::COUNT) == 13, "Update this");
@@ -518,8 +524,8 @@ int main(int argc, char* argv[])
518524
currentOptions,
519525
scriptArgs,
520526
nullptr,
521-
parsedScriptInfo,
522527
outputDir,
528+
parsedScriptInfo,
523529
result) != runcpp2::PipelineResult::SUCCESS)
524530
{
525531
return -1;

Src/runcpp2/runcpp2.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
400400
const std::unordered_map<CmdOptions, std::string> currentOptions,
401401
const std::vector<std::string>& runArgs,
402402
const Data::ScriptInfo* lastScriptInfo,
403-
Data::ScriptInfo& outScriptInfo,
404403
const std::string& buildOutputDir,
404+
Data::ScriptInfo& outScriptInfo,
405405
int& returnStatus)
406406
{
407407
INTERNAL_RUNCPP2_SAFE_START();
@@ -464,7 +464,15 @@ runcpp2::StartPipeline( const std::string& scriptPath,
464464
//Parsing the script, setting up dependencies, compiling and linking
465465
std::vector<std::string> filesToCopyPaths;
466466
{
467-
const int tempMaxThreads = 16;
467+
const int maxThreads =
468+
currentOptions.count(CmdOptions::THREADS) ?
469+
strtol(currentOptions.at(CmdOptions::THREADS).c_str(), nullptr, 10) :
470+
8;
471+
if(maxThreads == 0)
472+
{
473+
ssLOG_ERROR("Invalid number of threads passed in");
474+
return PipelineResult::INVALID_OPTION;
475+
}
468476

469477
BuildsManager buildsManager("/tmp");
470478
IncludeManager includeManager;
@@ -505,7 +513,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
505513
profiles.at(profileIndex),
506514
absoluteScriptPath,
507515
lastScriptInfo,
508-
tempMaxThreads,
516+
maxThreads,
509517
recompileNeeded,
510518
relinkNeeded,
511519
changedDependencies);
@@ -525,7 +533,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
525533
buildDir,
526534
currentOptions,
527535
changedDependencies,
528-
tempMaxThreads,
536+
maxThreads,
529537
availableDependencies,
530538
gatheredBinariesPaths);
531539

@@ -667,7 +675,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
667675
availableDependencies,
668676
profiles.at(profileIndex),
669677
currentOptions.count(CmdOptions::EXECUTABLE) > 0,
670-
tempMaxThreads))
678+
maxThreads))
671679
{
672680
return PipelineResult::COMPILE_LINK_FAILED;
673681
}
@@ -685,7 +693,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
685693
profiles.at(profileIndex),
686694
linkFilesPaths,
687695
currentOptions.count(CmdOptions::EXECUTABLE) > 0,
688-
tempMaxThreads))
696+
maxThreads))
689697
{
690698
ssLOG_ERROR("Failed to compile or link script");
691699
return PipelineResult::COMPILE_LINK_FAILED;

0 commit comments

Comments
 (0)