github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/cppcompile/flagsparser.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 cppcompile
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/clangparser"
    22  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    23  )
    24  
    25  // ClangParser parses clang command args to produce a CommandFlags object.
    26  type ClangParser struct{}
    27  
    28  // ParseFlags is used to translate the given action command into clang compiler
    29  // options, so that they can be used during input processing.
    30  // Android build, throw error for unsupported flags.
    31  func (cp ClangParser) ParseFlags(ctx context.Context, command []string, workingDir, execRoot string) (*flags.CommandFlags, error) {
    32  	numArgs := len(command)
    33  	if numArgs < 2 {
    34  		return nil, fmt.Errorf("insufficient number of arguments in command: %v", command)
    35  	}
    36  
    37  	res := &flags.CommandFlags{
    38  		ExecutablePath:   command[0],
    39  		TargetFilePaths:  []string{},
    40  		WorkingDirectory: workingDir,
    41  		ExecRoot:         execRoot,
    42  	}
    43  	var state clangparser.State
    44  	s := clangparser.New(command)
    45  	defer state.Finalize(res)
    46  	for s.HasNext() {
    47  		if err := state.HandleClangFlags(s.ReadNextFlag(), res, false); err != nil {
    48  			return res, err
    49  		}
    50  	}
    51  	return res, nil
    52  }