Skip to content

Commit

Permalink
Merge pull request #62700 from apple/es-pkg1
Browse files Browse the repository at this point in the history
Add -package-name flag and de/serialize package name in module binary
Resoles rdar://103531218, rdar://103531208
  • Loading branch information
elsh committed Dec 20, 2022
2 parents c8eb1f2 + 509e74e commit 7323d32
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class ModuleDecl
/// The ABI name of the module, if it differs from the module name.
mutable Identifier ModuleABIName;

/// The name of the package this module belongs to
mutable Identifier PackageName;
public:
/// Produces the components of a given module's full name in reverse order.
///
Expand Down Expand Up @@ -400,6 +402,14 @@ class ModuleDecl
ModuleABIName = name;
}

/// Get the package name of the module
Identifier getPackageName() const { return PackageName; }

/// Set the name of the package this module belongs to
void setPackageName(Identifier name) {
PackageName = name;
}

/// Retrieve the actual module name of an alias used for this module (if any).
///
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class FrontendOptions {
/// The name of the library to link against when using this module.
std::string ModuleLinkName;

/// The name of the package this module belongs to.
std::string PackageName;

/// Arguments which should be passed in immediate mode.
std::vector<std::string> ImmediateArgv;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ def autolink_force_load : Flag<["-"], "autolink-force-load">,
def module_abi_name : Separate<["-"], "module-abi-name">,
Flags<[FrontendOption, ModuleInterfaceOption]>,
HelpText<"ABI name to use for the contents of this module">;
def package_name : Separate<["-"], "package-name">,
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,
HelpText<"Name of the package the module belongs to">;

def emit_module : Flag<["-"], "emit-module">,
Flags<[FrontendOption, NoInteractiveOption, SupplementaryOutput]>,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ExtendedValidationInfo {
SmallVector<StringRef, 4> ExtraClangImporterOpts;
std::string SDKPath;
StringRef ModuleABIName;
StringRef ModulePackageName;
struct {
unsigned ArePrivateImportsEnabled : 1;
unsigned IsSIB : 1;
Expand Down Expand Up @@ -179,6 +180,9 @@ class ExtendedValidationInfo {
StringRef getModuleABIName() const { return ModuleABIName; }
void setModuleABIName(StringRef name) { ModuleABIName = name; }

StringRef getModulePackageName() const { return ModulePackageName; }
void setModulePackageName(StringRef name) { ModulePackageName = name; }

bool isConcurrencyChecked() const {
return Bits.IsConcurrencyChecked;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);
inputArgs.AddLastArg(arguments, options::OPT_module_abi_name);
inputArgs.AddLastArg(arguments, options::OPT_package_name);
inputArgs.AddLastArg(arguments, options::OPT_nostdimport);
inputArgs.AddLastArg(arguments, options::OPT_parse_stdlib);
inputArgs.AddLastArg(arguments, options::OPT_resource_dir);
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ bool ArgsToFrontendOptionsConverter::convert(
if (const Arg *A = Args.getLastArg(OPT_module_link_name))
Opts.ModuleLinkName = A->getValue();

if (const Arg *A = Args.getLastArg(OPT_package_name))
Opts.PackageName = A->getValue();

// This must be called after computing module name, module abi name,
// and module link name. If computing module aliases is unsuccessful,
// return early.
Expand Down
4 changes: 4 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
MainModule->setABIName(getASTContext().getIdentifier(
Invocation.getFrontendOptions().ModuleABIName));
}
if (!Invocation.getFrontendOptions().PackageName.empty()) {
MainModule->setPackageName(getASTContext().getIdentifier(
Invocation.getFrontendOptions().PackageName));
}
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))
Expand Down
3 changes: 3 additions & 0 deletions lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ class ModuleFile
return Core->Name;
}

StringRef getModulePackageName() const {
return Core->ModulePackageName;
}
/// The ABI name of the module.
StringRef getModuleABIName() const {
return Core->ModuleABIName;
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
case options_block::IS_CONCURRENCY_CHECKED:
extendedInfo.setIsConcurrencyChecked(true);
break;
case options_block::MODULE_PACKAGE_NAME:
extendedInfo.setModulePackageName(blobData);
break;
default:
// Unknown options record, possibly for use by a future version of the
// module format.
Expand Down Expand Up @@ -1346,6 +1349,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
Bits.IsConcurrencyChecked = extInfo.isConcurrencyChecked();
MiscVersion = info.miscVersion;
ModuleABIName = extInfo.getModuleABIName();
ModulePackageName = extInfo.getModulePackageName();

hasValidControlBlock = true;
break;
Expand Down
3 changes: 3 additions & 0 deletions lib/Serialization/ModuleFileSharedCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class ModuleFileSharedCore {
/// The module ABI name.
StringRef ModuleABIName;

/// The name of the package this module belongs to.
StringRef ModulePackageName;

/// \c true if this module has incremental dependency information.
bool HasIncrementalInfo = false;

Expand Down
8 changes: 7 additions & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 727; // closure capture args
const uint16_t SWIFTMODULE_VERSION_MINOR = 728; // package name field

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -836,6 +836,7 @@ namespace options_block {
IS_ALLOW_MODULE_WITH_COMPILER_ERRORS_ENABLED,
MODULE_ABI_NAME,
IS_CONCURRENCY_CHECKED,
MODULE_PACKAGE_NAME,
};

using SDKPathLayout = BCRecordLayout<
Expand Down Expand Up @@ -894,6 +895,11 @@ namespace options_block {
using IsConcurrencyCheckedLayout = BCRecordLayout<
IS_CONCURRENCY_CHECKED
>;

using ModulePackageNameLayout = BCRecordLayout<
MODULE_PACKAGE_NAME,
BCBlob
>;
}

/// The record types within the input block.
Expand Down
6 changes: 6 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ void Serializer::writeBlockInfoBlock() {
BLOCK_RECORD(options_block, IS_ALLOW_MODULE_WITH_COMPILER_ERRORS_ENABLED);
BLOCK_RECORD(options_block, MODULE_ABI_NAME);
BLOCK_RECORD(options_block, IS_CONCURRENCY_CHECKED);
BLOCK_RECORD(options_block, MODULE_PACKAGE_NAME);

BLOCK(INPUT_BLOCK);
BLOCK_RECORD(input_block, IMPORTED_MODULE);
Expand Down Expand Up @@ -1064,6 +1065,11 @@ void Serializer::writeHeader(const SerializationOptions &options) {
ABIName.emit(ScratchRecord, M->getABIName().str());
}

if (!M->getPackageName().empty()) {
options_block::ModulePackageNameLayout PackageName(Out);
PackageName.emit(ScratchRecord, M->getPackageName().str());
}

if (M->isConcurrencyChecked()) {
options_block::IsConcurrencyCheckedLayout IsConcurrencyChecked(Out);
IsConcurrencyChecked.emit(ScratchRecord);
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
M.setABIName(Ctx.getIdentifier(loadedModuleFile->getModuleABIName()));
if (loadedModuleFile->isConcurrencyChecked())
M.setIsConcurrencyChecked();
if (!loadedModuleFile->getModulePackageName().empty())
M.setPackageName(Ctx.getIdentifier(loadedModuleFile->getModulePackageName()));
M.setUserModuleVersion(loadedModuleFile->getUserModuleVersion());
for (auto name: loadedModuleFile->getAllowableClientNames()) {
M.addAllowableClientName(Ctx.getIdentifier(name));
Expand Down
15 changes: 15 additions & 0 deletions test/Serialization/module_package_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -module-name Logging -package-name MyLoggingPkg %t/File.swift -emit-module -emit-module-path %t/Logging.swiftmodule
// RUN: test -f %t/Logging.swiftmodule
// RUN: llvm-bcanalyzer -dump %t/Logging.swiftmodule | %FileCheck %s -check-prefix CHECK-BLOB
// CHECK-BLOB: <MODULE_PACKAGE_NAME abbrevid=5/> blob data = 'MyLoggingPkg'

// RUN: %target-swift-frontend -module-name Logging -package-name MyLoggingPkg %t/File.swift -emit-module -emit-module-interface-path %t/Logging.swiftinterface -swift-version 5 -enable-library-evolution -I %t
// RUN: test -f %t/Logging.swiftinterface
// RUN: %FileCheck %s -input-file %t/Logging.swiftinterface -check-prefix CHECK-FLAG
// CHECK-FLAG: -package-name MyLoggingPkg

// BEGIN File.swift
public func log(level: Int) {}

0 comments on commit 7323d32

Please sign in to comment.