Skip to content

Commit 2ba77bd

Browse files
authored
Merge pull request #540 from AbhiTheModder/android
Add Android compatibility
2 parents 37c9430 + e36ec57 commit 2ba77bd

5 files changed

Lines changed: 268 additions & 55 deletions

File tree

src/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ PREFIX ?= /usr/local
126126
LIBDIRNAME ?= /lib/faketime
127127
PLATFORM ?=$(shell uname)
128128

129+
ifneq ($(filter android%,$(subst -, ,$(CC))),)
130+
IS_ANDROID := true
131+
endif
132+
133+
ifeq ($(shell $(CC) -dM -E - < /dev/null | grep -c "__ANDROID__"),1)
134+
IS_ANDROID := true
135+
endif
136+
129137
ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
130138
COMPILER := clang
131139
else
@@ -147,15 +155,26 @@ ifeq ($(PLATFORM),SunOS)
147155
CFLAGS += -D__EXTENSIONS__ -D_XOPEN_SOURCE=600
148156
endif
149157

158+
ifdef IS_ANDROID
159+
CFLAGS += -D__ANDROID__
160+
endif
161+
150162
LIB_LDFLAGS += -shared
151163

152164
LDFLAGS += $(FAKETIME_LINK_FLAGS)
153165
ifneq ($(PLATFORM),SunOS)
166+
ifndef IS_ANDROID
154167
LDFLAGS += -Wl,--version-script=libfaketime.map
155168
endif
169+
endif
156170

171+
ifdef IS_ANDROID
172+
LDADD += -ldl -lm
173+
BIN_LDFLAGS +=
174+
else
157175
LDADD += -ldl -lm -lrt -lpthread
158176
BIN_LDFLAGS += -lrt -lpthread
177+
endif
159178

160179
SRC = libfaketime.c
161180
LIBS_OBJ = libfaketime.o libfaketimeMT.o

src/android_compat.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef ANDROID_COMPAT_H
2+
#define ANDROID_COMPAT_H
3+
4+
#ifdef __ANDROID__
5+
6+
#include <fcntl.h>
7+
#include <sys/stat.h>
8+
#include <unistd.h>
9+
#include <string.h>
10+
#include <errno.h>
11+
#include <stdio.h>
12+
13+
static inline const char *__android_shm_tmpdir(void)
14+
{
15+
const char *dir = getenv("FAKETIME_SHM_DIR");
16+
if (dir == NULL)
17+
dir = getenv("TMPDIR");
18+
if (dir == NULL)
19+
dir = "/data/local/tmp";
20+
return dir;
21+
}
22+
23+
static inline int __android_shm_open(const char *name, int oflag, mode_t mode)
24+
{
25+
char path[512];
26+
const char *dir = __android_shm_tmpdir();
27+
while (*name == '/') name++;
28+
snprintf(path, sizeof(path), "%s/faketime_shm_%s", dir, name);
29+
int fd = open(path, oflag, mode);
30+
if (fd == -1 && (oflag & O_CREAT))
31+
{
32+
mkdir(dir, 0777);
33+
fd = open(path, oflag, mode);
34+
}
35+
return fd;
36+
}
37+
38+
static inline int __android_shm_unlink(const char *name)
39+
{
40+
char path[512];
41+
const char *dir = __android_shm_tmpdir();
42+
while (*name == '/') name++;
43+
snprintf(path, sizeof(path), "%s/faketime_shm_%s", dir, name);
44+
return unlink(path);
45+
}
46+
47+
#define shm_open __android_shm_open
48+
#define shm_unlink __android_shm_unlink
49+
50+
#endif /* __ANDROID__ */
51+
#endif /* ANDROID_COMPAT_H */

src/faketime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <sys/mman.h>
4747
#include "ft_sem.h"
4848

49+
#include "android_compat.h"
4950
#include "faketime_common.h"
5051

5152
const char version[] = "0.9.12";

src/ft_sem.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ static int ft_sem_name_to_path(const char *name, char *path, size_t pathlen)
7373
return -1;
7474
}
7575
const char *pid_str = p + strlen(prefix);
76-
snprintf(path, pathlen, "/dev/shm/faketime_lock_%s", pid_str);
76+
const char *tmpdir;
77+
#ifdef __ANDROID__
78+
tmpdir = getenv("TMPDIR");
79+
if (tmpdir == NULL) tmpdir = "/data/local/tmp";
80+
#else
81+
tmpdir = "/dev/shm";
82+
#endif
83+
snprintf(path, pathlen, "%s/faketime_lock_%s", tmpdir, pid_str);
7784
path[pathlen - 1] = '\0';
7885
return 0;
7986
}

0 commit comments

Comments
 (0)