我想概括一个构建系统,以便在多个(有些相似)平台上进行编译。确定运行 shell 脚本或 Makefile 的主机类型的好方法是什么?我想区分 mac 和 linux,以及不同的特定 Linux 发行版(例如 RHEL、Ubuntu)。 Cygwin 对我来说并不重要,但如果您将其包含在您的回复中,我相信其他人会发现它很有价值。
其基本原理可能包括使用主机类型来获取和安装正确版本的二进制包,因为这样做比从源代码编译更方便。此外,一些商业软件是针对特定发行版进行二进制打包的,因此部分动机是获取正确的二进制文件。
谢谢,
SetJmp
I would like to generalize a build system to compile on several (somewhat similar) platforms. What is a good method for determining the type of host that the shell script or Makefile is running on. I would like to distinguish between mac and linux, but also different specific distributions of linux (e.g. RHEL, Ubuntu). Cygwin is not important for me, but if you include it in your response I am sure others will find it valuable.
The rationale may include using the host type to fetch and install the correct versions of binary packages when it is more convenient to do so than compile from source. In addition, some commercial software is binary-packaged for specific distros, so part of the motivation is to grab the right binary.
Thanks,
SetJmp
发布评论
评论(5)
自动工具来救援。它有大量的宏可以帮助你做这类事情。
http://www.lrde.epita.fr/~adl/autotools.html
Autotools to the rescue. It has tons of macros that help you do this kind of stuff.
http://www.lrde.epita.fr/~adl/autotools.html
uname -a 来区分主要的 *nix 变体
不太确定区分 red hat 和 ubuntu 的最佳方法是什么 - 可以寻找包管理工具并查询已安装的包,最终帮助您缩小不同的 debian 衍生品的范围,等等。可能有一些东西不过更明显和更前面。
uname -a to distinguish major *nix variants
Not so sure what the best way to distinguish red hat from ubuntu would be - could look for package managing tools and query installed packages, eventually helping you narrow down different debian derivatives, etc. There's probably something more obvious and up front though.
Linux 变体通常将发行版信息存储在 /etc/issue 中。
大多数内核会将信息放在 /proc/version 中
linux variants generally store distro information in /etc/issue.
most kernels will put info in /proc/version
这并不完全简单。您可以使用
uname
找出一般参数,但区分发行版是一项更困难的任务。也许您应该考虑使用 autoconf 之类的东西来概括您的构建系统?It's not completely straightforward. You can use
uname
to find out the general parameters but to differentiate between distributions is a harder task. Maybe you should consider using something like autoconf to generalise your build system?以防万一您使用的是 Qt,有一组非常好的定义 Q_OS_*,可以引导您找到正在编译的操作系统:
它们在 QtGlobal。甚至还有一些定义可以帮助您确定使用的编译器 Q_CC_* 或目标窗口系统 Q_WS_*。
但是,如果您不使用 Qt 并且想要使用通用方法,那么您很可能必须退回到 Autotools 包或 CMake。
确定 Linux 发行版相当棘手,但并不难。您首先必须弄清楚您关心哪些发行版,然后进行各种特定于发行版的文件/配置检查,如 此示例适用于您选择的版本,因为您无法真正支持所有可用的 Linux 发行版。 :-)
至于 Mac 方面,我会让 Mac 专家来回答,但这应该不会那么难,因为至少多样性问题是不可能的。
Just in case you're using Qt, there's this really nice set of defines, Q_OS_*, that guide you to the Operating System you're compiling on:
They are defined in QtGlobal. There are even defines that help you figure out the compiler used Q_CC_* or the target Windowing System Q_WS_*.
But if you're not using Qt and want to go for a generic method, you most likely have to fall back to the Autotools package or CMake.
Determining Linux distributions is pretty tricky, but not hard. You first have to figure out what distributions you care about and then make all kinds of distribution specific file/configuration checks like in this example for the ones you've chosen, since you can't really support all of the myriad of Linux distros available out the. :-)
As for the Mac side i'll let the Mac experts answer, but it shouldn't be that hard, since at least the diversity issue is out of the question.