github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/archive/preprocessor.go (about)

     1  // Copyright 2023 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 archive
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor"
    23  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    24  	"github.com/bazelbuild/reclient/internal/pkg/rsp"
    25  )
    26  
    27  // Preprocessor is the preprocessor of archive link actions.
    28  type Preprocessor struct {
    29  	*inputprocessor.BasePreprocessor
    30  }
    31  
    32  const (
    33  	opArchive = iota
    34  	opPrint
    35  	opDelete
    36  )
    37  
    38  // ParseFlags is used to parse the flags in an archive link invocation command.
    39  func (p *Preprocessor) ParseFlags() error {
    40  	if len(p.Options.Cmd) < 1 {
    41  		return fmt.Errorf("insufficient number of arguments in command: %v", p.Options.Cmd)
    42  	}
    43  
    44  	p.Flags = &flags.CommandFlags{
    45  		WorkingDirectory: p.Options.WorkingDir,
    46  		ExecRoot:         p.Options.ExecRoot,
    47  		ExecutablePath:   p.Options.Cmd[0],
    48  	}
    49  
    50  	// Warning: This is a NAIVE implementation. Expect edge cases to fail.
    51  	// For instance:
    52  	//
    53  	// * It doesn't know how to parse flags grouped together (eg -abCfX).
    54  	// * It doesn't know how to extract archives (to do that, it'd need to read them).
    55  	//
    56  	// See reference for `ar` commands: https://linux.die.net/man/1/ar
    57  	nextArgs := p.Options.Cmd[1:]
    58  
    59  	// Indicates that the archive is the input, not the ouput.
    60  	operation := opArchive
    61  	var archive string
    62  	var objects []string
    63  
    64  	for len(nextArgs) > 0 {
    65  		arg := nextArgs[0]
    66  		nextArgs = nextArgs[1:]
    67  
    68  		switch {
    69  		case strings.HasPrefix(arg, "-"):
    70  			p.Flags.Flags = append(p.Flags.Flags, &flags.Flag{Value: arg})
    71  
    72  			switch arg {
    73  			case "-t":
    74  				operation = opPrint
    75  			case "-d":
    76  				operation = opDelete
    77  			}
    78  		case strings.HasSuffix(arg, ".a"):
    79  			archive = arg
    80  		case strings.HasPrefix(arg, "@"):
    81  			arg = arg[1:]
    82  			objects = append(objects, arg)
    83  
    84  			if !filepath.IsAbs(arg) {
    85  				arg = filepath.Join(p.Flags.ExecRoot, p.Flags.WorkingDirectory, arg)
    86  			}
    87  
    88  			rspFlags, err := rsp.Parse(arg)
    89  			if err != nil {
    90  				return err
    91  			}
    92  
    93  			nextArgs = append(nextArgs, rspFlags...)
    94  		default:
    95  			objects = append(objects, arg)
    96  		}
    97  	}
    98  
    99  	// For eg print or delete, there should be no other dependencies
   100  	// besides the archive itself.
   101  	//
   102  	// (maybe printing shallow archives requires the contents of the archives?)
   103  	switch operation {
   104  	case opArchive:
   105  		p.Flags.Dependencies = objects
   106  		p.Flags.OutputFilePaths = []string{archive}
   107  	case opDelete:
   108  		p.Flags.Dependencies = []string{archive}
   109  		p.Flags.OutputFilePaths = []string{archive}
   110  	case opPrint:
   111  		p.Flags.Dependencies = []string{archive}
   112  	}
   113  
   114  	p.FlagsToActionSpec()
   115  	return nil
   116  }