summaryrefslogtreecommitdiff
path: root/src/Makefile
blob: df72d47472d2f1e5e79969be5027f75627c8a99b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# Notes:
#
#   * Compilation Defines that are set by default:
#
#     FAKE_STAT
#         - Enables time faking when reading files' timestamps.
#
#     FAKE_SLEEP
#         - Also intercept sleep(), nanosleep(), usleep(), alarm(), [p]poll()
#
#     FAKE_TIMERS
#         - Also intercept timer_settime() and timer_gettime()
#
#     FAKE_PTHREAD
#         - Intercept pthread_cond_timedwait
#
#     FAKE_INTERNAL_CALLS
#         - Also intercept libc internal __functions, e.g. not just time(),
#           but also __time(). Enhances compatibility with applications
#           that make use of low-level system calls, such as Java Virtual
#           Machines.
#
#     PTHREAD_SINGLETHREADED_TIME (only set in libfaketimeMT.so)
#         - Define this if you want to single-thread time() ... there ARE
#           possible caching side-effects in a multithreaded environment
#           without this, but the performance impact may require you to
#           try it unsynchronized.
#
#     FAIL_PRE_INIT_CALLS
#         - If the time is queried before the library was initialised, let the
#           call fail instead of trying to initialise on-the-fly. This fixes /
#           works around hangs that were seen with address sanitizer.
#
#   * Compilation Defines that are unset by default:
#
#     FAKE_FILE_TIMESTAMPS, FAKE_UTIME
#         - Enables time faking for the utime* functions.  If enabled via
#           FAKE_FILE_TIMESTAMPS, the faking is opt-in at runtime using
#           with the FAKE_UTIME environment variable. If enabled via
#           FAKE_UTIME, the faking is opt-out at runtime.
#
#     NO_ATFILE
#         - Disables support for the fstatat() group of functions
#
#     FAKE_SETTIME
#         - Intercept clock_settime(), settimeofday(), and adjtime()
#
#     FAKE_RANDOM
#         - Intercept getrandom()
#
#     FAKE_PID
#         - Intercept getpid()
#
#     INTERCEPT_SYSCALL
#         - (On GNU/Linux only) intercept glibc's syscall() for known relevant syscalls.
#           If enabled, this currently only works to divert the getrandom syscall.
#
#         - note that on unusual architectures, if INTERCEPT_SYSCALL is set, you may
#           need to explicitly define variadic_promotion_t (e.g. by putting
#           -Dvariadic_promotion_t=int into CFLAGS).  See src/faketime_common.h for
#           more info.
#
#     FAKE_STATELESS
#         - Remove support for any functionality that requires sharing state across
#           threads of a process, or different processes. This decreases the risk of
#           interference with a program's normal execution, at the cost of supporting
#           fewer ways of specifying the time.
#           Concretely, this currently:
#           - disables PTHREAD_SINGLETHREADED_TIME, which can cause deadlocks in
#             multithreaded programs that fork due to making clock_gettime not
#             async-signal-safe
#           - disables all shared-memory across processes
#
#     FORCE_MONOTONIC_FIX
#         - If the test program hangs forever on
#                  " pthread_cond_timedwait: CLOCK_MONOTONIC test
#                    (Intentionally sleeping 1 second...)          "
#           then add -DFORCE_MONOTONIC_FIX to CFLAGS and recompile.
#           (This is a platform-specific issue we cannot handle at run-time.)
#
#     MULTI_ARCH
#         - If MULTI_ARCH is set, the faketime wrapper program will put a literal
#           $LIB into the LD_PRELOAD environment variable it creates, which makes
#           ld automatically choose the correct library version to use for the
#           target binary. Use for Linux platforms with Multi-Arch support only!
#
#     SILENT
#         - avoid that the faketime wrapper complains when running within a
#           libfaketime environment
#
#   * Compilation addition: second libMT target added for building the pthread-
#     enabled library as a separate library
#
#   * Compilation switch change: previous versions compiled using '-nostartfiles'
#     This is no longer the case since there is a 'startup' constructor for the library
#     which is used to activate the start-at times when specified. This also initializes
#     the dynamic disabling of the FAKE_STAT calls.
#
# By default, libfaketime will be compiled for your system's default architecture.
# To build 32-bit libraries and binaries, add -m32 to CFLAGS and LDFLAGS.
#
# Change PREFIX to where you want libfaketime (libraries and wrapper binary) installed.
# LIBDIRNAME is relative to PREFIX. The default is to install into $PREFIX/lib/faketime,
# but you can set LIBDIRNAME to, e.g., /lib64 if you want to install it elsewhere.
# LIBDIRNAME has been introduced to support MultiLib systems. Please do not change the
# default value on MultiArch systems.
#
# For testing in the current directory without installation, try make PREFIX= LIBDIRNAME='.'

CC ?= gcc
INSTALL ?= install

PREFIX ?= /usr/local
LIBDIRNAME ?= /lib/faketime
PLATFORM ?=$(shell uname)

CFLAGS += -std=gnu99 -Wall -Wextra -Werror -Wno-nonnull-compare -DFAKE_PTHREAD -DFAKE_STAT -DFAKE_UTIME -DFAKE_SLEEP -DFAKE_TIMERS -DFAKE_INTERNAL_CALLS -DFAIL_PRE_INIT_CALLS -fPIC -DPREFIX='"'$(PREFIX)'"' -DLIBDIRNAME='"'$(LIBDIRNAME)'"' $(FAKETIME_COMPILE_CFLAGS)
ifeq ($(PLATFORM),SunOS)
CFLAGS += -D__EXTENSIONS__ -D_XOPEN_SOURCE=600
endif

LIB_LDFLAGS += -shared

LDFLAGS += $(FAKETIME_LINK_FLAGS) -lpthread
ifneq ($(PLATFORM),SunOS)
LDFLAGS += -Wl,--version-script=libfaketime.map
endif

LDADD += -ldl -lm -lrt
BIN_LDFLAGS += -lrt

SRC = libfaketime.c
LIBS_OBJ = libfaketime.o libfaketimeMT.o
BINS = faketime

SONAME = 1
LIBS = libfaketime.so.${SONAME} libfaketimeMT.so.${SONAME}

all: ${LIBS} ${BINS}

libfaketimeMT.o: EXTRA_FLAGS := -DPTHREAD_SINGLETHREADED_TIME

${LIBS_OBJ}: libfaketime.c
	${CC} -o $@ -c ${CFLAGS} ${CPPFLAGS} ${EXTRA_FLAGS} $<

%.so.${SONAME}: %.o libfaketime.map
	${CC} -o $@ -Wl,-soname,$@ ${LDFLAGS} ${LIB_LDFLAGS} $< ${LDADD}

${BINS}: faketime.c
	${CC} -o $@ ${CFLAGS} ${CPPFLAGS} ${EXTRA_FLAGS} $< ${LDFLAGS} ${BIN_LDFLAGS}

clean:
	@rm -f ${LIBS_OBJ} ${LIBS} ${BINS}

distclean: clean
	@echo

install: ${LIBS} ${BINS}
	@echo
	@echo "Copying the faketime libraries to ${DESTDIR}${PREFIX}${LIBDIRNAME} and the faketime wrapper script to ${DESTDIR}${PREFIX}/bin ..."
	$(INSTALL) -dm0755 "${DESTDIR}${PREFIX}${LIBDIRNAME}/"
	$(INSTALL) -m0644 ${LIBS} "${DESTDIR}${PREFIX}${LIBDIRNAME}/"
	$(INSTALL) -Dm0755 faketime "${DESTDIR}${PREFIX}/bin/faketime"

uninstall:
	for f in ${LIBS}; do rm -f "${DESTDIR}${PREFIX}${LIBDIRNAME}/$$f"; done
	rmdir "${DESTDIR}${PREFIX}${LIBDIRNAME}"
	rm -f "${DESTDIR}${PREFIX}/bin/faketime"

.PHONY: all clean distclean install uninstall