[关闭]
@cyysu 2017-10-18T08:44:33.000000Z 字数 10864 阅读 877

Makefile-项目篇(一)

  • 时间:2017年10月10日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com
  • 版本:3.0
  • 描述:Makefile调用shell实战

Makefile系列教程


基础准备

  1. # 在Makefile中调用shell时需要注意一个完整的shell命令必须在一行内写完,或者采用"\"进行连接,并且每一个完毕的shell采用";"结尾,同时建议所有变量书写格式为“xxx=xxx”, 等号两边没有空格。
  2. # 下面我们来看几个栗子:
  3. mj@DZ:~/桌面/makefile/my$ cat Makefile
  4. DIR=./
  5. .PHONY: all
  6. all:
  7. @for i in `ls $(DIR)`;do \
  8. echo $$i; \
  9. done
  10. @echo ${MAKE}
  11. # 执行效果
  12. mj@DZ:~/桌面/makefile/my$ make
  13. foo.o
  14. main
  15. main.c
  16. main.o
  17. Makefile
  18. test.c
  19. make
  20. # 如果一个shell命令过长,那么我们可以采用命令定义和执行分开模式,我们来改写一下上面的格式:
  21. DIR=./
  22. shellcmd = @for i in `ls $(DIR)`;do \
  23. echo $$i; \
  24. done
  25. .PHONY: test
  26. test:
  27. $(shellcmd)
  28. all:
  29. @echo ${MAKE}
  30. # 还有就是shell的传递变量问题,如果时一个$符号,传递的是Makefile中的变量,如果时两个$$,那么传递的是shell中的变量,可以看下面的栗子
  31. DIR=./
  32. shellDateCmd=`date "+%Y-%m-%d %H:%M:%S"`
  33. shellShowDirCmd=@for i in `ls $(DIR)`;do \
  34. echo $$i; \
  35. done
  36. data=1
  37. .PHONY: vari
  38. vari:
  39. @echo "当前时间为:" $(shellDateCmd);
  40. @echo "data value = " ${data};
  41. # 这里表示是一个shell
  42. @data=2;
  43. # 这里输出的是Makefile中的变量
  44. @echo ${data};
  45. # 这里输出的时shell中的变量
  46. @echo $${data};
  47. test:
  48. $(shellShowDirCmd)
  49. all:
  50. @echo ${MAKE}
  51. # 执行结果,可以看到最后一行输出为空,表示我们建立的这个shell中时没有data这个变量的
  52. mj@DZ:~/桌面/makefile/my$ make
  53. 当前时间为: 2017-10-10 16:24:57
  54. data value = 1
  55. 1
  56. mj@DZ:~/桌面/makefile/my$
  57. # 再来一个栗子,我们改写上面的栗子
  58. DIR=./
  59. shellDateCmd=`date "+%Y-%m-%d %H:%M:%S"`
  60. shellShowDirCmd=@for i in `ls $(DIR)`;do \
  61. echo $$i; \
  62. done
  63. data=1
  64. .PHONY: shellvari
  65. shellvari:
  66. @echo "当前时间为:" $(shellDateCmd);
  67. @echo "data value = " ${data};
  68. @data=2; \
  69. echo ${data}; \
  70. echo $${data};
  71. vari:
  72. @echo "当前时间为:" $(shellDateCmd);
  73. @echo "data value = " ${data};
  74. @data=2;
  75. @echo ${data};
  76. @echo $${data};
  77. test:
  78. $(shellShowDirCmd)
  79. all:
  80. @echo ${MAKE}
  81. # 执行结果,一开始data为1,这个原因就不多说了。从data=2之后用“\”连接,那么这里表示时一个shell,也就是shell中的data此时数值为2,但是这个命令echo ${data}; \输出的是Makefile中的变量,echo $${data};这个才是输出的shell中的变量
  82. mj@DZ:~/桌面/makefile/my$ make
  83. 当前时间为: 2017-10-10 16:29:30
  84. data value = 1
  85. 1
  86. 2
  87. mj@DZ:~/桌面/makefile/my$

项目实战

项目来源于:https://github.com/cyysu/sphinx.git

  1. # 我在这里选择了一个文档生成工具Sphinx的Makefile来作为本次项目实战教程
  2. # 纵观整个Makefile,正好我们的基础篇和项目篇面大部分内容都涉及到了,没有涉及到的内容我就会在文中给出注释。
  3. # Makefile for Sphinx documentation
  4. #
  5. # You can set these variables from the command line.
  6. SPHINXOPTS =
  7. SPHINXBUILD = sphinx-build
  8. PAPER =
  9. BUILDDIR = build
  10. # ifeq为Makefile内置函数,作用和C语言中的if语句一致,可以看到这里的shell语句也是连成一句话的
  11. # User-friendly check for sphinx-build
  12. ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
  13. $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
  14. endif
  15. # 这里的 -D 表示时一个宏定义 -d是sphinx设置输出目标目录
  16. # Internal variables.
  17. PAPEROPT_a4 = -D latex_paper_size=a4
  18. PAPEROPT_letter = -D latex_paper_size=letter
  19. ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
  20. # the i18n builder cannot share the environment and doctrees with the others
  21. I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
  22. # 设置伪目标 剩下的内容都差不多,就不做过多的介绍了,看完了这里有没有感觉这么长的东西其实看起来也是蛮简单的啊!
  23. .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext
  24. help:
  25. # 增加@不会回显命令
  26. @echo "Please use \`make <target>' where <target> is one of"
  27. @echo " html to make standalone HTML files"
  28. @echo " dirhtml to make HTML files named index.html in directories"
  29. @echo " singlehtml to make a single large HTML file"
  30. @echo " pickle to make pickle files"
  31. @echo " json to make JSON files"
  32. @echo " htmlhelp to make HTML files and a HTML help project"
  33. @echo " qthelp to make HTML files and a qthelp project"
  34. @echo " applehelp to make an Apple Help Book"
  35. @echo " devhelp to make HTML files and a Devhelp project"
  36. @echo " epub to make an epub"
  37. @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
  38. @echo " latexpdf to make LaTeX files and run them through pdflatex"
  39. @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
  40. @echo " text to make text files"
  41. @echo " man to make manual pages"
  42. @echo " texinfo to make Texinfo files"
  43. @echo " info to make Texinfo files and run them through makeinfo"
  44. @echo " gettext to make PO message catalogs"
  45. @echo " changes to make an overview of all changed/added/deprecated items"
  46. @echo " xml to make Docutils-native XML files"
  47. @echo " pseudoxml to make pseudoxml-XML files for display purposes"
  48. @echo " linkcheck to check all external links for integrity"
  49. @echo " doctest to run all doctests embedded in the documentation (if enabled)"
  50. @echo " coverage to run coverage check of the documentation (if enabled)"
  51. clean:
  52. rm -rf $(BUILDDIR)/*
  53. html:
  54. $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
  55. @echo
  56. @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
  57. dirhtml:
  58. $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
  59. @echo
  60. @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
  61. singlehtml:
  62. $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
  63. @echo
  64. @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
  65. pickle:
  66. $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
  67. @echo
  68. @echo "Build finished; now you can process the pickle files."
  69. json:
  70. $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
  71. @echo
  72. @echo "Build finished; now you can process the JSON files."
  73. htmlhelp:
  74. $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
  75. @echo
  76. @echo "Build finished; now you can run HTML Help Workshop with the" \
  77. ".hhp project file in $(BUILDDIR)/htmlhelp."
  78. qthelp:
  79. $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
  80. @echo
  81. @echo "Build finished; now you can run "qcollectiongenerator" with the" \
  82. ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
  83. @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Python-OperSource-Project-Developer-Guide.qhcp"
  84. @echo "To view the help file:"
  85. @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Python-OperSource-Project-Developer-Guide.qhc"
  86. applehelp:
  87. $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
  88. @echo
  89. @echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
  90. @echo "N.B. You won't be able to view it unless you put it in" \
  91. "~/Library/Documentation/Help or install it in your application" \
  92. "bundle."
  93. devhelp:
  94. $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
  95. @echo
  96. @echo "Build finished."
  97. @echo "To view the help file:"
  98. @echo "# mkdir -p $$HOME/.local/share/devhelp/Python-OperSource-Project-Developer-Guide"
  99. @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Python-OperSource-Project-Developer-Guide"
  100. @echo "# devhelp"
  101. epub:
  102. $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
  103. @echo
  104. @echo "Build finished. The epub file is in $(BUILDDIR)/epub."
  105. latex:
  106. $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
  107. @echo
  108. @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
  109. @echo "Run \`make' in that directory to run these through (pdf)latex" \
  110. "(use \`make latexpdf' here to do that automatically)."
  111. latexpdf:
  112. $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
  113. @echo "Running LaTeX files through pdflatex..."
  114. $(MAKE) -C $(BUILDDIR)/latex all-pdf
  115. @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
  116. latexpdfja:
  117. $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
  118. @echo "Running LaTeX files through platex and dvipdfmx..."
  119. $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
  120. @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
  121. text:
  122. $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
  123. @echo
  124. @echo "Build finished. The text files are in $(BUILDDIR)/text."
  125. man:
  126. $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
  127. @echo
  128. @echo "Build finished. The manual pages are in $(BUILDDIR)/man."
  129. texinfo:
  130. $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
  131. @echo
  132. @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
  133. @echo "Run \`make' in that directory to run these through makeinfo" \
  134. "(use \`make info' here to do that automatically)."
  135. info:
  136. $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
  137. @echo "Running Texinfo files through makeinfo..."
  138. make -C $(BUILDDIR)/texinfo info
  139. @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
  140. gettext:
  141. $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
  142. @echo
  143. @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
  144. changes:
  145. $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
  146. @echo
  147. @echo "The overview file is in $(BUILDDIR)/changes."
  148. linkcheck:
  149. $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
  150. @echo
  151. @echo "Link check complete; look for any errors in the above output " \
  152. "or in $(BUILDDIR)/linkcheck/output.txt."
  153. doctest:
  154. $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
  155. @echo "Testing of doctests in the sources finished, look at the " \
  156. "results in $(BUILDDIR)/doctest/output.txt."
  157. coverage:
  158. $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
  159. @echo "Testing of coverage in the sources finished, look at the " \
  160. "results in $(BUILDDIR)/coverage/python.txt."
  161. xml:
  162. $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
  163. @echo
  164. @echo "Build finished. The XML files are in $(BUILDDIR)/xml."
  165. pseudoxml:
  166. $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
  167. @echo
  168. @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."

实战运行演示

  1. # sphinx-build安装成功之后的情况
  2. mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ sphinx-build --version
  3. Sphinx (sphinx-build) 1.6.4
  4. # 如果你的系统没有,那么执行下面程序
  5. mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ sudo easy_install sphinx
  6. Searching for sphinx
  7. Best match: Sphinx 1.6.4
  8. Adding Sphinx 1.6.4 to easy-install.pth file
  9. Installing sphinx-apidoc script to /usr/local/bin
  10. Installing sphinx-build script to /usr/local/bin
  11. Installing sphinx-quickstart script to /usr/local/bin
  12. Installing sphinx-autogen script to /usr/local/bin
  13. Using /usr/local/lib/python2.7/dist-packages
  14. Processing dependencies for sphinx
  15. Finished processing dependencies for sphinx
  16. #或者使用pip安装
  17. mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ pip install sphinx
  18. Requirement already satisfied: sphinx in /usr/local/lib/python2.7/dist-packages
  19. Requirement already satisfied: sphinxcontrib-websupport in /usr/local/lib/python2.7/dist-packages (from sphinx)
  20. Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from sphinx)
  21. Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  22. Requirement already satisfied: docutils>=0.11 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  23. Requirement already satisfied: typing; python_version < "3.5" in /usr/local/lib/python2.7/dist-packages (from sphinx)
  24. Requirement already satisfied: Pygments>=2.0 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  25. Requirement already satisfied: setuptools in /usr/local/lib/python2.7/dist-packages/setuptools-33.1.1-py2.7.egg (from sphinx)
  26. Requirement already satisfied: imagesize in /usr/local/lib/python2.7/dist-packages (from sphinx)
  27. Requirement already satisfied: babel!=2.0,>=1.3 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  28. Requirement already satisfied: snowballstemmer>=1.1 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  29. Requirement already satisfied: Jinja2>=2.3 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  30. Requirement already satisfied: alabaster<0.8,>=0.7 in /usr/local/lib/python2.7/dist-packages (from sphinx)
  31. Requirement already satisfied: idna<2.6,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
  32. Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
  33. Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
  34. Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
  35. Requirement already satisfied: pytz>=0a in /usr/local/lib/python2.7/dist-packages (from babel!=2.0,>=1.3->sphinx)
  36. Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.3->sphinx)

演示Demo

演示Demo

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注