Cutting a string after occurrence of sub-string in a Makefile
I am writing a Makefile which needs to figure out the directory of an installed program, which can be found in PATH
; or that is to say, is assumed to be in PATH
.
For example: if the binary is /opt/Xilinx/12.1/ISE_DS/ISE/bin/lin64/xst
, then the path I am looking for is /opt/Xilinx/12.1/ISE_DS/ISE
. The program could also be found as /home/markus/Xilinx/10.1/ISE/bin/lin64/xst
开发者_高级运维 for example. Xilinx included the ISE_DS
part of the path only in later versions.
As a matter of fact the /bin/lin64/xst
substring cannot simply be deleted, because it could also be /bin/lin/xst
depending on your installation.
The Makefile could do something like XILINX_PATH = $(shell which xst)
and then process this string according to the above example, but I cannot figure out how to do this in a clean way.
The best I could come up with is this:
XILINX_PATH_1 = $(shell which xst)
XILINX_PATH_2 = $(subst /bin/lin64/xst,,${XILINX_PATH_1})
XILINX_PATH = $(subst /bin/lin/xst,,${XILINX_PATH_2})
Is there a better solution?
This seems to work:
XILINX_PATH = $(shell which xst | sed 's%bin\/lin\(64\)*/xst%%')
Note that the *
really ought to be ?
for the sake of tidiness, but I'm having trouble getting that to work. I'll do a few more experiments and post an update if I solve it.
EDIT:
Using \?
instead of *
works on non-obsolete versions of GNUMAKE (or at least on 3.81) and is more correct.
精彩评论