Skip to content

Instantly share code, notes, and snippets.

@eatnumber1
Created August 18, 2011 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eatnumber1/1153395 to your computer and use it in GitHub Desktop.
Save eatnumber1/1153395 to your computer and use it in GitHub Desktop.
Thoughts on make extensions
CC ?= gcc
CXX ?= g++
.PHONY: all
all: foo.o foopp.o
foo.o: foo.c
$(CC) -c -o $< $@
foopp.o: foopp.cpp
$(CXX) -c -o $< $@
#include <stdio.h>
#include <hello.h>
int main() {
printf("%s\n", hello);
return 0;
}
static const char * const hello = "@HELLO@";
CC ?= gcc
FOO_CC := gcc-1.0
CXX ?= g++
.PHONY: all
all: foo_all bar.o barpp.o
bar.o: bar.c
$(CC) -c -o $< $@
barpp.o: barpp.cpp
$(CXX) -c -o $< $@
# The second argument prefixes all variables with FOO_. If the generated
# variable is unset, it is set to the value of the bare variable.
# The third argument prefixes all rules with foo_.
include foo.mk FOO_ foo_
# This should result in the following execution:
# all {
# foo_all {
# foo.o {
# foo.c (found)
# } foo.o -> gcc-1.0 -c -o foo.o foo.c
# foopp.o {
# foopp.cpp (found)
# } foopp.o -> g++ -c -o foopp.o foopp.cpp
# } foo_all
# bar.o {
# bar.c (found)
# } barpp.o -> gcc -c -o bar.o bar.c
# barpp.o {
# barpp.cpp (found)
# } barpp.o -> g++ -c -o barpp.o barpp.cpp
# } all
.EVAL_RULES := %.l!pre %.o!pre
.PHONY: all %.l!pre %.o!pre
all: nethack sokoban.l
nethack: nethack.o
levcc: levcc.c
$(CC) -o $< $@
declgen: declgen.c
$(CC) -o $< $@
decltab.h: decltab.s declgen
declgen -o $< $@
%.l!pre: %.ld
include $@
%.l: %.lev levcc
levcc -o $< $@
%.o!pre: %.d
include $@
%.o: %.c
$(CC) -c $< $@
%.ld: %.lev levcc
levcc -M -o $< $@
%.d: %.c
${CC} -M -o $< $@
# Exeution should go as follows:
# make
# all {
# nethack {
# nethack.o!pre {
# nethack.d {
# nethack.c (found)
# } nethack.d -> gcc -M -o nethack.d nethack.c # nethack.d = "nethack.o: declgen.d"
# } nethack.o!pre -> include nethack.d
# nethack.o {
# nethack.c (found)
# decltab.h {
# decltab.s (found)
# declgen {
# declgen.o!pre {
# declgen.d {
# declgen.c (found)
# } declgen.d -> gcc -M -o declgen.d declgen.c
# } declgen.o!pre -> include declgen.d
# declgen.o {
# declgen.c (found)
# } declgen.o -> gcc -c -o declgen.o declgen.c
# } declgen -> gcc -o declgen declgen.o
# } decltab.h -> declgen -o decltab.h decltab.s
# } nethack.o -> gcc -c nethack.o nethack.c
# } nethack -> gcc -o nethack nethack.o
# sokoban.l!pre {
# sokoban.ld {
# sokoban.lev (found)
# levcc {
# levcc.o!pre {
# levcc.d {
# levcc.c (found)
# } gcc -M -o levcc.d levcc.c
# } levcc.o!pre -> include levcc.d
# levcc.o {
# levcc.c (found)
# } levcc.o -> gcc -c -o levcc.o levcc.c
# } levcc -> gcc -o levcc levcc.o
# } sokoban.ld -> levcc -M -o sokoban.ld sokoban.lev # sokoban.ld = "sokoban.l: monsters.m"
# } sokoban.l!pre -> include sokoban.ld
# sokoban.l {
# monsters.m (found)
# } sokoban.l -> levcc -o sokoban.l sokoban.lev
# } all
.EVAL_RULES := %.o!pre
.PHONY: all %.o!pre
all: hello
hello: hello.o
$(CC) -o $< $@
%.h: %.h.in
sed -e 's/@HELLO@/Hello World!/g' $@ > $<
%.d: %.c
$(CC) -M -o $< $@
%.o!pre: %.d
include $@
%.o: %.c
$(CC) -c -o $< $@
# Execution should go as follows:
# all {
# hello {
# hello.o!pre {
# hello.d {
# hello.c (found)
# } hello.d -> gcc -M -o hello.d hello.c
# } hello.o!pre -> include hello.d
# hello.o {
# hello.c (found)
# hello.h { # added by hello.d
# hello.h.in (found)
# } hello.h -> sed -e 's/@HELLO@/Hello World!/g' hello.h.in > hello.h
# } hello.o -> gcc -c -o hello.o hello.c
# } hello -> gcc -o hello hello.o
# } all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment