| 1 |
# An interesting trick to run a shell command: |
| 2 |
# GNU Make uses $(shell cmd), whereas |
| 3 |
# BSD make use $(var:sh), where ${var} holds the command |
| 4 |
# We can run a shell command on both by storing the value of the command |
| 5 |
# in a variable var and then using $(shell $(var))$(var:sh). |
| 6 |
|
| 7 |
# To detect the operating system it's generally sufficient to run `uname -s`, |
| 8 |
# but this way Android is detected as Linux. Android can be detected by `uname -o`, |
| 9 |
# but not all `uname` implementation even have the `-o` flag. |
| 10 |
# So we first detect the kernel, and then if it's Linux we use the -o detection |
| 11 |
# to find if this is Android, otherwise falling back to whatever the kernel was. |
| 12 |
|
| 13 |
OS.exec = t="$$(uname -s)" ; [ Linux = "$$t" ] && uname -o || printf "%s\n" "$$t" |
| 14 |
OS ?= $(shell $(OS.exec))$(OS.exec:sh) |
| 15 |
# Force expansion |
| 16 |
OS := $(OS) |
| 17 |
|
| 18 |
# Headers |
| 19 |
|
| 20 |
PROG = clinfo |
| 21 |
MAN = man1/$(PROG).1 |
| 22 |
|
| 23 |
HDR = src/error.h \ |
| 24 |
src/ext.h \ |
| 25 |
src/ctx_prop.h \ |
| 26 |
src/fmtmacros.h \ |
| 27 |
src/memory.h \ |
| 28 |
src/ms_support.h \ |
| 29 |
src/info_loc.h \ |
| 30 |
src/info_ret.h \ |
| 31 |
src/opt_out.h \ |
| 32 |
src/strbuf.h |
| 33 |
|
| 34 |
VPATH = src |
| 35 |
|
| 36 |
# Make it easier to find the OpenCL headers on systems |
| 37 |
# that don't ship them by default; the user can just clone |
| 38 |
# them on a parallel directory from the official repository |
| 39 |
CPPFLAGS += -I../OpenCL-Headers |
| 40 |
|
| 41 |
CFLAGS ?= -g -pedantic -Werror |
| 42 |
CFLAGS += -std=c99 -Wall -Wextra |
| 43 |
|
| 44 |
SPARSE ?= sparse |
| 45 |
SPARSEFLAGS=-Wsparse-all -Wno-decl |
| 46 |
|
| 47 |
# BSD make does not define RM |
| 48 |
RM ?= rm -f |
| 49 |
|
| 50 |
# Installation paths and modes |
| 51 |
PREFIX ?= /usr/local |
| 52 |
BINDIR ?= $(PREFIX)/bin |
| 53 |
BINMODE ?= 555 |
| 54 |
MANDIR ?= $(PREFIX)/man |
| 55 |
MAN1DIR ?= $(MANDIR)/man1 |
| 56 |
MANMODE ?= 444 |
| 57 |
|
| 58 |
ANDROID_VENDOR_PATH ?= ${ANDROID_ROOT}/vendor/lib64 |
| 59 |
|
| 60 |
LDFLAGS_Android += -Wl,-rpath-link=${ANDROID_VENDOR_PATH} -L${ANDROID_VENDOR_PATH} |
| 61 |
|
| 62 |
LDFLAGS += $(LDFLAGS_$(OS)) |
| 63 |
|
| 64 |
# Common library includes |
| 65 |
LDLIBS__common = -lOpenCL -ldl |
| 66 |
|
| 67 |
# OS-specific library includes |
| 68 |
LDLIBS_Darwin = -framework OpenCL |
| 69 |
LDLIBS_Darwin_exclude = -lOpenCL |
| 70 |
|
| 71 |
LDLIBS += $(LDLIBS_${OS}) $(LDLIBS__common:$(LDLIBS_${OS}_exclude)=) |
| 72 |
|
| 73 |
# The main target is the executable, which is normally called clinfo. |
| 74 |
# However, on Android, due to the lack of support for RPATH, clinfo |
| 75 |
# needs an approprite LD_LIBRARY_PATH, so we map `clinfo` to a shell script |
| 76 |
# that sets LD_LIBRARY_PATH and invokes the real program, which is now called |
| 77 |
# clinfo.real. |
| 78 |
# |
| 79 |
# Of course on Android we need to buid both, but not on other OSes |
| 80 |
|
| 81 |
|
| 82 |
EXT.Android = .real |
| 83 |
EXENAME = $(PROG)$(EXT.${OS}) |
| 84 |
|
| 85 |
TARGETS.Android = $(PROG) |
| 86 |
TARGETS = $(EXENAME) $(TARGETS.${OS}) |
| 87 |
|
| 88 |
all: $(TARGETS) |
| 89 |
|
| 90 |
# |
| 91 |
# Targets to actually build the stuff |
| 92 |
# |
| 93 |
# Many versions of make define a LINK.c as a synthetic rule to link |
| 94 |
# C object files. In case it's not defined already, propose our own: |
| 95 |
LINK.c ?= $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) |
| 96 |
|
| 97 |
# Recipe for the actual executable, either clinfo (non-Android) |
| 98 |
# or clinfo.real (on Androd) |
| 99 |
$(EXENAME): $(PROG).o |
| 100 |
$(LINK.c) -o $@ $< $(LDLIBS) |
| 101 |
|
| 102 |
$(PROG).o: $(PROG).c $(HDR) |
| 103 |
|
| 104 |
# For Android: create a wrapping shell script to run |
| 105 |
# clinfo with the appropriate LD_LIBRARY_PATH. |
| 106 |
$(OS:Android=)$(PROG): |
| 107 |
@echo '#!/bin/sh' > $@ |
| 108 |
@echo 'wrapperdir="$$(dirname "$$(readlink -n -f "$$(command -v "$$0")")")"' >> $@ |
| 109 |
@echo 'LD_LIBRARY_PATH="${ANDROID_VENDOR_PATH}:${ANDROID_VENDOR_PATH}/egl:$$LD_LIBRARY_PATH" $${wrapperdir}/$(EXENAME) "$$@"' >> $@ |
| 110 |
chmod +x $@ |
| 111 |
|
| 112 |
clean: |
| 113 |
$(RM) $(PROG).o $(TARGETS) |
| 114 |
|
| 115 |
install: all |
| 116 |
install -d $(DESTDIR)$(BINDIR) |
| 117 |
install -d $(DESTDIR)$(MAN1DIR) |
| 118 |
|
| 119 |
install -p -m $(BINMODE) $(PROG) $(DESTDIR)$(BINDIR)/$(PROG) |
| 120 |
install -p -m $(MANMODE) $(MAN) $(DESTDIR)$(MAN1DIR) |
| 121 |
|
| 122 |
sparse: $(PROG).c |
| 123 |
$(SPARSE) $(CPPFLAGS) $(CFLAGS) $(SPARSEFLAGS) $^ |
| 124 |
|
| 125 |
show: |
| 126 |
@printf 'OS=%s\n' "${OS}" |
| 127 |
@printf 'CFLAGS=%s\n' "${CFLAGS}" |
| 128 |
@printf 'CPPFLAGS=%s\n' "${CPPFLAGS}" |
| 129 |
@printf 'LDFLAGS=%s\n' "${LDFLAGS}" |
| 130 |
@printf 'LDLIBS=%s\n' "${LDLIBS}" |
| 131 |
@printf 'TARGETS=%s\n' "${TARGETS}" |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
.PHONY: clean sparse install show |