M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor?...
-
Upload
pearl-rose -
Category
Documents
-
view
228 -
download
0
Transcript of M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor?...
![Page 1: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/1.jpg)
M4 Macro-processing M4 Macro-processing LanguageLanguageGeoffrey SewellGeoffrey Sewell
![Page 2: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/2.jpg)
What will be shown?What will be shown?
• What’s a macro processor?• History of M4• Uses• Autotools• Syntax
Hopefully, you all learn something from this!!!
![Page 3: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/3.jpg)
What’s a Macro-What’s a Macro-processor?processor?
![Page 4: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/4.jpg)
General Macro ProcessorGeneral Macro Processor
• Copies a stream of text to a different location
• Makes use of replacements
• GPM (General Purpose Macro processor)
![Page 5: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/5.jpg)
UsesUses
• Language Expansion
• Textual Replacements
• Text Reformatting
![Page 6: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/6.jpg)
History of M4History of M4
• Developed in 1977 based off of ideas by Christopher Strachey
• Developed by Brian Kernighan and Dennis Ritchie
• Derives from GPM (the General Purpose Macroprocessor)
• Original macro used to run Rational Fortran– Provides a control structure for Fortran– Fortran is more like C
![Page 7: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/7.jpg)
Uses of M4Uses of M4
• Autotools• Handle hierarchical files
– M4 can recursively look through files
• Features– Arguments– Condition testing– Arithmetic– String and substring substitution– Macro Expansion
![Page 8: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/8.jpg)
AutotoolsAutotools
• Collection of packages
• Tools to create build system from simple instructions
• Central place to put fixes and improvements
![Page 9: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/9.jpg)
ToolsTools
• Aclocal
• Autoheader
• Libtoolize
• Automake (explained later)
• Autoconf (explained later)
• Configure
• Libtool
![Page 10: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/10.jpg)
http://devmanual.gentoo.org/general-concepts/autotools/
![Page 11: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/11.jpg)
AutoConfAutoConf
• Automatically configures source code packages
• Able to allow packages to work with many kinds of UNIX systems
• Transform a user written configure .ac/.in file to a shell script
• Generates Configure file
![Page 12: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/12.jpg)
AutoMakeAutoMake
• Produces makefiles for use with the make command
• Used with AutoConf
• Constructs Makefile.in, install-sh, missing, COPYING, depcomp
![Page 13: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/13.jpg)
Why/ Why not use AutotoolsWhy/ Why not use Autotools
• Why– Unpredictable Environment
• Why not– When it’s more troublesome to do it
![Page 14: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/14.jpg)
Basic Constructs of M4Basic Constructs of M4
![Page 15: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/15.jpg)
NameName
• Sequence of characters (letters, numbers, ‘_’) that are binded to a macro
• Must not start with a number
First01
alpha
![Page 16: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/16.jpg)
QuotesQuotes
• String to be quoted is placed in ` and ‘• Must be balanced• Can use quotes in the middle of another
set of quotes– Expansions won’t occur if name is in quotes
• Changequote• Nested quotes = stop expansion
``time’’ = `time’
![Page 17: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/17.jpg)
Comments & TokensComments & Tokens
• Comments– Started by ‘#’ and ended by ‘\n’ (newlines)– Not ignored by the language– When ‘\n’ entered, comment is ended
• Tokens– Anything that’s not a name or a quote
![Page 18: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/18.jpg)
Macro InvocationMacro Invocation
• name1
• Geof(arg1,arg2,arg3,…)
• Not a standard Macro Invocation– Bad()
• Empty Parentheses = empty string
![Page 19: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/19.jpg)
Macro Invocation (cont.)Macro Invocation (cont.)
• Too few arguments…– Other arguments seen as an empty string– No error returned
• Arguments expanded first
![Page 20: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/20.jpg)
Define New MacrosDefine New Macros
• Use define keyword
• Will map a name to an expansion– Expansion can involve another expansion
Define(hey, `Hello World.’)
![Page 21: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/21.jpg)
Delete MacroDelete Macro
Undefine(`macroName’)
• `’ are necessary for this to be done
• Will unbind a macro name with an expansion
![Page 22: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/22.jpg)
Macro ArgumentsMacro Arguments
• Argument n refered to as $n
• Arguments are positional
Define(switch, `$2, $1’)
What’s the result?
![Page 23: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/23.jpg)
Macro Defn TestMacro Defn Test
Ifdef(name, string1, string2)
• Test to see if a Macro is defined
• Specialized if statement
• String 2 is optional
![Page 24: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/24.jpg)
String Comparison ifelseString Comparison ifelse
Ifelse(string1, string2, equal, not-equal)
• Same concept as If Else statements in most programming languages
• Any Idea what this would do?Ifelse(cold, hot, `fresh’, clean, froggy, `tight’, `supafly’)
![Page 25: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/25.jpg)
Special CharactersSpecial Characters
• $# - number of arguments
• $* - runs through all arguments
• $ - nothing special
• $@ - same as $* but quotes argument
![Page 26: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/26.jpg)
Rename MacrosRename Macros
Defn(name)
• Copy a macro expansion to another name
• Only works if it’s considered to be an expansion
![Page 27: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/27.jpg)
Counting and ArithmeticCounting and Arithmetic
• Incr(#)
• Decr(#)
• Eval(expr) where expression is an arithmetic expression
![Page 28: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/28.jpg)
RedefineRedefine
• Like a stack
• Can have multiple definitions for a macro
• Pushdef(name, expansion)– Add expansion to macro
• Popdef(name)– Takes away an expansion associated with a
macro
• Define will replace top most expansion
![Page 29: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/29.jpg)
RecursionRecursion
• Works like most other languages
define(`reverse', `ifelse($#, 0, , $#, 1, "$1", `reverse(shift($@)), `$1")')
• Shift – Looks at all arguments except the first one
![Page 30: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/30.jpg)
For loopFor loop
• Forloop(valName,start, end, statement)
• In actuality a recursive call– No real implementation for loops
define(`forloop', `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
define(`_forloop', `$4`'ifelse($1, `$3', , `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
![Page 31: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/31.jpg)
String ManipulationString Manipulation
• Len(string1)
• Substr(string1,pos,#ofchars)
• Index(string1,string2)
• Translit(string,set1,set2)– Can use regular expressions– Example
patsubst(`GNUs not Unix', `[A-Z][a-z]+')
![Page 32: M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.](https://reader036.fdocuments.in/reader036/viewer/2022062423/56649f0b5503460f94c1e8b6/html5/thumbnails/32.jpg)
ReferencesReferences
• http://en.wikipedia.org/wiki/GNU_build_system
• http://en.wikipedia.org/wiki/GNU_build_system#GNU_Automake
• http://www.cs.utah.edu/dept/old/texinfo/m4/m4.html
• http://devmanual.gentoo.org/general-concepts/autotools/