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

Call optionally cbuild2cmake instead of cbuildgen #167

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 24 additions & 21 deletions cmd/cbuild/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,31 @@ func NewRootCmd() *cobra.Command {
toolchain, _ := cmd.Flags().GetString("toolchain")
useContextSet, _ := cmd.Flags().GetBool("context-set")
frozenPacks, _ := cmd.Flags().GetBool("frozen-packs")
useCbuild2CMake, _ := cmd.Flags().GetBool("cbuild2cmake")

options := builder.Options{
IntDir: intDir,
OutDir: outDir,
LockFile: lockFile,
LogFile: logFile,
Generator: generator,
Target: target,
Jobs: jobs,
Quiet: quiet,
Debug: debug,
Verbose: verbose,
Clean: clean,
Schema: schema,
Packs: packs,
Rebuild: rebuild,
UpdateRte: updateRte,
Contexts: contexts,
UseContextSet: useContextSet,
Load: load,
Output: output,
Toolchain: toolchain,
FrozenPacks: frozenPacks,
IntDir: intDir,
OutDir: outDir,
LockFile: lockFile,
LogFile: logFile,
Generator: generator,
Target: target,
Jobs: jobs,
Quiet: quiet,
Debug: debug,
Verbose: verbose,
Clean: clean,
Schema: schema,
Packs: packs,
Rebuild: rebuild,
UpdateRte: updateRte,
Contexts: contexts,
UseContextSet: useContextSet,
Load: load,
Output: output,
Toolchain: toolchain,
FrozenPacks: frozenPacks,
UseCbuild2CMake: useCbuild2CMake,
}

configs, err := utils.GetInstallConfigs()
Expand Down Expand Up @@ -208,6 +210,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.PersistentFlags().BoolP("schema", "s", false, "Validate project input file(s) against schema")
rootCmd.PersistentFlags().StringP("log", "", "", "Save output messages in a log file")
rootCmd.PersistentFlags().StringP("toolchain", "", "", "Input toolchain to be used")
rootCmd.Flags().BoolP("cbuild2cmake", "", false, "Use cbuild2cmake")

// CPRJ specific hidden flags
rootCmd.Flags().StringP("intdir", "i", "", "Set directory for intermediate files")
Expand Down
207 changes: 207 additions & 0 deletions pkg/builder/cbuildidx/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* Copyright (c) 2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package cbuildidx

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

builder "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
utils "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
log "github.com/sirupsen/logrus"
)

type CbuildIdxBuilder struct {
builder.BuilderParams
}

func (b CbuildIdxBuilder) checkCbuildIdx() error {
fileName := filepath.Base(b.InputFile)
if !strings.HasSuffix(fileName, ".cbuild-idx.yml") {
err := errors.New(".cbuild-idx.yml file not found")
return err
} else {
if _, err := os.Stat(b.InputFile); os.IsNotExist(err) {
log.Error("cbuild-idx file " + b.InputFile + " does not exist")
return err
}
}
return nil
}

func (b CbuildIdxBuilder) clean(dirs builder.BuildDirs, vars builder.InternalVars) (err error) {
if _, err := os.Stat(dirs.IntDir); !os.IsNotExist(err) {
soumeh01 marked this conversation as resolved.
Show resolved Hide resolved
args := []string{"-E", "remove_directory", dirs.IntDir}
_, err = b.Runner.ExecuteCommand(vars.CmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cmake' clean " + dirs.IntDir)
return err
}
}
if _, err := os.Stat(dirs.OutDir); !os.IsNotExist(err) {
soumeh01 marked this conversation as resolved.
Show resolved Hide resolved
args := []string{"-E", "remove_directory", dirs.OutDir}
_, err = b.Runner.ExecuteCommand(vars.CmakeBin, false, args...)
if err != nil {
log.Error("error executing 'cmake' clean " + dirs.OutDir)
return err
}
}
log.Info("clean finished successfully!")
return nil
}

func (b CbuildIdxBuilder) getDirs() (dirs builder.BuildDirs, err error) {
if _, err := os.Stat(b.InputFile); os.IsNotExist(err) {
log.Error("project file " + b.InputFile + " does not exist")
return dirs, err
}

if b.Options.IntDir != "" {
dirs.IntDir = b.Options.IntDir
}
if b.Options.OutDir != "" {
dirs.OutDir = b.Options.OutDir
}

if b.Options.Output != "" {
dirs.IntDir = ""
dirs.OutDir = ""
}

path := filepath.Dir(b.InputFile)
cbuildFile := filepath.Join(path, b.Options.Contexts[0]+".cbuild.yml")
_, outDir, err := GetBuildDirs(cbuildFile)
if err != nil {
log.Error("error parsing file: " + cbuildFile)
return dirs, err
}

// cbuild2cmake generates cmake files under fixed tmp directory
dirs.IntDir = "tmp"
dirs.IntDir = filepath.Join(filepath.Dir(b.InputFile), dirs.IntDir)

if dirs.OutDir == "" {
soumeh01 marked this conversation as resolved.
Show resolved Hide resolved
dirs.OutDir = outDir
if dirs.OutDir == "" {
dirs.OutDir = "OutDir"
}
if !filepath.IsAbs(dirs.OutDir) {
dirs.OutDir = filepath.Join(filepath.Dir(b.InputFile), dirs.OutDir)
}
}

dirs.IntDir, _ = filepath.Abs(dirs.IntDir)
dirs.OutDir, _ = filepath.Abs(dirs.OutDir)

log.Debug("dirs.IntDir: " + dirs.IntDir)
log.Debug("dirs.OutDir: " + dirs.OutDir)

return dirs, err
}

func (b CbuildIdxBuilder) Build() error {
b.InputFile, _ = filepath.Abs(b.InputFile)
err := b.checkCbuildIdx()
if err != nil {
return err
}

vars, err := b.GetInternalVars()
if err != nil {
return err
}

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

dirs, err := b.getDirs()
if err != nil {
return err
}

if b.Options.Rebuild {
err = b.clean(dirs, vars)
if err != nil {
return err
}
} else if b.Options.Clean {
return b.clean(dirs, vars)
}

args := []string{b.InputFile}
if b.Options.Debug {
log.Debug("cbuild2cmake command: " + vars.Cbuild2cmakeBin + " " + strings.Join(args, " "))
}

_, 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) {
return err
}

if vars.CmakeBin == "" {
log.Error("cmake was not found")
return err
}

if b.Options.Generator == "" {
b.Options.Generator = "Ninja"
if vars.NinjaBin == "" {
log.Error("ninja was not found")
return err
}
}

args = []string{"-G", b.Options.Generator, "-S", dirs.IntDir, "-B", dirs.IntDir}
if b.Options.Debug {
args = append(args, "-Wdev")
} else {
args = append(args, "-Wno-dev")
}

if b.Options.Debug {
log.Debug("cmake configuration command: " + vars.CmakeBin + " " + strings.Join(args, " "))
}

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

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

if len(b.Options.Contexts) == 1 {
args = append(args, "--target", b.Options.Contexts[0])
args = append(args, "--target", b.Options.Contexts[0]+"-database")
} else {
err = errors.New("error invalid context(s) process request")
return err
}
if b.Options.Debug || b.Options.Verbose {
args = append(args, "--verbose")
}

if b.Options.Debug {
log.Debug("cmake build command: " + vars.CmakeBin + " " + strings.Join(args, " "))
}

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

log.Info("build finished successfully!")
return nil
}
Loading
Loading