Skip to content

Commit 737ba52

Browse files
committed
Python Package
1 parent 01a6100 commit 737ba52

File tree

9 files changed

+143
-16
lines changed

9 files changed

+143
-16
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ libparse.cc
66

77
# swig
88
libparse.py
9-
libparse_wrap.c
10-
libparse_wrap.cxx
9+
libparse_wrap.*
1110

1211
# python
1312
__pycache__
13+
/venv
14+
*.egg-info
1415

1516
# binaries
1617
*.out

Makefile

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11

2+
SRC_DIR = libparse
3+
SWIG_IN = $(SRC_DIR)/libparse.i
4+
SWIG_OUT = $(SRC_DIR)/libparse_wrap.cpp $(SRC_DIR)/libparse.py
5+
SOURCES = $(SRC_DIR)/libparse_wrap.cpp $(SRC_DIR)/libparse.cpp
6+
OBJECTS = $(patsubst %.cpp,%.o,$(SOURCES))
7+
HEADERS = $(SRC_DIR)/libparse.h $(SRC_DIR)/py_iostream.h
8+
29
CXXFLAGS += -g -std=c++17 -I$(shell python3 -c "import sysconfig; print(sysconfig.get_path('include'))")
310

411

512
all: _libparse.so
13+
swig_out: $(SWIG_OUT)
14+
15+
_libparse.so: $(OBJECTS)
16+
$(CXX) -shared $^ -o $@
617

7-
_libparse.so: libparse.cc libparse_wrap.cxx libparse.h py_iostream.h
8-
$(CXX) -fPIC -c $(CXXFLAGS) -DFILTERLIB libparse.cc
9-
$(CXX) -fPIC -c $(CXXFLAGS) -DFILTERLIB libparse_wrap.cxx
10-
$(CXX) -shared libparse.o libparse_wrap.o -o $@
18+
$(OBJECTS): %.o : %.cpp $(HEADERS)
19+
$(CXX) -fPIC -o $@ -c $(CXXFLAGS) -DFILTERLIB $<
1120

12-
libparse.py libparse_wrap.cxx: libparse.i libparse.h
13-
swig -c++ -python $<
21+
$(SWIG_OUT): $(SWIG_IN) $(HEADERS)
22+
swig -c++ -o $(SRC_DIR)/libparse_wrap.cpp -oh $(SRC_DIR)/libparse.h -python $<
1423

15-
libparse.h: yosys/passes/techmap/libparse.h libparse.h.patch
24+
$(SRC_DIR)/libparse.h: yosys/passes/techmap/libparse.h $(SRC_DIR)/libparse.h.patch
1625
cp $< $@
17-
patch libparse.h libparse.h.patch
26+
patch $@ $(SRC_DIR)/libparse.h.patch
1827

19-
libparse.cc: yosys/passes/techmap/libparse.cc
28+
$(SRC_DIR)/libparse.cpp: yosys/passes/techmap/libparse.cc $(SRC_DIR)/libparse.cpp.patch
2029
cp $< $@
21-
patch libparse.cc libparse.cc.patch
30+
patch $@ $(SRC_DIR)/libparse.cpp.patch
2231

2332
.PHONY: regen-patches
2433
regen-patches:
25-
diff yosys/passes/techmap/libparse.h libparse.h > libparse.h.patch || true
26-
diff yosys/passes/techmap/libparse.cc libparse.cc > libparse.cc.patch || true
34+
diff yosys/passes/techmap/libparse.h $(SRC_DIR)/libparse.h > $(SRC_DIR)/libparse.h.patch || true
35+
diff yosys/passes/techmap/libparse.cc $(SRC_DIR)/libparse.cpp > $(SRC_DIR)/libparse.cpp.patch || true
2736

2837
.PHONY: clean
2938
clean:
30-
rm -f libparse*.cc libparse*.cxx libparse.h libparse.py
31-
rm -f *.so *.o *.dylib *.dll
39+
rm -f $(SRC_DIR)/*.cpp $(SRC_DIR)/*.o $(SRC_DIR)/libparse.py $(SRC_DIR)/libparse.h
40+
rm -f *.so *.o *.dylib *.dll
41+
rm -rf build/
42+
rm -rf libparse.egg-info/

Readme.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# libparse-python
2+
3+
A SWIG-based Python wrapper around [Yosys](https://github.com/yosyshq/yosys)'s
4+
`libparse`, for all your lib file parsing needs.
5+
6+
# Requirements
7+
* Python 3.2+
8+
* A C++17 compiler
9+
* GNU Make
10+
11+
# License
12+
Apache 2.0. Check 'License'.
13+
14+
## Acknowledgements
15+
`libparse` from [Yosys](https://github.com/yosyshq/yosys) used under the following conditions:
16+
17+
```
18+
Copyright (C) 2012 - 2020 Claire Xenia Wolf <[email protected]>
19+
20+
Permission to use, copy, modify, and/or distribute this software for any
21+
purpose with or without fee is hereby granted, provided that the above
22+
copyright notice and this permission notice appear in all copies.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
25+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
26+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
27+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
29+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31+
```
32+
33+
`stdio_filebuf` from [Android Frameworks](https://android.googlesource.com/platform/frameworks/native/+/refs/heads/main/services/vr/performanced) used under the following conditions:
34+
35+
```
36+
// Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT
37+
// Copyright (c) 2016 Google, Inc.
38+
//
39+
// Permission is hereby granted, free of charge, to any person obtaining a copy
40+
// of this software and associated documentation files (the "Software"), to deal
41+
// in the Software without restriction, including without limitation the rights
42+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
// copies of the Software, and to permit persons to whom the Software is
44+
// furnished to do so, subject to the following conditions:
45+
//
46+
// The above copyright notice and this permission notice shall be included in
47+
// all copies or substantial portions of the Software.
48+
//
49+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55+
// THE SOFTWARE.
56+
```
57+
> The `CREDITS.TXT` in question could not be located at press time.
File renamed without changes.
File renamed without changes.

setup.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import subprocess
4+
from setuptools import setup, find_packages, Extension
5+
from setuptools.command.build_py import build_py
6+
7+
module_name = "libparse"
8+
__dir__ = os.path.dirname(__file__)
9+
10+
ext = Extension(
11+
name='_libparse',
12+
swig_opts=['-c++'],
13+
sources=[
14+
'libparse/libparse.cpp',
15+
'libparse/libparse.i',
16+
],
17+
include_dirs=[
18+
'libparse',
19+
],
20+
extra_compile_args=[
21+
'-std=c++17',
22+
"-DFILTERLIB"
23+
]
24+
)
25+
26+
class BuildPy(build_py):
27+
def run(self):
28+
subprocess.check_call([
29+
"make",
30+
"swig_out"
31+
])
32+
self.run_command('build_ext')
33+
super(build_py, self).run()
34+
35+
setup(
36+
name=module_name,
37+
packages=find_packages(),
38+
version="0.1.0",
39+
description="Python wrapper around Yosys' libparse module",
40+
long_description=open("Readme.md").read(),
41+
long_description_content_type="text/markdown",
42+
author="Efabless Corporation and Contributors",
43+
author_email="[email protected]",
44+
install_requires=[],
45+
classifiers=[
46+
"License :: OSI Approved :: Apache Software License",
47+
"Programming Language :: Python :: 3",
48+
"Intended Audience :: Developers",
49+
"Operating System :: POSIX :: Linux",
50+
"Operating System :: MacOS :: MacOS X",
51+
],
52+
python_requires=">3.6",
53+
ext_modules=[ext],
54+
cmdclass={
55+
'build_py': BuildPy,
56+
},
57+
)

shell.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ with pkgs; mkShell {
66
swig
77
clang
88
python3Full
9+
valgrind
910
];
1011
}

0 commit comments

Comments
 (0)