From 5d3f94ab91d6cf106b6d73b877c91327662c1c96 Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Wed, 27 Nov 2013 10:08:11 +0000 Subject: [PATCH 1/7] Compile correctly under Cygwin using the following toolchains: x86_64-pc-cygwin- x86_64-w64-mingw32- i686-pc-mingw32- --- Makefile | 24 +++++++++++------------- tests/Makefile | 20 ++++++++------------ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index a2186d57aa..b63dc2f0d9 100644 --- a/Makefile +++ b/Makefile @@ -21,14 +21,22 @@ LIBOBJ += MCInst.o # by default, lib extension is .so EXT = so -# OSX is the exception +# OSX UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Darwin) EXT = dylib endif +# Cygwin +UNAME_S := $(shell uname -s | sed 's|.*\(CYGWIN\).*|CYGWIN|') +ifeq ($(UNAME_S),CYGWIN) +EXT = dll +# Cygwin doesn't like -fPIC +CFLAGS := $(CFLAGS:-fPIC=) +endif + -.PHONY: all clean lib windows win_lib install uninstall +.PHONY: all clean lib install uninstall all: lib make -C tests @@ -52,18 +60,8 @@ uninstall: rm -rf /usr/include/$(LIBNAME) rm -rf /usr/lib/lib$(LIBNAME).$(EXT) -# Mingw32 -windows: win_lib - install -m0644 $(LIBNAME).dll tests - make -C tests windows - -# Mingw32 -win_lib: $(LIBOBJ) - $(CC) $(LDFLAGS) $(LIBOBJ) -o $(LIBNAME).dll - strip $(LIBNAME).dll - clean: - rm -f $(LIBOBJ) lib$(LIBNAME).* $(LIBNAME).dll + rm -f $(LIBOBJ) lib$(LIBNAME).* #cd bindings/ruby; make clean; rm -rf Makefile cd bindings/python; make clean cd bindings/csharp; make clean diff --git a/tests/Makefile b/tests/Makefile index 56cb43f942..6797e74785 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -10,7 +10,13 @@ CFLAGS += -fPIC -O3 -Wall -I$(INCDIR) -L$(LIBDIR) LIBNAME = capstone -.PHONY: all clean win_test +# Cygwin +UNAME_S := $(shell uname -s | sed 's|.*\(CYGWIN\).*|CYGWIN|') +ifeq ($(UNAME_S),CYGWIN) +CFLAGS := $(CFLAGS:-fPIC=) +endif + +.PHONY: all clean all: test.o test_detail.o test_x86.o test_arm64.o test_arm.o test_mips.o ${CC} $(CFLAGS) test.o -O3 -Wall -l$(LIBNAME) -o test @@ -20,15 +26,6 @@ all: test.o test_detail.o test_x86.o test_arm64.o test_arm.o test_mips.o ${CC} $(CFLAGS) test_arm.o -O3 -Wall -l$(LIBNAME) -o test_arm ${CC} $(CFLAGS) test_mips.o -O3 -Wall -l$(LIBNAME) -o test_mips -# Mingw32 -windows: test.o test_detail.o test_x86.o test_arm64.o test_arm.o test_mips.o - ${CC} test.o -O3 -Wall $(LIBNAME).dll -o test.exe - ${CC} test_detail.o -O3 -Wall $(LIBNAME).dll -o test_detail.exe - ${CC} test_x86.o -O3 -Wall $(LIBNAME).dll -o test_x86.exe - ${CC} test_arm64.o -O3 -Wall $(LIBNAME).dll -o test_arm64.exe - ${CC} test_arm.o -O3 -Wall $(LIBNAME).dll -o test_arm.exe - ${CC} test_mips.o -O3 -Wall $(LIBNAME).dll -o test_mips.exe - clean: rm -rf test_x86 test_x86.exe test_x86.o rm -rf test_arm64 test_arm64.exe test_arm64.o @@ -36,8 +33,7 @@ clean: rm -rf test_mips test_mips.exe test_mips.o rm -rf test test.exe test.o rm -rf test_detail test_detail.exe test_detail.o - rm -rf *.dll - rm -rf *.so + rm -f libcapstone.* .c.o: ${CC} ${CFLAGS} -c $< -o $@ From 28775cb20c8417848cd4349a8d210d586b469033 Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Wed, 27 Nov 2013 10:19:59 +0000 Subject: [PATCH 2/7] Removed a couple of inoffensive compiler warnings --- arch/AArch64/AArch64BaseInfo.c | 2 +- arch/X86/mapping.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/AArch64/AArch64BaseInfo.c b/arch/AArch64/AArch64BaseInfo.c index b63094d5ee..bbed5a8ee9 100644 --- a/arch/AArch64/AArch64BaseInfo.c +++ b/arch/AArch64/AArch64BaseInfo.c @@ -41,7 +41,7 @@ static bool compare_lower_str(char *s1, char *s2) { char *lower = strdup(s2), *c; for (c = lower; *c; c++) - *c = tolower(*c); + *c = tolower((int) *c); bool res = (strcmp(s1, lower) == 0); free(lower); diff --git a/arch/X86/mapping.c b/arch/X86/mapping.c index 0f42b31995..5120e06c60 100644 --- a/arch/X86/mapping.c +++ b/arch/X86/mapping.c @@ -26,7 +26,7 @@ static enum { X86_REG_BP_DI = 503, X86_REG_sib = 504, X86_REG_sib64 = 505 -} _dummy; +} __attribute__((unused)) _dummy; static x86_reg sib_index_map[] = { X86_REG_INVALID, From a1b56226db61f1e5594d88bd2614ea6f76ee9b35 Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Wed, 27 Nov 2013 21:35:58 +0000 Subject: [PATCH 3/7] make shared library executable if we are using Cygwin --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b63dc2f0d9..b011b12dc3 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ LIBOBJ += MCInst.o # by default, lib extension is .so EXT = so +PERMS = 0644 # OSX UNAME_S := $(shell uname -s) @@ -33,6 +34,8 @@ ifeq ($(UNAME_S),CYGWIN) EXT = dll # Cygwin doesn't like -fPIC CFLAGS := $(CFLAGS:-fPIC=) +# On Windows we need the shared library to be executable +PERMS = 0755 endif @@ -40,7 +43,7 @@ endif all: lib make -C tests - install -m0644 lib$(LIBNAME).$(EXT) tests + install -m$(PERMS) lib$(LIBNAME).$(EXT) tests lib: $(LIBOBJ) $(CC) $(LDFLAGS) $(LIBOBJ) -o lib$(LIBNAME).$(EXT) @@ -48,7 +51,7 @@ lib: $(LIBOBJ) #strip lib$(LIBNAME).$(EXT) install: lib - install -m0644 lib$(LIBNAME).$(EXT) /usr/lib + install -m$(PERMS) lib$(LIBNAME).$(EXT) /usr/lib mkdir -p /usr/include/$(LIBNAME) install -m0644 include/capstone.h /usr/include/$(LIBNAME) install -m0644 include/x86.h /usr/include/$(LIBNAME) From 07a89c9a91cdcec9ef68741a6cbf3a3cb51681da Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Thu, 28 Nov 2013 09:45:51 +0000 Subject: [PATCH 4/7] fixes to unbreak the compile script --- Makefile | 17 +++++++++++++---- compile.sh | 24 ++++++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index b011b12dc3..f02711629b 100644 --- a/Makefile +++ b/Makefile @@ -22,13 +22,12 @@ LIBOBJ += MCInst.o EXT = so PERMS = 0644 -# OSX +# OSX? UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Darwin) EXT = dylib -endif - -# Cygwin +else +# Cygwin? UNAME_S := $(shell uname -s | sed 's|.*\(CYGWIN\).*|CYGWIN|') ifeq ($(UNAME_S),CYGWIN) EXT = dll @@ -36,6 +35,16 @@ EXT = dll CFLAGS := $(CFLAGS:-fPIC=) # On Windows we need the shared library to be executable PERMS = 0755 +else +# mingw? +x86_64-w64-mingw32-gcc.exe --version | grep -i mingw +IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l) +ifeq ($(IS_MINGW),1) +EXT = dll +# On Windows we need the shared library to be executable +PERMS = 0755 +endif +endif endif diff --git a/compile.sh b/compile.sh index 08ffb84679..c6da2f6867 100755 --- a/compile.sh +++ b/compile.sh @@ -3,11 +3,23 @@ # Capstone Disassembler Engine # By Nguyen Anh Quynh , 2013> +function build { + make clean + + if [ ${CC}x != x ]; then + make CC=$CC + else + make + fi +} + case "$1" in - "" ) make clean; make;; - "nix32" ) make clean; CFLAGS=-m32 LDFLAGS=-m32 make;; - "clang" ) make clean; make CC=clang;; - "win32" ) make clean; make CROSS=i686-w64-mingw32- windows;; - "win64" ) make clean; make CROSS=x86_64-w64-mingw32- windows;; - * ) echo "Usage: compile.sh [nix32|clang|win32|win64]"; exit 1;; + "" ) build;; + "nix32" ) CFLAGS=-m32 LDFLAGS=-m32 build;; + "clang" ) CC=clang build;; + "cross-win32" ) CROSS=i686-w64-mingw32- build;; + "cross-win64" ) CROSS=x86_64-w64-mingw32- build;; + "cygwin-mingw32" ) CROSS=i686-pc-mingw32- build;; + "cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build;; + * ) echo "Usage: compile.sh [nix32|clang|cross-win32|cross-win64|cygwin-mingw32|cygwin-mingw64]"; exit 1;; esac From 1aa1f457045a63ad5cf85127c9bec9d69481f4fe Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Thu, 28 Nov 2013 09:55:16 +0000 Subject: [PATCH 5/7] removed some garbage --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index f02711629b..7beaa1e614 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,6 @@ CFLAGS := $(CFLAGS:-fPIC=) PERMS = 0755 else # mingw? -x86_64-w64-mingw32-gcc.exe --version | grep -i mingw IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l) ifeq ($(IS_MINGW),1) EXT = dll From 9d31fee70b728698ee30eeffa14b5dcf52e121f6 Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Thu, 28 Nov 2013 10:03:33 +0000 Subject: [PATCH 6/7] remove -fPIC when compiling with mingw to avoid warnings --- Makefile | 2 ++ tests/Makefile | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7beaa1e614..e9b20a14d2 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,8 @@ else IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l) ifeq ($(IS_MINGW),1) EXT = dll +# mingw doesn't like -fPIC either +CFLAGS := $(CFLAGS:-fPIC=) # On Windows we need the shared library to be executable PERMS = 0755 endif diff --git a/tests/Makefile b/tests/Makefile index 6797e74785..f102dca613 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -10,10 +10,16 @@ CFLAGS += -fPIC -O3 -Wall -I$(INCDIR) -L$(LIBDIR) LIBNAME = capstone -# Cygwin +# Cygwin? UNAME_S := $(shell uname -s | sed 's|.*\(CYGWIN\).*|CYGWIN|') ifeq ($(UNAME_S),CYGWIN) CFLAGS := $(CFLAGS:-fPIC=) +else +# mingw? +IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l) +ifeq ($(IS_MINGW),1) +CFLAGS := $(CFLAGS:-fPIC=) +endif endif .PHONY: all clean From 227046403866c0b6910bfc02d8e39f4db261d1a6 Mon Sep 17 00:00:00 2001 From: Daniel Godas-Lopez Date: Thu, 28 Nov 2013 10:59:33 +0000 Subject: [PATCH 7/7] updated documentation for cross-compiling --- COMPILE | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/COMPILE b/COMPILE index c97fc28cfd..e5824b88ec 100644 --- a/COMPILE +++ b/COMPILE @@ -45,20 +45,29 @@ only following files: if you want 64-bit binaries) are required. - To cross-compile Windows 32-bit binary, simply run - $ ./compile.sh win32 + $ ./compile.sh cross-win32 - To cross-compile Windows 64-bit binary, simply run - $ ./compile.sh win64 + $ ./compile.sh cross-win64 Resulted files "capstone.dll" and "tests/test*.exe" can then be used on Windows machine. -(4) By default, gcc is used as compiler. If you want to use "clang" instead, compile +(4) To compile under Cygwin gcc-mingw-w64-i686 or x86_64-w64-mingw32 run: + + - To compile Windows 32-bit binary under Cygwin, simply run + $ ./compile.sh cygwin-mingw32 + + - To compile Windows 64-bit binary under Cygwin, simply run + $ ./compile.sh cygwin-mingw64 + + +(5) By default, gcc is used as compiler. If you want to use "clang" instead, compile the code with: $ ./compile clang -(5) So far, Python, Ruby, Ocaml, Java, C# and Go are supported by bindings. Look for the bindings +(6) So far, Python, Ruby, Ocaml, Java, C# and Go are supported by bindings. Look for the bindings under directory bindings/, and refer to README file of corresponding languages.