]> Cypherpunks.ru repositories - goredo.git/blob - sources.go
Optimise redo-sources
[goredo.git] / sources.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "os"
22         "path"
23 )
24
25 func sourcesWalker(
26         tgts []string,
27         seen map[string]struct{},
28         seenDeps map[string]struct{},
29         srcs map[string]struct{},
30 ) error {
31         for _, tgt := range tgts {
32                 tgtAbsPath := mustAbs(path.Join(Cwd, tgt))
33                 cwd, f := path.Split(path.Join(Cwd, tgt))
34                 depPath := path.Join(cwd, RedoDir, f+DepSuffix)
35                 if _, ok := seenDeps[depPath]; ok {
36                         continue
37                 }
38                 seenDeps[depPath] = struct{}{}
39                 fdDep, err := os.Open(depPath)
40                 if err != nil {
41                         if errors.Is(err, fs.ErrNotExist) {
42                                 continue
43                         }
44                         return ErrLine(err)
45                 }
46                 depInfo, err := depRead(fdDep)
47                 fdDep.Close()
48                 if err != nil {
49                         return ErrLine(err)
50                 }
51                 for _, m := range depInfo.ifchanges {
52                         depTgt := m["Target"]
53                         depTgtAbsPath := mustAbs(path.Join(cwd, depTgt))
54                         if _, ok := seen[depTgtAbsPath]; ok {
55                                 continue
56                         }
57                         seen[depTgtAbsPath] = struct{}{}
58                         if isSrc(cwd, depTgt) {
59                                 srcs[cwdMustRel(depTgtAbsPath)] = struct{}{}
60                         } else if depTgtAbsPath != tgtAbsPath {
61                                 if err := sourcesWalker(
62                                         []string{cwdMustRel(depTgtAbsPath)},
63                                         seen, seenDeps, srcs,
64                                 ); err != nil {
65                                         return err
66                                 }
67                         }
68                 }
69         }
70         return nil
71 }