Create symbolic links in db for devices with identical fabric

Signed-off-by: Tomasz Michalak <tmichalak@antmicro.com>
diff --git a/Makefile b/Makefile
index acb7f42..2aa25df 100644
--- a/Makefile
+++ b/Makefile
@@ -168,14 +168,18 @@
 	@$(IN_ENV) cd database/$(1); python3 ../../utils/sort_db.py
 	@if [ -e database/Info.md ]; then $(IN_ENV) ./utils/info_md.py --keep; fi
 
-.PHONY: db-prepare-$(1) db-$(1) db-check-$(1) db-format-$(1) db-extras-$(1) db-extras-$(1)-parts db-extra-$(1)-roi db-extras-$(1)-harness
+db-extras-$(1)-links:
+	@source settings/$(1).sh && $(IN_ENV) ./utils/create_db_links.py
 
-db-extras-$(1): db-extras-$(1)-parts db-extras-$(1)-roi db-extras-$(1)-harness
+.PHONY: db-prepare-$(1) db-$(1) db-check-$(1) db-format-$(1) db-extras-$(1) db-extras-$(1)-parts db-extra-$(1)-roi db-extras-$(1)-harness db-extras-$(1)-links
+
+db-extras-$(1): db-extras-$(1)-parts db-extras-$(1)-roi db-extras-$(1)-harness db-extras-$(1)-links
 
 db-$(1)-all: db-$(1) db-extras-$(1)-parts
 	# Build harnesses after database is complete
 	$$(MAKE) db-extras-$(1)-roi
 	$$(MAKE) db-extras-$(1)-harness
+	$$(MAKE) db-extras-$(1)-links
 
 db-check: db-check-$(1)
 db-format: db-format-$(1)
diff --git a/utils/create_db_links.py b/utils/create_db_links.py
new file mode 100755
index 0000000..993b0c0
--- /dev/null
+++ b/utils/create_db_links.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2022  The SymbiFlow Authors.
+#
+# Use of this source code is governed by a ISC-style
+# license that can be found in the LICENSE file or at
+# https://opensource.org/licenses/ISC
+#
+# SPDX-License-Identifier: ISC
+import argparse
+import os
+from prjxray import util
+
+
+def main():
+    """Script for creating symlinks to device files within the db directory
+    in case a device has an identical fabric.
+
+    """
+    parser = argparse.ArgumentParser(
+        description="Creates symlinks for devices with identical fabric.")
+
+    util.db_root_arg(parser)
+    args = parser.parse_args()
+
+    # Create links for all devices listed in the devices.yaml mapping file
+    devices = util.get_devices(args.db_root)
+    for device in devices:
+        dst = os.path.join(args.db_root, device)
+        if os.path.exists(dst):
+            continue
+        fabric = devices[device]['fabric']
+        src = os.path.join(args.db_root, fabric)
+        assert os.path.exists(src), "Fabric db files don't exist"
+        os.symlink(src, dst)
+
+
+if __name__ == '__main__':
+    main()