Skip to content

Commit 9bc4374

Browse files
Grouping compiled files into folders
1 parent 9e978cf commit 9bc4374

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed
File renamed without changes.
File renamed without changes.

Examples/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
All:
1010
# Target Profile
1111
"g++":
12-
- "./AnotherSourceFileGcc.cpp"
12+
- "./OtherSources/AnotherSourceFileGcc.cpp"
1313
All:
1414
- "./AnotherSourceFile.cpp"
1515
- "./AnotherSourceFile2.cpp"
@@ -44,7 +44,7 @@
4444
#define ssLOG_DLL 1
4545
#include "ssLogger/ssLog.hpp"
4646

47-
#include "./AnotherSourceFileGcc.hpp"
47+
#include "./OtherSources/AnotherSourceFileGcc.hpp"
4848

4949
#include <iostream>
5050
#include <chrono>

Include/runcpp2/CompilingLinking.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace runcpp2
1212
{
1313
bool CompileScriptOnly( const ghc::filesystem::path& buildDir,
14+
const ghc::filesystem::path& scriptPath,
1415
const std::vector<ghc::filesystem::path>& sourceFiles,
1516
const std::vector<bool>& sourceHasCache,
1617
const Data::ScriptInfo& scriptInfo,
@@ -20,6 +21,7 @@ namespace runcpp2
2021

2122
//TODO: Convert string paths to filesystem paths
2223
bool CompileAndLinkScript( const ghc::filesystem::path& buildDir,
24+
const ghc::filesystem::path& scriptPath,
2325
const std::string& outputName,
2426
const std::vector<ghc::filesystem::path>& sourceFiles,
2527
const std::vector<bool>& sourceHasCache,

Src/runcpp2/CompilingLinking.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ namespace
7979
}
8080

8181
bool CompileScript( const ghc::filesystem::path& buildDir,
82+
const ghc::filesystem::path& scriptPath,
8283
const std::vector<ghc::filesystem::path>& sourceFiles,
8384
const runcpp2::Data::ScriptInfo& scriptInfo,
8485
const std::vector<runcpp2::Data::DependencyInfo*>& availableDependencies,
@@ -130,17 +131,27 @@ namespace
130131

131132
for(int i = 0; i < sourceFiles.size(); ++i)
132133
{
134+
std::error_code e;
133135
ghc::filesystem::path currentSource = sourceFiles.at(i);
136+
ghc::filesystem::path relativeSourcePath =
137+
ghc::filesystem::relative(currentSource, scriptPath.parent_path(), e);
138+
139+
if(e)
140+
{
141+
ssLOG_ERROR("Failed to get relative path for " << currentSource);
142+
ssLOG_ERROR("Failed with error: " << e.message());
143+
return false;
144+
}
134145

135146
//TODO: Maybe do ProcessPath on all the .string()?
136-
std::string souceDirectory = currentSource.parent_path().string();
147+
std::string sourceDirectory = currentSource.parent_path().string();
137148
std::string sourceName = currentSource.stem().string();
138149
std::string sourceExt = currentSource.extension().string();
139150
//Input File
140151
{
141152
substitutionMap["{InputFileName}"] = {sourceName};
142153
substitutionMap["{InputFileExtension}"] = {sourceExt};
143-
substitutionMap["{InputFileDirectory}"] = {souceDirectory};
154+
substitutionMap["{InputFileDirectory}"] = {sourceDirectory};
144155
substitutionMap["{InputFilePath}"] = {currentSource.string()};
145156
}
146157
//Output File
@@ -151,12 +162,24 @@ namespace
151162
if(objectFileExt.empty())
152163
ssLOG_WARNING("Object file extension is empty");
153164

154-
const ghc::filesystem::path outputFilePath =
155-
(buildDir / sourceName).concat(objectFileExt);
165+
ghc::filesystem::path outputFilePath = buildDir /
166+
relativeSourcePath.parent_path() /
167+
sourceName;
168+
outputFilePath.concat(objectFileExt);
169+
170+
// Create the directory structure if it doesn't exist
171+
ghc::filesystem::create_directories(outputFilePath.parent_path(), e);
172+
if(e)
173+
{
174+
ssLOG_ERROR("Failed to create directory structure for " << outputFilePath);
175+
ssLOG_ERROR("Failed with error: " << e.message());
176+
return false;
177+
}
156178

157179
substitutionMap["{OutputFileName}"] = {sourceName};
158180
substitutionMap["{OutputFileExtension}"] = {objectFileExt};
159-
substitutionMap["{OutputFileDirectory}"] = {runcpp2::ProcessPath(buildDir.string())};
181+
substitutionMap["{OutputFileDirectory}"] =
182+
{runcpp2::ProcessPath(outputFilePath.parent_path().string())};
160183
substitutionMap["{OutputFilePath}"] =
161184
{runcpp2::ProcessPath(outputFilePath.string())};
162185
outObjectsFilesPaths.push_back(outputFilePath);
@@ -594,6 +617,7 @@ namespace
594617
}
595618

596619
bool runcpp2::CompileScriptOnly(const ghc::filesystem::path& buildDir,
620+
const ghc::filesystem::path& scriptPath,
597621
const std::vector<ghc::filesystem::path>& sourceFiles,
598622
const std::vector<bool>& sourceHasCache,
599623
const Data::ScriptInfo& scriptInfo,
@@ -618,6 +642,7 @@ bool runcpp2::CompileScriptOnly(const ghc::filesystem::path& buildDir,
618642
std::vector<ghc::filesystem::path> objectsFilesPaths;
619643

620644
if(!CompileScript( buildDir,
645+
scriptPath,
621646
sourceFilesNeededToCompile,
622647
scriptInfo,
623648
availableDependencies,
@@ -633,6 +658,7 @@ bool runcpp2::CompileScriptOnly(const ghc::filesystem::path& buildDir,
633658
}
634659

635660
bool runcpp2::CompileAndLinkScript( const ghc::filesystem::path& buildDir,
661+
const ghc::filesystem::path& scriptPath,
636662
const std::string& outputName,
637663
const std::vector<ghc::filesystem::path>& sourceFiles,
638664
const std::vector<bool>& sourceHasCache,
@@ -660,6 +686,7 @@ bool runcpp2::CompileAndLinkScript( const ghc::filesystem::path& buildDir,
660686

661687
//Compile source files that don't have cache
662688
if(!CompileScript( buildDir,
689+
scriptPath,
663690
sourceFilesNeededToCompile,
664691
scriptInfo,
665692
availableDependencies,
@@ -732,3 +759,4 @@ bool runcpp2::CompileAndLinkScript( const ghc::filesystem::path& buildDir,
732759

733760
return true;
734761
}
762+

Src/runcpp2/runcpp2.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ namespace
311311
return true;
312312
}
313313

314-
bool HasCompiledCache( const std::vector<ghc::filesystem::path>& sourceFiles,
314+
bool HasCompiledCache( const ghc::filesystem::path& scriptPath,
315+
const std::vector<ghc::filesystem::path>& sourceFiles,
315316
const ghc::filesystem::path& buildDir,
316317
const runcpp2::Data::Profile& currentProfile,
317318
std::vector<bool>& outHasCache,
@@ -335,9 +336,22 @@ namespace
335336

336337
outFinalObjectWriteTime = ghc::filesystem::file_time_type();
337338

339+
std::error_code e;
338340
for(int i = 0; i < sourceFiles.size(); ++i)
339341
{
340-
ghc::filesystem::path currentObjectFilePath = buildDir / sourceFiles.at(i).stem();
342+
ghc::filesystem::path relativeSourcePath =
343+
ghc::filesystem::relative(sourceFiles.at(i), scriptPath.parent_path(), e);
344+
345+
if(e)
346+
{
347+
ssLOG_ERROR("Failed to get relative path for " << sourceFiles.at(i).string());
348+
ssLOG_ERROR("Failed with error: " << e.message());
349+
return false;
350+
}
351+
352+
ghc::filesystem::path currentObjectFilePath = buildDir /
353+
relativeSourcePath.parent_path() /
354+
relativeSourcePath.stem();
341355
currentObjectFilePath.concat(objectExt);
342356

343357
ssLOG_DEBUG("Trying to use cache: " << sourceFiles.at(i).string());
@@ -787,7 +801,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
787801

788802
if(currentOptions.count(runcpp2::CmdOptions::RESET_CACHE) > 0 || scriptInfoChanged)
789803
sourceHasCache = std::vector<bool>(sourceFiles.size(), false);
790-
else if(!HasCompiledCache( sourceFiles,
804+
else if(!HasCompiledCache( absoluteScriptPath,
805+
sourceFiles,
791806
buildDir,
792807
profiles.at(profileIndex),
793808
sourceHasCache,
@@ -882,6 +897,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
882897
if(currentOptions.count(CmdOptions::WATCH) > 0)
883898
{
884899
if(!CompileScriptOnly( buildDir,
900+
absoluteScriptPath,
885901
sourceFiles,
886902
sourceHasCache,
887903
scriptInfo,
@@ -893,6 +909,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
893909
}
894910
}
895911
else if(!CompileAndLinkScript( buildDir,
912+
absoluteScriptPath,
896913
ghc::filesystem::path(absoluteScriptPath).stem(),
897914
sourceFiles,
898915
sourceHasCache,

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
TODO:
2-
- Group object files in folders to avoid name collision
2+
- Add ability to add defines for cross-compiler
3+
- Track include dependencies for source files
34
- Allow to specify files to copy for dependencies
45
- Allow runcpp2 to be library
5-
- Add ability to add defines for cross-compiler
66
- Ability to compile runcpp2 as single cpp
77

88
- Add wildcard support for filenames and extensions

0 commit comments

Comments
 (0)