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?

rookie
  • 1,168
  • 3
  • 14
  • 25
  • The recommended practice is to use `$(MAKE)` and not call **make** by name. – Alex Cohn Mar 15 '19 at 09: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 Mar 15 '19 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 Mar 15 '19 at 19:26

1 Answers1

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.

Ken Jackson
  • 479
  • 4
  • 5