source: mainline/uspace/Makefile.common@ 38d8849

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38d8849 was 2eadda9, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Remove undefined references to main program from shared libc.

  • Property mode set to 100644
File size: 11.6 KB
Line 
1#
2# Copyright (c) 2005 Martin Decky
3# Copyright (c) 2007 Jakub Jermar
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 the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30# Individual makefiles set:
31#
32# USPACE_PREFIX (*) relative path to uspace/ directory
33# SOURCES (*) list of source files
34# LIBS libraries to link with
35# DEFS compiler defines
36# EXTRA_CFLAGS additional flags to pass to C compiler
37# LINKER_SCRIPT linker script
38# PRE_DEPEND targets required for dependency check
39#
40# BINARY (/) binary output name (like appname)
41# LIBRARY (/) library output name (like libname)
42#
43# EXTRA_OUTPUT additional output targets
44# EXTRA_CLEAN additional cleanup targets
45#
46# Optionally, for a binary:
47# STATIC_NEEDED set to 'y' for init binaries, will build statically
48# linked version
49# STATIC_ONLY set to 'y' if binary cannot be linked dynamically
50# (e.g. uses thread-local variables)
51#
52# Optionally, for a library:
53# SOVERSION shared library version (major.minor),
54# if missing, no shared library is built
55#
56# (x) required variables
57# (/) exactly one of the variables must be defined
58#
59
60ROOT_PATH = $(USPACE_PREFIX)/..
61
62VERSION_DEF = $(ROOT_PATH)/version
63
64COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
65COMMON_HEADER = $(ROOT_PATH)/common.h
66
67CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
68CONFIG_HEADER = $(ROOT_PATH)/config.h
69
70-include $(VERSION_DEF)
71-include $(COMMON_MAKEFILE)
72-include $(CONFIG_MAKEFILE)
73
74OUTPUTS = $(EXTRA_OUTPUT)
75
76ifneq ($(BINARY),)
77 JOB = $(BINARY).job
78 TEST_BINARY = test-$(BINARY)
79 OUTPUTS += $(BINARY) $(BINARY).disasm
80 EXTRA_CLEAN += $(BINARY).map
81endif
82
83ifneq ($(LIBRARY),)
84 JOB = $(LIBRARY).job
85 TEST_BINARY = test-$(LIBRARY)
86 OUTPUTS += $(LIBRARY).a
87endif
88
89ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
90 ifneq ($(SOVERSION),)
91 SLIBRARY = $(LIBRARY).so.$(SOVERSION)
92 LSONAME = $(LIBRARY).so.$(basename $(SOVERSION))
93 OUTPUTS += $(SLIBRARY) $(SLIBRARY).disasm $(LSONAME)
94 EXTRA_CLEAN += $(LIBRARY).la $(SLIBRARY).map
95 endif
96endif
97
98LIB_PREFIX = $(USPACE_PREFIX)/lib
99
100LIBC_PREFIX = $(LIB_PREFIX)/c
101LIBC_INCLUDES_FLAGS = \
102 -I$(LIBC_PREFIX)/include \
103 -I$(LIBC_PREFIX)/arch/$(UARCH)/include \
104 -I$(ROOT_PATH)/abi/include
105
106LIBCPP_PREFIX = $(LIB_PREFIX)/cpp
107LIBCPP_INCLUDES_FLAGS = -I$(LIBCPP_PREFIX)/include
108
109LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat
110LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
111
112LIBMATH_PREFIX = $(LIB_PREFIX)/math
113LIBMATH_INCLUDES_FLAGS = \
114 -I$(LIBMATH_PREFIX)/include \
115 -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include
116
117LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
118LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
119
120START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o
121
122AFLAGS = --fatal-warnings
123LDFLAGS = -Wl,--fatal-warnings,--warn-common
124
125ifeq ($(STATIC_NEEDED),y)
126 STATIC_BUILD = y
127else
128 ifeq ($(STATIC_ONLY),y)
129 STATIC_BUILD = y
130 else
131 ifeq ($(CONFIG_USE_SHARED_LIBS),y)
132 STATIC_BUILD = n
133 else
134 STATIC_BUILD = y
135 endif
136 endif
137endif
138
139ifneq ($(STATIC_BUILD),y)
140 LINK_DYNAMIC = y
141endif
142
143BASE_LIBS =
144
145ifneq ($(LIBRARY),libc)
146 ifeq ($(STATIC_BUILD),y)
147 BASE_LIBS += $(LIBC_PREFIX)/libc.a
148 else
149 BASE_LIBS += $(LIBC_PREFIX)/libc.so.0
150 endif
151endif
152
153BASE_LIBS += $(LIBSOFTFLOAT_PREFIX)/libsoftfloat.a $(LIBSOFTINT_PREFIX)/libsoftint.a
154
155ifeq ($(LINK_DYNAMIC),y)
156 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
157else
158 LDFLAGS += -static
159 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
160endif
161
162LIB_LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
163
164INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
165
166ifneq ($(LIBRARY),)
167 INCLUDES_FLAGS += -Iinclude -I.
168endif
169
170INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
171
172# TODO: get rid of this special case
173ifneq ($(filter math, $(LIBS)),)
174 INCLUDES_FLAGS += $(LIBMATH_INCLUDES_FLAGS)
175endif
176
177# PCUT-based unit tests
178ifneq ($(TEST_SOURCES),)
179 TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
180 TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS)
181 TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
182 EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
183ifneq ($(LIBRARY),)
184 TEST_BINARY_LIBS += $(LIBRARY).a
185endif
186 TEST_BINARY_LIBS += $(TEST_LIBS)
187endif
188
189# Flags that are not necessary, and can be overriden, but are used by default.
190DEFAULT_CFLAGS = \
191 -O$(OPTIMIZATION) \
192 -ffunction-sections \
193 -pipe \
194 -Wall \
195 -Wextra \
196 -Wno-unused-parameter \
197 -Wmissing-prototypes \
198 -Wwrite-strings \
199 -Werror-implicit-function-declaration
200
201ifeq ($(CONFIG_DEBUG),y)
202 DEFAULT_CFLAGS += -Werror
203endif
204
205ifeq ($(CONFIG_UBSAN),y)
206 DEFAULT_CFLAGS += -fsanitize=undefined
207endif
208
209ifeq ($(COMPILER),clang)
210 DEFAULT_CFLAGS += \
211 -Wno-missing-field-initializers \
212 -Wno-typedef-redefinition \
213 -Wno-unused-command-line-argument
214else
215 DEFAULT_CFLAGS += \
216 -Wno-clobbered
217endif
218
219ifeq ($(CONFIG_LINE_DEBUG),y)
220 DEFAULT_CFLAGS += -ggdb
221endif
222
223# Flags that should always be used, even for third-party software.
224COMMON_CPPFLAGS = \
225 -nostdinc \
226 -D__$(ENDIANESS)__
227
228COMMON_CFLAGS = \
229 -ffreestanding \
230 -nostdlib
231
232# Flags that are always used for HelenOS code, but not for third-party.
233HELENOS_CFLAGS = \
234 -std=gnu99 \
235 $(INCLUDES_FLAGS) \
236 -imacros $(CONFIG_HEADER) \
237 -D_HELENOS_SOURCE \
238 -fexec-charset=UTF-8 \
239 -finput-charset=UTF-8 \
240 -fno-common \
241 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
242
243# TODO: Use a different name.
244# CFLAGS variable is traditionally used for overridable flags.
245CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
246
247# Flags for the compilation of C++ code.
248CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS)
249DEFAULT_CXXFLAGS = \
250 -O$(OPTIMIZATION) \
251 -ffunction-sections \
252 -pipe \
253 -Wall \
254 -Wextra \
255 -Wno-unused-parameter \
256 -Wwrite-strings \
257 -Werror-implicit-function-declaration
258
259COMMON_CXXFLAGS = $(COMMON_CFLAGS)
260HELENOS_CXXFLAGS = \
261 -std=c++17 -frtti \
262 $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \
263 -imacros $(CONFIG_HEADER) \
264 -D_HELENOS_SOURCE \
265 -fexec-charset=UTF-8 \
266 -finput-charset=UTF-8 \
267 -fno-common \
268 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
269
270CXXFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CXXFLAGS) $(HELENOS_CXXFLAGS) $(DEFAULT_CXXFLAGS)
271
272## Setup platform configuration
273#
274
275-include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
276
277## Compilation options
278#
279
280ifeq ($(PRECHECK),y)
281 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
282 # XXX: Do not change the order of arguments.
283 CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
284 CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@
285else
286 CC_JOB = $(CC) $< -o $@
287 CXX_JOB = $(CXX) $< -o $@
288endif
289
290ifeq ($(CONFIG_STRIP_BINARIES),y)
291 LDFLAGS += -s
292endif
293
294LIB_CFLAGS = $(CFLAGS) -fPIC
295LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined
296
297AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
298
299OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
300LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
301TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
302DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
303
304LIBTAGS := $(foreach lib,$(LIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
305LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
306
307.PHONY: all all-test clean fasterclean depend
308
309all: tag $(OUTPUTS)
310
311all-test: $(TEST_OUTPUTS)
312
313clean: fasterclean
314 find . -name '*.o' -follow -exec rm \{\} \;
315 find . -name '*.lo' -follow -exec rm \{\} \;
316 find . -name '*.d' -follow -exec rm \{\} \;
317
318fasterclean:
319 rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
320
321depend: $(PRE_DEPEND)
322
323# "Tag" files are used to force relink of binaries when dependencies get rebuilt,
324# regardless of whether the dependency was linked statically or dynamically,
325# or which version of a dynamic library was used. Prerequisites for this file
326# are defined further down.
327tag:
328 touch tag
329
330# Generate inter-module make dependencies.
331# This is needed to ensure correct build order of libraries and code depending on them.
332deps.mk: Makefile
333 echo > $@.new
334 for lib in $(LIBS); do \
335 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
336 done
337 mv -f $@.new $@
338
339%.disasm: %
340ifeq ($(CONFIG_LINE_DEBUG),y)
341 $(OBJDUMP) -d -S $< > $@
342else
343 $(OBJDUMP) -d $< > $@
344endif
345
346ifneq ($(BINARY),)
347
348ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
349$(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS)
350 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS)
351else
352$(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS)
353 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
354endif
355
356endif
357
358ifneq ($(TEST_BINARY),)
359$(TEST_BINARY): $(LINKER_SCRIPT) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS)
360 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS)
361endif
362
363ifneq ($(LIBRARY),)
364tag: $(LIBRARY).a
365
366$(LIBRARY).a: $(OBJECTS)
367 $(AR) rc $@ $(OBJECTS)
368endif
369
370ifneq ($(SLIBRARY),)
371tag: $(SLIBRARY)
372
373$(LIBRARY).la: $(LOBJECTS)
374 $(AR) rc $@ $(LOBJECTS)
375
376$(SLIBRARY): $(LIB_LINKER_SCRIPT) $(LIBRARY).la
377 $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -T $(LIB_LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS)
378
379$(LSONAME):
380 ln -s $(SLIBRARY) $@
381endif
382
383%.o: %.S | depend
384 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
385
386%.o: %.s | depend
387 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
388
389%.o: %.c | depend
390 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
391
392%.o: %.cpp | depend
393 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
394
395%.o: %.cxx | depend
396 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
397
398%.o: %.cc | depend
399 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
400
401%.test.o: %.c | depend
402 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
403
404%.lo: %.S | depend
405 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
406
407%.lo: %.s | depend
408 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
409
410%.lo: %.c | depend
411 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
412
413%.lo: %.cpp | depend
414 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
415
416%.lo: %.cxx | depend
417 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
418
419%.lo: %.cc | depend
420 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
421
422-include $(DEPENDS)
423
Note: See TracBrowser for help on using the repository browser.