Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cba2330e authored by LaMont Jones's avatar LaMont Jones
Browse files

release-config: disallow new duplicate flag declarations

Flags must only be declared in one place in the tree.  This change
prevents new ones from being added while we fix the bad flags already
present.

Bug: 352105274
Test: manual
Merged-In: I8c4bf2b2852685e84177500f28fe8908016082b9
Change-Id: I8c4bf2b2852685e84177500f28fe8908016082b9
parent 92b54c49
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -18,10 +18,21 @@ import (
	rc_proto "android/soong/cmd/release_config/release_config_proto"
)

var (
	// Allowlist: these flags may have duplicate (identical) declarations
	// without generating an error.  This will be removed once all such
	// declarations have been fixed.
	DuplicateDeclarationAllowlist = map[string]bool{}
)

func FlagDeclarationFactory(protoPath string) (fd *rc_proto.FlagDeclaration) {
	fd = &rc_proto.FlagDeclaration{}
	if protoPath != "" {
		LoadMessage(protoPath, fd)
	}
	// If the input didn't specify a value, create one (== UnspecifiedValue).
	if fd.Value == nil {
		fd.Value = &rc_proto.Value{Val: &rc_proto.Value_UnspecifiedValue{false}}
	}
	return fd
}
+16 −10
Original line number Diff line number Diff line
@@ -276,6 +276,20 @@ func (configs *ReleaseConfigs) LoadReleaseConfigMap(path string, ConfigDirIndex
		configs.Aliases[name] = alias.Target
	}
	var err error
	// Temporarily allowlist duplicate flag declaration files to prevent
	// more from entering the tree while we work to clean up the duplicates
	// that already exist.
	dupFlagFile := filepath.Join(dir, "duplicate_allowlist.txt")
	data, err := os.ReadFile(dupFlagFile)
	if err == nil {
		for _, flag := range strings.Split(string(data), "\n") {
			flag = strings.TrimSpace(flag)
			if strings.HasPrefix(flag, "//") || strings.HasPrefix(flag, "#") {
				continue
			}
			DuplicateDeclarationAllowlist[flag] = true
		}
	}
	err = WalkTextprotoFiles(dir, "flag_declarations", func(path string, d fs.DirEntry, err error) error {
		flagDeclaration := FlagDeclarationFactory(path)
		// Container must be specified.
@@ -289,14 +303,6 @@ func (configs *ReleaseConfigs) LoadReleaseConfigMap(path string, ConfigDirIndex
			}
		}

		// TODO: once we have namespaces initialized, we can throw an error here.
		if flagDeclaration.Namespace == nil {
			flagDeclaration.Namespace = proto.String("android_UNKNOWN")
		}
		// If the input didn't specify a value, create one (== UnspecifiedValue).
		if flagDeclaration.Value == nil {
			flagDeclaration.Value = &rc_proto.Value{Val: &rc_proto.Value_UnspecifiedValue{false}}
		}
		m.FlagDeclarations = append(m.FlagDeclarations, *flagDeclaration)
		name := *flagDeclaration.Name
		if name == "RELEASE_ACONFIG_VALUE_SETS" {
@@ -304,8 +310,8 @@ func (configs *ReleaseConfigs) LoadReleaseConfigMap(path string, ConfigDirIndex
		}
		if def, ok := configs.FlagArtifacts[name]; !ok {
			configs.FlagArtifacts[name] = &FlagArtifact{FlagDeclaration: flagDeclaration, DeclarationIndex: ConfigDirIndex}
		} else if !proto.Equal(def.FlagDeclaration, flagDeclaration) {
			return fmt.Errorf("Duplicate definition of %s", *flagDeclaration.Name)
		} else if !proto.Equal(def.FlagDeclaration, flagDeclaration) || !DuplicateDeclarationAllowlist[name] {
			return fmt.Errorf("Duplicate definition of %s in %s", *flagDeclaration.Name, path)
		}
		// Set the initial value in the flag artifact.
		configs.FilesUsedMap[path] = true