first public release
[dpdk.git] / mk / internal / rte.compile-pre.mk
1 #   BSD LICENSE
2
3 #   Copyright(c) 2010-2012 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 #  version: DPDK.L.1.2.3-3
33
34 #
35 # Common to rte.lib.mk, rte.app.mk, rte.obj.mk
36 #
37
38 SRCS-all := $(SRCS-y) $(SRCS-n) $(SRCS-)
39
40 # convert source to obj file
41 src2obj = $(strip $(patsubst %.c,%.o,\
42         $(patsubst %.S,%_s.o,$(1))))
43
44 # add a dot in front of the file name
45 dotfile = $(strip $(foreach f,$(1),\
46         $(join $(dir $f),.$(notdir $f))))
47
48 # convert source/obj files into dot-dep filename (does not
49 # include .S files)
50 src2dep = $(strip $(call dotfile,$(patsubst %.c,%.o.d, \
51                 $(patsubst %.S,,$(1)))))
52 obj2dep = $(strip $(call dotfile,$(patsubst %.o,%.o.d,$(1))))
53
54 # convert source/obj files into dot-cmd filename
55 src2cmd = $(strip $(call dotfile,$(patsubst %.c,%.o.cmd, \
56                 $(patsubst %.S,%_s.o.cmd,$(1)))))
57 obj2cmd = $(strip $(call dotfile,$(patsubst %.o,%.o.cmd,$(1))))
58
59 OBJS-y := $(call src2obj,$(SRCS-y))
60 OBJS-n := $(call src2obj,$(SRCS-n))
61 OBJS-  := $(call src2obj,$(SRCS-))
62 OBJS-all := $(filter-out $(SRCS-all),$(OBJS-y) $(OBJS-n) $(OBJS-))
63
64 DEPS-y := $(call src2dep,$(SRCS-y))
65 DEPS-n := $(call src2dep,$(SRCS-n))
66 DEPS-  := $(call src2dep,$(SRCS-))
67 DEPS-all := $(DEPS-y) $(DEPS-n) $(DEPS-)
68 DEPSTMP-all := $(DEPS-all:%.d=%.d.tmp)
69
70 CMDS-y := $(call src2cmd,$(SRCS-y))
71 CMDS-n := $(call src2cmd,$(SRCS-n))
72 CMDS-  := $(call src2cmd,$(SRCS-))
73 CMDS-all := $(CMDS-y) $(CMDS-n) $(CMDS-)
74
75 -include $(DEPS-y) $(CMDS-y)
76
77 # command to compile a .c file to generate an object
78 ifeq ($(USE_HOST),1)
79 C_TO_O = $(HOSTCC) -Wp,-MD,$(call obj2dep,$(@)).tmp $(HOST_CFLAGS) \
80         $(CFLAGS_$(@)) $(HOST_EXTRA_CFLAGS) -o $@ -c $<
81 C_TO_O_STR = $(subst ','\'',$(C_TO_O)) #'# fix syntax highlight
82 C_TO_O_DISP = $(if $(V),"$(C_TO_O_STR)","  HOSTCC $(@)")
83 else
84 C_TO_O = $(CC) -Wp,-MD,$(call obj2dep,$(@)).tmp $(CFLAGS) \
85         $(CFLAGS_$(@)) $(EXTRA_CFLAGS) -o $@ -c $<
86 C_TO_O_STR = $(subst ','\'',$(C_TO_O)) #'# fix syntax highlight
87 C_TO_O_DISP = $(if $(V),"$(C_TO_O_STR)","  CC $(@)")
88 endif
89 C_TO_O_CMD = "cmd_$@ = $(C_TO_O_STR)"
90 C_TO_O_DO = @set -e; \
91         echo $(C_TO_O_DISP); \
92         $(C_TO_O) && \
93         echo $(C_TO_O_CMD) > $(call obj2cmd,$(@)) && \
94         sed 's,'$@':,dep_'$@' =,' $(call obj2dep,$(@)).tmp > $(call obj2dep,$(@)) && \
95         rm -f $(call obj2dep,$(@)).tmp
96
97 # return an empty string if string are equal
98 compare = $(strip $(subst $(1),,$(2)) $(subst $(2),,$(1)))
99
100 # return a non-empty string if the dst file does not exist
101 file_missing = $(call compare,$(wildcard $@),$@)
102
103 # return a non-empty string if cmdline changed
104 cmdline_changed = $(call compare,$(cmd_$@),$(1))
105
106 # return a non-empty string if a dependency file does not exist
107 depfile_missing = $(call compare,$(wildcard $(dep_$@)),$(dep_$@))
108
109 # return an empty string if no prereq is newer than target
110 #     - $^ -> names of all the prerequisites
111 #     - $(wildcard $^) -> every existing prereq
112 #     - $(filter-out $(wildcard $^),$^) -> every prereq that don't
113 #       exist (filter-out removes existing ones from the list)
114 #     - $? -> names of all the prerequisites newer than target
115 depfile_newer = $(strip $(filter-out FORCE,$? \
116         $(filter-out $(wildcard $^),$^)))
117
118 # return 1 if parameter is a non-empty string, else 0
119 boolean = $(if $1,1,0)
120
121 #
122 # Compile .c file if needed
123 # Note: dep_$$@ is from the .d file and DEP_$$@ can be specified by
124 # user (by default it is empty)
125 #
126 .SECONDEXPANSION:
127 %.o: %.c $$(wildcard $$(dep_$$@)) $$(DEP_$$(@)) FORCE
128         @[ -d $(dir $@) ] || mkdir -p $(dir $@)
129         $(if $(D),\
130                 @echo -n "$< -> $@ " ; \
131                 echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
132                 echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(C_TO_O_STR))) " ; \
133                 echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
134                 echo "depfile_newer=$(call boolean,$(depfile_newer))")
135         $(if $(or \
136                 $(file_missing),\
137                 $(call cmdline_changed,$(C_TO_O_STR)),\
138                 $(depfile_missing),\
139                 $(depfile_newer)),\
140                 $(C_TO_O_DO))
141
142 # command to assemble a .S file to generate an object
143 ifeq ($(USE_HOST),1)
144 S_TO_O = $(CPP) $(HOST_CPPFLAGS) $($(@)_CPPFLAGS) $(HOST_EXTRA_CPPFLAGS) $< $(@).tmp && \
145         $(HOSTAS) $(HOST_ASFLAGS) $($(@)_ASFLAGS) $(HOST_EXTRA_ASFLAGS) -o $@ $(@).tmp
146 S_TO_O_STR = $(subst ','\'',$(S_TO_O)) #'# fix syntax highlight
147 S_TO_O_DISP =  $(if $(V),"$(S_TO_O_STR)","  HOSTAS $(@)")
148 else
149 S_TO_O = $(CPP) $(CPPFLAGS) $($(@)_CPPFLAGS) $(EXTRA_CPPFLAGS) $< -o $(@).tmp && \
150         $(AS) $(ASFLAGS) $($(@)_ASFLAGS) $(EXTRA_ASFLAGS) -o $@ $(@).tmp
151 S_TO_O_STR = $(subst ','\'',$(S_TO_O)) #'# fix syntax highlight
152 S_TO_O_DISP =  $(if $(V),"$(S_TO_O_STR)","  AS $(@)")
153 endif
154
155 S_TO_O_CMD = "cmd_$@ = $(S_TO_O_STR)"
156 S_TO_O_DO = @set -e; \
157         echo $(S_TO_O_DISP); \
158         $(S_TO_O) && \
159         echo $(S_TO_O_CMD) > $(call obj2cmd,$(@))
160
161 #
162 # Compile .S file if needed
163 # Note: DEP_$$@ can be specified by user (by default it is empty)
164 #
165 %_s.o: %.S $$(DEP_$$@) FORCE
166         @[ ! -d $(dir $@) ] || mkdir -p $(dir $@)
167         $(if $(D),\
168                 @echo -n "$< -> $@ " ; \
169                 echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
170                 echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(S_TO_O_STR))) " ; \
171                 echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
172                 echo "depfile_newer=$(call boolean,$(depfile_newer)) ")
173         $(if $(or \
174                 $(file_missing),\
175                 $(call cmdline_changed,$(S_TO_O_STR)),\
176                 $(depfile_missing),\
177                 $(depfile_newer)),\
178                 $(S_TO_O_DO))