wiki:PortfileRecipes

Version 1 (modified by blb@…, 15 years ago) (diff)

New page to talk about various Portfile tricks not documented elsewhere

Portfile Recipes

Using glob results in "Cannot stat: <list of files>"

glob returns a list which is not handled by some commands (eg, xinstall); instead, wrap the command in an eval. Instead of

xinstall -m 644 [glob ${worksrcpath}/docs/*.html] ${destroot}${prefix}/share/doc/${name}

which fails with the "Cannot stat..." error, instead use

eval xinstall -m 644 [glob ${worksrcpath}/docs/*.html] ${destroot}${prefix}/share/doc/${name}

variant_isset doesn't work when variant is set

Make sure you have the variant defined, even if it is empty, in the Portfile. If all you need is to do something when the variant is set and not dedicate an entire variant section to it, variant_isset can work but you must still define the variant (or platform for things like darwin, darwin_9, etc). So just add

variant myvariant {
}

or

platform darwin 9 {
}

Then port will set the variant when selected and varaint_isset should work.

Preferred use of default_variants

Currently negating a variant (through -variant) is not remembered which means a port upgrade will not keep that negation around. This causes issues with default_variants which must be kept in mind (is there a ticket for this?).

The preferred technique is to only select a default variant when one of a set is actually needed; eg, from ImageMagick, when selecting the pixel quantum:

if {![variant_isset q8] && ![variant_isset q32]} {
    default_variants +q16
}

This also leads to preferring negative-based variants (like no_x11) as using +no_x11 is remembered, whereas if you had x11 as a variant and set it in default_variants, -x11 would not be remembered.

cvs/svn/git tag for consistency

When using one of the checkout-based fetch types (cvs, svn, git, etc) it is best to also use the accompanying tag or date to make sure everyone gets the same checkout as you originally did when creating the port. For example, slime does the following for cvs:

cvs.date        ${version}

irssi-devel does the following for svn:

svn.tag             ${version}

(note that it instead uses svn.revision as svn.tag will be deprecated in favor of svn.revision when MacPorts 1.8 is released).

Don't hardcode /opt/local

Make sure to never hardcode /opt/local anywhere as that is the default prefix for MacPorts, but other ones can be used. Many ports will either use a simple reinplace like

reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/Makefile

to replace instances of /usr/local with the right MacPorts prefix when a Makefile simply assumes /usr/local (see for example id3v2). Others, when a patch being applied needs to reference the prefix, will use a template for the prefix like @@PREFIX@@, then a similar reinplace:

reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/configure

that is then run in post-patch (see for example glib2).