Merge fixes from branch 'xorn'
[geda-gaf.git] / xorn / src / backend / gnet_makedepend.py
blob09d3dab67f6b898ff0d01e92cb69b62560601bb5
1 # gaf.netlist - gEDA Netlist Extraction and Generation
2 # Copyright (C) 1998-2010 Ales Hvezda
3 # Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
4 # Copyright (C) 2013-2020 Roland Lutz
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Copyright (C) 2011 Dan White <dan@whiteaudio.com>
22 # This backend does not work properly due to the way gnetlist handles
23 # source= attributes.
25 # --------------------------------------------------------------------------
27 # Backend to determine the dependencies of a given schematic.
29 # Output is Makefile lines relevant to the input schematic(s):
31 # foo.sch: subsheetA.sch subsheetB.sch
32 # foo.cir: foo.sch subsheetA.cir subsheetB.cir
34 # See the following for the intended usage:
35 # http://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites
37 # Backend example description
38 # ---------------------------
39 # A two-page schematic (idac-1.sch idac-2.sch) contains two sub-schematic
40 # symbols: inv1-1.sym and idac_8p-1.sym. `inv1-1' is implemented with a
41 # single page indicated by the symbol's attribute `source=inv1-1.sch'. The
42 # second block `idac_8p' is a three-page schematic whose symbol has a
43 # `source=idac_8p-1.sch,idac_8p-2.sch,idac_8p-3.sch' attribute. Additionally,
44 # the top-level schematic pages include two `spice-include-1.sym' components
45 # with `file=' attribute values `dactune.pwl' and `idac.param'.
47 # Such a top-level schematic depends on the following files:
48 # inv1-1.sch - `inv1' sub-schematic
49 # idac_8p-1.sch - `idac_8p' sub-schematic, page 1
50 # idac_8p-2.sch - ... page 2
51 # idac_8p-3.sch - ... page 3
53 # Note: The symbol files inv1-1.sym etc. are also dependencies and not
54 # currently handled.
56 # For top-level SPICE simulation of `idac.cir' with hierarchical subckts, the
57 # additional files are dependencies:
58 # inv1.cir - from `gnetlist -g spice-sdb -o inv1.cir inv1-1.sch'
59 # idac_8p.cir - `gnetlist -g spice-sdb -o idac_8p.cir idac_8p-*.sch'
60 # dactune.pwl - additional SPICE include file
61 # idac.param - ...
63 # Calling this backend as:
64 # gnetlist -g makedepend -o idac.d idac-*.sch
66 # generates the `idac.d' file contents as:
68 # idac-1.sch idac-2.sch: inv1-1.sch idac_8p-1.sch idac_8p-2.sch idac_8p-3.sch
69 # idac.cir: idac-1.sch idac-2.sch inv1.cir idac_8p.cir dactune.pwl idac.param
71 # Makefile snippet for use:
72 # ###
73 # modules=idac
74 # depends=$(addsuffix .d, $(modules))
76 # depend: $(depends)
77 # idac.d: $(wildcard idac-*.sch)
78 # gnetlist -g makedepend -o $@ $^
80 # include $(depends)
81 # ###
82 # --------------------------------------------------------------------------
84 import re, sys
86 # Split a filename into 3 parts: base name, page number, and extension
88 MAKEDEPEND_SCHEME = re.compile('(\w+)-(\d+)\.(\w+)$')
89 # was: '([[:alnum:]_]+)-([[:digit:]]+).([[:alpha:]]+)$'
91 # Return a list of all values found for the given attribute name
92 # over all packages in the input files.
94 def get_all_attr_values(attribute, packages):
95 l = []
96 # collect values from all packages into a list
97 # get all values for a given refdes
98 for package in packages:
99 for value in package.get_all_attributes(attribute):
100 if value is not None:
101 # split individual values
102 # (gschem wants a single comma-sep list for source=)
103 l += value.split(',')
104 # ignore non-existent values
105 return l
107 def run(f, netlist):
108 input_files = [sheet.blueprint.filename
109 for sheet in netlist.toplevel_sheets]
111 # lazy version, use first filename only for naming scheme
112 match = MAKEDEPEND_SCHEME.match(input_files[0])
113 if match is None:
114 sys.stderr.write("ERROR: Schematic file name must take the form: "
115 "BASE-PAGENUM.EXT\n")
116 netlist.failed = True
117 return
118 base = match.group(1)
119 # page = match.group(2)
120 # ext = match.group(3)
122 sources = get_all_attr_values('source', reversed(netlist.packages))
123 files = get_all_attr_values('file', reversed(netlist.packages))
125 # schematic deps
126 f.write('%s: %s\n' % (' '.join(input_files), ' '.join(sources)))
128 # netlist deps
129 f.write('%s.cir: %s %s\n' % (base, ' '.join(input_files), ' '.join(files)))