Skip to content

Commit

Permalink
feat: implement new logic branches
Browse files Browse the repository at this point in the history
  • Loading branch information
seppzer0 committed Sep 7, 2024
1 parent 74f4c2a commit 697368c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 54 deletions.
6 changes: 3 additions & 3 deletions agb/command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (bc *BuildCommand) Execute() error {
return err
}

//if err := kernel_builder.Build(); err != nil {
// return err
//}
if err := kernel_builder.Build(); err != nil {
return err
}

return nil
}
22 changes: 17 additions & 5 deletions agb/core/gki.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ func (gb *GkiBuilder) Patch() error {

// Prepare runs all the preparations for the build.
func (gb *GkiBuilder) Prepare() error {
//if err := gb.resourceManager.GetCompiler(); err != nil {
// return err
//}
// for regular GKI sources, separate Clang is not required
if !(gb.LinuxKernelVersion >= 5.10) {
if err := gb.resourceManager.GetCompiler(); err != nil {
return err
}
}

if err := gb.resourceManager.CleanKernelSource(); err != nil {
return err
}

if err := gb.resourceManager.GetSource(gb.AndroidVersion, gb.LinuxKernelVersion, gb.PatchVersion); err != nil {
return err
Expand All @@ -90,8 +97,13 @@ func (gb *GkiBuilder) Prepare() error {

// Build launches GKI kernel build.
func (gb *GkiBuilder) Build() error {
//cmd := "LTO=thin BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh CC=\"clang\""
cmd := "tools/bazel run --disk_cache=~/.cache/bazel --config=fast --config=stamp --lto=thin //common:kernel_aarch64_dist -- --dist_dir=dist"
var cmd string

if gb.AndroidVersion >= 14 {
cmd = "tools/bazel run --disk_cache=~/.cache/bazel --config=fast --config=stamp --lto=thin //common:kernel_aarch64_dist -- --dist_dir=dist"
} else {
cmd = "LTO=thin BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh CC=\"clang\""
}

out, err := tool.RunCmd(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions agb/manager/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (gm *GitManager) Clone(url string, path string, shallow bool) error {

command := fmt.Sprintf("git clone%s %s %s", depth_flag, url, path)

if _, err := tool.RunCmd(command); err != nil {
return cerror.ErrCommandRun{Command: command}
if out, err := tool.RunCmd(command); err != nil {
return cerror.ErrCommandRun{Command: command, Output: out}
}

return nil
Expand Down
24 changes: 13 additions & 11 deletions agb/manager/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ type ResourceManager struct {

// NewResourceManager creates new instance of ResourceManager.
func NewResourceManager(cu string, sloc string) *ResourceManager {
dc := config.NewDirectoryConfig()
gm := NewGitManager()
fm := NewFileManager()

return &ResourceManager{
ClangUrl: cu,
SourceLocation: sloc,
directoryConfig: dc,
gitManager: gm,
fileManager: fm,
directoryConfig: config.NewDirectoryConfig(),
gitManager: NewGitManager(),
fileManager: NewFileManager(),
}
}

Expand Down Expand Up @@ -124,17 +120,23 @@ func (rm *ResourceManager) GetSource(av int, lkv float64, pv string) error {
}

for _, cmd := range cmds {
err := tool.RunCmdWDirVerbose(cmd, rm.directoryConfig.KernelSourcePath)
out, err := tool.RunCmdWDir(cmd, rm.directoryConfig.KernelSourcePath)
if err != nil {
return cerror.ErrCommandRun{Command: cmd, Output: ""}
return cerror.ErrCommandRun{Command: cmd, Output: out}
}
}
} else {
// TODO: implement custom git repository download
return nil
if err := rm.gitManager.Clone(rm.SourceLocation, rm.directoryConfig.KernelSourcePath, true); err != nil {
return err
}
}
}

tool.Mdone()
return nil
}

// CleanKernelSource cleans the directory with kernel sources from potential artifacts.
func (rm *ResourceManager) CleanKernelSource() error {
return rm.gitManager.Reset(rm.directoryConfig.KernelSourcePath)
}
71 changes: 38 additions & 33 deletions agb/tool/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,66 @@ package tool

import (
"bufio"
"bytes"
"fmt"
"os"
"io"
"os/exec"
"strings"
"sync"
)

// RunCmd executes a specified shell command.
func RunCmd(command string) (string, error) {
var stdout bytes.Buffer

args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)

// process stdout and stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = &stdout
cmd.Stderr = &stdout

err := cmd.Run()
out, err := cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout
if err != nil {
return "", err
}

return strings.TrimSpace(stdout.String()), err
}
var wg sync.WaitGroup
wg.Add(1)

// RunCmdWDir executes a specified shell command in a specified working directory.
func RunCmdWDir(command string, path string) (string, error) {
var stdout bytes.Buffer
scanner := bufio.NewScanner(out)
go func() {
for scanner.Scan() {
fmt.Printf("\n%s", scanner.Text())
}
wg.Done()
}()

args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
if err = cmd.Start(); err != nil {
return "", err
}

// process stdout and stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = &stdout
cmd.Stderr = &stdout
wg.Wait()

cmd.Dir = path
err := cmd.Run()
// convert output into string
fout, err := io.ReadAll(out)
if err != nil {
return "", err
}

return strings.TrimSpace(stdout.String()), err
return strings.TrimSpace(string(fout)), cmd.Wait()
}

// RunCmdWDirVerbose executes a specified shell command in a working specified directory with a verbose output.
func RunCmdWDirVerbose(command string, path string) error {
// RunCmdWDir executes a specified shell command in a specified working directory.
func RunCmdWDir(command string, path string) (string, error) {
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = path

output, err := cmd.StdoutPipe()
out, err := cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout
if err != nil {
return err
return "", err
}

var wg sync.WaitGroup
wg.Add(1)

scanner := bufio.NewScanner(output)
scanner := bufio.NewScanner(out)
go func() {
for scanner.Scan() {
fmt.Printf("\n%s", scanner.Text())
Expand All @@ -71,10 +70,16 @@ func RunCmdWDirVerbose(command string, path string) error {
}()

if err = cmd.Start(); err != nil {
return err
return "", err
}

wg.Wait()

return cmd.Wait()
// convert output into string
fout, err := io.ReadAll(out)
if err != nil {
return "", err
}

return strings.TrimSpace(string(fout)), cmd.Wait()
}

0 comments on commit 697368c

Please sign in to comment.