1919# along with this program; if not, write to the Free Software
2020# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121#
22+ OUTPUT =./
23+ ifeq ("$(origin O ) ", "command line")
24+ OUTPUT := $(O)/
25+ endif
26+
27+ ifneq ($(OUTPUT ) ,)
28+ # check that the output directory actually exists
29+ OUTDIR := $(shell cd $(OUTPUT ) && /bin/pwd)
30+ $(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist))
31+ endif
2232
2333# --- CONFIGURATION BEGIN ---
2434
@@ -87,6 +97,7 @@ AR = $(CROSS)ar
8797STRIP = $(CROSS ) strip
8898RANLIB = $(CROSS ) ranlib
8999HOSTCC = gcc
100+ MKDIR = mkdir
90101
91102
92103# Now we set up the build system
@@ -95,7 +106,7 @@ HOSTCC = gcc
95106# set up PWD so that older versions of make will work with our build.
96107PWD = $(shell pwd)
97108
98- GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo po/$$HLANG.gmo; done;}
109+ GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo $( OUTPUT ) po/$$HLANG.gmo; done;}
99110
100111export CROSS CC AR STRIP RANLIB CFLAGS LDFLAGS LIB_OBJS
101112
@@ -122,15 +133,18 @@ UTIL_OBJS = utils/helpers/amd.o utils/helpers/topology.o utils/helpers/msr.o \
122133 utils/cpupower.o utils/cpufreq-info.o utils/cpufreq-set.o \
123134 utils/cpupower-set.o utils/cpupower-info.o utils/cpuidle-info.o
124135
136+ UTIL_SRC := $(UTIL_OBJS:.o=.c )
137+
138+ UTIL_OBJS := $(addprefix $(OUTPUT ) ,$(UTIL_OBJS ) )
139+
125140UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h \
126141 utils/helpers/bitmask.h \
127142 utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def
128143
129- UTIL_SRC := $(UTIL_OBJS:.o=.c )
130-
131144LIB_HEADERS = lib/cpufreq.h lib/sysfs.h
132145LIB_SRC = lib/cpufreq.c lib/sysfs.c
133146LIB_OBJS = lib/cpufreq.o lib/sysfs.o
147+ LIB_OBJS := $(addprefix $(OUTPUT ) ,$(LIB_OBJS ) )
134148
135149CFLAGS += -pipe
136150
@@ -168,79 +182,90 @@ endif
168182
169183# the actual make rules
170184
171- all : libcpupower cpupower $(COMPILE_NLS ) $(COMPILE_BENCH )
185+ all : libcpupower $( OUTPUT ) cpupower $(COMPILE_NLS ) $(COMPILE_BENCH )
172186
173- lib/% .o : $(LIB_SRC ) $(LIB_HEADERS )
187+ $( OUTPUT ) lib/% .o : $(LIB_SRC ) $(LIB_HEADERS )
174188 $(ECHO ) " CC " $@
175189 $(QUIET ) $(CC ) $(CFLAGS ) -fPIC -o $@ -c lib/$* .c
176190
177- libcpupower.so.$(LIB_MAJ ) : $(LIB_OBJS )
191+ $( OUTPUT ) libcpupower.so.$(LIB_MAJ ) : $(LIB_OBJS )
178192 $(ECHO ) " LD " $@
179193 $(QUIET ) $(CC ) -shared $(CFLAGS ) $(LDFLAGS ) -o $@ \
180194 -Wl,-soname,libcpupower.so.$(LIB_MIN ) $(LIB_OBJS )
181- @ln -sf $@ libcpupower.so
182- @ln -sf $@ libcpupower.so.$(LIB_MIN )
195+ @ln -sf $( @F ) $( OUTPUT ) libcpupower.so
196+ @ln -sf $( @F ) $( OUTPUT ) libcpupower.so.$(LIB_MIN )
183197
184- libcpupower : libcpupower.so.$(LIB_MAJ )
198+ libcpupower : $( OUTPUT ) libcpupower.so.$(LIB_MAJ )
185199
186200# Let all .o files depend on its .c file and all headers
187201# Might be worth to put this into utils/Makefile at some point of time
188202$(UTIL_OBJS ) : $(UTIL_HEADERS )
189203
190- .c.o :
204+ $( OUTPUT ) % .o : % .c
191205 $(ECHO ) " CC " $@
192206 $(QUIET ) $(CC ) $(CFLAGS ) -I./lib -I ./utils -o $@ -c $* .c
193207
194- cpupower : $(UTIL_OBJS ) libcpupower.so.$(LIB_MAJ )
208+ $( OUTPUT ) cpupower : $(UTIL_OBJS ) $( OUTPUT ) libcpupower.so.$(LIB_MAJ )
195209 $(ECHO ) " CC " $@
196- $(QUIET ) $(CC ) $(CFLAGS ) $(LDFLAGS ) $(UTIL_OBJS ) -lcpupower -lrt -lpci -L. -o $@
210+ $(QUIET ) $(CC ) $(CFLAGS ) $(LDFLAGS ) $(UTIL_OBJS ) -lcpupower -lrt -lpci -L$( OUTPUT ) -o $@
197211 $(QUIET ) $(STRIPCMD ) $@
198212
199- po/$(PACKAGE ) .pot : $(UTIL_SRC )
213+ $( OUTPUT ) po/$(PACKAGE ) .pot : $(UTIL_SRC )
200214 $(ECHO ) " GETTEXT " $@
201215 $(QUIET ) xgettext --default-domain=$(PACKAGE ) --add-comments \
202216 --keyword=_ --keyword=N_ $(UTIL_SRC ) -p $(@D ) -o $(@F )
203217
204- po/% .gmo : po/% .po
218+ $( OUTPUT ) po/% .gmo : po/% .po
205219 $(ECHO ) " MSGFMT " $@
206220 $(QUIET ) msgfmt -o $@ po/$* .po
207221
208222create-gmo : ${GMO_FILES}
209223
210- update-po : po/$(PACKAGE ) .pot
224+ update-po : $( OUTPUT ) po/$(PACKAGE ) .pot
211225 $(ECHO ) " MSGMRG " $@
212226 $(QUIET ) @for HLANG in $(LANGUAGES ) ; do \
213227 echo -n " Updating $$ HLANG " ; \
214- if msgmerge po/$$ HLANG.po po/ $( PACKAGE ) .pot -o \
215- po/$$ HLANG.new.po; then \
216- mv -f po/$$ HLANG.new.po po/$$ HLANG.po; \
228+ if msgmerge po/$$ HLANG.po $ < -o \
229+ $( OUTPUT ) po/$$ HLANG.new.po; then \
230+ mv -f $( OUTPUT ) po/$$ HLANG.new.po $( OUTPUT ) po/$$ HLANG.po; \
217231 else \
218232 echo " msgmerge for $$ HLANG failed!" ; \
219- rm -f po/$$ HLANG.new.po; \
233+ rm -f $( OUTPUT ) po/$$ HLANG.new.po; \
220234 fi ; \
221235 done ;
222236
223- compile-bench : libcpupower.so.$(LIB_MAJ )
224- @V=$(V ) confdir=$(confdir ) $(MAKE ) -C bench
237+ compile-bench : $(OUTPUT ) libcpupower.so.$(LIB_MAJ )
238+ @V=$(V ) confdir=$(confdir ) $(MAKE ) -C bench O=$(OUTPUT )
239+
240+ # we compile into subdirectories. if the target directory is not the
241+ # source directory, they might not exists. So we depend the various
242+ # files onto their directories.
243+ DIRECTORY_DEPS = $(LIB_OBJS ) $(UTIL_OBJS ) $(GMO_FILES )
244+ $(DIRECTORY_DEPS ) : | $(sort $(dir $(DIRECTORY_DEPS ) ) )
245+
246+ # In the second step, we make a rule to actually create these directories
247+ $(sort $(dir $(DIRECTORY_DEPS ) ) ) :
248+ $(ECHO ) " MKDIR " $@
249+ $(QUIET ) $(MKDIR ) -p $@ 2> /dev/null
225250
226251clean :
227- -find . \( -not -type d \) -and \( -name ' *~' -o -name ' *.[oas]' \) -type f -print \
252+ -find $( OUTPUT ) \( -not -type d \) -and \( -name ' *~' -o -name ' *.[oas]' \) -type f -print \
228253 | xargs rm -f
229- -rm -f cpupower
230- -rm -f libcpupower.so*
231- -rm -rf po/* .gmo po/ * . pot
232- $(MAKE ) -C bench clean
254+ -rm -f $( OUTPUT ) cpupower
255+ -rm -f $( OUTPUT ) libcpupower.so*
256+ -rm -rf $( OUTPUT ) po/* .{ gmo, pot}
257+ $(MAKE ) -C bench O= $( OUTPUT ) clean
233258
234259
235260install-lib :
236261 $(INSTALL ) -d $(DESTDIR ) ${libdir}
237- $(CP ) libcpupower.so* $(DESTDIR ) ${libdir} /
262+ $(CP ) $( OUTPUT ) libcpupower.so* $(DESTDIR ) ${libdir} /
238263 $(INSTALL ) -d $(DESTDIR ) ${includedir}
239264 $(INSTALL_DATA ) lib/cpufreq.h $(DESTDIR ) ${includedir} /cpufreq.h
240265
241266install-tools :
242267 $(INSTALL ) -d $(DESTDIR ) ${bindir}
243- $(INSTALL_PROGRAM ) cpupower $(DESTDIR ) ${bindir}
268+ $(INSTALL_PROGRAM ) $( OUTPUT ) cpupower $(DESTDIR ) ${bindir}
244269
245270install-man :
246271 $(INSTALL_DATA ) -D man/cpupower.1 $(DESTDIR ) ${mandir} /man1/cpupower.1
@@ -253,13 +278,13 @@ install-man:
253278install-gmo :
254279 $(INSTALL ) -d $(DESTDIR ) ${localedir}
255280 for HLANG in $( LANGUAGES) ; do \
256- echo ' $(INSTALL_DATA) -D po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo' ; \
257- $(INSTALL_DATA ) -D po/$$ HLANG.gmo $(DESTDIR ) ${localedir} /$$ HLANG/LC_MESSAGES/cpupower.mo; \
281+ echo ' $(INSTALL_DATA) -D $(OUTPUT) po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo' ; \
282+ $(INSTALL_DATA ) -D $( OUTPUT ) po/$$ HLANG.gmo $(DESTDIR ) ${localedir} /$$ HLANG/LC_MESSAGES/cpupower.mo; \
258283 done ;
259284
260285install-bench :
261286 @# DESTDIR must be set from outside to survive
262- @sbindir=$(sbindir ) bindir=$(bindir ) docdir=$(docdir ) confdir=$(confdir ) $(MAKE ) -C bench install
287+ @sbindir=$(sbindir ) bindir=$(bindir ) docdir=$(docdir ) confdir=$(confdir ) $(MAKE ) -C bench O= $( OUTPUT ) install
263288
264289install : all install-lib install-tools install-man $(INSTALL_NLS ) $(INSTALL_BENCH )
265290
0 commit comments