Skip to content

[cxx-interop] Make CxxShim header and modulemap arch-independent #66765

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 1 commit into from
Jun 21, 2023
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
20 changes: 16 additions & 4 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using Path = SmallString<128>;

static Optional<Path> getActualModuleMapPath(
StringRef name, SearchPathOptions &Opts, const llvm::Triple &triple,
bool isArchSpecific,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
StringRef platform = swift::getPlatformNameForTriple(triple);
StringRef arch = swift::getMajorArchitectureName(triple);
Expand All @@ -37,7 +38,11 @@ static Optional<Path> getActualModuleMapPath(
if (!SDKPath.empty()) {
result.append(SDKPath.begin(), SDKPath.end());
llvm::sys::path::append(result, "usr", "lib", "swift");
llvm::sys::path::append(result, platform, arch, name);
llvm::sys::path::append(result, platform, arch);
if (isArchSpecific) {
llvm::sys::path::append(result, arch);
}
llvm::sys::path::append(result, name);

// Only specify the module map if that file actually exists. It may not;
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
Expand All @@ -50,7 +55,11 @@ static Optional<Path> getActualModuleMapPath(
result.clear();
result.append(Opts.RuntimeResourcePath.begin(),
Opts.RuntimeResourcePath.end());
llvm::sys::path::append(result, platform, arch, name);
llvm::sys::path::append(result, platform);
if (isArchSpecific) {
llvm::sys::path::append(result, arch);
}
llvm::sys::path::append(result, name);

// Only specify the module map if that file actually exists. It may not;
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
Expand Down Expand Up @@ -88,19 +97,22 @@ static llvm::Optional<Path> getInjectedModuleMapPath(
static Optional<Path> getGlibcModuleMapPath(
SearchPathOptions &Opts, const llvm::Triple &triple,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
return getActualModuleMapPath("glibc.modulemap", Opts, triple, vfs);
return getActualModuleMapPath("glibc.modulemap", Opts, triple,
/*isArchSpecific*/ true, vfs);
}

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

Optional<SmallString<128>>
swift::getCxxShimModuleMapPath(SearchPathOptions &opts,
const llvm::Triple &triple) {
return getActualModuleMapPath("libcxxshim.modulemap", opts, triple,
/*isArchSpecific*/ false,
llvm::vfs::getRealFileSystem());
}

Expand Down
77 changes: 36 additions & 41 deletions stdlib/public/Cxx/cxxshim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,50 @@
set(libcxxshim_modulemap_target_list)
foreach(sdk ${SWIFT_SDKS})
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES} ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES})
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(module_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}")

add_custom_command(OUTPUT ${module_dir}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir}")
if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command(OUTPUT ${module_dir_static}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir_static}")
endif()

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}")

set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")

add_custom_command(OUTPUT ${module_dir}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir}")
if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command(OUTPUT ${module_dir_static}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir_static}")
endif()

set(outputs)
foreach(source libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h)
add_custom_command(OUTPUT ${module_dir}/${source}
add_custom_command(OUTPUT ${module_dir_static}/${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}")

if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command(OUTPUT ${module_dir_static}/${source}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${source}
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${module_dir_static}/${source}"
COMMENT "Copying ${source} to ${module_dir_static}")
list(APPEND outputs "${module_dir_static}/${source}")
endif()
endforeach()
list(APPEND outputs ${module_dir})
if(SWIFT_BUILD_STATIC_STDLIB)
list(APPEND outputs ${module_dir_static})
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${module_dir_static}/${source}"
COMMENT "Copying ${source} to ${module_dir_static}")
list(APPEND outputs "${module_dir_static}/${source}")
endif()
endforeach()
list(APPEND outputs ${module_dir})
if(SWIFT_BUILD_STATIC_STDLIB)
list(APPEND outputs ${module_dir_static})
endif()

add_custom_target(cxxshim-${sdk}-${arch} ALL
DEPENDS ${outputs}
COMMENT "Copying cxxshims to ${module_dir}")
list(APPEND libcxxshim_modulemap_target_list cxxshim-${sdk}-${arch})
add_custom_target(cxxshim-${sdk} ALL
DEPENDS ${outputs}
COMMENT "Copying cxxshims to ${module_dir}")
list(APPEND libcxxshim_modulemap_target_list cxxshim-${sdk})


swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT compiler)
if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift/${arch_subdir}"
DESTINATION "lib/swift_static/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT compiler)
if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift_static/${arch_subdir}"
COMPONENT compiler)
endif()
endforeach()
endif()
endforeach()

add_custom_target(libcxxshim_modulemap DEPENDS ${libcxxshim_modulemap_target_list})
Expand Down