Skip to content

Commit e522ee2

Browse files
committed
PoC: Support of older Android versions (aiming SDK 23 / this version works for SDK 24+ though)
1 parent f75bc35 commit e522ee2

File tree

10 files changed

+1322
-3
lines changed

10 files changed

+1322
-3
lines changed

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ if(CMAKE_SYSTEM_NAME MATCHES "Android")
100100
)
101101
endif()
102102

103+
# Builds & places for packing the glob library, for Android-targeted builds.
104+
if(CMAKE_SYSTEM_NAME MATCHES "Android")
105+
execute_process(
106+
COMMAND ${CMAKE_COMMAND} ${CMAKE_CURRENT_SOURCE_DIR}/glob
107+
-B ${CMAKE_BINARY_DIR}/glob
108+
-DCMAKE_BUILD_TYPE=Release
109+
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/sysroot
110+
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
111+
${EXTRA_BUILD_ARGS}
112+
COMMAND_ECHO STDOUT
113+
COMMAND_ERROR_IS_FATAL ANY
114+
)
115+
execute_process(
116+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/glob
117+
--config Release ${EXTRA_INSTALL_ARGS}
118+
)
119+
execute_process(COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR}/glob)
120+
file(
121+
COPY ${CMAKE_BINARY_DIR}/sysroot/lib/libglob.so
122+
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/android/src/main/jniLibs/${ANDROID_ABI}
123+
)
124+
endif()
125+
103126
if(CMAKE_SYSTEM_NAME MATCHES "Android|Windows")
104127
set(BUILD_SHARED_LIBS 1)
105128
endif()

example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
33
buildToolsVersion = "34.0.0"
4-
minSdkVersion = 28
4+
minSdkVersion = 23
55
compileSdkVersion = 34
66
targetSdkVersion = 34
77
ndkVersion = "26.1.10909125"

glob/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2+
project(GLOB C)
3+
4+
set(CMAKE_C_STANDARD 99)
5+
set(CMAKE_C_STANDARD_REQUIRED TRUE)
6+
7+
set(HEADERS collate.h freebsd-compat.h glob.h)
8+
set(SOURCES glob.c)
9+
10+
add_compile_definitions(__USE_BSD)
11+
add_library(glob ${HEADERS} ${SOURCES})
12+
13+
install(TARGETS glob)
14+
install(FILES ${HEADERS} DESTINATION include)

glob/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The Glob library is a part of Android SDK starting from SDK 28; thus, to support
2+
older SDKs we compile it from sources. These source files are taken from
3+
the https://android.googlesource.com/platform/bionic repository
4+
([bionic](https://en.wikipedia.org/wiki/Bionic_%28software%29)
5+
is Android's C library, math library, and dynamic linker).

glob/collate.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "freebsd-compat.h"
2+
3+
/**
4+
* [reallocarray(3)](https://man7.org/linux/man-pages/man3/realloc.3.html) resizes
5+
* allocated memory on the heap.
6+
*
7+
* Equivalent to `realloc(__ptr, __item_count * __item_size)` but fails if the
8+
* multiplication overflows.
9+
*
10+
* Returns a pointer (which may be different from `__ptr`) to the resized
11+
* memory on success and returns a null pointer and sets `errno` on failure
12+
* (but see the notes for malloc()).
13+
*/
14+
#if __ANDROID_API__ >= 29
15+
void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __INTRODUCED_IN(29);
16+
#else
17+
#include <errno.h>
18+
static __inline void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) {
19+
size_t __new_size;
20+
if (__builtin_mul_overflow(__item_count, __item_size, &__new_size)) {
21+
errno = ENOMEM;
22+
return NULL;
23+
}
24+
return realloc(__ptr, __new_size);
25+
}
26+
#endif

glob/freebsd-compat.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2013 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+
#pragma once
18+
19+
#define _BSD_SOURCE
20+
21+
#define REPLACE_GETOPT
22+
23+
/* FreeBSD has this, but we can't really implement it correctly on Linux. */
24+
#define issetugid() 0
25+
26+
#define __compiler_membar() __asm __volatile(" " : : : "memory")

0 commit comments

Comments
 (0)