1

I've come across this question whenever I want a make command to display a file that resulted from following a particular recipe using the operating system's default application for opening that type of file.

For example... I work primarily in Linux. I can generate and view documentation of my code with make docs where I have put the following in my Makefile:

docs:
     cd ${DIR_WORKING}/doc/; doxygen Doxyfile
     xdg-open ${DIR_WORKING}/doc/html/index.html &

I want to use make docs in Windows and have the same effect. I could alternatively use a user-assigned variable like a $(OPEN) in place of xdg-open. Is there a way to open a file using the default program in Windows and Linux without requiring the user to modify the Makefile? I've never used Autoconf for my codes and hope there is a solution that doesn't depend on going in that direction.

Andrew
  • 183
  • 1
  • 2
  • 9

2 Answers2

5

You could consider doing OS detection and in your Makefile initialize the OPEN variable depending on the OS.

ifeq ($(OS),Windows_NT)
    OPEN := start
else
    UNAME := $(shell uname -s)
    ifeq ($(UNAME),Linux)
        OPEN := xdg-open
    endif
    ...

In your target:

docs:
     cd ${DIR_WORKING}/doc/; doxygen Doxyfile
     $(OPEN) ${DIR_WORKING}/doc/html/index.html &

I looked at the following answers for some inspiration with this one.

OS detecting makefile

Open file from the command line on Windows

Community
  • 1
  • 1
jacob
  • 4,656
  • 1
  • 23
  • 32
0

Here is a Makefile snippet using MAKE_HOST as per user657267.

LINUX:=$(findstring linux,$(MAKE_HOST))

ifeq ($(LINUX),linux)
VDSO_PARSE:=parse_vdso.c
endif

In my case, I was testing time functionality under msys and wsl2 and wanted to minimize library overhead by calling get_time_of_day directly. For the OP, the OS by itself is not enough to guarantee that applications are installed, but this is conceptually as good as the accepted answer and maybe useful to someone.

artless noise
  • 21,212
  • 6
  • 68
  • 105