Skip to content

Commit

Permalink
Add '-debugbuild' option to 'install' command
Browse files Browse the repository at this point in the history
  • Loading branch information
afjoseph committed Mar 26, 2024
1 parent 2abbd5f commit c36f675
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
flagNameDebug = "debug"
flagSkipUpgrade = "skip-upgrade"
flagUpdateDB = "update-db"
flagDebugBuild = "debugbuild"
)

func setDebug(value bool) {
Expand Down Expand Up @@ -234,7 +235,7 @@ func installPackages(c *cli.Context) error {
site.CleanPackage(pkg)
}

err = site.InstallPackage(pkg)
err = site.InstallPackage(pkg, c.Bool(flagDebugBuild))
if err != nil {
return err
}
Expand Down Expand Up @@ -376,6 +377,10 @@ RZ_PM_SITE:
Name: "file",
Usage: "install a local file(s)",
},
&cli.BoolFlag{
Name: flagDebugBuild,
Usage: "Build the package in debug mode",
},
},
},
{
Expand Down
32 changes: 23 additions & 9 deletions pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type Package interface {
Description() string
Source() RizinPackageSource
Download(baseArtifactsPath string) error
Build(site Site) error
Install(site Site) ([]string, error)
Build(site Site, debugBuild bool) error
Install(site Site, debugBuild bool) ([]string, error)
Uninstall(site Site) error
}

Expand Down Expand Up @@ -233,7 +233,7 @@ func (rp RizinPackage) sourcePath(baseArtifactsPath string) string {
return filepath.Join(rp.artifactsPath(baseArtifactsPath), rp.PackageSource.Directory)
}

func (rp RizinPackage) buildMeson(site Site) error {
func (rp RizinPackage) buildMeson(site Site, debugBuild bool) error {
srcPath := rp.sourcePath(site.GetArtifactsDir())
args := []string{"setup"}
args = append(args, rp.PackageSource.BuildArguments...)
Expand All @@ -244,6 +244,13 @@ func (rp RizinPackage) buildMeson(site Site) error {
if site.GetCMakeDir() != "" {
args = append(args, fmt.Sprintf("--cmake-prefix-path=%s", site.GetCMakeDir()))
}
if debugBuild {
log.Println("Building in debug mode")
args = append(args, "--buildtype=debug")
} else {
log.Println("Building in optimized debug mode")
args = append(args, "--buildtype=debugoptimized")
}
args = append(args, "build")
cmd := exec.Command("meson", args...)
cmd.Dir = srcPath
Expand All @@ -263,14 +270,21 @@ func (rp RizinPackage) buildMeson(site Site) error {
return nil
}

func (rp RizinPackage) buildCMake(site Site) error {
func (rp RizinPackage) buildCMake(site Site, debugBuild bool) error {
srcPath := rp.sourcePath(site.GetArtifactsDir())
args := []string{}
args = append(args, rp.PackageSource.BuildArguments...)
args = append(args, fmt.Sprintf("-DCMAKE_INSTALL_PREFIX=%s/.local", xdg.Home))
if site.GetCMakeDir() != "" {
args = append(args, fmt.Sprintf("-DCMAKE_PREFIX_PATH=%s", site.GetCMakeDir()))
}
if debugBuild {
log.Println("Building in debug mode")
args = append(args, "-DCMAKE_BUILD_TYPE=Debug")
} else {
log.Println("Building in optimized debug mode")
args = append(args, "-DCMAKE_BUILD_TYPE=RelWithDebInfo")
}
args = append(args, "-B")
args = append(args, "build")
cmd := exec.Command("cmake", args...)
Expand Down Expand Up @@ -368,7 +382,7 @@ func buildErrorMsg(msg string) string {
}

// Build a package if a source is provided
func (rp RizinPackage) Build(site Site) error {
func (rp RizinPackage) Build(site Site, debugBuild bool) error {
if site.GetPkgConfigDir() == "" && site.GetCMakeDir() == "" {
return fmt.Errorf("make sure rizin development files are installed (e.g. librizin-dev, rizin-devel, etc.)")
}
Expand Down Expand Up @@ -397,7 +411,7 @@ func (rp RizinPackage) Build(site Site) error {
}
}

return rp.buildMeson(site)
return rp.buildMeson(site, debugBuild)
} else if rp.PackageSource.BuildSystem == "cmake" {
_, err := exec.LookPath("cmake")
if err != nil {
Expand All @@ -409,16 +423,16 @@ func (rp RizinPackage) Build(site Site) error {
return fmt.Errorf(buildErrorMsg("make sure `pkg-config` is installed and in PATH"))
}

return rp.buildCMake(site)
return rp.buildCMake(site, debugBuild)
} else {
log.Printf("BuildSystem %s is not supported yet.", rp.PackageSource.BuildSystem)
return fmt.Errorf("unsupported build system")
}
}

// Install a package after building it
func (rp RizinPackage) Install(site Site) ([]string, error) {
err := rp.Build(site)
func (rp RizinPackage) Install(site Site, debugBuild bool) ([]string, error) {
err := rp.Build(site, debugBuild)
if err != nil {
return []string{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s FakeSite) GetPkgConfigDir() string {
func (s FakeSite) GetCMakeDir() string {
return ""
}
func (s FakeSite) InstallPackage(pkg Package) error {
func (s FakeSite) InstallPackage(pkg Package, debugBuild bool) error {
return nil
}
func (s FakeSite) UninstallPackage(pkg Package) error {
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestInstallSimplePackage(t *testing.T) {
err = p.Download(tmpPath)
require.NoError(t, err, "package should be downloaded")

installed_files, err := p.Install(FakeSite{ArtifactsDir: tmpPath})
installed_files, err := p.Install(FakeSite{ArtifactsDir: tmpPath}, false)
assert.NoError(t, err, "The plugin should be built and installed without errors")
files, err := ioutil.ReadDir(pluginsPath)
require.NoError(t, err, "pluginsPath should be read")
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestUninstallSimplePackage(t *testing.T) {
require.NoError(t, err, "package should be downloaded")

s := FakeSite{ArtifactsDir: tmpPath}
_, err = p.Install(s)
_, err = p.Install(s, false)
assert.NoError(t, err, "The plugin should be built and installed without errors")

err = p.Uninstall(s)
Expand Down
12 changes: 7 additions & 5 deletions pkg/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Site interface {
GetArtifactsDir() string
GetPkgConfigDir() string
GetCMakeDir() string
InstallPackage(pkg Package) error
InstallPackage(pkg Package, debugBuild bool) error
UninstallPackage(pkg Package) error
CleanPackage(pkg Package) error
Remove() error
Expand Down Expand Up @@ -137,8 +137,10 @@ func (rp InstalledPackage) Source() RizinPackageSource { return RizinPackageSour
func (rp InstalledPackage) Download(baseArtifactsPath string) error {
return fmt.Errorf("cannot be called")
}
func (rp InstalledPackage) Build(site Site) error { return fmt.Errorf("cannot be called") }
func (rp InstalledPackage) Install(site Site) ([]string, error) {
func (rp InstalledPackage) Build(site Site, debugBuild bool) error {
return fmt.Errorf("cannot be called")
}
func (rp InstalledPackage) Install(site Site, debugBuild bool) ([]string, error) {
return nil, fmt.Errorf("cannot be called")
}
func (rp InstalledPackage) Uninstall(site Site) error { return fmt.Errorf("cannot be called") }
Expand Down Expand Up @@ -204,12 +206,12 @@ func (s *RizinSite) GetCMakeDir() string {
return s.CMakePath
}

func (s *RizinSite) InstallPackage(pkg Package) error {
func (s *RizinSite) InstallPackage(pkg Package, debugBuild bool) error {
if s.ContainsInstalledPackage(pkg.Name()) {
return fmt.Errorf("package %s already installed", pkg.Name())
}

files, err := pkg.Install(s)
files, err := pkg.Install(s, debugBuild)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func (fp FakePackage) Source() RizinPackageSource {
func (fp FakePackage) Download(baseArtifactsPath string) error {
return nil
}
func (fp FakePackage) Build(site Site) error {
func (fp FakePackage) Build(site Site, debugBuild bool) error {
return nil
}
func (fp FakePackage) Install(site Site) ([]string, error) {
func (fp FakePackage) Install(site Site, debugBuild bool) ([]string, error) {
return nil, nil
}
func (fp FakePackage) Uninstall(site Site) error {
Expand All @@ -181,7 +181,7 @@ func TestListInstalledPackages(t *testing.T) {

pkg := FakePackage{myName: "jsdec"}

err = site.InstallPackage(pkg)
err = site.InstallPackage(pkg, false)
require.NoError(t, err)

packages, err := site.ListAvailablePackages()
Expand Down

0 comments on commit c36f675

Please sign in to comment.