github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tools/depsgen/mkfile.go (about)

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