@cyysu
2017-10-18T08:44:33.000000Z
字数 10864
阅读 877
- 时间:2017年10月10日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com
- 版本:3.0
- 描述:Makefile调用shell实战
Makefile系列教程
# 在Makefile中调用shell时需要注意一个完整的shell命令必须在一行内写完,或者采用"\"进行连接,并且每一个完毕的shell采用";"结尾,同时建议所有变量书写格式为“xxx=xxx”, 等号两边没有空格。
# 下面我们来看几个栗子:
mj@DZ:~/桌面/makefile/my$ cat Makefile
DIR=./
.PHONY: all
all:
@for i in `ls $(DIR)`;do \
echo $$i; \
done
@echo ${MAKE}
# 执行效果
mj@DZ:~/桌面/makefile/my$ make
foo.o
main
main.c
main.o
Makefile
test.c
make
# 如果一个shell命令过长,那么我们可以采用命令定义和执行分开模式,我们来改写一下上面的格式:
DIR=./
shellcmd = @for i in `ls $(DIR)`;do \
echo $$i; \
done
.PHONY: test
test:
$(shellcmd)
all:
@echo ${MAKE}
# 还有就是shell的传递变量问题,如果时一个$符号,传递的是Makefile中的变量,如果时两个$$,那么传递的是shell中的变量,可以看下面的栗子
DIR=./
shellDateCmd=`date "+%Y-%m-%d %H:%M:%S"`
shellShowDirCmd=@for i in `ls $(DIR)`;do \
echo $$i; \
done
data=1
.PHONY: vari
vari:
@echo "当前时间为:" $(shellDateCmd);
@echo "data value = " ${data};
# 这里表示是一个shell
@data=2;
# 这里输出的是Makefile中的变量
@echo ${data};
# 这里输出的时shell中的变量
@echo $${data};
test:
$(shellShowDirCmd)
all:
@echo ${MAKE}
# 执行结果,可以看到最后一行输出为空,表示我们建立的这个shell中时没有data这个变量的
mj@DZ:~/桌面/makefile/my$ make
当前时间为: 2017-10-10 16:24:57
data value = 1
1
mj@DZ:~/桌面/makefile/my$
# 再来一个栗子,我们改写上面的栗子
DIR=./
shellDateCmd=`date "+%Y-%m-%d %H:%M:%S"`
shellShowDirCmd=@for i in `ls $(DIR)`;do \
echo $$i; \
done
data=1
.PHONY: shellvari
shellvari:
@echo "当前时间为:" $(shellDateCmd);
@echo "data value = " ${data};
@data=2; \
echo ${data}; \
echo $${data};
vari:
@echo "当前时间为:" $(shellDateCmd);
@echo "data value = " ${data};
@data=2;
@echo ${data};
@echo $${data};
test:
$(shellShowDirCmd)
all:
@echo ${MAKE}
# 执行结果,一开始data为1,这个原因就不多说了。从data=2之后用“\”连接,那么这里表示时一个shell,也就是shell中的data此时数值为2,但是这个命令echo ${data}; \输出的是Makefile中的变量,echo $${data};这个才是输出的shell中的变量
mj@DZ:~/桌面/makefile/my$ make
当前时间为: 2017-10-10 16:29:30
data value = 1
1
2
mj@DZ:~/桌面/makefile/my$
项目来源于:https://github.com/cyysu/sphinx.git
# 我在这里选择了一个文档生成工具Sphinx的Makefile来作为本次项目实战教程
# 纵观整个Makefile,正好我们的基础篇和项目篇面大部分内容都涉及到了,没有涉及到的内容我就会在文中给出注释。
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = build
# ifeq为Makefile内置函数,作用和C语言中的if语句一致,可以看到这里的shell语句也是连成一句话的
# User-friendly check for sphinx-build
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
$(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/)
endif
# 这里的 -D 表示时一个宏定义 -d是sphinx设置输出目标目录
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
# 设置伪目标 剩下的内容都差不多,就不做过多的介绍了,看完了这里有没有感觉这么长的东西其实看起来也是蛮简单的啊!
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext
help:
# 增加@不会回显命令
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " applehelp to make an Apple Help Book"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " xml to make Docutils-native XML files"
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
@echo " coverage to run coverage check of the documentation (if enabled)"
clean:
rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Python-OperSource-Project-Developer-Guide.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Python-OperSource-Project-Developer-Guide.qhc"
applehelp:
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
@echo
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
@echo "N.B. You won't be able to view it unless you put it in" \
"~/Library/Documentation/Help or install it in your application" \
"bundle."
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/Python-OperSource-Project-Developer-Guide"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Python-OperSource-Project-Developer-Guide"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
latexpdfja:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through platex and dvipdfmx..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
coverage:
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
@echo "Testing of coverage in the sources finished, look at the " \
"results in $(BUILDDIR)/coverage/python.txt."
xml:
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
@echo
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
pseudoxml:
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
@echo
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
# sphinx-build安装成功之后的情况
mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ sphinx-build --version
Sphinx (sphinx-build) 1.6.4
# 如果你的系统没有,那么执行下面程序
mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ sudo easy_install sphinx
Searching for sphinx
Best match: Sphinx 1.6.4
Adding Sphinx 1.6.4 to easy-install.pth file
Installing sphinx-apidoc script to /usr/local/bin
Installing sphinx-build script to /usr/local/bin
Installing sphinx-quickstart script to /usr/local/bin
Installing sphinx-autogen script to /usr/local/bin
Using /usr/local/lib/python2.7/dist-packages
Processing dependencies for sphinx
Finished processing dependencies for sphinx
#或者使用pip安装
mj@DZ:~/桌面/Linux/Python-OpenSource-Project-Developer-Guide$ pip install sphinx
Requirement already satisfied: sphinx in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: sphinxcontrib-websupport in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: docutils>=0.11 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: typing; python_version < "3.5" in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: Pygments>=2.0 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/dist-packages/setuptools-33.1.1-py2.7.egg (from sphinx)
Requirement already satisfied: imagesize in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: babel!=2.0,>=1.3 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: snowballstemmer>=1.1 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: Jinja2>=2.3 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: alabaster<0.8,>=0.7 in /usr/local/lib/python2.7/dist-packages (from sphinx)
Requirement already satisfied: idna<2.6,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests>=2.0.0->sphinx)
Requirement already satisfied: pytz>=0a in /usr/local/lib/python2.7/dist-packages (from babel!=2.0,>=1.3->sphinx)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.3->sphinx)
演示Demo
支付宝 微信