Skip to content

Commit

Permalink
Add create-sysroot command
Browse files Browse the repository at this point in the history
  • Loading branch information
mikusaq committed Nov 12, 2024
1 parent 58930c0 commit 2729470
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 9 deletions.
47 changes: 42 additions & 5 deletions bap-builder/CmdArgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ type BuildPackageCmdLineArgs struct {
OutputDirMode *OutputDirMode
}

// CreateSysrootCmdLineArgs
// Options/setting for Sysroot mode
type CreateSysrootCmdLineArgs struct {
// Path to the Git Lfs repository with packages
Repo *string
// Name of the new sysroot directory to be created
Sysroot *string
// Name of the docker image which are the packages build for
ImageName *string
}

// CmdLineArgs
// Represents Cmd line arguments passed to cmd line of the target program.
// Program operates in two modes
Expand All @@ -54,11 +65,15 @@ type CmdLineArgs struct {
// Standard Cmd line arguments for Docker mode
BuildImagesArgs BuildImageCmdLineArgs
// If true the program is in the "Package" mode
BuildPackage bool
BuildPackageArgs BuildPackageCmdLineArgs
buildImageParser *argparse.Command
buildPackageParser *argparse.Command
parser *argparse.Parser
BuildPackage bool
// If true the program is in the "Sysroot" mode
CreateSysroot bool
BuildPackageArgs BuildPackageCmdLineArgs
CreateSysrootArgs CreateSysrootCmdLineArgs
buildImageParser *argparse.Command
buildPackageParser *argparse.Command
createSysrootParser *argparse.Command
parser *argparse.Parser
}

// InitFlags
Expand Down Expand Up @@ -126,6 +141,27 @@ func (cmd *CmdLineArgs) InitFlags() {
Help: "Name of the docker image to build",
},
)

cmd.createSysrootParser = cmd.parser.NewCommand("create-sysroot", "Create Sysroot")
cmd.CreateSysrootArgs.Sysroot = cmd.createSysrootParser.String("", "sysroot-dir",
&argparse.Options{
Required: true,
Help: "Name of the sysroot directory which will be created",
Default: false,
},
)
cmd.CreateSysrootArgs.Repo = cmd.createSysrootParser.String("", "git-lfs",
&argparse.Options{
Required: true,
Help: "Git Lfs directory where packages are stored",
},
)
cmd.CreateSysrootArgs.ImageName = cmd.createSysrootParser.String("", "image-name",
&argparse.Options{
Required: true,
Help: "Name of docker image which are the packages build for",
},
)
}

// ParseArgs
Expand All @@ -146,6 +182,7 @@ func (cmd *CmdLineArgs) ParseArgs(args []string) error {
if *cmd.BuildPackageArgs.All && *cmd.BuildPackageArgs.BuildDeps {
return fmt.Errorf("all and build-deps flags at the same time")
}
cmd.CreateSysroot = cmd.createSysrootParser.Happened()

return nil
}
140 changes: 140 additions & 0 deletions bap-builder/SysrootMode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package main

import (
"bringauto/modules/bringauto_config"
"bringauto/modules/bringauto_context"
"bringauto/modules/bringauto_log"
"bringauto/modules/bringauto_package"
"bringauto/modules/bringauto_prerequisites"
"bringauto/modules/bringauto_repository"
"fmt"
"io"
"os"
"path"

"github.com/mholt/archiver/v3"
)

const (
ReleasePath = "release"
DebugPath = "debug"
)

// BuildDockerImage
// process Docker mode of cmd line
func CreateSysroot(cmdLine *CreateSysrootCmdLineArgs, contextPath string) error {
err := os.MkdirAll(*cmdLine.Repo, 0766)
if err != nil {
return nil
}

dirEmpty, err := isDirEmpty(*cmdLine.Sysroot)
if err != nil {
return err
}
if !dirEmpty {
return fmt.Errorf("given sysroot directory is not empty")
}

repo := bringauto_repository.GitLFSRepository{
GitRepoPath: *cmdLine.Repo,
}
err = bringauto_prerequisites.Initialize(&repo)
if err != nil {
return nil
}
platformString, err := determinePlatformString(*cmdLine.ImageName)
if err != nil {
return err
}
contextManager := bringauto_context.ContextManager{
ContextPath: contextPath,
}
logger := bringauto_log.GetLogger()
logger.Info("Checking Git Lfs directory consistency")
err = repo.CheckGitLfsConsistency(&contextManager, platformString)
if err != nil {
return err
}
packages, err := getPackages(&contextManager, platformString)

logger.Info("Creating sysroot directory from packages")
err = unzipAllPackagesToDir(packages, &repo, *cmdLine.Sysroot)
if err != nil {
return err
}

return nil
}

func unzipAllPackagesToDir(packages []bringauto_package.Package, repo *bringauto_repository.GitLFSRepository, dirPath string) error {
for _, pack := range packages {
packPath := path.Join(repo.CreatePackagePath(pack), pack.GetFullPackageName() + bringauto_package.ZipExt)
_, err := os.Stat(packPath)
if err == nil { // Package exists in Git Lfs
var sysrootPath string
if pack.IsDebug {
sysrootPath = path.Join(dirPath, DebugPath)
} else {
sysrootPath = path.Join(dirPath, ReleasePath)
}

zipArchive := archiver.Zip{
MkdirAll: true,
OverwriteExisting: false,
SelectiveCompression: true,
}
err := zipArchive.Unarchive(packPath, sysrootPath)
if err != nil {
return err
}
}
}

return nil
}

func isDirEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil { // The directory do not exists
return true, nil
}
defer f.Close()

_, err = f.Readdirnames(1)

if err == io.EOF { // The directory exists, but is empty
return true, nil
} else if err != nil {
return false, err
}

return false, nil
}

func getPackages(contextManager *bringauto_context.ContextManager, platformString *bringauto_package.PlatformString) ([]bringauto_package.Package, error) {
var packConfigs []*bringauto_config.Config
packageJsonPathMap, err := contextManager.GetAllPackagesJsonDefPaths()
if err != nil {
return nil, err
}
logger := bringauto_log.GetLogger()
for _, packageJsonPaths := range packageJsonPathMap {
for _, packageJsonPath := range packageJsonPaths {
var config bringauto_config.Config
err = config.LoadJSONConfig(packageJsonPath)
if err != nil {
logger.Warn("Couldn't load JSON config from %s path - %s", packageJsonPath, err)
continue
}
packConfigs = append(packConfigs, &config)
}
}
var packages []bringauto_package.Package
for _, packConfig := range packConfigs {
packConfig.Package.PlatformString = *platformString
packages = append(packages, packConfig.Package)
}

return packages, nil
}
9 changes: 9 additions & 0 deletions bap-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,14 @@ func main() {
return
}

if args.CreateSysroot {
err = CreateSysroot(&args.CreateSysrootArgs, *args.Context)
if err != nil {
logger.Error("Failed to create sys: %s", err)
return
}
return
}

return
}
3 changes: 2 additions & 1 deletion modules/bringauto_package/Package.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
ZipExt = ".zip"
defaultPackageNameConst = "generic-package"
defaultVersionTagConst = "v0.0.0"
stringSeparator = "_"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (packg *Package) CreatePackage(sourceDir string, outputDir string) error {
return err
}

packageName := packg.GetFullPackageName() + ".zip"
packageName := packg.GetFullPackageName() + ZipExt

err = createZIPArchive(sourceDir, outputDir+"/"+packageName)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions modules/bringauto_repository/GitLFSRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (lfs *GitLFSRepository) CheckGitLfsConsistency(contextManager *bringauto_co

var expectedPackPaths []string
for _, pack := range packages {
packPath := filepath.Join(lfs.createPackagePath(pack) + "/" + pack.GetFullPackageName() + ".zip")
packPath := filepath.Join(lfs.CreatePackagePath(pack) + "/" + pack.GetFullPackageName() + ".zip")
expectedPackPaths = append(expectedPackPaths, packPath)
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func printErrors(errorPackPaths []string, expectedPackPaths []string) error {
return nil
}

func (lfs *GitLFSRepository) createPackagePath(pack bringauto_package.Package) string {
func (lfs *GitLFSRepository) CreatePackagePath(pack bringauto_package.Package) string {
repositoryPath := path.Join(
pack.PlatformString.String.DistroName,
pack.PlatformString.String.DistroRelease,
Expand All @@ -179,7 +179,7 @@ func (lfs *GitLFSRepository) createPackagePath(pack bringauto_package.Package) s
// Each package is stored in different directory structure represented by
// PlatformString.DistroName / PlatformString.DistroRelease / PlatformString.Machine / <package>
func (lfs *GitLFSRepository) CopyToRepository(pack bringauto_package.Package, sourceDir string) error {
archiveDirectory := lfs.createPackagePath(pack)
archiveDirectory := lfs.CreatePackagePath(pack)

var err error
err = os.MkdirAll(archiveDirectory, 0755)
Expand Down

0 comments on commit 2729470

Please sign in to comment.