python: fix setup.py, so we only copy the core with sdist, thus normal build/compile process does not build the core

This commit is contained in:
Nguyen Anh Quynh 2015-01-30 10:21:09 +08:00
parent 19af3ec9e0
commit ce10b01ad5
2 changed files with 45 additions and 64 deletions

View File

@ -11,11 +11,11 @@ gen_const:
install:
rm -rf $(OBJDIR)
python setup.py build -b $(OBJDIR) install --do-not-build-core
python setup.py build -b $(OBJDIR) install
install3:
rm -rf $(OBJDIR)
python3 setup.py build -b $(OBJDIR) install --do-not-build-core
python3 setup.py build -b $(OBJDIR) install
# NOTE: Newer cython can be installed by: sudo pip install --upgrade cython
install_cython:

View File

@ -13,38 +13,18 @@ from distutils.command.sdist import sdist
from distutils.core import setup
from distutils.sysconfig import get_python_lib
import argparse
# platform description refers at https://docs.python.org/2/library/sys.html#sys.platform
VERSION = '3.0'
SYSTEM = platform.system().lower()
FLAG_DONT_BUILD_CORE = "--do-not-build-core"
# parse parameters to detect if FLAG_DONT_BUILD_CORE exists
parser = argparse.ArgumentParser()
parser.add_argument(FLAG_DONT_BUILD_CORE, action="store_true")
args, _ = parser.parse_known_args()
DONT_BUILD_CORE = args.do_not_build_core
# remove FLAG_DONT_BUILD_CORE to prevent it pass to distutils setup parameters
if FLAG_DONT_BUILD_CORE in sys.argv:
sys.argv.remove(FLAG_DONT_BUILD_CORE)
SITE_PACKAGES = os.path.join(get_python_lib(), "capstone")
SETUP_DATA_FILES = []
if not DONT_BUILD_CORE:
if SYSTEM == "darwin":
SETUP_DATA_FILES.append("src/libcapstone.dylib")
elif SYSTEM != "win32":
SETUP_DATA_FILES.append("src/libcapstone.so")
class LazyList(list):
"""A list which re-evaluates each time.
This is used to provide late binding for setup() below.
"""
def __init__(self, callback):
@ -54,51 +34,46 @@ class LazyList(list):
def __iter__(self):
return iter(self.callback())
def get_sources():
#custom_sdist.copy_sources()
return []
def copy_sources():
"""Copy the C sources into the source directory.
This rearranges the source files under the python distribution
directory.
"""
result = []
try:
dir_util.remove_tree("src/")
except (IOError, OSError):
pass
dir_util.copy_tree("../../arch", "src/arch/")
dir_util.copy_tree("../../include", "src/include/")
dir_util.copy_tree("../../msvc/headers", "src/msvc/headers/")
result.extend(glob.glob("../../*.[ch]"))
result.extend(glob.glob("../../*.mk"))
result.extend(glob.glob("../../Makefile"))
result.extend(glob.glob("../../LICENSE*"))
result.extend(glob.glob("../../README"))
result.extend(glob.glob("../../*.TXT"))
result.extend(glob.glob("../../RELEASE_NOTES"))
result.extend(glob.glob("../../make.sh"))
for filename in result:
outpath = os.path.join("./src/", os.path.basename(filename))
log.info("%s -> %s" % (filename, outpath))
shutil.copy(filename, outpath)
class custom_sdist(sdist):
"""Reshuffle files for distribution."""
def run(self):
self.copy_sources()
copy_sources()
return sdist.run(self)
@staticmethod
def copy_sources():
"""Copy the C sources into the source directory.
This rearranges the source files under the python distribution
directory.
"""
result = []
try:
dir_util.remove_tree("src/")
except (IOError, OSError):
pass
dir_util.copy_tree("../../arch", "src/arch/")
dir_util.copy_tree("../../include", "src/include/")
dir_util.copy_tree("../../msvc/headers", "src/msvc/headers/")
result.extend(glob.glob("../../*.[ch]"))
result.extend(glob.glob("../../*.mk"))
result.extend(glob.glob("../../Makefile"))
result.extend(glob.glob("../../LICENSE*"))
result.extend(glob.glob("../../README"))
result.extend(glob.glob("../../*.TXT"))
result.extend(glob.glob("../../RELEASE_NOTES"))
result.extend(glob.glob("../../make.sh"))
for filename in result:
outpath = os.path.join("./src/", os.path.basename(filename))
log.info("%s -> %s" % (filename, outpath))
shutil.copy(filename, outpath)
class custom_build_clib(build_clib):
"""Customized build_clib command."""
@ -121,13 +96,10 @@ class custom_build_clib(build_clib):
build_clib.finalize_options(self)
def build_libraries(self, libraries):
if DONT_BUILD_CORE:
if not os.path.exists('src'):
return
for (lib_name, build_info) in libraries:
sources = self.get_source_files()
sources = list(sources)
log.info("building '%s' library", lib_name)
os.chdir("src")
@ -137,9 +109,18 @@ class custom_build_clib(build_clib):
os.chmod("make.sh", stat.S_IREAD|stat.S_IEXEC)
os.system("CAPSTONE_BUILD_CORE_ONLY=yes ./make.sh")
if SYSTEM == "darwin":
SETUP_DATA_FILES.append("src/libcapstone.dylib")
elif SYSTEM != "win32":
SETUP_DATA_FILES.append("src/libcapstone.so")
os.chdir("..")
def dummy_src():
return []
setup(
provides=['capstone'],
packages=['capstone'],
@ -162,7 +143,7 @@ setup(
libraries=[(
'capstone', dict(
package='capstone',
sources=LazyList(get_sources)
sources=LazyList(dummy_src)
),
)],