Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for Symbolator diagrams generation #245

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ flake8
# rst_include tool as GitHub doesn't support `.. include::` when rendering
# previews.
rst_include
git+git://github.com/SymbiFlow/symbolator.git@master#egg=symbolator

# The Python API for the SkyWater PDK.
-e scripts/python-skywater-pdk
124 changes: 124 additions & 0 deletions scripts/python-skywater-pdk/skywater_pdk/generate_symbols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env python3
glatosinski marked this conversation as resolved.
Show resolved Hide resolved
# -*- coding: utf-8 -*-
#
# Copyright 2020 SkyWater PDK Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0


import sys
import argparse
from pathlib import Path
import errno
import contextlib
import traceback
import subprocess


def main(argv):
parser = argparse.ArgumentParser(prog=argv[0])
parser.add_argument(
'libraries_dir',
help='Path to the libraries directory of skywater-pdk',
type=Path
)
parser.add_argument(
'output_dir',
help='Path to the output directory',
type=Path
)
parser.add_argument(
'--libname',
help='Library name to generate the Symbolator diagrams for',
type=str
)
parser.add_argument(
'--version',
help='Version for which the Symbolator diagrams should be generated',
type=str
)
parser.add_argument(
'--create-dirs',
help='Create directories for output when not present',
action='store_true'
)
parser.add_argument(
glatosinski marked this conversation as resolved.
Show resolved Hide resolved
'--failed-inputs',
help='Path to files for which Symbolator failed to generate diagram',
type=Path
)
parser.add_argument(
'--overwrite-existing',
help='If present, the script will overwrite existing symbol.svg files',
action='store_true'
)

args = parser.parse_args(argv[1:])

libraries_dir = args.libraries_dir

symbol_v_files = libraries_dir.rglob('*.symbol.v')

nc = contextlib.nullcontext()

with open(args.failed_inputs, 'w') if args.failed_inputs else nc as err:
for symbol_v_file in symbol_v_files:
if args.libname and args.libname != symbol_v_file.parts[1]:
continue
if args.version and args.version != symbol_v_file.parts[2]:
continue

print(f'===> {str(symbol_v_file)}')
libname = symbol_v_file.parts[1]
out_filename = (args.output_dir /
symbol_v_file.resolve()
.relative_to(libraries_dir.resolve()))
out_filename = out_filename.with_suffix('.svg')
out_dir = out_filename.parent

if not out_dir.exists():
if args.create_dirs:
out_dir.mkdir(parents=True)
else:
print(f'The output directory {str(out_dir)} is missing')
print('Run the script with --create-dirs')
return errno.ENOENT

if out_filename.exists() and not args.overwrite_existing:
print(f'The {out_filename} already exists')
return errno.EEXIST

program = ('symbolator' +
f' --libname {libname} --title -t -o {out_filename}' +
f' --output-as-filename -i {str(symbol_v_file)}' +
' --format svg')
res = subprocess.run(
program.split(' '),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
if res.returncode != 0:
print(
f'Failed to run: {program}',
file=sys.stderr
)
print('STDOUT:\n', file=sys.stderr)
print(res.stdout.decode())
err.write(f'{symbol_v_file}\n')
return 0


if __name__ == '__main__':
sys.exit(main(sys.argv))