Skip to content

Commit 370bcec

Browse files
authored
Merge pull request #75704 from swiftlang/version-number-hack
DependencyScanner: report user module version in dependency scanning results for binary Swift modules
2 parents 31a9cd9 + b4d64bd commit 370bcec

13 files changed

+70
-12
lines changed

include/swift-c/DependencyScan/DependencyScan.h

+3
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ SWIFTSCAN_PUBLIC swiftscan_string_ref_t
251251
swiftscan_swift_binary_detail_get_module_cache_key(
252252
swiftscan_module_details_t details);
253253

254+
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
255+
swiftscan_swift_binary_detail_get_user_module_version(
256+
swiftscan_module_details_t details);
254257
//=== Swift Placeholder Module Details query APIs -------------------------===//
255258

256259
SWIFTSCAN_PUBLIC swiftscan_string_ref_t

include/swift/AST/ModuleDependencies.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,15 @@ class SwiftBinaryModuleDependencyStorage
398398
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
399399
ArrayRef<LinkLibrary> linkLibraries, StringRef headerImport,
400400
StringRef definingModuleInterface, bool isFramework, bool isStatic,
401-
StringRef moduleCacheKey)
401+
StringRef moduleCacheKey, StringRef userModuleVersion)
402402
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary,
403403
moduleImports, optionalModuleImports,
404404
linkLibraries, moduleCacheKey),
405405
compiledModulePath(compiledModulePath), moduleDocPath(moduleDocPath),
406406
sourceInfoPath(sourceInfoPath), headerImport(headerImport),
407407
definingModuleInterfacePath(definingModuleInterface),
408-
isFramework(isFramework), isStatic(isStatic) {}
408+
isFramework(isFramework), isStatic(isStatic),
409+
userModuleVersion(userModuleVersion) {}
409410

410411
ModuleDependencyInfoStorageBase *clone() const override {
411412
return new SwiftBinaryModuleDependencyStorage(*this);
@@ -439,6 +440,9 @@ class SwiftBinaryModuleDependencyStorage
439440
/// A flag that indicates this dependency is associated with a static archive
440441
const bool isStatic;
441442

443+
/// The user module version of this binary module.
444+
const std::string userModuleVersion;
445+
442446
/// Return the path to the defining .swiftinterface of this module
443447
/// of one was determined. Otherwise, return the .swiftmodule path
444448
/// itself.
@@ -604,12 +608,13 @@ class ModuleDependencyInfo {
604608
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
605609
ArrayRef<LinkLibrary> linkLibraries, StringRef headerImport,
606610
StringRef definingModuleInterface, bool isFramework,
607-
bool isStatic, StringRef moduleCacheKey) {
611+
bool isStatic, StringRef moduleCacheKey, StringRef userModuleVer) {
608612
return ModuleDependencyInfo(
609613
std::make_unique<SwiftBinaryModuleDependencyStorage>(
610614
compiledModulePath, moduleDocPath, sourceInfoPath, moduleImports,
611615
optionalModuleImports, linkLibraries, headerImport,
612-
definingModuleInterface,isFramework, isStatic, moduleCacheKey));
616+
definingModuleInterface,isFramework, isStatic, moduleCacheKey,
617+
userModuleVer));
613618
}
614619

615620
/// Describe the main Swift module.

include/swift/DependencyScan/DependencyScanImpl.h

+3
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ typedef struct {
166166

167167
/// ModuleCacheKey
168168
swiftscan_string_ref_t module_cache_key;
169+
170+
/// User module version
171+
swiftscan_string_ref_t user_module_version;
169172
} swiftscan_swift_binary_details_t;
170173

171174
/// Swift placeholder modules carry additional details that specify their

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ using SwiftBinaryModuleDetailsLayout =
178178
FileIDArrayIDField, // headerSourceFiles
179179
IsFrameworkField, // isFramework
180180
IsStaticField, // isStatic
181-
IdentifierIDField // moduleCacheKey
181+
IdentifierIDField, // moduleCacheKey
182+
IdentifierIDField // UserModuleVersion
182183
>;
183184

184185
using SwiftPlaceholderModuleDetailsLayout =

lib/DependencyScan/DependencyScanJSON.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ void writeJSON(llvm::raw_ostream &out,
550550
swiftBinaryDeps->compiled_module_path,
551551
/*indentLevel=*/5,
552552
/*trailingComma=*/true);
553+
writeJSONSingleField(out, "userModuleVersion",
554+
swiftBinaryDeps->user_module_version,
555+
/*indentLevel=*/5,
556+
/*trailingComma=*/true);
553557
// Module doc file
554558
if (swiftBinaryDeps->module_doc_path.data &&
555559
get_C_string(swiftBinaryDeps->module_doc_path)[0] != '\0')

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,13 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
490490
overlayDependencyIDArrayID, headerImportID,
491491
headerModuleDependenciesArrayID,
492492
headerImportsSourceFilesArrayID, isFramework, isStatic,
493-
moduleCacheKeyID;
493+
moduleCacheKeyID, userModuleVersionID;
494494
SwiftBinaryModuleDetailsLayout::readRecord(
495495
Scratch, compiledModulePathID, moduleDocPathID,
496496
moduleSourceInfoPathID, overlayDependencyIDArrayID,
497497
headerImportID, headerModuleDependenciesArrayID,
498498
headerImportsSourceFilesArrayID, isFramework, isStatic,
499-
moduleCacheKeyID);
499+
moduleCacheKeyID, userModuleVersionID);
500500

501501
auto compiledModulePath = getIdentifier(compiledModulePathID);
502502
if (!compiledModulePath)
@@ -510,6 +510,9 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
510510
auto moduleCacheKey = getIdentifier(moduleCacheKeyID);
511511
if (!moduleCacheKey)
512512
llvm::report_fatal_error("Bad moduleCacheKey");
513+
auto userModuleVersion = getIdentifier(userModuleVersionID);
514+
if (!userModuleVersion)
515+
llvm::report_fatal_error("Bad userModuleVersion");
513516
auto headerImport = getIdentifier(headerImportID);
514517
if (!headerImport)
515518
llvm::report_fatal_error("Bad binary direct dependencies: no header import");
@@ -519,7 +522,7 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
519522
auto moduleDep = ModuleDependencyInfo::forSwiftBinaryModule(
520523
*compiledModulePath, *moduleDocPath, *moduleSourceInfoPath,
521524
currentModuleImports, currentOptionalModuleImports, {},
522-
*headerImport, "", isFramework, isStatic, *moduleCacheKey);
525+
*headerImport, "", isFramework, isStatic, *moduleCacheKey, *userModuleVersion);
523526

524527
auto headerModuleDependencies = getStringArray(headerModuleDependenciesArrayID);
525528
if (!headerModuleDependencies)
@@ -1050,7 +1053,8 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(
10501053
getArrayID(moduleID, ModuleIdentifierArrayKind::HeaderInputModuleDependencies),
10511054
getArrayID(moduleID, ModuleIdentifierArrayKind::HeaderInputDependencySourceFiles),
10521055
swiftBinDeps->isFramework, swiftBinDeps->isStatic,
1053-
getIdentifier(swiftBinDeps->moduleCacheKey));
1056+
getIdentifier(swiftBinDeps->moduleCacheKey),
1057+
getIdentifier(swiftBinDeps->userModuleVersion));
10541058

10551059
break;
10561060
}

lib/DependencyScan/ScanDependencies.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,8 @@ generateFullDependencyGraph(const CompilerInstance &instance,
761761
create_set(swiftBinaryDeps->headerSourceFiles),
762762
swiftBinaryDeps->isFramework,
763763
swiftBinaryDeps->isStatic,
764-
create_clone(swiftBinaryDeps->moduleCacheKey.c_str())};
764+
create_clone(swiftBinaryDeps->moduleCacheKey.c_str()),
765+
create_clone(swiftBinaryDeps->userModuleVersion.c_str())};
765766
} else {
766767
// Clang module details
767768
details->kind = SWIFTSCAN_DEPENDENCY_INFO_CLANG;

lib/Serialization/ModuleFileSharedCore.h

+4
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ class ModuleFileSharedCore {
623623
return Bits.IsStaticLibrary;
624624
}
625625

626+
llvm::VersionTuple getUserModuleVersion() const {
627+
return UserModuleVersion;
628+
}
629+
626630
/// If the module-defining `.swiftinterface` file is an SDK-relative path,
627631
/// resolve it to be absolute to the specified SDK.
628632
std::string resolveModuleDefiningFilePath(const StringRef SDKPath) const;

lib/Serialization/SerializedModuleLoader.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,13 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
515515
std::string definingModulePath =
516516
loadedModuleFile->resolveModuleDefiningFilePath(Ctx.SearchPathOpts.getSDKPath());
517517

518+
std::string userModuleVer = loadedModuleFile->getUserModuleVersion().getAsString();
518519
// Map the set of dependencies over to the "module dependencies".
519520
auto dependencies = ModuleDependencyInfo::forSwiftBinaryModule(
520521
modulePath.str(), moduleDocPath, sourceInfoPath, moduleImports,
521522
optionalModuleImports, linkLibraries, importedHeader,
522523
definingModulePath, isFramework, loadedModuleFile->isStaticLibrary(),
523-
/*module-cache-key*/ "");
524+
/*module-cache-key*/ "", userModuleVer);
524525

525526
return std::move(dependencies);
526527
}

test/ScanDependencies/binary_framework_dependency.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Foo
2323
// CHECK-NEXT: "directDependencies": [
2424
// CHECK: "details": {
2525
// CHECK-NEXT: "swiftPrebuiltExternal": {
26-
// CHECK-NEXT: "compiledModulePath":
26+
// CHECK-NEXT: "compiledModulePath":
27+
// CHECK-NEXT: "userModuleVersion":
2728
// CHECK-NEXT: "isFramework": true
2829
// CHECK-NEXT: }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/clang-module-cache)
4+
// RUN: %empty-directory(%t/DependencyModules)
5+
6+
// Emit a textual module dependency
7+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/DependencyModules/Foo.swiftmodule -module-cache-path %t/clang-module-cache -module-name Foo %s -D FOO -user-module-version 42.3.3
8+
9+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %t/DependencyModules/ -module-name main
10+
11+
// Check the contents of the JSON output
12+
// RUN: %FileCheck -check-prefix CHECK %s < %t/deps.json
13+
14+
#if FOO
15+
16+
public func foo() {}
17+
18+
#else
19+
import Foo
20+
21+
#endif
22+
23+
// CHECK: "userModuleVersion": "42.3.3.0"

tools/libSwiftScan/libSwiftScan.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ void swiftscan_dependency_info_details_dispose(
8888
details_impl->swift_binary_details.header_dependency);
8989
swiftscan_string_dispose(
9090
details_impl->swift_binary_details.module_cache_key);
91+
swiftscan_string_dispose(
92+
details_impl->swift_binary_details.user_module_version);
9193
break;
9294
case SWIFTSCAN_DEPENDENCY_INFO_SWIFT_PLACEHOLDER:
9395
swiftscan_string_dispose(
@@ -437,6 +439,11 @@ swiftscan_string_ref_t swiftscan_swift_binary_detail_get_module_cache_key(
437439
return details->swift_binary_details.module_cache_key;
438440
}
439441

442+
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
443+
swiftscan_swift_binary_detail_get_user_module_version(
444+
swiftscan_module_details_t details) {
445+
return details->swift_binary_details.user_module_version;
446+
}
440447

441448
//=== Swift Placeholder Module Details query APIs -------------------------===//
442449

tools/libSwiftScan/libSwiftScan.exports

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ swiftscan_swift_binary_detail_get_header_dependency
3232
swiftscan_swift_binary_detail_get_header_dependency_module_dependencies
3333
swiftscan_swift_binary_detail_get_is_framework
3434
swiftscan_swift_binary_detail_get_module_cache_key
35+
swiftscan_swift_binary_detail_get_user_module_version
3536
swiftscan_swift_placeholder_detail_get_compiled_module_path
3637
swiftscan_swift_placeholder_detail_get_module_doc_path
3738
swiftscan_swift_placeholder_detail_get_module_source_info_path

0 commit comments

Comments
 (0)