allow for out-of-tree builds

This adds a few missing build vars to the compile and makes it
so the code can be compiled out-of-tree.

BUG=None
TEST=`emerge-x86-alex ply-image` still works

Change-Id: Ifbec0b8706523c6ec61751f20d8fc874f76c97bb
Reviewed-on: https://gerrit.chromium.org/gerrit/48858
Reviewed-by: Daniel Erat <derat@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/Makefile b/Makefile
index 2f5479b..9d803c1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,3 @@
-all: ply-image
-
-ply-image:
-	$(MAKE) -C src $@
-
-clean:
+all:
+%:
 	$(MAKE) -C src $@
diff --git a/src/Makefile b/src/Makefile
index 5c1b3f3..f7d7f26 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -5,7 +5,6 @@
     ply-list.c \
     ply-monitor.c
 
-
 H = ply-frame-buffer.h \
     ply-gamma.h \
     ply-kms.h \
@@ -13,11 +12,19 @@
     ply-monitor.h \
     ply-utils.h
 
-CFLAGS ?= -O2
-PKG_CONFIG ?= pkg-config
+SRC ?= .
+OUT ?= .
+VPATH = $(SRC)
 
-ply-image: $(C) $(H) Makefile
-	$(CC) $(CFLAGS) $(C) $(LIBS) -Wall -I. -o ply-image -o ply-image \
+CFLAGS ?= -O2
+CFLAGS += -Wall
+PKG_CONFIG ?= pkg-config
+CPPFLAGS += -I$(SRC)
+
+all: $(OUT)/ply-image
+
+$(OUT)/ply-image: $(C) $(H) Makefile
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(C) -o $@ \
 		-lpng -lrt -lm `$(PKG_CONFIG) --libs --cflags libdrm`
 
 clean: