Create .debs using Makefile in linux

93 Views Asked by At

How can we create .deb packages using Makefile in ubuntu. Can anyone explain.am new in Makefile.

OSARCH := "linux/amd64 linux/386 windows/amd64 windows/386 darwin/amd64 darwin/386"
ENV = /usr/bin/env
VERSION=$(shell git describe --dirty --tags --always)

.SHELLFLAGS = -c # Run commands in a -c flag 
.SILENT: ; # no need for @
.ONESHELL: ; # recipes execute in same shell
.NOTPARALLEL: ; # wait for this target to finish
.EXPORT_ALL_VARIABLES: ; # send all vars to shell

.PHONY: all # All targets are accessible for user
.DEFAULT: help # Running Make will run the help target

help: ## Show Help
        @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

dep: ## Get build dependencies
        go get -v -u github.com/golang/dep/cmd/dep

ensure: ## update dependencies
        dep ensure

build: ## Build the app
        for gomodule in  $$(go list ./... | grep -vE 'x1|x2|x3|x4|x5|myself|rfc****') ; do
                go build -gcflags "-N -l" -ldflags "-X main.version=$(VERSION) -s -w"  -o bin/$$(basename $$gomodule).s $$gomodule
                upx --force -qq -o bin/$$(basename $$gomodule) bin/$$(basename $$gomodule).s > /dev/null
        done

test: ## Launch tests
        go test -v ./...

clean: ## Launch clean
        go clean ./...
        rm -rf bin

install: ## Launch install
        go install -ldflags "-X main.version=$(VERSION)" $$(go list ./... | grep -v test)

test-cover: ## Launch tests coverage and send it to coverall
        $(ENV) ./scripts/test-coverage.sh

How to add lines to this makefile to create .deb packages after using the command sudo make

1

There are 1 best solutions below

5
Friedrich On

To create a Debian package in general, you need to have two directory trees: one with the actual files you want to package, and one with the control information for the package. You'll need a tree somewhat like this one:

+ DEBIAN
| + control
+ files
  + bin
  | + myapp
  + etc
  | + myapp.ini
  ...

The file control is a text file with some mandatory fields as documented in man dpkg-control.

To create a Debian package, refer to man dpkg-deb. The command line to do it looks like this

dpkg-deb --build files my-package_<version>_<arch>.deb

Reading your Makefile, it looks as if you created your binaries in a bin right below the project root. So you would either need to create them somewhere else or copy them into a tree for package creation.

Assuming all your files (DEBIAN and installation) reside in a directory package, these are the lines you would need to put into your Makefile:

package: <artifacts you want to package>
    dpkg-deb --build package my-package_<version>_<arch>.deb

A variable to contain the version will make things easier. (Hint: there's also version information in the control file and they should not contradict each other. You can set one from the other with a line of script.) The target architecture is only known to you.

Note that above is a very minimal example. There's lots of optional and highly useful functionality you can leverage. For further reading, refer to the Debian docs on Control files and Package maintainer scripts.

It might also be worthwhile to read the GNU Make Manual. Notably, your Makefile does not define any prerequisites for the targets (which is a prime motivation for using make in the first place) and .PHONY: all does not do what the comment says. In fact, it does nothing because there's no target named all.

Start with the manual's introduction and move on until you get an impression how Makefiles work. Then consider doing an overhaul of your Makefile with your newly gained knowledge. It's really well-documented and straightforward.