Skip to content

Commit 660b272

Browse files
committed
add test, fix yosys version, match yosys version
1 parent 79c033e commit 660b272

File tree

111 files changed

+4735
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4735
-9
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ jobs:
6363
fetch-depth: 0
6464
submodules: true
6565
persist-credentials: false
66+
- run: |
67+
perl -i -pe 'print "__attribute__((weak)) " if /LibertyParser::error/' yosys/passes/techmap/libparse.cc
6668
- uses: actions/setup-python@v5
6769
- if: ${{ matrix.os.family == 'macos' && matrix.os.archs == 'arm64' }}
6870
name: "[macOS/arm64] Install Python 3.8 (see: https://cibuildwheel.pypa.io/en/stable/faq/#macos-building-cpython-38-wheels-on-arm64)"

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
7676
THE SOFTWARE.
7777
```
7878
> The `CREDITS.TXT` in question could not be located.
79+
80+
Test suite from [liberty2json](https://github.com/silimate/liberty2json) by
81+
Silimate.

default.nix

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
buildPythonPackage,
55
pytest,
66
pybind11,
7+
pytestCheckHook,
8+
perl,
79
}: let
810
version_file = builtins.readFile ./libparse/__version__.py;
911
version_list = builtins.match ''.+''\n__version__ = "([^"]+)"''\n.+''$'' version_file;
@@ -14,17 +16,31 @@ in
1416
inherit version;
1517

1618
inherit src;
19+
20+
postPatch = ''
21+
perl -i -pe 'print "__attribute__((weak)) " if /LibertyParser::error/' yosys/passes/techmap/libparse.cc
22+
'';
23+
24+
nativeBuildInputs = [
25+
perl
26+
];
1727

1828
buildInputs = [
1929
pybind11
2030
];
2131

2232
pythonImportsCheck = [ "libparse" ];
33+
34+
doCheck = true;
35+
36+
nativeCheckInputs = [
37+
pytestCheckHook
38+
];
2339

24-
meta = with lib; {
40+
meta = {
2541
description = "Python wrapper around Yosys's libparse";
26-
license = with licenses; [asl20];
42+
license = with lib.licenses; [asl20];
2743
homepage = "https://github.com/librelane/libparse-python";
28-
platforms = platforms.linux ++ platforms.darwin;
44+
platforms = with lib.platforms; linux ++ darwin;
2945
};
3046
}

libparse/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
from _libparse import *
1+
from _libparse import LibertyAst, LibertyParser
22
from .__version__ import __version__
3+
4+
def __LibertyAst__to_dict(self):
5+
result = {}
6+
result["id"] = self.id
7+
if len(self.args):
8+
result["args"] = self.args
9+
if self.value != "":
10+
result["value"] = self.value
11+
if len(self.children):
12+
result["children"] = [child.to_dict() for child in self.children]
13+
return result
14+
15+
LibertyAst.to_dict = __LibertyAst__to_dict

libparse/__main__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import sys
3+
import json
4+
from argparse import ArgumentParser
5+
6+
from . import LibertyParser, __version__
7+
8+
def main(argv):
9+
p = ArgumentParser("libparse")
10+
p.add_argument("-v", "--version", action='version', version=f'%(prog)s {__version__}')
11+
p.add_argument("file", nargs=1)
12+
ns = p.parse_args(argv)
13+
f = open(ns.file[0], "r")
14+
try:
15+
parsed = LibertyParser(f)
16+
except RuntimeError as e:
17+
print(e, file=sys.stderr)
18+
exit(1)
19+
print(json.dumps(parsed.ast.to_dict()))
20+
21+
if __name__ == "__main__":
22+
main(sys.argv[1:])

libparse/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
19-
__version__ = "1.0.0"
19+
__version__ = "0.56.0"
2020

2121
if __name__ == "__main__":
2222
print(__version__, end="")

libparse/wrapper.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,34 @@
1515
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
// See the License for the specific language governing permissions and
1717
// limitations under the License.
18+
#define private public
19+
#include "libparse.h"
20+
#undef private
1821

1922
#include <pybind11/pybind11.h>
2023
#include <pybind11/stl.h>
2124

22-
#include "libparse.h"
2325
#include "stdio_filebuf.h"
2426

2527
namespace py = pybind11;
2628
using namespace Yosys;
2729

30+
void LibertyParser::error() const
31+
{
32+
std::ostringstream oss;
33+
oss << "Syntax error in liberty file on line " << line << ".\n";
34+
throw std::runtime_error(oss.str());
35+
}
36+
37+
void LibertyParser::error(const std::string &str) const
38+
{
39+
std::stringstream oss;
40+
oss << "Syntax error in liberty file on line " << line << ".\n";
41+
oss << " " << str << "\n";
42+
throw std::runtime_error(oss.str());
43+
}
44+
45+
2846
struct PyIStream : public std::istream {
2947
PyIStream(FILE *f) : std::istream(&buffer_), buffer_(f) {}
3048

@@ -35,7 +53,8 @@ struct PyIStream : public std::istream {
3553
}
3654

3755
auto fd_attr = pyfile.attr("fileno");
38-
auto fd = fd_attr.cast<int>();
56+
auto fd_obj = fd_attr();
57+
auto fd = fd_obj.cast<int>();
3958
if (fd == -1) {
4059
throw std::runtime_error("Failed to get file descriptor");
4160
}
@@ -58,6 +77,7 @@ LibertyParser *from_file(const py::object &pyfile)
5877
return new LibertyParser(*cxx_stream);
5978
}
6079

80+
6181
PYBIND11_MODULE(_libparse, m)
6282
{
6383
m.doc() = "libparse from yosys, native component";

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ requires = [
44
"pybind11>=2.10.0",
55
]
66
build-backend = "setuptools.build_meta"
7+
8+
[tool.pytest.ini_options]
9+
testpaths = ["test"]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
author="Mohamed Gaber",
3232
author_email="me@donn.website",
3333
install_requires=["wheel"],
34-
python_requires=">3.8",
34+
python_requires=">=3.8",
3535
ext_modules=[ext],
3636
cmdclass={"build_ext": build_ext},
3737
)

test/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.diff.json
2+
*.log

0 commit comments

Comments
 (0)