9c25ff6c25e86a0552af57f6b2fac84c03826991
[dpdk.git] / mk / internal / rte.compile-pre.mk
1 #   BSD LICENSE
2 #
3 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
4 #   All rights reserved.
5 #
6 #   Redistribution and use in source and binary forms, with or without
7 #   modification, are permitted provided that the following conditions
8 #   are met:
9 #
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above copyright
13 #       notice, this list of conditions and the following disclaimer in
14 #       the documentation and/or other materials provided with the
15 #       distribution.
16 #     * Neither the name of Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19 #
20 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 #   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 #   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 #   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 #   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 #   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 #   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #
33 # Common to rte.lib.mk, rte.app.mk, rte.obj.mk
34 #
35
36 SRCS-all := $(SRCS-y) $(SRCS-n) $(SRCS-)
37
38 # convert source to obj file
39 src2obj = $(strip $(patsubst %.c,%.o,\
40         $(patsubst %.S,%_s.o,$(1))))
41
42 # add a dot in front of the file name
43 dotfile = $(strip $(foreach f,$(1),\
44         $(join $(dir $f),.$(notdir $f))))
45
46 # convert source/obj files into dot-dep filename (does not
47 # include .S files)
48 src2dep = $(strip $(call dotfile,$(patsubst %.c,%.o.d, \
49                 $(patsubst %.S,,$(1)))))
50 obj2dep = $(strip $(call dotfile,$(patsubst %.o,%.o.d,$(1))))
51
52 # convert source/obj files into dot-cmd filename
53 src2cmd = $(strip $(call dotfile,$(patsubst %.c,%.o.cmd, \
54                 $(patsubst %.S,%_s.o.cmd,$(1)))))
55 obj2cmd = $(strip $(call dotfile,$(patsubst %.o,%.o.cmd,$(1))))
56
57 OBJS-y := $(call src2obj,$(SRCS-y))
58 OBJS-n := $(call src2obj,$(SRCS-n))
59 OBJS-  := $(call src2obj,$(SRCS-))
60 OBJS-all := $(filter-out $(SRCS-all),$(OBJS-y) $(OBJS-n) $(OBJS-))
61
62 DEPS-y := $(call src2dep,$(SRCS-y))
63 DEPS-n := $(call src2dep,$(SRCS-n))
64 DEPS-  := $(call src2dep,$(SRCS-))
65 DEPS-all := $(DEPS-y) $(DEPS-n) $(DEPS-)
66 DEPSTMP-all := $(DEPS-all:%.d=%.d.tmp)
67
68 CMDS-y := $(call src2cmd,$(SRCS-y))
69 CMDS-n := $(call src2cmd,$(SRCS-n))
70 CMDS-  := $(call src2cmd,$(SRCS-))
71 CMDS-all := $(CMDS-y) $(CMDS-n) $(CMDS-)
72
73 -include $(DEPS-y) $(CMDS-y)
74
75 # command to compile a .c file to generate an object
76 ifeq ($(USE_HOST),1)
77 C_TO_O = $(HOSTCC) -Wp,-MD,$(call obj2dep,$(@)).tmp $(HOST_CFLAGS) \
78         $(CFLAGS_$(@)) $(HOST_EXTRA_CFLAGS) -o $@ -c $<
79 C_TO_O_STR = $(subst ','\'',$(C_TO_O)) #'# fix syntax highlight
80 C_TO_O_DISP = $(if $(V),"$(C_TO_O_STR)","  HOSTCC $(@)")
81 else
82 C_TO_O = $(CC) -Wp,-MD,$(call obj2dep,$(@)).tmp $(CFLAGS) \
83         $(CFLAGS_$(@)) $(EXTRA_CFLAGS) -o $@ -c $<
84 C_TO_O_STR = $(subst ','\'',$(C_TO_O)) #'# fix syntax highlight
85 C_TO_O_DISP = $(if $(V),"$(C_TO_O_STR)","  CC $(@)")
86 endif
87 PMDINFO_GEN = $(RTE_SDK_BIN)/app/pmdinfogen $@ $@.pmd.c
88 PMDINFO_CC = $(CC) $(CFLAGS) -c -o $@.pmd.o $@.pmd.c
89 PMDINFO_LD = $(CROSS)ld $(LDFLAGS) -r -o $@.o $@.pmd.o $@
90 PMDINFO_TO_O = if grep -q 'PMD_REGISTER_DRIVER(.*)' $<; then \
91         echo "$(if $V,$(PMDINFO_GEN),  PMDINFO $@.pmd.c)" && \
92         $(PMDINFO_GEN) && \
93         echo "$(if $V,$(PMDINFO_CC),  CC $@.pmd.o)" && \
94         $(PMDINFO_CC) && \
95         echo "$(if $V,$(PMDINFO_LD),  LD $@)" && \
96         $(PMDINFO_LD) && \
97         mv -f $@.o $@; fi
98 C_TO_O_CMD = 'cmd_$@ = $(C_TO_O_STR)'
99 C_TO_O_DO = @set -e; \
100         echo $(C_TO_O_DISP); \
101         $(C_TO_O) && \
102         $(PMDINFO_TO_O) && \
103         echo $(C_TO_O_CMD) > $(call obj2cmd,$(@)) && \
104         sed 's,'$@':,dep_'$@' =,' $(call obj2dep,$(@)).tmp > $(call obj2dep,$(@)) && \
105         rm -f $(call obj2dep,$(@)).tmp
106
107 # return an empty string if string are equal
108 compare = $(strip $(subst $(1),,$(2)) $(subst $(2),,$(1)))
109
110 # return a non-empty string if the dst file does not exist
111 file_missing = $(call compare,$(wildcard $@),$@)
112
113 # return a non-empty string if cmdline changed
114 cmdline_changed = $(call compare,$(strip $(cmd_$@)),$(strip $(1)))
115
116 # return a non-empty string if a dependency file does not exist
117 depfile_missing = $(call compare,$(wildcard $(dep_$@)),$(dep_$@))
118
119 # return an empty string if no prereq is newer than target
120 #     - $^ -> names of all the prerequisites
121 #     - $(wildcard $^) -> every existing prereq
122 #     - $(filter-out $(wildcard $^),$^) -> every prereq that don't
123 #       exist (filter-out removes existing ones from the list)
124 #     - $? -> names of all the prerequisites newer than target
125 depfile_newer = $(strip $(filter-out FORCE,$? \
126         $(filter-out $(wildcard $^),$^)))
127
128 # return 1 if parameter is a non-empty string, else 0
129 boolean = $(if $1,1,0)
130
131 #
132 # Compile .c file if needed
133 # Note: dep_$$@ is from the .d file and DEP_$$@ can be specified by
134 # user (by default it is empty)
135 #
136 .SECONDEXPANSION:
137 %.o: %.c $$(wildcard $$(dep_$$@)) $$(DEP_$$(@)) FORCE
138         @[ -d $(dir $@) ] || mkdir -p $(dir $@)
139         $(if $(D),\
140                 @echo -n "$< -> $@ " ; \
141                 echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
142                 echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(C_TO_O))) " ; \
143                 echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
144                 echo "depfile_newer=$(call boolean,$(depfile_newer))")
145         $(if $(or \
146                 $(file_missing),\
147                 $(call cmdline_changed,$(C_TO_O)),\
148                 $(depfile_missing),\
149                 $(depfile_newer)),\
150                 $(C_TO_O_DO))
151
152 # command to assemble a .S file to generate an object
153 ifeq ($(USE_HOST),1)
154 S_TO_O = $(CPP) $(HOST_CPPFLAGS) $($(@)_CPPFLAGS) $(HOST_EXTRA_CPPFLAGS) $< $(@).tmp && \
155         $(HOSTAS) $(HOST_ASFLAGS) $($(@)_ASFLAGS) $(HOST_EXTRA_ASFLAGS) -o $@ $(@).tmp
156 S_TO_O_STR = $(subst ','\'',$(S_TO_O)) #'# fix syntax highlight
157 S_TO_O_DISP =  $(if $(V),"$(S_TO_O_STR)","  HOSTAS $(@)")
158 else
159 S_TO_O = $(CPP) $(CPPFLAGS) $($(@)_CPPFLAGS) $(EXTRA_CPPFLAGS) $< -o $(@).tmp && \
160         $(AS) $(ASFLAGS) $($(@)_ASFLAGS) $(EXTRA_ASFLAGS) -o $@ $(@).tmp
161 S_TO_O_STR = $(subst ','\'',$(S_TO_O)) #'# fix syntax highlight
162 S_TO_O_DISP =  $(if $(V),"$(S_TO_O_STR)","  AS $(@)")
163 endif
164
165 S_TO_O_CMD = "cmd_$@ = $(S_TO_O_STR)"
166 S_TO_O_DO = @set -e; \
167         echo $(S_TO_O_DISP); \
168         $(S_TO_O) && \
169         echo $(S_TO_O_CMD) > $(call obj2cmd,$(@))
170
171 #
172 # Compile .S file if needed
173 # Note: DEP_$$@ can be specified by user (by default it is empty)
174 #
175 %_s.o: %.S $$(DEP_$$@) FORCE
176         @[ ! -d $(dir $@) ] || mkdir -p $(dir $@)
177         $(if $(D),\
178                 @echo -n "$< -> $@ " ; \
179                 echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
180                 echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(S_TO_O_STR))) " ; \
181                 echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
182                 echo "depfile_newer=$(call boolean,$(depfile_newer)) ")
183         $(if $(or \
184                 $(file_missing),\
185                 $(call cmdline_changed,$(S_TO_O_STR)),\
186                 $(depfile_missing),\
187                 $(depfile_newer)),\
188                 $(S_TO_O_DO))