You are not logged in.

#1 2013-02-13 11:03:54

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,420
Website

Makefile to install files rather than a PKGBUILD doing it [SOLVED]

In the past, I have used my PKGBUILD to do all the dirty work installing files into the $pkgdir dirtree.  My strategy has now shifted to using a Makefile.  Why?  I want to make my app more friendly to other-distro (particularly ubuntu/debian's build system).  I plan on having makepkg call it via a `make DESTDIR="$pkgdir" install` in the PKGBUILD.

I'd like some feedback on the strategy in general.  I also have a few questions:

1) This Makefile is totally Arch Linux-centric.  How should I handle building for other distros?
*Upstream (me) will handle it: i.e. some make target in my Makefile like 'make-ubuntu' and 'make-gentoo' etc.?
*Do nothing and let packagers of others distros patch my Makefile to their specs?

2) How to add a clean target that won't zap the entire DESTDIR?

VERSION = 5.19
PN = profile-sync-daemon

DESTDIR =
PREFIX = /usr
CONFDIR = /etc
CRONDIR = /etc/cron.hourly
INITDIR = /usr/lib/systemd/system
BINDIR = $(PREFIX)/bin
DOCDIR = $(PREFIX)/share/doc/$(PN)-$(VERSION)
MANDIR = $(PREFIX)/share/man/man1

install-bin:
	install -Dm755 common/$(PN) "$(DESTDIR)$(BINDIR)/$(PN)"
	install -Dm644 common/psd.conf "$(DESTDIR)$(CONFDIR)/psd.conf"
	install -Dm644 init/psd.service "$(DESTDIR)$(INITDIR)/psd.service"
	@sed -i -e 's/@VERSION@/'$(VERSION)'/' "$(DESTDIR)$(BINDIR)/$(PN)"
	ln -s $(PN) "$(DESTDIR)$(BINDIR)/psd"

install-man:
	install -Dm644 doc/psd.1 "$(DESTDIR)$(MANDIR)/psd.1"
	gzip -9 "$(DESTDIR)$(MANDIR)/psd.1"
	ln -s psd.1.gz "$(DESTDIR)$(MANDIR)/$(PN).1.gz"

install-cron:
	install -Dm755 common/psd.cron.hourly "$(DESTDIR)$(CRONDIR)/psd-update"

install: install-bin install-man install-cron

Thanks all.  Note that this is the first time I ever seriously looked into a Makefile - I am totally new to this script.

Last edited by graysky (2013-02-13 17:07:07)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2013-02-13 11:23:47

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,271

Re: Makefile to install files rather than a PKGBUILD doing it [SOLVED]

Moving to Creating & Modifying Packages upon graysky's request.


To know or not to know ...
... the questions remain forever.

Offline

#3 2013-02-13 12:05:16

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,510
Website

Re: Makefile to install files rather than a PKGBUILD doing it [SOLVED]

PREFIX (and most other variables for that matter) can be set with the ?= operator.  This way if it was set in the environment (eg by a package manager) it will not be overwritten.  Also, I don't think DESTDIR should be set in a Makefile - that should be left to package managers - and if it is, it definitely needs the ?= operator, or you will simply break package management.   You can play with various environment settings with the following to see what is echos:

PREFIX ?= /usr

show-path:
	@echo would install to ${DESTDIR}${PREFIX}/path

RE: clean, do you mean clean, or uninstall?  clean should *definitely* not touch the DESTDIR.  Clean should only clean the working diretory.  Uninstall is what would remove any installed files from ${DESTDIR}.

I also wouldn't set MANDIR to prefix plus the mandir, I just set it to the mandir then invoke it in the install line as `install -Dm644 page.1 ${DESTDIR}${PREFIX}${MANDIR}/page.1`.  This may realy just a style issue and I don't know that it'd ever cause problems either way.


EDIT: You don't have much to 'clean' as nothing is built, but I now see your desire for cleaning DESTDIR ... but you are doing an earlier step wrong.  Do not install the raw man page then zip it in the installed location - this is what you want to clean up after right?  Instead, zip the man page in the working (build) directory, then only install the man.1.gz file.  You can then have a clean directive that removes the local man.1.gz file.

Last edited by Trilby (2013-02-13 12:18:16)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#4 2013-02-13 14:44:17

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,420
Website

Re: Makefile to install files rather than a PKGBUILD doing it [SOLVED]

Thanks for the reply, T.  The concept of a working directory puzzles me.

I added an 'all' target.  So now when I run `make` it will modify the source (common/profile-sync-daemon) to give it the right version.
When I run `make DESTDIR=$pkgdir" install` the DESTDIR is my $pkgdir.  Where is the working dir in this?   I am confused about how to write the uninstall target.

Here is the new Makefile:

VERSION = 5.19
PN = profile-sync-daemon

PREFIX ?= /usr
CONFDIR = /etc
CRONDIR = /etc/cron.hourly
INITDIR = /usr/lib/systemd/system
BINDIR = $(PREFIX)/bin
DOCDIR = $(PREFIX)/share/doc/$(PN)-$(VERSION)
MANDIR = $(PREFIX)/share/man/man1

all:
	@echo -e '\033[1;32mseting up\033[0m'
	@sed -i -e 's/@VERSION@/'$(VERSION)'/' common/$(PN)

install-bin:
	@echo -e '\033[1;32minstalling main script, initd and config...\033[0m'
	install -Dm644 common/psd.conf "$(DESTDIR)$(CONFDIR)/psd.conf"
	install -Dm644 init/psd.service "$(DESTDIR)$(INITDIR)/psd.service"
	install -Dm755 common/$(PN) "$(DESTDIR)$(BINDIR)/$(PN)"
	ln -s $(PN) "$(DESTDIR)$(BINDIR)/psd"

install-man:
	@echo -e '\033[1;32minstalling manpage...\033[0m'
	install -Dm644 doc/psd.1 "$(DESTDIR)$(MANDIR)/psd.1"
	gzip -9 "$(DESTDIR)$(MANDIR)/psd.1"
	ln -s psd.1.gz "$(DESTDIR)$(MANDIR)/$(PN).1.gz"

install-cron:
	@echo -e '\033[1;32minstalling cronjob...\033[0m'
	install -Dm755 common/psd.cron.hourly "$(DESTDIR)$(CRONDIR)/psd-update"

install: install-bin install-man install-cron

Last edited by graysky (2013-02-13 14:44:52)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2013-02-13 15:04:44

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,510
Website

Re: Makefile to install files rather than a PKGBUILD doing it [SOLVED]

The working directory is just that, the current working directory (cwd/pwd).  In the context of a PKGBUILD running make, the working directory would be $srcdir or source directory.  This is the directory in which any compile commands called by make are run.  When you run install common/psd.conf, for example, "common" is a subdirectory of the working directory.  If it wasn't, that would produce a file not found error.

When a Makefile builds (compiles) a program in the working directory, it can be useful to clean the compiled program or any intermediate files from the working directory after they are installed.  As an example where clean would be more useful, imagine a package that used a lexer like flex.  Flex takes a .lex, lets say example.lex, and parses it to produce example.c.  example.c is then compiled (by gcc) to produce example.o, then this is linked to produce the example binary.

Now we have example.lex (the original source), the binary, and example.c and example.o hanging around in the soruce directory.  The binary will get installed (copied) to the destination, then one might want to "clean" or remove all these no-longer-needed intermediate files.  This is the purpose of a clean directive.  Clean directives are not strictly necessary for anything - this intermediate files do no harm, they just take up space.  Clean is particularly useful for making source tarballs and reducing the disk space of source directory trees, but the absence of a clean directive will not prevent the Makefile from working perfectly well.

Uninstall directives are quite different.  As the name implies, they remove/uninstall everything that was installed by the Makefile.  I don't know the ins-and-outs of other package managers, but with a decent package management system, uninstall directives should not be needed.  Uninstall directives are mainly useful for when users manually install packaes - it gives them a way to cleanly remove them.  But this assumes the Makefile author did everything just right ... good package managers do not make this assumption.  Pacman, as an illustrative example, uses a fakeroot destdir, then pacman copies every file in this fakeroot to the real root and "remembers" each one, so the uninstall is a clean removal of every file installed.

So in short, uninstall directives are entirely superfluous when the makefile is used with a package manager, but they are still good to have to help users who chose to manually install if they chose to uninstall and still have the Makefile.  The unsintall directive should simple "undo" everything that was done.  It should delete all the files installed from ${DESTDIR}.


EDIT: I was mistaken about the zipping of the man page.  Your way works great.  I've always used gzip -c man.1 > man.1.gz so I have two files, then I install only man.1.gz.  Given this, you have no need for a clean directive, there really is nothing to clean.

Last edited by Trilby (2013-02-13 15:12:15)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#6 2013-02-13 16:45:59

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,420
Website

Re: Makefile to install files rather than a PKGBUILD doing it [SOLVED]

Thanks for the info.  I think I have this in a good state now.  At least it builds under Arch, Ubunutu, and Gentoo.

https://github.com/graysky2/profile-sync-daemon

Last edited by graysky (2013-02-13 17:07:29)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB