最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】给makefile添加make help时输出帮助信息

makefile crifan 5824浏览 0评论

折腾了:

【已解决】Mac中写Makefile或脚步实现自动化执行命令

后,想要make help输出用法。

一种是自己写echo或info,但是需要找个合适的例子去参考。

make help

Add a help target to a Makefile that will allow all targets to be self documenting

好像(才知道)可以自动生成help信息?

GNU make

还有库:

make-help

Linux make command information and examples

# target: dir_info – For debug, to Dispay current directory/folder related info.

dir_info:

  @echo MAKEFILE_LIST=$(MAKEFILE_LIST)

# target: help – Display callable targets.

help:

  @egrep “^# target:” [Mm]akefile

效果:

➜  http_summary git:(master) ✗ make help

####################################################################################################

Author: crifan.com

Version: 20171207

Function: Auto use gitbook to generated files: website/pdf/epub/mobi. Run ‘make help’ to see usage

####################################################################################################

# target: dir_info – For debug, to Dispay current directory/folder related info.

# target: help – Display callable targets.

Add a help target to a Makefile that will allow all targets to be self documenting

【总结】

最后在Makefile的最后,加上:

<code>TARGET_MAX_CHAR_NUM=20
</code>

## Show help

help:

  @echo ”

  @echo ‘Usage:’

  @echo ‘ make <target>’

  @echo ”

  @echo ‘Targets:’

  @awk ‘/^[a-zA-Z\-\_0-9]+:/ { \

    helpMessage = match(lastLine, /^## (.*)/); \

    if (helpMessage) { \

      helpCommand = substr($$1, 0, index($$1, “:”)-1); \

      helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \

      printf ” %-$(TARGET_MAX_CHAR_NUM)s %s\n”, helpCommand, helpMessage; \

    } \

  } \

  { lastLine = $$0 }’ $(MAKEFILE_LIST)

其他地方的target,写成:

<code>## your target description
your_target:
    xxxx
</code>

即可自动生成help的信息。

相关的代码:

<code>
## Generate Mobi file
mobi: clean_mobi create_foler_mobi
  gitbook mobi $(CURRENT_DIR_NOSLASH) $(MOBI_FULLNAME)

## Generate all files: website/pdf/epub/mobi
all: website pdf epub mobi


################################################################################
# Help
################################################################################
TARGET_MAX_CHAR_NUM=20
## Show help
help:
  @echo ''
  @echo 'Usage:'
  @echo ' make &lt;target&gt;'
  @echo ''
  @echo 'Targets:'
  @awk '/^[a-zA-Z\-\_0-9]+:/ { \
    helpMessage = match(lastLine, /^## (.*)/); \
    if (helpMessage) { \
      helpCommand = substr($$1, 0, index($$1, ":")-1); \
      helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
      printf " %-$(TARGET_MAX_CHAR_NUM)s %s\n", helpCommand, helpMessage; \
    } \
  } \
  { lastLine = $$0 }' $(MAKEFILE_LIST)
</code>

效果为:

<code>➜  http_summary git:(master) ✗ make help
</code>

<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————

Author: crifan.com

Version: 20171207

Function: Auto use gitbook to generated files: website/pdf/epub/mobi

        Run ‘make help’ to see usage

<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————

Usage:

  make <target>

Targets:

  debug_dir            [Debug] Dispay current directory related info

  create_foler_website Create folder for gitbook website

  create_foler_pdf     Create folder for pdf

  create_foler_epub    Create folder for epub

  create_foler_mobi    Create folder for mobi

  create_foler_all     Create folder for all target: website/pdf/epub/mobi

  clean_website        Clean generated gitbook website

  clean_pdf            Clean generated PDF file

  clean_epub           Clean generated ePub file

  clean_mobi           Clean generated Mobi file

  clean_all            Clean all generated files

  website              Generate gitbook website

  pdf                  Generate PDF file

  epub                 Generate ePub file

  mobi                 Generate Mobi file

  all                  Generate all files: website/pdf/epub/mobi

  help                 Show help

转载请注明:在路上 » 【已解决】给makefile添加make help时输出帮助信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.170 seconds, using 22.07MB memory