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.
This commit is contained in:
Franck Jullien 2022-01-27 16:18:07 +01:00
parent 5fd7c758a1
commit 598d678f59
2 changed files with 29 additions and 6 deletions

View File

@ -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 = []

View File

@ -279,6 +279,7 @@ class Builder:
def build(self, **kwargs):
# Pass Output Directory to Platform.
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: