aboutsummaryrefslogtreecommitdiff
path: root/apps/mk/app.mk
blob: 1d8b0e44c1203d31ead55dee2c9b56e5760c43c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
############################################################################
#
#   Copyright (C) 2012 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

#
# Common Makefile for nsh command modules and utility libraries; should be 
# included by the module-specific Makefile.
#
# To build an app that appears as an nsh external command, the caller 
# must define:
#
# APPNAME	- the name of the application, defaults to the name
#		  of the parent directory.
#
# If APPNAME is not defined, a utility library is built instead. The library
# name is normally automatically determined, but it can be overridden by
# setting:
#
# LIBNAME	- the name of the library, defaults to the name of the
#		  directory
#
# The calling makefile may also set:
#
# ASRCS		- list of assembly source files, defaults to all .S 
#		  files in the directory
#
# CSRCS		- list of C source files, defaults to all .c files
#		  in the directory
#
# CXXSRCS	- list of C++ source files, defaults to all .cpp
#		  files in the directory
#
# INCLUDES	- list of directories to be added to the include
#		  search path
#
# PRIORITY	- thread priority for the command (defaults to 
#		  SCHED_PRIORITY_DEFAULT)
#
# STACKSIZE	- stack size for the command (defaults to 
#		  CONFIG_PTHREAD_STACK_DEFAULT)
#
# Symbols in the module are private to the module unless deliberately exported
# using the __EXPORT tag, or DEFAULT_VISIBILITY is set
#

############################################################################
# No user-serviceable parts below
############################################################################


############################################################################
# Work out who included us so we can report decent errors
#
THIS_MAKEFILE	:= $(lastword $(MAKEFILE_LIST))
PARENT_MAKEFILE	:= $(lastword $(filter-out $(THIS_MAKEFILE),$(MAKEFILE_LIST)))

############################################################################
# Get configuration
#
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs

############################################################################
# Sanity-check the information we've been given and set any defaults
#
SRCDIR		?= $(dir $(PARENT_MAKEFILE))
PRIORITY	?= SCHED_PRIORITY_DEFAULT
STACKSIZE	?= CONFIG_PTHREAD_STACK_DEFAULT

INCLUDES	+= $(APPDIR)

ASRCS		?= $(wildcard $(SRCDIR)/*.S)
CSRCS		?= $(wildcard $(SRCDIR)/*.c)
CHDRS		?= $(wildcard $(SRCDIR)/*.h)
CXXSRCS		?= $(wildcard $(SRCDIR)/*.cpp)
CXXHDRS		?= $(wildcard $(SRCDIR)/*.hpp)

# if APPNAME is not set, this is a library
ifeq ($(APPNAME),)
LIBNAME		?= $(lastword $(subst /, ,$(realpath $(SRCDIR))))
endif

# there has to be a source file
ifeq ($(ASRCS)$(CSRCS)$(CXXSRCS),)
$(error $(realpath $(PARENT_MAKEFILE)): at least one of ASRCS, CSRCS or CXXSRCS must be set)
endif

# check that C++ is configured if we have C++ source files and we are building
ifneq ($(CXXSRCS),)
ifneq ($(CONFIG_HAVE_CXX),y)
ifeq ($(MAKECMDGOALS),build)
$(error $(realpath $(PARENT_MAKEFILE)): cannot set CXXSRCS if CONFIG_HAVE_CXX not set in configuration)
endif
endif
endif

############################################################################
# Adjust compilation flags to implement EXPORT
#

ifeq ($(DEFAULT_VISIBILITY),)
DEFAULT_VISIBILITY = hidden
else
DEFAULT_VISIBILITY = default
endif

CFLAGS		+= -fvisibility=$(DEFAULT_VISIBILITY) -include $(APPDIR)/systemlib/visibility.h
CXXFLAGS	+= -fvisibility=$(DEFAULT_VISIBILITY) -include $(APPDIR)/systemlib/visibility.h

############################################################################
# Add extra include directories
#
CFLAGS		+= $(addprefix -I,$(INCLUDES))
CXXFLAGS	+= $(addprefix -I,$(INCLUDES))

############################################################################
# Things we are going to build
#

SRCS		 = $(ASRCS) $(CSRCS) $(CXXSRCS)
AOBJS		 = $(patsubst %.S,%.o,$(ASRCS))
COBJS		 = $(patsubst %.c,%.o,$(CSRCS))
CXXOBJS		 = $(patsubst %.cpp,%.o,$(CXXSRCS))
OBJS		 = $(AOBJS) $(COBJS) $(CXXOBJS)

# The prelinked object that we are ultimately going to build
ifneq ($(APPNAME),)
PRELINKOBJ	 = $(APPNAME).pre.o
else
PRELINKOBJ	 = $(LIBNAME).pre.o
endif

# The archive that the object file will be placed in
# XXX does WINTOOL ever get set?
ifeq ($(WINTOOL),y)
  INCDIROPT	= -w
  BIN		 = "$(shell cygpath -w  $(APPDIR)/libapps$(LIBEXT))"
else
  BIN		 = "$(APPDIR)/libapps$(LIBEXT)"
endif

############################################################################
# Rules for building things
#

all:		.built
.PHONY:		clean depend distclean

#
# Top-level build; add prelinked object to the apps archive
#
.built:		$(PRELINKOBJ)
	@$(call ARCHIVE, $(BIN), $(PRELINKOBJ))
	@touch $@

#
# Source dependencies
#
depend:		.depend
.depend:	$(MAKEFILE_LIST) $(SRCS)
	@$(MKDEP) --dep-path . $(CC) -- $(CFLAGS) -- $(CSRCS) $(CHDRS) >Make.dep
	@$(MKDEP) --dep-path . $(CXX) -- $(CXXFLAGS) -- $(CXXSRCS) $(CXXHDRS) >>Make.dep
	@touch $@

ifneq ($(APPNAME),)
#
# App registration
#
context:	.context
.context:	$(MAKEFILE_LIST)
	$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
	@touch $@
else
context:
endif

#
# Object files
#
$(PRELINKOBJ):	$(OBJS)
	$(call PRELINK, $@, $(OBJS))

$(AOBJS): %.o : %.S
	$(call ASSEMBLE, $<, $@)

$(COBJS): %.o : %.c
	$(call COMPILE, $<, $@)

$(CXXOBJS): %.o : %.cpp
	$(call COMPILEXX, $<, $@)

#
# Tidying up
#
clean:
	@rm -f $(OBJS) $(PRELINKOBJ) Make.dep .built
	$(call CLEAN)

distclean:	clean
	@rm -f Make.dep .depend

-include Make.dep