This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
mkfile.go
119 lines (95 loc) · 3.78 KB
/
mkfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"strings"
)
const (
// fileDepMkTemplate holds a template for file-based
// dependency tracking make file.
fileDepMkTemplate = `# generated by !!!DEPS_GEN_APP_NAME!!!
define _DEPS_GEN_GET_ALL_FILES_
!!!DEPS_GEN_FILES_GENERATOR!!!
endef
_DEPS_GEN_ALL_FILES_STORED_ := !!!DEPS_GEN_ALL_FILES_STORED!!!
_DEPS_GEN_SPACE_ :=
_DEPS_GEN_SPACE_ +=
_DEPS_GEN_F1_ := $(sort $(strip $(_DEPS_GEN_ALL_FILES_STORED_)))
_DEPS_GEN_F2_ := $(sort $(strip $(call _DEPS_GEN_GET_ALL_FILES_)))
ifeq ($(_DEPS_GEN_F1_),$(_DEPS_GEN_F2_))
!!!DEPS_GEN_TARGET!!!: $(_DEPS_GEN_ALL_FILES_STORED_)
else
$(info Invalidating !!!DEPS_GEN_TARGET!!!)
$(info Prerequisites stored in deps file that are not in currently on disk: $(filter-out $(_DEPS_GEN_F2_),$(_DEPS_GEN_F1_)))
$(info Prerequisites currently on disk that are not stored in deps file: $(filter-out $(_DEPS_GEN_F1_),$(_DEPS_GEN_F2_)))
# invalidate the target
!!!DEPS_GEN_TARGET!!!: _DEPS_GEN_INVALIDATE_
.PHONY: _DEPS_GEN_INVALIDATE_
endif
$(call undefine-namespaces,_DEPS_GEN)
`
// kvStoredPrefix is used to prefix the variable name we want
// to store (so actual variable checked at runtime is FOO and
// its stored value from previous run is $(kvStoredPrefix)FOO.
kvStoredPrefix = "_DEPS_GEN_STORED_"
// kvDepMkTemplate holds a template for key-value-based
// dependency tracking make file.
kvDepMkTemplate = `# generated by !!!DEPS_GEN_APP_NAME!!!
_DEPS_GEN_VARIABLES_ := !!!DEPS_GEN_VARIABLES!!!
!!!DEPS_GEN_STORED_VALUES!!!
_DEPS_GEN_DO_INVALIDATE_ := no
$(foreach v,$(_DEPS_GEN_VARIABLES_), \
$(if $(call equal,$($v),$(!!!DEPS_GEN_STORED_PREFIX!!!$v)), \
, \
$(eval _DEPS_GEN_DO_INVALIDATE_ := yes) \
$(eval _DEPS_GEN_DIFFS_ += $v)))
ifeq ($(_DEPS_GEN_DO_INVALIDATE_),yes)
$(info Invalidating !!!DEPS_GEN_TARGET!!!)
$(foreach k,$(_DEPS_GEN_DIFFS_), \
$(info Differing key: $k) \
$(info Value stored in deps file: $(!!!DEPS_GEN_STORED_PREFIX!!!$k)) \
$(info Value currently used: $($k)))
# invalidate the target
!!!DEPS_GEN_TARGET!!!: _DEPS_GEN_INVALIDATE_
.PHONY: _DEPS_GEN_INVALIDATE_
endif
$(call undefine-namespaces,_DEPS_GEN)
`
)
// GenerateFileDeps returns contents of make file describing
// dependencies of given target on given files and checking if files
// weren't added or removed in directories where given files are.
func GenerateFileDeps(target, filesGenerator string, files []string) string {
return replacePlaceholders(fileDepMkTemplate,
"DEPS_GEN_APP_NAME", appName(),
"DEPS_GEN_FILES_GENERATOR", filesGenerator,
"DEPS_GEN_ALL_FILES_STORED", strings.Join(files, " "),
"DEPS_GEN_TARGET", target,
)
}
func GenerateKvDeps(target string, keysValues map[string]string) string {
variables := make([]string, 0, len(keysValues))
storedValues := make([]string, 0, len(keysValues))
for k, v := range keysValues {
variables = append(variables, k)
storedValues = append(storedValues, kvStoredPrefix+k+" := "+v)
}
return replacePlaceholders(kvDepMkTemplate,
"DEPS_GEN_APP_NAME", appName(),
"DEPS_GEN_VARIABLES", strings.Join(variables, " "),
"DEPS_GEN_STORED_VALUES", strings.Join(storedValues, "\n"),
"DEPS_GEN_STORED_PREFIX", kvStoredPrefix,
"DEPS_GEN_TARGET", target,
)
}