Skip to content

Commit c6121d5

Browse files
committed
[Un-revert][Glibc] Configure modulemap for target, not host
This reverts commit f2154ee, which reverted 04e1cd5. The original commit needed to be reverted because of an issue in which install targets were added to OS X builds that did not target Linux. This addresses that issue by guarding all the Linux-only CMake logic with a check for the SDK being built.
1 parent 5befdc3 commit c6121d5

File tree

4 files changed

+92
-43
lines changed

4 files changed

+92
-43
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
319319
// Enable modules.
320320
"-fmodules",
321321

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

325325
// Don't emit LLVM IR.
@@ -382,6 +382,35 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
382382
// Just use the most feature-rich C language mode.
383383
"-x", "c", "-std=gnu11",
384384
});
385+
386+
// The module map used for Glibc depends on the target we're compiling for,
387+
// and is not included in the resource directory with the other implicit
388+
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
389+
SmallString<128> GlibcModuleMapPath;
390+
if (!importerOpts.OverrideResourceDir.empty()) {
391+
GlibcModuleMapPath = importerOpts.OverrideResourceDir;
392+
} else if (!searchPathOpts.RuntimeResourcePath.empty()) {
393+
GlibcModuleMapPath = searchPathOpts.RuntimeResourcePath;
394+
}
395+
396+
// Running without a resource directory is not a supported configuration.
397+
assert(!GlibcModuleMapPath.empty());
398+
399+
llvm::sys::path::append(
400+
GlibcModuleMapPath,
401+
swift::getPlatformNameForTriple(triple), triple.getArchName(),
402+
"glibc.modulemap");
403+
404+
// Only specify the module map if that file actually exists.
405+
// It may not--for example in the case that
406+
// `swiftc -target x86_64-unknown-linux-gnu -emit-ir` is invoked using
407+
// a Swift compiler not built for Linux targets.
408+
if (llvm::sys::fs::exists(GlibcModuleMapPath)) {
409+
invocationArgStrs.push_back(
410+
(Twine("-fmodule-map-file=") + GlibcModuleMapPath).str());
411+
} else {
412+
// FIXME: Emit a warning of some kind.
413+
}
385414
}
386415

387416
if (triple.isOSDarwin()) {

stdlib/public/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ if(SWIFT_BUILD_STDLIB)
3131
add_subdirectory(stubs)
3232
add_subdirectory(core)
3333
add_subdirectory(SwiftOnoneSupport)
34+
add_subdirectory(Glibc)
3435
endif()
3536

3637
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
3738
if(SWIFT_BUILD_SDK_OVERLAY)
3839
add_subdirectory(SDK)
3940
endif()
4041
endif()
41-
42-
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
43-
add_subdirectory(Glibc)
44-
endif()

stdlib/public/Glibc/CMakeLists.txt

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,57 @@ set(sources
33
Misc.c
44
)
55

6-
set(output_dir "${SWIFTLIB_DIR}/glibc")
6+
# When cross-compiling the stdlib on Unix platforms, we'll need a separate
7+
# glibc for each target.
8+
foreach(SDK ${SWIFT_SDKS})
9+
foreach(arch ${SWIFT_SDK_${SDK}_ARCHITECTURES})
10+
# Don't generate Glibc module maps for Darwin targets.
11+
if("${SDK}" STREQUAL "LINUX" OR "${SDK}" STREQUAL "FREEBSD")
12+
set(output_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${SDK}_LIB_SUBDIR}/${arch}")
713

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

19-
# Generate module.map
20-
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
21-
configure_file(module.map.in "${CMAKE_CURRENT_BINARY_DIR}/module.map" @ONLY)
22-
endif()
23-
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
24-
configure_file(module.freebsd.map.in "${CMAKE_CURRENT_BINARY_DIR}/module.map" @ONLY)
25-
endif()
26+
# Configure the module map based on the target. Each platform needs to
27+
# reference different headers, based on what's available in their glibc.
28+
set(modulemap_path "${CMAKE_CURRENT_BINARY_DIR}/${SWIFT_SDK_${SDK}_LIB_SUBDIR}/${arch}/module.map")
29+
if("${SDK}" STREQUAL "FREEBSD")
30+
configure_file(module.freebsd.map.in "${modulemap_path}" @ONLY)
31+
else()
32+
configure_file(module.map.in "${modulemap_path}" @ONLY)
33+
endif()
2634

27-
add_custom_command_target(unused_var
28-
COMMAND
29-
"${CMAKE_COMMAND}" "-E" "make_directory" "${output_dir}"
30-
COMMAND
31-
"${CMAKE_COMMAND}" "-E" "copy_if_different"
32-
"${CMAKE_CURRENT_BINARY_DIR}/module.map"
33-
"${output_dir}/module.map"
34-
CUSTOM_TARGET_NAME "copy_glibc_module"
35-
OUTPUT "${output_dir}/module.map" "${output_dir}"
36-
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/module.map"
37-
COMMENT "Copying Glibc module to ${output_dir}")
35+
set(VARIANT_SUFFIX "-${SWIFT_SDK_${SDK}_LIB_SUBDIR}-${arch}")
36+
add_custom_command_target(unused_var
37+
COMMAND
38+
"${CMAKE_COMMAND}" "-E" "make_directory" "${output_dir}"
39+
COMMAND
40+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
41+
"${modulemap_path}"
42+
"${output_dir}/glibc.modulemap"
43+
CUSTOM_TARGET_NAME "copy_glibc_module${VARIANT_SUFFIX}"
44+
OUTPUT "${output_dir}/glibc.modulemap" "${output_dir}"
45+
DEPENDS "${modulemap_path}"
46+
COMMENT "Copying Glibc module to ${output_dir}")
3847

39-
swift_install_in_component(stdlib
40-
FILES "${output_dir}/module.map"
41-
DESTINATION "lib/swift/glibc")
48+
swift_install_in_component(stdlib
49+
FILES "${output_dir}/glibc.modulemap"
50+
DESTINATION "${output_dir}")
4251

43-
add_swift_library(swiftGlibc IS_SDK_OVERLAY
44-
${sources}
45-
FILE_DEPENDS copy_glibc_module "${output_dir}"
46-
INSTALL_IN_COMPONENT stdlib-experimental)
52+
add_swift_library(swiftGlibc IS_SDK_OVERLAY
53+
${sources}
54+
FILE_DEPENDS "copy_glibc_module${VARIANT_SUFFIX}" "${output_dir}"
55+
TARGET_SDKS "${SDK}"
56+
INSTALL_IN_COMPONENT stdlib-experimental)
57+
endif()
58+
endforeach()
59+
endforeach()

tools/driver/modulewrap_main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,20 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
153153
return 1;
154154
}
155155

156-
// Wrap the bitstream in an object file.
156+
// Wrap the bitstream in a module object file. To use the ClangImporter to
157+
// create the module loader, we need to properly set the runtime library path.
158+
SearchPathOptions SearchPathOpts;
159+
// FIXME: This logic has been duplicated from
160+
// CompilerInvocation::setMainExecutablePath. ModuleWrapInvocation
161+
// should share its implementation.
162+
SmallString<128> RuntimeResourcePath(MainExecutablePath);
163+
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /swift
164+
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /bin
165+
llvm::sys::path::append(RuntimeResourcePath, "lib", "swift");
166+
SearchPathOpts.RuntimeResourcePath = RuntimeResourcePath.str();
167+
157168
SourceManager SrcMgr;
158169
LangOptions LangOpts;
159-
SearchPathOptions SearchPathOpts;
160170
LangOpts.Target = Invocation.getTargetTriple();
161171
ASTContext ASTCtx(LangOpts, SearchPathOpts, SrcMgr, Instance.getDiags());
162172
ClangImporterOptions ClangImporterOpts;

0 commit comments

Comments
 (0)