initial revision
[ucgine.git] / mk / ucgine-exe-vars.mk
1 #
2 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 #     * Redistributions of source code must retain the above copyright
8 #       notice, this list of conditions and the following disclaimer.
9 #     * Redistributions in binary form must reproduce the above copyright
10 #       notice, this list of conditions and the following disclaimer in the
11 #       documentation and/or other materials provided with the distribution.
12 #     * Neither the name of the University of California, Berkeley nor the
13 #       names of its contributors may be used to endorse or promote products
14 #       derived from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 # exe-y-$(exe) is provided by the user
29 #   $(exe) is the path of the binary, and the variable contains
30 #   the list of sources. Several exe-y-$(exe) can be present.
31
32 # list all exe builds requested by user
33 all-exe := $(patsubst exe-y-%,%,$(filter exe-y-%,$(.VARIABLES)))
34
35 # add them to the list of targets
36 all-targets += $(all-exe)
37
38 # for each exe, create the following variables:
39 #   out-$(exe) = output path of the executable
40 #   pre-$(exe) = list of prerequisites for this executable
41 # Some source files need intermediate objects, we define these variables
42 # for them too, and add them in a list: $(all-iobj).
43 # Last, we add the generated files in $(all-clean-file).
44 $(foreach exe,$(all-exe),\
45         $(eval out-$(exe) := $(dir $(exe))) \
46         $(eval pre-$(exe) := ) \
47         $(foreach src,$(exe-y-$(exe)), \
48                 $(if $(call is_cc_source,$(src)), \
49                         $(eval iobj := $(call src2iobj,$(src),$(out-$(exe)))) \
50                         $(eval pre-$(iobj) := $(src)) \
51                         $(eval all-iobj += $(iobj)) \
52                         $(eval all-clean-file += $(iobj)) \
53                         $(eval pre-$(exe) += $(iobj)) \
54                 , \
55                 $(if $(call is_obj_source,$(src)),\
56                         $(eval pre-$(exe) += $(src)) \
57                 , \
58                 $(if $(call is_alib_source,$(src)),\
59                         $(eval pre-$(exe) += $(src)) \
60                 , \
61                 $(error "unsupported source format: $(src)")))) \
62         )\
63         $(eval all-clean-file += $(exe)) \
64 )
65
66 # link several *.o files into a exeary
67 #   $1: sources (*.o) (*.a)
68 #   $2: dst (xyz.o too)
69 link_cmd = $(CC) $(LDFLAGS) $(ldflags-$(2)) -o $(2) $(filter %.o,$(1)) \
70          $(filter %.a,$(1)) $(LDLIBS) $(ldlibs-$(2))
71
72 # print line used to link object files
73 ifeq ($(V),1)
74 link_print_cmd = echo $(call protect_quote,$(call link_cmd,$1,$2))
75 else
76 link_print_cmd = echo "  EXE $(2)"
77 endif
78
79 all-clean-file += $(all-exe)