操作系统检测makefile

sam*_*moz 237 makefile os-agnostic os-detection

我经常在几台不同的计算机和几种不同的操作系统上工作,这些操作系统是Mac OS X,Linux或Solaris.对于我正在进行的项目,我从远程git存储库中提取代码.

无论我在哪个终端,我都希望能够处理我的项目.到目前为止,我已经找到了通过每次切换计算机时更改makefile来绕过操作系统更改的方法.然而,这是乏味的,并引起一堆头痛.

如何修改我的makefile,以便它检测我正在使用哪个操作系统并相应地修改语法?

这是makefile:

cc = gcc -g
CC = g++ -g
yacc=$(YACC)
lex=$(FLEX)

all: assembler

assembler: y.tab.o lex.yy.o
        $(CC) -o assembler y.tab.o lex.yy.o -ll -l y

assembler.o: assembler.c
        $(cc) -o assembler.o assembler.c

y.tab.o: assem.y
        $(yacc) -d assem.y
        $(CC) -c y.tab.c

lex.yy.o: assem.l
        $(lex) assem.l
        $(cc) -c lex.yy.c

clean:
        rm -f lex.yy.c y.tab.c y.tab.h assembler *.o *.tmp *.debug *.acts
Run Code Online (Sandbox Code Playgroud)

Tre*_*son 266

这里有很多好的答案,但我想分享一个更完整的例子:

  • 不假设uname在Windows上存在
  • 还检测处理器

这里定义的CCFLAGS不一定是推荐的或理想的; 它们正是我添加OS/CPU自动检测的项目恰好正在使用的项目.

ifeq ($(OS),Windows_NT)
    CCFLAGS += -D WIN32
    ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
        CCFLAGS += -D AMD64
    else
        ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
            CCFLAGS += -D AMD64
        endif
        ifeq ($(PROCESSOR_ARCHITECTURE),x86)
            CCFLAGS += -D IA32
        endif
    endif
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        CCFLAGS += -D LINUX
    endif
    ifeq ($(UNAME_S),Darwin)
        CCFLAGS += -D OSX
    endif
    UNAME_P := $(shell uname -p)
    ifeq ($(UNAME_P),x86_64)
        CCFLAGS += -D AMD64
    endif
    ifneq ($(filter %86,$(UNAME_P)),)
        CCFLAGS += -D IA32
    endif
    ifneq ($(filter arm%,$(UNAME_P)),)
        CCFLAGS += -D ARM
    endif
endif
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是,`PROCESSOR_ARCHITECTURE` envvar似乎是虚拟化的,具体取决于进程是32位还是64位.因此,如果你的`make`是32位并且你正在尝试构建一个64位应用程序,它将失败.将它与`PROCESSOR_ARCHITEW6432`结合使用对我有用(见[this](http://stackoverflow.com/questions/1738985/why-processor-architecture-always-returns-x86-instead-of-amd64),[即](http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx)) (8认同)
  • @JanusTroelsen:在非Windows系统上是否设置了"OS"并不重要.将处理设置为与空相同,这将导致跳转到基于"uname"的块.你只需要在那里添加一个FreeBSD检查. (6认同)
  • 如果`make`团队用os和arch添加几个魔术变量会很好,可能太麻烦了. (4认同)
  • 这也打破了osx。`/ bin / sh:-c:第0行:意外标记附近的语法错误,Windows_NT'/ bin / sh:-c:第0行:'ifeq(,Windows_NT)'make:*** [os]错误2` (3认同)
  • `if`、`else` 和 `endif` *不得*缩进(在我的实验中)。我也只让它在目标块之外*工作 (3认同)

dbr*_*708 117

没有参数的uname命令(http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/uname.1.html)应该告诉您操作系统名称.我会使用它,然后根据返回值创建条件.

UNAME := $(shell uname)

ifeq ($(UNAME), Linux)
# do something Linux-y
endif
ifeq ($(UNAME), Solaris)
# do something Solaris-y
endif
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果将其放在Maketarget中,则不得缩进. (4认同)
  • 是不是特定于GNU Make的":="语法? (4认同)
  • 我尝试时,UNAME:= $(shell uname)命令失败. (3认同)

oli*_*bre 35

使用两个简单的技巧检测操作系统:

  • 首先是环境变量 OS
  • 然后uname命令
ifeq ($(OS),Windows_NT)     # is Windows_NT on XP, 2000, 7, Vista, 10...
    detected_OS := Windows
else
    detected_OS := $(shell uname)  # same as "uname -s"
endif
Run Code Online (Sandbox Code Playgroud)

或者更安全的方式,如果不是在Windows上并且uname不可用:

ifeq ($(OS),Windows_NT) 
    detected_OS := Windows
else
    detected_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
endif
Run Code Online (Sandbox Code Playgroud)

如果你想区分Cygwin/MinGW/MSYS/Windows,肯杰克逊提出了一个有趣的选择.看到他的答案看起来像这样:

ifeq '$(findstring ;,$(PATH))' ';'
    detected_OS := Windows
else
    detected_OS := $(shell uname 2>/dev/null || echo Unknown)
    detected_OS := $(patsubst CYGWIN%,Cygwin,$(detected_OS))
    detected_OS := $(patsubst MSYS%,MSYS,$(detected_OS))
    detected_OS := $(patsubst MINGW%,MSYS,$(detected_OS))
endif
Run Code Online (Sandbox Code Playgroud)

然后您可以根据以下内容选择相关内容detected_OS:

ifeq ($(detected_OS),Windows)
    CFLAGS += -D WIN32
endif
ifeq ($(detected_OS),Darwin)        # Mac OS X
    CFLAGS += -D OSX
endif
ifeq ($(detected_OS),Linux)
    CFLAGS   +=   -D LINUX
endif
ifeq ($(detected_OS),GNU)           # Debian GNU Hurd
    CFLAGS   +=   -D GNU_HURD
endif
ifeq ($(detected_OS),GNU/kFreeBSD)  # Debian kFreeBSD
    CFLAGS   +=   -D GNU_kFreeBSD
endif
ifeq ($(detected_OS),FreeBSD)
    CFLAGS   +=   -D FreeBSD
endif
ifeq ($(detected_OS),NetBSD)
    CFLAGS   +=   -D NetBSD
endif
ifeq ($(detected_OS),DragonFly)
    CFLAGS   +=   -D DragonFly
endif
ifeq ($(detected_OS),Haiku)
    CFLAGS   +=   -D Haiku
endif
Run Code Online (Sandbox Code Playgroud)

笔记:


下面我提供一个使用makegcc构建共享库的完整示例:*.so或者*.dll取决于平台.这个例子尽可能简单易懂.

要在Windows上安装make,gcc请参阅CygwinMinGW.

我的例子基于五个文件

 ??? lib
 ?   ??? Makefile
 ?   ??? hello.h
 ?   ??? hello.c
 ??? app
     ??? Makefile
     ??? main.c
Run Code Online (Sandbox Code Playgroud)

提醒: Makefile使用制表缩进.在示例文件下面复制粘贴时的注意事项.

这两个Makefile文件

1. lib/Makefile

ifeq ($(OS),Windows_NT)
    uname_S := Windows
else
    uname_S := $(shell uname -s)
endif

ifeq ($(uname_S), Windows)
    target = hello.dll
endif
ifeq ($(uname_S), Linux)
    target = libhello.so
endif
#ifeq ($(uname_S), .....) #See https://stackoverflow.com/a/27776822/938111
#    target = .....
#endif

%.o: %.c
    gcc  -c $<  -fPIC  -o $@
    # -c $<  => $< is first file after ':' => Compile hello.c
    # -fPIC  => Position-Independent Code (required for shared lib)
    # -o $@  => $@ is the target => Output file (-o) is hello.o

$(target): hello.o
    gcc  $^  -shared  -o $@
    # $^      => $^ expand to all prerequisites (after ':') => hello.o
    # -shared => Generate shared library
    # -o $@   => Output file (-o) is $@ (libhello.so or hello.dll)
Run Code Online (Sandbox Code Playgroud)

2. app/Makefile

ifeq ($(OS),Windows_NT)
    uname_S := Windows
else
    uname_S := $(shell uname -s)
endif

ifeq ($(uname_S), Windows)
    target = app.exe
endif
ifeq ($(uname_S), Linux)
    target = app
endif
#ifeq ($(uname_S), .....) #See https://stackoverflow.com/a/27776822/938111
#    target = .....
#endif

%.o: %.c
    gcc  -c $< -I ../lib  -o $@
    # -c $<     => compile (-c) $< (first file after :) = main.c
    # -I ../lib => search headers (*.h) in directory ../lib
    # -o $@     => output file (-o) is $@ (target) = main.o

$(target): main.o
    gcc  $^  -L../lib  -lhello  -o $@
    # $^       => $^ (all files after the :) = main.o (here only one file)
    # -L../lib => look for libraries in directory ../lib
    # -lhello  => use shared library hello (libhello.so or hello.dll)
    # -o $@    => output file (-o) is $@ (target) = "app.exe" or "app"
Run Code Online (Sandbox Code Playgroud)

要了解更多信息,请阅读cfi指出的自动变量文档.

源代码

- lib/hello.h

#ifndef HELLO_H_
#define HELLO_H_

const char* hello();

#endif
Run Code Online (Sandbox Code Playgroud)

- lib/hello.c

#include "hello.h"

const char* hello()
{
    return "hello";
}
Run Code Online (Sandbox Code Playgroud)

- app/main.c

#include "hello.h" //hello()
#include <stdio.h> //puts()

int main()
{
    const char* str = hello();
    puts(str);
}
Run Code Online (Sandbox Code Playgroud)

构建

修复Makefile(通过一个制表替换前导空格)的复制粘贴.

> sed  's/^  */\t/'  -i  */Makefile
Run Code Online (Sandbox Code Playgroud)

make两个平台上的命令都是相同的.给定的输出是在类Unix操作系统上:

> make -C lib
make: Entering directory '/tmp/lib'
gcc  -c hello.c  -fPIC  -o hello.o
# -c hello.c  => hello.c is first file after ':' => Compile hello.c
# -fPIC       => Position-Independent Code (required for shared lib)
# -o hello.o  => hello.o is the target => Output file (-o) is hello.o
gcc  hello.o  -shared  -o libhello.so
# hello.o        => hello.o is the first after ':' => Link hello.o
# -shared        => Generate shared library
# -o libhello.so => Output file (-o) is libhello.so (libhello.so or hello.dll)
make: Leaving directory '/tmp/lib'

> make -C app
make: Entering directory '/tmp/app'
gcc  -c main.c -I ../lib  -o main.o
# -c main.c => compile (-c) main.c (first file after :) = main.cpp
# -I ../lib => search headers (*.h) in directory ../lib
# -o main.o => output file (-o) is main.o (target) = main.o
gcc  main.o  -L../lib  -lhello  -o app
# main.o   => main.o (all files after the :) = main.o (here only one file)
# -L../lib => look for libraries in directory ../lib
# -lhello  => use shared library hello (libhello.so or hello.dll)
# -o app   => output file (-o) is app.exe (target) = "app.exe" or "app"
make: Leaving directory '/tmp/app'
Run Code Online (Sandbox Code Playgroud)

运行

应用程序需要知道共享库的位置.

在Windows上,一个简单的解决方案是复制应用程序所在的库:

> cp -v lib/hello.dll app
`lib/hello.dll' -> `app/hello.dll'
Run Code Online (Sandbox Code Playgroud)

在类Unix操作系统上,您可以使用LD_LIBRARY_PATH环境变量:

> export LD_LIBRARY_PATH=lib
Run Code Online (Sandbox Code Playgroud)

在Windows上运行该命令:

> app/app.exe
hello
Run Code Online (Sandbox Code Playgroud)

在类Unix操作系统上运行命令:

> app/app
hello
Run Code Online (Sandbox Code Playgroud)


phs*_*sym 18

我最近在做实验,以便回答我问自己的问题.以下是我的结论:

因为在Windows中,您无法确定该uname命令是否可用,您可以使用gcc -dumpmachine.这将显示编译器目标.

uname如果要进行某些交叉编译,使用时可能还会出现问题.

以下是可能输出的示例列表gcc -dumpmachine:

  • mingw32的
  • i686的-PC-的cygwin
  • x86_64的-红帽Linux的

您可以在makefile中检查结果,如下所示:

SYS := $(shell gcc -dumpmachine)
ifneq (, $(findstring linux, $(SYS)))
 # Do Linux things
else ifneq(, $(findstring mingw, $(SYS)))
 # Do MinGW things
else ifneq(, $(findstring cygwin, $(SYS)))
 # Do Cygwin things
else
 # Do things for others
endif
Run Code Online (Sandbox Code Playgroud)

它对我来说效果很好,但我不确定这是获得系统类型的可靠方法.至少它对MinGW是可靠的,这就是我所需要的,因为它不需要在Windows中使用uname命令或MSYS包.

综上所述,uname给你的系统,你正在编译,并gcc -dumpmachine为您提供了系统您的编译.

  • @Shahbaz MinGW设置可以安装MSYS(包含uname),但它是可选的.仍然可以找到只有MinGW gcc工具的系统 (2认同)

Jes*_*erE 16

git的makefile文件中对如何管理没有了autoconf/automake的,但对unixy平台众多仍然工作的例子不胜枚举.

  • 知道Git不会以某种方式使用Autofools让我觉得我厌恶它们是合理的...... (13认同)
  • "Autofools"?这是故意错字吗?:) (11认同)
  • 英语是一种如此不精确的语言.怎么样:`if(!usesAutotools(git))aversionTo(autotools)= justified;`我还要澄清它只是我反对的工具.我确信Autotools的人都是好人. (8认同)
  • 它是.但在第二个想法,我认为我更喜欢"Autostools".:d (6认同)

Ken*_*son 11

更新:我现在认为这个答案已经过时了.我发布了一个新的完美解决方案.

如果您的makefile可能在非Cygwin Windows上运行,则uname可能无法使用.这很尴尬,但这是一个潜在的解决方案.您必须首先检查Cygwin以排除它,因为它的PATH环境变量中也有WINDOWS .

ifneq (,$(findstring /cygdrive/,$(PATH)))
    UNAME := Cygwin
else
ifneq (,$(findstring WINDOWS,$(PATH)))
    UNAME := Windows
else
    UNAME := $(shell uname -s)
endif
endif
Run Code Online (Sandbox Code Playgroud)


Ken*_*son 8

我终于找到了为我解决这个问题的完美解决方案。

ifeq '$(findstring ;,$(PATH))' ';'
    UNAME := Windows
else
    UNAME := $(shell uname 2>/dev/null || echo Unknown)
    UNAME := $(patsubst CYGWIN%,Cygwin,$(UNAME))
    UNAME := $(patsubst MSYS%,MSYS,$(UNAME))
    UNAME := $(patsubst MINGW%,MSYS,$(UNAME))
endif
Run Code Online (Sandbox Code Playgroud)

UNAME 变量设置为 Linux、Cygwin、MSYS、Windows、FreeBSD、NetBSD(或可能是 Solaris、Darwin、OpenBSD、AIX、HP-UX)或未知。然后可以在整个 Makefile 的其余部分进行比较,以分离任何操作系统敏感的变量和命令。

关键是 Windows 使用分号来分隔 PATH 变量中的路径,而其他所有人都使用冒号。(可以在名称中创建一个带有 ';' 的 Linux 目录并将其添加到 PATH 中,这会破坏这一点,但是谁会这样做呢?)这似乎是检测本机 Windows 的风险最低的方法,因为它不需要shell调用。Cygwin 和 MSYS PATH 使用冒号,因此需要为它们调用uname

请注意,OS 环境变量可用于检测 Windows,但不能用于区分 Cygwin 和本机 Windows。测试引号的回显有效,但它需要一个 shell 调用。

不幸的是,Cygwin 在uname的输出中添加了一些版本信息,因此我添加了“patsubst”调用以将其更改为“Cygwin”。此外,MSYS 的 uname 实际上有三个可能的输出,以 MSYS 或 MINGW 开头,但我也使用 patsubst 将所有输出都转换为“MSYS”。

如果区分路径上带有和不带有一些 uname.exe 的本机 Windows 系统很重要,则可以使用此行代替简单的赋值:

UNAME := $(shell uname 2>NUL || echo Windows)
Run Code Online (Sandbox Code Playgroud)

当然,在所有情况下都需要GNU make,或者另一个支持所使用函数的make


Dou*_*der 7

这是GNU的automake/autoconf旨在解决的工作.您可能想要调查它们.

或者,您可以在不同的平台上设置环境变量,并使Makefile成为条件.

  • 我强烈建议不要使用automake/autoconf.它们使用起来很繁琐,会给文件增加很多开销,也会增加构建时间.它们只是增加了复杂性,通常效果很小(系统之间仍然没有可移植性). (11认同)
  • 我刚刚花了几天时间学习如何让“make”来做我想做的事情。我现在也想进入 automake/autoconf 吗?- 不。_可以_在makefile中完成的事情当然应该在makefile中完成,只要这样我就不会每次想要修改编译和链接时都有几个停止点。 (3认同)
  • 我不会要求无用的依赖,并改变我的整个构建系统只是为了找出它正在编译的操作系统. (2认同)

Huc*_*kle 6

我今天遇到了这个问题,我在Solaris上需要它,所以这是一个POSIX标准的方法(非常接近).

#Detect OS
UNAME = `uname`

# Build based on OS name
DetectOS:
    -@make $(UNAME)


# OS is Linux, use GCC
Linux: program.c
    @SHELL_VARIABLE="-D_LINUX_STUFF_HERE_"
    rm -f program
    gcc $(SHELL_VARIABLE) -o program program.c

# OS is Solaris, use c99
SunOS: program.c
    @SHELL_VARIABLE="-D_SOLARIS_STUFF_HERE_"
    rm -f program
    c99 $(SHELL_VARIABLE) -o program program.c
Run Code Online (Sandbox Code Playgroud)


Sam*_*uel 6

这是一个简单的解决方案,用于检查您是否在 Windows 或 posix 类(Linux/Unix/Cygwin/Mac)环境中:

ifeq ($(shell echo "check_quotes"),"check_quotes")
   WINDOWS := yes
else
   WINDOWS := no
endif
Run Code Online (Sandbox Code Playgroud)

它利用了类似 posix 和 Windows 环境中都存在 echo 的事实,并且在 Windows 中,shell 不会过滤引号。

  • 这个“解决方案”适用于所有环境,但我的观点是,这绝对不能安全地检测窗口。如果我想设置“-mwindows”标志或在“.dll”或“.so”之间进行选择,则会失败。 (2认同)