Question about `platforms` and `${os.platform}`
Ryan Schmidt
ryandesign at macports.org
Sat Dec 11 22:19:56 UTC 2021
On Dec 10, 2021, at 15:07, Jason Liu wrote:
> A conversation in one of my PRs has brought up an interesting question that I've been wondering about for a long time. In Portfiles, whenever I've had a test for `${os.major} <= xx`, I've typically always added an additional check for darwin in the front, i.e.:
>
> if {${os.platform} eq "darwin" && ${os.major} <= xx} {
>
> I've done it that way because I basically copied what I saw from other Portfiles, and because I get gently admonished by the committers when I forget to.
Yes, please always do that.
> But I've also always wondered why it's necessary.
Your Portfile could be parsed or accessed by non-Darwin operating systems. For example, before we took over server hosting duties in 2016, all of the server VMs including the one that generated the PortIndex files (except the actual macOS build machines) were running Linux; perhaps some day we will once again want to try using a server OS other than macOS for this task. There are even some users using Linux to test various things in MacPorts. We don't really expect many ports to be installable on other operating systems or for maintainers to test anything on other operating systems, but just take 2 seconds when you're writing an OS version conditional to think about what you're trying to express, and then express it, including checking os.platform. Typical forms include the one you mentioned:
if {${os.platform} eq "darwin" && ${os.major} <= xx}
(<, <=, ==, >=, >)
And the other one:
if {${os.platform} ne "darwin" || ${os.major} <= xx}
(<, <=, ==, >=, >)
Usually the decision about which to use comes down to whether the thing you're doing is Mac-specific or not. For example, if you were writing a conditional to use the Security framework on recent macOS and openssl on older macOS, what should happen if perchance the port is used on non-macOS? In this case, the answer is that frameworks are a Mac thing, so you would want to use openssl for non-macOS.
> I was under the impression that the `platforms darwin` line means that the entire Portfile is supposed to be valid only for `${os.platform} eq "darwin"`, no? (In other words, my understanding is that a line such as `platforms darwin freebsd openbsd` is meant to signify that "this Portfile is supposed to be valid for the listed platforms".) If that's not the case, then what is the purpose of `platforms darwin`?
The platforms line is not used by MacPorts in any way at this time, other than to display it in the output of "port info". There is a ticket about possibly using it in the future as a way to indicate which OS versions the port is compatible with, but I don't think that got beyond the idea phase.
More information about the macports-dev
mailing list