# # 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 # 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 # # 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 = \ -isystem $(LIBC_PREFIX)/include \ -isystem $(LIBC_PREFIX)/arch/$(UARCH)/include \ -isystem $(ROOT_PATH)/abi/arch/$(KARCH)/include \ -isystem $(ROOT_PATH)/abi/include LIBCPP_PREFIX = $(LIB_PREFIX)/cpp LIBCPP_INCLUDES_FLAGS = -isystem $(LIBCPP_PREFIX)/include LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o LDFLAGS = -Wl,--fatal-warnings,--warn-common STATIC_BUILD = n ifeq ($(STATIC_NEEDED),y) STATIC_BUILD = y endif ifneq ($(CONFIG_BUILD_SHARED_LIBS),y) STATIC_BUILD = y endif ifneq ($(CONFIG_USE_SHARED_LIBS),y) ifeq ($(LIBRARY),) STATIC_BUILD = y endif endif # LINK_DYNAMIC is only here to allow app/dltest to link dynamically in otherwise static build. ifeq ($(LINK_DYNAMIC),y) STATIC_BUILD = n endif ifeq ($(STATIC_BUILD),y) LDFLAGS += -static endif BASE_LIBS = ifneq ($(LIBRARY),libc) ifeq ($(STATIC_BUILD),y) BASE_LIBS += $(LIBC_PREFIX)/libc.a else BASE_LIBS += $(LIBC_PREFIX)/libc.so.0 endif endif BASE_LIBS += -lgcc 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) DEPLIBS := $(LIBS) ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),) ifneq ($(LIBRARY),libcpp) DEPLIBS += cpp endif endif ifneq ($(LIBRARY),libc) DEPLIBS += c endif # PCUT-based unit tests ifneq ($(TEST_SOURCES),) TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS) 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 # Flags that are not necessary, and can be overriden, but are used by default. DEFAULT_CFLAGS = \ -O$(OPTIMIZATION) \ -ffunction-sections \ -fno-builtin-strftime \ -pipe \ -Wall \ -Wextra \ -Wno-unused-parameter \ -Wmissing-prototypes \ -Wwrite-strings \ -Werror-implicit-function-declaration \ -Wsystem-headers \ -Wunknown-pragmas \ -Wa,--fatal-warnings # XXX: -fno-builtin-strftime is for a seemingly spurious format warning. ifeq ($(CONFIG_DEBUG),y) DEFAULT_CFLAGS += -Werror endif ifeq ($(CONFIG_UBSAN),y) DEFAULT_CFLAGS += -fsanitize=undefined endif ifeq ($(COMPILER),clang) DEFAULT_CFLAGS += \ -Wno-missing-braces \ -Wno-missing-field-initializers \ -Wno-typedef-redefinition \ -Wno-unused-command-line-argument else DEFAULT_CFLAGS += \ -Wno-nonnull-compare \ -Wno-clobbered endif ifeq ($(CONFIG_LINE_DEBUG),y) DEFAULT_CFLAGS += -ggdb endif # Flags that should always be used, even for third-party software. COMMON_CPPFLAGS = \ -D__$(ENDIANESS)__ COMMON_CFLAGS = \ -nostdlib # Flags that are always used for HelenOS code, but not for third-party. HELENOS_CFLAGS = \ -std=gnu11 \ $(INCLUDES_FLAGS) \ -imacros $(CONFIG_HEADER) \ -D_HELENOS_SOURCE \ -fexec-charset=UTF-8 \ -finput-charset=UTF-8 \ -fno-common \ -fdebug-prefix-map=$(realpath $(ROOT_PATH))=. # TODO: Use a different name. # CFLAGS variable is traditionally used for overridable flags. CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS) # Flags for the compilation of C++ code. CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS) DEFAULT_CXXFLAGS = \ -O$(OPTIMIZATION) \ -ffunction-sections \ -pipe \ -Wall \ -Wextra \ -Wno-unused-parameter \ -Wwrite-strings \ -Werror-implicit-function-declaration ifeq ($(CONFIG_DEBUG),y) DEFAULT_CXXFLAGS += -Werror endif COMMON_CXXFLAGS = $(COMMON_CFLAGS) -fno-exceptions HELENOS_CXXFLAGS = \ -std=c++17 -frtti \ $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \ -imacros $(CONFIG_HEADER) \ -D_HELENOS_SOURCE \ -fexec-charset=UTF-8 \ -finput-charset=UTF-8 \ -fno-common \ -fdebug-prefix-map=$(realpath $(ROOT_PATH))=. CXXFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CXXFLAGS) $(HELENOS_CXXFLAGS) $(DEFAULT_CXXFLAGS) ## Setup platform configuration # -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common ## Compilation options # ifeq ($(PRECHECK),y) JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py # NOTE: You must not change the order of arguments. CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@ CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@ else CC_JOB = $(CC) $< -o $@ CXX_JOB = $(CXX) $< -o $@ endif ifeq ($(CONFIG_STRIP_BINARIES),y) LDFLAGS += -s endif LIB_CFLAGS = $(CFLAGS) -fPIC LIB_CXXFLAGS = $(CXXFLAGS) -fPIC LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined 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,$(DEPLIBS), $(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: fasterclean find . -name '*.o' -follow -exec rm \{\} \; find . -name '*.lo' -follow -exec rm \{\} \; find . -name '*.d' -follow -exec rm \{\} \; fasterclean: rm -rf $(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 $(DEPLIBS); 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),) ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),) $(BINARY): $(OBJECTS) $(LIBTAGS) $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS) else $(BINARY): $(OBJECTS) $(LIBTAGS) $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS) endif endif ifneq ($(TEST_BINARY),) $(TEST_BINARY): $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS) $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(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): $(LIBRARY).la $(LIBTAGS) $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS) $(LSONAME): ln -s $(SLIBRARY) $@ endif %.o: %.S | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) %.o: %.s | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) %.o: %.c | depend $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) %.o: %.cpp | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS) %.o: %.cxx | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS) %.o: %.cc | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS) %.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) %.lo: %.s | depend $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) %.lo: %.c | depend $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) %.lo: %.cpp | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS) %.lo: %.cxx | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS) %.lo: %.cc | depend $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS) -include $(DEPENDS)