Makefile.top 4.87 KB
Newer Older
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
all:

# To be filled in by included makefiles
TARGETS =
BINARIES =
DOCS =

# Build compiler flags
ifdef RELEASE
	CFLAGS += -O2
else
	CFLAGS += -ggdb3 -O0 -DDEBUG
endif
ifeq ($(TARGET),mips)
	CC = mips-softfloat-linux-uclibc-gcc
endif
ifdef VERBOSE
	Q=
	M=@\#
else
	Q=@
	M=@echo " "
endif
ifndef PLUGIN_PATH
	PLUGIN_PATH := lib
endif
CFLAGS_ALL += $(CFLAGS) --std=gnu99 -pedantic -Wall -Wextra -DOUTPUT_DIRECTORY=\"$(abspath $(O))\" -DSOURCE_DIRECTORY=\"$(abspath $(S))\" -DPAGE_SIZE=$(PAGE_SIZE) -DPREFIX=$(abspath $(O)) -DMAX_LOG_LEVEL=$(MAX_LOG_LEVEL) -DPLUGIN_PATH=\"$(PLUGIN_PATH)\" $(addprefix,-D,$(EXTRA_DEFINES))
LDFLAGS_ALL += $(LDFLAGS)

ifdef STATIC
	CFLAGS_ALL += -static -DSTATIC
	LDFLAGS_ALL += -static
endif
34 35
CFLAGS += $(addprefix -I,$(EXTRA_INCLUDES))
LDFLAGS += $(addprefix -L,$(EXTRA_LIBDIRS))
36 37 38
ifeq ($(ENABLE_PYTHON), 1)
	CFLAGS_ALL += $(shell python2.7-config --includes)
endif
39 40 41
ifeq ($(LUA_COMPILE),1)
	CFLAGS_ALL += -DLUA_COMPILE
endif
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

# Magic to track dependencies on header files. They are stored in the .d files
# by the compiler, then we load them on the next run and use them.
DEP_FILES := $(shell if test -d $(O)/.deps ; then find $(O)/.deps -name '*.d' ; fi | sort)
include $(DEP_FILES)

# Stuff to generate dependencies and link commands from the target_SOMETHING variables.
ifdef STATIC
LIB_SUFFIX :=.a
else
LIB_SUFFIX :=.so
endif

define COMMON_REGISTER

$(1): $$(patsubst %,$(O)/.objs/$(3)%.o,$$($(2)_MODULES))
58 59
$(1): CFLAGS_ALL += $$($(2)_CFLAGS) $$(foreach CONFIG,$$($(2)_PKG_CONFIGS),$$(shell pkg-config --cflags $$(CONFIG))) $$(foreach CONFIG,$$($(2)_EXE_CONFIGS),$$(shell $$(CONFIG)-config --cflags))
$(1): LDFLAGS_ALL += $$($(2)_LDFLAGS) $$(foreach CONFIG,$$($(2)_PKG_CONFIGS),$$(shell pkg-config --libs $$(CONFIG))) $$(foreach CONFIG,$$($(2)_EXE_CONFIGS),$$(shell $$(CONFIG)-config --libs))
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
$(1): CFLAGS_ALL += $$($(2)_CFLAGS)
$(1): LDFLAGS_ALL += $$($(2)_LDFLAGS)
ifdef STATIC
TODO: Static linking
else
$(1): LINK_LIBRARIES += -L$(O)/lib -Wl,-R$(abspath $(O)/lib) $$(addprefix -l,$$($(2)_LOCAL_LIBS) $$($(2)_SYSTEM_LIBS))
$(1): LINK_SO_LIBRARIES += $$(addprefix -l,$$($(2)_SO_LIBS))
$(1): $$(patsubst %,$(O)/lib/lib%.so,$$($(2)_LOCAL_LIBS))
endif

endef

# Function to register a binary to TARGETS and generate the rules to build it.
# It shall be called as:
#
# binary_name_MODULES := main other_file third_file
# $(eval $(call REGISTER_BIN,binary_name,src/path/to/directory))
define REGISTER_BIN

ifneq (,$(findstring ^$(RESTRICT),^$(1)))
TARGETS += $(O)/$(1)
endif
$(call COMMON_REGISTER,$(O)/bin/$$(notdir $(1)),$$(notdir $(1)),$$(dir $(1)))
$(O)/$(1): $(O)/bin/$$(notdir $(1))
	$(M) LN $$@
	$(Q)mkdir -p $$(dir $(O)/$(1))
	$(Q)ln -sf $$(abspath $$<) $$@

endef

# Similar, for libraries
define REGISTER_LIB

ifneq (,$(findstring ^$(RESTRICT),^$(1)))
TARGETS += $(O)/$(1)$(LIB_SUFFIX)
endif
$(call COMMON_REGISTER,$(O)/lib/$$(notdir $(1)).a,$$(notdir $(1)),$$(dir $(1)))
$(call COMMON_REGISTER,$(O)/lib/$$(notdir $(1)).so,$$(notdir $(1)),$$(dir $(1)))
$(O)/$(1).a: $(O)/lib/$$(notdir $(1)).a
	$(M) LN $$@
	$(Q)mkdir -p $$(dir $(O)/$(1))
	$(Q)ln -sf $$(abspath $$<) $$@
$(O)/$(1).so: $(O)/lib/$$(notdir $(1)).so
	$(M) LN $$@
	$(Q)mkdir -p $$(dir $(O)/$(1))
	$(Q)ln -sf $$(abspath $$<) $$@

endef

# Similar, for documentation
define REGISTER_DOC

ifneq (,$(findstring ^$(RESTRICT),^$(1)))
TARGETS += $(O)/$(1).html
endif
$(O)/docs/$$(notdir $(1)).html: $(S)/$$(addsuffix .txt,$(1))
$(O)/$(1).html: $(O)/docs/$$(notdir $(1)).html
	$(M) LN $$@
	$(Q)mkdir -p $$(dir $(O)/$(1))
	$(Q)ln -sf $$(abspath $$<) $$@

endef

123 124 125 126 127 128 129 130 131
# For lua
define REGISTER_LUA

ifneq (,$(findstring ^$(RESTRICT),^$(1)))
TARGETS_LUA += $(O)/lua_lib/$(notdir $(1)).luac
endif
$(O)/lua_lib/$$(notdir $(1)).luac: $(S)/$$(addsuffix .lua,$(1))
endef

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
define REGISTER_SCRIPT

ifneq (,$(findstring ^$(RESTRICT),^$(1)))
TARGETS += $(O)/$(1)
endif

$(O)/$(1): $(O)/bin/$$(notdir $(1))
	$(M) LN $$@
	$(Q)mkdir -p $$(dir $(O)/$(1))
	$(Q)ln -sf $$(abspath $$<) $$@

$(O)/bin/$$(notdir $(1)): $(S)/$$(dir $(1))/$$($$(notdir $(1))_SOURCE)
	$(M) LN $$@
	$(Q)mkdir -p $(O)/bin/
	$(Q)ln -sf $$(abspath $$<) $$@

endef

# Generic rules to compile and link
$(O)/.objs/%.o: $(S)/%.c
	$(M) CC $@
	$(Q)mkdir -p $(dir $@ $(subst .objs/,.deps/,$@))
	$(Q)$(CC) $(CFLAGS_ALL) -c $< -o $@ -MD -MF $(patsubst %.o,%.pre,$(subst .objs/,.deps/,$@))
	$(Q)$(S)/build/normalize_dep_file.pl $(O) $(patsubst %.o,%.pre,$(subst .objs/,.deps/,$@)) >$(patsubst %.o,%.d,$(subst .objs/,.deps/,$@))

$(O)/bin/%:
	$(M) LD $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(CC) $(LDFLAGS_ALL) $(LINK_LIBRARIES) $(filter %.o,$^) -o $@

$(O)/lib/%.a:
	$(M) AR $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(AR) -rc $@ $(filter %.o,$^)

$(O)/lib/%.so: CFLAGS_ALL += -fPIC
$(O)/lib/%.so:
	$(M) LD $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(CC) $(LDFLAGS_ALL) $(LINK_SO_LIBRARIES) -shared -o $@ $(filter %.o,$^)

$(O)/docs/%:
	$(M) DOC-HTML $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(ASCIIDOC) -o $@ $<
177 178 179 180 181

$(O)/lua_lib/%:
	$(M) LUAC $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(LUAC) -o $@ $<