From 598d678f59bf9f138a68b3f10eab5e34ac90b46c Mon Sep 17 00:00:00 2001 From: Franck Jullien Date: Thu, 27 Jan 2022 16:18:07 +0100 Subject: [PATCH] Add an argument to add_source for files copy With this argument, added files will be copied to the gateware directory and referenced in the project from this new location. --- litex/build/generic_platform.py | 22 +++++++++++++++++----- litex/soc/integration/builder.py | 13 ++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/litex/build/generic_platform.py b/litex/build/generic_platform.py index 4e9101b51..786ba8366 100644 --- a/litex/build/generic_platform.py +++ b/litex/build/generic_platform.py @@ -9,6 +9,7 @@ import sys import os import re +import shutil from migen.fhdl.structure import Signal, Cat from migen.genlib.record import Record @@ -312,6 +313,7 @@ class GenericPlatform: self.sources = [] self.verilog_include_paths = [] self.output_dir = None + self.gateware_dir = None self.finalized = False self.use_default_clk = False @@ -367,20 +369,30 @@ class GenericPlatform: except ConstraintError: pass - def add_source(self, filename, language=None, library=None): + def add_source(self, filename, language=None, library=None, copy=False): filename = os.path.abspath(filename) if language is None: language = tools.language_by_filename(filename) if library is None: library = "work" - for f, _, _ in self.sources: + for f, *r in self.sources: if f == filename: return - self.sources.append((filename, language, library)) + # If the gateware_dir is already defined, we can copy the file + if self.gateware_dir: + dst = filename + if copy: + dst = os.path.join(self.gateware_dir, os.path.basename(s[0])) + shutil.copyfile(filename, dst) + self.sources.append((dst, language, library)) + # If it's too early and the gateware_dir is not known and if it's + # necessary, the file will be copied by the Builder class + else: + self.sources.append((filename, language, library, copy)) - def add_sources(self, path, *filenames, language=None, library=None): + def add_sources(self, path, *filenames, language=None, library=None, copy=False): for f in filenames: - self.add_source(os.path.join(path, f), language, library) + self.add_source(os.path.join(path, f), language, library, copy) def add_source_dir(self, path, recursive=True, language=None, library=None): dir_files = [] diff --git a/litex/soc/integration/builder.py b/litex/soc/integration/builder.py index acbdc47a5..eab918834 100644 --- a/litex/soc/integration/builder.py +++ b/litex/soc/integration/builder.py @@ -278,7 +278,8 @@ class Builder: def build(self, **kwargs): # Pass Output Directory to Platform. - self.soc.platform.output_dir = self.output_dir + self.soc.platform.output_dir = self.output_dir + self.soc.platform.gateware_dir = self.gateware_dir # Check if BIOS is used and add software package if so. with_bios = self.soc.cpu_type is not None @@ -288,6 +289,16 @@ class Builder: # Create Gateware directory. _create_dir(self.gateware_dir) + # If asked, copy files to the gateware directory and change the source file location. + # Drop the 'copy' element from the tupple. + # 'copy' may or may not be present in the tupple. + for i, (f, language, library, *r) in enumerate(self.soc.platform.sources): + dst = f + if r[0] is True: + dst = os.path.join(self.gateware_dir, os.path.basename(f)) + shutil.copyfile(f, dst) + self.soc.platform.sources[i] = (dst, language, library) + # Create Software directory. # First check if software needs a full re-build and remove software dir if so. if with_bios: