code cleanup

This commit is contained in:
SCG82 2020-01-06 01:55:26 -08:00
parent f02982dfa9
commit 7f41661132
8 changed files with 66 additions and 73 deletions

View File

@ -52,7 +52,8 @@ Dependency::Dependency(std::string path, const std::string& dependent_file) : is
prefix += "/"; prefix += "/";
// check if this dependency is in /usr/lib, /System/Library, or in ignored list // check if this dependency is in /usr/lib, /System/Library, or in ignored list
if (!Settings::isPrefixBundled(prefix)) return; if (!Settings::isPrefixBundled(prefix))
return;
if (original_file.find(".framework") != std::string::npos) { if (original_file.find(".framework") != std::string::npos) {
is_framework = true; is_framework = true;
@ -70,15 +71,13 @@ Dependency::Dependency(std::string path, const std::string& dependent_file) : is
// check if the lib is in a known location // check if the lib is in a known location
if (prefix.empty() || !fileExists(prefix+filename)) { if (prefix.empty() || !fileExists(prefix+filename)) {
std::vector<std::string> search_paths = Settings::searchPaths();
// the paths contains at least /usr/lib so if it is empty we have not initialized it // the paths contains at least /usr/lib so if it is empty we have not initialized it
size_t search_path_count = Settings::searchPathCount(); if (search_paths.empty())
if (search_path_count == 0)
initSearchPaths(); initSearchPaths();
// check if file is contained in one of the paths // check if file is contained in one of the paths
search_path_count = Settings::searchPathCount(); for (const auto& search_path : search_paths) {
for (size_t i=0; i<search_path_count; ++i) {
std::string search_path = Settings::searchPath(i);
if (fileExists(search_path+filename)) { if (fileExists(search_path+filename)) {
warning_msg += "FOUND " + filename + " in " + search_path + "\n"; warning_msg += "FOUND " + filename + " in " + search_path + "\n";
prefix = search_path; prefix = search_path;
@ -98,7 +97,7 @@ Dependency::Dependency(std::string path, const std::string& dependent_file) : is
if (Settings::verboseOutput()) if (Settings::verboseOutput())
std::cout << " path: " << (prefix+filename) << std::endl; std::cout << " path: " << (prefix+filename) << std::endl;
Settings::missingPrefixes(true); Settings::missingPrefixes(true);
Settings::addSearchPath(getUserInputDirForFile(filename)); Settings::addSearchPath(getUserInputDirForFile(filename, dependent_file));
} }
new_name = filename; new_name = filename;

View File

@ -40,8 +40,6 @@ private:
// installation // installation
std::string new_name; std::string new_name;
void print();
}; };
#endif #endif

View File

@ -25,33 +25,34 @@ std::set<std::string> frameworks;
std::set<std::string> rpaths; std::set<std::string> rpaths;
bool qt_plugins_called = false; bool qt_plugins_called = false;
void addDependency(std::string path, const std::string& dependent_file) void addDependency(const std::string& path, const std::string& dependent_file)
{ {
Dependency dep(std::move(path), dependent_file); Dependency dependency(path, dependent_file);
// 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;
for (auto& n : deps) { for (auto& dep : deps) {
if (dep.mergeIfSameAs(n)) if (dependency.mergeIfSameAs(dep))
in_deps = true; in_deps = true;
} }
// check if this library was already added to |deps_per_file[dependent_file]| to avoid duplicates // check if this library was already added to |deps_per_file[dependent_file]| to avoid duplicates
bool in_deps_per_file = false; bool in_deps_per_file = false;
for (auto& n : deps_per_file[dependent_file]) { for (auto& dep : deps_per_file[dependent_file]) {
if (dep.mergeIfSameAs(n)) if (dependency.mergeIfSameAs(dep))
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
if (!Settings::isPrefixBundled(dep.getPrefix())) return; if (!Settings::isPrefixBundled(dependency.getPrefix()))
return;
if (!in_deps && dep.isFramework()) if (!in_deps && dependency.isFramework())
frameworks.insert(dep.getOriginalPath()); frameworks.insert(dependency.getOriginalPath());
if (!in_deps) if (!in_deps)
deps.push_back(dep); deps.push_back(dependency);
if (!in_deps_per_file) if (!in_deps_per_file)
deps_per_file[dependent_file].push_back(dep); deps_per_file[dependent_file].push_back(dependency);
} }
void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines) void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines)
@ -72,7 +73,8 @@ void collectDependenciesForFile(const std::string& dependent_file)
collectRpathsForFilename(dependent_file); collectRpathsForFilename(dependent_file);
for (const auto& line : lines) { for (const auto& line : lines) {
if (!Settings::isPrefixBundled(line)) continue; // skip system/ignored prefixes if (!Settings::isPrefixBundled(line))
continue; // skip system/ignored prefixes
addDependency(line, dependent_file); addDependency(line, dependent_file);
} }
deps_collected[dependent_file] = true; deps_collected[dependent_file] = true;
@ -119,12 +121,14 @@ void collectSubDependencies()
collectRpathsForFilename(original_path); collectRpathsForFilename(original_path);
for (const auto& line : lines) { for (const auto& line : lines) {
if (!Settings::isPrefixBundled(line)) continue; // skip system/ignored prefixes if (!Settings::isPrefixBundled(line))
continue; // skip system/ignored prefixes
addDependency(line, original_path); addDependency(line, original_path);
} }
} }
// 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() == deps_size) break; if (deps.size() == deps_size)
break;
} }
if (Settings::verboseOutput()) { if (Settings::verboseOutput()) {
@ -180,17 +184,17 @@ void bundleDependencies()
// copy & fix up dependencies // copy & fix up dependencies
if (Settings::bundleLibs()) { if (Settings::bundleLibs()) {
createDestDir(); createDestDir();
for (auto& dep : deps) { for (const auto& dep : deps) {
dep.copyToAppBundle(); dep.copyToAppBundle();
changeLibPathsOnFile(dep.getInstallPath()); changeLibPathsOnFile(dep.getInstallPath());
fixRpathsOnFile(dep.getOriginalPath(), dep.getInstallPath()); fixRpathsOnFile(dep.getOriginalPath(), dep.getInstallPath());
} }
} }
// fix up selected files // fix up selected files
const auto files_to_fix = Settings::filesToFix(); const auto files = Settings::filesToFix();
for (const auto& file_to_fix : files_to_fix) { for (const auto& file : files) {
changeLibPathsOnFile(file_to_fix); changeLibPathsOnFile(file);
fixRpathsOnFile(file_to_fix, file_to_fix); fixRpathsOnFile(file, file);
} }
} }

View File

@ -6,7 +6,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
void addDependency(std::string path, const std::string& dependent_file); void addDependency(const std::string& path, const std::string& dependent_file);
// std::string searchFilenameInRpaths(const std::string& rpath_file, const std::string& dependent_file); // std::string searchFilenameInRpaths(const std::string& rpath_file, const std::string& dependent_file);
// std::string searchFilenameInRpaths(const std::string& rpath_file); // std::string searchFilenameInRpaths(const std::string& rpath_file);

View File

@ -28,7 +28,6 @@ std::string inside_path_str_app = "@executable_path/../Frameworks/";
std::string inside_path = inside_path_str; std::string inside_path = inside_path_str;
std::string app_bundle; std::string app_bundle;
bool appBundleProvided() { return !app_bundle.empty(); }
std::string appBundle() { return app_bundle; } std::string appBundle() { return app_bundle; }
void appBundle(std::string path) void appBundle(std::string path)
{ {
@ -56,6 +55,7 @@ void appBundle(std::string path)
if (dest_path[dest_path.size()-1] != '/') if (dest_path[dest_path.size()-1] != '/')
dest_path += "/"; dest_path += "/";
} }
bool appBundleProvided() { return !app_bundle.empty(); }
std::string destFolder() { return dest_path; } std::string destFolder() { return dest_path; }
void destFolder(std::string path) void destFolder(std::string path)
@ -70,6 +70,14 @@ void destFolder(std::string path)
dest_path += "/"; dest_path += "/";
} }
std::string insideLibPath() { return inside_path; }
void insideLibPath(std::string p)
{
inside_path = std::move(p);
if (inside_path[inside_path.size()-1] != '/')
inside_path += "/";
}
std::string executableFolder() { return app_bundle + "Contents/MacOS/"; } std::string executableFolder() { return app_bundle + "Contents/MacOS/"; }
std::string frameworksFolder() { return app_bundle + "Contents/Frameworks/"; } std::string frameworksFolder() { return app_bundle + "Contents/Frameworks/"; }
std::string pluginsFolder() { return app_bundle + "Contents/PlugIns/"; } std::string pluginsFolder() { return app_bundle + "Contents/PlugIns/"; }
@ -83,18 +91,10 @@ void addFileToFix(std::string path)
path = buffer; path = buffer;
files.push_back(path); files.push_back(path);
} }
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(); } size_t filesToFixCount() { return files.size(); }
std::string insideLibPath() { return inside_path; }
void insideLibPath(std::string p)
{
inside_path = std::move(p);
if (inside_path[inside_path.size()-1] != '/')
inside_path += "/";
}
std::vector<std::string> prefixes_to_ignore; std::vector<std::string> prefixes_to_ignore;
void ignorePrefix(std::string prefix) void ignorePrefix(std::string prefix)
{ {
@ -104,8 +104,8 @@ void ignorePrefix(std::string prefix)
} }
bool isPrefixIgnored(const std::string& prefix) bool isPrefixIgnored(const std::string& prefix)
{ {
for (size_t n=0; n<prefixes_to_ignore.size(); n++) { for (const auto& prefix_to_ignore : prefixes_to_ignore) {
if (prefix == prefixes_to_ignore[n]) if (prefix == prefix_to_ignore)
return true; return true;
} }
return false; return false;
@ -127,16 +127,12 @@ bool isPrefixBundled(const std::string& prefix)
} }
std::vector<std::string> search_paths; std::vector<std::string> search_paths;
void addSearchPath(const std::string& path) { search_paths.push_back(path); }
std::vector<std::string> searchPaths() { return search_paths; } std::vector<std::string> searchPaths() { return search_paths; }
std::string searchPath(const int n) { return search_paths[n]; } void addSearchPath(const std::string& path) { search_paths.push_back(path); }
size_t searchPathCount() { return search_paths.size(); }
std::vector<std::string> user_search_paths; std::vector<std::string> user_search_paths;
void addUserSearchPath(const std::string& path) { user_search_paths.push_back(path); }
std::vector<std::string> userSearchPaths() { return user_search_paths; } std::vector<std::string> userSearchPaths() { return user_search_paths; }
std::string userSearchPath(const int n) { return user_search_paths[n]; } void addUserSearchPath(const std::string& path) { user_search_paths.push_back(path); }
size_t userSearchPathCount() { return user_search_paths.size(); }
bool canCreateDir() { return create_dir; } bool canCreateDir() { return create_dir; }
void canCreateDir(bool permission) { create_dir = permission; } void canCreateDir(bool permission) { create_dir = permission; }

View File

@ -16,35 +16,30 @@ bool isPrefixBundled(const std::string& prefix);
bool isPrefixIgnored(const std::string& prefix); bool isPrefixIgnored(const std::string& prefix);
void ignorePrefix(std::string prefix); void ignorePrefix(std::string prefix);
bool appBundleProvided();
std::string appBundle(); std::string appBundle();
void appBundle(std::string path); void appBundle(std::string path);
bool appBundleProvided();
std::string destFolder(); std::string destFolder();
void destFolder(std::string path); void destFolder(std::string path);
std::string insideLibPath();
void insideLibPath(std::string p);
std::string executableFolder(); std::string executableFolder();
std::string frameworksFolder(); std::string frameworksFolder();
std::string pluginsFolder(); std::string pluginsFolder();
std::string resourcesFolder(); std::string resourcesFolder();
void addFileToFix(std::string path);
std::string fileToFix(int n);
std::vector<std::string> filesToFix(); std::vector<std::string> filesToFix();
void addFileToFix(std::string path);
size_t filesToFixCount(); size_t filesToFixCount();
std::string insideLibPath();
void insideLibPath(std::string p);
void addSearchPath(const std::string& path);
std::vector<std::string> searchPaths(); std::vector<std::string> searchPaths();
std::string searchPath(int n); void addSearchPath(const std::string& path);
size_t searchPathCount();
void addUserSearchPath(const std::string& path);
std::vector<std::string> userSearchPaths(); std::vector<std::string> userSearchPaths();
std::string userSearchPath(int n); void addUserSearchPath(const std::string& path);
size_t userSearchPathCount();
bool canCreateDir(); bool canCreateDir();
void canCreateDir(bool permission); void canCreateDir(bool permission);

View File

@ -66,7 +66,8 @@ std::string systemOutput(const std::string& cmd)
try { try {
command_output = popen(cmd.c_str(), "r"); command_output = popen(cmd.c_str(), "r");
if (command_output == nullptr) throw; if (command_output == nullptr)
throw;
while (amount_read > 0) { while (amount_read > 0) {
amount_read = fread(output, 1, 127, command_output); amount_read = fread(output, 1, 127, command_output);
@ -86,7 +87,8 @@ std::string systemOutput(const std::string& cmd)
} }
int return_value = pclose(command_output); int return_value = pclose(command_output);
if (return_value != 0) return ""; if (return_value != 0)
return "";
return full_output; return full_output;
} }
@ -253,25 +255,24 @@ void createDestDir()
} }
} }
std::string getUserInputDirForFile(const std::string& filename) std::string getUserInputDirForFile(const std::string& filename, const std::string& dependent_file)
{ {
const size_t searchPathCount = Settings::userSearchPathCount(); std::vector<std::string> search_paths = Settings::userSearchPaths();
for (size_t n=0; n<searchPathCount; ++n) { for (auto& search_path : search_paths) {
auto searchPath = Settings::userSearchPath(n); if (!search_path.empty() && search_path[search_path.size() - 1] != '/')
if (!searchPath.empty() && searchPath[searchPath.size()-1] != '/') search_path += "/";
searchPath += "/"; if (fileExists(search_path + filename)) {
if (fileExists(searchPath+filename)) {
if (!Settings::quietOutput()) { if (!Settings::quietOutput()) {
std::cerr << (searchPath+filename) << " was found\n" std::cerr << (search_path + filename) << " was found\n"
<< "/!\\ WARNING: dylibbundler MAY NOT CORRECTLY HANDLE THIS DEPENDENCY: Check the executable with 'otool -L'\n"; << "/!\\ WARNING: dylibbundler MAY NOT CORRECTLY HANDLE THIS DEPENDENCY: Check the executable with 'otool -L'\n";
} }
return searchPath; return search_path;
} }
} }
while (true) { while (true) {
if (Settings::quietOutput()) if (Settings::quietOutput())
std::cerr << "\n/!\\ WARNING: Dependency " << filename << " has an incomplete name (location unknown)\n"; std::cerr << "\n/!\\ WARNING: Dependency " << filename << " of " << dependent_file << " not found\n";
std::cout << "\nPlease specify the directory where this file is located (or enter 'quit' to abort): "; std::cout << "\nPlease specify the directory where this file is located (or enter 'quit' to abort): ";
fflush(stdout); fflush(stdout);
@ -432,7 +433,7 @@ std::string searchFilenameInRpaths(const std::string& rpath_file, const std::str
std::cout << " ** rpath fullpath: not found" << std::endl; std::cout << " ** rpath fullpath: not found" << std::endl;
if (!Settings::quietOutput()) if (!Settings::quietOutput())
std::cerr << "\n/!\\ WARNING: Can't get path for '" << rpath_file << "'\n"; std::cerr << "\n/!\\ WARNING: Can't get path for '" << rpath_file << "'\n";
fullpath = getUserInputDirForFile(suffix) + suffix; fullpath = getUserInputDirForFile(suffix, dependent_file) + suffix;
if (Settings::quietOutput() && fullpath.empty()) if (Settings::quietOutput() && fullpath.empty())
std::cerr << "\n/!\\ WARNING: Can't get path for '" << rpath_file << "'\n"; std::cerr << "\n/!\\ WARNING: Can't get path for '" << rpath_file << "'\n";
if (realpath(fullpath.c_str(), fullpath_buffer)) if (realpath(fullpath.c_str(), fullpath_buffer))

View File

@ -42,7 +42,7 @@ bool mkdir(const std::string& path);
void createDestDir(); void createDestDir();
std::string getUserInputDirForFile(const std::string& filename); std::string getUserInputDirForFile(const std::string& filename, const std::string& dependent_file);
void parseLoadCommands(const std::string& file, const std::string& cmd, const std::string& value, std::vector<std::string>& lines); void parseLoadCommands(const std::string& file, const std::string& cmd, const std::string& value, std::vector<std::string>& lines);