Skip to content

[embedded] Include and use cxxshims in the embedded/ resource dir #78147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ void importer::getNormalInvocationArguments(
}

if (LangOpts.EnableCXXInterop) {
if (auto path = getCxxShimModuleMapPath(searchPathOpts, triple)) {
if (auto path = getCxxShimModuleMapPath(searchPathOpts, LangOpts, triple)) {
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
}
}
Expand Down
22 changes: 15 additions & 7 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ using namespace swift;
using Path = SmallString<128>;

static std::optional<Path> getActualModuleMapPath(
StringRef name, SearchPathOptions &Opts, const llvm::Triple &triple,
bool isArchSpecific,
StringRef name, SearchPathOptions &Opts, const LangOptions &LangOpts,
const llvm::Triple &triple, bool isArchSpecific,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
StringRef platform;
if (swift::tripleIsMacCatalystEnvironment(triple))
platform = "macosx";
else
platform = swift::getPlatformNameForTriple(triple);

if (LangOpts.hasFeature(Feature::Embedded))
platform = "embedded";

StringRef arch = swift::getMajorArchitectureName(triple);

Path result;
Expand Down Expand Up @@ -95,16 +99,18 @@ static std::optional<Path> getInjectedModuleMapPath(
}

static std::optional<Path> getLibStdCxxModuleMapPath(
SearchPathOptions &opts, const llvm::Triple &triple,
SearchPathOptions &opts, const LangOptions &langOpts,
const llvm::Triple &triple,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
return getActualModuleMapPath("libstdcxx.modulemap", opts, triple,
return getActualModuleMapPath("libstdcxx.modulemap", opts, langOpts, triple,
/*isArchSpecific*/ false, vfs);
}

std::optional<SmallString<128>>
swift::getCxxShimModuleMapPath(SearchPathOptions &opts,
const LangOptions &langOpts,
const llvm::Triple &triple) {
return getActualModuleMapPath("libcxxshim.modulemap", opts, triple,
return getActualModuleMapPath("libcxxshim.modulemap", opts, langOpts, triple,
/*isArchSpecific*/ false,
llvm::vfs::getRealFileSystem());
}
Expand Down Expand Up @@ -225,7 +231,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,

Path actualModuleMapPath;
if (auto path = getActualModuleMapPath(modulemapFileName, ctx.SearchPathOpts,
triple, /*isArchSpecific*/ true, vfs))
ctx.LangOpts, triple,
/*isArchSpecific*/ true, vfs))
actualModuleMapPath = path.value();
else
// FIXME: Emit a warning of some kind.
Expand Down Expand Up @@ -305,7 +312,8 @@ static void getLibStdCxxFileMapping(
}

Path actualModuleMapPath;
if (auto path = getLibStdCxxModuleMapPath(ctx.SearchPathOpts, triple, vfs))
if (auto path = getLibStdCxxModuleMapPath(ctx.SearchPathOpts, ctx.LangOpts,
triple, vfs))
actualModuleMapPath = path.value();
else
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/ClangImporter/ClangIncludePaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace swift {

std::optional<SmallString<128>>
getCxxShimModuleMapPath(SearchPathOptions &opts, const llvm::Triple &triple);
getCxxShimModuleMapPath(SearchPathOptions &opts, const LangOptions &langOpts,
const llvm::Triple &triple);

} // namespace swift

Expand Down
21 changes: 21 additions & 0 deletions stdlib/public/Cxx/cxxshim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ foreach(sdk ${SWIFT_SDKS})
endif()
endforeach()

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
set(module_dir "${SWIFTLIB_DIR}/embedded")
add_custom_command(OUTPUT ${module_dir}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir}")
set(outputs)
foreach(source libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h)
add_custom_command(OUTPUT ${module_dir}/${source}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${source}
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${module_dir}/${source}"
COMMENT "Copying ${source} to ${module_dir}")
list(APPEND outputs "${module_dir}/${source}")
endforeach()
add_custom_target(cxxshim-embedded ALL
DEPENDS ${outputs}
COMMENT "Copying cxxshims to ${module_dir}")
list(APPEND libcxxshim_modulemap_target_list cxxshim-embedded)
swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift/embedded"
COMPONENT compiler)
endif()

add_custom_target(libcxxshim_modulemap DEPENDS ${libcxxshim_modulemap_target_list})
set_property(TARGET libcxxshim_modulemap PROPERTY FOLDER "Miscellaneous")
add_dependencies(sdk-overlay libcxxshim_modulemap)
Expand Down
30 changes: 30 additions & 0 deletions test/embedded/cxxshim.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -I %t %t/Main.swift -enable-experimental-feature Embedded -cxx-interoperability-mode=default -c -o %t/a.o -Rmodule-loading

// REQUIRES: swift_in_compiler
// REQUIRES: OS=macosx || OS=linux-gnu
// REQUIRES: swift_feature_Embedded

// BEGIN header.h

struct Base { int field; };
struct Derived : Base {};

// BEGIN module.modulemap

module MyModule {
header "header.h"
}

// BEGIN Main.swift

import MyModule

public func foo() {
var d = Derived()
d.field = 123
}

foo()