22
33#include " runcpp2/ParseUtil.hpp"
44#include " runcpp2/PlatformUtil.hpp"
5-
5+ # include " runcpp2/StringUtil.hpp "
66#include " ssLogger/ssLog.hpp"
77#include " tinydir.h"
88#include " cfgpath.h"
@@ -748,10 +748,6 @@ bool runcpp2::CopyDependenciesBinaries( const std::string& scriptPath,
748748 return false ;
749749 }
750750
751- // auto dirEntry = ghc::filesystem::directory_entry(searchPath, _);
752-
753- // auto dirIt = ghc::filesystem::directory_iterator(searchPath, _);
754-
755751 for (auto it : ghc::filesystem::directory_iterator (searchPath, _))
756752 {
757753 if (it.is_directory ())
@@ -803,6 +799,12 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
803799 const CompilerProfile& profile)
804800{
805801 // TODO(NOW)
802+ std::string scriptDirectory = ghc::filesystem::path (scriptPath).parent_path ().string ();
803+ std::string scriptName = ghc::filesystem::path (scriptPath).stem ().string ();
804+ std::string runcpp2ScriptDir = scriptDirectory + " /.runcpp2" ;
805+ std::vector<std::string> platformNames = Internal::GetPlatformNames ();
806+
807+
806808
807809 std::string compileCommand = profile.Compiler .Executable + " " +
808810 profile.Compiler .CompileArgs ;
@@ -819,13 +821,26 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
819821
820822 if (scriptInfo.OverrideCompileFlags .find (profile.Name ) != scriptInfo.OverrideCompileFlags .end ())
821823 {
822- // std::vector<std::string> compileArgsToRemove = scriptInfo .OverrideCompileFlags
823- // .at(profile.Name)
824- // .Remove.
825-
826-
824+ std::vector<std::string> compileArgsToRemove;
825+ Internal::SplitString ( scriptInfo.OverrideCompileFlags .at (profile.Name ).Remove ,
826+ " " ,
827+ compileArgsToRemove);
827828
829+ for (int i = 0 ; i < compileArgsToRemove.size (); ++i)
830+ {
831+ std::size_t foundIndex = compileArgs.find (compileArgsToRemove.at (i));
832+
833+ if (foundIndex != std::string::npos)
834+ {
835+ compileArgs.erase (foundIndex, compileArgsToRemove.at (i).size () + 1 );
836+ continue ;
837+ }
838+
839+ ssLOG_WARNING (" Compile flag to remove not found: " << compileArgsToRemove.at (i));
840+ }
828841
842+ Internal::TrimRight (compileArgs);
843+ compileArgs += " " + scriptInfo.OverrideCompileFlags .at (profile.Name ).Append ;
829844 }
830845
831846 compileCommand.replace ( foundIndex,
@@ -838,8 +853,6 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
838853 return false ;
839854 }
840855
841-
842- #if 0
843856 // Replace {InputFile}
844857 const std::string inputFileSubstitution = " {InputFile}" ;
845858 foundIndex = compileCommand.find (inputFileSubstitution);
@@ -853,10 +866,21 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
853866
854867 // Replace {ObjectFile}
855868 const std::string objectFileSubstitution = " {ObjectFile}" ;
869+ std::string objectFileExt;
870+
871+ for (int i = 0 ; i < platformNames.size (); ++i)
872+ {
873+ if (profile.ObjectFileExtensions .find (platformNames.at (i)) != profile.ObjectFileExtensions .end ())
874+ {
875+ objectFileExt = profile.ObjectFileExtensions .at (platformNames.at (i));
876+ break ;
877+ }
878+ }
879+
856880 foundIndex = compileCommand.find (objectFileSubstitution);
857881 if (foundIndex != std::string::npos)
858882 {
859- std::string objectFileName = scriptDirectory + "/.runcpp2 /" + scriptName + "." + objectFileExt;
883+ std::string objectFileName = runcpp2ScriptDir + " /" + scriptName + " ." + objectFileExt;
860884 compileCommand.replace (foundIndex, objectFileSubstitution.size (), objectFileName);
861885 }
862886 else
@@ -867,13 +891,62 @@ bool runcpp2::CompileAndLinkScript( const std::string& scriptPath,
867891
868892 // Compile the script
869893 ssLOG_INFO (" running compile command: " << compileCommand);
870- //TODO(NOW): Replace this with system2
871- if(std::system(compileCommand.c_str()) != 0)
894+
895+ System2CommandInfo compileCommandInfo;
896+ SYSTEM2_RESULT result = System2Run (compileCommand.c_str (), &compileCommandInfo);
897+
898+ if (result != SYSTEM2_RESULT_SUCCESS)
872899 {
873- ssLOG_ERROR("Failed to run compile script with command: " << compileCommand);
900+ ssLOG_ERROR (" System2Run failed with result: " << result);
901+ return false ;
902+ }
903+
904+ std::vector<char > output;
905+ output.resize (4096 );
906+ output.back () = ' \0 ' ;
907+
908+ uint32_t byteRead = 0 ;
909+
910+ do
911+ {
912+ output.resize (output.size () + 4096 );
913+ output.back () = ' \0 ' ;
914+
915+ result = System2ReadFromOutput ( &compileCommandInfo,
916+ output.data () + output.size () - 4096 ,
917+ 4096 - 1 ,
918+ &byteRead);
919+ }
920+ while (result == SYSTEM2_RESULT_READ_NOT_FINISHED);
921+
922+ output.at (byteRead) = ' \0 ' ;
923+
924+ if (result != SYSTEM2_RESULT_SUCCESS)
925+ {
926+ ssLOG_ERROR (" Failed to read from output with result: " << result);
874927 return false ;
875928 }
876- #endif
929+
930+ ssLOG_DEBUG (" Compile Output: \n " << output.data ());
931+
932+ int statusCode = 0 ;
933+ result = System2GetCommandReturnValueSync (&compileCommandInfo, &statusCode);
934+
935+ if (result != SYSTEM2_RESULT_SUCCESS)
936+ {
937+ ssLOG_ERROR (" System2GetCommandReturnValueSync failed with result: " << result);
938+ return false ;
939+ }
940+
941+ if (statusCode != 0 )
942+ {
943+ ssLOG_ERROR (" Compile command returned with non-zero status code: " << statusCode);
944+ return false ;
945+ }
946+
947+ // TODO(NOW): Link
948+
949+
877950 return true ;
878951}
879952
0 commit comments