02 Simple C Project - HammerBS Examples

This project is a relatively minimum example for building C projects. The code features automatted makefile dependencies (i.e. gcc's `-MD`), string substituions, and for loops. The example download includes the C sources in addition to the `Hammer` file.

02_c_proj.tar.xz

# compiler options
ld = gcc;
cc = gcc;
cflags = -g -O2;
ldflags = ;

# sources
src = main.c lib.c;
obj = ${src.sub(.c,.o)};
dep = ${src.sub(.c,.d)};

# makefile dependencies
makedep $dep;


## Building ##
.all: prog;

# linking rule
prog: $obj {
  $ld $^ -o $@ $ldflags;
}

# we need a rule for every source file
for c : $src {
  # object/makedep path via substition
  o = ${c.sub(.c, .o)};

  # compile source to object rule
  $o: $c {
    $cc -c $< -o $@ -MD $cflags;
  }
}


## Cleanup ##
.clean: {
  rm -f $obj $dep prog;
}


## Full Cleanup ##
.distclean: .clean {
  rm -f hammer.cache;
}


## Program Execution ##
.run: .all {
  ./prog;
}