# # Copyright (c) 2005 Martin Decky # Copyright (c) 2007 Jakub Jermar # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # - 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. # - The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. # # Individual makefiles set: # # USPACE_PREFIX (*) relative path to uspace/ directory # SOURCES (*) list of source files # LIBS libraries to link with # DEFS compiler defines # EXTRA_CFLAGS additional flags to pass to C compiler # LINKER_SCRIPT linker script # PRE_DEPEND targets required for dependency check # # BINARY (/) binary output name (like appname) # LIBRARY (/) library output name (like libname) # # EXTRA_OUTPUT additional output targets # EXTRA_CLEAN additional cleanup targets # # Optionally, for a binary: # STATIC_NEEDED set to 'y' for init binaries, will build statically # linked version # STATIC_ONLY set to 'y' if binary cannot be linked dynamically # (e.g. uses thread-local variables) # # Optionally, for a library: # SOVERSION shared library version (major.minor), # if missing, no shared library is built # # (x) required variables # (/) exactly one of the variables must be defined # ROOT_PATH = $(USPACE_PREFIX)/.. VERSION_DEF = $(ROOT_PATH)/version COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common COMMON_HEADER = $(ROOT_PATH)/common.h CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config CONFIG_HEADER = $(ROOT_PATH)/config.h -include $(VERSION_DEF) -include $(COMMON_MAKEFILE) -include $(CONFIG_MAKEFILE) OUTPUTS = $(EXTRA_OUTPUT) ifneq ($(BINARY),) JOB = $(BINARY).job TEST_BINARY = test-$(BINARY) OUTPUTS += $(BINARY) $(BINARY).disasm EXTRA_CLEAN += $(BINARY).map endif ifneq ($(LIBRARY),) JOB = $(LIBRARY).job TEST_BINARY = test-$(LIBRARY) OUTPUTS += $(LIBRARY).a endif ifeq ($(CONFIG_BUILD_SHARED_LIBS),y) ifneq ($(SOVERSION),) SLIBRARY = $(LIBRARY).so.$(SOVERSION) LSONAME = $(LIBRARY).so.$(basename $(SOVERSION)) OUTPUTS += $(SLIBRARY) $(SLIBRARY).disasm $(LSONAME) EXTRA_CLEAN += $(LIBRARY).la $(SLIBRARY).map endif endif LIB_PREFIX = $(USPACE_PREFIX)/lib LIBC_PREFIX = $(LIB_PREFIX)/c LIBC_INCLUDES_FLAGS = \ -I$(LIBC_PREFIX)/include \ -I$(LIBC_PREFIX)/arch/$(UARCH)/include \ -I$(ROOT_PATH)/abi/include LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint LIBMATH_PREFIX = $(LIB_PREFIX)/math LIBMATH_INCLUDES_FLAGS = \ -I$(LIBMATH_PREFIX)/include \ -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest AFLAGS = --fatal-warnings LFLAGS = --fatal-warnings --warn-common ifeq ($(STATIC_NEEDED),y) STATIC_BUILD = y else ifeq ($(STATIC_ONLY),y) STATIC_BUILD = y else ifeq ($(CONFIG_USE_SHARED_LIBS),y) STATIC_BUILD = n else STATIC_BUILD = y endif endif endif ifeq ($(STATIC_BUILD),y) BASE_LIBS = $(LIBC_PREFIX)/libc.a else BASE_LIBS = $(LIBC_PREFIX)/libc.so.0 LINK_DYNAMIC = y endif BASE_LIBS += $(LIBSOFTFLOAT_PREFIX)/libsoftfloat.a $(LIBSOFTINT_PREFIX)/libsoftint.a ifeq ($(LINK_DYNAMIC),y) LFLAGS += -Bdynamic LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld else LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld endif LIB_LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS) ifneq ($(LIBRARY),) INCLUDES_FLAGS += -Iinclude -I. endif INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include) # TODO: get rid of this special case ifneq ($(filter math, $(LIBS)),) INCLUDES_FLAGS += $(LIBMATH_INCLUDES_FLAGS) endif # PCUT-based unit tests ifneq ($(TEST_SOURCES),) TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map ifneq ($(LIBRARY),) TEST_BINARY_LIBS += $(LIBRARY).a endif TEST_BINARY_LIBS += $(TEST_LIBS) endif COMMON_CFLAGS = $(INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \ -ffreestanding -fno-builtin -nostdlib -nostdinc -fexec-charset=UTF-8 \ -finput-charset=UTF-8 -D__$(ENDIANESS)__ -fno-common \ -fdebug-prefix-map=$(realpath $(ROOT_PATH))=. GCC_CFLAGS = -ffunction-sections -Wall -Wextra -Wno-clobbered \ -Wno-unused-parameter -Wmissing-prototypes -std=gnu99 \ -Werror-implicit-function-declaration \ -Wwrite-strings -pipe # -Wno-missing-prototypes is there because it warns about main(). # This should be fixed elsewhere. CLANG_CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wno-typedef-redefinition \ -Wno-missing-prototypes -Wno-unused-command-line-argument \ -std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \ -pipe -fno-stack-protector -fno-PIC ifeq ($(CONFIG_DEBUG),y) COMMON_CFLAGS += -Werror endif ifeq ($(CONFIG_LINE_DEBUG),y) GCC_CFLAGS += -ggdb CLANG_CFLAGS += -g endif ## Setup platform configuration # -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common ## Compilation options # ifeq ($(PRECHECK),y) JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py # XXX: Do not change the order of arguments. CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@ else CC_JOB = $(CC) $< -o $@ endif ifeq ($(COMPILER),clang) CFLAGS += $(COMMON_CFLAGS) $(CLANG_CFLAGS) else CFLAGS += $(COMMON_CFLAGS) $(GCC_CFLAGS) endif ifeq ($(CONFIG_STRIP_BINARIES),y) LFLAGS += --strip-all endif LIB_CFLAGS = $(CFLAGS) -fPIC LIB_LFLAGS = $(LFLAGS) -shared -soname $(LSONAME) AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS)) LD_CFLAGS := $(addprefix -Xlinker ,$(LFLAGS)) OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES))) TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES))) DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES))) LIBTAGS := $(foreach lib,$(LIBS), $(USPACE_PREFIX)/lib/$(lib)/tag) LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS)) .PHONY: all all-test clean fasterclean depend all: tag $(OUTPUTS) all-test: $(TEST_OUTPUTS) clean: rm -f $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk find . -name '*.o' -follow -exec rm \{\} \; find . -name '*.lo' -follow -exec rm \{\} \; find . -name '*.d' -follow -exec rm \{\} \; fasterclean: rm -f $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk depend: $(PRE_DEPEND) # "Tag" files are used to force relink of binaries when dependencies get rebuilt, # regardless of whether the dependency was linked statically or dynamically, # or which version of a dynamic library was used. Prerequisites for this file # are defined further down. tag: touch tag # Generate inter-module make dependencies. # This is needed to ensure correct build order of libraries and code depending on them. deps.mk: Makefile echo > $@.new for lib in $(LIBS); do \ echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \ done mv -f $@.new $@ %.disasm: % ifeq ($(CONFIG_LINE_DEBUG),y) $(OBJDUMP) -d -S $< > $@ else $(OBJDUMP) -d $< > $@ endif ifneq ($(BINARY),) $(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS) $(BASE_LIBS) $(LD) $(LFLAGS) -T $(LINKER_SCRIPT) -Map $@.map -o $@ $(OBJECTS) $(LIBARGS) $(BASE_LIBS) endif ifneq ($(TEST_BINARY),) $(TEST_BINARY): $(LINKER_SCRIPT) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS) $(BASE_LIBS) $(LD) $(LFLAGS) -T $(LINKER_SCRIPT) -Map $@.map -o $@ $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS) endif ifneq ($(LIBRARY),) tag: $(LIBRARY).a $(LIBRARY).a: $(OBJECTS) $(AR) rc $@ $(OBJECTS) endif ifneq ($(SLIBRARY),) tag: $(SLIBRARY) $(LIBRARY).la: $(LOBJECTS) $(AR) rc $@ $(LOBJECTS) $(SLIBRARY): $(LIB_LINKER_SCRIPT) $(LIBRARY).la $(LD) $(LIB_LFLAGS) -T $(LIB_LINKER_SCRIPT) -Map $@.map -o $@ --whole-archive $(LIBRARY).la --no-whole-archive $(LSONAME): ln -s $(SLIBRARY) $@ endif %.o: %.S | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS) -D__ASM__ %.o: %.s | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS) -D__ASM__ %.o: %.c | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) %.test.o: %.c | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS) %.lo: %.S | depend $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS) -D__ASM__ %.lo: %.s | depend $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS) -D__ASM__ %.lo: %.c | depend $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) -include $(DEPENDS)