3
2

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 5 years have passed since last update.

Makefileで環境変数が設定されているか確認する処理を書く

Last updated at Posted at 2020-03-04

背景

Makefileのコマンド実行時に、環境変数が設定されていることをバリデートする処理を書きたいことがあったのでメモ。

方法

ifndef を使って処理を書く。

ifndefとは

以下を参照
http://quruli.ivory.ne.jp/document/make_3.79.1/make-jp_6.html

ifndef variable-name
変数variable-nameが空の値を持つ場合、text-if-trueが有効となり、 そうでない場合は、text-if-falseが有効となります。

変数が定義されていない場合は、trueになる。

コード

今回は、PROJECT_ROOT_PATH という(環境)変数が設定されていることを保証したい。

Makefile

validate-env-vars:
    ifndef PROJECT_ROOT_PATH
      echo 'PROJECT_ROOT_PATH を環境変数に設定して下さい。'
      exit 1
    endif

do-hogehoge: validate-env-vars
    # 処理

これで、do-hogehoge を実行する前に環境変数をチェックできる。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?