OpenRAM/__init__.py

82 lines
2.9 KiB
Python
Raw Normal View History

2022-10-25 21:22:18 +02:00
# See LICENSE for licensing information.
#
2024-01-03 23:32:44 +01:00
# Copyright (c) 2016-2024 Regents of the University of California and The Board
2022-10-25 21:22:18 +02:00
# of Regents for the Oklahoma Agricultural and Mechanical College
# (acting for and on behalf of Oklahoma State University)
# All rights reserved.
#
import os
2023-12-05 19:31:18 +01:00
import sys
2022-10-25 21:22:18 +02:00
# Attempt to add the source code to the PYTHONPATH here before running globals.init_openram()
2022-10-25 21:22:18 +02:00
try:
OPENRAM_HOME = os.path.abspath(os.environ.get("OPENRAM_HOME"))
except:
2022-11-22 00:38:49 +01:00
OPENRAM_HOME = os.path.dirname(os.path.abspath(__file__)) + "/compiler"
2022-10-25 21:22:18 +02:00
if not os.path.isdir(OPENRAM_HOME):
assert False
2022-11-27 22:01:20 +01:00
# Make sure that OPENRAM_HOME is an environment variable just in case
if "OPENRAM_HOME" not in os.environ.keys():
os.environ["OPENRAM_HOME"] = OPENRAM_HOME
# Prepend $OPENRAM_HOME to __path__ so that openram will use those modules
__path__.insert(0, OPENRAM_HOME)
2023-01-21 00:34:45 +01:00
2026-04-30 21:00:56 +02:00
# Nix toolchain root (flake location)
NIX_HOME = os.path.abspath(OPENRAM_HOME + "/..")
if "NIX_HOME" in os.environ.keys():
NIX_HOME = os.environ["NIX_HOME"]
os.environ["NIX_HOME"] = NIX_HOME
2023-01-21 00:34:45 +01:00
2022-11-27 22:01:20 +01:00
# Import everything in globals.py
from .globals import *
# Import classes in the "openram" namespace
from .sram_config import *
from .sram import *
2023-03-08 09:08:43 +01:00
from .rom_config import *
from .rom import *
# Add a meta path finder for custom modules
from importlib.abc import MetaPathFinder
class custom_module_finder(MetaPathFinder):
"""
This class is a 'hook' in Python's import system. If it encounters a module
that can be customized, it checks if there is a custom module specified in
the configuration file. If there is a custom module, it is imported instead
of the default one.
"""
def find_spec(self, fullname, path, target=None):
# Get package and module names
package_name = fullname.split(".")[0]
module_name = fullname.split(".")[-1]
# Skip if the package is not openram
if package_name != "openram":
return None
# Search for the module name in customizable modules
from openram import OPTS
for k, v in OPTS.__dict__.items():
if module_name == v:
2023-04-25 21:50:39 +02:00
break
else:
return None
# Search for the custom module
2023-04-25 21:50:39 +02:00
import sys
# Try to find the module in sys.path
for path in sys.path:
# Skip this path if not directory
if not os.path.isdir(path):
continue
for file in os.listdir(path):
# If there is a script matching the custom module name,
# import it with the default module name
if file == (module_name + ".py"):
from importlib.util import spec_from_file_location
return spec_from_file_location(module_name, "{0}/{1}.py".format(path, module_name))
return None
# Python calls meta path finders and asks them to handle the module import if
# they can
sys.meta_path.insert(0, custom_module_finder())