diff --git a/agb/command/build.go b/agb/command/build.go index f77bb65..fe5e73c 100644 --- a/agb/command/build.go +++ b/agb/command/build.go @@ -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 } diff --git a/agb/core/gki.go b/agb/core/gki.go index 94d7611..798331c 100644 --- a/agb/core/gki.go +++ b/agb/core/gki.go @@ -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 @@ -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 { diff --git a/agb/manager/git.go b/agb/manager/git.go index b23c132..e2fd95d 100644 --- a/agb/manager/git.go +++ b/agb/manager/git.go @@ -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 diff --git a/agb/manager/resource.go b/agb/manager/resource.go index 6038712..3230fb2 100644 --- a/agb/manager/resource.go +++ b/agb/manager/resource.go @@ -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(), } } @@ -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) +} diff --git a/agb/tool/command.go b/agb/tool/command.go index b22be55..b4732f8 100644 --- a/agb/tool/command.go +++ b/agb/tool/command.go @@ -2,9 +2,8 @@ package tool import ( "bufio" - "bytes" "fmt" - "os" + "io" "os/exec" "strings" "sync" @@ -12,57 +11,57 @@ import ( // 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()) @@ -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() }