不同的实现在自动工具不同的文件 [英] Different implementations in different files in autotools

查看:162
本文介绍了不同的实现在自动工具不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个特点,富是我执行多个操作系统。有一个通用的方法来做到这一点,但如果操作系统有它的原生支持,然后我想使用。

我如何有条件地使用自动工具编译只有正确的源文件?

例如:
比方说,富是重新启动计算机。

在Linux上我会用重启(),我会提出,在reboot_reboot.c,在那里我定义myreboot()不只是调用使用正确的参数重新启动()。

在所有的BSD我会用bsd_made_up_blah_reboot(),这需要不同的参数。我会把这reboot_bsd_made_up_blah_reboot.c,从而做到这一点。 (这只是一个例子,我知道BSD系统有重新启动())

否则,使用reboot_generic.c,这只是调用系统(关机-i6 -g0)。 (是的,让我们忽略了不同关闭的二进制文件的语法)

在configure.ac我可以检查重新启动,bsd_made_up_blah_reboot,然后在Makefile.am的presence做的:

 如果HAVE_REBOOT
 blah_SOURCES + = reboot_reboot.c
其他
 如果HAVE_BSD_MADE_UP_BLAH_REBOOT
  blah_SOURCES + = reboot_bsd_made_up_blah_reboot.c
 其他
  blah_SOURCES + = reboot_generic.c
 万一
万一

我不喜欢这一点,因为(据我所知)没有否则,如果在Makefile.am,并作为实现的数量增长会得到丑陋和丑陋。

什么是做到这一点的正确方法?请记住,我想,以防止其的两个的重新启动()未来系统和bsd_made_up_blah_reboot(),所以我不能只包括


解决方案

  

我不喜欢这一点,因为(据我所知)没有否则,如果在Makefile.am,并作为实现的数量增长,如果将得到丑陋和丑陋。


但是,如果你能保证案件 HAVE_REBOOT HAVE_BSD_REBOOT (以及其他任何可能)是两两析取,你可以简单地写

 如果HAVE_REBOOT
x_SOURCES + = reboot.c
万一
如果HAVE_BSD_REBOOT
x_SOURCES + = bsd_reboot.c
万一
如果HAVE_NO_REBOOT
x_SOURCES + = generic_reboot.c
万一

(您需要定义一个configure.ac HAVE_NO_REBOOT)并不难或者,假设,例如:

 的情况下,$主机
(* -linux)
    have_reboot = 1 ;;
(* -bsd)
    have_bsd_reboot = 1 ;;
(*)
    have_no_reboot = 1 ;;
ESAC;
AM_CONDITIONAL([HAVE_REBOOT],[测试$ have_reboot= 1])
AM_CONDITIONAL([HAVE_BSD_REBOOT],[测试$ have_bsd_reboot= 1])
AM_CONDITIONAL([HAVE_NO_REBOOT],[测试$ have_no_reboot= 1])

Let's say I have a feature "foo" that I'm implementing for several operating systems. There is a generic way to do it, but if the OS has native support for it then I want to use that.

How do I conditionally compile only the right source files with autotools?

Example: Let's say "foo" is "reboot the machine".

On Linux I would use reboot(), and I would put that in reboot_reboot.c, where I define myreboot() do just call reboot() with the correct parameters.

On all the BSDs I would use bsd_made_up_blah_reboot(), which takes different parameters. I would put this in reboot_bsd_made_up_blah_reboot.c, which does this. (this is just an example, I know BSDs have reboot())

Else, use reboot_generic.c, which just calls system("shutdown -i6 -g0"). (Yes, let's ignore the syntax for different shutdown binaries)

In configure.ac I can check for the presence of "reboot", "bsd_made_up_blah_reboot", and then in Makefile.am do:

if HAVE_REBOOT
 blah_SOURCES += reboot_reboot.c
else
 if HAVE_BSD_MADE_UP_BLAH_REBOOT
  blah_SOURCES += reboot_bsd_made_up_blah_reboot.c
 else
  blah_SOURCES += reboot_generic.c
 endif
endif

I don't like this, since (as I understand it) there is no "else if" in Makefile.am, and as the number of implementations grow it will get uglier and uglier.

What's the right way to do this? Keep in mind that I want to protect against a future system having both reboot() and bsd_made_up_blah_reboot(), so I can't just include both.

解决方案

I don't like this, since (as I understand it) there is no "else if" in Makefile.am, and as the number of implementations grow if will get uglier and uglier.

But if you can guarantee that the cases HAVE_REBOOT, HAVE_BSD_REBOOT (and whatever else might be) are pairwise disjunctive, you can simply write

if HAVE_REBOOT
x_SOURCES += reboot.c
endif
if HAVE_BSD_REBOOT
x_SOURCES += bsd_reboot.c
endif
if HAVE_NO_REBOOT
x_SOURCES += generic_reboot.c
endif

(You would need to define a HAVE_NO_REBOOT in configure.ac.) Not that hard either, assuming, for example

case "$host" in
(*-linux)
    have_reboot=1;;
(*-bsd)
    have_bsd_reboot=1;;
(*)
    have_no_reboot=1;;
esac;
AM_CONDITIONAL([HAVE_REBOOT], [test "$have_reboot" = 1])
AM_CONDITIONAL([HAVE_BSD_REBOOT], [test "$have_bsd_reboot" = 1])
AM_CONDITIONAL([HAVE_NO_REBOOT], [test "$have_no_reboot" = 1])

这篇关于不同的实现在自动工具不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆