Skip to content

Commit

Permalink
feat: dpkg: improve strip of fakeroot binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Sep 21, 2024
1 parent 0cf41f2 commit cdd5a4f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 55 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ linters:
# they will be enabled
- depguard
- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen
Expand Down
19 changes: 11 additions & 8 deletions pkg/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ type Apk struct {
apkDir string
}

// Build builds the APK package.
//
// It takes the artifactsPath as a parameter and returns an error.
func (a *Apk) Build(artifactsPath string) error {
// BuildPackage initiates the package building process for the Apk instance.
// It takes artifactsPath to specify where to store the build artifacts
// and calls the internal apkBuild method, returning any errors encountered.
func (a *Apk) BuildPackage(artifactsPath string) error {
return a.apkBuild(artifactsPath)
}

// PrepareFakeroot sets up the environment for building an APK package in a fakeroot context.
// It initializes the apkDir, cleans up any existing directory, creates the necessary packer directory,
// and generates the APKBUILD and post-installation script files. The method returns an error if any step fails.
func (a *Apk) PrepareFakeroot(_ string) error {
a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk")

if err := utils.RemoveAll(a.apkDir); err != nil {
Expand All @@ -47,10 +54,6 @@ func (a *Apk) Build(artifactsPath string) error {
return err
}

if err := a.apkBuild(artifactsPath); err != nil {
return err
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func (builder *Builder) Compile(noBuild bool) error {

if !noBuild {
if err := processFunction(builder.PKGBUILD.Prepare,
"preparing"); err != nil {
"preparing sources"); err != nil {
return err
}

if err := processFunction(builder.PKGBUILD.Build,
"building"); err != nil {
"processing sources"); err != nil {
return err
}

if err := processFunction(builder.PKGBUILD.Package,
"generating package"); err != nil {
"preparing fakeroot"); err != nil {
return err
}
}
Expand Down
48 changes: 26 additions & 22 deletions pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package dpkg

import (
"bytes"
"fmt"
"path/filepath"
"regexp"
"strings"
"text/template"

"github.com/M0Rf30/yap/pkg/options"
"github.com/M0Rf30/yap/pkg/pkgbuild"
Expand Down Expand Up @@ -168,19 +166,17 @@ func (d *Deb) Prepare(makeDepends []string) error {
// It does not take any parameters.
// It returns an error if there is any issue during stripping.
func (d *Deb) Strip() error {
var tmplBytesBuffer bytes.Buffer
var output strings.Builder

utils.Logger.Info("stripping binaries")

tmpl := template.New("strip")
tmpl := d.PKGBUILD.RenderSpec(options.StripScript)

template.Must(tmpl.Parse(options.StripScript))
utils.Logger.Info("stripping binaries")

if pkgbuild.Verbose {
return tmpl.Execute(&tmplBytesBuffer, d.PKGBUILD)
if err := tmpl.Execute(&output, d.PKGBUILD); err != nil {
return err
}

return utils.RunScript(tmplBytesBuffer.String())
return utils.RunScript(output.String())
}

// Update updates the Deb package list.
Expand Down Expand Up @@ -246,30 +242,38 @@ func (d *Deb) createDebResources() error {
return nil
}

// Build builds the Deb package.
//
// It takes the artifactsPath as a parameter and returns an error if any.
func (d *Deb) Build(artifactsPath string) error {
d.getArch()
d.getRelease()

if err := utils.RemoveAll(d.debDir); err != nil {
// BuildPackage builds the Debian package and cleans up afterward.
// It takes artifactsPath to specify where to store the package.
// The method calls dpkgDeb to create the package and removes the
// package directory, returning an error if any step fails.
func (d *Deb) BuildPackage(artifactsPath string) error {
if err := d.dpkgDeb(artifactsPath); err != nil {
return err
}

if err := d.createDebResources(); err != nil {
if err := utils.RemoveAll(d.PKGBUILD.PackageDir); err != nil {
return err
}

if err := d.Strip(); err != nil {
return nil
}

// PrepareFakeroot sets up the environment for building a Debian package in a fakeroot context.
// It retrieves architecture and release information, cleans up the debDir, creates necessary
// resources, and strips binaries. The method returns an error if any step fails.
func (d *Deb) PrepareFakeroot(_ string) error {
d.getArch()
d.getRelease()

if err := utils.RemoveAll(d.debDir); err != nil {
return err
}

if err := d.dpkgDeb(artifactsPath); err != nil {
if err := d.createDebResources(); err != nil {
return err
}

if err := utils.RemoveAll(d.PKGBUILD.PackageDir); err != nil {
if err := d.Strip(); err != nil {
return err
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import (

// Packer is the common interface implemented by all package managers.
type Packer interface {
// Prepare appends the dependencies required to build all the projects. It
// returns any error if encountered.
Prepare(depends []string) error
// Build reads the path where the final artifact will be written. It returns any
// error if encountered.
Build(output string) error
// BuildPackage starts the package building process and writes the final artifact
// to the specified output path. It returns an error if any issues occur during the build.
BuildPackage(output string) error
// Install reads the path where the final artifact will be written. It returns
// any error if encountered.
Install(output string) error
// Prepare appends the dependencies required to build all the projects. It
// returns any error if encountered.
Prepare(depends []string) error
// PrepareEnvironment reads a flag to install golang tools on request, on the
// build machine. It returns any error if encountered.
PrepareEnvironment(flag bool) error
// PrepareFakeroot sets up the environment for building the final artifact in a fakeroot context.
// It takes an output path where the artifact will be written and returns an error if any issues occur.
PrepareFakeroot(output string) error
// Update performs a package manager update operation. It returns any error if
// encountered.
Update() error
Expand Down
24 changes: 17 additions & 7 deletions pkg/pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ type Pacman struct {
pacmanDir string
}

// Build builds the Pacman package.
// BuildPackage initiates the package building process for the Pacman instance.
//
// It takes the artifactsPath as a parameter and returns an error if any.
func (p *Pacman) Build(artifactsPath string) error {
// It takes a single parameter:
// - artifactsPath: a string representing the path where the build artifacts will be stored.
//
// The method calls the internal pacmanBuild function to perform the actual build process.
// It returns an error if the build process encounters any issues.
func (p *Pacman) BuildPackage(_ string) error {
return p.pacmanBuild()
}

// PrepareFakeroot sets up the environment for building a package in a fakeroot context.
//
// It takes an artifactsPath parameter, which specifies where to store build artifacts.
// The method initializes the pacmanDir, resolves the package destination, and creates
// the PKGBUILD and post-installation script files if necessary. It returns an error
// if any step fails.
func (p *Pacman) PrepareFakeroot(artifactsPath string) error {
p.pacmanDir = p.PKGBUILD.StartDir

p.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath)
Expand All @@ -41,10 +55,6 @@ func (p *Pacman) Build(artifactsPath string) error {
return err
}

if err := p.pacmanBuild(); err != nil {
return err
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ func (pkgBuild *PKGBUILD) CreateSpec(filePath string, tmpl *template.Template) e
return tmpl.Execute(file, pkgBuild)
}

// RenderSpec initializes a new template with custom functions and parses the provided script.
// It adds two custom functions to the template:
// 1. "join": Takes a slice of strings and joins them into a single string, separated by commas,
// while also trimming any leading or trailing spaces.
// 2. "multiline": Takes a string and replaces newline characters with a newline followed by a space,
// effectively formatting the string for better readability in multi-line contexts.
//
// The method returns the parsed template, which can be used for rendering with data.
func (pkgBuild *PKGBUILD) RenderSpec(script string) *template.Template {
tmpl := template.New("template").Funcs(template.FuncMap{
"join": func(strs []string) string {
Expand Down
8 changes: 7 additions & 1 deletion pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,13 @@ func (mpc *MultipleProject) createPackages(proj *Project) error {
return err
}

if err := proj.PackageManager.Build(mpc.Output); err != nil {
if err := proj.PackageManager.PrepareFakeroot(mpc.Output); err != nil {
return err
}

utils.Logger.Info("building resulting package")

if err := proj.PackageManager.BuildPackage(mpc.Output); err != nil {
return err
}

Expand Down
20 changes: 12 additions & 8 deletions pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ type RPM struct {
srpmsDir string
}

// Build builds the RPM package.
//
// It takes the artifactsPath as a parameter and returns an error.
func (r *RPM) Build(artifactsPath string) error {
// BuildPackage initiates the package building process for the RPM instance.
// It calls the internal rpmBuild method and returns any errors encountered during the process.
func (r *RPM) BuildPackage(_ string) error {
return r.rpmBuild()
}

// PrepareFakeroot sets up the environment for building an RPM package in a fakeroot context.
// It retrieves architecture, group, and release information, sets the package destination,
// cleans up the RPM directory, creates necessary directories, and gathers files.
// It also processes package dependencies and creates the RPM spec file, returning
// an error if any step fails.
func (r *RPM) PrepareFakeroot(artifactsPath string) error {
r.getArch()
r.getGroup()
r.getRelease()
Expand Down Expand Up @@ -71,10 +79,6 @@ func (r *RPM) Build(artifactsPath string) error {
return err
}

if err := r.rpmBuild(); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit cdd5a4f

Please sign in to comment.