1

I am trying to write a top level Makefile that will enter a directory and use the make.exe present in it to make a target. The project is based in Eclipse on a vendor SDK(hence bundling of the make binary). I wanted to create a generic Makefile that could work on both Windows cmd and WSL (Windows linux). I have been able to kind of get it to work individually, but I am unsure how to make it work irrespective of who is using which platform to run this makefile.

For Windows, here is what worked:

all:
     cd DIR && make.exe TARGET

On WSL, I use this command:

all:
    cd DIR && ./make.exe TARGET

I had to do this as the sub makefile checks if the binary used is actually the one bundled, else throws an error. How can I make this Makefile generic?

3
  • The recommended practice is to use $(MAKE) and not call make by name.
    – Alex Cohn
    Commented Mar 15, 2019 at 9:21
  • I agree, however the SDK has a bundled make and using $(MAKE) uses my system's make binary and build fails because of a check in the SDK that only wants to build with the bundled binary
    – rookie
    Commented Mar 15, 2019 at 16:08
  • The main advantage of $(MAKE) is that it reuses the make executable that runs the top-level. But if you use system's native make there, this is no help. IMHO, this is kind of abuse of make. To achieve the same result in a more direct manner, I would recommend to wrap this cd … make in a shell script / batch file.
    – Alex Cohn
    Commented Mar 15, 2019 at 19:26

1 Answer 1

1

You may need to have the Makefile detect which platform it's being used on and execute separate commands. Here's how you can do that.

ifeq '$(findstring ;,$(PATH))' ';'
    UNAME := Windows
else
    UNAME := $(shell uname 2>/dev/null || echo Unknown)
endif

The UNAME variable is set to Linux, Windows or Unknown. It can then be compared throughout the remainder of the Makefile to separate any OS-sensitive variables and commands.

The key is that Windows uses semicolons to separate paths in the PATH variable whereas Linux uses colons.

Of course, GNU make is required, or another make which supports the functions used.

I posted a more complete version of this solution that handles Cygwin and MSYS2 here.

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