use size_t in for loops

This commit is contained in:
SCG82 2019-12-28 11:14:35 -08:00
parent 38f7687b2c
commit 767a6f9a9b
7 changed files with 35 additions and 51 deletions

View File

@ -176,8 +176,7 @@ void Dependency::print()
{ {
std::cout << "\n * " << filename << " from " << prefix << "\n"; std::cout << "\n * " << filename << " from " << prefix << "\n";
const int symamount = symlinks.size(); for (size_t n=0; n<symlinks.size(); ++n)
for (int n=0; n<symamount; ++n)
std::cout << " symlink --> " << symlinks[n] << "\n";; std::cout << " symlink --> " << symlinks[n] << "\n";;
} }
@ -202,9 +201,8 @@ void Dependency::addSymlink(std::string s)
bool Dependency::mergeIfSameAs(Dependency& dep2) bool Dependency::mergeIfSameAs(Dependency& dep2)
{ {
if (dep2.getOriginalFileName().compare(filename) == 0) { if (dep2.getOriginalFileName().compare(filename) == 0) {
const int samount = getSymlinkAmount(); for (size_t n=0; n<symlinks.size(); ++n)
for (int n=0; n<samount; ++n) dep2.addSymlink(symlinks[n]);
dep2.addSymlink(getSymlink(n));
return true; return true;
} }
return false; return false;
@ -278,8 +276,7 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
} }
// for symlinks // for symlinks
const int symamount = symlinks.size(); for (size_t n=0; n<symlinks.size(); ++n) {
for (int n=0; n<symamount; ++n) {
command = std::string("install_name_tool -change ") + symlinks[n] + " " + getInnerPath() + " " + file_to_fix; command = std::string("install_name_tool -change ") + symlinks[n] + " " + getInnerPath() + " " + file_to_fix;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << file_to_fix << "\n"; std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << file_to_fix << "\n";
@ -297,7 +294,7 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
} }
// for symlinks // for symlinks
for (int n=0; n<symamount; ++n) { for (size_t n=0; n<symlinks.size(); ++n) {
command = std::string("install_name_tool -change ") + symlinks[n] + " " + getInnerPath() + " " + file_to_fix; command = std::string("install_name_tool -change ") + symlinks[n] + " " + getInnerPath() + " " + file_to_fix;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << file_to_fix << "\n"; std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << file_to_fix << "\n";

View File

@ -17,7 +17,7 @@ public:
std::string getInnerPath(); std::string getInnerPath();
void addSymlink(std::string s); void addSymlink(std::string s);
int getSymlinkAmount() const { return symlinks.size(); } size_t symlinksCount() const { return symlinks.size(); }
std::string getSymlink(int i) const { return symlinks[i]; } std::string getSymlink(int i) const { return symlinks[i]; }
std::string getPrefix() const { return prefix; } std::string getPrefix() const { return prefix; }
@ -37,13 +37,6 @@ private:
// installation // installation
std::string new_name; std::string new_name;
// the paths to search for dylibs, store it globally to parse the environment variables only once
// std::vector<std::string> paths;
// if some libs are missing prefixes, this will be set to true
// more stuff will then be necessary to do
// bool missing_prefixes;
}; };
#endif #endif

View File

@ -16,9 +16,10 @@
std::vector<Dependency> deps; std::vector<Dependency> deps;
std::map<std::string, std::vector<Dependency>> deps_per_file; std::map<std::string, std::vector<Dependency>> deps_per_file;
std::map<std::string, bool> deps_collected;
std::set<std::string> frameworks;
std::set<std::string> rpaths; std::set<std::string> rpaths;
std::map<std::string, std::vector<std::string>> rpaths_per_file; std::map<std::string, std::vector<std::string>> rpaths_per_file;
std::map<std::string, bool> deps_collected;
void changeLibPathsOnFile(std::string file_to_fix) void changeLibPathsOnFile(std::string file_to_fix)
{ {
@ -29,7 +30,7 @@ void changeLibPathsOnFile(std::string file_to_fix)
std::vector<Dependency> deps_in_file = deps_per_file[file_to_fix]; std::vector<Dependency> deps_in_file = deps_per_file[file_to_fix];
const int dep_amount = deps_in_file.size(); const int dep_amount = deps_in_file.size();
for (int n=0; n<dep_amount; ++n) for (size_t n=0; n<dep_amount; ++n)
deps_in_file[n].fixFileThatDependsOnMe(file_to_fix); deps_in_file[n].fixFileThatDependsOnMe(file_to_fix);
} }
@ -148,16 +149,14 @@ void addDependency(std::string path, std::string filename)
// check if this library was already added to |deps| to avoid duplicates // check if this library was already added to |deps| to avoid duplicates
bool in_deps = false; bool in_deps = false;
const int dep_amount = deps.size(); for (size_t n=0; n<deps.size(); ++n)
for (int n=0; n<dep_amount; n++)
if (dep.mergeIfSameAs(deps[n])) if (dep.mergeIfSameAs(deps[n]))
in_deps = true; in_deps = true;
// check if this library was already added to |deps_per_file[filename]| to avoid duplicates // check if this library was already added to |deps_per_file[filename]| to avoid duplicates
std::vector<Dependency> deps_in_file = deps_per_file[filename];
bool in_deps_per_file = false; bool in_deps_per_file = false;
for (int n=0; n<deps_in_file.size(); n++) for (size_t n=0; n<deps_per_file[filename].size(); ++n)
if (dep.mergeIfSameAs(deps_in_file[n])) if (dep.mergeIfSameAs(deps_per_file[filename][n]))
in_deps_per_file = true; in_deps_per_file = true;
// check if this library is in /usr/lib, /System/Library, or in ignored list // check if this library is in /usr/lib, /System/Library, or in ignored list
@ -167,10 +166,8 @@ void addDependency(std::string path, std::string filename)
if (!in_deps) if (!in_deps)
deps.push_back(dep); deps.push_back(dep);
if (!in_deps_per_file) { if (!in_deps_per_file)
deps_in_file.push_back(dep); deps_per_file[filename].push_back(dep);
deps_per_file[filename] = deps_in_file;
}
} }
// Fill |lines| with dependencies of given |filename| // Fill |lines| with dependencies of given |filename|
@ -195,8 +192,7 @@ void collectDependencies(std::string filename)
std::vector<std::string> lines; std::vector<std::string> lines;
collectDependencies(filename, lines); collectDependencies(filename, lines);
const int line_amount = lines.size(); for (size_t n=0; n<lines.size(); n++) {
for (int n=0; n<line_amount; n++) {
if (!Settings::bundleFrameworks()) if (!Settings::bundleFrameworks())
if (lines[n].find(".framework") != std::string::npos) if (lines[n].find(".framework") != std::string::npos)
continue; continue;
@ -214,13 +210,13 @@ void collectDependencies(std::string filename)
void collectSubDependencies() void collectSubDependencies()
{ {
int dep_amount = deps.size(); size_t deps_size = deps.size();
// recursively collect each dependency's dependencies // recursively collect each dependency's dependencies
while (true) { while (true) {
dep_amount = deps.size(); deps_size = deps.size();
for (int n=0; n<dep_amount; n++) { for (size_t n=0; n<deps_size; n++) {
std::string original_path = deps[n].getOriginalPath(); std::string original_path = deps[n].getOriginalPath();
if (Settings::verboseOutput()) if (Settings::verboseOutput())
std::cout << "original path: " << original_path << std::endl; std::cout << "original path: " << original_path << std::endl;
@ -232,8 +228,7 @@ void collectSubDependencies()
std::vector<std::string> lines; std::vector<std::string> lines;
collectDependencies(original_path, lines); collectDependencies(original_path, lines);
const int line_amount = lines.size(); for (size_t n=0; n<lines.size(); ++n) {
for (int n=0; n<line_amount; n++) {
if (!Settings::bundleFrameworks()) if (!Settings::bundleFrameworks())
if (lines[n].find(".framework") != std::string::npos) if (lines[n].find(".framework") != std::string::npos)
continue; continue;
@ -252,7 +247,7 @@ void collectSubDependencies()
} }
} }
// if no more dependencies were added on this iteration, stop searching // if no more dependencies were added on this iteration, stop searching
if (deps.size() == dep_amount) if (deps.size() == deps_size)
break; break;
} }
} }
@ -293,10 +288,10 @@ void createDestDir()
void doneWithDeps_go() void doneWithDeps_go()
{ {
const int dep_amount = deps.size(); const size_t deps_size = deps.size();
std::cout << "\n"; std::cout << "\n";
for (int n=0; n<dep_amount; n++) for (size_t n=0; n<deps_size; ++n)
deps[n].print(); deps[n].print();
std::cout << "\n"; std::cout << "\n";
@ -304,15 +299,15 @@ void doneWithDeps_go()
if (Settings::bundleLibs()) { if (Settings::bundleLibs()) {
createDestDir(); createDestDir();
for (int i=0; i<dep_amount; ++i) { for (size_t i=0; i<deps_size; ++i) {
deps[i].copyYourself(); deps[i].copyYourself();
changeLibPathsOnFile(deps[i].getInstallPath()); changeLibPathsOnFile(deps[i].getInstallPath());
fixRpathsOnFile(deps[i].getOriginalPath(), deps[i].getInstallPath()); fixRpathsOnFile(deps[i].getOriginalPath(), deps[i].getInstallPath());
} }
} }
const int fileToFixAmount = Settings::fileToFixAmount(); const size_t filesToFixSize = Settings::filesToFix().size();
for (int n=0; n<fileToFixAmount; n++) { for (size_t n=0; n<filesToFixSize; ++n) {
changeLibPathsOnFile(Settings::fileToFix(n)); changeLibPathsOnFile(Settings::fileToFix(n));
fixRpathsOnFile(Settings::fileToFix(n), Settings::fileToFix(n)); fixRpathsOnFile(Settings::fileToFix(n), Settings::fileToFix(n));
} }

View File

@ -38,9 +38,9 @@ void destFolder(std::string path)
std::vector<std::string> files; std::vector<std::string> files;
void addFileToFix(std::string path) { files.push_back(path); } void addFileToFix(std::string path) { files.push_back(path); }
int fileToFixAmount() { return files.size(); }
std::string fileToFix(const int n) { return files[n]; } std::string fileToFix(const int n) { return files[n]; }
std::vector<std::string> filesToFix() { return files; } std::vector<std::string> filesToFix() { return files; }
size_t filesToFixCount() { return files.size(); }
std::string insideLibPath() { return inside_path_str; } std::string insideLibPath() { return inside_path_str; }
void insideLibPath(std::string p) void insideLibPath(std::string p)
@ -61,8 +61,7 @@ void ignorePrefix(std::string prefix)
bool isPrefixIgnored(std::string prefix) bool isPrefixIgnored(std::string prefix)
{ {
const int prefix_amount = prefixes_to_ignore.size(); for (size_t n=0; n<prefixes_to_ignore.size(); n++) {
for (int n=0; n<prefix_amount; n++) {
if (prefix.compare(prefixes_to_ignore[n]) == 0) if (prefix.compare(prefixes_to_ignore[n]) == 0)
return true; return true;
} }
@ -88,7 +87,7 @@ bool isPrefixBundled(std::string prefix)
std::vector<std::string> searchPaths; std::vector<std::string> searchPaths;
void addSearchPath(std::string path) { searchPaths.push_back(path); } void addSearchPath(std::string path) { searchPaths.push_back(path); }
int searchPathAmount() { return searchPaths.size(); } int searchPathCount() { return searchPaths.size(); }
std::string searchPath(const int n) { return searchPaths[n]; } std::string searchPath(const int n) { return searchPaths[n]; }
bool quietOutput() { return quiet_output; } bool quietOutput() { return quiet_output; }

View File

@ -29,15 +29,15 @@ std::string destFolder();
void destFolder(std::string path); void destFolder(std::string path);
void addFileToFix(std::string path); void addFileToFix(std::string path);
int fileToFixAmount();
std::string fileToFix(int n); std::string fileToFix(int n);
std::vector<std::string> filesToFix(); std::vector<std::string> filesToFix();
size_t filesToFixCount();
std::string insideLibPath(); std::string insideLibPath();
void insideLibPath(std::string p); void insideLibPath(std::string p);
void addSearchPath(std::string path); void addSearchPath(std::string path);
int searchPathAmount(); int searchPathCount();
std::string searchPath(int n); std::string searchPath(int n);
bool quietOutput(); bool quietOutput();

View File

@ -134,8 +134,8 @@ int systemp(const std::string& cmd)
std::string getUserInputDirForFile(const std::string& filename) std::string getUserInputDirForFile(const std::string& filename)
{ {
const int searchPathAmount = Settings::searchPathAmount(); const size_t searchPathCount = Settings::searchPathCount();
for (int n=0; n<searchPathAmount; ++n) { for (size_t n=0; n<searchPathCount; ++n) {
auto searchPath = Settings::searchPath(n); auto searchPath = Settings::searchPath(n);
if (!searchPath.empty() && searchPath[searchPath.size()-1] != '/') if (!searchPath.empty() && searchPath[searchPath.size()-1] != '/')
searchPath += "/"; searchPath += "/";

View File

@ -115,15 +115,15 @@ int main (int argc, char * const argv[])
} }
} }
if (!Settings::bundleLibs() && Settings::fileToFixAmount() < 1) { if (!Settings::bundleLibs() && Settings::filesToFixCount() < 1) {
showHelp(); showHelp();
exit(0); exit(0);
} }
std::cout << "* Collecting dependencies...\n"; std::cout << "* Collecting dependencies...\n";
const int amount = Settings::fileToFixAmount(); const size_t count = Settings::filesToFixCount();
for (int n=0; n<amount; n++) for (size_t n=0; n<count; ++n)
collectDependencies(Settings::fileToFix(n)); collectDependencies(Settings::fileToFix(n));
collectSubDependencies(); collectSubDependencies();