source: mainline/uspace/Makefile.common@ f67d8ee

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

Use default linker scripts (as provided by the linker) in uspace.

There have been some issues before, but they appear to have been resolved
since then.

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