1# Capstone Disassembler Engine 2# By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 3 4include ../config.mk 5include ../functions.mk 6 7# Verbose output? 8V ?= 0 9 10INCDIR = ../include 11ifndef BUILDDIR 12TESTDIR = . 13OBJDIR = . 14LIBDIR = .. 15else 16TESTDIR = $(BUILDDIR)/tests 17OBJDIR = $(BUILDDIR)/obj/tests 18LIBDIR = $(BUILDDIR) 19endif 20 21ifeq ($(CROSS),) 22CC ?= cc 23else 24CC = $(CROSS)gcc 25endif 26 27 28CFLAGS += -Wall -I$(INCDIR) 29LDFLAGS += -L$(LIBDIR) 30 31CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch)) 32LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch)) 33 34LIBNAME = capstone 35 36BIN_EXT = 37AR_EXT = a 38 39# Cygwin? 40IS_CYGWIN := $(shell $(CC) -dumpmachine | grep -i cygwin | wc -l) 41ifeq ($(IS_CYGWIN),1) 42CFLAGS := $(CFLAGS:-fPIC=) 43BIN_EXT = .exe 44AR_EXT = lib 45else 46# mingw? 47IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l) 48ifeq ($(IS_MINGW),1) 49CFLAGS := $(CFLAGS:-fPIC=) 50BIN_EXT = .exe 51AR_EXT = lib 52endif 53endif 54 55ifeq ($(CAPSTONE_STATIC),yes) 56ifeq ($(IS_MINGW),1) 57ARCHIVE = $(LIBDIR)/$(LIBNAME).$(AR_EXT) 58else ifeq ($(IS_CYGWIN),1) 59ARCHIVE = $(LIBDIR)/$(LIBNAME).$(AR_EXT) 60else 61ARCHIVE = $(LIBDIR)/lib$(LIBNAME).$(AR_EXT) 62endif 63endif 64 65.PHONY: all clean 66 67SOURCES = test_basic.c test_detail.c test_skipdata.c test_iter.c test_customized_mnem.c 68ifneq (,$(findstring arm,$(CAPSTONE_ARCHS))) 69CFLAGS += -DCAPSTONE_HAS_ARM 70SOURCES += test_arm.c 71endif 72ifneq (,$(findstring aarch64,$(CAPSTONE_ARCHS))) 73CFLAGS += -DCAPSTONE_HAS_ARM64 74SOURCES += test_arm64.c 75endif 76ifneq (,$(findstring m68k,$(CAPSTONE_ARCHS))) 77CFLAGS += -DCAPSTONE_HAS_M68K 78SOURCES += test_m68k.c 79endif 80ifneq (,$(findstring mips,$(CAPSTONE_ARCHS))) 81CFLAGS += -DCAPSTONE_HAS_MIPS 82SOURCES += test_mips.c 83endif 84ifneq (,$(findstring powerpc,$(CAPSTONE_ARCHS))) 85CFLAGS += -DCAPSTONE_HAS_POWERPC 86SOURCES += test_ppc.c 87endif 88ifneq (,$(findstring sparc,$(CAPSTONE_ARCHS))) 89CFLAGS += -DCAPSTONE_HAS_SPARC 90SOURCES += test_sparc.c 91endif 92ifneq (,$(findstring systemz,$(CAPSTONE_ARCHS))) 93CFLAGS += -DCAPSTONE_HAS_SYSZ 94SOURCES += test_systemz.c 95endif 96ifneq (,$(findstring x86,$(CAPSTONE_ARCHS))) 97CFLAGS += -DCAPSTONE_HAS_X86 98SOURCES += test_x86.c 99endif 100ifneq (,$(findstring xcore,$(CAPSTONE_ARCHS))) 101CFLAGS += -DCAPSTONE_HAS_XCORE 102SOURCES += test_xcore.c 103endif 104ifneq (,$(findstring tms320c64x,$(CAPSTONE_ARCHS))) 105CFLAGS += -DCAPSTONE_HAS_TMS320C64X 106SOURCES += test_tms320c64x.c 107endif 108ifneq (,$(findstring m680x,$(CAPSTONE_ARCHS))) 109CFLAGS += -DCAPSTONE_HAS_M680X 110SOURCES += test_m680x.c 111endif 112ifneq (,$(findstring evm,$(CAPSTONE_ARCHS))) 113CFLAGS += -DCAPSTONE_HAS_EVM 114SOURCES += test_evm.c 115endif 116 117OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o)) 118BINARY = $(addprefix $(TESTDIR)/,$(SOURCES:.c=$(BIN_EXT))) 119 120all: $(BINARY) 121 122clean: 123 rm -rf $(OBJS) $(BINARY) $(TESTDIR)/*.exe $(TESTDIR)/*.static $(OBJDIR)/lib$(LIBNAME).* $(OBJDIR)/$(LIBNAME).* 124 # remove orphan files due to renaming from test.c to test_basic.c 125 rm -rf $(TESTDIR)/test.o $(TESTDIR)/test.exe $(TESTDIR)/test.static $(TESTDIR)/test 126 127$(BINARY): $(OBJS) 128 129$(TESTDIR)/%$(BIN_EXT): $(OBJDIR)/%.o 130 @mkdir -p $(@D) 131ifeq ($(V),0) 132ifeq ($(CAPSTONE_SHARED),yes) 133 $(call log,LINK,$(notdir $@)) 134 @$(link-dynamic) 135endif 136ifeq ($(CAPSTONE_STATIC),yes) 137 $(call log,LINK,$(notdir $(call staticname,$@))) 138 @$(link-static) 139endif 140else 141ifeq ($(CAPSTONE_SHARED),yes) 142 $(link-dynamic) 143endif 144ifeq ($(CAPSTONE_STATIC),yes) 145 $(link-static) 146endif 147endif 148 149$(OBJDIR)/%.o: %.c 150 @mkdir -p $(@D) 151ifeq ($(V),0) 152 $(call log,CC,$(@:$(OBJDIR)/%=%)) 153 @$(compile) 154else 155 $(compile) 156endif 157 158 159define link-dynamic 160 $(CC) $(LDFLAGS) $< -l$(LIBNAME) -o $@ 161endef 162 163 164define link-static 165 $(CC) $(LDFLAGS) $< $(ARCHIVE) -o $(call staticname,$@) 166endef 167 168 169staticname = $(subst $(BIN_EXT),,$(1)).static$(BIN_EXT) 170