Sync Mk with ports
[dports.git] / Mk / Uses / shebangfix.mk
1 # $FreeBSD$
2 #
3 # Replace #! interpreters in scripts by what we actually have.
4 #
5 # Standard templates for bash, perl, python,... are included out of
6 # the box, others can easily be added per port.
7 #
8 # Feature:      shebangfix
9 # Usage:        USES=shebangfix
10 #
11 # To specify that ${WRKSRC}/path1/file and all .pl files in ${WRKSRC}/path2
12 # should be processed:
13 #
14 #   SHEBANG_FILES=      path1/file path2/*.pl
15 #
16 # To define custom shebangs to replace, use the following (note that
17 # shebangs with spaces should be quoted):
18 #
19 #   perl_OLD_CMD=       /usr/bin/perl5.005 "/usr/bin/setenv perl5.005"
20 #
21 # To define a new shebang scheme add the following to the port Makefile:
22 #
23 #   SHEBANG_LANG=       lua
24 #   lua_OLD_CMD=        /usr/bin/lua
25 #   lua_CMD=    ${LOCALBASE}/bin/lua
26 #
27 # To override a definition, for example replacing /usr/bin/perl by
28 # /usr/bin/env perl, add the following:
29 #
30 #   perl_CMD=   ${SETENV} perl
31 #
32 # MAINTAINER: portmgr@FreeBSD.org
33
34 .if !defined(_INCLUDE_USES_SHEBANGFIX_MK)
35 _INCLUDE_USES_SHEBANGFIX_MK=    yes
36
37 SHEBANG_LANG+=  bash java ksh perl php python ruby tcl tk
38
39 .if ${USES:Mlua*}
40 SHEBANG_LANG+=  lua
41 lua_CMD?=       ${LOCALBASE}/bin/${LUA_CMD}
42 .endif
43
44 tcl_OLD_CMD+=   /usr/bin/tclsh
45 tcl_CMD?=       ${TCLSH}
46
47 tk_OLD_CMD+=    /usr/bin/wish
48 tk_CMD?=        ${WISH}
49
50 .if ${USES:Mpython*}
51 python_CMD?=    ${PYTHON_CMD}
52 .endif
53
54 # Replace the same patterns for all langs and setup a default, that may have
55 # been set already above with ?=.
56 .for lang in ${SHEBANG_LANG}
57 ${lang}_CMD?= ${LOCALBASE}/bin/${lang}
58 ${lang}_OLD_CMD+= "/usr/bin/env ${lang}"
59 ${lang}_OLD_CMD+= /bin/${lang}
60 ${lang}_OLD_CMD+= /usr/bin/${lang}
61 .endfor
62
63 .for lang in ${SHEBANG_LANG}
64 .  if !defined(${lang}_CMD)
65 IGNORE+=        missing definition for ${lang}_CMD
66 .  endif
67 .  if !defined(${lang}_OLD_CMD)
68 IGNORE+=        missing definition for ${lang}_OLD_CMD
69 .  endif
70 .  for old_cmd in ${${lang}_OLD_CMD}
71 _SHEBANG_REINPLACE_ARGS+=       -e "1s|^\#![[:space:]]*${old_cmd:C/\"//g}\([[:space:]]\)|\#!${${lang}_CMD}\1|"
72 _SHEBANG_REINPLACE_ARGS+=       -e "1s|^\#![[:space:]]*${old_cmd:C/\"//g}$$|\#!${${lang}_CMD}|"
73 .  endfor
74 .endfor
75
76 _USES_patch+=   210:fix-shebang
77 fix-shebang:
78         @cd ${WRKSRC}; \
79                 ${ECHO_CMD} ${SHEBANG_FILES} | ${XARGS} ${SED} -i '' ${_SHEBANG_REINPLACE_ARGS}
80
81 .endif