Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 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 compliance |
| 16 | |
Bob Badour | c817845 | 2022-01-31 13:05:53 -0800 | [diff] [blame] | 17 | var ( |
| 18 | // AllResolutions is a TraceConditions function that resolves all |
| 19 | // unfiltered license conditions. |
| 20 | AllResolutions = TraceConditions(func(tn *TargetNode) LicenseConditionSet { return tn.licenseConditions }) |
| 21 | ) |
| 22 | |
| 23 | // TraceConditions is a function that returns the conditions to trace for each |
| 24 | // target node `tn`. |
| 25 | type TraceConditions func(tn *TargetNode) LicenseConditionSet |
| 26 | |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 27 | // ResolveBottomUpConditions performs a bottom-up walk of the LicenseGraph |
| 28 | // propagating conditions up the graph as necessary according to the properties |
| 29 | // of each edge and according to each license condition in question. |
| 30 | // |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 31 | // e.g. if a "restricted" condition applies to a binary, it also applies to all |
| 32 | // of the statically-linked libraries and the transitive closure of their static |
| 33 | // dependencies; even if neither they nor the transitive closure of their |
| 34 | // dependencies originate any "restricted" conditions. The bottom-up walk will |
| 35 | // not resolve the library and its transitive closure, but the later top-down |
| 36 | // walk will. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 37 | func ResolveBottomUpConditions(lg *LicenseGraph) { |
Bob Badour | c817845 | 2022-01-31 13:05:53 -0800 | [diff] [blame] | 38 | TraceBottomUpConditions(lg, AllResolutions) |
| 39 | } |
| 40 | |
| 41 | // TraceBottomUpConditions performs a bottom-up walk of the LicenseGraph |
| 42 | // propagating trace conditions from `conditionsFn` up the graph as necessary |
| 43 | // according to the properties of each edge and according to each license |
| 44 | // condition in question. |
| 45 | func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) { |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 46 | |
| 47 | // short-cut if already walked and cached |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 48 | lg.onceBottomUp.Do(func() { |
| 49 | // amap identifes targets previously walked. (guarded by mu) |
| 50 | amap := make(map[*TargetNode]struct{}) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 51 | |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 52 | var walk func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 53 | |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 54 | walk = func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet { |
| 55 | priorWalkResults := func() (LicenseConditionSet, bool) { |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 56 | if _, alreadyWalked := amap[target]; alreadyWalked { |
| 57 | if treatAsAggregate { |
| 58 | return target.resolution, true |
| 59 | } |
| 60 | if !target.pure { |
| 61 | return target.resolution, true |
| 62 | } |
| 63 | // previously walked in a pure aggregate context, |
| 64 | // needs to walk again in non-aggregate context |
| 65 | } else { |
| 66 | target.resolution |= conditionsFn(target) |
| 67 | amap[target] = struct{}{} |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 68 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 69 | target.pure = treatAsAggregate |
| 70 | return target.resolution, false |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 71 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 72 | cs, alreadyWalked := priorWalkResults() |
| 73 | if alreadyWalked { |
| 74 | return cs |
| 75 | } |
| 76 | |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 77 | // add all the conditions from all the dependencies |
| 78 | for _, edge := range target.edges { |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 79 | // walk dependency to get its conditions |
| 80 | dcs := walk(edge.dependency, treatAsAggregate && edge.dependency.IsContainer()) |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 81 | |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 82 | // turn those into the conditions that apply to the target |
| 83 | dcs = depConditionsPropagatingToTarget(lg, edge, dcs, treatAsAggregate) |
| 84 | cs |= dcs |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 85 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 86 | target.resolution |= cs |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 87 | cs = target.resolution |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 88 | |
| 89 | // return conditions up the tree |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 90 | return cs |
| 91 | } |
| 92 | |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 93 | // walk each of the roots |
| 94 | for _, rname := range lg.rootFiles { |
| 95 | rnode := lg.targets[rname] |
| 96 | _ = walk(rnode, rnode.IsContainer()) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 97 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 98 | }) |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // ResolveTopDownCondtions performs a top-down walk of the LicenseGraph |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 102 | // propagating conditions from target to dependency. |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 103 | // |
| 104 | // e.g. For current policy, none of the conditions propagate from target to |
| 105 | // dependency except restricted. For restricted, the policy is to share the |
| 106 | // source of any libraries linked to restricted code and to provide notice. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 107 | func ResolveTopDownConditions(lg *LicenseGraph) { |
Bob Badour | c817845 | 2022-01-31 13:05:53 -0800 | [diff] [blame] | 108 | TraceTopDownConditions(lg, AllResolutions) |
| 109 | } |
| 110 | |
| 111 | // TraceTopDownCondtions performs a top-down walk of the LicenseGraph |
| 112 | // propagating trace conditions returned by `conditionsFn` from target to |
| 113 | // dependency. |
| 114 | func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) { |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 115 | |
| 116 | // short-cut if already walked and cached |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 117 | lg.onceTopDown.Do(func() { |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 118 | // start with the conditions propagated up the graph |
| 119 | TraceBottomUpConditions(lg, conditionsFn) |
| 120 | |
| 121 | // amap contains the set of targets already walked. (guarded by mu) |
| 122 | amap := make(map[*TargetNode]struct{}) |
| 123 | |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 124 | var walk func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool) |
| 125 | |
| 126 | walk = func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool) { |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 127 | continueWalk := func() bool { |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 128 | if _, alreadyWalked := amap[fnode]; alreadyWalked { |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 129 | if cs.IsEmpty() { |
| 130 | return false |
| 131 | } |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 132 | if cs.Difference(fnode.resolution).IsEmpty() { |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 133 | // no new conditions |
| 134 | |
| 135 | // pure aggregates never need walking a 2nd time with same conditions |
| 136 | if treatAsAggregate { |
| 137 | return false |
| 138 | } |
| 139 | // non-aggregates don't need walking as non-aggregate a 2nd time |
| 140 | if !fnode.pure { |
| 141 | return false |
| 142 | } |
| 143 | // previously walked as pure aggregate; need to re-walk as non-aggregate |
| 144 | } |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 145 | } else { |
| 146 | fnode.resolution |= conditionsFn(fnode) |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 147 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 148 | fnode.resolution |= cs |
| 149 | fnode.pure = treatAsAggregate |
| 150 | amap[fnode] = struct{}{} |
| 151 | cs = fnode.resolution |
| 152 | return true |
| 153 | }() |
| 154 | if !continueWalk { |
| 155 | return |
| 156 | } |
| 157 | // for each dependency |
| 158 | for _, edge := range fnode.edges { |
| 159 | // dcs holds the dpendency conditions inherited from the target |
| 160 | dcs := targetConditionsPropagatingToDep(lg, edge, cs, treatAsAggregate, conditionsFn) |
| 161 | dnode := edge.dependency |
| 162 | // add the conditions to the dependency |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 163 | walk(dnode, dcs, treatAsAggregate && dnode.IsContainer()) |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | // walk each of the roots |
| 168 | for _, rname := range lg.rootFiles { |
| 169 | rnode := lg.targets[rname] |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 170 | // add the conditions to the root and its transitive closure |
Bob Badour | f9d23a9 | 2022-12-18 14:19:47 -0800 | [diff] [blame] | 171 | walk(rnode, NewLicenseConditionSet(), rnode.IsContainer()) |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 172 | } |
Bob Badour | 5c12c66 | 2022-10-29 22:45:12 -0700 | [diff] [blame] | 173 | }) |
Bob Badour | b285515 | 2021-12-08 12:52:59 -0800 | [diff] [blame] | 174 | } |