OpenCores
URL https://opencores.org/ocsvn/radiohdl/radiohdl/trunk

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [core/] [generate_ip_libs] - Rev 4

Go to most recent revision | Compare with Previous | Blame | View Log

#!/usr/bin/env python3
###############################################################################
#
# Copyright (C) 2014-2018
# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# $Id$
#
###############################################################################

import sys
import os
from os.path import expandvars, dirname, basename
from subprocess import call, STDOUT
from common_radiohdl import listify, mkdir
from argparse import ArgumentParser
from hdl_configfile import HdlBuildset, HdlTool
from hdl_configtree import HdlLibTree


def run_qmegawiz(buildset, outputdir, hdllib, vhdl_files, options):
    """
    Run qmegawiz for the configuration in the given hdllib.
    The script takes care that the exit code of qmegawiz is returned to the caller.
    """
    extra_options = hdllib.get_value("qmegawiz_extra_options", False)
    if not extra_options:
        extra_options = ""

    error_code = 0
    for vhdl_file in vhdl_files:
        script  = '. ${RADIOHDL_GEAR}/quartus/set_quartus %s\n' % buildset
        script += 'cd %s\n' % outputdir
        script += 'cp %s/%s .\n' % (hdllib.location, vhdl_file)
        script += 'set -o pipefail\n'
        # echo line without 'Info:' to make sure grep it's exit code is 0
        script += '(echo " " ; qmegawiz %s %s %s 2>&1) | grep -iv Info:\n' \
                  % (options, extra_options, vhdl_file)
        script += 'exit_code=$?\n'
        script += 'rm %s\n' % vhdl_file
        script += 'exit $exit_code\n'
        # execute script
        print("compiling {} ... ".format(vhdl_file))
        return_code = call(script, stdout=None, stderr=STDOUT, shell=True)
        # qmegawiz is very sloppy with it's exitcodes. We assume 0 is OK although this not always the case. :-(
        if return_code == 0:
            print("*** Generation (probably) OK\n")
        else:
            print("*** Error during generation, exitcode={}\n".format(return_code))
            error_code |= return_code
    return error_code


def run_qsys(buildset, outputdir, hdllib, vhdl_files, options):
    """
    Run qsys for the configuration in the given hdllib.
    The script takes care that the exit code of qsys is returned to the caller.
    """
    extra_options = hdllib.get_value("qsys-generate_extra_options", False)
    if not extra_options:
        extra_options = ""

    error_code = 0
    for vhdl_file in vhdl_files:
        script  = '. ${RADIOHDL_GEAR}/quartus/set_quartus %s\n' % buildset
        script += 'cd %s\n' % outputdir
        script += 'cp %s/%s .\n' % (hdllib.location, vhdl_file)
        #script += 'cd %s\n' % hdllib.location
        script += 'set -o pipefail\n'
        # echo line without 'Info:' to make sure grep it's exit code is 0
        script += '(echo " " ; qsys-generate %s %s %s --search-path=%s --output-directory=%s  2>&1) | grep -iv Info:\n' \
                  % (vhdl_file, options, extra_options, "outputdir,$", outputdir)
        script += 'exit_code=$?\n'
        script += 'rm %s\n' % vhdl_file
        script += 'exit $exit_code\n'
        # execute script
        print("compiling {} ... , output-dir = {}".format(vhdl_file, outputdir))
        return_code = call(script, stdout=None, stderr=STDOUT, shell=True)
        if return_code == 0:
            print("*** Generation OK\n")
        else:
            print("*** Error during generation, exitcode={}\n".format(return_code))
            error_code |= return_code
    return error_code


def run_quartus_sh(buildset, outputdir, hdllib, tcl_files, options):
    """
    Run quartus_sh for the configuration in the given hdllib.
    The script takes care that the exit code of quartus_sh is returned to the caller.
    """
    extra_options = hdllib.get_value("quartus_sh_extra_options", False)
    if not extra_options:
        extra_options = ""

    error_code = 0
    for tcl_file in tcl_files:
        script  = '. ${RADIOHDL_GEAR}/quartus/set_quartus %s\n' % buildset
        script += 'cd %s/%s\n' % (outputdir, hdllib.quartus_sh_ip_srcdir)
        script += 'set -o pipefail\n'
        # echo line without 'Info:' to make sure grep it's exit code is 0
        script += '(echo " " ; quartus_sh %s %s -t %s 2>&1) | grep -iv Info:\n' \
                  % (options, extra_options, tcl_file)
        script += 'exit_code=$?\n'
        script += 'exit $exit_code\n'
        # execute script
        print("compiling {} ... ".format(tcl_file))
        return_code = call(script, stdout=None, stderr=STDOUT, shell=True)
        if return_code == 0:
            print("*** Generation OK\n")
        else:
            print("*** Error during generation, exitcode={}\n".format(return_code))
            error_code |= return_code
    return error_code


def expand_all_vars(dir_name):
    _dirname = dir_name
    while '$' in _dirname:
        # print(_dirname)
        _dirname = expandvars(_dirname)
    return _dirname


if __name__ == '__main__':
    # setup parser and parse the arguments.
    argparser = ArgumentParser(description='Generate the IP libraries for all technologies of the given buildset')
    argparser.add_argument('buildset', help="Filename like 'hdl_buildset_<buildset>.cfg'")
    args = argparser.parse_args()

    # resolve full name of buildsetfile and force it to be explicit absolute or relative.
    full_buildsetfile_name = os.path.join(os.getenv('RADIOHDL_CONFIG'), 'hdl_buildset_{}.cfg'.format(args.buildset))
    print('full_buildsetfile_name={}'.format(full_buildsetfile_name))
    if full_buildsetfile_name[0] != '/':
        full_buildsetfile_name = "./" + full_buildsetfile_name
    # read the file
    buildset_info = HdlBuildset(full_buildsetfile_name)
    buildset_info.resolve_key_references()

    # read in all hdllib configfiles
    root_dirs = [expand_all_vars(rootdir) for rootdir in buildset_info.lib_root_dirs.replace("\t", " ").split(" ")
                 if rootdir != '']
    lib_tree = HdlLibTree(rootdirs=root_dirs, filename="hdllib.cfg", sections="generate_ip_libs")

    # read in the tool environment settings
    tool_config_file = os.path.join(os.getenv('RADIOHDL_CONFIG'), 'hdl_tool_{}.cfg'.format(buildset_info.synth_tool_name))
    print('tool_config_file={}'.format(tool_config_file))
    tool_info = HdlTool(tool_config_file)
    tool_info.resolve_key_references()
    ip_tools = [tool for tool in tool_info.ip_tools.replace("\t", " ").split(" ")
                if tool != '']

    files_with_errors = []
    for technology in listify(buildset_info.technology_names):
        print()
        print("Generating IP libraries for technology:", technology)
        # for all tools supported by quartus
        for ip_tool in ip_tools:
            tool_options = tool_info['{}_default_options'.format(ip_tool)]
            ip_tool_key = "{}_ip_files".format(ip_tool)
            # for all hdllib.cfg files found
            for ip_lib_name in sorted(lib_tree.configfiles.keys())[::-1]:   # TODO reverse order issue!
                ip_lib_info = lib_tree.configfiles[ip_lib_name]
                # if technology matches and there are files defined for the current tool
                if ip_lib_info.hdl_lib_technology == technology and ip_tool_key in ip_lib_info.content:
                    # we have a match do the compilation
                    print("==> Processing {} with {}".format(ip_lib_info.ID, ip_tool))
                    outputdir = os.path.join(os.getenv('RADIOHDL_BUILD_DIR'), '{}/{}/'.format(args.buildset, ip_tool))
                    mkdir(outputdir)
                    vhdl_files = [name for name in ip_lib_info[ip_tool_key].replace("\t", " ").split(" ")
                                  if name != '']
                    if ip_tool == 'qmegawiz':
                        err_code = run_qmegawiz(args.buildset, outputdir, ip_lib_info, vhdl_files, tool_options)
                    elif ip_tool == 'qsys-generate':
                        err_code = run_qsys(args.buildset, outputdir, ip_lib_info, vhdl_files, tool_options)

                    elif ip_tool == 'quartus_sh':
                        err_code = run_quartus_sh(args.buildset, outputdir, ip_lib_info, vhdl_files, tool_options)
                    else:
                        raise NameError("Hdllib file in %s contains a unknown tool (%s) for generating IP." %
                                        (ip_lib_info.ID, ip_tool))
                    if err_code:
                        files_with_errors.append(ip_lib_info.ID)

    if files_with_errors:
        print("##### The following files had compile errors:")
        print("     ", files_with_errors)
    else:
        print("+++++ No errors during compilation! +++++\n")

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.