Skip to content

[Glibc] Configure modulemap for target, not host #1679

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
Mar 16, 2016
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
31 changes: 30 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
// Enable modules.
"-fmodules",

// Enable implicit module maps
// Enable implicit module maps (this option is implied by "-fmodules").
"-fimplicit-module-maps",

// Don't emit LLVM IR.
Expand Down Expand Up @@ -358,6 +358,35 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
// Just use the most feature-rich C language mode.
"-x", "c", "-std=gnu11",
});

// The module map used for Glibc depends on the target we're compiling for,
// and is not included in the resource directory with the other implicit
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
SmallString<128> GlibcModuleMapPath;
if (!importerOpts.OverrideResourceDir.empty()) {
GlibcModuleMapPath = importerOpts.OverrideResourceDir;
} else if (!searchPathOpts.RuntimeResourcePath.empty()) {
GlibcModuleMapPath = searchPathOpts.RuntimeResourcePath;
}

// Running without a resource directory is not a supported configuration.
assert(!GlibcModuleMapPath.empty());

llvm::sys::path::append(
GlibcModuleMapPath,
swift::getPlatformNameForTriple(triple), triple.getArchName(),
"glibc.modulemap");

// 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 -emit-ir` is invoked using
// a Swift compiler not built for Linux targets.
if (llvm::sys::fs::exists(GlibcModuleMapPath)) {
invocationArgStrs.push_back(
(Twine("-fmodule-map-file=") + GlibcModuleMapPath).str());
} else {
// FIXME: Emit a warning of some kind.
}
}

if (triple.isOSDarwin()) {
Expand Down
5 changes: 1 addition & 4 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ if(SWIFT_BUILD_STDLIB)
add_subdirectory(stubs)
add_subdirectory(core)
add_subdirectory(SwiftOnoneSupport)
add_subdirectory(Glibc)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if(SWIFT_BUILD_SDK_OVERLAY)
add_subdirectory(SDK)
endif()
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
add_subdirectory(Glibc)
endif()
84 changes: 48 additions & 36 deletions stdlib/public/Glibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,56 @@ set(sources
Misc.c
)

set(output_dir "${SWIFTLIB_DIR}/glibc")
# When cross-compiling the stdlib on Unix platforms, we'll need a separate
# glibc for each target.
foreach(SDK ${SWIFT_SDKS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To actually make this work, you need to change this condition in the parent CMakeLists.txt:

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  add_subdirectory(Glibc)
endif()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent CMakeLists.txt already has those very same lines! And to be clear: this does actually work, both on its own when building for Linux (and I assume FreeBSD), and when #1442 is rebased on top of these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I misread--you're saying that condition needs to change somehow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Imagine trying to cross-compile from OS X to Linux. The Glibc directory won't even be considered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! And that, in fact, was the very problem with the OS X CI. Thanks for pointing this out.

foreach(arch ${SWIFT_SDK_${SDK}_ARCHITECTURES})
set(output_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${SDK}_LIB_SUBDIR}/${arch}")

# Set correct paths to glibc headers
set(GLIBC_INCLUDE_PATH "/usr/include")
if(CMAKE_LIBRARY_ARCHITECTURE)
set(GLIBC_ARCH_INCLUDE_PATH "${GLIBC_INCLUDE_PATH}/${CMAKE_LIBRARY_ARCHITECTURE}")
else()
set(GLIBC_ARCH_INCLUDE_PATH "${GLIBC_INCLUDE_PATH}")
endif()
if (NOT EXISTS "${GLIBC_ARCH_INCLUDE_PATH}/sys")
message(FATAL_ERROR "Glibc headers were not found.")
endif()
# Determine the location of glibc based on the target.
set(GLIBC_INCLUDE_PATH "/usr/include")
set(GLIBC_ARCH_INCLUDE_PATH "${GLIBC_INCLUDE_PATH}")
if(("${SDK}" STREQUAL "LINUX" OR "${SDK}" STREQUAL "FREEBSD") AND CMAKE_LIBRARY_ARCHITECTURE)
# FIXME: Some distributions install headers in
# "/usr/include/x86_64-linux-gnu/sys/...". Here we use the host
# machine's path, regardless of the SDK target we're building for.
# This will break if cross-compiling from a distro that uses the
# architecture as part of the path to a distro that does not.
set(GLIBC_ARCH_INCLUDE_PATH "${GLIBC_INCLUDE_PATH}/${CMAKE_LIBRARY_ARCHITECTURE}")
endif()

# Generate module.map
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
configure_file(module.map.in "${CMAKE_CURRENT_BINARY_DIR}/module.map" @ONLY)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
configure_file(module.freebsd.map.in "${CMAKE_CURRENT_BINARY_DIR}/module.map" @ONLY)
endif()
# Configure the modulemap based on the target. Each platform needs to
# reference different headers, based on what's available in their glibc.
set(modulemap_path "${CMAKE_CURRENT_BINARY_DIR}/${SWIFT_SDK_${SDK}_LIB_SUBDIR}/${arch}/module.map")
if("${SDK}" STREQUAL "FREEBSD")
configure_file(module.freebsd.map.in "${modulemap_path}" @ONLY)
else()
configure_file(module.map.in "${modulemap_path}" @ONLY)
endif()

add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${output_dir}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy_if_different"
"${CMAKE_CURRENT_BINARY_DIR}/module.map"
"${output_dir}/module.map"
CUSTOM_TARGET_NAME "copy_glibc_module"
OUTPUT "${output_dir}/module.map" "${output_dir}"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/module.map"
COMMENT "Copying Glibc module to ${output_dir}")
set(VARIANT_SUFFIX "-${SWIFT_SDK_${SDK}_LIB_SUBDIR}-${arch}")
add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${output_dir}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy_if_different"
"${modulemap_path}"
"${output_dir}/glibc.modulemap"
CUSTOM_TARGET_NAME "copy_glibc_module${VARIANT_SUFFIX}"
OUTPUT "${output_dir}/glibc.modulemap" "${output_dir}"
DEPENDS "${modulemap_path}"
COMMENT "Copying Glibc module to ${output_dir}")

swift_install_in_component(stdlib
FILES "${output_dir}/module.map"
DESTINATION "lib/swift/glibc")
swift_install_in_component(stdlib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we've got a problem: this shows up as part of the install component even when we're not building for Linux or FreeBSD. Maybe this just needs to go in the if below? (Maybe everything needs to be guarded by that if?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I believe I may have misinterpreted some of the comments in the pull request review. I could place an if("${SDK}" STREQUAL "LINUX" OR "${SDK}" STREQUAL "${FREEBSD}") at the very beginning of the inner loop--does that sound right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Why would "${FREEBSD}" be a variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy-pasted from five lines down, but you're absolutely right. I'll change that part, too. 😅

FILES "${output_dir}/glibc.modulemap"
DESTINATION "${output_dir}")

add_swift_library(swiftGlibc IS_SDK_OVERLAY
${sources}
FILE_DEPENDS copy_glibc_module "${output_dir}"
INSTALL_IN_COMPONENT stdlib-experimental)
if("${SDK}" STREQUAL "LINUX" OR "${SDK}" STREQUAL "${FREEBSD}")
add_swift_library(swiftGlibc IS_SDK_OVERLAY
${sources}
FILE_DEPENDS "copy_glibc_module${VARIANT_SUFFIX}" "${output_dir}"
TARGET_SDKS "${SDK}"
INSTALL_IN_COMPONENT stdlib-experimental)
endif()
endforeach()
endforeach()
14 changes: 12 additions & 2 deletions tools/driver/modulewrap_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,20 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
return 1;
}

// Wrap the bitstream in an object file.
// Wrap the bitstream in a module object file. To use the ClangImporter to
// create the module loader, we need to properly set the runtime library path.
SearchPathOptions SearchPathOpts;
// FIXME: This logic has been duplicated from
// CompilerInvocation::setMainExecutablePath. ModuleWrapInvocation
// should share its implementation.
SmallString<128> RuntimeResourcePath(MainExecutablePath);
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /swift
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /bin
llvm::sys::path::append(RuntimeResourcePath, "lib", "swift");
SearchPathOpts.RuntimeResourcePath = RuntimeResourcePath.str();

SourceManager SrcMgr;
LangOptions LangOpts;
SearchPathOptions SearchPathOpts;
LangOpts.Target = Invocation.getTargetTriple();
ASTContext ASTCtx(LangOpts, SearchPathOpts, SrcMgr, Instance.getDiags());
ClangImporterOptions ClangImporterOpts;
Expand Down