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

Add flag to build packages in debug mode #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"

Expand All @@ -21,6 +22,7 @@ const (
flagNameDebug = "debug"
flagSkipUpgrade = "skip-upgrade"
flagUpdateDB = "update-db"
flagDebugBuild = "debugbuild"
)

func setDebug(value bool) {
Expand Down Expand Up @@ -109,8 +111,14 @@ func infoPackage(c *cli.Context) error {
fmt.Printf("Name: %s\n", pkg.Name())
fmt.Printf("Version: %s\n", pkg.Version())
fmt.Printf("Summary: %s\n", pkg.Summary())
fmt.Printf("URL: %s\n", pkg.Source().URL)
fmt.Printf("Build system: %s\n", pkg.Source().BuildSystem)
fmt.Printf("Build arguments: %v\n", pkg.Source().BuildArguments)
fmt.Printf("Description: %s\n", pkg.Description())
fmt.Printf("Installed: %s\n", isInstalled)
if isInstalled == "yes" {
fmt.Printf("Install location: %s\n", filepath.Join(site.GetArtifactsDir(), pkg.Source().Directory))
}
Comment on lines +119 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the "build" directory?

return nil
}

Expand Down Expand Up @@ -227,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 @@ -369,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
13 changes: 8 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 All @@ -221,6 +223,7 @@ func (s *RizinSite) InstallPackage(pkg Package) error {
&minorVersion,
})
installedFilePath := filepath.Join(s.Path, installedFile)
log.Printf("Installed package %s at %s\n", pkg.Name(), installedFilePath)
return updateInstalledPackages(installedFilePath, s.installedPackages)
}

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
Loading