Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 2 | import glob |
| 3 | import os |
| 4 | import platform |
| 5 | import shutil |
Nguyen Tan Cong | 3d6e56a | 2015-01-28 00:35:44 +0700 | [diff] [blame] | 6 | import stat |
Nguyen Tan Cong | 3e605a0 | 2015-01-28 11:54:02 +0700 | [diff] [blame] | 7 | import sys |
Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 8 | |
neuromancer | 6fd8044 | 2015-06-02 14:31:36 +0200 | [diff] [blame^] | 9 | from site import getusersitepackages |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 10 | from distutils import log |
| 11 | from distutils import dir_util |
| 12 | from distutils.command.build_clib import build_clib |
| 13 | from distutils.command.sdist import sdist |
Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 14 | from distutils.core import setup |
Nguyen Tan Cong | 3e605a0 | 2015-01-28 11:54:02 +0700 | [diff] [blame] | 15 | from distutils.sysconfig import get_python_lib |
Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 16 | |
Nguyen Anh Quynh | 0d55bde | 2015-02-03 09:05:57 +0800 | [diff] [blame] | 17 | # prebuilt libraries for Windows - for sdist |
| 18 | PATH_LIB64 = "prebuilt/win64/capstone.dll" |
| 19 | PATH_LIB32 = "prebuilt/win32/capstone.dll" |
Nguyen Tan Cong | 60dcfa0 | 2015-01-29 17:06:14 +0700 | [diff] [blame] | 20 | |
Nguyen Anh Quynh | ca10128 | 2015-02-03 22:31:19 +0800 | [diff] [blame] | 21 | # package name can be 'capstone' or 'capstone-windows' |
Nguyen Anh Quynh | a2b6030 | 2015-02-03 11:45:20 +0800 | [diff] [blame] | 22 | PKG_NAME = 'capstone' |
| 23 | if os.path.exists(PATH_LIB64) and os.path.exists(PATH_LIB32): |
Nguyen Anh Quynh | 4892c45 | 2015-02-03 22:30:10 +0800 | [diff] [blame] | 24 | PKG_NAME = 'capstone-windows' |
Nguyen Anh Quynh | a2b6030 | 2015-02-03 11:45:20 +0800 | [diff] [blame] | 25 | |
Nguyen Anh Quynh | 697ee40 | 2015-05-08 17:06:59 +0800 | [diff] [blame] | 26 | VERSION = '3.0.3' |
Nguyen Anh Quynh | 84fc740 | 2015-01-30 10:52:17 +0800 | [diff] [blame] | 27 | SYSTEM = sys.platform |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 28 | |
neuromancer | 6fd8044 | 2015-06-02 14:31:36 +0200 | [diff] [blame^] | 29 | if "--user" in sys.argv: |
| 30 | SITE_PACKAGES = os.path.join(getusersitepackages(), "capstone") |
| 31 | else: |
| 32 | SITE_PACKAGES = os.path.join(get_python_lib(), "capstone") |
| 33 | |
Nguyen Tan Cong | ac0cb81 | 2015-01-28 12:33:06 +0700 | [diff] [blame] | 34 | |
| 35 | SETUP_DATA_FILES = [] |
| 36 | |
Nguyen Anh Quynh | 0d55bde | 2015-02-03 09:05:57 +0800 | [diff] [blame] | 37 | # adapted from commit e504b81 of Nguyen Tan Cong |
| 38 | # Reference: https://docs.python.org/2/library/platform.html#cross-platform |
| 39 | is_64bits = sys.maxsize > 2**32 |
| 40 | |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 41 | def 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 Quynh | bcf684d | 2015-01-30 11:16:48 +0800 | [diff] [blame] | 46 | src = [] |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 47 | |
| 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 Cong | bc3ffd4 | 2015-02-02 22:32:46 +0700 | [diff] [blame] | 55 | dir_util.copy_tree("../../msvc/headers", "src/msvc/headers") |
Nguyen Tan Cong | ca8298c | 2015-02-02 22:16:47 +0700 | [diff] [blame] | 56 | |
Nguyen Anh Quynh | bcf684d | 2015-01-30 11:16:48 +0800 | [diff] [blame] | 57 | src.extend(glob.glob("../../*.[ch]")) |
| 58 | src.extend(glob.glob("../../*.mk")) |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 59 | |
Nguyen Anh Quynh | bcf684d | 2015-01-30 11:16:48 +0800 | [diff] [blame] | 60 | 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 Quynh | 55e27c1 | 2015-01-30 11:54:44 +0800 | [diff] [blame] | 66 | src.extend(glob.glob("../../CMakeLists.txt")) |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 67 | |
Nguyen Anh Quynh | bcf684d | 2015-01-30 11:16:48 +0800 | [diff] [blame] | 68 | for filename in src: |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 69 | outpath = os.path.join("./src/", os.path.basename(filename)) |
| 70 | log.info("%s -> %s" % (filename, outpath)) |
| 71 | shutil.copy(filename, outpath) |
| 72 | |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 73 | |
| 74 | class custom_sdist(sdist): |
| 75 | """Reshuffle files for distribution.""" |
| 76 | |
| 77 | def run(self): |
Nguyen Anh Quynh | 77bd828 | 2015-02-03 09:24:14 +0800 | [diff] [blame] | 78 | # 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 Quynh | a2b6030 | 2015-02-03 11:45:20 +0800 | [diff] [blame] | 80 | return sdist.run(self) |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 81 | copy_sources() |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 82 | return sdist.run(self) |
| 83 | |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 84 | |
| 85 | class 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 Vilas | 34831d0 | 2015-03-17 15:02:29 +0100 | [diff] [blame] | 106 | if SYSTEM in ("win32", "cygwin"): |
Nguyen Anh Quynh | 0d55bde | 2015-02-03 09:05:57 +0800 | [diff] [blame] | 107 | # 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 Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 116 | if not os.path.exists('src'): |
Nguyen Tan Cong | 60dcfa0 | 2015-01-29 17:06:14 +0700 | [diff] [blame] | 117 | return |
| 118 | |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 119 | try: |
| 120 | for (lib_name, build_info) in libraries: |
| 121 | log.info("building '%s' library", lib_name) |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 122 | |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 123 | os.chdir("src") |
Michael Cohen | f051e8f | 2015-01-26 18:03:14 +0100 | [diff] [blame] | 124 | |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 125 | # platform description refers at https://docs.python.org/2/library/sys.html#sys.platform |
Nguyen Anh Quynh | 3036ada | 2015-03-19 16:57:22 +0800 | [diff] [blame] | 126 | if SYSTEM == "win32": |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 127 | # 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 Quynh | 8aef143 | 2015-02-03 17:45:55 +0800 | [diff] [blame] | 133 | os.system('cmake -DCMAKE_BUILD_TYPE=RELEASE -DCAPSTONE_BUILD_TESTS=0 -DCAPSTONE_BUILD_STATIC=0 -G "NMake Makefiles" ..') |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 134 | os.system("nmake") |
| 135 | os.chdir("..") |
Nguyen Anh Quynh | 3036ada | 2015-03-19 16:57:22 +0800 | [diff] [blame] | 136 | SETUP_DATA_FILES.append("src/build/capstone.dll") |
Mario Vilas | 34831d0 | 2015-03-17 15:02:29 +0100 | [diff] [blame] | 137 | elif SYSTEM == "cygwin": |
Nguyen Tan Cong | 569f7a5 | 2015-03-19 00:08:59 +0700 | [diff] [blame] | 138 | 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 Vilas | 34831d0 | 2015-03-17 15:02:29 +0100 | [diff] [blame] | 143 | SETUP_DATA_FILES.append("src/capstone.dll") |
Nguyen Anh Quynh | 3036ada | 2015-03-19 16:57:22 +0800 | [diff] [blame] | 144 | 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 Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 151 | |
Nguyen Anh Quynh | 55e27c1 | 2015-01-30 11:54:44 +0800 | [diff] [blame] | 152 | os.chdir("..") |
Nguyen Anh Quynh | 1798ada | 2015-01-30 13:25:16 +0800 | [diff] [blame] | 153 | except: |
| 154 | pass |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 155 | |
Nguyen Anh Quynh | f7f15a8 | 2014-01-09 08:14:42 +0800 | [diff] [blame] | 156 | |
Nguyen Anh Quynh | ce10b01 | 2015-01-30 10:21:09 +0800 | [diff] [blame] | 157 | def dummy_src(): |
| 158 | return [] |
| 159 | |
| 160 | |
Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 161 | setup( |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 162 | provides=['capstone'], |
| 163 | packages=['capstone'], |
Nguyen Anh Quynh | a2b6030 | 2015-02-03 11:45:20 +0800 | [diff] [blame] | 164 | name=PKG_NAME, |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 165 | 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 Quynh | 84fc740 | 2015-01-30 10:52:17 +0800 | [diff] [blame] | 173 | 'Programming Language :: Python :: 3', |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 174 | ], |
| 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 Cong | 33f75a2 | 2015-02-02 12:08:45 +0700 | [diff] [blame] | 184 | sources=dummy_src() |
Michael Cohen | 18ee47b | 2014-11-15 22:15:12 +0100 | [diff] [blame] | 185 | ), |
| 186 | )], |
Nguyen Tan Cong | 3e605a0 | 2015-01-28 11:54:02 +0700 | [diff] [blame] | 187 | |
Nguyen Tan Cong | ac0cb81 | 2015-01-28 12:33:06 +0700 | [diff] [blame] | 188 | data_files=[(SITE_PACKAGES, SETUP_DATA_FILES)], |
Nguyen Anh Quynh | 923d926 | 2013-12-03 13:13:39 +0800 | [diff] [blame] | 189 | ) |