Option to disable printing package-name in public or private interface.

Having package-name flag in non-package interfaces causes them to be built as if
belonging to a package, which causes an issue for a loading client outside of the
package as follows.

For example, when building X that depends on A with the following dependency chain:
  X --> A --> B --(package-only)--> C

1. X itself is not in the same package as A, B, and C.
2. When dependency scanning X, and opening up B, because the scan target is in a
   different package domain, the scanner decides that B's package-only dependency
   on C is to be ignored.
3. When then finally building A itself, it will load its dependencies, but because
   the .private.swiftinterface of A still specifies -package-name, when it loads
   B, it will then examine its dependencies and deem that this package-only dependency
   on C is required.

Because (2) and (3) disagree, we get an error now when building the private A textual interface.

rdar://130701866
This commit is contained in:
Ellie Shin
2024-06-28 12:04:29 -07:00
parent f27139ea16
commit e5b4655108
6 changed files with 106 additions and 1 deletions

View File

@@ -60,8 +60,35 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
<< InterfaceFormatVersion << "\n";
out << "// " SWIFT_COMPILER_VERSION_KEY ": "
<< ToolsVersion << "\n";
// Check if printing package-name is disabled for
// non-package interfaces (by default, it's printed
// in all interfaces).
std::string flagsStr = Opts.Flags;
if (Opts.DisablePackageNameForNonPackageInterface &&
!Opts.printPackageInterface()) {
size_t pkgIdx = 0;
size_t end = flagsStr.size();
auto pkgFlag = StringRef("-package-name ");
size_t pkgLen = pkgFlag.size();
// Find the package-name flag and its value and
// drop them. There can be multiple package-name
// flags passed, so drop them all.
while (pkgIdx < end) {
// First, find "-package-name "
pkgIdx = flagsStr.find(pkgFlag, 0);
if (pkgIdx == std::string::npos)
break;
// If found, find the next flag's starting pos.
auto next = flagsStr.find_first_of("-", pkgIdx + pkgLen + 1);
// Remove the substr in-place.
flagsStr.erase(pkgIdx, next - pkgIdx);
}
}
out << "// " SWIFT_MODULE_FLAGS_KEY ": "
<< Opts.Flags;
<< flagsStr;
// Insert additional -module-alias flags
if (Opts.AliasModuleNames) {