Example #1
0
File: ui.py Project: jcparrad/vunit
    def add_source_file(self, file_name, library_name, preprocessors=None, include_dirs=None):
        """
        Add source file to library

        :param file_name: The name of the file
        :param library_name: The name of the library to add the file into
        :param include_dirs: A list of include directories
        :returns: The :class:`.SourceFile` which was added

        :example:

        .. code-block:: python

           prj.add_source_file("file.vhd", "lib")

        """
        file_type = file_type_of(file_name)

        if file_type == "verilog":
            include_dirs = include_dirs if include_dirs is not None else []
            add_verilog_include_dir(include_dirs)

        file_name = self._preprocess(library_name, abspath(file_name), preprocessors)
        return SourceFile(self._project.add_source_file(file_name,
                                                        library_name,
                                                        file_type=file_type,
                                                        include_dirs=include_dirs),
                          self._project)
Example #2
0
    def add_source_file(self, file_name, library_name, preprocessors=None, include_dirs=None):
        """
        Add source file to library
        """
        file_type = file_type_of(file_name)

        if file_type == "verilog":
            include_dirs = include_dirs if include_dirs is not None else []
            add_verilog_include_dir(include_dirs)

        file_name = self._preprocess(library_name, abspath(file_name), preprocessors)
        self._project.add_source_file(file_name,
                                      library_name,
                                      file_type=file_type,
                                      include_dirs=include_dirs)
Example #3
0
    def test_add_source_files_has_no_parse(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])

        for no_parse in (True, False):
            for method in range(4):
                (
                    ui,
                    add_source_file,
                ) = self._create_ui_with_mocked_project_add_source_file()
                lib = ui.add_library("lib")

                if method == 0:
                    ui.add_source_files(file_name, "lib", no_parse=no_parse)
                elif method == 1:
                    ui.add_source_file(file_name, "lib", no_parse=no_parse)
                elif method == 2:
                    lib.add_source_files(file_name, no_parse=no_parse)
                elif method == 3:
                    lib.add_source_file(file_name, no_parse=no_parse)

                    add_source_file.assert_called_once_with(
                        str(Path("verilog.v").resolve()),
                        "lib",
                        file_type="verilog",
                        include_dirs=all_include_dirs,
                        defines=None,
                        vhdl_standard=VHDL.STD_2008,
                        no_parse=no_parse,
                    )
Example #4
0
    def test_add_source_files_has_defines(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])
        defines = {"foo": "bar"}

        def check(action):
            """
            Helper to check that project method was called
            """
            ui, add_source_file = self._create_ui_with_mocked_project_add_source_file(
            )
            lib = ui.add_library("lib")
            action(ui, lib)
            add_source_file.assert_called_once_with(
                str(Path("verilog.v").resolve()),
                "lib",
                file_type="verilog",
                include_dirs=all_include_dirs,
                defines=defines,
                vhdl_standard=VHDL.STD_2008,
                no_parse=False,
            )

        check(lambda ui, _: ui.add_source_files(
            file_name, "lib", defines=defines))
        check(lambda ui, _: ui.add_source_file(
            file_name, "lib", defines=defines))
        check(lambda _, lib: lib.add_source_files(file_name, defines=defines))
        check(lambda _, lib: lib.add_source_file(file_name, defines=defines))
Example #5
0
    def test_add_source_files_has_no_parse(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])

        for no_parse in (True, False):
            for method in range(4):

                ui = self._create_ui()
                with mock.patch.object(ui, "_project",
                                       autospec=True) as project:
                    project.has_library.return_value = True

                    if method == 0:
                        ui.add_source_files(file_name,
                                            "lib",
                                            no_parse=no_parse)
                    elif method == 1:
                        ui.add_source_file(file_name, "lib", no_parse=no_parse)
                    elif method == 2:
                        lib = ui.library("lib")
                        lib.add_source_files(file_name, no_parse=no_parse)
                    elif method == 3:
                        lib = ui.library("lib")
                        lib.add_source_file(file_name, no_parse=no_parse)

                    project.add_source_file.assert_called_once_with(
                        abspath("verilog.v"),
                        "lib",
                        file_type="verilog",
                        include_dirs=all_include_dirs,
                        defines=None,
                        vhdl_standard=None,
                        no_parse=no_parse)
Example #6
0
    def test_add_source_files_has_defines(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])
        defines = {"foo": "bar"}

        def check(action):
            """
            Helper to check that project method was called
            """
            ui = self._create_ui()
            with mock.patch.object(ui, "_project", autospec=True) as project:
                project.has_library.return_value = True
                lib = ui.library("lib")
                action(ui, lib)
                project.add_source_file.assert_called_once_with(
                    abspath("verilog.v"),
                    "lib",
                    file_type="verilog",
                    include_dirs=all_include_dirs,
                    defines=defines,
                    vhdl_standard=None,
                    no_parse=False)

        check(lambda ui, _: ui.add_source_files(
            file_name, "lib", defines=defines))
        check(lambda ui, _: ui.add_source_file(
            file_name, "lib", defines=defines))
        check(lambda _, lib: lib.add_source_files(file_name, defines=defines))
        check(lambda _, lib: lib.add_source_file(file_name, defines=defines))
Example #7
0
    def test_add_source_files_has_defines(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])
        defines = {"foo": "bar"}

        def check(action):
            """
            Helper to check that project method was called
            """
            with mock.patch.object(ui, "_project", autospec=True) as project:
                action()
                project.add_source_file.assert_called_once_with(abspath("verilog.v"),
                                                                "lib",
                                                                file_type="verilog",
                                                                include_dirs=all_include_dirs,
                                                                defines=defines,
                                                                vhdl_standard='2008')
        ui = self._create_ui()
        ui.add_library("lib")
        check(lambda: ui.add_source_files(file_name, "lib", defines=defines))

        ui = self._create_ui()
        ui.add_library("lib")
        check(lambda: ui.add_source_file(file_name, "lib", defines=defines))

        ui = self._create_ui()
        lib = ui.add_library("lib")
        check(lambda: lib.add_source_files(file_name, defines=defines))

        ui = self._create_ui()
        lib = ui.add_library("lib")
        check(lambda: lib.add_source_file(file_name, defines=defines))
Example #8
0
    def test_add_source_files_has_include_dirs(self):
        file_name = "verilog.v"
        include_dirs = ["include_dir"]
        all_include_dirs = add_verilog_include_dir(include_dirs)
        self.create_file(file_name)

        def check(action):
            """
            Helper to check that project method was called
            """
            ui = self._create_ui()
            with mock.patch.object(ui, "_project", autospec=True) as project:
                lib = ui.add_library("lib")
                action(ui, lib)
                project.add_source_file.assert_called_once_with(
                    abspath("verilog.v"),
                    "lib",
                    file_type="verilog",
                    include_dirs=all_include_dirs,
                    defines=None,
                    vhdl_standard='2008')

        check(lambda ui, _: ui.add_source_files(
            file_name, "lib", include_dirs=include_dirs))
        check(lambda ui, _: ui.add_source_file(
            file_name, "lib", include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_files(file_name,
                                                  include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_file(file_name,
                                                 include_dirs=include_dirs))
Example #9
0
    def test_add_source_files_has_defines(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])
        defines = {"foo": "bar"}

        def check(action):
            """
            Helper to check that project method was called
            """
            with mock.patch.object(ui, "_project", autospec=True) as project:
                action()
                project.add_source_file.assert_called_once_with(
                    abspath("verilog.v"),
                    "lib",
                    file_type="verilog",
                    include_dirs=all_include_dirs,
                    defines=defines,
                    vhdl_standard='2008')

        ui = self._create_ui()
        ui.add_library("lib")
        check(lambda: ui.add_source_files(file_name, "lib", defines=defines))

        ui = self._create_ui()
        ui.add_library("lib")
        check(lambda: ui.add_source_file(file_name, "lib", defines=defines))

        ui = self._create_ui()
        lib = ui.add_library("lib")
        check(lambda: lib.add_source_files(file_name, defines=defines))

        ui = self._create_ui()
        lib = ui.add_library("lib")
        check(lambda: lib.add_source_file(file_name, defines=defines))
Example #10
0
    def test_add_source_files_has_include_dirs(self):
        file_name = "verilog.v"
        include_dirs = ["include_dir"]
        all_include_dirs = add_verilog_include_dir(include_dirs)
        self.create_file(file_name)

        def check(action):
            """
            Helper to check that project method was called
            """
            with mock.patch.object(ui, "_project", autospec=True) as project:
                action()
                project.add_source_file.assert_called_once_with(abspath("verilog.v"),
                                                                "lib",
                                                                file_type="verilog",
                                                                include_dirs=all_include_dirs,
                                                                defines=None)
        ui = self._create_ui()
        check(lambda: ui.add_source_files(file_name, "lib", include_dirs=include_dirs))

        ui = self._create_ui()
        check(lambda: ui.add_source_file(file_name, "lib", include_dirs=include_dirs))

        ui = self._create_ui()
        lib = ui.library("lib")
        check(lambda: lib.add_source_files(file_name, include_dirs=include_dirs))

        ui = self._create_ui()
        lib = ui.library("lib")
        check(lambda: lib.add_source_file(file_name, include_dirs=include_dirs))
Example #11
0
    def test_add_source_files_has_no_parse(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])

        for no_parse in (True, False):
            for method in range(4):

                ui = self._create_ui()
                with mock.patch.object(ui, "_project", autospec=True) as project:
                    project.has_library.return_value = True

                    if method == 0:
                        ui.add_source_files(file_name, "lib", no_parse=no_parse)
                    elif method == 1:
                        ui.add_source_file(file_name, "lib", no_parse=no_parse)
                    elif method == 2:
                        lib = ui.library("lib")
                        lib.add_source_files(file_name, no_parse=no_parse)
                    elif method == 3:
                        lib = ui.library("lib")
                        lib.add_source_file(file_name, no_parse=no_parse)

                    project.add_source_file.assert_called_once_with(abspath("verilog.v"),
                                                                    "lib",
                                                                    file_type="verilog",
                                                                    include_dirs=all_include_dirs,
                                                                    defines=None,
                                                                    vhdl_standard=None,
                                                                    no_parse=no_parse)
Example #12
0
    def test_add_source_files_has_defines(self):
        file_name = "verilog.v"
        self.create_file(file_name)
        all_include_dirs = add_verilog_include_dir([])
        defines = {"foo": "bar"}

        def check(action):
            """
            Helper to check that project method was called
            """
            ui = self._create_ui()
            with mock.patch.object(ui, "_project", autospec=True) as project:
                project.has_library.return_value = True
                lib = ui.library("lib")
                action(ui, lib)
                project.add_source_file.assert_called_once_with(abspath("verilog.v"),
                                                                "lib",
                                                                file_type="verilog",
                                                                include_dirs=all_include_dirs,
                                                                defines=defines,
                                                                vhdl_standard=None,
                                                                no_parse=False)
        check(lambda ui, _: ui.add_source_files(file_name, "lib", defines=defines))
        check(lambda ui, _: ui.add_source_file(file_name, "lib", defines=defines))
        check(lambda _, lib: lib.add_source_files(file_name, defines=defines))
        check(lambda _, lib: lib.add_source_file(file_name, defines=defines))
Example #13
0
    def test_add_source_files_has_include_dirs(self):
        file_name = "verilog.v"
        include_dirs = ["include_dir"]
        all_include_dirs = add_verilog_include_dir(include_dirs)
        self.create_file(file_name)

        def check(action):
            """
            Helper to check that project method was called
            """
            ui = self._create_ui()
            with mock.patch.object(ui, "_project", autospec=True) as project:
                lib = ui.add_library("lib")
                action(ui, lib)
                project.add_source_file.assert_called_once_with(abspath("verilog.v"),
                                                                "lib",
                                                                file_type="verilog",
                                                                include_dirs=all_include_dirs,
                                                                defines=None,
                                                                vhdl_standard='2008',
                                                                no_parse=False)
        check(lambda ui, _: ui.add_source_files(file_name, "lib", include_dirs=include_dirs))
        check(lambda ui, _: ui.add_source_file(file_name, "lib", include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_files(file_name, include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_file(file_name, include_dirs=include_dirs))
Example #14
0
    def test_add_source_files_has_include_dirs(self):
        file_name = "verilog.v"
        include_dirs = ["include_dir"]
        all_include_dirs = add_verilog_include_dir(include_dirs)
        self.create_file(file_name)

        def check(action):
            """
            Helper to check that project method was called
            """
            ui, add_source_file = self._create_ui_with_mocked_project_add_source_file(
            )
            lib = ui.add_library("lib")
            action(ui, lib)
            add_source_file.assert_called_once_with(
                abspath("verilog.v"),
                "lib",
                file_type="verilog",
                include_dirs=all_include_dirs,
                defines=None,
                vhdl_standard=VHDL.STD_2008,
                no_parse=False,
            )

        check(lambda ui, _: ui.add_source_files(
            file_name, "lib", include_dirs=include_dirs))
        check(lambda ui, _: ui.add_source_file(
            file_name, "lib", include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_files(file_name,
                                                  include_dirs=include_dirs))
        check(lambda _, lib: lib.add_source_file(file_name,
                                                 include_dirs=include_dirs))
Example #15
0
    def add_source_file(self, file_name, library_name, preprocessors=None, include_dirs=None, defines=None):
        """
        Add source file to library

        :param file_name: The name of the file
        :param library_name: The name of the library to add the file into
        :param include_dirs: A list of include directories
        :param defines: A dictionary containing Verilog defines to be set
        :returns: The :class:`.SourceFile` which was added

        :example:

        .. code-block:: python

           prj.add_source_file("file.vhd", "lib")

        """
        file_type = file_type_of(file_name)

        if file_type == "verilog":
            include_dirs = include_dirs if include_dirs is not None else []
            include_dirs = add_verilog_include_dir(include_dirs)

        file_name = self._preprocess(library_name, abspath(file_name), preprocessors)
        return SourceFile(self._project.add_source_file(file_name,
                                                        library_name,
                                                        file_type=file_type,
                                                        include_dirs=include_dirs,
                                                        defines=defines),
                          self._project,
                          self)