简体   繁体   English

Windows和Cygwin的Makefile

[英]Makefile for Windows and Cygwin

I need to have a makefile work under Windows and Cygwin. 我需要在Windows和Cygwin下运行makefile I having problems with the makefile detecting the OS correctly and setting appropriate variables. 我在makefile正确检测操作系统并设置适当的变量时遇到问题。

The objective is to set variables for the following commands, then invoke the commands in rules using the variables: 目的是为以下命令设置变量,然后使用变量在规则中调用命令:

  • Delete file: rm in Cygwin, del in Windows. 删除文件:Cygwin中的rm ,Windows中的del
  • Remove directory: rmdir (different parameters in Cygwin and Windows) 删除目录: rmdir (Cygwin和Windows中的不同参数)
  • Copy file: cp in Cygwin, copy in Windows. 复制文件:Cygwin中为cp ,Windows中为copy
  • Testing for file existance: test in Cygwin, IF EXIST in Windows. 测试文件是否存在:在Cygwin中进行test ,在Windows中为IF EXIST
  • Listing contents of a file: cat in Cygwin, type in Windows. 列出文件内容:在Cygwin中为cat ,在Windows中type

Here is my attempt, which always uses the else clause: 这是我的尝试,该尝试始终使用else子句:

OS_KIND = $(OSTYPE) #OSTYPE is an environment variable set by Cygwin.
ifeq ($(OS_KIND), cygwin)
 ENV_OS = Cygwin
 RM = rm -f
 RMDIR = rmdir -r
 CP = cp
 REN = mv
 IF_EXIST = test -a
 IF_NOT_EXIST = ! test -a
 LIST_FILE = cat
else
 ENV_OS = Win_Cmd
 RM = del -f -Q
 RMDIR = rmdir /S /Q
 IF_EXIST = if exist
 IF_NOT_EXIST = if not exist
 LIST_FILE = type
endif

I'm using the forward slash character, '/', as a directory separator. 我使用正斜杠字符“ /”作为目录分隔符。 This is a problem with the Windows command, as it is interpreting it as program argument rather than a separator. Windows命令存在问题,因为它将命令解释为程序参数而不是分隔符。 Anybody know how to resolve this issue? 有人知道如何解决这个问题吗?

I am using make with Mingw in both Windows Console and Cygwin. 我在Windows Console和Cygwin中都使用Mingw的make。

I highly recommend you move to CMake to automatically generate your Makefiles. 我强烈建议您使用CMake自动生成Makefile。 These problems have all been solved, and it supports MingW, MSYS and Cygwin. 这些问题均已解决,并且支持MingW,MSYS和Cygwin。

RM:           file(REMOVE [file1 ...])
RMDIR:        file(REMOVE_RECURSE [file1 ...]) # (not quite the same functionality; deletes files, too)
CP:           file(COPY files... DESTINATION...)
REN:          file(RENAME <oldname> <newname>)
IF_EXIST:     if(EXISTS file-name)
IF_NOT_EXIST: if(NOT EXISTS file-name)
LIST_FILE:    file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])

All your paths are automagically converted to DOS-style if you are generating MinGW Makefiles. 如果要生成MinGW Makefile,则所有路径都会自动转换为DOS样式。 It's a beautiful thing. 这是一件美丽的事。

And, finally, it makes the rest of your Makefile stuff much simpler, too. 最后,它也使Makefile的其余部分变得更加简单。

CMake , suggested by Matt B., is one of the right answers to this question. Matt B.建议的CMake是此问题的正确答案之一。 Other right answers are SCons , Jam , and Bakefile . 其他正确的答案是SConsJamBakefile

Since you're using Cygwin, Autoconf + Automake also gives you the tools to solve your problem. 由于您使用的是Cygwin,因此Autoconf + Automake还为您提供了解决问题的工具。 It's more of a toolkit for solving the problem than an out-of-the-box solution, though. 但是,它不是现成的解决方案,更是用于解决问题的工具包。

The overarching point is to move up a layer of abstraction so you can support lots of build systems from only a single source. 最重要的一点是向上一层抽象,这样您就可以仅从一个来源就支持许多构建系统。

Accurately detecting the build platform is the tricky part. 准确检测构建平台是棘手的部分。 This is how I do it. 这就是我的方法。

ifeq '$(findstring ;,$(PATH))' ';'
    UNAME := Windows
else
    UNAME := $(shell uname 2>/dev/null || echo Unknown)
    UNAME := $(patsubst CYGWIN%,Cygwin,$(UNAME))  # CYGWIN_NT-10.0 -> Cygwin
endif

The UNAME variable is set to Linux, Cygwin, Windows, FreeBSD, NetBSD (or presumably Solaris, Darwin, OpenBSD, AIX, HP-UX), or Unknown. UNAME变量设置为Linux,Cygwin,Windows,FreeBSD,NetBSD(或可能是Solaris,Darwin,OpenBSD,AIX,HP-UX)或未知。 It can then be compared throughout the remainder of the Makefile to separate any OS-sensitive variables and commands. 然后可以在整个Makefile的其余部分进行比较,以分隔所有OS敏感变量和命令。

I discuss this further in another thread 我将在另一个主题中对此进行进一步讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM