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}
0 commit comments