Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add '--install-deps-path' and '--skip-*' options #87

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
28 changes: 17 additions & 11 deletions src/Dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Dependency::Dependency(std::string path, const std::string& dependent_file)
}

//If the location is still unknown, ask the user for search path
if( !Settings::isPrefixIgnored(prefix)
if( !Settings::isPrefixIgnored(prefix) && !Settings::skipMissing()
&& ( prefix.empty() || !fileExists( prefix+filename ) ) )
{
std::cerr << "\n/!\\ WARNING : Library " << filename << " has an incomplete name (location unknown)" << std::endl;
Expand All @@ -167,9 +167,9 @@ std::string Dependency::getInstallPath()
{
return Settings::destFolder() + new_name;
}
std::string Dependency::getInnerPath()
std::string Dependency::getInnerPath(const std::string& new_rpath)
{
return Settings::inside_lib_path() + new_name;
return new_rpath + new_name;
}


Expand All @@ -194,40 +194,46 @@ bool Dependency::mergeIfSameAs(Dependency& dep2)
return false;
}

void Dependency::copyYourself()
bool Dependency::copyYourself()
{
copyFile(getOriginalPath(), getInstallPath());
const bool was_copied = copyFile(getOriginalPath(), getInstallPath());

if (!was_copied || Settings::skipPatching()) {
return was_copied;
}

// Fix the lib's inner name
std::string command = std::string("install_name_tool -id \"") + getInnerPath() + "\" \"" + getInstallPath() + "\"";
std::string command = std::string("install_name_tool -id \"") + getInnerPath(Settings::inside_dep_load_path()) + "\" \"" + getInstallPath() + "\"";
if( systemp( command ) != 0 )
{
std::cerr << "\n\nError : An error occured while trying to change identity of library " << getInstallPath() << std::endl;
exit(1);
}

return true;
}

void Dependency::fixFileThatDependsOnMe(const std::string& file_to_fix)
void Dependency::fixFileThatDependsOnMe(const std::string& file_to_fix, const std::string& new_rpath)
{
// for main lib file
changeInstallName(file_to_fix, getOriginalPath(), getInnerPath());
changeInstallName(file_to_fix, getOriginalPath(), getInnerPath(new_rpath));
// for symlinks
const int symamount = symlinks.size();
for(int n=0; n<symamount; n++)
{
changeInstallName(file_to_fix, symlinks[n], getInnerPath());
changeInstallName(file_to_fix, symlinks[n], getInnerPath(new_rpath));
}

// FIXME - hackish
if(missing_prefixes)
{
// for main lib file
changeInstallName(file_to_fix, filename, getInnerPath());
changeInstallName(file_to_fix, filename, getInnerPath(new_rpath));
// for symlinks
const int symamount = symlinks.size();
for(int n=0; n<symamount; n++)
{
changeInstallName(file_to_fix, symlinks[n], getInnerPath());
changeInstallName(file_to_fix, symlinks[n], getInnerPath(new_rpath));
}
}
}
10 changes: 5 additions & 5 deletions src/Dependency.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ class Dependency
std::string getOriginalFileName() const{ return filename; }
std::string getOriginalPath() const{ return prefix+filename; }
std::string getInstallPath();
std::string getInnerPath();
std::string getInnerPath(const std::string& new_rpath);

void addSymlink(const std::string& s);
int getSymlinkAmount() const{ return symlinks.size(); }

std::string getSymlink(const int i) const{ return symlinks[i]; }
std::string getPrefix() const{ return prefix; }

void copyYourself();
void fixFileThatDependsOnMe(const std::string& file);
bool copyYourself();
void fixFileThatDependsOnMe(const std::string& file, const std::string& new_rpath);

// Compares the given dependency with this one. If both refer to the same file,
// it returns true and merges both entries into one.
bool mergeIfSameAs(Dependency& dep2);
};


#endif
#endif
35 changes: 26 additions & 9 deletions src/DylibBundler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ std::map<std::string, bool> deps_collected;
std::map<std::string, std::vector<std::string> > rpaths_per_file;
std::map<std::string, std::string> rpath_to_fullpath;

void changeLibPathsOnFile(std::string file_to_fix)
void changeLibPathsOnFile(std::string file_to_fix, const std::string& new_rpath)
{
if (deps_collected.find(file_to_fix) == deps_collected.end())
{
Expand All @@ -58,7 +58,7 @@ void changeLibPathsOnFile(std::string file_to_fix)
const int dep_amount = deps_in_file.size();
for(int n=0; n<dep_amount; n++)
{
deps_in_file[n].fixFileThatDependsOnMe(file_to_fix);
deps_in_file[n].fixFileThatDependsOnMe(file_to_fix, new_rpath);
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ std::string searchFilenameInRpaths(const std::string& rpath_dep)
return searchFilenameInRpaths(rpath_dep, rpath_dep);
}

void fixRpathsOnFile(const std::string& original_file, const std::string& file_to_fix)
void fixRpathsOnFile(const std::string& original_file, const std::string& file_to_fix, const std::string new_rpath)
{
std::vector<std::string> rpaths_to_fix;
std::map<std::string, std::vector<std::string> >::iterator found = rpaths_per_file.find(original_file);
Expand All @@ -205,7 +205,7 @@ void fixRpathsOnFile(const std::string& original_file, const std::string& file_t
for (size_t i=0; i < rpaths_to_fix.size(); ++i)
{
std::string command = std::string("install_name_tool -rpath \"") +
rpaths_to_fix[i] + "\" \"" + Settings::inside_lib_path() + "\" \"" + file_to_fix + "\"";
rpaths_to_fix[i] + "\" \"" + new_rpath + "\" \"" + file_to_fix + "\"";
if ( systemp(command) != 0)
{
std::cerr << "\n\nError : An error occured while trying to fix dependencies of " << file_to_fix << std::endl;
Expand Down Expand Up @@ -252,6 +252,10 @@ void collectDependencies(const std::string& filename, std::vector<std::string>&
if(output.find("can't open file")!=std::string::npos or output.find("No such file")!=std::string::npos or output.size()<1)
{
std::cerr << "Cannot find file " << filename << " to read its dependencies" << std::endl;
if (Settings::skipMissing()) {
return;
}

exit(1);
}

Expand Down Expand Up @@ -394,9 +398,17 @@ void doneWithDeps_go()
for(int n=dep_amount-1; n>=0; n--)
{
std::cout << "\n* Processing dependency " << deps[n].getInstallPath() << std::endl;
deps[n].copyYourself();
changeLibPathsOnFile(deps[n].getInstallPath());
fixRpathsOnFile(deps[n].getOriginalPath(), deps[n].getInstallPath());
const bool was_copied = deps[n].copyYourself();
if (!was_copied) {
continue;
}

if (Settings::skipPatching()) {
continue;
}

changeLibPathsOnFile(deps[n].getInstallPath(), Settings::inside_dep_load_path());
fixRpathsOnFile(deps[n].getOriginalPath(), deps[n].getInstallPath(), Settings::inside_dep_load_path());
adhocCodeSign(deps[n].getInstallPath());
}
}
Expand All @@ -406,8 +418,13 @@ void doneWithDeps_go()
{
std::cout << "\n* Processing " << Settings::fileToFix(n) << std::endl;
copyFile(Settings::fileToFix(n), Settings::fileToFix(n)); // to set write permission
changeLibPathsOnFile(Settings::fileToFix(n));
fixRpathsOnFile(Settings::fileToFix(n), Settings::fileToFix(n));

if (Settings::skipPatching()) {
continue;
}

changeLibPathsOnFile(Settings::fileToFix(n), Settings::inside_bin_load_path());
fixRpathsOnFile(Settings::fileToFix(n), Settings::fileToFix(n), Settings::inside_bin_load_path());
adhocCodeSign(Settings::fileToFix(n));
}
}
29 changes: 24 additions & 5 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ namespace Settings
{

bool overwrite_files = false;
bool skip_existing = false;
bool overwrite_dir = false;
bool create_dir = false;
bool codesign = true;

bool skipExisting(){ return skip_existing; }
bool canOverwriteFiles(){ return overwrite_files; }
bool canOverwriteDir(){ return overwrite_dir; }
bool canCreateDir(){ return create_dir; }
bool canCodesign(){ return codesign; }

void skipExisting(bool permission){ skip_existing = permission; }
void canOverwriteFiles(bool permission){ overwrite_files = permission; }
void canOverwriteDir(bool permission){ overwrite_dir = permission; }
void canCreateDir(bool permission){ create_dir = permission; }
Expand All @@ -48,6 +51,13 @@ bool bundleLibs_bool = false;
bool bundleLibs(){ return bundleLibs_bool; }
void bundleLibs(bool on){ bundleLibs_bool = on; }

bool skipPatching_bool = false;
bool skipPatching(){ return skipPatching_bool && bundleLibs_bool; }
void skipPatching(bool on){ skipPatching_bool = on; }

bool skipMissing_bool = false;
bool skipMissing(){ return skipMissing_bool; }
void skipMissing(bool on){ skipMissing_bool = on; }

std::string dest_folder_str = "./libs/";
std::string destFolder(){ return dest_folder_str; }
Expand All @@ -63,13 +73,22 @@ void addFileToFix(const std::string& path){ files.push_back(path); }
int fileToFixAmount(){ return files.size(); }
std::string fileToFix(const int n){ return files[n]; }

std::string inside_path_str = "@executable_path/../libs/";
std::string inside_lib_path(){ return inside_path_str; }
void inside_lib_path(const std::string& p)
std::string inside_bin_load_path_str = "@executable_path/../libs/";
std::string inside_bin_load_path(){ return inside_bin_load_path_str; }
void inside_bin_load_path(const std::string& p)
{
inside_bin_load_path_str = p;
// fix path if needed so it ends with '/'
if( inside_bin_load_path_str[ inside_bin_load_path_str.size()-1 ] != '/' ) inside_bin_load_path_str += "/";
}

std::string inside_dep_load_path_str = "";
std::string inside_dep_load_path(){ return inside_dep_load_path_str; }
void inside_dep_load_path(const std::string& p)
{
inside_path_str = p;
inside_dep_load_path_str = p;
// fix path if needed so it ends with '/'
if( inside_path_str[ inside_path_str.size()-1 ] != '/' ) inside_path_str += "/";
if( inside_dep_load_path_str[ inside_dep_load_path_str.size()-1 ] != '/' ) inside_dep_load_path_str += "/";
}

std::vector<std::string> prefixes_to_ignore;
Expand Down
16 changes: 14 additions & 2 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void ignore_prefix(std::string prefix);
bool canOverwriteFiles();
void canOverwriteFiles(bool permission);

bool skipExisting();
void skipExisting(bool permission);

bool canOverwriteDir();
void canOverwriteDir(bool permission);

Expand All @@ -50,15 +53,24 @@ void canCodesign(bool permission);
bool bundleLibs();
void bundleLibs(bool on);

bool skipPatching();
void skipPatching(bool on);

bool skipMissing();
void skipMissing(bool on);

std::string destFolder();
void destFolder(const std::string& path);

void addFileToFix(const std::string& path);
int fileToFixAmount();
std::string fileToFix(const int n);

std::string inside_lib_path();
void inside_lib_path(const std::string& p);
std::string inside_bin_load_path();
void inside_bin_load_path(const std::string& p);

std::string inside_dep_load_path();
void inside_dep_load_path(const std::string& p);

void addSearchPath(const std::string& path);
int searchPathAmount();
Expand Down
11 changes: 10 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ bool fileExists(const std::string& filename)
}
}

void copyFile(const string& from, const string& to)
bool copyFile(const string& from, const string& to)
{
bool override = Settings::canOverwriteFiles();
bool ignore = Settings::skipExisting();
if( from != to && !override )
{
if(fileExists( to ))
{
if (ignore)
{
cerr << "\n/!\\ WARNING : library " << to.c_str() << " already exists in dest dir and will not be patched." << endl;
return false;
}

cerr << "\n\nError : File " << to.c_str() << " already exists. Remove it or enable overwriting." << endl;
exit(1);
}
Expand All @@ -122,6 +129,8 @@ void copyFile(const string& from, const string& to)
cerr << "\n\nError : An error occured while trying to set write permissions on file " << to << endl;
exit(1);
}

return true;
}

std::string system_get_output(const std::string& cmd)
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Library;
void tokenize(const std::string& str, const char* delimiters, std::vector<std::string>*);
bool fileExists(const std::string& filename);

void copyFile(const std::string& from, const std::string& to);
bool copyFile(const std::string& from, const std::string& to);

// executes a command in the native shell and returns output in string
std::string system_get_output(const std::string& cmd);
Expand Down
Loading