e9838e27ecd72261c1cd1951e4b6ebe5fecf7353
[WebKit-https.git] / Source / ThirdParty / capstone / Source / Makefile
1 # Capstone Disassembly Engine
2 # By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014
3
4 include config.mk
5 include pkgconfig.mk    # package version
6 include functions.mk
7
8 # Verbose output?
9 V ?= 0
10
11 ifeq ($(PKG_EXTRA),)
12 PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR)
13 else
14 PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA)
15 endif
16
17 ifeq ($(CROSS),)
18 CC ?= cc
19 AR ?= ar
20 RANLIB ?= ranlib
21 STRIP ?= strip
22 else
23 CC = $(CROSS)gcc
24 AR = $(CROSS)ar
25 RANLIB = $(CROSS)ranlib
26 STRIP = $(CROSS)strip
27 endif
28
29 ifneq (,$(findstring yes,$(CAPSTONE_DIET)))
30 CFLAGS ?= -Os
31 CFLAGS += -DCAPSTONE_DIET
32 else
33 CFLAGS ?= -O3
34 endif
35
36 ifneq (,$(findstring yes,$(CAPSTONE_X86_ATT_DISABLE)))
37 CFLAGS += -DCAPSTONE_X86_ATT_DISABLE
38 endif
39
40 CFLAGS += -fPIC -Wall -Iinclude
41
42 ifeq ($(CAPSTONE_USE_SYS_DYN_MEM),yes)
43 CFLAGS += -DCAPSTONE_USE_SYS_DYN_MEM
44 endif
45
46 ifeq ($(CAPSTONE_HAS_OSXKERNEL), yes)
47 CFLAGS += -DCAPSTONE_HAS_OSXKERNEL
48 SDKROOT ?= $(shell xcodebuild -version -sdk macosx Path)
49 CFLAGS += -mmacosx-version-min=10.5 \
50                   -isysroot$(SDKROOT) \
51                   -I$(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers \
52                   -mkernel \
53                   -fno-builtin
54 endif
55
56 PREFIX ?= /usr
57 DESTDIR ?=
58 ifndef BUILDDIR
59 BLDIR = .
60 OBJDIR = .
61 else
62 BLDIR = $(abspath $(BUILDDIR))
63 OBJDIR = $(BLDIR)/obj
64 endif
65 INCDIR ?= $(PREFIX)/include
66
67 UNAME_S := $(shell uname -s)
68
69 LIBDIRARCH ?= lib
70 # Uncomment the below line to installs x86_64 libs to lib64/ directory.
71 # Or better, pass 'LIBDIRARCH=lib64' to 'make install/uninstall' via 'make.sh'.
72 #LIBDIRARCH ?= lib64
73 LIBDIR ?= $(PREFIX)/$(LIBDIRARCH)
74 BINDIR = $(PREFIX)/bin
75
76 LIBDATADIR ?= $(LIBDIR)
77
78 # Don't redefine $LIBDATADIR when global environment variable
79 # USE_GENERIC_LIBDATADIR is set. This is used by the pkgsrc framework.
80
81 ifndef USE_GENERIC_LIBDATADIR
82 ifeq ($(UNAME_S), FreeBSD)
83 LIBDATADIR = $(PREFIX)/libdata
84 endif
85 ifeq ($(UNAME_S), DragonFly)
86 LIBDATADIR = $(PREFIX)/libdata
87 endif
88 endif
89
90 INSTALL_BIN ?= install
91 INSTALL_DATA ?= $(INSTALL_BIN) -m0644
92 INSTALL_LIB ?= $(INSTALL_BIN) -m0755
93
94 LIBNAME = capstone
95
96
97 DEP_ARM =
98 DEP_ARM += $(wildcard arch/ARM/ARM*.inc)
99
100 LIBOBJ_ARM =
101 ifneq (,$(findstring arm,$(CAPSTONE_ARCHS)))
102         CFLAGS += -DCAPSTONE_HAS_ARM
103         LIBSRC_ARM += $(wildcard arch/ARM/ARM*.c)
104         LIBOBJ_ARM += $(LIBSRC_ARM:%.c=$(OBJDIR)/%.o)
105 endif
106
107 DEP_ARM64 =
108 DEP_ARM64 += $(wildcard arch/AArch64/AArch64*.inc)
109
110 LIBOBJ_ARM64 =
111 ifneq (,$(findstring aarch64,$(CAPSTONE_ARCHS)))
112         CFLAGS += -DCAPSTONE_HAS_ARM64
113         LIBSRC_ARM64 += $(wildcard arch/AArch64/AArch64*.c)
114         LIBOBJ_ARM64 += $(LIBSRC_ARM64:%.c=$(OBJDIR)/%.o)
115 endif
116
117
118 DEP_M68K =
119 DEP_M68K += $(wildcard arch/M68K/M68K*.h)
120
121 LIBOBJ_M68K =
122 ifneq (,$(findstring m68k,$(CAPSTONE_ARCHS)))
123         CFLAGS += -DCAPSTONE_HAS_M68K
124         LIBSRC_M68K += $(wildcard arch/M68K/M68K*.c)
125         LIBOBJ_M68K += $(LIBSRC_M68K:%.c=$(OBJDIR)/%.o)
126 endif
127
128 DEP_MIPS =
129 DEP_MIPS += $(wildcard arch/Mips/Mips*.inc)
130
131 LIBOBJ_MIPS =
132 ifneq (,$(findstring mips,$(CAPSTONE_ARCHS)))
133         CFLAGS += -DCAPSTONE_HAS_MIPS
134         LIBSRC_MIPS += $(wildcard arch/Mips/Mips*.c)
135         LIBOBJ_MIPS += $(LIBSRC_MIPS:%.c=$(OBJDIR)/%.o)
136 endif
137
138
139 DEP_PPC =
140 DEP_PPC += $(wildcard arch/PowerPC/PPC*.inc)
141
142 LIBOBJ_PPC =
143 ifneq (,$(findstring powerpc,$(CAPSTONE_ARCHS)))
144         CFLAGS += -DCAPSTONE_HAS_POWERPC
145         LIBSRC_PPC += $(wildcard arch/PowerPC/PPC*.c)
146         LIBOBJ_PPC += $(LIBSRC_PPC:%.c=$(OBJDIR)/%.o)
147 endif
148
149
150 DEP_SPARC =
151 DEP_SPARC += $(wildcard arch/Sparc/Sparc*.inc)
152
153 LIBOBJ_SPARC =
154 ifneq (,$(findstring sparc,$(CAPSTONE_ARCHS)))
155         CFLAGS += -DCAPSTONE_HAS_SPARC
156         LIBSRC_SPARC += $(wildcard arch/Sparc/Sparc*.c)
157         LIBOBJ_SPARC += $(LIBSRC_SPARC:%.c=$(OBJDIR)/%.o)
158 endif
159
160
161 DEP_SYSZ =
162 DEP_SYSZ += $(wildcard arch/SystemZ/SystemZ*.inc)
163
164 LIBOBJ_SYSZ =
165 ifneq (,$(findstring systemz,$(CAPSTONE_ARCHS)))
166         CFLAGS += -DCAPSTONE_HAS_SYSZ
167         LIBSRC_SYSZ += $(wildcard arch/SystemZ/SystemZ*.c)
168         LIBOBJ_SYSZ += $(LIBSRC_SYSZ:%.c=$(OBJDIR)/%.o)
169 endif
170
171
172 # by default, we compile full X86 instruction sets
173 X86_REDUCE =
174 ifneq (,$(findstring yes,$(CAPSTONE_X86_REDUCE)))
175 X86_REDUCE = _reduce
176 CFLAGS += -DCAPSTONE_X86_REDUCE -Os
177 endif
178
179 DEP_X86 =
180 DEP_X86 += arch/X86/X86GenAsmWriter$(X86_REDUCE).inc
181 DEP_X86 += arch/X86/X86GenAsmWriter1$(X86_REDUCE).inc
182 DEP_X86 += arch/X86/X86GenDisassemblerTables$(X86_REDUCE).inc
183 DEP_X86 += arch/X86/X86GenInstrInfo$(X86_REDUCE).inc
184 DEP_X86 += arch/X86/X86GenRegisterInfo.inc
185 DEP_X86 += arch/X86/X86MappingInsn$(X86_REDUCE).inc
186 DEP_X86 += arch/X86/X86MappingInsnOp$(X86_REDUCE).inc
187 DEP_X86 += arch/X86/X86ImmSize.inc
188
189 LIBOBJ_X86 =
190 ifneq (,$(findstring x86,$(CAPSTONE_ARCHS)))
191         CFLAGS += -DCAPSTONE_HAS_X86
192         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86DisassemblerDecoder.o
193         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Disassembler.o
194         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86IntelInstPrinter.o
195 # assembly syntax is irrelevant in Diet mode, when this info is suppressed
196 ifeq (,$(findstring yes,$(CAPSTONE_DIET)))
197 ifeq (,$(findstring yes,$(CAPSTONE_X86_ATT_DISABLE)))
198         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86ATTInstPrinter.o
199 endif
200 endif
201         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Mapping.o
202         LIBOBJ_X86 += $(OBJDIR)/arch/X86/X86Module.o
203 endif
204
205
206 DEP_XCORE =
207 DEP_XCORE += $(wildcard arch/XCore/XCore*.inc)
208
209 LIBOBJ_XCORE =
210 ifneq (,$(findstring xcore,$(CAPSTONE_ARCHS)))
211         CFLAGS += -DCAPSTONE_HAS_XCORE
212         LIBSRC_XCORE += $(wildcard arch/XCore/XCore*.c)
213         LIBOBJ_XCORE += $(LIBSRC_XCORE:%.c=$(OBJDIR)/%.o)
214 endif
215
216
217 DEP_TMS320C64X =
218 DEP_TMS320C64X += $(wildcard arch/TMS320C64x/TMS320C64x*.inc)
219
220 LIBOBJ_TMS320C64X =
221 ifneq (,$(findstring tms320c64x,$(CAPSTONE_ARCHS)))
222         CFLAGS += -DCAPSTONE_HAS_TMS320C64X
223         LIBSRC_TMS320C64X += $(wildcard arch/TMS320C64x/TMS320C64x*.c)
224         LIBOBJ_TMS320C64X += $(LIBSRC_TMS320C64X:%.c=$(OBJDIR)/%.o)
225 endif
226
227 DEP_M680X =
228 DEP_M680X += $(wildcard arch/M680X/*.inc)
229 DEP_M680X += $(wildcard arch/M680X/M680X*.h)
230
231 LIBOBJ_M680X =
232 ifneq (,$(findstring m680x,$(CAPSTONE_ARCHS)))
233         CFLAGS += -DCAPSTONE_HAS_M680X
234         LIBSRC_M680X += $(wildcard arch/M680X/*.c)
235         LIBOBJ_M680X += $(LIBSRC_M680X:%.c=$(OBJDIR)/%.o)
236 endif
237
238
239 DEP_EVM =
240 DEP_EVM += $(wildcard arch/EVM/EVM*.inc)
241
242 LIBOBJ_EVM =
243 ifneq (,$(findstring evm,$(CAPSTONE_ARCHS)))
244         CFLAGS += -DCAPSTONE_HAS_EVM
245         LIBSRC_EVM += $(wildcard arch/EVM/EVM*.c)
246         LIBOBJ_EVM += $(LIBSRC_EVM:%.c=$(OBJDIR)/%.o)
247 endif
248
249
250 LIBOBJ =
251 LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o
252 LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM)
253 LIBOBJ += $(OBJDIR)/MCInst.o
254
255
256 ifeq ($(PKG_EXTRA),)
257 PKGCFGDIR = $(LIBDATADIR)/pkgconfig
258 else
259 PKGCFGDIR ?= $(LIBDATADIR)/pkgconfig
260 ifeq ($(PKGCFGDIR),)
261 PKGCFGDIR = $(LIBDATADIR)/pkgconfig
262 endif
263 endif
264
265 API_MAJOR=$(shell echo `grep -e CS_API_MAJOR include/capstone/capstone.h | grep -v = | awk '{print $$3}'` | awk '{print $$1}')
266 VERSION_EXT =
267
268 IS_APPLE := $(shell $(CC) -dM -E - < /dev/null 2> /dev/null | grep __apple_build_version__ | wc -l | tr -d " ")
269 ifeq ($(IS_APPLE),1)
270 # on MacOS, compile in Universal format by default
271 MACOS_UNIVERSAL ?= yes
272 ifeq ($(MACOS_UNIVERSAL),yes)
273 CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
274 LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
275 endif
276 EXT = dylib
277 VERSION_EXT = $(API_MAJOR).$(EXT)
278 $(LIBNAME)_LDFLAGS += -dynamiclib -install_name lib$(LIBNAME).$(VERSION_EXT) -current_version $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA) -compatibility_version $(PKG_MAJOR).$(PKG_MINOR)
279 AR_EXT = a
280 # Homebrew wants to make sure its formula does not disable FORTIFY_SOURCE
281 # However, this is not really necessary because 'CAPSTONE_USE_SYS_DYN_MEM=yes' by default
282 ifneq ($(HOMEBREW_CAPSTONE),1)
283 ifneq ($(CAPSTONE_USE_SYS_DYN_MEM),yes)
284 # remove string check because OSX kernel complains about missing symbols
285 CFLAGS += -D_FORTIFY_SOURCE=0
286 endif
287 endif
288 else
289 CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
290 LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
291 $(LIBNAME)_LDFLAGS += -shared
292 # Cygwin?
293 IS_CYGWIN := $(shell $(CC) -dumpmachine 2>/dev/null | grep -i cygwin | wc -l)
294 ifeq ($(IS_CYGWIN),1)
295 EXT = dll
296 AR_EXT = lib
297 # Cygwin doesn't like -fPIC
298 CFLAGS := $(CFLAGS:-fPIC=)
299 # On Windows we need the shared library to be executable
300 else
301 # mingw?
302 IS_MINGW := $(shell $(CC) --version 2>/dev/null | grep -i mingw | wc -l)
303 ifeq ($(IS_MINGW),1)
304 EXT = dll
305 AR_EXT = lib
306 # mingw doesn't like -fPIC either
307 CFLAGS := $(CFLAGS:-fPIC=)
308 # On Windows we need the shared library to be executable
309 else
310 # Linux, *BSD
311 EXT = so
312 VERSION_EXT = $(EXT).$(API_MAJOR)
313 AR_EXT = a
314 $(LIBNAME)_LDFLAGS += -Wl,-soname,lib$(LIBNAME).$(VERSION_EXT)
315 endif
316 endif
317 endif
318
319 ifeq ($(CAPSTONE_SHARED),yes)
320 ifeq ($(IS_MINGW),1)
321 LIBRARY = $(BLDIR)/$(LIBNAME).$(VERSION_EXT)
322 else ifeq ($(IS_CYGWIN),1)
323 LIBRARY = $(BLDIR)/$(LIBNAME).$(VERSION_EXT)
324 else    # *nix
325 LIBRARY = $(BLDIR)/lib$(LIBNAME).$(VERSION_EXT)
326 CFLAGS += -fvisibility=hidden
327 endif
328 endif
329
330 ifeq ($(CAPSTONE_STATIC),yes)
331 ifeq ($(IS_MINGW),1)
332 ARCHIVE = $(BLDIR)/$(LIBNAME).$(AR_EXT)
333 else ifeq ($(IS_CYGWIN),1)
334 ARCHIVE = $(BLDIR)/$(LIBNAME).$(AR_EXT)
335 else
336 ARCHIVE = $(BLDIR)/lib$(LIBNAME).$(AR_EXT)
337 endif
338 endif
339
340 PKGCFGF = $(BLDIR)/$(LIBNAME).pc
341
342 .PHONY: all clean install uninstall dist
343
344 all: $(LIBRARY) $(ARCHIVE) $(PKGCFGF)
345 ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
346         @V=$(V) CC=$(CC) $(MAKE) -C cstool
347 ifndef BUILDDIR
348         cd tests && $(MAKE)
349 else
350         cd tests && $(MAKE) BUILDDIR=$(BLDIR)
351 endif
352         $(call install-library,$(BLDIR)/tests/)
353 endif
354
355 ifeq ($(CAPSTONE_SHARED),yes)
356 $(LIBRARY): $(LIBOBJ)
357 ifeq ($(V),0)
358         $(call log,LINK,$(@:$(BLDIR)/%=%))
359         @$(create-library)
360 else
361         $(create-library)
362 endif
363 endif
364
365 $(LIBOBJ): config.mk *.h include/capstone/*.h
366
367 $(LIBOBJ_ARM): $(DEP_ARM)
368 $(LIBOBJ_ARM64): $(DEP_ARM64)
369 $(LIBOBJ_M68K): $(DEP_M68K)
370 $(LIBOBJ_MIPS): $(DEP_MIPS)
371 $(LIBOBJ_PPC): $(DEP_PPC)
372 $(LIBOBJ_SPARC): $(DEP_SPARC)
373 $(LIBOBJ_SYSZ): $(DEP_SYSZ)
374 $(LIBOBJ_X86): $(DEP_X86)
375 $(LIBOBJ_XCORE): $(DEP_XCORE)
376 $(LIBOBJ_TMS320C64X): $(DEP_TMS320C64X)
377 $(LIBOBJ_M680X): $(DEP_M680X)
378 $(LIBOBJ_EVM): $(DEP_EVM)
379
380 ifeq ($(CAPSTONE_STATIC),yes)
381 $(ARCHIVE): $(LIBOBJ)
382         @rm -f $(ARCHIVE)
383 ifeq ($(V),0)
384         $(call log,AR,$(@:$(BLDIR)/%=%))
385         @$(create-archive)
386 else
387         $(create-archive)
388 endif
389 endif
390
391 $(PKGCFGF):
392 ifeq ($(V),0)
393         $(call log,GEN,$(@:$(BLDIR)/%=%))
394         @$(generate-pkgcfg)
395 else
396         $(generate-pkgcfg)
397 endif
398
399 install: $(PKGCFGF) $(ARCHIVE) $(LIBRARY)
400         mkdir -p $(DESTDIR)$(LIBDIR)
401         $(call install-library,$(DESTDIR)$(LIBDIR))
402 ifeq ($(CAPSTONE_STATIC),yes)
403         $(INSTALL_DATA) $(ARCHIVE) $(DESTDIR)$(LIBDIR)
404 endif
405         mkdir -p $(DESTDIR)$(INCDIR)/$(LIBNAME)
406         $(INSTALL_DATA) include/capstone/*.h $(DESTDIR)$(INCDIR)/$(LIBNAME)
407         mkdir -p $(DESTDIR)$(PKGCFGDIR)
408         $(INSTALL_DATA) $(PKGCFGF) $(DESTDIR)$(PKGCFGDIR)
409         mkdir -p $(DESTDIR)$(BINDIR)
410         $(INSTALL_LIB) cstool/cstool $(DESTDIR)$(BINDIR)
411
412 uninstall:
413         rm -rf $(DESTDIR)$(INCDIR)/$(LIBNAME)
414         rm -f $(DESTDIR)$(LIBDIR)/lib$(LIBNAME).*
415         rm -f $(DESTDIR)$(PKGCFGDIR)/$(LIBNAME).pc
416         rm -f $(DESTDIR)$(BINDIR)/cstool
417
418 clean:
419         rm -f $(LIBOBJ)
420         rm -f $(BLDIR)/lib$(LIBNAME).* $(BLDIR)/$(LIBNAME).pc
421         rm -f $(PKGCFGF)
422         $(MAKE) -C cstool clean
423
424 ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
425         cd tests && $(MAKE) clean
426         rm -f $(BLDIR)/tests/lib$(LIBNAME).$(EXT)
427 endif
428
429 ifdef BUILDDIR
430         rm -rf $(BUILDDIR)
431 endif
432
433 ifeq (,$(findstring yes,$(CAPSTONE_BUILD_CORE_ONLY)))
434         cd bindings/python && $(MAKE) clean
435         cd bindings/java && $(MAKE) clean
436         cd bindings/ocaml && $(MAKE) clean
437 endif
438
439
440 TAG ?= HEAD
441 ifeq ($(TAG), HEAD)
442 DIST_VERSION = latest
443 else
444 DIST_VERSION = $(TAG)
445 endif
446
447 dist:
448         git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz
449         git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip
450
451
452 TESTS = test_basic test_detail test_arm test_arm64 test_m68k test_mips test_ppc test_sparc
453 TESTS += test_systemz test_x86 test_xcore test_iter
454 TESTS += test_basic.static test_detail.static test_arm.static test_arm64.static
455 TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static
456 TESTS += test_systemz.static test_x86.static test_xcore.static test_m680x.static
457 TESTS += test_skipdata test_skipdata.static test_iter.static
458 check:
459         @for t in $(TESTS); do \
460                 echo Check $$t ... ; \
461                 LD_LIBRARY_PATH=./tests ./tests/$$t > /dev/null && echo OK || echo FAILED; \
462         done
463
464 $(OBJDIR)/%.o: %.c
465         @mkdir -p $(@D)
466 ifeq ($(V),0)
467         $(call log,CC,$(@:$(OBJDIR)/%=%))
468         @$(compile)
469 else
470         $(compile)
471 endif
472
473
474 ifeq ($(CAPSTONE_SHARED),yes)
475 define install-library
476         $(INSTALL_LIB) $(LIBRARY) $1
477         $(if $(VERSION_EXT),
478                 cd $1 && \
479                 rm -f lib$(LIBNAME).$(EXT) && \
480                 ln -s lib$(LIBNAME).$(VERSION_EXT) lib$(LIBNAME).$(EXT))
481 endef
482 else
483 define install-library
484 endef
485 endif
486
487
488 define create-archive
489         $(AR) q $(ARCHIVE) $(LIBOBJ)
490         $(RANLIB) $(ARCHIVE)
491 endef
492
493
494 define create-library
495         $(CC) $(LDFLAGS) $($(LIBNAME)_LDFLAGS) $(LIBOBJ) -o $(LIBRARY)
496 endef
497
498
499 define generate-pkgcfg
500         mkdir -p $(BLDIR)
501         echo 'Name: capstone' > $(PKGCFGF)
502         echo 'Description: Capstone disassembly engine' >> $(PKGCFGF)
503         echo 'Version: $(PKG_VERSION)' >> $(PKGCFGF)
504         echo 'libdir=$(LIBDIR)' >> $(PKGCFGF)
505         echo 'includedir=$(INCDIR)' >> $(PKGCFGF)
506         echo 'archive=$${libdir}/libcapstone.a' >> $(PKGCFGF)
507         echo 'Libs: -L$${libdir} -lcapstone' >> $(PKGCFGF)
508         echo 'Cflags: -I$${includedir}' >> $(PKGCFGF)
509 endef