blob: d9f15e10d0d745ae65a9fe4b831685da6109e7c7 [file] [log] [blame]
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +08001#!/usr/bin/env python
Michael Cohen18ee47b2014-11-15 22:15:12 +01002import glob
3import os
4import platform
5import shutil
Nguyen Tan Cong3d6e56a2015-01-28 00:35:44 +07006import stat
Nguyen Tan Cong3e605a02015-01-28 11:54:02 +07007import sys
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +08008
neuromancer6fd80442015-06-02 14:31:36 +02009from site import getusersitepackages
Michael Cohen18ee47b2014-11-15 22:15:12 +010010from distutils import log
11from distutils import dir_util
12from distutils.command.build_clib import build_clib
13from distutils.command.sdist import sdist
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +080014from distutils.core import setup
Nguyen Tan Cong3e605a02015-01-28 11:54:02 +070015from distutils.sysconfig import get_python_lib
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +080016
Nguyen Anh Quynh0d55bde2015-02-03 09:05:57 +080017# prebuilt libraries for Windows - for sdist
18PATH_LIB64 = "prebuilt/win64/capstone.dll"
19PATH_LIB32 = "prebuilt/win32/capstone.dll"
Nguyen Tan Cong60dcfa02015-01-29 17:06:14 +070020
Nguyen Anh Quynhca101282015-02-03 22:31:19 +080021# package name can be 'capstone' or 'capstone-windows'
Nguyen Anh Quynha2b60302015-02-03 11:45:20 +080022PKG_NAME = 'capstone'
23if os.path.exists(PATH_LIB64) and os.path.exists(PATH_LIB32):
Nguyen Anh Quynh4892c452015-02-03 22:30:10 +080024 PKG_NAME = 'capstone-windows'
Nguyen Anh Quynha2b60302015-02-03 11:45:20 +080025
Nguyen Anh Quynh697ee402015-05-08 17:06:59 +080026VERSION = '3.0.3'
Nguyen Anh Quynh84fc7402015-01-30 10:52:17 +080027SYSTEM = sys.platform
Michael Cohen18ee47b2014-11-15 22:15:12 +010028
neuromancer6fd80442015-06-02 14:31:36 +020029if "--user" in sys.argv:
30 SITE_PACKAGES = os.path.join(getusersitepackages(), "capstone")
31else:
32 SITE_PACKAGES = os.path.join(get_python_lib(), "capstone")
33
Nguyen Tan Congac0cb812015-01-28 12:33:06 +070034
35SETUP_DATA_FILES = []
36
Nguyen Anh Quynh0d55bde2015-02-03 09:05:57 +080037# adapted from commit e504b81 of Nguyen Tan Cong
38# Reference: https://docs.python.org/2/library/platform.html#cross-platform
39is_64bits = sys.maxsize > 2**32
40
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080041def copy_sources():
42 """Copy the C sources into the source directory.
43 This rearranges the source files under the python distribution
44 directory.
45 """
Nguyen Anh Quynhbcf684d2015-01-30 11:16:48 +080046 src = []
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080047
48 try:
49 dir_util.remove_tree("src/")
50 except (IOError, OSError):
51 pass
52
53 dir_util.copy_tree("../../arch", "src/arch/")
54 dir_util.copy_tree("../../include", "src/include/")
Nguyen Tan Congbc3ffd42015-02-02 22:32:46 +070055 dir_util.copy_tree("../../msvc/headers", "src/msvc/headers")
Nguyen Tan Congca8298c2015-02-02 22:16:47 +070056
Nguyen Anh Quynhbcf684d2015-01-30 11:16:48 +080057 src.extend(glob.glob("../../*.[ch]"))
58 src.extend(glob.glob("../../*.mk"))
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080059
Nguyen Anh Quynhbcf684d2015-01-30 11:16:48 +080060 src.extend(glob.glob("../../Makefile"))
61 src.extend(glob.glob("../../LICENSE*"))
62 src.extend(glob.glob("../../README"))
63 src.extend(glob.glob("../../*.TXT"))
64 src.extend(glob.glob("../../RELEASE_NOTES"))
65 src.extend(glob.glob("../../make.sh"))
Nguyen Anh Quynh55e27c12015-01-30 11:54:44 +080066 src.extend(glob.glob("../../CMakeLists.txt"))
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080067
Nguyen Anh Quynhbcf684d2015-01-30 11:16:48 +080068 for filename in src:
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080069 outpath = os.path.join("./src/", os.path.basename(filename))
70 log.info("%s -> %s" % (filename, outpath))
71 shutil.copy(filename, outpath)
72
Michael Cohen18ee47b2014-11-15 22:15:12 +010073
74class custom_sdist(sdist):
75 """Reshuffle files for distribution."""
76
77 def run(self):
Nguyen Anh Quynh77bd8282015-02-03 09:24:14 +080078 # if prebuilt libraries are existent, then do not copy source
79 if os.path.exists(PATH_LIB64) and os.path.exists(PATH_LIB32):
Nguyen Anh Quynha2b60302015-02-03 11:45:20 +080080 return sdist.run(self)
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +080081 copy_sources()
Michael Cohen18ee47b2014-11-15 22:15:12 +010082 return sdist.run(self)
83
Michael Cohen18ee47b2014-11-15 22:15:12 +010084
85class custom_build_clib(build_clib):
86 """Customized build_clib command."""
87
88 def run(self):
89 log.info('running custom_build_clib')
90 build_clib.run(self)
91
92 def finalize_options(self):
93 # We want build-clib to default to build-lib as defined by the "build"
94 # command. This is so the compiled library will be put in the right
95 # place along side the python code.
96 self.set_undefined_options('build',
97 ('build_lib', 'build_clib'),
98 ('build_temp', 'build_temp'),
99 ('compiler', 'compiler'),
100 ('debug', 'debug'),
101 ('force', 'force'))
102
103 build_clib.finalize_options(self)
104
105 def build_libraries(self, libraries):
Mario Vilas34831d02015-03-17 15:02:29 +0100106 if SYSTEM in ("win32", "cygwin"):
Nguyen Anh Quynh0d55bde2015-02-03 09:05:57 +0800107 # if Windows prebuilt library is available, then include it
108 if is_64bits and os.path.exists(PATH_LIB64):
109 SETUP_DATA_FILES.append(PATH_LIB64)
110 return
111 elif os.path.exists(PATH_LIB32):
112 SETUP_DATA_FILES.append(PATH_LIB32)
113 return
114
115 # build library from source if src/ is existent
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +0800116 if not os.path.exists('src'):
Nguyen Tan Cong60dcfa02015-01-29 17:06:14 +0700117 return
118
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800119 try:
120 for (lib_name, build_info) in libraries:
121 log.info("building '%s' library", lib_name)
Michael Cohen18ee47b2014-11-15 22:15:12 +0100122
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800123 os.chdir("src")
Michael Cohenf051e8f2015-01-26 18:03:14 +0100124
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800125 # platform description refers at https://docs.python.org/2/library/sys.html#sys.platform
Nguyen Anh Quynh3036ada2015-03-19 16:57:22 +0800126 if SYSTEM == "win32":
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800127 # Windows build: this process requires few things:
128 # - CMake + MSVC installed
129 # - Run this command in an environment setup for MSVC
130 os.mkdir("build")
131 os.chdir("build")
132 # Do not build tests & static library
Nguyen Anh Quynh8aef1432015-02-03 17:45:55 +0800133 os.system('cmake -DCMAKE_BUILD_TYPE=RELEASE -DCAPSTONE_BUILD_TESTS=0 -DCAPSTONE_BUILD_STATIC=0 -G "NMake Makefiles" ..')
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800134 os.system("nmake")
135 os.chdir("..")
Nguyen Anh Quynh3036ada2015-03-19 16:57:22 +0800136 SETUP_DATA_FILES.append("src/build/capstone.dll")
Mario Vilas34831d02015-03-17 15:02:29 +0100137 elif SYSTEM == "cygwin":
Nguyen Tan Cong569f7a52015-03-19 00:08:59 +0700138 os.chmod("make.sh", stat.S_IREAD|stat.S_IEXEC)
139 if is_64bits:
140 os.system("CAPSTONE_BUILD_CORE_ONLY=yes ./make.sh cygwin-mingw64")
141 else:
142 os.system("CAPSTONE_BUILD_CORE_ONLY=yes ./make.sh cygwin-mingw32")
Mario Vilas34831d02015-03-17 15:02:29 +0100143 SETUP_DATA_FILES.append("src/capstone.dll")
Nguyen Anh Quynh3036ada2015-03-19 16:57:22 +0800144 else: # Unix
145 os.chmod("make.sh", stat.S_IREAD|stat.S_IEXEC)
146 os.system("CAPSTONE_BUILD_CORE_ONLY=yes ./make.sh")
147 if SYSTEM == "darwin":
148 SETUP_DATA_FILES.append("src/libcapstone.dylib")
149 else: # Non-OSX
150 SETUP_DATA_FILES.append("src/libcapstone.so")
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800151
Nguyen Anh Quynh55e27c12015-01-30 11:54:44 +0800152 os.chdir("..")
Nguyen Anh Quynh1798ada2015-01-30 13:25:16 +0800153 except:
154 pass
Michael Cohen18ee47b2014-11-15 22:15:12 +0100155
Nguyen Anh Quynhf7f15a82014-01-09 08:14:42 +0800156
Nguyen Anh Quynhce10b012015-01-30 10:21:09 +0800157def dummy_src():
158 return []
159
160
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +0800161setup(
Michael Cohen18ee47b2014-11-15 22:15:12 +0100162 provides=['capstone'],
163 packages=['capstone'],
Nguyen Anh Quynha2b60302015-02-03 11:45:20 +0800164 name=PKG_NAME,
Michael Cohen18ee47b2014-11-15 22:15:12 +0100165 version=VERSION,
166 author='Nguyen Anh Quynh',
167 author_email='aquynh@gmail.com',
168 description='Capstone disassembly engine',
169 url='http://www.capstone-engine.org',
170 classifiers=[
171 'License :: OSI Approved :: BSD License',
172 'Programming Language :: Python :: 2',
Nguyen Anh Quynh84fc7402015-01-30 10:52:17 +0800173 'Programming Language :: Python :: 3',
Michael Cohen18ee47b2014-11-15 22:15:12 +0100174 ],
175 requires=['ctypes'],
176 cmdclass=dict(
177 build_clib=custom_build_clib,
178 sdist=custom_sdist,
179 ),
180
181 libraries=[(
182 'capstone', dict(
183 package='capstone',
Nguyen Tan Cong33f75a22015-02-02 12:08:45 +0700184 sources=dummy_src()
Michael Cohen18ee47b2014-11-15 22:15:12 +0100185 ),
186 )],
Nguyen Tan Cong3e605a02015-01-28 11:54:02 +0700187
Nguyen Tan Congac0cb812015-01-28 12:33:06 +0700188 data_files=[(SITE_PACKAGES, SETUP_DATA_FILES)],
Nguyen Anh Quynh923d9262013-12-03 13:13:39 +0800189)