Skip to content

Instantly share code, notes, and snippets.

@cr1901
Last active July 3, 2022 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cr1901/b11772db755978c78306 to your computer and use it in GitHub Desktop.
Save cr1901/b11772db755978c78306 to your computer and use it in GitHub Desktop.
m4 (simple) Makefile generator
dnl http://mbreen.com/m4.html#toc19
define(`LQ',`changequote(<,>)`dnl'
changequote`'')dnl
define(`RQ',`changequote(<,>)dnl`
'changequote`'')dnl
define(`for',`ifelse($#,0,``$0'',`ifelse(eval($2<=$3),1,
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')dnl
define(`foreach',`ifelse(eval($#>2),1,
`pushdef(`$1',`$3')$2`'popdef(`$1')dnl
`'ifelse(eval($#>3),1,`$0(`$1',`$2',shift(shift(shift($@))))')')')dnl
define(`nospaceforeach',`ifelse(eval($#>2),1,
`pushdef(`$1',`$3')$2`'popdef(`$1')dnl
`'ifelse(eval($#>3),1,`$0(`$1',`$2',shift(shift(shift($@))))')')')dnl
define(`_DEFINE_MACROS', `CC='cc
`CFLAGS'=cflags
`AS='as
`ASFLAGS'=asflags
`LD'=ld
`LDFLAGS'=ldflags
`RM='rm
`CP='cp)dnl
define(`_APPENDEXT', `foreach(`X', `X.$2', $1)')dnl
define(`_CHEADER', `$(CC) $(CFLAGS) -c')dnl
define(`_ASHEADER', `$(AS) $(ASFLAGS)')dnl
define(`_CHOOSECOMPILER', `ifelse($1, `c', _CHEADER, $1, `asm', _ASHEADER)')dnl
define(`_BUILDSRC', `$1.$3: $1.$2
_CHOOSECOMPILER($2) $1.$2
')dnl
define(`_LINKOBJ', `$2: _APPENDEXT(`$1', `$3')
$(LD) $(LDFLAGS) -o $2 _APPENDEXT(`$1', `$3')')dnl
define(`_CLEAN', `clean:
$(RM)' $1)dnl
define(`_INSTALL', `install: $1
$(CP) $1 $2')dnl
include(`genmake.m4')dnl
_DEFINE_MACROS
define(ALL_SRCS, `ifdef(asmsrcs, `csrcs, asmsrcs', `csrcs')')dnl
#Executable Link
_LINKOBJ(`ALL_SRCS', outname, objext)
#C Sources
ifdef(csrcs, nospaceforeach(`X', _BUILDSRC(`X', `c', objext), csrcs))dnl
#ASM Sources
ifdef(asmsrcs, nospaceforeach(`X', _BUILDSRC(`X', `asm', objext), asmsrcs))dnl
#Clean
_CLEAN(`outname *.objext')
#Install
_INSTALL(outname, `/usr/local/bin')
m4 -Dcflags -Dldflags -Dasflags -Dcc=gcc -Dcsrcs="a, b, c" -Dasmsrcs="d, e, f" -Doutname=hello.exe -Dobjext=obj maketest.m4 > makefile.out
#!/bin/bash
m4 -Dcflags -Dldflags -Dasflags -Dcc=gcc -Dcsrcs="a, b, c" -Dasmsrcs="d, e, f" -Doutname=hello.exe -Dobjext=obj maketest.m4 > makefile.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment