Merge pull request #143 from lightwave-lab/pymod

Pymod improvements for MacOS
This commit is contained in:
Matthias Köfferlein 2018-07-17 00:08:16 +02:00 committed by GitHub
commit 8d87138cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 214 additions and 176 deletions

5
.gitignore vendored
View File

@ -48,3 +48,8 @@ src/plugins/*
# QTCreator files # QTCreator files
src/klayout.pro.user src/klayout.pro.user
# python setuptools eggs
*.egg-info/
build/
dist/

19
.travis.yml Normal file
View File

@ -0,0 +1,19 @@
matrix:
include:
- os: osx
osx_image: xcode9.3beta
env:
- MATRIX_EVAL=""
before_install:
- eval "${MATRIX_EVAL}"
- brew update
- brew bundle
- env
install:
- python3 setup.py install
script:
- python3 -c 'import klayout.db as db; print(dir(db))'
- python3 -c 'import klayout.rdb as rdb; print(dir(rdb))'
- python3 -c 'import klayout.tl as tl; print(dir(tl))'

2
Brewfile Normal file
View File

@ -0,0 +1,2 @@
tap "homebrew/core"
brew "python3"

364
setup.py
View File

@ -4,21 +4,21 @@
KLayout standalone Python module setup script KLayout standalone Python module setup script
Copyright (C) 2006-2018 Matthias Koefferlein Copyright (C) 2006-2018 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This script provides distutils-based deployment for KLayout's This script provides distutils-based deployment for KLayout's
@ -31,30 +31,30 @@ Build requirements are:
The main challenge is to map KLayout's shared object architecture. The main challenge is to map KLayout's shared object architecture.
The structure consists of the Python extension libraries and a bunch The structure consists of the Python extension libraries and a bunch
of libraries providing the actual implementation part. of libraries providing the actual implementation part.
The Python extension libraries reference the implementation libraries. The Python extension libraries reference the implementation libraries.
The extension libraries are prefixed with an underscore. The extension libraries are prefixed with an underscore.
For example: For example:
tl (Python) -> _tl, _gsi tl (Python) -> _tl, _gsi
There is a specific issue with the building of the extension There is a specific issue with the building of the extension
libraries: distutils only allows building of shared objects libraries: distutils only allows building of shared objects
as extension libraries and does not provide a intrinsic feature as extension libraries and does not provide a intrinsic feature
for linking other libraries into the extension library. for linking other libraries into the extension library.
Hence we need to add the dependencies through "extra_objects". Hence we need to add the dependencies through "extra_objects".
On Linux there is a specific issue: The shared objects produced On Linux there is a specific issue: The shared objects produced
for extension libraries are not matching the Linux name conventions. for extension libraries are not matching the Linux name conventions.
So we have to add them as .so link objects including the path. So we have to add them as .so link objects including the path.
If we do so, the runtime loading will look them up by their path If we do so, the runtime loading will look them up by their path
and won't find them. So we need to take away the path with and won't find them. So we need to take away the path with
"-Wl,-soname" on Linux (see Config.link_args). "-Wl,-soname" on Linux (see Config.link_args).
""" """
from distutils.core import setup, Extension, Distribution from setuptools import setup, Extension, Distribution
import glob import glob
import os import os
import platform import platform
@ -62,91 +62,93 @@ import sysconfig
# ---------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------
class Config(object): class Config(object):
""" """
Provides some configuration-specific methods Provides some configuration-specific methods
""" """
def __init__(self): def __init__(self):
# TODO: is this really how we get the build paths? # TODO: is this really how we get the build paths?
build_cmd = Distribution().get_command_obj('build') build_cmd = Distribution().get_command_obj('build')
build_cmd.finalize_options() build_cmd.finalize_options()
self.build_platlib = build_cmd.build_platlib self.build_platlib = build_cmd.build_platlib
install_cmd = Distribution().get_command_obj('install') install_cmd = Distribution().get_command_obj('install')
install_cmd.finalize_options() install_cmd.finalize_options()
self.install_platlib = install_cmd.install_platlib self.install_platlib = install_cmd.install_platlib
self.ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") self.ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
self.root = "klayout" self.root = "klayout"
def libname_of(self, mod): def libname_of(self, mod):
""" """
Returns the library name for a given module Returns the library name for a given module
The library name is usually decorated (i.e. "tl" -> "tl.cpython-35m-x86_64-linux-gnu.so"). The library name is usually decorated (i.e. "tl" -> "tl.cpython-35m-x86_64-linux-gnu.so").
""" """
return mod + self.ext_suffix return mod + self.ext_suffix
def path_of(self, mod): def path_of(self, mod):
""" """
Returns the build path of the library for a given module Returns the build path of the library for a given module
""" """
return os.path.join(self.build_platlib, self.root, self.libname_of(mod)) return os.path.join(self.build_platlib, self.root, self.libname_of(mod))
def rpath(self): def rpath(self):
""" """
Returns the runtime_library_dir to use when linking the modules Returns the runtime_library_dir to use when linking the modules
This path will ensure the auxiliary modules are found at runtime. This path will ensure the auxiliary modules are found at runtime.
""" """
return [ os.path.join(self.install_platlib, self.root) ] return [os.path.join(self.install_platlib, self.root)]
def compile_args(self, mod): def compile_args(self, mod):
""" """
Gets additional compiler arguments Gets additional compiler arguments
""" """
if platform.system() == "Windows": if platform.system() == "Windows":
return [ ] return []
elif platform.system() == "Darwin": elif platform.system() == "Darwin":
return [ ] return []
else: else:
# Avoids many "type-punned pointer" warnings # Avoids many "type-punned pointer" warnings
return [ "-Wno-strict-aliasing" ] return ["-Wno-strict-aliasing"]
def link_args(self, mod): def link_args(self, mod):
""" """
Gets additional linker arguments Gets additional linker arguments
""" """
if platform.system() == "Windows": if platform.system() == "Windows":
return [ ] return []
elif platform.system() == "Darwin": elif platform.system() == "Darwin":
# For the dependency modules, make sure we produce a dylib. # For the dependency modules, make sure we produce a dylib.
# We can only link against such, but the bundles produced otherwise. # We can only link against such, but the bundles produced otherwise.
if mod[0:1] == "_": args = []
return [ "-Wl,-dylib" ] if mod[0] == "_":
else: args += ["-Wl,-dylib", '-Wl,-install_name,@rpath/%s' % self.libname_of(mod)]
return [] args += ['-Wl,-rpath,@loader_path/']
else: return args
# this makes the libraries suitable for linking with a path - else:
# i.e. from path_of('_tl'). Without this option, the path # this makes the libraries suitable for linking with a path -
# will be included in the reference and at runtime the loaded # i.e. from path_of('_tl'). Without this option, the path
# will look for the path-qualified library. But that's the # will be included in the reference and at runtime the loaded
# build path and the loader will fail. # will look for the path-qualified library. But that's the
return ['-Wl,-soname,' + self.libname_of(mod)] # build path and the loader will fail.
return ['-Wl,-soname,' + self.libname_of(mod)]
def macros(self): def macros(self):
""" """
Returns the macros to use for building Returns the macros to use for building
""" """
return [ ('HAVE_CURL', 1), ('HAVE_EXPAT', 1) ] return [('HAVE_CURL', 1), ('HAVE_EXPAT', 1)]
def version(self): def version(self):
""" """
Gets the version string Gets the version string
""" """
return "0.26" return "0.26"
config = Config() config = Config()
@ -161,43 +163,43 @@ _tl_sources.remove("src/tl/tl/tlHttpStreamNoQt.cc")
_tl_sources.remove("src/tl/tl/tlFileSystemWatcher.cc") _tl_sources.remove("src/tl/tl/tlFileSystemWatcher.cc")
_tl_sources.remove("src/tl/tl/tlDeferredExecutionQt.cc") _tl_sources.remove("src/tl/tl/tlDeferredExecutionQt.cc")
_tl = Extension(config.root + '._tl', _tl = Extension(config.root + '._tl',
define_macros = config.macros() + [ ('MAKE_TL_LIBRARY', 1) ], define_macros=config.macros() + [('MAKE_TL_LIBRARY', 1)],
language = 'c++', language='c++',
libraries = [ 'curl', 'expat' ], libraries=['curl', 'expat'],
extra_link_args = config.link_args('_tl'), extra_link_args=config.link_args('_tl'),
extra_compile_args = config.compile_args('_tl'), extra_compile_args=config.compile_args('_tl'),
sources = _tl_sources) sources=_tl_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# _gsi dependency library # _gsi dependency library
_gsi_sources = glob.glob("src/gsi/gsi/*.cc") _gsi_sources = glob.glob("src/gsi/gsi/*.cc")
_gsi = Extension(config.root + '._gsi', _gsi = Extension(config.root + '._gsi',
define_macros = config.macros() + [ ('MAKE_GSI_LIBRARY', 1) ], define_macros=config.macros() + [('MAKE_GSI_LIBRARY', 1)],
include_dirs = [ 'src/tl/tl' ], include_dirs=['src/tl/tl'],
extra_objects = [ config.path_of('_tl') ], extra_objects=[config.path_of('_tl')],
runtime_library_dirs = config.rpath(), runtime_library_dirs=config.rpath(),
language = 'c++', language='c++',
extra_link_args = config.link_args('_gsi'), extra_link_args=config.link_args('_gsi'),
extra_compile_args = config.compile_args('_gsi'), extra_compile_args=config.compile_args('_gsi'),
sources = _gsi_sources) sources=_gsi_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# _pya dependency library # _pya dependency library
_pya_sources = glob.glob("src/pya/pya/*.cc") _pya_sources = glob.glob("src/pya/pya/*.cc")
_pya = Extension(config.root + '._pya', _pya = Extension(config.root + '._pya',
define_macros = config.macros() + [ ('MAKE_PYA_LIBRARY', 1) ], define_macros=config.macros() + [('MAKE_PYA_LIBRARY', 1)],
include_dirs = [ 'src/tl/tl', 'src/gsi/gsi' ], include_dirs=['src/tl/tl', 'src/gsi/gsi'],
extra_objects = [ config.path_of('_tl'), config.path_of('_gsi') ], extra_objects=[config.path_of('_tl'), config.path_of('_gsi')],
runtime_library_dirs = config.rpath(), runtime_library_dirs=config.rpath(),
language = 'c++', language='c++',
extra_link_args = config.link_args('_pya'), extra_link_args=config.link_args('_pya'),
extra_compile_args = config.compile_args('_pya'), extra_compile_args=config.compile_args('_pya'),
sources = _pya_sources) sources=_pya_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# _db dependency library # _db dependency library
@ -207,30 +209,31 @@ _db_sources = glob.glob("src/db/db/*.cc")
# Not a real source: # Not a real source:
_db_sources.remove("src/db/db/fonts.cc") _db_sources.remove("src/db/db/fonts.cc")
_db = Extension(config.root + '._db', _db = Extension(config.root + '._db',
define_macros = config.macros() + [ ('MAKE_DB_LIBRARY', 1) ], define_macros=config.macros() + [('MAKE_DB_LIBRARY', 1)],
include_dirs = [ 'src/tl/tl', 'src/gsi/gsi', 'src/db/db' ], include_dirs=['src/tl/tl', 'src/gsi/gsi', 'src/db/db'],
extra_objects = [ config.path_of('_tl'), config.path_of('_gsi') ], extra_objects=[config.path_of('_tl'), config.path_of('_gsi')],
runtime_library_dirs = config.rpath(), runtime_library_dirs=config.rpath(),
language = 'c++', language='c++',
extra_link_args = config.link_args('_db'), extra_link_args=config.link_args('_db'),
extra_compile_args = config.compile_args('_db'), extra_compile_args=config.compile_args('_db'),
sources = _db_sources) sources=_db_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# _rdb dependency library # _rdb dependency library
_rdb_sources = glob.glob("src/rdb/rdb/*.cc") _rdb_sources = glob.glob("src/rdb/rdb/*.cc")
_rdb = Extension(config.root + '._rdb', _rdb = Extension(config.root + '._rdb',
define_macros = config.macros() + [ ('MAKE_RDB_LIBRARY', 1) ], define_macros=config.macros() + [('MAKE_RDB_LIBRARY', 1)],
include_dirs = [ 'src/db/db', 'src/tl/tl', 'src/gsi/gsi' ], include_dirs=['src/db/db', 'src/tl/tl', 'src/gsi/gsi'],
extra_objects = [ config.path_of('_tl'), config.path_of('_gsi'), config.path_of('_db') ], extra_objects=[config.path_of('_tl'), config.path_of(
runtime_library_dirs = config.rpath(), '_gsi'), config.path_of('_db')],
language = 'c++', runtime_library_dirs=config.rpath(),
extra_link_args = config.link_args('_rdb'), language='c++',
extra_compile_args = config.compile_args('_rdb'), extra_link_args=config.link_args('_rdb'),
sources = _rdb_sources) extra_compile_args=config.compile_args('_rdb'),
sources=_rdb_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# dependency libraries from db_plugins # dependency libraries from db_plugins
@ -239,67 +242,76 @@ db_plugins = []
for pi in glob.glob("src/plugins/*/db_plugin") + glob.glob("src/plugins/*/*/db_plugin"): for pi in glob.glob("src/plugins/*/db_plugin") + glob.glob("src/plugins/*/*/db_plugin"):
mod_name = "_" + os.path.split(os.path.split(pi)[-2])[-1] + "_dbpi" mod_name = "_" + os.path.split(os.path.split(pi)[-2])[-1] + "_dbpi"
pi_sources = glob.glob(pi + "/*.cc") pi_sources = glob.glob(pi + "/*.cc")
pi_ext = Extension(config.root + '.db_plugins.' + mod_name,
define_macros=config.macros() + [('MAKE_DB_PLUGIN_LIBRARY', 1)],
include_dirs=['src/plugins/common',
'src/db/db', 'src/tl/tl', 'src/gsi/gsi'],
extra_objects=[config.path_of('_tl'), config.path_of(
'_gsi'), config.path_of('_db')],
runtime_library_dirs=config.rpath(),
language='c++',
extra_link_args=config.link_args(mod_name),
extra_compile_args=config.compile_args(mod_name),
sources=pi_sources)
db_plugins.append(pi_ext)
pi_ext = Extension(config.root + '.db_plugins.' + mod_name,
define_macros = config.macros() + [ ('MAKE_DB_PLUGIN_LIBRARY', 1) ],
include_dirs = [ 'src/plugins/common', 'src/db/db', 'src/tl/tl', 'src/gsi/gsi' ],
extra_objects = [ config.path_of('_tl'), config.path_of('_gsi'), config.path_of('_db') ],
runtime_library_dirs = config.rpath(),
language = 'c++',
extra_link_args = config.link_args(mod_name),
extra_compile_args = config.compile_args(mod_name),
sources = pi_sources)
db_plugins.append(pi_ext)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# tl extension library # tl extension library
tl_sources = glob.glob("src/pymod/tl/*.cc") tl_sources = glob.glob("src/pymod/tl/*.cc")
tl = Extension(config.root + '.tl', tl = Extension(config.root + '.tl',
define_macros = config.macros(), define_macros=config.macros(),
include_dirs = [ 'src/tl/tl', 'src/gsi/gsi', 'src/pya/pya' ], include_dirs=['src/tl/tl', 'src/gsi/gsi', 'src/pya/pya'],
extra_objects = [ config.path_of('_tl'), config.path_of('_gsi'), config.path_of('_pya') ], extra_objects=[config.path_of('_tl'), config.path_of(
runtime_library_dirs = config.rpath(), '_gsi'), config.path_of('_pya')],
sources = tl_sources) runtime_library_dirs=config.rpath(),
extra_link_args=config.link_args('tl'),
sources=tl_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# db extension library # db extension library
db_sources = glob.glob("src/pymod/db/*.cc") db_sources = glob.glob("src/pymod/db/*.cc")
db = Extension(config.root + '.db', db = Extension(config.root + '.db',
define_macros = config.macros(), define_macros=config.macros(),
include_dirs = [ 'src/db/db', 'src/tl/tl', 'src/gsi/gsi', 'src/pya/pya' ], include_dirs=['src/db/db', 'src/tl/tl', 'src/gsi/gsi', 'src/pya/pya'],
extra_objects = [ config.path_of('_db'), config.path_of('_tl'), config.path_of('_gsi'), config.path_of('_pya') ], extra_objects=[config.path_of('_db'), config.path_of(
runtime_library_dirs = config.rpath(), '_tl'), config.path_of('_gsi'), config.path_of('_pya')],
sources = db_sources) runtime_library_dirs=config.rpath(),
extra_link_args=config.link_args('db'),
sources=db_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# rdb extension library # rdb extension library
rdb_sources = glob.glob("src/pymod/rdb/*.cc") rdb_sources = glob.glob("src/pymod/rdb/*.cc")
rdb = Extension(config.root + '.rdb', rdb = Extension(config.root + '.rdb',
define_macros = config.macros(), define_macros=config.macros(),
include_dirs = [ 'src/rdb/rdb', 'src/db/db', 'src/tl/tl', 'src/gsi/gsi', 'src/pya/pya' ], include_dirs=['src/rdb/rdb', 'src/db/db',
extra_objects = [ config.path_of('_rdb'), config.path_of('_gsi'), config.path_of('_pya') ], 'src/tl/tl', 'src/gsi/gsi', 'src/pya/pya'],
runtime_library_dirs = config.rpath(), extra_objects=[config.path_of('_rdb'), config.path_of(
sources = rdb_sources) '_gsi'), config.path_of('_pya')],
runtime_library_dirs=config.rpath(),
extra_link_args=config.link_args('rdb'),
sources=rdb_sources)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# Core setup function # Core setup function
setup(name = config.root, if __name__ == '__main__':
version = config.version(), setup(name=config.root,
description = 'KLayout standalone Python package', version=config.version(),
author = 'Matthias Koefferlein', description='KLayout standalone Python package',
author_email = 'matthias@klayout.de', author='Matthias Koefferlein',
packages = [ config.root ], author_email='matthias@klayout.de',
package_dir = { config.root: 'src/pymod/distutils_src' }, packages=[config.root],
ext_modules = [ _tl, _gsi, _pya, _db, _rdb ] + db_plugins + [ tl, db, rdb ]) package_dir={config.root: 'src/pymod/distutils_src'},
ext_modules=[_tl, _gsi, _pya, _db, _rdb] + db_plugins + [tl, db, rdb])