@@ -23,17 +23,18 @@ namespace
2323 bool CreateLocalBuildDirectory ( const std::string& scriptPath,
2424 ghc::filesystem::path& outBuildPath)
2525 {
26- std::string scriptDirectory = ghc::filesystem::path (scriptPath).parent_path ().string ();
26+ // Get the current working directory
27+ std::string currentWorkingDir = ghc::filesystem::current_path ().string ();
2728
28- // Create the runcpp2 directory
29- std::string runcpp2Dir = scriptDirectory + " /.runcpp2" ;
29+ // Create the . runcpp2 directory in the current working directory
30+ std::string runcpp2Dir = currentWorkingDir + " /.runcpp2" ;
3031
3132 std::error_code e;
3233 if (!ghc::filesystem::exists (runcpp2Dir, e))
3334 {
3435 if (!ghc::filesystem::create_directory (runcpp2Dir, e))
3536 {
36- ssLOG_ERROR (" Failed to create runcpp2 directory" );
37+ ssLOG_ERROR (" Failed to create . runcpp2 directory in the current working directory" );
3738 return false ;
3839 }
3940 }
@@ -481,6 +482,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
481482 const std::vector<std::string>& runArgs,
482483 const Data::ScriptInfo* lastScriptInfo,
483484 Data::ScriptInfo& outScriptInfo,
485+ const std::string& buildOutputDir,
484486 int & returnStatus)
485487{
486488 INTERNAL_RUNCPP2_SAFE_START ();
@@ -559,6 +561,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
559561 #endif
560562
561563 // Parsing the script, setting up dependencies, compiling and linking
564+ std::vector<std::string> filesToCopyPaths;
562565 {
563566 // Check if there's script info as yaml file instead
564567 std::error_code e;
@@ -611,9 +614,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
611614
612615 // Create build directory
613616 {
614- const bool localBuildDir = currentOptions.count (CmdOptions::LOCAL) > 0 ;
615617 bool createdBuildDir = false ;
616- if (localBuildDir )
618+ if (currentOptions. count (CmdOptions::LOCAL) > 0 )
617619 {
618620 if (CreateLocalBuildDirectory (absoluteScriptPath, buildDir))
619621 createdBuildDir = true ;
@@ -796,18 +798,70 @@ runcpp2::StartPipeline( const std::string& scriptPath,
796798 return PipelineResult::UNEXPECTED_FAILURE;
797799 }
798800
799- // Update finalObjectWriteTime
801+ std::vector<std::string> linkFilesPaths;
802+ std::unordered_set<std::string> linkExtensions;
803+ const Data::FilesTypesInfo& currentFileTypes = profiles.at (profileIndex).FilesTypes ;
804+
805+ // Populate the set of link extensions
806+ if (runcpp2::HasValueFromPlatformMap (currentFileTypes.StaticLinkFile .Extension ))
807+ {
808+ linkExtensions.insert (*runcpp2::GetValueFromPlatformMap (currentFileTypes.StaticLinkFile
809+ .Extension ));
810+ }
811+ if (runcpp2::HasValueFromPlatformMap (currentFileTypes.SharedLinkFile .Extension ))
812+ {
813+ linkExtensions.insert (*runcpp2::GetValueFromPlatformMap (currentFileTypes.SharedLinkFile
814+ .Extension ));
815+ }
816+ if (runcpp2::HasValueFromPlatformMap (currentFileTypes.ObjectLinkFile .Extension ))
817+ {
818+ linkExtensions.insert (*runcpp2::GetValueFromPlatformMap (currentFileTypes.ObjectLinkFile
819+ .Extension ));
820+ }
821+
822+ // Separate the copied files from dependencies into files to link and files to copy
800823 for (int i = 0 ; i < copiedBinariesPaths.size (); ++i)
801824 {
802- if (!ghc::filesystem::exists (copiedBinariesPaths.at (i), e))
825+ ghc::filesystem::path filePath (copiedBinariesPaths.at (i));
826+ std::string extension = filePath.extension ().string ();
827+
828+ // Check if the file is a link file based on its extension
829+ if (linkExtensions.find (extension) != linkExtensions.end ())
803830 {
804- ssLOG_ERROR (copiedBinariesPaths.at (i) << " reported as cached but doesn't exist" );
831+ linkFilesPaths.push_back (copiedBinariesPaths.at (i));
832+
833+ // Special case when SharedLinkFile and SharedLibraryFile share the same extension
834+ if ( runcpp2::HasValueFromPlatformMap (currentFileTypes.SharedLibraryFile .Extension ) &&
835+ *runcpp2::GetValueFromPlatformMap (currentFileTypes .SharedLibraryFile
836+ .Extension ) == extension)
837+ {
838+ filesToCopyPaths.push_back (copiedBinariesPaths.at (i));
839+ }
840+ }
841+ else
842+ filesToCopyPaths.push_back (copiedBinariesPaths.at (i));
843+ }
844+
845+ ssLOG_DEBUG (" Files to link:" );
846+ for (int i = 0 ; i < linkFilesPaths.size (); ++i)
847+ ssLOG_DEBUG (" " << linkFilesPaths[i]);
848+
849+ ssLOG_INFO (" Files to copy:" );
850+ for (int i = 0 ; i < filesToCopyPaths.size (); ++i)
851+ ssLOG_INFO (" " << filesToCopyPaths[i]);
852+
853+ // Update finalObjectWriteTime
854+ for (int i = 0 ; i < linkFilesPaths.size (); ++i)
855+ {
856+ if (!ghc::filesystem::exists (linkFilesPaths.at (i), e))
857+ {
858+ ssLOG_ERROR (linkFilesPaths.at (i) << " reported as cached but doesn't exist" );
805859 return PipelineResult::UNEXPECTED_FAILURE;
806860 }
807861
808862 ghc::filesystem::file_time_type lastWriteTime =
809- ghc::filesystem::last_write_time (copiedBinariesPaths .at (i), e);
810-
863+ ghc::filesystem::last_write_time (linkFilesPaths .at (i), e);
864+
811865 if (lastWriteTime > finalObjectWriteTime)
812866 finalObjectWriteTime = lastWriteTime;
813867 }
@@ -819,11 +873,11 @@ runcpp2::StartPipeline( const std::string& scriptPath,
819873 currentOptions.count (CmdOptions::EXECUTABLE) > 0 ,
820874 scriptName,
821875 exeExt,
822- copiedBinariesPaths ,
876+ linkFilesPaths ,
823877 finalObjectWriteTime))
824878 {
825879 for (int i = 0 ; i < cachedObjectsFiles.size (); ++i)
826- copiedBinariesPaths .push_back (cachedObjectsFiles.at (i));
880+ linkFilesPaths .push_back (cachedObjectsFiles.at (i));
827881
828882 if (currentOptions.count (CmdOptions::WATCH) > 0 )
829883 {
@@ -845,7 +899,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
845899 scriptInfo,
846900 availableDependencies,
847901 profiles.at (profileIndex),
848- copiedBinariesPaths ,
902+ linkFilesPaths ,
849903 currentOptions.count (CmdOptions::EXECUTABLE) > 0 ,
850904 exeExt))
851905 {
@@ -896,26 +950,66 @@ runcpp2::StartPipeline( const std::string& scriptPath,
896950 return PipelineResult::COMPILE_LINK_FAILED;
897951 }
898952
899- if (currentOptions.count (CmdOptions::EXECUTABLE) > 0 )
953+ if (currentOptions.count (CmdOptions::BUILD) == 0 )
900954 {
901- // Running the script
902- if (!RunCompiledScript (target, absoluteScriptPath, runArgs, returnStatus))
955+ if (currentOptions.count (CmdOptions::EXECUTABLE) > 0 )
956+ {
957+ // Running the script
958+ if (!RunCompiledScript (target, absoluteScriptPath, runArgs, returnStatus))
959+ {
960+ ssLOG_ERROR (" Failed to run script" );
961+ return PipelineResult::RUN_SCRIPT_FAILED;
962+ }
963+
964+ return PipelineResult::SUCCESS;
965+ }
966+ // Load the shared library and run it
967+ else
903968 {
904- ssLOG_ERROR (" Failed to run script" );
905- return PipelineResult::RUN_SCRIPT_FAILED;
969+ if (!RunCompiledSharedLib (absoluteScriptPath, target, runArgs, returnStatus))
970+ {
971+ ssLOG_ERROR (" Failed to run script" );
972+ return PipelineResult::RUN_SCRIPT_FAILED;
973+ }
974+
975+ return PipelineResult::SUCCESS;
906976 }
907-
908- return PipelineResult::SUCCESS;
909977 }
910- // Load the shared library and run it
911978 else
912979 {
913- if (!RunCompiledSharedLib (absoluteScriptPath, target, runArgs, returnStatus))
980+ std::error_code e;
981+
982+ // Copy the output file
914983 {
915- ssLOG_ERROR (" Failed to run script" );
916- return PipelineResult::RUN_SCRIPT_FAILED;
984+ ghc::filesystem::path destFile =
985+ ghc::filesystem::path (buildOutputDir) / target.filename ();
986+
987+ ghc::filesystem::copy ( target, destFile,
988+ ghc::filesystem::copy_options::overwrite_existing, e);
989+ if (e)
990+ {
991+ ssLOG_ERROR (" Failed to copy output file: " << e.message ());
992+ return PipelineResult::UNEXPECTED_FAILURE;
993+ }
917994 }
918-
995+
996+ // Copy the files that need to be copied
997+ for (int i = 0 ; i < filesToCopyPaths.size (); ++i)
998+ {
999+ ghc::filesystem::path srcFile (filesToCopyPaths.at (i));
1000+ ghc::filesystem::path destFile =
1001+ ghc::filesystem::path (buildOutputDir) / srcFile.filename ();
1002+ ghc::filesystem::copy ( srcFile, destFile,
1003+ ghc::filesystem::copy_options::overwrite_existing, e);
1004+ if (e)
1005+ {
1006+ ssLOG_ERROR (" Failed to copy file " << filesToCopyPaths.at (i) << " : " <<
1007+ e.message ());
1008+ return PipelineResult::UNEXPECTED_FAILURE;
1009+ }
1010+ }
1011+
1012+ ssLOG_INFO (" Build completed. Files copied to " << buildOutputDir);
9191013 return PipelineResult::SUCCESS;
9201014 }
9211015 }
0 commit comments