@@ -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
596619bool 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
635660bool 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+
0 commit comments