blob: 837148cad32b9b0f10e924343e316e9df88de1de [file] [log] [blame]
Nguyen Anh Quynh296a2312014-03-05 23:50:41 +08001# Capstone Disassembly Engine
Nguyen Anh Quynh1ed8bb62014-03-10 23:14:30 -07002# By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014
Nguyen Anh Quynh5e107ab2013-12-31 23:38:28 +08003
danghvu6f52bf92013-12-19 22:43:01 -06004include config.mk
Nguyen Anh Quynh3ff259c2014-03-07 17:11:18 +08005include pkgconfig.mk # package version
Nguyen Anh Quynh58a00472014-05-01 22:53:52 +08006include functions.mk
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +02008# Verbose output?
9V ?= 0
10
Stephen5e184ec2018-05-16 16:55:44 -070011OS := $(shell uname)
12ifeq ($(OS),Darwin)
13LIBARCHS = i386 x86_64
14PREFIX ?= /usr/local
15endif
16
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +020017ifeq ($(PKG_EXTRA),)
18PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR)
19else
20PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA)
21endif
22
Nguyen Anh Quynhd80cede2014-01-16 21:07:59 +080023ifeq ($(CROSS),)
Nguyen Anh Quynhd80cede2014-01-16 21:07:59 +080024RANLIB ?= ranlib
Nguyen Anh Quynhd80cede2014-01-16 21:07:59 +080025else
26CC = $(CROSS)gcc
27AR = $(CROSS)ar
28RANLIB = $(CROSS)ranlib
29STRIP = $(CROSS)strip
30endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080031
Nguyen Anh Quynh77af9fb2014-04-29 07:11:55 +080032ifneq (,$(findstring yes,$(CAPSTONE_DIET)))
33CFLAGS ?= -Os
34CFLAGS += -DCAPSTONE_DIET
35else
36CFLAGS ?= -O3
37endif
38
baguette86e84502014-08-17 20:59:05 +020039ifneq (,$(findstring yes,$(CAPSTONE_X86_ATT_DISABLE)))
40CFLAGS += -DCAPSTONE_X86_ATT_DISABLE
41endif
42
Richard Henderson22ead3e2017-10-21 17:45:40 -070043CFLAGS += -fPIC -Wall -Wwrite-strings -Iinclude
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080044
Nguyen Anh Quynhe31327d2014-04-29 14:25:15 +080045ifeq ($(CAPSTONE_USE_SYS_DYN_MEM),yes)
Nguyen Anh Quynh87a3f692014-05-10 19:26:32 +080046CFLAGS += -DCAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080047endif
48
reverserbcf09f42015-04-09 18:28:19 +010049ifeq ($(CAPSTONE_HAS_OSXKERNEL), yes)
50CFLAGS += -DCAPSTONE_HAS_OSXKERNEL
Pbb8741bd2015-11-10 23:02:26 +010051SDKROOT ?= $(shell xcodebuild -version -sdk macosx Path)
52CFLAGS += -mmacosx-version-min=10.5 \
53 -isysroot$(SDKROOT) \
54 -I$(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers \
55 -mkernel \
56 -fno-builtin
reverserbcf09f42015-04-09 18:28:19 +010057endif
58
pancake02cad6e2013-11-27 15:38:44 +010059PREFIX ?= /usr
60DESTDIR ?=
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +020061ifndef BUILDDIR
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +080062BLDIR = .
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +020063OBJDIR = .
64else
Ole André Vadla Ravnås7a734392014-04-30 00:06:41 +020065BLDIR = $(abspath $(BUILDDIR))
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +080066OBJDIR = $(BLDIR)/obj
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +020067endif
pancake02cad6e2013-11-27 15:38:44 +010068INCDIR = $(DESTDIR)$(PREFIX)/include
Nguyen Anh Quynh9f0e6c22013-12-25 22:47:28 +080069
Nguyen Anh Quynh235a2742014-03-29 21:09:35 +080070UNAME_S := $(shell uname -s)
Nguyen Anh Quynhad6cc982014-04-08 23:34:44 +080071
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080072LIBDIRARCH ?= lib
73# Uncomment the below line to installs x86_64 libs to lib64/ directory.
74# Or better, pass 'LIBDIRARCH=lib64' to 'make install/uninstall' via 'make.sh'.
75#LIBDIRARCH ?= lib64
76LIBDIR = $(DESTDIR)$(PREFIX)/$(LIBDIRARCH)
Nguyen Anh Quynh34603272016-10-10 22:54:16 +080077BINDIR = $(DESTDIR)$(PREFIX)/bin
pancake02cad6e2013-11-27 15:38:44 +010078
Oliver Pinterc5ecae52014-03-23 02:19:59 +010079LIBDATADIR = $(LIBDIR)
Kamil Rytarowski453b41a2015-07-27 04:03:48 +020080
81# Don't redefine $LIBDATADIR when global environment variable
82# USE_GENERIC_LIBDATADIR is set. This is used by the pkgsrc framework.
83
84ifndef USE_GENERIC_LIBDATADIR
Oliver Pinterc5ecae52014-03-23 02:19:59 +010085ifeq ($(UNAME_S), FreeBSD)
86LIBDATADIR = $(DESTDIR)$(PREFIX)/libdata
Oliver Pinterc5ecae52014-03-23 02:19:59 +010087endif
Oliver Pinter25efded2014-03-23 19:10:13 +010088ifeq ($(UNAME_S), DragonFly)
89LIBDATADIR = $(DESTDIR)$(PREFIX)/libdata
90endif
Kamil Rytarowski453b41a2015-07-27 04:03:48 +020091endif
Oliver Pinterc5ecae52014-03-23 02:19:59 +010092
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080093INSTALL_BIN ?= install
94INSTALL_DATA ?= $(INSTALL_BIN) -m0644
Oliver Pinterc5ecae52014-03-23 02:19:59 +010095INSTALL_LIB ?= $(INSTALL_BIN) -m0755
pancake02cad6e2013-11-27 15:38:44 +010096
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080097LIBNAME = capstone
pancakeca9f1b22013-12-06 17:03:41 +010098
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080099
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800100DEP_ARM =
101DEP_ARM += arch/ARM/ARMGenAsmWriter.inc
102DEP_ARM += arch/ARM/ARMGenDisassemblerTables.inc
103DEP_ARM += arch/ARM/ARMGenInstrInfo.inc
104DEP_ARM += arch/ARM/ARMGenRegisterInfo.inc
105DEP_ARM += arch/ARM/ARMGenSubtargetInfo.inc
106
107LIBOBJ_ARM =
Nguyen Anh Quynh9cc56a32014-01-15 16:01:55 +0800108ifneq (,$(findstring arm,$(CAPSTONE_ARCHS)))
109 CFLAGS += -DCAPSTONE_HAS_ARM
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200110 LIBOBJ_ARM += $(OBJDIR)/arch/ARM/ARMDisassembler.o
111 LIBOBJ_ARM += $(OBJDIR)/arch/ARM/ARMInstPrinter.o
112 LIBOBJ_ARM += $(OBJDIR)/arch/ARM/ARMMapping.o
113 LIBOBJ_ARM += $(OBJDIR)/arch/ARM/ARMModule.o
danghvu02db4882013-12-20 00:57:12 -0600114endif
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800115
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800116DEP_ARM64 =
117DEP_ARM64 += arch/AArch64/AArch64GenAsmWriter.inc
118DEP_ARM64 += arch/AArch64/AArch64GenInstrInfo.inc
119DEP_ARM64 += arch/AArch64/AArch64GenSubtargetInfo.inc
120DEP_ARM64 += arch/AArch64/AArch64GenDisassemblerTables.inc
121DEP_ARM64 += arch/AArch64/AArch64GenRegisterInfo.inc
122
123LIBOBJ_ARM64 =
124ifneq (,$(findstring aarch64,$(CAPSTONE_ARCHS)))
125 CFLAGS += -DCAPSTONE_HAS_ARM64
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200126 LIBOBJ_ARM64 += $(OBJDIR)/arch/AArch64/AArch64BaseInfo.o
127 LIBOBJ_ARM64 += $(OBJDIR)/arch/AArch64/AArch64Disassembler.o
128 LIBOBJ_ARM64 += $(OBJDIR)/arch/AArch64/AArch64InstPrinter.o
129 LIBOBJ_ARM64 += $(OBJDIR)/arch/AArch64/AArch64Mapping.o
130 LIBOBJ_ARM64 += $(OBJDIR)/arch/AArch64/AArch64Module.o
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800131endif
132
133
134DEP_MIPS =
135DEP_MIPS += arch/Mips/MipsGenAsmWriter.inc
136DEP_MIPS += arch/Mips/MipsGenDisassemblerTables.inc
137DEP_MIPS += arch/Mips/MipsGenInstrInfo.inc
138DEP_MIPS += arch/Mips/MipsGenRegisterInfo.inc
139DEP_MIPS += arch/Mips/MipsGenSubtargetInfo.inc
140
141LIBOBJ_MIPS =
142ifneq (,$(findstring mips,$(CAPSTONE_ARCHS)))
143 CFLAGS += -DCAPSTONE_HAS_MIPS
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200144 LIBOBJ_MIPS += $(OBJDIR)/arch/Mips/MipsDisassembler.o
145 LIBOBJ_MIPS += $(OBJDIR)/arch/Mips/MipsInstPrinter.o
146 LIBOBJ_MIPS += $(OBJDIR)/arch/Mips/MipsMapping.o
147 LIBOBJ_MIPS += $(OBJDIR)/arch/Mips/MipsModule.o
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800148endif
149
150
151DEP_PPC =
152DEP_PPC += arch/PowerPC/PPCGenAsmWriter.inc
153DEP_PPC += arch/PowerPC/PPCGenInstrInfo.inc
154DEP_PPC += arch/PowerPC/PPCGenSubtargetInfo.inc
155DEP_PPC += arch/PowerPC/PPCGenDisassemblerTables.inc
156DEP_PPC += arch/PowerPC/PPCGenRegisterInfo.inc
157
158LIBOBJ_PPC =
159ifneq (,$(findstring powerpc,$(CAPSTONE_ARCHS)))
160 CFLAGS += -DCAPSTONE_HAS_POWERPC
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200161 LIBOBJ_PPC += $(OBJDIR)/arch/PowerPC/PPCDisassembler.o
162 LIBOBJ_PPC += $(OBJDIR)/arch/PowerPC/PPCInstPrinter.o
163 LIBOBJ_PPC += $(OBJDIR)/arch/PowerPC/PPCMapping.o
164 LIBOBJ_PPC += $(OBJDIR)/arch/PowerPC/PPCModule.o
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800165endif
166
167
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800168DEP_SPARC =
169DEP_SPARC += arch/Sparc/SparcGenAsmWriter.inc
170DEP_SPARC += arch/Sparc/SparcGenInstrInfo.inc
171DEP_SPARC += arch/Sparc/SparcGenSubtargetInfo.inc
172DEP_SPARC += arch/Sparc/SparcGenDisassemblerTables.inc
173DEP_SPARC += arch/Sparc/SparcGenRegisterInfo.inc
174
175LIBOBJ_SPARC =
176ifneq (,$(findstring sparc,$(CAPSTONE_ARCHS)))
177 CFLAGS += -DCAPSTONE_HAS_SPARC
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200178 LIBOBJ_SPARC += $(OBJDIR)/arch/Sparc/SparcDisassembler.o
179 LIBOBJ_SPARC += $(OBJDIR)/arch/Sparc/SparcInstPrinter.o
180 LIBOBJ_SPARC += $(OBJDIR)/arch/Sparc/SparcMapping.o
181 LIBOBJ_SPARC += $(OBJDIR)/arch/Sparc/SparcModule.o
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800182endif
183
184
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800185DEP_SYSZ =
186DEP_SYSZ += arch/SystemZ/SystemZGenAsmWriter.inc
187DEP_SYSZ += arch/SystemZ/SystemZGenInstrInfo.inc
188DEP_SYSZ += arch/SystemZ/SystemZGenSubtargetInfo.inc
189DEP_SYSZ += arch/SystemZ/SystemZGenDisassemblerTables.inc
190DEP_SYSZ += arch/SystemZ/SystemZGenRegisterInfo.inc
191
192LIBOBJ_SYSZ =
193ifneq (,$(findstring systemz,$(CAPSTONE_ARCHS)))
194 CFLAGS += -DCAPSTONE_HAS_SYSZ
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200195 LIBOBJ_SYSZ += $(OBJDIR)/arch/SystemZ/SystemZDisassembler.o
196 LIBOBJ_SYSZ += $(OBJDIR)/arch/SystemZ/SystemZInstPrinter.o
197 LIBOBJ_SYSZ += $(OBJDIR)/arch/SystemZ/SystemZMapping.o
198 LIBOBJ_SYSZ += $(OBJDIR)/arch/SystemZ/SystemZModule.o
199 LIBOBJ_SYSZ += $(OBJDIR)/arch/SystemZ/SystemZMCTargetDesc.o
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800200endif
201
202
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800203# by default, we compile full X86 instruction sets
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800204X86_REDUCE =
205ifneq (,$(findstring yes,$(CAPSTONE_X86_REDUCE)))
206X86_REDUCE = _reduce
Nguyen Anh Quynh81d930a2014-03-28 10:28:57 +0800207CFLAGS += -DCAPSTONE_X86_REDUCE -Os
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800208endif
209
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800210DEP_X86 =
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800211DEP_X86 += arch/X86/X86GenAsmWriter$(X86_REDUCE).inc
212DEP_X86 += arch/X86/X86GenAsmWriter1$(X86_REDUCE).inc
213DEP_X86 += arch/X86/X86GenDisassemblerTables$(X86_REDUCE).inc
214DEP_X86 += arch/X86/X86GenInstrInfo$(X86_REDUCE).inc
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800215DEP_X86 += arch/X86/X86GenRegisterInfo.inc
216
217LIBOBJ_X86 =
218ifneq (,$(findstring x86,$(CAPSTONE_ARCHS)))
219 CFLAGS += -DCAPSTONE_HAS_X86
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200220 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86DisassemblerDecoder.o
221 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Disassembler.o
222 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86IntelInstPrinter.o
Nguyen Anh Quynh50689082014-05-14 12:26:53 +0800223# assembly syntax is irrelevant in Diet mode, when this info is suppressed
224ifeq (,$(findstring yes,$(CAPSTONE_DIET)))
baguette86e84502014-08-17 20:59:05 +0200225ifeq (,$(findstring yes,$(CAPSTONE_X86_ATT_DISABLE)))
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200226 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86ATTInstPrinter.o
Nguyen Anh Quynh50689082014-05-14 12:26:53 +0800227endif
baguette86e84502014-08-17 20:59:05 +0200228endif
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200229 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Mapping.o
230 LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Module.o
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800231endif
232
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800233
234DEP_XCORE =
235DEP_XCORE += arch/XCore/XCoreGenAsmWriter.inc
236DEP_XCORE += arch/XCore/XCoreGenInstrInfo.inc
237DEP_XCORE += arch/XCore/XCoreGenDisassemblerTables.inc
238DEP_XCORE += arch/XCore/XCoreGenRegisterInfo.inc
239
240LIBOBJ_XCORE =
241ifneq (,$(findstring xcore,$(CAPSTONE_ARCHS)))
242 CFLAGS += -DCAPSTONE_HAS_XCORE
243 LIBOBJ_XCORE += $(OBJDIR)/arch/XCore/XCoreDisassembler.o
244 LIBOBJ_XCORE += $(OBJDIR)/arch/XCore/XCoreInstPrinter.o
245 LIBOBJ_XCORE += $(OBJDIR)/arch/XCore/XCoreMapping.o
246 LIBOBJ_XCORE += $(OBJDIR)/arch/XCore/XCoreModule.o
247endif
248
249
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800250LIBOBJ =
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200251LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800252LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_X86) $(LIBOBJ_XCORE)
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200253LIBOBJ += $(OBJDIR)/MCInst.o
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800254
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800255
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800256PKGCFGDIR ?= $(LIBDATADIR)/pkgconfig
Nguyen Anh Quynh315f02d2014-03-25 06:43:19 +0800257API_MAJOR=$(shell echo `grep -e CS_API_MAJOR include/capstone.h | grep -v = | awk '{print $$3}'` | awk '{print $$1}')
Nguyen Anh Quynhc0c0d522014-03-17 17:31:33 +0800258VERSION_EXT =
Nguyen Anh Quynh8f856c72014-02-24 15:17:40 +0800259
Ron Pinzd0863842015-12-16 11:46:00 -0500260IS_APPLE := $(shell $(CC) -dM -E - < /dev/null | grep -cm 1 -e __apple_build_version__ -e __APPLE_CC__)
Ole André Vadla Ravnåsc20b0492014-05-01 14:14:57 +0200261ifeq ($(IS_APPLE),1)
Jonas60f17f52017-09-05 16:45:48 +0200262# on MacOS, compile in Universal format by default
263MACOS_UNIVERSAL ?= yes
264ifeq ($(MACOS_UNIVERSAL),yes)
265CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
266LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
267endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800268EXT = dylib
pancake26943332014-02-04 08:37:55 +0100269VERSION_EXT = $(API_MAJOR).$(EXT)
danghvue1c7a212014-05-14 02:13:36 -0500270$(LIBNAME)_LDFLAGS += -dynamiclib -install_name lib$(LIBNAME).$(VERSION_EXT) -current_version $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA) -compatibility_version $(PKG_MAJOR).$(PKG_MINOR)
Nguyen Anh Quynh4ebdd212013-12-25 23:54:45 +0800271AR_EXT = a
Nguyen Anh Quynhe3606d92014-04-12 18:02:55 +0800272# Homebrew wants to make sure its formula does not disable FORTIFY_SOURCE
Nguyen Anh Quynhe31327d2014-04-29 14:25:15 +0800273# However, this is not really necessary because 'CAPSTONE_USE_SYS_DYN_MEM=yes' by default
Nguyen Anh Quynh189e83a2014-03-29 22:36:05 +0800274ifneq ($(HOMEBREW_CAPSTONE),1)
Nguyen Anh Quynhe31327d2014-04-29 14:25:15 +0800275ifneq ($(CAPSTONE_USE_SYS_DYN_MEM),yes)
Nguyen Anh Quynhc77cf3a2014-03-07 22:06:51 +0800276# remove string check because OSX kernel complains about missing symbols
277CFLAGS += -D_FORTIFY_SOURCE=0
278endif
Nguyen Anh Quynh189e83a2014-03-29 22:36:05 +0800279endif
pancake02cad6e2013-11-27 15:38:44 +0100280else
Jonas60f17f52017-09-05 16:45:48 +0200281CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
282LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
danghvue1c7a212014-05-14 02:13:36 -0500283$(LIBNAME)_LDFLAGS += -shared
Daniel Godas-Lopez0f7c4322013-11-28 18:22:50 +0000284# Cygwin?
285IS_CYGWIN := $(shell $(CC) -dumpmachine | grep -i cygwin | wc -l)
286ifeq ($(IS_CYGWIN),1)
287EXT = dll
Nguyen Anh Quynh76087d22014-05-14 14:53:51 +0800288AR_EXT = lib
Daniel Godas-Lopez0f7c4322013-11-28 18:22:50 +0000289# Cygwin doesn't like -fPIC
290CFLAGS := $(CFLAGS:-fPIC=)
291# On Windows we need the shared library to be executable
292else
293# mingw?
294IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l)
295ifeq ($(IS_MINGW),1)
296EXT = dll
Nguyen Anh Quynh76087d22014-05-14 14:53:51 +0800297AR_EXT = lib
Daniel Godas-Lopez0f7c4322013-11-28 18:22:50 +0000298# mingw doesn't like -fPIC either
299CFLAGS := $(CFLAGS:-fPIC=)
300# On Windows we need the shared library to be executable
Nguyen Anh Quynh4ebdd212013-12-25 23:54:45 +0800301else
302# Linux, *BSD
Nguyen Anh Quynhc0c0d522014-03-17 17:31:33 +0800303EXT = so
304VERSION_EXT = $(EXT).$(API_MAJOR)
Nguyen Anh Quynh6d5381e2014-03-17 10:37:08 +0800305AR_EXT = a
danghvue1c7a212014-05-14 02:13:36 -0500306$(LIBNAME)_LDFLAGS += -Wl,-soname,lib$(LIBNAME).$(VERSION_EXT)
Daniel Godas-Lopez0f7c4322013-11-28 18:22:50 +0000307endif
308endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800309endif
310
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200311ifeq ($(CAPSTONE_SHARED),yes)
Nguyen Anh Quynh0936b562014-06-04 00:26:33 +0800312ifeq ($(IS_MINGW),1)
313LIBRARY = $(BLDIR)/$(LIBNAME).$(EXT)
314else ifeq ($(IS_CYGWIN),1)
315LIBRARY = $(BLDIR)/$(LIBNAME).$(EXT)
316else # *nix
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +0800317LIBRARY = $(BLDIR)/lib$(LIBNAME).$(EXT)
Hilko Bengend83bf842015-02-26 18:37:32 +0100318CFLAGS += -fvisibility=hidden
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200319endif
Nguyen Anh Quynh0936b562014-06-04 00:26:33 +0800320endif
321
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200322ifeq ($(CAPSTONE_STATIC),yes)
Nguyen Anh Quynh0936b562014-06-04 00:26:33 +0800323ifeq ($(IS_MINGW),1)
324ARCHIVE = $(BLDIR)/$(LIBNAME).$(AR_EXT)
325else ifeq ($(IS_CYGWIN),1)
326ARCHIVE = $(BLDIR)/$(LIBNAME).$(AR_EXT)
327else
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +0800328ARCHIVE = $(BLDIR)/lib$(LIBNAME).$(AR_EXT)
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200329endif
Nguyen Anh Quynh0936b562014-06-04 00:26:33 +0800330endif
331
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +0800332PKGCFGF = $(BLDIR)/$(LIBNAME).pc
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800333
Nguyen Anh Quynh5bbbfce2014-02-28 10:49:46 +0800334.PHONY: all clean install uninstall dist
pancakeca9f1b22013-12-06 17:03:41 +0100335
Nguyen Anh Quynh80ff30b2013-12-09 00:38:44 +0800336all: $(LIBRARY) $(ARCHIVE) $(PKGCFGF)
Nguyen Anh Quynh19af3ec2015-01-30 09:07:51 +0800337ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
Adrian Herrera229593d2017-06-02 15:49:10 +0200338 @V=$(V) CC=$(CC) $(MAKE) -C cstool
Nguyen Anh Quynh243d41e2015-01-28 04:42:34 +0800339ifndef BUILDDIR
Stephen4d388002018-05-11 20:36:10 -0700340 $(MAKE) -C tests
Nguyen Anh Quynh243d41e2015-01-28 04:42:34 +0800341else
Stephen4d388002018-05-11 20:36:10 -0700342 $(MAKE) -C tests BUILDDIR=$(BLDIR)
Nguyen Anh Quynh243d41e2015-01-28 04:42:34 +0800343endif
Akihiko Odaki798821f2016-09-03 10:54:56 +0900344 $(call install-library,$(BLDIR)/tests/)
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200345endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200347ifeq ($(CAPSTONE_SHARED),yes)
pancakeaddb1922014-02-28 02:44:07 +0100348$(LIBRARY): $(LIBOBJ)
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200349ifeq ($(V),0)
Nguyen Anh Quynh8c0ab862014-06-03 23:43:53 +0700350 $(call log,LINK,$(@:$(BLDIR)/%=%))
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200351 @$(create-library)
352else
353 $(create-library)
354endif
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200355endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356
Nguyen Anh Quynh648b3982016-10-23 02:00:47 +0800357$(LIBOBJ): *.h include/*.h config.mk
Nguyen Anh Quynhaeb0c402014-03-27 17:54:27 +0800358
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800359$(LIBOBJ_ARM): $(DEP_ARM)
360$(LIBOBJ_ARM64): $(DEP_ARM64)
361$(LIBOBJ_MIPS): $(DEP_MIPS)
362$(LIBOBJ_PPC): $(DEP_PPC)
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800363$(LIBOBJ_SPARC): $(DEP_SPARC)
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800364$(LIBOBJ_SYSZ): $(DEP_SYSZ)
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800365$(LIBOBJ_X86): $(DEP_X86)
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800366$(LIBOBJ_XCORE): $(DEP_XCORE)
Nguyen Anh Quynh4a328ca2014-02-28 23:09:04 +0800367
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200368ifeq ($(CAPSTONE_STATIC),yes)
pancakeca9f1b22013-12-06 17:03:41 +0100369$(ARCHIVE): $(LIBOBJ)
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200370 @rm -f $(ARCHIVE)
371ifeq ($(V),0)
372 $(call log,AR,$(@:$(BLDIR)/%=%))
373 @$(create-archive)
374else
375 $(create-archive)
376endif
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200377endif
pancakef1cbce22013-11-27 15:31:26 +0100378
pancakeca9f1b22013-12-06 17:03:41 +0100379$(PKGCFGF):
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200380ifeq ($(V),0)
381 $(call log,GEN,$(@:$(BLDIR)/%=%))
382 @$(generate-pkgcfg)
Nguyen Anh Quynh3ff259c2014-03-07 17:11:18 +0800383else
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200384 $(generate-pkgcfg)
Nguyen Anh Quynh3ff259c2014-03-07 17:11:18 +0800385endif
pancake44ce0fb2013-12-03 04:11:37 +0100386
pancakeca9f1b22013-12-06 17:03:41 +0100387install: $(PKGCFGF) $(ARCHIVE) $(LIBRARY)
pancake02cad6e2013-11-27 15:38:44 +0100388 mkdir -p $(LIBDIR)
Akihiko Odaki798821f2016-09-03 10:54:56 +0900389 $(call install-library,$(LIBDIR))
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200390ifeq ($(CAPSTONE_STATIC),yes)
Nguyen Anh Quynh0936b562014-06-04 00:26:33 +0800391 $(INSTALL_DATA) $(ARCHIVE) $(LIBDIR)
Ole André Vadla Ravnås927fcbc2014-05-01 16:00:52 +0200392endif
pancake02cad6e2013-11-27 15:38:44 +0100393 mkdir -p $(INCDIR)/$(LIBNAME)
Nguyen Anh Quynh35240562014-01-04 09:18:48 +0800394 $(INSTALL_DATA) include/*.h $(INCDIR)/$(LIBNAME)
Nguyen Anh Quynh9b4b3772014-04-12 18:06:54 +0800395 mkdir -p $(PKGCFGDIR)
396 $(INSTALL_DATA) $(PKGCFGF) $(PKGCFGDIR)/
Nguyen Anh Quynh34603272016-10-10 22:54:16 +0800397 mkdir -p $(BINDIR)
398 $(INSTALL_LIB) cstool/cstool $(BINDIR)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800399
400uninstall:
pancake02cad6e2013-11-27 15:38:44 +0100401 rm -rf $(INCDIR)/$(LIBNAME)
Nguyen Anh Quynh6d5381e2014-03-17 10:37:08 +0800402 rm -f $(LIBDIR)/lib$(LIBNAME).*
Nguyen Anh Quynh9b4b3772014-04-12 18:06:54 +0800403 rm -f $(PKGCFGDIR)/$(LIBNAME).pc
Nguyen Anh Quynh34603272016-10-10 22:54:16 +0800404 rm -f $(BINDIR)/cstool
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405
406clean:
Nguyen Anh Quynh5519a682014-04-29 16:24:30 +0800407 rm -f $(LIBOBJ)
Nguyen Anh Quynhf4c5d5e2017-05-05 09:46:46 +0800408 rm -f $(BLDIR)/lib$(LIBNAME).* $(BLDIR)/$(LIBNAME).pc
Nguyen Anh Quynhd63ddac2014-01-09 10:35:58 +0800409 rm -f $(PKGCFGF)
Nguyen Anh Quynh34603272016-10-10 22:54:16 +0800410 $(MAKE) -C cstool clean
Nguyen Tan Cong3d6e56a2015-01-28 00:35:44 +0700411
Nguyen Anh Quynh19af3ec2015-01-30 09:07:51 +0800412ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
Stephen4d388002018-05-11 20:36:10 -0700413 $(MAKE) -C tests clean
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +0800414 rm -f $(BLDIR)/tests/lib$(LIBNAME).$(EXT)
Nguyen Tan Cong3d6e56a2015-01-28 00:35:44 +0700415endif
Nguyen Anh Quynh6ae472f2014-04-29 16:29:55 +0800416
417ifdef BUILDDIR
418 rm -rf $(BUILDDIR)
419endif
Nguyen Anh Quynh5519a682014-04-29 16:24:30 +0800420
Nguyen Anh Quynh19af3ec2015-01-30 09:07:51 +0800421ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
Stephen4d388002018-05-11 20:36:10 -0700422 $(MAKE) -C bindings/python clean
423 $(MAKE) -C bindings/java clean
424 $(MAKE) -C bindings/ocaml clean
Nguyen Tan Cong3d6e56a2015-01-28 00:35:44 +0700425endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426
Nguyen Anh Quynha580d922014-01-18 12:47:15 +0800427
428TAG ?= HEAD
429ifeq ($(TAG), HEAD)
430DIST_VERSION = latest
431else
432DIST_VERSION = $(TAG)
433endif
434
pancake35cef7f2014-01-18 03:42:15 +0100435dist:
Nguyen Anh Quynh186d5012014-01-22 18:46:20 +0800436 git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz
Nguyen Anh Quynhf4f72532014-03-11 12:29:16 +0800437 git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip
pancake35cef7f2014-01-18 03:42:15 +0100438
Nguyen Anh Quynh984d4502014-10-01 16:42:29 +0800439
Nick Briggsce3f0d02017-05-11 08:58:12 -0700440TESTS = test_basic test_detail test_arm test_arm64 test_mips test_ppc test_sparc
Nguyen Anh Quynhb16dc852014-11-02 08:21:34 +0800441TESTS += test_systemz test_x86 test_xcore test_iter
Nick Briggsce3f0d02017-05-11 08:58:12 -0700442TESTS += test_basic.static test_detail.static test_arm.static test_arm64.static
Nguyen Anh Quynh984d4502014-10-01 16:42:29 +0800443TESTS += test_mips.static test_ppc.static test_sparc.static
444TESTS += test_systemz.static test_x86.static test_xcore.static
Nguyen Anh Quynhb16dc852014-11-02 08:21:34 +0800445TESTS += test_skipdata test_skipdata.static test_iter.static
Stephen93613782018-05-25 08:59:30 -0400446check: $(TESTS)
447test_%:
448 ./tests/$@ > /dev/null && echo OK || echo FAILED
Nguyen Anh Quynh984d4502014-10-01 16:42:29 +0800449
Ole André Vadla Ravnås35f937d2014-04-29 09:00:34 +0200450$(OBJDIR)/%.o: %.c
451 @mkdir -p $(@D)
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200452ifeq ($(V),0)
453 $(call log,CC,$(@:$(OBJDIR)/%=%))
454 @$(compile)
455else
456 $(compile)
457endif
458
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200459
Akihiko Odaki798821f2016-09-03 10:54:56 +0900460ifeq ($(CAPSTONE_SHARED),yes)
461define install-library
462 $(INSTALL_LIB) $(LIBRARY) $1
463 $(if $(VERSION_EXT),
464 cd $1 && \
465 mv lib$(LIBNAME).$(EXT) lib$(LIBNAME).$(VERSION_EXT) && \
466 ln -s lib$(LIBNAME).$(VERSION_EXT) lib$(LIBNAME).$(EXT))
467endef
468else
469define install-library
470endef
471endif
472
473
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200474define create-archive
475 $(AR) q $(ARCHIVE) $(LIBOBJ)
476 $(RANLIB) $(ARCHIVE)
477endef
478
Nguyen Anh Quynh58a00472014-05-01 22:53:52 +0800479
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200480define create-library
danghvue1c7a212014-05-14 02:13:36 -0500481 $(CC) $(LDFLAGS) $($(LIBNAME)_LDFLAGS) $(LIBOBJ) -o $(LIBRARY)
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200482endef
483
Nguyen Anh Quynh58a00472014-05-01 22:53:52 +0800484
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200485define generate-pkgcfg
486 echo 'Name: capstone' > $(PKGCFGF)
487 echo 'Description: Capstone disassembly engine' >> $(PKGCFGF)
488 echo 'Version: $(PKG_VERSION)' >> $(PKGCFGF)
489 echo 'libdir=$(LIBDIR)' >> $(PKGCFGF)
Nguyen Anh Quynhfff13072014-10-02 16:14:26 +0800490 echo 'includedir=$(INCDIR)/capstone' >> $(PKGCFGF)
Ole André Vadla Ravnåsf536d402014-05-01 15:22:25 +0200491 echo 'archive=$${libdir}/libcapstone.a' >> $(PKGCFGF)
492 echo 'Libs: -L$${libdir} -lcapstone' >> $(PKGCFGF)
493 echo 'Cflags: -I$${includedir}' >> $(PKGCFGF)
494endef