Skip to content

Commit 63131ae

Browse files
Merge pull request #17 from Neko-Box-Coder/CopyFiles
Ability to copy files for dependencies
2 parents 26b750d + 201c13c commit 63131ae

File tree

11 files changed

+194
-16
lines changed

11 files changed

+194
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ add_executable(runcpp2
8585
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/FilesTypesInfo.cpp"
8686
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/StageInfo.cpp"
8787
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/ProfilesDefines.cpp"
88+
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/FilesToCopyInfo.cpp"
8889
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/ProfileHelper.cpp"
8990
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/CompilingLinking.cpp"
9091
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/ConfigParsing.cpp"

DefaultYAMLs/DefaultScriptInfo.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,20 @@
134134
# Linux:
135135
# "g++":
136136
# - "sudo apt purge MyLibrary"
137+
#
138+
# # (Optional) Files to be copied for each platform and profile
139+
# FilesToCopy:
140+
# # Target Platform (Default, Windows, Linux, MacOS, or Unix)
141+
# Default:
142+
# # Profile name (e.g., "g++", "clang++", "msvc", or "Default" for any profile)
143+
# "Default":
144+
# # List of files to copy (relative to the dependency folder)
145+
# - "assets/textures/sprite.png"
146+
# Windows:
147+
# "msvc":
148+
# - "assets/textures/sprite.png"
149+
# - "assets/fonts/windows_specific_font.ttf"
150+
# Linux:
151+
# "g++":
152+
# - "assets/textures/sprite.png"
153+
# - "assets/shaders/linux_optimized_shader.glsl"

Examples/test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"Default":
4242
- "cd build && cmake .. -DssLOG_BUILD_TYPE=SHARED"
4343
- "cd build && cmake --build . --config Release -j 16"
44+
FilesToCopy:
45+
# Target Platform (Default, Windows, Linux, MacOS, or Unix)
46+
Default:
47+
"Default":
48+
- "./Include/ssLogger/ssLog.hpp"
4449
*/
4550

4651

Include/runcpp2/Data/DependencyInfo.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "runcpp2/Data/DependencyLinkProperty.hpp"
77
#include "runcpp2/Data/DependencyCommands.hpp"
88
#include "runcpp2/Data/ParseCommon.hpp"
9-
9+
#include "runcpp2/Data/FilesToCopyInfo.hpp"
1010
#include "ryml.hpp"
1111

1212
#include <string>
@@ -29,6 +29,7 @@ namespace runcpp2
2929
std::unordered_map<PlatformName, DependencyCommands> Setup;
3030
std::unordered_map<PlatformName, DependencyCommands> Cleanup;
3131
std::unordered_map<PlatformName, DependencyCommands> Build;
32+
std::unordered_map<PlatformName, FilesToCopyInfo> FilesToCopy;
3233

3334
bool ParseYAML_Node(ryml::ConstNodeRef& node);
3435
std::string ToString(std::string indentation) const;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef RUNCPP2_DATA_FILES_TO_COPY_INFO_HPP
2+
#define RUNCPP2_DATA_FILES_TO_COPY_INFO_HPP
3+
4+
#include "runcpp2/Data/ParseCommon.hpp"
5+
#include "ryml.hpp"
6+
#include <unordered_map>
7+
#include <vector>
8+
#include <string>
9+
10+
namespace runcpp2
11+
{
12+
namespace Data
13+
{
14+
struct FilesToCopyInfo
15+
{
16+
std::unordered_map<ProfileName, std::vector<std::string>> ProfileFiles;
17+
18+
bool ParseYAML_Node(const ryml::ConstNodeRef& node);
19+
std::string ToString(std::string indentation) const;
20+
};
21+
}
22+
}
23+
24+
#endif

Src/runcpp2/Data/DependencyInfo.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ bool runcpp2::Data::DependencyInfo::ParseYAML_Node(ryml::ConstNodeRef& node)
1717
NodeRequirement("LinkProperties", ryml::NodeType_e::MAP, false, false),
1818
NodeRequirement("Setup", ryml::NodeType_e::MAP, false, true),
1919
NodeRequirement("Cleanup", ryml::NodeType_e::MAP, false, true),
20-
NodeRequirement("Build", ryml::NodeType_e::MAP, false, true)
20+
NodeRequirement("Build", ryml::NodeType_e::MAP, false, true),
21+
NodeRequirement("FilesToCopy", ryml::NodeType_e::MAP, false, true)
2122
};
2223

2324
if(!CheckNodeRequirements(node, requirements))
@@ -141,6 +142,24 @@ bool runcpp2::Data::DependencyInfo::ParseYAML_Node(ryml::ConstNodeRef& node)
141142
}
142143
}
143144

145+
if(ExistAndHasChild(node, "FilesToCopy"))
146+
{
147+
for(int i = 0; i < node["FilesToCopy"].num_children(); ++i)
148+
{
149+
FilesToCopyInfo currentFilesToCopy;
150+
ryml::ConstNodeRef currentFilesToCopyNode = node["FilesToCopy"][i];
151+
PlatformName platform = GetKey(currentFilesToCopyNode);
152+
153+
if(!currentFilesToCopy.ParseYAML_Node(currentFilesToCopyNode))
154+
{
155+
ssLOG_ERROR("DependencyInfo: Failed to parse FilesToCopy");
156+
return false;
157+
}
158+
159+
FilesToCopy[platform] = currentFilesToCopy;
160+
}
161+
}
162+
144163
return true;
145164

146165
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
@@ -200,5 +219,12 @@ std::string runcpp2::Data::DependencyInfo::ToString(std::string indentation) con
200219
out += it->second.ToString(indentation + " ");
201220
}
202221

222+
out += indentation + " FilesToCopy:\n";
223+
for(auto it = FilesToCopy.begin(); it != FilesToCopy.end(); ++it)
224+
{
225+
out += indentation + " " + it->first + ":\n";
226+
out += it->second.ToString(indentation + " ");
227+
}
228+
203229
return out;
204230
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "runcpp2/Data/FilesToCopyInfo.hpp"
2+
#include "runcpp2/ParseUtil.hpp"
3+
#include "ssLogger/ssLog.hpp"
4+
5+
bool runcpp2::Data::FilesToCopyInfo::ParseYAML_Node(const ryml::ConstNodeRef& node)
6+
{
7+
INTERNAL_RUNCPP2_SAFE_START();
8+
9+
if(!node.is_map())
10+
{
11+
ssLOG_ERROR("FilesToCopyInfo: Node is not a Map");
12+
return false;
13+
}
14+
15+
for(int i = 0; i < node.num_children(); i++)
16+
{
17+
if(!node[i].is_seq())
18+
{
19+
ssLOG_ERROR("FilesToCopyInfo: Node is not a sequence");
20+
return false;
21+
}
22+
23+
ProfileName profile = GetKey(node[i]);
24+
for(int j = 0; j < node[i].num_children(); j++)
25+
ProfileFiles[profile].push_back(GetValue(node[i][j]));
26+
}
27+
28+
return true;
29+
30+
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
31+
}
32+
33+
std::string runcpp2::Data::FilesToCopyInfo::ToString(std::string indentation) const
34+
{
35+
std::string out;
36+
for(auto it = ProfileFiles.begin(); it != ProfileFiles.end(); it++)
37+
{
38+
out += indentation + it->first + ":\n";
39+
for(int i = 0; i < it->second.size(); i++)
40+
out += indentation + "- " + it->second[i] + "\n";
41+
}
42+
43+
return out;
44+
}

Src/runcpp2/DependenciesHelper.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,66 @@ bool runcpp2::CopyDependenciesBinaries( const ghc::filesystem::path& buildDir,
493493

494494
for(int i = 0; i < availableDependencies.size(); ++i)
495495
{
496+
std::vector<std::string> currentProfileNames;
497+
profile.GetNames(currentProfileNames);
498+
499+
if(runcpp2::HasValueFromPlatformMap(availableDependencies.at(i)->FilesToCopy))
500+
{
501+
const runcpp2::Data::FilesToCopyInfo& filesToCopy =
502+
*runcpp2::GetValueFromPlatformMap(availableDependencies.at(i)->FilesToCopy);
503+
504+
std::string profileNameToUse;
505+
for(int j = 0; j < currentProfileNames.size(); ++j)
506+
{
507+
if( filesToCopy.ProfileFiles.find(currentProfileNames.at(j)) !=
508+
filesToCopy.ProfileFiles.end())
509+
{
510+
profileNameToUse = currentProfileNames.at(j);
511+
break;
512+
}
513+
}
514+
515+
if(!profileNameToUse.empty())
516+
{
517+
const std::vector<std::string>& filesToCopyForProfile =
518+
filesToCopy.ProfileFiles.at(profileNameToUse);
519+
520+
for(int j = 0; j < filesToCopyForProfile.size(); ++j)
521+
{
522+
ghc::filesystem::path srcPath =
523+
ghc::filesystem::path(dependenciesCopiesPaths.at(i)) /
524+
filesToCopyForProfile.at(j);
525+
526+
ghc::filesystem::path destPath =
527+
buildDir / ghc::filesystem::path(filesToCopyForProfile.at(j)).filename();
528+
529+
std::error_code e;
530+
//TODO: Maybe we can check if destPath timestamp is newer and avoid copy?
531+
if(ghc::filesystem::exists(srcPath, e))
532+
{
533+
ghc::filesystem::copy( srcPath,
534+
destPath,
535+
ghc::filesystem::copy_options::overwrite_existing,
536+
e);
537+
538+
if(e)
539+
{
540+
ssLOG_ERROR("Failed to copy file from " << srcPath.string() <<
541+
" to " << destPath.string());
542+
ssLOG_ERROR("Error: " << e.message());
543+
return false;
544+
}
545+
546+
ssLOG_INFO("Copied from " << srcPath.string());
547+
ssLOG_INFO("Copied to " << destPath.string());
548+
outCopiedBinariesPaths.push_back(runcpp2::ProcessPath(destPath));
549+
}
550+
else
551+
ssLOG_WARNING("File not found: " << srcPath.string());
552+
}
553+
}
554+
}
555+
496556
std::vector<std::string> extensionsToCopy;
497557

498558
//Get all the file extensions to copy
@@ -525,9 +585,6 @@ bool runcpp2::CopyDependenciesBinaries( const ghc::filesystem::path& buildDir,
525585
const PropertyMap& linkProperties = availableDependencies.at(i)->LinkProperties;
526586

527587
//See if we can find the link properties with the profile name
528-
std::vector<std::string> currentProfileNames;
529-
profile.GetNames(currentProfileNames);
530-
531588
auto foundPropertyIt = linkProperties.end();
532589
for(int j = 0; j < currentProfileNames.size(); ++j)
533590
{

Src/runcpp2/PlatformUtil.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ bool runcpp2::RunCommandAndGetOutput( const std::string& command,
104104
sys2Result =
105105
System2ReadFromOutput( &commandInfo,
106106
const_cast<char*>(outOutput.data()) + outOutput.size() - 4096,
107-
4096 - 1,
107+
4096,
108108
&byteRead);
109109

110-
outOutput.resize(outOutput.size() - 4096 + byteRead + 1);
111-
outOutput.back() = '\0';
110+
outOutput.resize(outOutput.size() + byteRead);
112111
}
113112
while(sys2Result == SYSTEM2_RESULT_READ_NOT_FINISHED);
114113

Src/runcpp2/runcpp2.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -857,13 +857,15 @@ runcpp2::StartPipeline( const std::string& scriptPath,
857857
filesToCopyPaths.push_back(copiedBinariesPaths.at(i));
858858
}
859859

860-
ssLOG_DEBUG("Files to link:");
861-
for(int i = 0; i < linkFilesPaths.size(); ++i)
862-
ssLOG_DEBUG(" " << linkFilesPaths[i]);
860+
{
861+
ssLOG_DEBUG("Files to link:");
862+
for(int i = 0; i < linkFilesPaths.size(); ++i)
863+
ssLOG_DEBUG(" " << linkFilesPaths[i]);
863864

864-
ssLOG_INFO("Files to copy:");
865-
for(int i = 0; i < filesToCopyPaths.size(); ++i)
866-
ssLOG_INFO(" " << filesToCopyPaths[i]);
865+
ssLOG_DEBUG("Files to copy:");
866+
for(int i = 0; i < filesToCopyPaths.size(); ++i)
867+
ssLOG_DEBUG(" " << filesToCopyPaths[i]);
868+
}
867869

868870
// Update finalObjectWriteTime
869871
for(int i = 0; i < linkFilesPaths.size(); ++i)
@@ -1008,6 +1010,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
10081010
ssLOG_ERROR("Failed to copy output file: " << e.message());
10091011
return PipelineResult::UNEXPECTED_FAILURE;
10101012
}
1013+
1014+
ssLOG_DEBUG("Copied " << target << " to " << destFile);
10111015
}
10121016

10131017
// Copy the files that need to be copied
@@ -1024,6 +1028,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
10241028
e.message());
10251029
return PipelineResult::UNEXPECTED_FAILURE;
10261030
}
1031+
1032+
ssLOG_DEBUG("Copied " << srcFile << " to " << destFile);
10271033
}
10281034

10291035
ssLOG_BASE("Build completed. Files copied to " << buildOutputDir);

0 commit comments

Comments
 (0)