Linux 在特定目录中运行的可执行Makefile
我努力实现的目标:Linux 在特定目录中运行的可执行Makefile,linux,makefile,gnu-make,executable,gnu,Linux,Makefile,Gnu Make,Executable,Gnu,我努力实现的目标: 使用shebang创建makefile可执行文件 让它先切换到某个目录,这样就可以从任何地方调用它 我所拥有的: /docker/images/Makefile(带可执行标志): 我能行 cd /docker/images ./Makefile 但是,我希望能够做到: 我尝试的是: manmake声明,我可以设置一个--directoy参数。 然而,shebang#/usr/bin/make--directory=/docker/images-f,不起作用: $ cd
- 使用shebang创建makefile可执行文件
- 让它先切换到某个目录,这样就可以从任何地方调用它
cd /docker/images
./Makefile
但是,我希望能够做到:
我尝试的是:
manmake
声明,我可以设置一个--directoy
参数。然而,shebang
#/usr/bin/make--directory=/docker/images-f
,不起作用:
$ cd /somewhere/else
$ /docker/images/Makefile
make: *** =/docker/images -f: Datei oder Verzeichnis nicht gefunden. Schluss.
(未找到文件或目录)
有什么猜测吗?我在使用GNU Make 4.1的Devuan ASCII上 我看到了没有解决我的问题的方法。当我们把
#/usr/bin/make--directory=/docker/images-f
然后运行/docker/images/Makefile
这相当于运行:
/usr/bin/make "--directory=/docker/images -f" /docker/images/Makefile
一种解决方案是委托给第二行:
#!/home/debian/bin/run_second_line
# /usr/bin/make --directory=/docker/images -f
default: ...
...
在/home/debian/bin/run\u第二行:
#!/bin/bash
get_second_line=$(awk 'NR == 2{print}' "$1")
exec bash -c "${get_second_line#?} $1"
更新:
它应该与这个shebang一起工作:
#!/usr/bin/perl -euse File::Basename;exec qq`make -C @{[dirname $ARGV[0]]} -f ` . $ARGV[0]
当我们把#/usr/bin/make--directory=/docker/images-f
然后运行/docker/images/Makefile
这相当于运行:
/usr/bin/make "--directory=/docker/images -f" /docker/images/Makefile
一种解决方案是委托给第二行:
#!/home/debian/bin/run_second_line
# /usr/bin/make --directory=/docker/images -f
default: ...
...
在/home/debian/bin/run\u第二行:
#!/bin/bash
get_second_line=$(awk 'NR == 2{print}' "$1")
exec bash -c "${get_second_line#?} $1"
更新:
它应该与这个shebang一起工作:
#!/usr/bin/perl -euse File::Basename;exec qq`make -C @{[dirname $ARGV[0]]} -f ` . $ARGV[0]
那么,仅仅:
make -C /some/dir/where/there/is/a/makefile
那么,仅仅:
make -C /some/dir/where/there/is/a/makefile
受类似方法的启发,我找到了一个解决方案: 我的makefile现在具有以下头部:
#!/bin/bash
# the following block will be executed by bash, but not make
define BASH_CODE 2>/dev/null
make -C $(dirname $0) -f $(basename $0) $@
exit 0
endef
# the following lines will be interpreted by make, but not bash
# Makefile starts here #
volumes = $$HOME/docker/volumes
# headings created with https://www.askapache.com/online-tools/figlet-ascii/
default: talk
talk:
@echo Dies ist ein Test
pwd
...
有了这些线路,我现在可以执行以下操作:
make-C/path/to/makefile/
/path/to/makefile/makefile
有谁有更好的解决方案吗?受类似方法的启发,我找到了一个解决方案: 我的makefile现在具有以下头部:
#!/bin/bash
# the following block will be executed by bash, but not make
define BASH_CODE 2>/dev/null
make -C $(dirname $0) -f $(basename $0) $@
exit 0
endef
# the following lines will be interpreted by make, but not bash
# Makefile starts here #
volumes = $$HOME/docker/volumes
# headings created with https://www.askapache.com/online-tools/figlet-ascii/
default: talk
talk:
@echo Dies ist ein Test
pwd
...
有了这些线路,我现在可以执行以下操作:
make-C/path/to/makefile/
/path/to/makefile/makefile
有谁有更好的解决方案吗?当然可以。将
/usr/bin/env
与-S
选项一起使用,该选项正好用于分割shebang行上的参数
$ cat Makefile
#!/usr/bin/env -S make -C /docker/images -f
all:
echo Foo
输出:
$ /docker/images/Makefile
make: Entering directory '/docker/images'
echo Foo
Foo
make: Leaving directory '/docker/images'
当然可以。将
/usr/bin/env
与-S
选项一起使用,该选项正好用于分割shebang行上的参数
$ cat Makefile
#!/usr/bin/env -S make -C /docker/images -f
all:
echo Foo
输出:
$ /docker/images/Makefile
make: Entering directory '/docker/images'
echo Foo
Foo
make: Leaving directory '/docker/images'
据我所知,在执行makefile之前,这不会更改为/some/dir/where/there/is/a/对吗?正如我所要求的,两者都不允许它以脚本的形式调用makefile。这正是
-C
或-directory
的用途。因此,您不能像调用可执行文件那样调用它,而是像调用make-C/some/dir/
(它将自动拾取任何名为makefile
的文件,该文件实际上是等效的。我刚刚重新测试了该文件,并在规则中打印了pwd
,它打印了所需的目录。这是一个建议的替代方案,您不必接受此答案:)是的,我知道。但是我正在明确地搜索一个解决方案,这将允许我以可执行文件的形式启动它,同时保持运行make-C/somedir Makefile
的可能性。无论如何,感谢您的输入!还有一个问题-您需要直接以可执行文件的形式调用Makefile吗?还是可以接受使用n位于makefile旁边的可执行文件(比如称为makefile\u exec.sh
或其他东西)。然后,这可以确定它自己的位置,确保它是当前目录,然后只需调用make
…在不知道您希望makefile可执行的确切原因的情况下,很难知道,但这又是另一种等效方法-但它是否满足您的特定需要我不知道:)是的,正如在另一篇文章中提到的,我希望能够将其称为可执行文件。并通过make-C
。有点好奇,有点因为通话部分的简单。我的理解是,在执行makefile之前,这不会更改为/some/dir/where/there/is/a/对吗?正如我所要求的,两者都不允许它以脚本的形式调用makefile。这正是-C
或-directory
的用途。因此,您不能像调用可执行文件那样调用它,而是像调用make-C/some/dir/
(它将自动拾取任何名为makefile
的文件,该文件实际上是等效的。我刚刚重新测试了该文件,并在规则中打印了pwd
,它打印了所需的目录。这是一个建议的替代方案,您不必接受此答案:)是的,我知道。但是我正在明确地搜索一个解决方案,这将允许我以可执行文件的形式启动它,同时保持运行make-C/somedir Makefile
的可能性。无论如何,感谢您的输入!还有一个问题-您需要直接以可执行文件的形式调用Makefile吗?还是可以接受使用n位于makefile旁边的可执行文件(比如称为makefile\u exec.sh
或其他东西)。然后,这可以确定它自己的位置,确保它是当前目录,然后只需调用make
…在不知道您希望makefile可执行的确切原因的情况下,很难知道,但这又是另一种等效方法-但它是否满足您的特定需要我不知道:)是的,正如在另一篇文章中提到的,我希望能够将其称为可执行文件。并通过make-C
。有点好奇,有点因为通话部分的简单。只是因为我可以,虽然它澄清了我的方法不起作用,但它也不能提供解决方案。我想要一个不需要调用外部程序或脚本就能工作的解决方案