0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Makefile で起動されるシェルをカスタムする

Last updated at Posted at 2021-09-14

Overview

Makefile は1行単位でシェルコマンドで起動されるけど、このシェルを変更したいときとかあるじゃないですか。

具体的には

デフォルトだと /bin/sh なので、 source が使えないので bash に差し替えたいときとか。

それ自体は Makefile の SHELL 変数に使いたいシェルを指定してあげれば実現できます。
cf. https://qiita.com/nassy20/items/3183cc17f36a3f21e333

Makefile
a:
	@ echo ${SHELL}
	@ echo $${BASH_VERSION}
	@ echo $${ZSH_VERSION}
bash
$ make
/bin/sh
3.2.57(1)-release

$ make SHELL=/bin/bash
/bin/bash
3.2.57(1)-release

$ make SHELL=/bin/zsh
/bin/zsh

5.7.1

自前のシェルを使いたい

で、内部的にはたぶん ${SHELL} -c "指定されたコマンド" が実行されてるんですねこれ。
cf. https://stackoverflow.com/questions/40203215/customize-shell-in-makefile-set-shell-my-shell-sh

なので

上記の stackoverflow のコメントにある通り、

my.sh
# !/bin/bash
shift  # `-c` を除外
echo "自前のシェルだよ"
eval $*
実行権限をつける
$ chmod +x my.sh
Makefile
SHELL := ./my.sh

a:
	@ echo "make target だよ"

とかやると、

bash
$ make
自前のシェルだよ
make target だよ

とかできちゃうわけです。

pipenv でもできちゃう

もはやネタの範疇なので、役に立つかというと微妙だけどw

Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]

[requires]
python_version = "3.9"
pipenv.sh
# !/bin/bash
shift
eval pipenv run $*
Makefile
SHELL := ./pipenv.sh

a:
	pip freeze
	python -V
	pip show requests
実行
$ make
certifi==2021.5.30
charset-normalizer==2.0.4
idna==3.2
requests==2.26.0
urllib3==1.26.6
-------------
Python 3.9.6
-------------
Name: requests
Version: 2.26.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /Users/kuryu/workspace/check/pipevn/foo/bar/.venv/lib/python3.9/site-packages
Requires: urllib3, certifi, charset-normalizer, idna
Required-by:

毎回 pipenv run で起動するのでまぁまぁ遅いです。
pipenv shell で起動した中で全部実行できれば理想的なんだけどねぇ。

っていう話。

ちなみに

↑の話は Makefile の中だけで全部完結できないかなー?って調べてたのが発端なので、

pipenv shell で起動した中で全部実行

するだけなら普通に

bash
$ pipenv run make

でできます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?