Rules.make 12.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
#
# This file contains rules which are shared between multiple Makefiles.
#

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Some standard vars

comma   := ,
empty   :=
space   := $(empty) $(empty)

# Figure out paths
# ---------------------------------------------------------------------------
# Find the path relative to the toplevel dir, $(RELDIR), and express
# the toplevel dir as a relative path from this dir, $(TOPDIR_REL)

ifeq ($(findstring $(TOPDIR),$(CURDIR)),)
  # Can only happen when something is built out of tree
  RELDIR := $(CURDIR)
  TOPDIR_REL := $(TOPDIR)
else
  RELDIR := $(subst $(TOPDIR)/,,$(CURDIR))
  TOPDIR_REL := $(subst $(space),,$(foreach d,$(subst /, ,$(RELDIR)),../))
endif
24

25 26 27
# Figure out what we need to build from the various variables
# ===========================================================================

Kai Germaschewski's avatar
Kai Germaschewski committed
28 29 30
# When an object is listed to be built compiled-in and modular,
# only build the compiled-in version

31
obj-m := $(filter-out $(obj-y),$(obj-m))
Linus Torvalds's avatar
Linus Torvalds committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Handle objects in subdirs
# ---------------------------------------------------------------------------
# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
#   and add the directory to the list of dirs to descend into: $(subdir-y)
# o if we encounter foo/ in $(obj-m), remove it from $(obj-m) 
#   and add the directory to the list of dirs to descend into: $(subdir-m)

__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
subdir-y	+= $(__subdir-y)
__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
subdir-m	+= $(__subdir-m)
__subdir-n	:= $(patsubst %/,%,$(filter %/, $(obj-n)))
subdir-n	+= $(__subdir-n)
__subdir-	:= $(patsubst %/,%,$(filter %/, $(obj-)))
subdir-		+= $(__subdir-)
obj-y		:= $(patsubst %/, %/built-in.o, $(obj-y))
obj-m		:= $(filter-out %/, $(obj-m))

# If a dir is selected in $(subdir-y) and also mentioned in $(mod-subdirs),
# add it to $(subdir-m)

Linus Torvalds's avatar
Linus Torvalds committed
54
both-m          := $(filter $(mod-subdirs), $(subdir-y))
55
SUB_DIRS	:= $(subdir-y) $(if $(BUILD_MODULES),$(subdir-m))
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58
MOD_SUB_DIRS	:= $(sort $(subdir-m) $(both-m))
ALL_SUB_DIRS	:= $(sort $(subdir-y) $(subdir-m) $(subdir-n) $(subdir-))

59 60 61 62 63 64
# export.o is never a composite object, since $(export-objs) has a
# fixed meaning (== objects which EXPORT_SYMBOL())
__obj-y = $(filter-out export.o,$(obj-y))
__obj-m = $(filter-out export.o,$(obj-m))

# if $(foo-objs) exists, foo.o is a composite object 
Kai Germaschewski's avatar
Kai Germaschewski committed
65 66
__multi-used-y := $(sort $(foreach m,$(__obj-y), $(if $($(m:.o=-objs)), $(m))))
__multi-used-m := $(sort $(foreach m,$(__obj-m), $(if $($(m:.o=-objs)), $(m))))
67 68 69 70 71 72 73 74 75 76 77

# FIXME: Rip this out later
# Backwards compatibility: if a composite object is listed in
# $(list-multi), skip it here, since the Makefile will have an explicit
# link rule for it

multi-used-y := $(filter-out $(list-multi),$(__multi-used-y))
multi-used-m := $(filter-out $(list-multi),$(__multi-used-m))

# Build list of the parts of our composite objects, our composite
# objects depend on those (obviously)
Kai Germaschewski's avatar
Kai Germaschewski committed
78 79
multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)))
multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)))
80

Kai Germaschewski's avatar
Kai Germaschewski committed
81 82 83 84 85
# $(subdir-obj-y) is the list of objects in $(obj-y) which do not live
# in the local directory
subdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o)))

# Replace multi-part objects by their individual parts, look at local dir only
86
real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $($(m:.o=-objs)),$($(m:.o=-objs)),$(m))) $(EXTRA_TARGETS)
Kai Germaschewski's avatar
Kai Germaschewski committed
87
real-objs-m := $(foreach m, $(obj-m), $(if $($(m:.o=-objs)),$($(m:.o=-objs)),$(m)))
88 89 90 91 92

# ==========================================================================
#
# Get things started.
#
93
first_rule: vmlinux $(if $(BUILD_MODULES),$(obj-m))
94

Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98
#
# Common rules
#

99 100 101
# Compile C sources (.c)
# ---------------------------------------------------------------------------

102 103 104 105 106 107 108 109 110 111 112 113 114
# FIXME: if we don't know if built-in or modular, assume built-in.
# Only happens in Makefiles which override the default first_rule:
modkern_cflags := $(CFLAGS_KERNEL)

$(real-objs-y)      : modkern_cflags := $(CFLAGS_KERNEL)
$(real-objs-y:.o=.i): modkern_cflags := $(CFLAGS_KERNEL)
$(real-objs-y:.o=.s): modkern_cflags := $(CFLAGS_KERNEL)

$(real-objs-m)      : modkern_cflags := $(CFLAGS_MODULE)
$(real-objs-m:.o=.i): modkern_cflags := $(CFLAGS_MODULE)
$(real-objs-m:.o=.s): modkern_cflags := $(CFLAGS_MODULE)

$(export-objs)      : export_flags := $(EXPORT_FLAGS)
115

116
c_flags = $(CFLAGS) $(modkern_cflags) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o) -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F))) $(export_flags)
117

118 119
cmd_cc_s_c = $(CC) $(c_flags) -S $< -o $@

120
%.s: %.c FORCE
121
	$(call if_changed,cmd_cc_s_c)
122 123

cmd_cc_i_c = $(CPP) $(c_flags) $< > $@
Linus Torvalds's avatar
Linus Torvalds committed
124

125
%.i: %.c FORCE
126
	$(call if_changed,cmd_cc_i_c)
127 128

cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
Linus Torvalds's avatar
Linus Torvalds committed
129

130
%.o: %.c FORCE
131
	$(call if_changed,cmd_cc_o_c)
Linus Torvalds's avatar
Linus Torvalds committed
132

133 134
# Compile assembler sources (.S)
# ---------------------------------------------------------------------------
Linus Torvalds's avatar
Linus Torvalds committed
135

136 137 138 139 140 141 142 143 144 145
# FIXME (s.a.)
modkern_aflags := $(AFLAGS_KERNEL)

$(real-objs-y)      : modkern_aflags := $(AFLAGS_KERNEL)
$(real-objs-y:.o=.s): modkern_aflags := $(AFLAGS_KERNEL)

$(real-objs-m)      : modkern_aflags := $(AFLAGS_MODULE)
$(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE)

a_flags = $(AFLAGS) $(modkern_aflags) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
146

147 148
cmd_as_s_S = $(CPP) $(a_flags) $< > $@

149
%.s: %.S FORCE
150
	$(call if_changed,cmd_as_s_S)
151 152

cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
Linus Torvalds's avatar
Linus Torvalds committed
153

154
%.o: %.S FORCE
155
	$(call if_changed,cmd_as_o_S)
Linus Torvalds's avatar
Linus Torvalds committed
156

157
# FIXME
158

Linus Torvalds's avatar
Linus Torvalds committed
159
%.lst: %.c
160
	$(CC) $(c_flags) -g -c -o $*.o $<
Linus Torvalds's avatar
Linus Torvalds committed
161
	$(TOPDIR)/scripts/makelst $* $(TOPDIR) $(OBJDUMP)
162 163 164 165 166 167 168 169 170 171 172 173 174 175


# If a Makefile does define neither O_TARGET nor L_TARGET,
# use a standard O_TARGET named "built-in.o"

ifndef O_TARGET
ifndef L_TARGET
O_TARGET := built-in.o
endif
endif

# Build the compiled-in targets
# ---------------------------------------------------------------------------

176
vmlinux: $(O_TARGET) $(L_TARGET) $(EXTRA_TARGETS) sub_dirs
177

Kai Germaschewski's avatar
Kai Germaschewski committed
178
# To build objects in subdirs, we need to descend into the directories
179
$(sort $(subdir-obj-y)): sub_dirs ;
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184

#
# Rule to compile a set of .o files into one .o file
#
ifdef O_TARGET
185 186 187 188 189
# If the list of objects to link is empty, just create an empty O_TARGET
cmd_link_o_target = $(if $(strip $(obj-y)),\
		      $(LD) $(EXTRA_LDFLAGS) -r -o $@ $(filter $(obj-y), $^),\
		      rm -f $@; $(AR) rcs $@)

190
$(O_TARGET): $(obj-y) FORCE
191
	$(call if_changed,cmd_link_o_target)
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195 196 197
endif # O_TARGET

#
# Rule to compile a set of .o files into one .a file
#
ifdef L_TARGET
198 199
cmd_link_l_target = rm -f $@; $(AR) $(EXTRA_ARFLAGS) rcs $@ $(obj-y)

200
$(L_TARGET): $(obj-y) FORCE
201
	$(call if_changed,cmd_link_l_target)
Linus Torvalds's avatar
Linus Torvalds committed
202 203
endif

204 205 206 207
#
# Rule to link composite objects
#

Kai Germaschewski's avatar
Kai Germaschewski committed
208

209 210
cmd_link_multi = $(LD) $(EXTRA_LDFLAGS) -r -o $@ $(filter $($(basename $@)-objs),$^)

Kai Germaschewski's avatar
Kai Germaschewski committed
211 212 213 214
# We would rather have a list of rules like
# 	foo.o: $(foo-objs)
# but that's not so easy, so we rather make all composite objects depend
# on the set of all their parts
215
$(multi-used-y) : %.o: $(multi-objs-y) FORCE
216 217
	$(call if_changed,cmd_link_multi)

218
$(multi-used-m) : %.o: $(multi-objs-m) FORCE
219
	$(call if_changed,cmd_link_multi)
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223

#
# This make dependencies quickly
#
224
fastdep: FORCE
Linus Torvalds's avatar
Linus Torvalds committed
225
	$(TOPDIR)/scripts/mkdep $(CFLAGS) $(EXTRA_CFLAGS) -- $(wildcard *.[chS]) > .depend
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229 230 231
ifdef ALL_SUB_DIRS
	$(MAKE) $(patsubst %,_sfdep_%,$(ALL_SUB_DIRS)) _FASTDEP_ALL_SUB_DIRS="$(ALL_SUB_DIRS)"
endif

ifdef _FASTDEP_ALL_SUB_DIRS
$(patsubst %,_sfdep_%,$(_FASTDEP_ALL_SUB_DIRS)):
232
	@$(MAKE) -C $(patsubst _sfdep_%,%,$@) fastdep
Linus Torvalds's avatar
Linus Torvalds committed
233 234
endif

235

Linus Torvalds's avatar
Linus Torvalds committed
236 237 238 239
#
# A rule to make subdirectories
#
subdir-list = $(sort $(patsubst %,_subdir_%,$(SUB_DIRS)))
240
sub_dirs: FORCE $(subdir-list)
Linus Torvalds's avatar
Linus Torvalds committed
241 242

ifdef SUB_DIRS
243
$(subdir-list) : FORCE
244
	@$(MAKE) -C $(patsubst _subdir_%,%,$@)
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249
endif

#
# A rule to make modules
#
Kai Germaschewski's avatar
Kai Germaschewski committed
250 251
ifneq "$(strip $(MOD_SUB_DIRS))" ""
.PHONY: $(patsubst %,_modsubdir_%,$(MOD_SUB_DIRS))
252
$(patsubst %,_modsubdir_%,$(MOD_SUB_DIRS)) : FORCE
253
	@$(MAKE) -C $(patsubst _modsubdir_%,%,$@) modules
Linus Torvalds's avatar
Linus Torvalds committed
254

Kai Germaschewski's avatar
Kai Germaschewski committed
255
.PHONY: $(patsubst %,_modinst_%,$(MOD_SUB_DIRS))
256
$(patsubst %,_modinst_%,$(MOD_SUB_DIRS)) : FORCE
257
	@$(MAKE) -C $(patsubst _modinst_%,%,$@) modules_install
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260
endif

.PHONY: modules
261
modules: $(obj-m) FORCE $(patsubst %,_modsubdir_%,$(MOD_SUB_DIRS))
Linus Torvalds's avatar
Linus Torvalds committed
262 263

.PHONY: _modinst__
264
_modinst__: FORCE
Kai Germaschewski's avatar
Kai Germaschewski committed
265
ifneq "$(strip $(obj-m))" ""
266 267
	mkdir -p $(MODLIB)/kernel/$(RELDIR)
	cp $(obj-m) $(MODLIB)/kernel/$(RELDIR)
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270
endif

.PHONY: modules_install
271
modules_install: _modinst__ $(patsubst %,_modinst_%,$(MOD_SUB_DIRS))
Linus Torvalds's avatar
Linus Torvalds committed
272

273 274 275 276 277

# Add FORCE to the prequisites of a target to force it to be always rebuilt.
# ---------------------------------------------------------------------------
.PHONY: FORCE
FORCE:
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280

#
# This is useful for testing
281
# FIXME: really?
Linus Torvalds's avatar
Linus Torvalds committed
282 283 284 285 286 287 288 289 290 291
script:
	$(SCRIPT)

#
# This sets version suffixes on exported symbols
# Separate the object into "normal" objects and "exporting" objects
# Exporting objects are: all objects that define symbol tables
#
ifdef CONFIG_MODULES

292
multi-objs	:= $(foreach m, $(obj-y) $(obj-m), $($(basename $(m))-objs))
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296 297
active-objs	:= $(sort $(multi-objs) $(obj-y) $(obj-m))

ifdef CONFIG_MODVERSIONS
ifneq "$(strip $(export-objs))" ""

298 299
MODINCL := $(TOPDIR)/include/linux/modules
MODPREFIX := $(subst /,-,$(RELDIR))__
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303 304 305 306 307 308 309 310 311

#
# Added the SMP separator to stop module accidents between uniprocessor
# and SMP Intel boxes - AC - from bits by Michael Chastain
#

ifdef CONFIG_SMP
	genksyms_smp_prefix := -p smp_
else
	genksyms_smp_prefix := 
endif

Jaroslav Kysela's avatar
Jaroslav Kysela committed
312 313
$(MODINCL)/$(MODPREFIX)%.ver: %.c
	@if [ ! -r $(MODINCL)/$(MODPREFIX)$*.stamp -o $(MODINCL)/$(MODPREFIX)$*.stamp -ot $< ]; then \
Linus Torvalds's avatar
Linus Torvalds committed
314
		echo '$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -E -D__GENKSYMS__ $<'; \
Linus Torvalds's avatar
Linus Torvalds committed
315
		echo '| $(GENKSYMS) $(genksyms_smp_prefix) -k $(VERSION).$(PATCHLEVEL).$(SUBLEVEL) > $@.tmp'; \
Linus Torvalds's avatar
Linus Torvalds committed
316
		$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -E -D__GENKSYMS__ $< \
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319
		| $(GENKSYMS) $(genksyms_smp_prefix) -k $(VERSION).$(PATCHLEVEL).$(SUBLEVEL) > $@.tmp; \
		if [ -r $@ ] && cmp -s $@ $@.tmp; then echo $@ is unchanged; rm -f $@.tmp; \
		else echo mv $@.tmp $@; mv -f $@.tmp $@; fi; \
Jaroslav Kysela's avatar
Jaroslav Kysela committed
320
	fi; touch $(MODINCL)/$(MODPREFIX)$*.stamp
321

Jaroslav Kysela's avatar
Jaroslav Kysela committed
322
$(addprefix $(MODINCL)/$(MODPREFIX),$(export-objs:.o=.ver)): $(TOPDIR)/include/linux/autoconf.h
Linus Torvalds's avatar
Linus Torvalds committed
323 324

# updates .ver files but not modversions.h
Jaroslav Kysela's avatar
Jaroslav Kysela committed
325
fastdep: $(addprefix $(MODINCL)/$(MODPREFIX),$(export-objs:.o=.ver))
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

# updates .ver files and modversions.h like before (is this needed?)
dep: fastdep update-modverfile

endif # export-objs 

# update modversions.h, but only if it would change
update-modverfile:
	@(echo "#ifndef _LINUX_MODVERSIONS_H";\
	  echo "#define _LINUX_MODVERSIONS_H"; \
	  echo "#include <linux/modsetver.h>"; \
	  cd $(TOPDIR)/include/linux/modules; \
	  for f in *.ver; do \
	    if [ -f $$f ]; then echo "#include <linux/modules/$${f}>"; fi; \
	  done; \
	  echo "#endif"; \
	) > $(TOPDIR)/include/linux/modversions.h.tmp
	@if [ -r $(TOPDIR)/include/linux/modversions.h ] && cmp -s $(TOPDIR)/include/linux/modversions.h $(TOPDIR)/include/linux/modversions.h.tmp; then \
		echo $(TOPDIR)/include/linux/modversions.h was not updated; \
		rm -f $(TOPDIR)/include/linux/modversions.h.tmp; \
	else \
		echo $(TOPDIR)/include/linux/modversions.h was updated; \
		mv -f $(TOPDIR)/include/linux/modversions.h.tmp $(TOPDIR)/include/linux/modversions.h; \
	fi

$(active-objs): $(TOPDIR)/include/linux/modversions.h

else

$(TOPDIR)/include/linux/modversions.h:
	@echo "#include <linux/modsetver.h>" > $@

endif # CONFIG_MODVERSIONS

ifneq "$(strip $(export-objs))" ""
361

362
$(export-objs): $(TOPDIR)/include/linux/modversions.h
363

Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
endif

endif # CONFIG_MODULES

#
# include dependency files if they exist
#
ifneq ($(wildcard .depend),)
include .depend
endif

ifneq ($(wildcard $(TOPDIR)/.hdepend),)
include $(TOPDIR)/.hdepend
endif

379 380 381 382 383 384 385 386
# ---------------------------------------------------------------------------
# Check if command line has changed

# Usage:
# normally one uses rules like
#
# %.o: %.c
# 	<command line>
Linus Torvalds's avatar
Linus Torvalds committed
387
#
388 389
# However, these only rebuild the target when the source has changed,
# but not when e.g. the command or the flags on the command line changed.
Linus Torvalds's avatar
Linus Torvalds committed
390
#
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
# This extension allows to do the following:
#
# command = <command line>
#
# %.o: %.c dummy
#	$(call if_changed,command)
#
# which will make sure to rebuild the target when either its prerequisites
# change or the command line changes
#
# The magic works as follows:
# The addition of dummy to the dependencies causes the rule for rebuilding
# to be always executed. However, the if_changed function will generate
# an empty command when 
# o none of the prequesites changed (i.e $? is empty)
# o the command line did not change (we compare the old command line,
#   which is saved in .<target>.o, to the current command line using
#   the two filter-out commands)
Linus Torvalds's avatar
Linus Torvalds committed
409

410
# read all saved command lines
Linus Torvalds's avatar
Linus Torvalds committed
411

412 413 414
cmd_files := $(wildcard .*.cmd)
ifneq ($(cmd_files),)
  include $(cmd_files)
Linus Torvalds's avatar
Linus Torvalds committed
415 416
endif

417 418 419
# function to only execute the passed command if necessary

if_changed = $(if $(strip $? \
420 421
		          $(filter-out $($(1)),$(cmd_$(@F)))\
			  $(filter-out $(cmd_$(@F)),$($(1)))),\
422
	       @echo '$($(1))' && $($(1)) && echo 'cmd_$@ := $($(1))' > $(@D)/.$(@F).cmd)
423