From 7f41661132b3e1530317738763993de4a5e05ad8 Mon Sep 17 00:00:00 2001 From: SCG82 Date: Mon, 6 Jan 2020 01:55:26 -0800 Subject: [PATCH] code cleanup --- src/Dependency.cpp | 13 ++++++------- src/Dependency.h | 2 -- src/DylibBundler.cpp | 42 +++++++++++++++++++++++------------------- src/DylibBundler.h | 2 +- src/Settings.cpp | 32 ++++++++++++++------------------ src/Settings.h | 19 +++++++------------ src/Utils.cpp | 27 ++++++++++++++------------- src/Utils.h | 2 +- 8 files changed, 66 insertions(+), 73 deletions(-) diff --git a/src/Dependency.cpp b/src/Dependency.cpp index c1ebb89..87a38d5 100644 --- a/src/Dependency.cpp +++ b/src/Dependency.cpp @@ -52,7 +52,8 @@ Dependency::Dependency(std::string path, const std::string& dependent_file) : is prefix += "/"; // 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) { 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 if (prefix.empty() || !fileExists(prefix+filename)) { + std::vector search_paths = Settings::searchPaths(); // 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_path_count == 0) + if (search_paths.empty()) initSearchPaths(); // check if file is contained in one of the paths - search_path_count = Settings::searchPathCount(); - for (size_t i=0; i frameworks; std::set rpaths; 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 bool in_deps = false; - for (auto& n : deps) { - if (dep.mergeIfSameAs(n)) + for (auto& dep : deps) { + if (dependency.mergeIfSameAs(dep)) in_deps = true; } // check if this library was already added to |deps_per_file[dependent_file]| to avoid duplicates bool in_deps_per_file = false; - for (auto& n : deps_per_file[dependent_file]) { - if (dep.mergeIfSameAs(n)) + for (auto& dep : deps_per_file[dependent_file]) { + if (dependency.mergeIfSameAs(dep)) in_deps_per_file = true; } // 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()) - frameworks.insert(dep.getOriginalPath()); + if (!in_deps && dependency.isFramework()) + frameworks.insert(dependency.getOriginalPath()); if (!in_deps) - deps.push_back(dep); + deps.push_back(dependency); 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& lines) @@ -72,7 +73,8 @@ void collectDependenciesForFile(const std::string& dependent_file) collectRpathsForFilename(dependent_file); 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); } deps_collected[dependent_file] = true; @@ -119,12 +121,14 @@ void collectSubDependencies() collectRpathsForFilename(original_path); 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); } } // 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()) { @@ -180,17 +184,17 @@ void bundleDependencies() // copy & fix up dependencies if (Settings::bundleLibs()) { createDestDir(); - for (auto& dep : deps) { + for (const auto& dep : deps) { dep.copyToAppBundle(); changeLibPathsOnFile(dep.getInstallPath()); fixRpathsOnFile(dep.getOriginalPath(), dep.getInstallPath()); } } // fix up selected files - const auto files_to_fix = Settings::filesToFix(); - for (const auto& file_to_fix : files_to_fix) { - changeLibPathsOnFile(file_to_fix); - fixRpathsOnFile(file_to_fix, file_to_fix); + const auto files = Settings::filesToFix(); + for (const auto& file : files) { + changeLibPathsOnFile(file); + fixRpathsOnFile(file, file); } } diff --git a/src/DylibBundler.h b/src/DylibBundler.h index 04d0aad..15f4578 100644 --- a/src/DylibBundler.h +++ b/src/DylibBundler.h @@ -6,7 +6,7 @@ #include #include -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); diff --git a/src/Settings.cpp b/src/Settings.cpp index 04a290d..d1804a1 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -28,7 +28,6 @@ std::string inside_path_str_app = "@executable_path/../Frameworks/"; std::string inside_path = inside_path_str; std::string app_bundle; -bool appBundleProvided() { return !app_bundle.empty(); } std::string appBundle() { return app_bundle; } void appBundle(std::string path) { @@ -56,6 +55,7 @@ void appBundle(std::string path) if (dest_path[dest_path.size()-1] != '/') dest_path += "/"; } +bool appBundleProvided() { return !app_bundle.empty(); } std::string destFolder() { return dest_path; } void destFolder(std::string path) @@ -70,6 +70,14 @@ void destFolder(std::string 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 frameworksFolder() { return app_bundle + "Contents/Frameworks/"; } std::string pluginsFolder() { return app_bundle + "Contents/PlugIns/"; } @@ -83,18 +91,10 @@ void addFileToFix(std::string path) path = buffer; files.push_back(path); } -std::string fileToFix(const int n) { return files[n]; } + std::vector filesToFix() { return files; } 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 prefixes_to_ignore; void ignorePrefix(std::string prefix) { @@ -104,8 +104,8 @@ void ignorePrefix(std::string prefix) } bool isPrefixIgnored(const std::string& prefix) { - for (size_t n=0; n search_paths; -void addSearchPath(const std::string& path) { search_paths.push_back(path); } std::vector searchPaths() { return search_paths; } -std::string searchPath(const int n) { return search_paths[n]; } -size_t searchPathCount() { return search_paths.size(); } +void addSearchPath(const std::string& path) { search_paths.push_back(path); } std::vector user_search_paths; -void addUserSearchPath(const std::string& path) { user_search_paths.push_back(path); } std::vector userSearchPaths() { return user_search_paths; } -std::string userSearchPath(const int n) { return user_search_paths[n]; } -size_t userSearchPathCount() { return user_search_paths.size(); } +void addUserSearchPath(const std::string& path) { user_search_paths.push_back(path); } bool canCreateDir() { return create_dir; } void canCreateDir(bool permission) { create_dir = permission; } diff --git a/src/Settings.h b/src/Settings.h index e8740ca..f4b480c 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -16,35 +16,30 @@ bool isPrefixBundled(const std::string& prefix); bool isPrefixIgnored(const std::string& prefix); void ignorePrefix(std::string prefix); -bool appBundleProvided(); std::string appBundle(); void appBundle(std::string path); +bool appBundleProvided(); std::string destFolder(); void destFolder(std::string path); +std::string insideLibPath(); +void insideLibPath(std::string p); + std::string executableFolder(); std::string frameworksFolder(); std::string pluginsFolder(); std::string resourcesFolder(); -void addFileToFix(std::string path); -std::string fileToFix(int n); std::vector filesToFix(); +void addFileToFix(std::string path); size_t filesToFixCount(); -std::string insideLibPath(); -void insideLibPath(std::string p); - -void addSearchPath(const std::string& path); std::vector searchPaths(); -std::string searchPath(int n); -size_t searchPathCount(); +void addSearchPath(const std::string& path); -void addUserSearchPath(const std::string& path); std::vector userSearchPaths(); -std::string userSearchPath(int n); -size_t userSearchPathCount(); +void addUserSearchPath(const std::string& path); bool canCreateDir(); void canCreateDir(bool permission); diff --git a/src/Utils.cpp b/src/Utils.cpp index 59db07c..6bde190 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -66,7 +66,8 @@ std::string systemOutput(const std::string& cmd) try { command_output = popen(cmd.c_str(), "r"); - if (command_output == nullptr) throw; + if (command_output == nullptr) + throw; while (amount_read > 0) { 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); - if (return_value != 0) return ""; + if (return_value != 0) + return ""; 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(); - for (size_t n=0; n search_paths = Settings::userSearchPaths(); + for (auto& search_path : search_paths) { + if (!search_path.empty() && search_path[search_path.size() - 1] != '/') + search_path += "/"; + if (fileExists(search_path + filename)) { 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"; } - return searchPath; + return search_path; } } while (true) { 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): "; 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; if (!Settings::quietOutput()) 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()) std::cerr << "\n/!\\ WARNING: Can't get path for '" << rpath_file << "'\n"; if (realpath(fullpath.c_str(), fullpath_buffer)) diff --git a/src/Utils.h b/src/Utils.h index 58148cb..866b280 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -42,7 +42,7 @@ bool mkdir(const std::string& path); 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& lines);