2

I need to have a makefile work under Windows and Cygwin. I having problems with the makefile detecting the OS correctly and setting appropriate variables.

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.
  • Remove directory: rmdir (different parameters in Cygwin and Windows)
  • Copy file: cp in Cygwin, copy in Windows.
  • Testing for file existance: test in Cygwin, IF EXIST in Windows.
  • Listing contents of a file: cat in Cygwin, type in Windows.

Here is my attempt, which always uses the else clause:

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. Anybody know how to resolve this issue?

I am using make with Mingw in both Windows Console and Cygwin.

3 Answers 3

3

I highly recommend you move to CMake to automatically generate your Makefiles. These problems have all been solved, and it supports MingW, MSYS and 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. It's a beautiful thing.

And, finally, it makes the rest of your Makefile stuff much simpler, too.

1

CMake, suggested by Matt B., is one of the right answers to this question. Other right answers are SCons, Jam, and Bakefile.

Since you're using Cygwin, Autoconf+Automake also gives you the tools to solve your problem. 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.

5
  • The company is using makefiles. Is there any way to do what I want using a makefile? I don't know if I can convince the company to use Bakefile or CMake. Jun 19, 2010 at 0:56
  • 1
    If you can't convince the company to add a layer of abstraction to solve a real problem, you're in bigger trouble than you may know. Hacking a problem in place instead of moving to a higher level where both problems become one problem that's easier to solve is well established software development practice. A company that ignores this principle is like to end up with a teetering pile of technology that falls over frequently. Jun 19, 2010 at 4:32
  • While I agree in principle that adding a layer of abstraction is often worthwhile, and that it's a common practice in software development, it's not always worthwhile. Adding a layer means learning an additional language (CMake), while typically not being able to drop the language of Makefiles. And the problem with abstraction layers is that they're usually a bit leaky (joelonsoftware.com/articles/leakyabstractions.html); so then you have to debug at two levels instead of one. So there are certainly times when it's better not to add a layer.
    – LarsH
    Dec 6, 2013 at 16:38
  • @LarsH: The alternative is to learn an entirely separate build system for each platform, since Makefile is not portable in and of itself. To make a Makefile portable, you have to build layers on top of it like Automake, CMake, SCons, Bakefile... Dec 6, 2013 at 23:00
  • I would agree that using CMake or the like is probably a better choice in this case, but it's not true that you have to learn "an entirely separate build system" otherwise. You can jerry-rig the unportable parts of Makefile. In this case it comes down to a choice of two manually maintained versions of a Makefile (one for DOS and one for Cygwin), or two automatically generated versions, using a meta-build system. In both cases you have to understand approximately 1.2 build systems.
    – LarsH
    Dec 10, 2013 at 16:01
0

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. It can then be compared throughout the remainder of the Makefile to separate any OS-sensitive variables and commands.

I discuss this further in another thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.