Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cbuild2cmake per context & build summary #235

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions pkg/builder/cbuildidx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func (b CbuildIdxBuilder) clean(dirs builder.BuildDirs, vars builder.InternalVar
}
args := []string{"-E", "remove_directory", dir}
_, err = b.Runner.ExecuteCommand(vars.CmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cmake' clean for " + dir)
}
return err
}

Expand Down Expand Up @@ -134,7 +131,7 @@ func (b CbuildIdxBuilder) Build() error {

_ = utils.UpdateEnvVars(vars.BinPath, vars.EtcPath)

if len(b.Options.Contexts) == 0 && len(b.BuildContexts) == 0 {
if len(b.Options.Contexts) == 0 && b.BuildContext == "" {
err = errors.New("error no context(s) to process")
return err
}
Expand All @@ -144,16 +141,14 @@ func (b CbuildIdxBuilder) Build() error {
}

if b.Options.Clean {
for _, context := range b.BuildContexts {
dirs, err := b.getDirs(context)
if err != nil {
return err
}
dirs, err := b.getDirs(b.BuildContext)
if err != nil {
return err
}

log.Info("Cleaning context: \"" + context + "\"")
if err := b.clean(dirs, vars); err != nil {
return err
}
log.Info("Cleaning context: \"" + b.BuildContext + "\"")
if err := b.clean(dirs, vars); err != nil {
return err
}
return nil
}
Expand All @@ -169,7 +164,6 @@ func (b CbuildIdxBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.Cbuild2cmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cbuild2cmake " + b.InputFile + "'")
return err
}
if _, err := os.Stat(dirs.IntDir + "/CMakeLists.txt"); errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -202,25 +196,20 @@ func (b CbuildIdxBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.CmakeBin, b.Options.Quiet, args...)
if err != nil {
log.Error("error executing 'cmake' configuration")
return err
}

// CMake build target(s) command
args = []string{"--build", dirs.IntDir, "-j", fmt.Sprintf("%d", b.GetJobs())}

if b.Options.Target != "" {
args = append(args, "--target", b.Options.Target)
} else if len(b.Options.Contexts) == 0 {
args = append(args, "--target", "all")
} else {
for _, context := range b.BuildContexts {
args = append(args, "--target", context)
}
} else if b.BuildContext != "" {
args = append(args, "--target", b.BuildContext)
}

if b.Setup {
for _, context := range b.BuildContexts {
args = append(args, "--target", context+"-database")
}
args = append(args, "--target", b.BuildContext+"-database")
}

if b.Options.Debug {
Expand All @@ -229,7 +218,6 @@ func (b CbuildIdxBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.CmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cmake' build")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/cbuildidx/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestBuildAllContexts(t *testing.T) {
BinExtn: configs.BinExtn,
EtcPath: configs.EtcPath,
},
BuildContexts: []string{"Hello.Debug+AVH"},
BuildContext: "Hello.Debug+AVH",
},
}
t.Run("test build cbuild-idx", func(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions pkg/builder/cproject/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ func (b CprjBuilder) clean(dirs builder.BuildDirs, vars builder.InternalVars) (e
if _, err := os.Stat(dirs.IntDir); !os.IsNotExist(err) {
_, err = b.Runner.ExecuteCommand(vars.CbuildgenBin, false, "rmdir", dirs.IntDir)
if err != nil {
log.Error("error executing 'cbuildgen rmdir'")
return err
}
}
if _, err := os.Stat(dirs.OutDir); !os.IsNotExist(err) {
_, err = b.Runner.ExecuteCommand(vars.CbuildgenBin, false, "rmdir", dirs.OutDir)
if err != nil {
log.Error("error executing 'cbuildgen rmdir'")
return err
}
}
Expand Down Expand Up @@ -140,7 +138,6 @@ func (b CprjBuilder) Build() error {
} else {
_, err = b.Runner.ExecuteCommand(vars.XmllintBin, b.Options.Quiet, "--schema", filepath.Join(vars.EtcPath, "CPRJ.xsd"), b.InputFile, "--noout")
if err != nil {
log.Error("error executing 'xmllint'")
return err
}
}
Expand All @@ -163,7 +160,6 @@ func (b CprjBuilder) Build() error {
}
_, err = b.Runner.ExecuteCommand(vars.CbuildgenBin, false, args...)
if err != nil {
log.Error("error executing 'cbuildgen packlist'")
return err
}

Expand All @@ -181,7 +177,6 @@ func (b CprjBuilder) Build() error {
}
_, err = b.Runner.ExecuteCommand(vars.CpackgetBin, b.Options.Quiet, args...)
if err != nil {
log.Error("error executing 'cpackget add'")
return err
}
} else {
Expand All @@ -206,7 +201,6 @@ func (b CprjBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.CbuildgenBin, false, args...)
if err != nil {
log.Error("error executing 'cbuildgen cmake'")
return err
}

Expand Down Expand Up @@ -240,7 +234,6 @@ func (b CprjBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.CmakeBin, b.Options.Quiet, args...)
if err != nil {
log.Error("error executing 'cmake' configuration")
return err
}

Expand All @@ -263,7 +256,6 @@ func (b CprjBuilder) Build() error {

_, err = b.Runner.ExecuteCommand(vars.CmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cmake' build")
return err
}

Expand Down
79 changes: 38 additions & 41 deletions pkg/builder/csolution/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func (b CSolutionBuilder) installMissingPacks() (err error) {
// Get list of missing packs
output, err := b.runCSolution(args, false)
if err != nil {
log.Error("error in getting list of missing packs")
return err
}

Expand All @@ -120,7 +119,6 @@ func (b CSolutionBuilder) installMissingPacks() (err error) {

_, err = b.Runner.ExecuteCommand(cpackgetBin, false, args...)
if err != nil {
log.Error("error installing pack : " + pack)
return err
}
}
Expand Down Expand Up @@ -175,7 +173,6 @@ func (b CSolutionBuilder) generateBuildFiles() (err error) {
// Retrieve all available contexts in yml-order
allContexts, err := b.listContexts(true, true)
if err != nil {
log.Error("error getting list of contexts: \"" + err.Error() + "\"")
return err
}

Expand Down Expand Up @@ -315,30 +312,30 @@ func (b CSolutionBuilder) getProjsBuilders(selectedContexts []string) (projBuild
}

var projBuilder builder.IBuilderInterface
if b.Options.UseCbuild2CMake {
// get idx builder
projBuilder = cbuildidx.CbuildIdxBuilder{
BuilderParams: builder.BuilderParams{
Runner: b.Runner,
Options: buildOptions,
InputFile: idxFile,
InstallConfigs: b.InstallConfigs,
Setup: b.Setup,
BuildContexts: selectedContexts,
},
for _, context := range selectedContexts {
infoMsg := "Retrieve build information for context: \"" + context + "\""
log.Info(infoMsg)

// if --output is used, ignore provided --outdir and --intdir
if b.Options.Output != "" && (b.Options.OutDir != "" || b.Options.IntDir != "") {
log.Warn("output files are generated under: \"" +
b.Options.Output + "\". Options --outdir and --intdir shall be ignored.")
}
projBuilders = append(projBuilders, projBuilder)
} else {
for _, context := range selectedContexts {
infoMsg := "Retrieve build information for context: \"" + context + "\""
log.Info(infoMsg)

// if --output is used, ignore provided --outdir and --intdir
if b.Options.Output != "" && (b.Options.OutDir != "" || b.Options.IntDir != "") {
log.Warn("output files are generated under: \"" +
b.Options.Output + "\". Options --outdir and --intdir shall be ignored.")
}

if b.Options.UseCbuild2CMake {
// get idx builder
projBuilder = cbuildidx.CbuildIdxBuilder{
BuilderParams: builder.BuilderParams{
Runner: b.Runner,
Options: buildOptions,
InputFile: idxFile,
InstallConfigs: b.InstallConfigs,
Setup: b.Setup,
BuildContext: context,
},
}
projBuilders = append(projBuilders, projBuilder)
} else {
cprjFile, err := b.getCprjFilePath(idxFile, context)
if err != nil {
log.Error("error getting cprj file: " + err.Error())
Expand Down Expand Up @@ -404,7 +401,13 @@ func (b CSolutionBuilder) buildContexts(selectedContexts []string, projBuilders
operation = "Setting up"
}

buildSuccess := true
printSeparator := func(delimiter string, length int) {
sep := strings.Repeat(delimiter, length-1)
utils.LogStdMsg("+" + sep)
}

buildPassCnt := 0
buildFailCnt := 0
var totalBuildTime time.Duration
for index := range projBuilders {
var infoMsg string
Expand All @@ -415,31 +418,27 @@ func (b CSolutionBuilder) buildContexts(selectedContexts []string, projBuilders
infoMsg = progress + " " + operation + " context: \"" + selectedContexts[index] + "\""
}

if !b.Options.UseCbuild2CMake {
sep := strings.Repeat("=", len(infoMsg)+13)
utils.LogStdMsg(sep)
utils.LogStdMsg(infoMsg)
}

printSeparator("-", len(infoMsg))
utils.LogStdMsg(infoMsg)
b.setBuilderOptions(&projBuilders[index], false)

buildStartTime := time.Now()
err = projBuilders[index].Build()
if err != nil {
log.Error("error " + strings.ToLower(operation) + " '" + b.getBuilderInputFile(projBuilders[index]) + "'")
buildSuccess = false
buildFailCnt += 1
} else {
buildPassCnt += 1
}
buildEndTime := time.Now()
elapsedTime := buildEndTime.Sub(buildStartTime)
totalBuildTime += elapsedTime
}
if !b.Setup {
buildStatus := "Build completed successfully"
if !buildSuccess {
buildStatus = "Build failed"
}
buildSummary := fmt.Sprintf("\nBuild summary: %s - Time Elapsed: %s", buildStatus, utils.FormatTime(totalBuildTime))
buildSummary := fmt.Sprintf("Build summary: %d succeeded, %d failed - Time Elapsed: %s", buildPassCnt, buildFailCnt, utils.FormatTime(totalBuildTime))
sepLen := len(buildSummary)
printSeparator("-", sepLen)
utils.LogStdMsg(buildSummary)
printSeparator("=", sepLen)
}
return
}
Expand Down Expand Up @@ -556,7 +555,6 @@ func (b CSolutionBuilder) build() (err error) {
if len(b.Options.Contexts) != 0 && !b.Options.UseContextSet {
allContexts, err = b.listContexts(true, true)
if err != nil {
log.Error("error getting list of contexts: \"" + err.Error() + "\"")
return err
}
selectedContexts, err = utils.ResolveContexts(allContexts, b.Options.Contexts)
Expand Down Expand Up @@ -603,7 +601,6 @@ func (b CSolutionBuilder) Build() (err error) {

// STEP 1: Install missing pack(s)
if err = b.installMissingPacks(); err != nil {
log.Error("error installing missing packs")
// Continue with build files generation upon setup command
if !b.Setup {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type BuilderParams struct {
InputFile string
InstallConfigs utils.Configurations
Setup bool
BuildContexts []string
BuildContext string
}

type Options struct {
Expand Down
24 changes: 23 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ func GetProjectName(csolutionFile string) (projectName string, err error) {
func ResolveContexts(allContext []string, contextFilters []string) ([]string, error) {
var selectedContexts []string

for _, filter := range contextFilters {
// remove duplicates (if any)
filters := RemoveDuplicates(contextFilters)

for _, filter := range filters {
filterContextItem, err := ParseContext(filter)
if err != nil {
return nil, err
Expand Down Expand Up @@ -351,3 +354,22 @@ func FormatTime(time time.Duration) string {
// Format time in "hh:mm:ss"
return fmt.Sprintf("%02d:%02d:%02d", int(time.Hours()), int(time.Minutes())%60, int(time.Seconds())%60)
}

func RemoveDuplicates(input []string) []string {
// Create a map to track seen strings
seen := make(map[string]bool)
// Create a slice to store the unique strings
var result []string

// Iterate over the input slice
for _, str := range input {
// If the string is not in the map,
// add it to the result and mark it as seen
if !seen[str] {
result = append(result, str)
seen[str] = true
}
}

return result
}
13 changes: 13 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,16 @@ func TestRemoveVersionRange(t *testing.T) {
assert.Equal(test.expectedOutput, outString)
}
}

func TestRemoveDuplicates(t *testing.T) {
assert := assert.New(t)

inputList := []string{"apple", "banana", "apple", "orange", "banana", "grape"}
UniqueList := []string{"apple", "banana", "orange", "grape"}

outUniqueList := RemoveDuplicates(inputList)
assert.Equal(UniqueList, outUniqueList)

outUniqueList = RemoveDuplicates(UniqueList)
assert.Equal(UniqueList, outUniqueList)
}