Macros Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons...
-
Upload
ernest-cox -
Category
Documents
-
view
213 -
download
0
Transcript of Macros Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons...
Macros
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
Automated Builds
Automated Builds Macros
Manage tasks and dependencies
Automated Builds Macros
figure-1.svg
summary-1.dat
Manage tasks and dependencies
figure-2.svg
data-1-1.dat data-1-3.datdata-1-2.dat
paper.pdf
paper.wdp
Automated Builds Macros
"must conform to university style"
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
C:\papers
home
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
C:\papers /lib/styles/
home lab
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
data-1-1.dat data-1-2.dateuphoric.fig
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
C:\papers
home
data-1-1.dat data-1-2.dateuphoric.fig
Automated Builds Macros
"must conform to university style"
figure-1.svg figure-2.svg
paper.pdf
euphoric.wps paper.wdp
C:\papers /lib/styles/
home lab
data-1-1.dat data-1-2.dateuphoric.fig
Automated Builds Macros
# false-dependencies.mk
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf $<
figure-%.svg : summary-%.dat sgr -N -r $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Makefile so far
Automated Builds Macros
# with-directories-at-home.mk
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style c:/papers/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s c:/papers/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Add directories for working at home
Automated Builds Macros
# with-directories-at-home.mk
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style c:/papers/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s c:/papers/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Add directories for working at home
Automated Builds Macros
# with-directories-at-home.mk
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style c:/papers/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s c:/papers/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Usually don't list "system" files explicitly
Add directories for working at home
Automated Builds Macros
# with-directories-at-home.mk
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style c:/papers/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s c:/papers/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Usually don't list "system" files explicitly
But what about the lab?
Add directories for working at home
Automated Builds Macros
1. Write two Makefiles
Automated Builds Macros
1. Write two Makefiles
Write and maintain
Automated Builds Macros
1. Write two Makefiles
2. Comment and uncomment lines
Write and maintain
Automated Builds Macros
1. Write two Makefiles
2. Comment and uncomment lines
Write and maintain
Consistently every time
Automated Builds Macros
1. Write two Makefiles
2. Comment and uncomment lines
Write and maintain
Consistently every time
Will create lots of noise in
version control
Automated Builds Macros
1. Write two Makefiles
2. Comment and uncomment lines
3. Refactor
Write and maintain
Consistently every time
Will create lots of noise in
version control
Automated Builds Macros
Use a macro
Automated Builds Macros
# with-macro.mk
STYLE_DIR=c:/papers/
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style ${STYLE_DIR}/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s ${STYLE_DIR}/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Use a macro
Automated Builds Macros
# with-macro.mk
STYLE_DIR=c:/papers/
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style ${STYLE_DIR}/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s ${STYLE_DIR}/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Use a macro
Automated Builds Macros
# with-macro.mk
STYLE_DIR=c:/papers/
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf --style ${STYLE_DIR}/euphoric.wps $<
figure-%.svg : summary-%.dat sgr -N -r -s ${STYLE_DIR}/euphoric.fig $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Use a macro
Automated Builds Macros
Only have one thing to change
Automated Builds Macros
Only have one thing to change
✓ Consistency
Automated Builds Macros
Only have one thing to change
✓ Consistency
✗ But still have noise
Automated Builds Macros
Only have one thing to change
Must use ${MACRO} or $(MACRO), not $MACRO
✓ Consistency
✗ But still have noise
Automated Builds Macros
Only have one thing to change
Must use ${MACRO} or $(MACRO), not $MACRO
Make reads $MACRO is ($M)ACRO
✓ Consistency
✗ But still have noise
Automated Builds Macros
Only have one thing to change
Must use ${MACRO} or $(MACRO), not $MACRO
Make reads $MACRO is ($M)ACRO
Which is probably just "ACRO"
✓ Consistency
✗ But still have noise
Automated Builds Macros
Only have one thing to change
Must use ${MACRO} or $(MACRO), not $MACRO
Make reads $MACRO is ($M)ACRO
Which is probably just "ACRO"
Which is probably not what you want
✓ Consistency
✗ But still have noise
Automated Builds Macros
Only have one thing to change
Must use ${MACRO} or $(MACRO), not $MACRO
Make reads $MACRO is ($M)ACRO
Which is probably just "ACRO"
Which is probably not what you want
yet another legacy wart
✓ Consistency
✗ But still have noise
Automated Builds Macros
Commonly define macros for control flags
Automated Builds Macros
# with-lots-of-macros.mk
STYLE_DIR=c:/papers/WDP2PDF_FLAGS=--style ${STYLE_DIR}/euphoric.wpsSGR_FLAGS=-N -r -s ${STYLE_DIR}/euphoric.fig
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf ${WDP2PDF_FLAGS} $<
figure-%.svg : summary-%.dat sgr ${SGR_FLAGS} $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Commonly define macros for control flags
Automated Builds Macros
# with-lots-of-macros.mk
STYLE_DIR=c:/papers/WDP2PDF_FLAGS=--style ${STYLE_DIR}/euphoric.wpsSGR_FLAGS=-N -r -s ${STYLE_DIR}/euphoric.fig
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf ${WDP2PDF_FLAGS} $<
figure-%.svg : summary-%.dat sgr ${SGR_FLAGS} $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Commonly define macros for control flags
Automated Builds Macros
# with-lots-of-macros.mk
STYLE_DIR=c:/papers/WDP2PDF_FLAGS=--style ${STYLE_DIR}/euphoric.wpsSGR_FLAGS=-N -r -s ${STYLE_DIR}/euphoric.fig
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf ${WDP2PDF_FLAGS} $<
figure-%.svg : summary-%.dat sgr ${SGR_FLAGS} $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
Commonly define macros for control flags
Automated Builds Macros
# config.mk
STYLE_DIR=c:/papers/
Now put the first macro in a separate file
Automated Builds Macros
# with-include.mkinclude config.mk
WDP2PDF_FLAGS=--style ${STYLE_DIR}/euphoric.wpsSGR_FLAGS=-N -r -s ${STYLE_DIR}/euphoric.fig
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf ${WDP2PDF_FLAGS} $<
figure-%.svg : summary-%.dat sgr ${SGR_FLAGS} $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
And include it from the main file
Automated Builds Macros
# with-include.mkinclude config.mk
WDP2PDF_FLAGS=--style ${STYLE_DIR}/euphoric.wpsSGR_FLAGS=-N -r -s ${STYLE_DIR}/euphoric.fig
paper.pdf : paper.wdp figure-1.svg figure-2.svg wdp2pdf ${WDP2PDF_FLAGS} $<
figure-%.svg : summary-%.dat sgr ${SGR_FLAGS} $@ $^
summary-%.dat : data-%-*.dat stats.py $@ $^
data-*-*.dat : stats.py touch $@
And include it from the main file
Automated Builds Macros
Actually create two configuration files
Automated Builds Macros
# config-home.mk
STYLE_DIR=c:/papers/
Actually create two configuration files
Automated Builds Macros
# config-home.mk
STYLE_DIR=c:/papers/
Actually create two configuration files
# config-lab.mk
STYLE_DIR=/lib/styles
Automated Builds Macros
# config-home.mk
STYLE_DIR=c:/papers/
Actually create two configuration files
# config-lab.mk
STYLE_DIR=/lib/styles
These two files stay in version control
Automated Builds Macros
# config-home.mk
STYLE_DIR=c:/papers/
Actually create two configuration files
# config-lab.mk
STYLE_DIR=/lib/styles
These two files stay in version control
Copy to create config.mk per machine
Automated Builds Macros
paper/
config-lab.mkconfig-home.mkMakefile
Home
Automated Builds Macros
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Home
Automated Builds Macros
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Home
paper/
config-lab.mkconfig-home.mkMakefile
Lab
Automated Builds Macros
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Home
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Lab
Automated Builds Macros
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Home
paper/
config.mkconfig-lab.mkconfig-home.mkMakefile
Lab
Automated Builds Macros
Can also define macro value on command line
Automated Builds Macros
$ make -DSTYLE_DIR=/lib/styles -f Makefile
Can also define macro value on command line
Automated Builds Macros
$ make -DSTYLE_DIR=/lib/styles -f Makefile
Can also define macro value on command line
Generally a bad idea
Automated Builds Macros
$ make -DSTYLE_DIR=/lib/styles -f Makefile
Can also define macro value on command line
Generally a bad idea
Have to remember to type definition each time
Automated Builds Macros
$ make -DSTYLE_DIR=/lib/styles -f Makefile
Can also define macro value on command line
Generally a bad idea
Have to remember to type definition each time
correctly
Automated Builds Macros
$ make -DSTYLE_DIR=/lib/styles -f Makefile
Can also define macro value on command line
Generally a bad idea
Have to remember to type definition each time
correctly
And there's no record of the flag
Automated Builds Macros
Many other approaches
Automated Builds Macros
Many other approaches
CMake and Autoconf/Automake: compile higher-level
specification into a Makefile (or equivalent)
Automated Builds Macros
Many other approaches
CMake and Autoconf/Automake: compile higher-level
specification into a Makefile (or equivalent)
✓ Automatically discover/manage differences
between machines
Automated Builds Macros
Many other approaches
CMake and Autoconf/Automake: compile higher-level
specification into a Makefile (or equivalent)
✓ Automatically discover/manage differences
between machines
✗ But even harder to debug
Automated Builds Macros
Many other approaches
CMake and Autoconf/Automake: compile higher-level
specification into a Makefile (or equivalent)
✓ Automatically discover/manage differences
between machines
✗ But even harder to debug
A build file is a program
Automated Builds Macros
Many other approaches
CMake and Autoconf/Automake: compile higher-level
specification into a Makefile (or equivalent)
✓ Automatically discover/manage differences
between machines
✗ But even harder to debug
A build file is a program
Requires the same degree of respect
August 2010
created by
Greg Wilson
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.