1#   honggfuzz - Makefile
2#   -----------------------------------------
3#
4#   Author: Robert Swiecki <swiecki@google.com>
5#
6#   Copyright 2010-2015 by Google Inc. All Rights Reserved.
7#
8#   Licensed under the Apache License, Version 2.0 (the "License");
9#   you may not use this file except in compliance with the License.
10#   You may obtain a copy of the License at
11#
12#     http://www.apache.org/licenses/LICENSE-2.0
13#
14#   Unless required by applicable law or agreed to in writing, software
15#   distributed under the License is distributed on an "AS IS" BASIS,
16#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17#   See the License for the specific language governing permissions and
18#   limitations under the License.
19
20
21# Common for all architectures
22CC ?= cc
23LD = $(CC)
24BIN := honggfuzz
25COMMON_CFLAGS := -D_GNU_SOURCE -Wall -Wframe-larger-than=131072
26COMMON_LDFLAGS := -lm
27COMMON_SRCS := honggfuzz.c cmdline.c display.c files.c fuzz.c log.c mangle.c report.c sancov.c util.c
28
29OS ?= $(shell uname -s)
30MARCH ?= $(shell uname -m)
31
32ifeq ($(OS),Linux)
33    ARCH := LINUX
34    ARCH_DSUFFIX := .so
35
36    ARCH_CFLAGS := -std=c11 -I. -I/usr/local/include -I/usr/include \
37                   -Wextra -Wno-initializer-overrides -Wno-override-init \
38                   -Wno-unknown-warning-option -funroll-loops -O3 \
39                   -D_FILE_OFFSET_BITS=64
40    ARCH_LDFLAGS := -L/usr/local/include -L/usr/include \
41                    -lpthread -lunwind-ptrace -lunwind-generic -lbfd -lopcodes -lrt
42    ARCH_SRCS := $(wildcard linux/*.c)
43    INTERCEPTOR_SRCS := $(wildcard interceptor/*.c)
44
45    ifeq ("$(wildcard /usr/include/bfd.h)","")
46        WARN_LIBRARY += binutils-devel
47    endif
48    ifeq ("$(wildcard /usr/include/libunwind-ptrace.h)","")
49        WARN_LIBRARY += libunwind-devel/libunwind8-devel
50    endif
51    ifeq ("$(wildcard /usr/local/include/intel-pt.h)","/usr/local/include/intel-pt.h")
52        ARCH_CFLAGS += -D_HF_LINUX_INTEL_PT_LIB
53        ARCH_CFLAGS += -I/usr/local/include
54        ARCH_LDFLAGS += -L/usr/local/lib -lipt -Wl,--rpath=/usr/local/lib
55    endif
56    ifeq ("$(wildcard /usr/include/intel-pt.h)","/usr/include/intel-pt.h")
57        ARCH_CFLAGS += -D_HF_LINUX_INTEL_PT_LIB
58        ARCH_LDFLAGS += -lipt
59    endif
60    ifdef WARN_LIBRARY
61        $(info ***************************************************************)
62        $(info Development libraries which are most likely missing on your OS:)
63        $(info $(WARN_LIBRARY))
64        $(info ***************************************************************)
65    endif
66
67    ifeq ($(MARCH),$(filter $(MARCH),x86_64 i386))
68        # Support for popcnt (used in linux/perf.c)
69        ARCH_CFLAGS += -msse4.2
70    endif       # MARCH
71    # OS Linux
72else ifeq ($(OS),Darwin)
73    ARCH := DARWIN
74    ARCH_DSUFFIX := .dylib
75    CRASHWRANGLER := third_party/mac
76    OS_VERSION := $(shell sw_vers -productVersion)
77    ifneq (,$(findstring 10.11,$(OS_VERSION)))
78        # El Capitan didn't break compatibility
79        SDK_NAME := "macosx10.11"
80        CRASH_REPORT := $(CRASHWRANGLER)/CrashReport_Yosemite.o
81    else ifneq (,$(findstring 10.10,$(OS_VERSION)))
82        SDK_NAME := "macosx10.10"
83        CRASH_REPORT := $(CRASHWRANGLER)/CrashReport_Yosemite.o
84    else ifneq (,$(findstring 10.9,$(OS_VERSION)))
85        SDK_NAME := "macosx10.9"
86        CRASH_REPORT := $(CRASHWRANGLER)/CrashReport_Mavericks.o
87    else ifneq (,$(findstring 10.8,$(OS_VERSION)))
88        SDK_NAME := "macosx10.8"
89        CRASH_REPORT := $(CRASHWRANGLER)/CrashReport_Mountain_Lion.o
90    else
91        $(error Unsupported MAC OS X version)
92    endif
93    SDK := $(shell xcrun --sdk $(SDK_NAME) --show-sdk-path 2>/dev/null)
94    ifeq (,$(findstring MacOSX.platform,$(SDK)))
95        XC_PATH := $(shell xcode-select -p)
96        $(error $(SDK_NAME) not found in $(XC_PATH))
97    endif
98    CC := $(shell xcrun --sdk $(SDK_NAME) --find cc)
99    LD := $(shell xcrun --sdk $(SDK_NAME) --find cc)
100    ARCH_CFLAGS := -arch x86_64 -O3 -std=c99 -isysroot $(SDK) -I. \
101                   -x objective-c -pedantic -fblocks \
102                   -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized \
103                   -Wreturn-type -Wpointer-arith -Wno-gnu-case-range -Wno-gnu-designator \
104                   -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-attributes
105    ARCH_LDFLAGS := -F/System/Library/PrivateFrameworks -framework CoreSymbolication -framework IOKit \
106                    -F$(SDK)/System/Library/Frameworks -F$(SDK)/System/Library/PrivateFrameworks \
107                    -framework Foundation -framework ApplicationServices -framework Symbolication \
108                    -framework CoreServices -framework CrashReporterSupport -framework CoreFoundation \
109                    -framework CommerceKit $(CRASH_REPORT)
110    MIG_RET := $(shell mig -header mac/mach_exc.h -user mac/mach_excUser.c -sheader mac/mach_excServer.h \
111                 -server mac/mach_excServer.c $(SDK)/usr/include/mach/mach_exc.defs &>/dev/null; echo $$?)
112    ifeq ($(MIG_RET),1)
113        $(error mig failed to generate RPC code)
114    endif
115    ARCH_SRCS := $(wildcard mac/*.c)
116    # OS Darwin
117else
118    ARCH := POSIX
119    ARCH_DSUFFIX := .so
120    ARCH_SRCS := $(wildcard posix/*.c)
121    ARCH_CFLAGS := -std=gnu11 -I. -I/usr/local/include -I/usr/include \
122                   -Wextra -Wno-initializer-overrides -Wno-override-init \
123                   -Wno-unknown-warning-option -Wno-unknown-pragmas \
124				   -funroll-loops -O2
125    ARCH_LDFLAGS := -lpthread -L/usr/local/include -L/usr/include
126    # OS Posix
127endif
128
129COMPILER = $(shell $(CC) -v 2>&1 | grep -E '(cc|clang) version' | grep -oE '(clang|gcc)')
130ifeq ($(COMPILER),clang)
131    ARCH_CFLAGS += -fblocks
132    ARCH_LDFLAGS += -lBlocksRuntime
133endif
134
135SRCS := $(COMMON_SRCS) $(ARCH_SRCS)
136OBJS := $(SRCS:.c=.o)
137INTERCEPTOR_LIBS := $(INTERCEPTOR_SRCS:.c=$(ARCH_DSUFFIX))
138
139# Respect external user defines
140CFLAGS += $(COMMON_CFLAGS) $(ARCH_CFLAGS) -D_HF_ARCH_${ARCH}
141LDFLAGS += $(COMMON_LDFLAGS) $(ARCH_LDFLAGS)
142
143ifeq ($(DEBUG),true)
144    CFLAGS += -g -ggdb
145    LDFLAGS += -g -ggdb
146endif
147
148# Control Android builds
149ANDROID_DEBUG_ENABLED ?= false
150ANDROID_APP_ABI       ?= armeabi-v7a
151ANDROID_API           ?= android-21
152ANDROID_NDK_TOOLCHAIN ?=
153NDK_BUILD_ARGS :=
154ifeq ($(ANDROID_DEBUG_ENABLED),true)
155    NDK_BUILD_ARGS += V=1 NDK_DEBUG=1 APP_OPTIM=debug
156endif
157
158SUBDIR_ROOTS := linux mac posix interceptor
159DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
160CLEAN_PATTERNS := *.o *~ core *.a *.dSYM *.la *.so *.dylib
161SUBDIR_GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(CLEAN_PATTERNS)))
162MAC_GARGBAGE := $(wildcard mac/mach_exc*)
163ANDROID_GARBAGE := obj libs
164
165all: $(BIN) $(INTERCEPTOR_LIBS)
166
167%.o: %.c
168	$(CC) -c $(CFLAGS) -o $@ $<
169
170%.so: %.c
171	$(CC) -fPIC -shared $(CFLAGS) -o $@ $<
172
173%.dylib: %.c
174	$(CC) -fPIC -shared $(CFLAGS) -o $@ $<
175
176$(BIN): $(OBJS)
177	$(LD) -o $(BIN) $(OBJS) $(LDFLAGS)
178
179.PHONY: clean
180clean:
181	$(RM) -r core $(OBJS) $(BIN) $(MAC_GARGBAGE) $(ANDROID_GARBAGE) $(SUBDIR_GARBAGE)
182
183.PHONY: indent
184indent:
185	indent -linux -l100 -lc100 -nut -i4 *.c *.h */*.c */*.h; rm -f *~ */*~
186
187.PHONY: depend
188depend:
189	makedepend -Y. -Y* -- $(SRCS)
190
191.PHONY: android
192android:
193	ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./android/Android.mk \
194                  APP_PLATFORM=$(ANDROID_API) APP_ABI=$(ANDROID_APP_ABI) \
195                  NDK_TOOLCHAIN=$(ANDROID_NDK_TOOLCHAIN) $(NDK_BUILD_ARGS)
196
197
198# DO NOT DELETE
199
200honggfuzz.o: common.h cmdline.h display.h log.h files.h fuzz.h util.h
201cmdline.o: cmdline.h common.h log.h files.h util.h
202display.o: common.h display.h log.h util.h
203files.o: common.h files.h log.h
204fuzz.o: common.h fuzz.h arch.h files.h log.h mangle.h report.h sancov.h
205fuzz.o: util.h
206log.o: common.h log.h
207mangle.o: common.h mangle.h log.h util.h
208report.o: common.h report.h log.h util.h
209sancov.o: common.h sancov.h files.h log.h util.h
210util.o: common.h files.h log.h
211linux/ptrace_utils.o: common.h linux/ptrace_utils.h files.h linux/bfd.h
212linux/ptrace_utils.o: linux/unwind.h log.h sancov.h util.h
213linux/perf.o: common.h linux/perf.h files.h linux/pt.h log.h util.h
214linux/bfd.o: common.h linux/bfd.h files.h log.h util.h
215linux/pt.o: common.h linux/pt.h log.h
216linux/unwind.o: common.h linux/unwind.h log.h
217linux/arch.o: common.h arch.h linux/perf.h linux/ptrace_utils.h log.h
218linux/arch.o: sancov.h util.h files.h
219