diff options
Diffstat (limited to 'src/Utils.cpp')
-rw-r--r-- | src/Utils.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp index 9903f20..fc2b990 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -217,3 +217,50 @@ std::string getUserInputDirForFile(const std::string& filename) } } } + +void adhocCodeSign(const std::string& file) +{ + // Add ad-hoc signature for ARM (Apple Silicon) binaries + std::string signCommand = std::string("codesign --force --deep --preserve-metadata=entitlements,requirements,flags,runtime --sign - \"") + file + "\""; + if( systemp( signCommand ) != 0 ) + { + // If the codesigning fails, it may be a bug in Apple's codesign utility. + // A known workaround is to copy the file to another inode, then move it back + // erasing the previous file. Then sign again. + std::cerr << " * Error : An error occurred while applying ad-hoc signature to " << file << ". Attempting workaround" << std::endl; + + std::string machine = system_get_output("machine"); + bool isArm = machine.find("arm") != std::string::npos; + std::string tempDirTemplate = std::string(std::getenv("TMPDIR") + std::string("dylibbundler.XXXXXXXX")); + std::string filename = file.substr(file.rfind("/")+1); + char* tmpDirCstr = mkdtemp((char *)(tempDirTemplate.c_str())); + if( tmpDirCstr == NULL ) + { + std::cerr << " * Error : Unable to create temp directory for signing workaround" << std::endl; + if( isArm ) + { + exit(1); + } + } + std::string tmpDir = std::string(tmpDirCstr); + std::string tmpFile = tmpDir+"/"+filename; + const auto runCommand = [isArm](const std::string& command, const std::string& errMsg) + { + if( systemp( command ) != 0 ) + { + std::cerr << errMsg << std::endl; + if( isArm ) + { + exit(1); + } + } + }; + std::string command = std::string("cp -p \"") + file + "\" \"" + tmpFile + "\""; + runCommand(command, " * Error : An error occurred copying " + file + " to " + tmpDir); + command = std::string("mv -f \"") + tmpFile + "\" \"" + file + "\""; + runCommand(command, " * Error : An error occurred moving " + tmpFile + " to " + file); + command = std::string("rm -rf \"") + tmpDir + "\""; + systemp( command ); + runCommand(signCommand, " * Error : An error occurred while applying ad-hoc signature to " + file); + } +} |