From e2ea6cb824a070e8ab6cc0dd465f190409bc0fab Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Thu, 11 May 2023 13:21:59 -0700 Subject: [PATCH] [Explicit Module Builds] Add 'ClangImporter' setting for explicitly-built modules This will mean that '-disable-implicit-swift-modules' also automatically implies two things: 1. Clang modules must also be explicit, and the importer's clang instance will get '-fno-implicit-modules' and '-fno-implicit-module-maps' 2. The importer's clang instance will no longer get a '-fmodules-cache-path=', since it is not needed in explicit builds --- include/swift/Basic/LangOptions.h | 4 ++++ include/swift/Frontend/FrontendOptions.h | 4 ++-- lib/ClangImporter/ClangImporter.cpp | 7 ++++++- lib/Frontend/CompilerInvocation.cpp | 3 +++ .../fail_implicit_concurrency_load.swift | 15 +++++++++++++++ test/Frontend/module-alias-explicit-build.swift | 16 +++++++++++++++- test/ScanDependencies/can_import_with_map.swift | 17 ++++++++++++++++- .../explicit-framework-irgen.swift | 14 ++++++++++++++ .../explicit-module-map-clang-and-swift.swift | 15 +++++++++++++++ 9 files changed, 90 insertions(+), 5 deletions(-) diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index f39f06d09ca4e..00a1a79710cd8 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -878,6 +878,10 @@ namespace swift { /// and completely bypass the Clang driver. bool DirectClangCC1ModuleBuild = false; + /// Disable implicitly-built Clang modules because they are explicitly + /// built and provided to the compiler invocation. + bool DisableImplicitClangModules = false; + /// Return a hash code of any components from these options that should /// contribute to a Swift Bridging PCH hash. llvm::hash_code getPCHHashComponents() const { diff --git a/include/swift/Frontend/FrontendOptions.h b/include/swift/Frontend/FrontendOptions.h index 7a080bd110815..2b17e556d51b9 100644 --- a/include/swift/Frontend/FrontendOptions.h +++ b/include/swift/Frontend/FrontendOptions.h @@ -322,8 +322,8 @@ class FrontendOptions { /// By default, we include ImplicitObjCHeaderPath directly. llvm::Optional BridgingHeaderDirForPrint; - /// Disable implicitly built Swift modules because they are explicitly - /// built and given to the compiler invocation. + /// Disable implicitly-built Swift modules because they are explicitly + /// built and provided to the compiler invocation. bool DisableImplicitModules = false; /// Disable building Swift modules from textual interfaces. This should be diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index d1a47bb2223c3..b57f8a3cb79fd 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -669,11 +669,16 @@ importer::getNormalInvocationArguments( } const std::string &moduleCachePath = importerOpts.ModuleCachePath; - if (!moduleCachePath.empty()) { + if (!moduleCachePath.empty() && !importerOpts.DisableImplicitClangModules) { invocationArgStrs.push_back("-fmodules-cache-path="); invocationArgStrs.back().append(moduleCachePath); } + if (importerOpts.DisableImplicitClangModules) { + invocationArgStrs.push_back("-fno-implicit-modules"); + invocationArgStrs.push_back("-fno-implicit-module-maps"); + } + if (ctx.SearchPathOpts.DisableModulesValidateSystemDependencies) { invocationArgStrs.push_back("-fno-modules-validate-system-headers"); } else { diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index b96bd1ed45a5e..438811a9fb2c5 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -1460,6 +1460,9 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, Opts.PCHDisableValidation |= Args.hasArg(OPT_pch_disable_validation); } + if (FrontendOpts.DisableImplicitModules) + Opts.DisableImplicitClangModules = true; + Opts.ValidateModulesOnce |= Args.hasArg(OPT_validate_clang_modules_once); if (auto *A = Args.getLastArg(OPT_clang_build_session_file)) Opts.BuildSessionFilePath = A->getValue(); diff --git a/test/Concurrency/fail_implicit_concurrency_load.swift b/test/Concurrency/fail_implicit_concurrency_load.swift index 1b3f99418e298..7b51f8c6969c6 100644 --- a/test/Concurrency/fail_implicit_concurrency_load.swift +++ b/test/Concurrency/fail_implicit_concurrency_load.swift @@ -1,9 +1,12 @@ // This test ensures that if implicit import of the Concurrency module is enabled, // but no such module can be located (here verified by forcing explicit modules), // a warning diagnostic is emitted. +// UNSUPPORTED: OS=windows-msvc // REQUIRES: concurrency // RUN: %empty-directory(%t) // RUN: mkdir -p %t/inputs +// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm // RUN: echo "[{" > %/t/inputs/map.json // RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json @@ -11,6 +14,18 @@ // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}," >> %/t/inputs/map.json // RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/_SwiftConcurrencyShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json // RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json // RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json diff --git a/test/Frontend/module-alias-explicit-build.swift b/test/Frontend/module-alias-explicit-build.swift index fcfca550f1e59..eb2dedef5bd8a 100644 --- a/test/Frontend/module-alias-explicit-build.swift +++ b/test/Frontend/module-alias-explicit-build.swift @@ -1,5 +1,5 @@ /// Test the -module-alias flag with an explicit module loader. - +// UNSUPPORTED: OS=windows-msvc // RUN: %empty-directory(%t) // RUN: mkdir -p %t/inputs // RUN: mkdir -p %t/outputs @@ -7,6 +7,8 @@ /// Create a module Bar // RUN: echo 'public func bar() {}' > %t/inputs/FileBar.swift // RUN: %target-swift-frontend -module-name Bar %t/inputs/FileBar.swift -emit-module -emit-module-path %t/inputs/Bar.swiftmodule +// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm /// Check Bar.swiftmodule is created // RUN: test -f %t/inputs/Bar.swiftmodule @@ -35,6 +37,18 @@ // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}," >> %/t/inputs/map.json // RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/_SwiftConcurrencyShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json // RUN: echo "\"moduleName\": \"_StringProcessing\"," >> %/t/inputs/map.json // RUN: echo "\"modulePath\": \"%/string_processing_module\"," >> %/t/inputs/map.json // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json diff --git a/test/ScanDependencies/can_import_with_map.swift b/test/ScanDependencies/can_import_with_map.swift index 7f1a42b047800..992175768a763 100644 --- a/test/ScanDependencies/can_import_with_map.swift +++ b/test/ScanDependencies/can_import_with_map.swift @@ -1,8 +1,11 @@ +// UNSUPPORTED: OS=windows-msvc // RUN: %empty-directory(%t) // RUN: mkdir -p %t/clang-module-cache // RUN: mkdir -p %t/inputs // RUN: echo "public func foo() {}" >> %t/foo.swift // RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo +// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm // RUN: echo "[{" > %/t/inputs/map.json // RUN: echo "\"moduleName\": \"Foo\"," >> %/t/inputs/map.json @@ -32,12 +35,24 @@ // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}," >> %/t/inputs/map.json // RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/_SwiftConcurrencyShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json // RUN: echo "\"moduleName\": \"Distributed\"," >> %/t/inputs/map.json // RUN: echo "\"modulePath\": \"%/distributed_module\"," >> %/t/inputs/map.json // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}]" >> %/t/inputs/map.json -// RUN: %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -disable-implicit-swift-modules +// RUN: %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -disable-implicit-swift-modules -disable-implicit-concurrency-module-import #if canImport(Foo) import Foo #endif diff --git a/test/ScanDependencies/explicit-framework-irgen.swift b/test/ScanDependencies/explicit-framework-irgen.swift index 3f62fb8fb0e06..dc77f3c8aaae4 100644 --- a/test/ScanDependencies/explicit-framework-irgen.swift +++ b/test/ScanDependencies/explicit-framework-irgen.swift @@ -4,6 +4,8 @@ // RUN: echo "/// Some cool comments" > %t/foo.swift // RUN: echo "public func foo() {}" >> %t/foo.swift // RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo +// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm // RUN: echo "[{" > %/t/inputs/map.json // RUN: echo "\"moduleName\": \"Foo\"," >> %/t/inputs/map.json @@ -33,6 +35,18 @@ // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}," >> %/t/inputs/map.json // RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/_SwiftConcurrencyShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json // RUN: echo "\"moduleName\": \"Distributed\"," >> %/t/inputs/map.json // RUN: echo "\"modulePath\": \"%/distributed_module\"," >> %/t/inputs/map.json // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json diff --git a/test/ScanDependencies/explicit-module-map-clang-and-swift.swift b/test/ScanDependencies/explicit-module-map-clang-and-swift.swift index 061a8c0306789..4adf2dd7fddf9 100644 --- a/test/ScanDependencies/explicit-module-map-clang-and-swift.swift +++ b/test/ScanDependencies/explicit-module-map-clang-and-swift.swift @@ -1,9 +1,12 @@ +// UNSUPPORTED: OS=windows-msvc // RUN: %empty-directory(%t) // RUN: mkdir -p %t/clang-module-cache // RUN: mkdir -p %t/inputs // RUN: echo "public func anotherFuncA() {}" > %t/A.swift // RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/A.swiftmodule -emit-module-doc-path %t/inputs/A.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/A.swiftsourceinfo -import-underlying-module -I%S/Inputs/CHeaders -module-cache-path %t.module-cache %t/A.swift -module-name A // RUN: %target-swift-emit-pcm -module-name A -o %t/inputs/A.pcm %S/Inputs/CHeaders/module.modulemap +// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift_obj_root/lib/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm // RUN: echo "[{" > %/t/inputs/map.json // RUN: echo "\"moduleName\": \"A\"," >> %/t/inputs/map.json @@ -33,6 +36,18 @@ // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json // RUN: echo "}," >> %/t/inputs/map.json // RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/inputs/map.json +// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json +// RUN: echo "\"clangModuleMapPath\": \"%swift_obj_root/lib/swift/shims/module.modulemap\"," >> %/t/inputs/map.json +// RUN: echo "\"clangModulePath\": \"%t/inputs/_SwiftConcurrencyShims.pcm\"" >> %/t/inputs/map.json +// RUN: echo "}," >> %/t/inputs/map.json +// RUN: echo "{" >> %/t/inputs/map.json // RUN: echo "\"moduleName\": \"_StringProcessing\"," >> %/t/inputs/map.json // RUN: echo "\"modulePath\": \"%/string_processing_module\"," >> %/t/inputs/map.json // RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json