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)
{
// 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())
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)
{
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]);
}
return true;
}
return false;
@ -168,24 +167,18 @@ void Dependency::copyYourself()
deleteFile(dest_path + "/*.prl");
}
// fix the lib's inner name
changeId(getInstallPath(), "@rpath/"+new_name);
}
void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
{
// for main lib file
changeInstallName(file_to_fix, getOriginalPath(), getInnerPath());
// for symlinks
for (size_t n=0; n<symlinks.size(); ++n) {
changeInstallName(file_to_fix, symlinks[n], getInnerPath());
}
// TODO: revise
if (Settings::missingPrefixes()) {
// for main lib file
changeInstallName(file_to_fix, filename, getInnerPath());
// for symlinks
for (size_t n=0; n<symlinks.size(); ++n) {
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);
}
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)
{
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);
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, const std::string& dependent_file);
// std::string searchFilenameInRpaths(const std::string& rpath_file);
// fill |lines| with dependencies of |dependent_file|
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::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::vector<std::string> filesToFix() { return files; }
size_t filesToFixCount() { return files.size(); }

View File

@ -73,7 +73,7 @@ std::string systemOutput(const std::string& cmd)
}
}
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);
return "";
}
@ -127,15 +127,12 @@ bool fileExists(const std::string& filename)
if (access(filename.c_str(), F_OK) != -1) {
return true;
}
else {
std::string delims = " \f\n\r\t\v";
std::string rtrimmed = filename.substr(0, filename.find_last_not_of(delims)+1);
std::string ftrimmed = rtrimmed.substr(rtrimmed.find_first_not_of(delims));
if (access(ftrimmed.c_str(), F_OK) != -1)
return true;
else
return false;
}
std::string delims = " \f\n\r\t\v";
std::string rtrimmed = filename.substr(0, filename.find_last_not_of(delims)+1);
std::string ftrimmed = rtrimmed.substr(rtrimmed.find_first_not_of(delims));
if (access(ftrimmed.c_str(), F_OK) != -1)
return true;
return false;
}
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 cmd = "/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "
+ app_bundle_path + "Contents/Info.plist";
std::string cmd = "/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' " + app_bundle_path + "Contents/Info.plist";
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;
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);
}
}
@ -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;
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);
}
}
@ -181,14 +177,14 @@ void copyFile(const std::string& from, const std::string& to)
// copy file/directory
std::string command = std::string("cp -R ") + overwrite_permission + from + std::string(" ") + to;
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);
}
// give file/directory write permission
std::string command2 = std::string("chmod -R +w ") + to;
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);
}
}
@ -198,7 +194,7 @@ void deleteFile(const std::string& path, bool overwrite)
std::string overwrite_permission = std::string(overwrite ? "-f " : " ");
std::string command = std::string("rm -r ") + overwrite_permission + path;
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);
}
}
@ -215,7 +211,7 @@ bool mkdir(const std::string& path)
std::cout << "* Creating directory " << path << "\n\n";
std::string command = std::string("mkdir -p ") + path;
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 true;
@ -233,7 +229,7 @@ void createDestDir()
std::cout << "* Erasing old output directory " << dest_folder << "\n";
std::string command = std::string("rm -r ") + dest_folder;
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);
}
dest_exists = false;
@ -243,12 +239,12 @@ void createDestDir()
if (Settings::canCreateDir()) {
std::cout << "* Creating output directory " << dest_folder << "\n\n";
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);
}
}
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);
}
}
@ -264,7 +260,7 @@ std::string getUserInputDirForFile(const std::string& filename)
if (fileExists(searchPath+filename)) {
if (!Settings::quietOutput()) {
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;
}
@ -292,7 +288,7 @@ std::string getUserInputDirForFile(const std::string& filename)
}
else {
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);
return prefix;
}
@ -321,7 +317,7 @@ void parseLoadCommands(const std::string& file, const std::string& cmd, const st
for (const auto& line : raw_lines) {
if (line.find(cmd_line) != std::string::npos) {
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);
}
searching = true;
@ -425,7 +421,7 @@ std::string searchFilenameInRpaths(const std::string& rpath_file, const std::str
std::string search_path = Settings::searchPath(i);
if (fileExists(search_path+suffix)) {
if (Settings::verboseOutput())
std::cout << "FOUND " + suffix + " in " + search_path + "\n";
std::cout << "FOUND " << suffix << " in " << search_path << std::endl;
fullpath = search_path + suffix;
break;
}
@ -452,6 +448,11 @@ std::string searchFilenameInRpaths(const std::string& rpath_file, const std::str
return fullpath;
}
std::string searchFilenameInRpaths(const std::string& rpath_file)
{
return searchFilenameInRpaths(rpath_file, rpath_file);
}
void initSearchPaths()
{
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);
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
void initSearchPaths();