Skip to content

Commit cb98c2b

Browse files
committed
[android] add an android NDK Swift overlay module, and use it instead of Glibc
1 parent fc69722 commit cb98c2b

File tree

6 files changed

+151
-16
lines changed

6 files changed

+151
-16
lines changed

lib/ClangImporter/ClangIncludePaths.cpp

+19-12
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static bool shouldInjectLibcModulemap(const llvm::Triple &triple) {
185185

186186
static SmallVector<std::pair<std::string, std::string>, 2>
187187
getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
188-
std::optional<StringRef> maybeHeaderFileName,
188+
std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
189189
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
190190
const llvm::Triple &triple = ctx.LangOpts.Target;
191191
if (!shouldInjectLibcModulemap(triple))
@@ -227,18 +227,20 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
227227
SmallVector<std::pair<std::string, std::string>, 2> vfsMappings{
228228
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)}};
229229

230-
if (maybeHeaderFileName) {
231-
// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
232-
// directly from the modulemap.
233-
Path actualHeaderPath = actualModuleMapPath;
234-
llvm::sys::path::remove_filename(actualHeaderPath);
235-
llvm::sys::path::append(actualHeaderPath, maybeHeaderFileName.value());
230+
if (maybeHeaderFileNames) {
231+
for (const auto &filename : *maybeHeaderFileNames) {
232+
// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
233+
// directly from the modulemap.
234+
Path actualHeaderPath = actualModuleMapPath;
235+
llvm::sys::path::remove_filename(actualHeaderPath);
236+
llvm::sys::path::append(actualHeaderPath, filename);
236237

237-
Path injectedHeaderPath(libcDir);
238-
llvm::sys::path::append(injectedHeaderPath, maybeHeaderFileName.value());
238+
Path injectedHeaderPath(libcDir);
239+
llvm::sys::path::append(injectedHeaderPath, filename);
239240

240-
vfsMappings.push_back(
241-
{std::string(injectedHeaderPath), std::string(actualHeaderPath)});
241+
vfsMappings.push_back(
242+
{std::string(injectedHeaderPath), std::string(actualHeaderPath)});
243+
}
242244
}
243245

244246
return vfsMappings;
@@ -534,8 +536,13 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
534536
// WASI Mappings
535537
libcFileMapping =
536538
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
539+
} else if (triple.isAndroid()) {
540+
// Android uses the android-specific module map that overlays the NDK.
541+
StringRef headerFiles[] = {"SwiftAndroidNDK.h", "SwiftBionic.h"};
542+
libcFileMapping =
543+
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs);
537544
} else {
538-
// Android/BSD/Linux Mappings
545+
// BSD/Linux Mappings
539546
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
540547
StringRef("SwiftGlibc.h"), vfs);
541548
}

stdlib/cmake/modules/AddSwiftStdlib.cmake

+6-1
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,7 @@ function(add_swift_target_library name)
18331833
SWIFT_COMPILE_FLAGS_WATCHOS
18341834
SWIFT_COMPILE_FLAGS_LINUX
18351835
SWIFT_MODULE_DEPENDS
1836+
SWIFT_MODULE_DEPENDS_ANDROID
18361837
SWIFT_MODULE_DEPENDS_CYGWIN
18371838
SWIFT_MODULE_DEPENDS_FREEBSD
18381839
SWIFT_MODULE_DEPENDS_FREESTANDING
@@ -2024,9 +2025,12 @@ function(add_swift_target_library name)
20242025
elseif(sdk STREQUAL "OPENBSD")
20252026
list(APPEND swiftlib_module_depends_flattened
20262027
${SWIFTLIB_SWIFT_MODULE_DEPENDS_OPENBSD})
2027-
elseif(sdk STREQUAL "LINUX" OR sdk STREQUAL "ANDROID")
2028+
elseif(sdk STREQUAL "LINUX")
20282029
list(APPEND swiftlib_module_depends_flattened
20292030
${SWIFTLIB_SWIFT_MODULE_DEPENDS_LINUX})
2031+
elseif(sdk STREQUAL "ANDROID")
2032+
list(APPEND swiftlib_module_depends_flattened
2033+
${SWIFTLIB_SWIFT_MODULE_DEPENDS_ANDROID})
20302034
elseif(sdk STREQUAL "CYGWIN")
20312035
list(APPEND swiftlib_module_depends_flattened
20322036
${SWIFTLIB_SWIFT_MODULE_DEPENDS_CYGWIN})
@@ -2819,6 +2823,7 @@ function(add_swift_target_executable name)
28192823
DEPENDS
28202824
LINK_LIBRARIES
28212825
SWIFT_MODULE_DEPENDS
2826+
SWIFT_MODULE_DEPENDS_ANDROID
28222827
SWIFT_MODULE_DEPENDS_CYGWIN
28232828
SWIFT_MODULE_DEPENDS_FREEBSD
28242829
SWIFT_MODULE_DEPENDS_FREESTANDING

stdlib/public/Concurrency/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
156156
${SWIFT_RUNTIME_CONCURRENCY_C_SOURCES}
157157
${SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES}
158158

159+
SWIFT_MODULE_DEPENDS_ANDROID Android
159160
SWIFT_MODULE_DEPENDS_LINUX Glibc
160161
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
161162
SWIFT_MODULE_DEPENDS_OPENBSD Glibc

stdlib/public/Platform/Android.swift

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import Android // Clang module
14+
15+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.")
16+
public let M_PI = Double.pi
17+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.")
18+
public let M_PI_2 = Double.pi / 2
19+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.")
20+
public let M_PI_4 = Double.pi / 4
21+
22+
@available(swift, deprecated: 3.0, message: "Please use 2.squareRoot()'.")
23+
public let M_SQRT2 = 2.squareRoot()
24+
25+
@available(swift, deprecated: 3.0, message: "Please use 0.5.squareRoot()'.")
26+
public let M_SQRT1_2 = 0.5.squareRoot()
27+
28+
@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
29+
public let FLT_RADIX = Double.radix
30+
31+
// Where does the 1 come from? C counts the usually-implicit leading
32+
// significand bit, but Swift does not. Neither is really right or wrong.
33+
@available(swift, deprecated: 3.0, message: "Please use 'Float.significandBitCount + 1'.")
34+
public let FLT_MANT_DIG = Float.significandBitCount + 1
35+
36+
// Where does the 1 come from? C models floating-point numbers as having a
37+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
38+
// significand to be in [1, 2). This rationale applies to FLT_MIN_EXP
39+
// as well.
40+
@available(swift, deprecated: 3.0, message: "Please use 'Float.greatestFiniteMagnitude.exponent + 1'.")
41+
public let FLT_MAX_EXP = Float.greatestFiniteMagnitude.exponent + 1
42+
43+
@available(swift, deprecated: 3.0, message: "Please use 'Float.leastNormalMagnitude.exponent + 1'.")
44+
public let FLT_MIN_EXP = Float.leastNormalMagnitude.exponent + 1
45+
46+
@available(swift, deprecated: 3.0, message: "Please use 'Float.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
47+
public let FLT_MAX = Float.greatestFiniteMagnitude
48+
49+
@available(swift, deprecated: 3.0, message: "Please use 'Float.ulpOfOne' or '.ulpOfOne'.")
50+
public let FLT_EPSILON = Float.ulpOfOne
51+
52+
@available(swift, deprecated: 3.0, message: "Please use 'Float.leastNormalMagnitude' or '.leastNormalMagnitude'.")
53+
public let FLT_MIN = Float.leastNormalMagnitude
54+
55+
@available(swift, deprecated: 3.0, message: "Please use 'Float.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
56+
public let FLT_TRUE_MIN = Float.leastNonzeroMagnitude
57+
58+
59+
// Where does the 1 come from? C counts the usually-implicit leading
60+
// significand bit, but Swift does not. Neither is really right or wrong.
61+
@available(swift, deprecated: 3.0, message: "Please use 'Double.significandBitCount + 1'.")
62+
public let DBL_MANT_DIG = Double.significandBitCount + 1
63+
64+
// Where does the 1 come from? C models floating-point numbers as having a
65+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
66+
// significand to be in [1, 2). This rationale applies to DBL_MIN_EXP
67+
// as well.
68+
@available(swift, deprecated: 3.0, message: "Please use 'Double.greatestFiniteMagnitude.exponent + 1'.")
69+
public let DBL_MAX_EXP = Double.greatestFiniteMagnitude.exponent + 1
70+
71+
@available(swift, deprecated: 3.0, message: "Please use 'Double.leastNormalMagnitude.exponent + 1'.")
72+
public let DBL_MIN_EXP = Double.leastNormalMagnitude.exponent + 1
73+
74+
@available(swift, deprecated: 3.0, message: "Please use 'Double.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
75+
public let DBL_MAX = Double.greatestFiniteMagnitude
76+
77+
@available(swift, deprecated: 3.0, message: "Please use 'Double.ulpOfOne' or '.ulpOfOne'.")
78+
public let DBL_EPSILON = Double.ulpOfOne
79+
80+
@available(swift, deprecated: 3.0, message: "Please use 'Double.leastNormalMagnitude' or '.leastNormalMagnitude'.")
81+
public let DBL_MIN = Double.leastNormalMagnitude
82+
83+
@available(swift, deprecated: 3.0, message: "Please use 'Double.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
84+
public let DBL_TRUE_MIN = Double.leastNonzeroMagnitude
85+
86+
public let M_LN2 = Android.M_LN2
87+
public let M_LOG10E = Android.M_LOG10E
88+
public let M_2_SQRTPI = Android.M_2_SQRTPI

stdlib/public/Platform/CMakeLists.txt

+36-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
126126
endforeach()
127127
endif()
128128

129-
set(swiftGlibc_target_sdks ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU)
129+
set(swiftGlibc_target_sdks CYGWIN FREEBSD OPENBSD LINUX HAIKU)
130130
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "linux")
131-
set(swiftGlibc_target_sdks ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU FREESTANDING)
131+
set(swiftGlibc_target_sdks CYGWIN FREEBSD OPENBSD LINUX HAIKU FREESTANDING)
132132
endif()
133133
add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
134134
${swift_platform_sources}
@@ -197,6 +197,22 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
197197
TARGET_SDKS WINDOWS
198198
INSTALL_IN_COMPONENT sdk-overlay)
199199

200+
add_swift_target_library(swiftAndroid ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
201+
Android.swift
202+
${swift_platform_sources}
203+
POSIXError.swift
204+
205+
GYB_SOURCES
206+
${swift_platform_gyb_sources}
207+
208+
SWIFT_COMPILE_FLAGS
209+
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
210+
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
211+
${swift_platform_compile_flags}
212+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
213+
TARGET_SDKS "ANDROID"
214+
INSTALL_IN_COMPONENT sdk-overlay)
215+
200216
set(glibc_modulemap_target_list)
201217
foreach(sdk ${SWIFT_SDKS})
202218
if(NOT "${sdk}" STREQUAL "LINUX" AND
@@ -213,6 +229,24 @@ foreach(sdk ${SWIFT_SDKS})
213229
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
214230
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
215231

232+
if ("${sdk}" STREQUAL "ANDROID")
233+
swift_install_in_component(FILES
234+
"android.modulemap"
235+
DESTINATION "lib/swift/${arch_subdir}"
236+
COMPONENT sdk-overlay)
237+
swift_install_in_component(FILES
238+
"SwiftAndroidNDK.h"
239+
DESTINATION "lib/swift/${arch_subdir}"
240+
COMPONENT sdk-overlay)
241+
swift_install_in_component(FILES
242+
"SwiftBionic.h"
243+
DESTINATION "lib/swift/${arch_subdir}"
244+
COMPONENT sdk-overlay)
245+
246+
# Do not build Glibc for Android.
247+
continue()
248+
endif()
249+
216250
set(glibc_modulemap_source "glibc.modulemap.gyb")
217251
set(glibc_modulemap_out "${module_dir}/glibc.modulemap")
218252
set(glibc_modulemap_out_static "${module_dir_static}/glibc.modulemap")

stdlib/public/Platform/Platform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public var S_IFIFO: Int32 { return Int32(0x1000) }
241241
public var S_IREAD: Int32 { return Int32(0x0100) }
242242
public var S_IWRITE: Int32 { return Int32(0x0080) }
243243
public var S_IEXEC: Int32 { return Int32(0x0040) }
244-
#else
244+
#elseif !os(Android)
245245
public var S_IFMT: mode_t { return mode_t(0o170000) }
246246
public var S_IFIFO: mode_t { return mode_t(0o010000) }
247247
public var S_IFCHR: mode_t { return mode_t(0o020000) }

0 commit comments

Comments
 (0)