Makefile.top 5.12 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
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
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
31 32 33 34
	STATIC_LOCAL := 1
	STACIC_SYSTEM := 1
endif
ifdef STATIC_LOCAL
35
	CFLAGS_ALL += -static -DSTATIC
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
36 37
endif
ifdef STATIC_SYSTEM
38 39
	LDFLAGS_ALL += -static
endif
40 41
CFLAGS += $(addprefix -I,$(EXTRA_INCLUDES))
LDFLAGS += $(addprefix -L,$(EXTRA_LIBDIRS))
42 43 44
ifeq ($(ENABLE_PYTHON), 1)
	CFLAGS_ALL += $(shell python2.7-config --includes)
endif
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
45 46 47
ifeq ($(LUA_COMPILE),1)
	CFLAGS_ALL += -DLUA_COMPILE
endif
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

# 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))
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
64 65
$(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))
66 67
$(1): CFLAGS_ALL += $$($(2)_CFLAGS)
$(1): LDFLAGS_ALL += $$($(2)_LDFLAGS)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
68 69 70 71 72 73 74 75
ifdef STATIC_LOCAL
$(1): LINK_LIBRARIES += $$(patsubst %,$(O)/lib/lib%.a,$$($(2)_LOCAL_LIBS))
$(1): $$(patsubst %,$(O)/lib/lib%.a,$$($(2)_LOCAL_LIBS))
else
$(1): LINK_LIBRARIES += -L$(O)/lib -Wl,-R$(abspath $(O)/lib) $$(addprefix -l,$$($(2)_LOCAL_LIBS))
endif
ifdef STATIC_GLOBAL
# TODO: The system libraries?
76
else
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
77
$(1): LINK_LIBRARIES += $$(addprefix -l,$$($(2)_SYSTEM_LIBS))
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 123 124 125 126 127 128 129 130 131 132 133
$(1): LINK_SO_LIBRARIES += $$(addprefix -l,$$($(2)_SO_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

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
134 135 136 137 138 139 140 141 142
# 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

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
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 $@)
171
	$(Q)$(CC) $(filter %.o,$^) -o $@ $(LDFLAGS_ALL) $(LINK_LIBRARIES)
172 173 174 175 176 177 178 179 180 181

$(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 $@)
182
	$(Q)$(CC) -o $@ $(filter %.o,$^) $(LDFLAGS_ALL) $(LINK_SO_LIBRARIES) -shared
183 184 185 186 187

$(O)/docs/%:
	$(M) DOC-HTML $@
	$(Q)mkdir -p $(dir $@)
	$(Q)$(ASCIIDOC) -o $@ $<
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
188 189 190 191 192

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