How can I get the path of NMAKE Makefile?

187 Views Asked by At

In order to support building an EXE file from any directory, I would like to make my paths relative to the location of the Makefile.

While I know how to do so with GNU Makefile:

$(dir $(realpath $(lastword $(MAKEFILE_LIST))))

How can I get the path of Microsoft NMAKE Makefile? Is it even possible to achieve the same behavior?

I know that the Makefile variable MAKEDIR can give me the path from where the nmake command was executed, and that at the nmake command the /F variable holds the path from there to the location of the Makefile, but I don't know how to access the value of /F inside the Makefile in order to combine the two.

(Please don't suggest me to use CMake instead.)

1

There are 1 best solutions below

4
Sreeram Nair On

Using the $(MAKEFLAGS) macro, you can access the value of the /F option, which specifies the path to the Makefile.

Example

# Extract the path from MAKEFLAGS and assign it to MAKEFILE_PATH
!IF "$(MAKEFLAGS)" != "" && "$(MAKEFLAGS)" != "-"
MAKEFILE_PATH = $(subst /,$(NULL),$(dir $(patsubst /F:,%,$(findstring /F:,$(MAKEFLAGS)))))
!ENDIF

# Set the default value of MAKEFILE_PATH if it's empty
!IFNDEF MAKEFILE_PATH
MAKEFILE_PATH = .
!ENDIF

# Usage example: combine MAKEFILE_PATH with other paths
SOME_DIR = $(MAKEFILE_PATH)\some\directory

# Print the value of MAKEFILE_PATH for verification
all:
    @echo $(MAKEFILE_PATH)