| 3550 | } |
| 3551 | |
| 3552 | static cm::optional<bool> ChangeRPathELF(std::string const& file, |
| 3553 | std::string const& oldRPath, |
| 3554 | std::string const& newRPath, |
| 3555 | bool removeEnvironmentRPath, |
| 3556 | std::string* emsg, bool* changed) |
| 3557 | { |
| 3558 | auto adjustCallback = [oldRPath, newRPath, removeEnvironmentRPath]( |
| 3559 | cm::optional<std::string>& outRPath, |
| 3560 | std::string const& inRPath, char const* se_name, |
| 3561 | std::string* emsg2) -> bool { |
| 3562 | // Make sure the current rpath contains the old rpath. |
| 3563 | std::string::size_type pos = cmSystemToolsFindRPath(inRPath, oldRPath); |
| 3564 | if (pos == std::string::npos) { |
| 3565 | // If it contains the new rpath instead then it is okay. |
| 3566 | if (cmSystemToolsFindRPath(inRPath, newRPath) != std::string::npos) { |
| 3567 | return true; |
| 3568 | } |
| 3569 | if (emsg2) { |
| 3570 | std::ostringstream e; |
| 3571 | /* clang-format off */ |
| 3572 | e << "The current " << se_name << " is:\n" |
| 3573 | " " << inRPath << "\n" |
| 3574 | "which does not contain:\n" |
| 3575 | " " << oldRPath << "\n" |
| 3576 | "as was expected."; |
| 3577 | /* clang-format on */ |
| 3578 | *emsg2 = e.str(); |
| 3579 | } |
| 3580 | return false; |
| 3581 | } |
| 3582 | |
| 3583 | std::string::size_type prefix_len = pos; |
| 3584 | |
| 3585 | // If oldRPath was at the end of the file's RPath, and newRPath is empty, |
| 3586 | // we should remove the unnecessary ':' at the end. |
| 3587 | if (newRPath.empty() && pos > 0 && inRPath[pos - 1] == ':' && |
| 3588 | pos + oldRPath.length() == inRPath.length()) { |
| 3589 | prefix_len--; |
| 3590 | } |
| 3591 | |
| 3592 | // Construct the new value which preserves the part of the path |
| 3593 | // not being changed. |
| 3594 | outRPath.emplace(); |
| 3595 | if (!removeEnvironmentRPath) { |
| 3596 | *outRPath += inRPath.substr(0, prefix_len); |
| 3597 | } |
| 3598 | *outRPath += newRPath; |
| 3599 | *outRPath += inRPath.substr(pos + oldRPath.length()); |
| 3600 | |
| 3601 | return true; |
| 3602 | }; |
| 3603 | |
| 3604 | return AdjustRPathELF(file, MakeEmptyCallback(newRPath), adjustCallback, |
| 3605 | emsg, changed); |
| 3606 | } |
| 3607 | |
| 3608 | static cm::optional<bool> SetRPathELF(std::string const& file, |
| 3609 | std::string const& newRPath, |
no test coverage detected
searching dependent graphs…