Makefile: test directories for existence and will first find

I am using a makefile to compile a MATLAB / C project. Typically, MATLAB can be installed in several standard locations, depending on its version, for example. /Applications/MATLAB_2012b.app , /Applications/MATLAB_2013a.app , etc.

What is the easiest way to see if these paths exist one by one and take the first one found? There will be at least five values ​​that I would like to check. I found the syntax of $(wildcard filename) , but I was wondering if there is anything shorter than writing them down one by one.


Clarifications on request. I can combine something working, but I am sure that this is far from the best / most concise way to do this. Is there a better way?

 ifeq ($(MATLAB),) ifneq ($(wildcard /Applications/MATLAB_2011b.app),) MATLAB = /Applications/MATLAB_2011b.app endif endif ifeq ($(MATLAB),) ifneq ($(wildcard /Applications/MATLAB_2012a.app),) MATLAB = /Applications/MATLAB_2012a.app endif endif ifeq ($(MATLAB),) ifneq ($(wildcard /Applications/MATLAB_2012b.app),) MATLAB = /Applications/MATLAB_2012b.app endif endif ifeq ($(MATLAB),) ifneq ($(wildcard /Applications/MATLAB_2013a.app),) MATLAB = /Applications/MATLAB_2013a.app endif endif 
+4
source share
2 answers

You can get the first such directory if it exists

 MATLAB_DIR := $(firstword $(wildcard /Applications/MATLAB_*.app)) 

If it does not exist, the variable will be empty

 ifeq (,$(MATLAB_DIR)) $(error Matlab not found) endif 

If your path has spaces, you can use the shell instead. Maybe something like this:

 MATLAB_DIR := $(shell ls -d /Applications/MATLAB_*.app | tail -n 1) 
+1
source

To answer your question regarding spaces in file / directory names, most Makefile functions do not handle spaces in a file name, and most of them do not even respect the way \ escaping spaces.

A few variables, such as $@ , $< , $% and the wildcard function, seem to handle the escape sequence \ as space correctly, but most other variables and makefile functions, such as firstword , etc. cannot handle spaces.

Say, if you find a valid directory in a wildcard, it would be impossible to distinguish 2 valid directories in the list from one directory with spaces in the path.

If you use Make files, I would suggest avoiding as many spaces as possible.

But there are always workarounds;) Assuming you are in a * nix environment with the find and sort commands available, this seems to work.

 BASE_DIR_NAME := . MATLAB_DIR := $(subst $(NULL) ,\ ,$(shell find $(BASE_DIR_PATH) -mindepth 1 -maxdepth 1 -name 'MATLAB_*.app' -type d -print | sort | head -1)) default: @echo MATLAB_DIR="$(MATLAB_DIR)" ls -l $(MATLAB_DIR) .PHONY: default 

This is the directory structure and exit from the Makefile:

 $ ls -l total 16 -rw-r--r-- 1 ash users 236 Mar 20 01:15 Makefile drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ a.app drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ b.app drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ c.app $ find . -type f ./Makefile ./MATLAB_ a.app/file1 ./MATLAB_ b.app/file2 $ make MATLAB_DIR=./MATLAB_\ a.app ls -l ./MATLAB_\ a.app total 0 -rw-r--r-- 1 ash users 0 Mar 20 01:13 file1 
+1
source

All Articles