Skip to content

Commit df6f255

Browse files
committed
android: stub out some missing methods on Android, disable split dwarf by default
1 parent 3f9cb7b commit df6f255

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS})
8686

8787
endforeach()
8888
endif()
89+
# FIXME: Android: make a swift.ld for armv7 android
8990
endforeach()
9091

stdlib/public/runtime/Casting.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,10 @@ static void _addImageProtocolConformances(const mach_header *mh,
23982398
_addImageProtocolConformancesBlock(conformances, conformancesSize);
23992399
}
24002400
#elif defined(__ELF__)
2401+
#ifdef __ANDROID__
2402+
#define RTLD_NOLOAD 0
2403+
#endif
2404+
24012405
static int _addImageProtocolConformances(struct dl_phdr_info *info,
24022406
size_t size, void * /*data*/) {
24032407
void *handle;
@@ -2438,7 +2442,10 @@ static void installCallbacksToInspectDylib() {
24382442
// loaded ones.
24392443
// FIXME: Find a way to have this continue to happen after.
24402444
// rdar://problem/19045112
2445+
#ifndef __ANDROID__
2446+
// FIXME: Android: doesn't have dl_iterate_phdr before API 21
24412447
dl_iterate_phdr(_addImageProtocolConformances, nullptr);
2448+
#endif
24422449
#else
24432450
# error No known mechanism to inspect dynamic libraries on this platform.
24442451
#endif

stdlib/public/stubs/LibcShims.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
#include <string.h>
1818
#include "../SwiftShims/LibcShims.h"
1919

20-
#if defined(__linux__)
20+
#if defined(__linux__) && !defined(__ANDROID__)
2121
#include <bsd/stdlib.h>
2222
#endif
2323

24+
// android's ssize_t is an int, which is the same size as long int
25+
#ifndef __ANDROID__
2426
static_assert(std::is_same<ssize_t, swift::__swift_ssize_t>::value,
2527
"__swift_ssize_t is wrong");
28+
#endif
2629

2730
namespace swift {
2831

@@ -50,7 +53,7 @@ int _swift_stdlib_close(int fd) { return close(fd); }
5053
#if defined(__APPLE__)
5154
#include <malloc/malloc.h>
5255
size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); }
53-
#elif defined(__GNU_LIBRARY__)
56+
#elif defined(__GNU_LIBRARY__) || defined(__ANDROID__)
5457
#include <malloc.h>
5558
size_t _swift_stdlib_malloc_size(const void *ptr) {
5659
return malloc_usable_size(const_cast<void *>(ptr));
@@ -59,12 +62,21 @@ size_t _swift_stdlib_malloc_size(const void *ptr) {
5962
#error No malloc_size analog known for this platform/libc.
6063
#endif
6164

65+
#ifndef __ANDROID__
6266
__swift_uint32_t _swift_stdlib_arc4random(void) { return arc4random(); }
6367

6468
__swift_uint32_t
6569
_swift_stdlib_arc4random_uniform(__swift_uint32_t upper_bound) {
6670
return arc4random_uniform(upper_bound);
6771
}
72+
#else
73+
// FIXME: Android: chosen by fair dice roll
74+
__swift_uint32_t _swift_stdlib_arc4random(void) { return 4; }
75+
__swift_uint32_t
76+
_swift_stdlib_arc4random_uniform(__swift_uint32_t upper_bound) {
77+
return 0;
78+
}
79+
#endif
6880

6981
} // namespace swift
7082

stdlib/public/stubs/Stubs.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#include "swift/Runtime/Debug.h"
3131
#include "swift/Basic/Lazy.h"
3232

33+
#ifdef __ANDROID__
34+
#include "getline.inc"
35+
#define getline swift_getline
36+
#endif
37+
3338
static uint64_t uint64ToStringImpl(char *Buffer, uint64_t Value,
3439
int64_t Radix, bool Uppercase,
3540
bool Negative) {
@@ -93,7 +98,7 @@ extern "C" uint64_t swift_uint64ToString(char *Buffer, intptr_t BufferLength,
9398
/*Negative=*/false);
9499
}
95100

96-
#if defined(__APPLE__) || defined(__FreeBSD__)
101+
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__ANDROID__)
97102
static inline locale_t getCLocale() {
98103
// On these platforms convenience functions from xlocale.h interpret nullptr
99104
// as C locale.
@@ -113,6 +118,22 @@ static locale_t getCLocale() {
113118
}
114119
#endif
115120

121+
#ifdef __ANDROID__
122+
#define uselocale(a) (nullptr)
123+
static double swift_strtod_l(const char* a, char** b, locale_t c) {
124+
return strtod(a, b);
125+
}
126+
static float swift_strtof_l(const char* a, char** b, locale_t c) {
127+
return strtof(a, b);
128+
}
129+
static long double swift_strtold_l(const char* a, char** b, locale_t c) {
130+
return strtold(a, b);
131+
}
132+
#define strtod_l swift_strtod_l
133+
#define strtof_l swift_strtof_l
134+
#define strtold_l swift_strtold_l
135+
#endif
136+
116137
#if defined(__APPLE__)
117138
#define swift_snprintf_l snprintf_l
118139
#else

stdlib/public/stubs/getline.inc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <stdio.h>
18+
19+
static ssize_t swift_getline(char **lineptr, size_t *n, FILE *stream)
20+
{
21+
char *ptr;
22+
23+
ptr = fgetln(stream, n);
24+
25+
if (ptr == NULL) {
26+
return -1;
27+
}
28+
29+
/* Free the original ptr */
30+
if (*lineptr != NULL) free(*lineptr);
31+
32+
/* Add one more space for '\0' */
33+
size_t len = n[0] + 1;
34+
35+
/* Update the length */
36+
n[0] = len;
37+
38+
/* Allocate a new buffer */
39+
*lineptr = (char*) malloc(len);
40+
41+
/* Copy over the string */
42+
memcpy(*lineptr, ptr, len-1);
43+
44+
/* Write the NULL character */
45+
(*lineptr)[len-1] = '\0';
46+
47+
/* Return the length of the new buffer */
48+
return len;
49+
}

utils/build-script-impl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,8 @@ COMMON_CMAKE_OPTIONS=(
10321032
-G "${CMAKE_GENERATOR}"
10331033
)
10341034

1035-
COMMON_C_FLAGS="-gsplit-dwarf"
1036-
COMMON_CXX_FLAGS="-gsplit-dwarf"
1035+
COMMON_C_FLAGS=""
1036+
COMMON_CXX_FLAGS=""
10371037

10381038
if [[ "${ENABLE_ASAN}" ]] ; then
10391039
COMMON_CMAKE_OPTIONS=(
@@ -1475,8 +1475,8 @@ for deployment_target in "${NATIVE_TOOLS_DEPLOYMENT_TARGETS[@]}" "${CROSS_TOOLS_
14751475
# gold linker support.
14761476
cmake_options=(
14771477
"${cmake_options[@]}"
1478-
-DCMAKE_EXE_LINKER_FLAGS:STRING="-fuse-ld=gold -Wl,--gdb-index"
1479-
-DCMAKE_SHARED_LINKER_FLAGS:STRING="-fuse-ld=gold -Wl,--gdb-index"
1478+
-DCMAKE_EXE_LINKER_FLAGS:STRING="-fuse-ld=gold"
1479+
-DCMAKE_SHARED_LINKER_FLAGS:STRING="-fuse-ld=gold"
14801480
)
14811481
fi
14821482
else

0 commit comments

Comments
 (0)