copy_license_metadata.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2022 Google Inc. All rights reserved.
  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. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "strings"
  21. "google.golang.org/protobuf/encoding/prototext"
  22. "google.golang.org/protobuf/proto"
  23. "android/soong/compliance/license_metadata_proto"
  24. "android/soong/response"
  25. )
  26. func newMultiString(flags *flag.FlagSet, name, usage string) *multiString {
  27. var f multiString
  28. flags.Var(&f, name, usage)
  29. return &f
  30. }
  31. type multiString []string
  32. func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
  33. func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
  34. func main() {
  35. var expandedArgs []string
  36. for _, arg := range os.Args[1:] {
  37. if strings.HasPrefix(arg, "@") {
  38. f, err := os.Open(strings.TrimPrefix(arg, "@"))
  39. if err != nil {
  40. fmt.Fprintln(os.Stderr, err.Error())
  41. os.Exit(1)
  42. }
  43. respArgs, err := response.ReadRspFile(f)
  44. f.Close()
  45. if err != nil {
  46. fmt.Fprintln(os.Stderr, err.Error())
  47. os.Exit(1)
  48. }
  49. expandedArgs = append(expandedArgs, respArgs...)
  50. } else {
  51. expandedArgs = append(expandedArgs, arg)
  52. }
  53. }
  54. flags := flag.NewFlagSet("flags", flag.ExitOnError)
  55. installed := flags.String("i", "", "installed target")
  56. sources := newMultiString(flags, "s", "source (input) file")
  57. dep := flags.String("d", "", "license metadata file dependency")
  58. outFile := flags.String("o", "", "output file")
  59. flags.Parse(expandedArgs)
  60. if len(*dep) == 0 || len(*installed) == 0 || len(*sources) == 0 {
  61. flags.Usage()
  62. if len(*dep) == 0 {
  63. fmt.Fprintf(os.Stderr, "source license metadata (-d flag) required\n")
  64. }
  65. if len(*sources) == 0 {
  66. fmt.Fprintf(os.Stderr, "source copy (-s flag required\n")
  67. }
  68. if len(*installed) == 0 {
  69. fmt.Fprintf(os.Stderr, "installed copy (-i flag) required\n")
  70. }
  71. os.Exit(1)
  72. }
  73. src_metadata := license_metadata_proto.LicenseMetadata{}
  74. err := readMetadata(*dep, &src_metadata)
  75. if err != nil {
  76. fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
  77. os.Exit(2)
  78. }
  79. metadata := src_metadata
  80. metadata.Built = nil
  81. metadata.InstallMap = nil
  82. metadata.Installed = []string{*installed}
  83. metadata.Sources = *sources
  84. metadata.Deps = []*license_metadata_proto.AnnotatedDependency{&license_metadata_proto.AnnotatedDependency{
  85. File: proto.String(*dep),
  86. Annotations: []string{"static"},
  87. }}
  88. err = writeMetadata(*outFile, &metadata)
  89. if err != nil {
  90. fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
  91. os.Exit(2)
  92. }
  93. }
  94. func readMetadata(file string, metadata *license_metadata_proto.LicenseMetadata) error {
  95. if file == "" {
  96. return fmt.Errorf("source metadata file (-d) required")
  97. }
  98. buf, err := ioutil.ReadFile(file)
  99. if err != nil {
  100. return fmt.Errorf("error reading textproto %q: %w", file, err)
  101. }
  102. err = prototext.Unmarshal(buf, metadata)
  103. if err != nil {
  104. return fmt.Errorf("error unmarshalling textproto: %w", err)
  105. }
  106. return nil
  107. }
  108. func writeMetadata(file string, metadata *license_metadata_proto.LicenseMetadata) error {
  109. buf, err := prototext.MarshalOptions{Multiline: true}.Marshal(metadata)
  110. if err != nil {
  111. return fmt.Errorf("error marshalling textproto: %w", err)
  112. }
  113. if file != "" {
  114. err = ioutil.WriteFile(file, buf, 0666)
  115. if err != nil {
  116. return fmt.Errorf("error writing textproto %q: %w", file, err)
  117. }
  118. } else {
  119. _, _ = os.Stdout.Write(buf)
  120. }
  121. return nil
  122. }