code cleanup

This commit is contained in:
SCG82 2020-01-03 22:24:13 -08:00
parent c4ff430bd3
commit 2603a0c0bc
6 changed files with 38 additions and 42 deletions

View File

@ -109,17 +109,16 @@ std::string Dependency::getInnerPath()
void Dependency::addSymlink(std::string s) void Dependency::addSymlink(std::string s)
{ {
// calling std::find on this vector is not as slow as an extra invocation of install_name_tool
if (std::find(symlinks.begin(), symlinks.end(), s) == symlinks.end()) if (std::find(symlinks.begin(), symlinks.end(), s) == symlinks.end())
symlinks.push_back(s); symlinks.push_back(s);
} }
// compare given Dependency with this one. if both refer to the same file, merge into one entry.
bool Dependency::mergeIfSameAs(Dependency& dep2) bool Dependency::mergeIfSameAs(Dependency& dep2)
{ {
if (dep2.getOriginalFileName().compare(filename) == 0) { if (dep2.getOriginalFileName().compare(filename) == 0) {
for (size_t n=0; n<symlinks.size(); ++n) for (size_t n=0; n<symlinks.size(); ++n) {
dep2.addSymlink(symlinks[n]); dep2.addSymlink(symlinks[n]);
}
return true; return true;
} }
return false; return false;
@ -168,24 +167,18 @@ void Dependency::copyYourself()
deleteFile(dest_path + "/*.prl"); deleteFile(dest_path + "/*.prl");
} }
// fix the lib's inner name
changeId(getInstallPath(), "@rpath/"+new_name); changeId(getInstallPath(), "@rpath/"+new_name);
} }
void Dependency::fixFileThatDependsOnMe(std::string file_to_fix) void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
{ {
// for main lib file
changeInstallName(file_to_fix, getOriginalPath(), getInnerPath()); changeInstallName(file_to_fix, getOriginalPath(), getInnerPath());
// for symlinks
for (size_t n=0; n<symlinks.size(); ++n) { for (size_t n=0; n<symlinks.size(); ++n) {
changeInstallName(file_to_fix, symlinks[n], getInnerPath()); changeInstallName(file_to_fix, symlinks[n], getInnerPath());
} }
// TODO: revise
if (Settings::missingPrefixes()) { if (Settings::missingPrefixes()) {
// for main lib file
changeInstallName(file_to_fix, filename, getInnerPath()); changeInstallName(file_to_fix, filename, getInnerPath());
// for symlinks
for (size_t n=0; n<symlinks.size(); ++n) { for (size_t n=0; n<symlinks.size(); ++n) {
changeInstallName(file_to_fix, symlinks[n], getInnerPath()); changeInstallName(file_to_fix, symlinks[n], getInnerPath());
} }

View File

@ -50,11 +50,6 @@ void addDependency(std::string path, std::string dependent_file)
deps_per_file[dependent_file].push_back(dep); deps_per_file[dependent_file].push_back(dep);
} }
std::string searchFilenameInRpaths(const std::string& rpath_file)
{
return searchFilenameInRpaths(rpath_file, rpath_file);
}
void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines) void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines)
{ {
parseLoadCommands(dependent_file, std::string("LC_LOAD_DYLIB"), std::string("name"), lines); parseLoadCommands(dependent_file, std::string("LC_LOAD_DYLIB"), std::string("name"), lines);

View File

@ -8,8 +8,8 @@
void addDependency(std::string path, std::string dependent_file); void addDependency(std::string path, 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);
// fill |lines| with dependencies of |dependent_file| // fill |lines| with dependencies of |dependent_file|
void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines); void collectDependencies(const std::string& dependent_file, std::vector<std::string>& lines);

View File

@ -75,7 +75,13 @@ std::string pluginsFolder() { return app_bundle + "Contents/PlugIns/"; }
std::string resourcesFolder() { return app_bundle + "Contents/Resources/"; } std::string resourcesFolder() { return app_bundle + "Contents/Resources/"; }
std::vector<std::string> files; std::vector<std::string> files;
void addFileToFix(std::string path) { files.push_back(path); } void addFileToFix(std::string path)
{
char buffer[PATH_MAX];
if (realpath(path.c_str(), buffer))
path = buffer;
files.push_back(path);
}
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(); } size_t filesToFixCount() { return files.size(); }

View File

@ -73,7 +73,7 @@ std::string systemOutput(const std::string& cmd)
} }
} }
catch (...) { catch (...) {
std::cerr << "An error occured while executing command " << cmd << "\n"; std::cerr << "An error occured while executing command " << cmd << std::endl;
pclose(command_output); pclose(command_output);
return ""; return "";
} }
@ -127,15 +127,12 @@ bool fileExists(const std::string& filename)
if (access(filename.c_str(), F_OK) != -1) { if (access(filename.c_str(), F_OK) != -1) {
return true; return true;
} }
else { std::string delims = " \f\n\r\t\v";
std::string delims = " \f\n\r\t\v"; std::string rtrimmed = filename.substr(0, filename.find_last_not_of(delims)+1);
std::string rtrimmed = filename.substr(0, filename.find_last_not_of(delims)+1); std::string ftrimmed = rtrimmed.substr(rtrimmed.find_first_not_of(delims));
std::string ftrimmed = rtrimmed.substr(rtrimmed.find_first_not_of(delims)); if (access(ftrimmed.c_str(), F_OK) != -1)
if (access(ftrimmed.c_str(), F_OK) != -1) return true;
return true; return false;
else
return false;
}
} }
bool isRpath(const std::string& path) bool isRpath(const std::string& path)
@ -145,8 +142,7 @@ bool isRpath(const std::string& path)
std::string bundleExecutableName(const std::string& app_bundle_path) std::string bundleExecutableName(const std::string& app_bundle_path)
{ {
std::string cmd = "/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' " std::string cmd = "/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' " + app_bundle_path + "Contents/Info.plist";
+ app_bundle_path + "Contents/Info.plist";
return rtrim(systemOutput(cmd)); return rtrim(systemOutput(cmd));
} }
@ -154,7 +150,7 @@ void changeId(const std::string& binary_file, const std::string& new_id)
{ {
std::string command = std::string("install_name_tool -id ") + new_id + " " + binary_file; std::string command = std::string("install_name_tool -id ") + new_id + " " + binary_file;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to change identity of library " << binary_file << "\n"; std::cerr << "\n\nError: An error occured while trying to change identity of library " << binary_file << std::endl;
exit(1); exit(1);
} }
} }
@ -163,7 +159,7 @@ void changeInstallName(const std::string& binary_file, const std::string& old_na
{ {
std::string command = std::string("install_name_tool -change ") + old_name + " " + new_name + " " + binary_file; std::string command = std::string("install_name_tool -change ") + old_name + " " + new_name + " " + binary_file;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << binary_file << "\n"; std::cerr << "\n\nError: An error occured while trying to fix dependencies of " << binary_file << std::endl;
exit(1); exit(1);
} }
} }
@ -181,14 +177,14 @@ void copyFile(const std::string& from, const std::string& to)
// copy file/directory // copy file/directory
std::string command = std::string("cp -R ") + overwrite_permission + from + std::string(" ") + to; std::string command = std::string("cp -R ") + overwrite_permission + from + std::string(" ") + to;
if (from != to && systemp(command) != 0) { if (from != to && systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to copy file " << from << " to " << to << "\n"; std::cerr << "\n\nError: An error occured while trying to copy file " << from << " to " << to << std::endl;
exit(1); exit(1);
} }
// give file/directory write permission // give file/directory write permission
std::string command2 = std::string("chmod -R +w ") + to; std::string command2 = std::string("chmod -R +w ") + to;
if (systemp(command2) != 0) { if (systemp(command2) != 0) {
std::cerr << "\n\nError: An error occured while trying to set write permissions on file " << to << "\n"; std::cerr << "\n\nError: An error occured while trying to set write permissions on file " << to << std::endl;
exit(1); exit(1);
} }
} }
@ -198,7 +194,7 @@ void deleteFile(const std::string& path, bool overwrite)
std::string overwrite_permission = std::string(overwrite ? "-f " : " "); std::string overwrite_permission = std::string(overwrite ? "-f " : " ");
std::string command = std::string("rm -r ") + overwrite_permission + path; std::string command = std::string("rm -r ") + overwrite_permission + path;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\nError: An error occured while trying to delete " << path << "\n"; std::cerr << "\n\nError: An error occured while trying to delete " << path << std::endl;
exit(1); exit(1);
} }
} }
@ -215,7 +211,7 @@ bool mkdir(const std::string& path)
std::cout << "* Creating directory " << path << "\n\n"; std::cout << "* Creating directory " << path << "\n\n";
std::string command = std::string("mkdir -p ") + path; std::string command = std::string("mkdir -p ") + path;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n/!\\ ERROR: An error occured while creating " + path + "\n"; std::cerr << "\n/!\\ ERROR: An error occured while creating " << path << std::endl;
return false; return false;
} }
return true; return true;
@ -233,7 +229,7 @@ void createDestDir()
std::cout << "* Erasing old output directory " << dest_folder << "\n"; std::cout << "* Erasing old output directory " << dest_folder << "\n";
std::string command = std::string("rm -r ") + dest_folder; std::string command = std::string("rm -r ") + dest_folder;
if (systemp(command) != 0) { if (systemp(command) != 0) {
std::cerr << "\n\n/!\\ ERROR: An error occured while attempting to overwrite dest folder\n"; std::cerr << "\n\n/!\\ ERROR: An error occured while attempting to overwrite destination folder\n";
exit(1); exit(1);
} }
dest_exists = false; dest_exists = false;
@ -243,12 +239,12 @@ void createDestDir()
if (Settings::canCreateDir()) { if (Settings::canCreateDir()) {
std::cout << "* Creating output directory " << dest_folder << "\n\n"; std::cout << "* Creating output directory " << dest_folder << "\n\n";
if (!mkdir(dest_folder)) { if (!mkdir(dest_folder)) {
std::cerr << "\n/!\\ ERROR: An error occured while creating " + dest_folder + "\n"; std::cerr << "\n/!\\ ERROR: An error occured while creating " << dest_folder << std::endl;
exit(1); exit(1);
} }
} }
else { else {
std::cerr << "\n\n/!\\ ERROR: Dest folder does not exist. Create it or pass the appropriate flag for automatic dest dir creation\n"; std::cerr << "\n\n/!\\ ERROR: Destination folder does not exist. Create it or pass the '-cd' or '-od' flag\n";
exit(1); exit(1);
} }
} }
@ -264,7 +260,7 @@ std::string getUserInputDirForFile(const std::string& filename)
if (fileExists(searchPath+filename)) { if (fileExists(searchPath+filename)) {
if (!Settings::quietOutput()) { if (!Settings::quietOutput()) {
std::cerr << (searchPath+filename) << " was found\n" std::cerr << (searchPath+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 searchPath;
} }
@ -292,7 +288,7 @@ std::string getUserInputDirForFile(const std::string& filename)
} }
else { else {
std::cerr << (prefix+filename) << " was found\n" std::cerr << (prefix+filename) << " was found\n"
<< "/!\\ WARNINGS: 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";
Settings::addUserSearchPath(prefix); Settings::addUserSearchPath(prefix);
return prefix; return prefix;
} }
@ -321,7 +317,7 @@ void parseLoadCommands(const std::string& file, const std::string& cmd, const st
for (const auto& line : raw_lines) { for (const auto& line : raw_lines) {
if (line.find(cmd_line) != std::string::npos) { if (line.find(cmd_line) != std::string::npos) {
if (searching) { if (searching) {
std::cerr << "\n\n/!\\ ERROR: Failed to find " << value << " before next cmd" << std::endl; std::cerr << "\n\n/!\\ ERROR: Failed to find " << value << " before next cmd\n";
exit(1); exit(1);
} }
searching = true; searching = true;
@ -425,7 +421,7 @@ std::string searchFilenameInRpaths(const std::string& rpath_file, const std::str
std::string search_path = Settings::searchPath(i); std::string search_path = Settings::searchPath(i);
if (fileExists(search_path+suffix)) { if (fileExists(search_path+suffix)) {
if (Settings::verboseOutput()) if (Settings::verboseOutput())
std::cout << "FOUND " + suffix + " in " + search_path + "\n"; std::cout << "FOUND " << suffix << " in " << search_path << std::endl;
fullpath = search_path + suffix; fullpath = search_path + suffix;
break; break;
} }
@ -452,6 +448,11 @@ std::string searchFilenameInRpaths(const std::string& rpath_file, const std::str
return fullpath; return fullpath;
} }
std::string searchFilenameInRpaths(const std::string& rpath_file)
{
return searchFilenameInRpaths(rpath_file, rpath_file);
}
void initSearchPaths() void initSearchPaths()
{ {
std::string searchPaths; std::string searchPaths;

View File

@ -47,6 +47,7 @@ std::string getUserInputDirForFile(const std::string& filename);
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);
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);
// check the same paths the system would search for dylibs // check the same paths the system would search for dylibs
void initSearchPaths(); void initSearchPaths();