Released version 3-2015061300
[notion.git] / build / rules.mk
blobcbd1a94da6757e35d6b9a807bc3b0daa42583818
1 ##
2 ## Some make rules
3 ##
5 ifdef MODULE
6 ifeq ($(PRELOAD_MODULES),1)
7 MODULE_TARGETS := $(MODULE).a $(MODULE).lc
8 else
9 MODULE_TARGETS := $(MODULE).so $(MODULE).lc
10 endif
11 TARGETS := $(TARGETS) $(MODULE_TARGETS)
12 endif
14 ifdef LUA_SOURCES
15 LUA_COMPILED := $(subst .lua,.lc, $(LUA_SOURCES))
16 TARGETS := $(TARGETS) $(LUA_COMPILED)
17 endif
19 ifdef EXTRA_EXECUTABLE
20 EXECUTABLE := $(EXTRA_EXECUTABLE)
21 BINDIR_ := $(EXTRABINDIR)
22 endif
24 ifdef EXECUTABLE
25 BINDIR_ ?= $(BINDIR)
26 EXECUTABLE_ := $(EXECUTABLE)$(BIN_SUFFIX)
27 TARGETS := $(TARGETS) $(EXECUTABLE_)
28 endif
31 # Main targets
32 ######################################
34 .PHONY: subdirs
35 .PHONY: subdirs-clean
36 .PHONY: subdirs-realclean
37 .PHONY: subdirs-install
38 .PHONY: _install
39 .PHONY: _exports
41 all: subdirs _exports $(TARGETS)
43 clean: subdirs-clean _clean
45 realclean: subdirs-realclean _clean _realclean
47 install: subdirs-install _install
50 ifdef MAKE_EXPORTS
52 # Exports
53 ######################################
55 EXPORTS_C = exports.c
56 EXPORTS_H = exports.h
58 TO_CLEAN := $(TO_CLEAN) $(EXPORTS_C) $(EXPORTS_H)
60 _exports: $(EXPORTS_C)
62 # this funny syntax (more than one pattern-based target) is meant to tell Make
63 # that this rule makes BOTH of these targets. Look at the last paragraph of
64 # http://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html
65 %xports.c %xports.h: $(SOURCES) $(MKEXPORTS_EXTRA_DEPS)
66 $(MKEXPORTS) -module $(MAKE_EXPORTS) -o $(EXPORTS_C) -h $(EXPORTS_H) \
67 $(SOURCES) $(MKEXPORTS_EXTRAS)
69 # Exports documentation
70 ######################################
72 EXPORTS_DOC = exports.tex
74 TO_CLEAN := $(TO_CLEAN) $(EXPORTS_DOC)
76 _exports_doc: $(EXPORTS_DOC)
78 $(EXPORTS_DOC): $(SOURCES) $(LUA_SOURCES) $(MKEXPORTS_EXTRA_DEPS)
79 $(MKEXPORTS) -mkdoc -module $(MAKE_EXPORTS) -o $(EXPORTS_DOC) \
80 $(SOURCES) $(LUA_SOURCES) $(MKEXPORTS_EXTRAS)
82 else # !MAKE_EXPORTS
84 EXPORTS_C =
85 EXPORTS_H =
86 EXPORTS_DOC =
88 endif # !MAKE_EXPORTS
91 # Compilation and linking
92 ######################################
94 OBJS=$(subst .c,.o,$(SOURCES) $(EXPORTS_C))
97 ifdef EXECUTABLE
99 ifdef MODULE_LIST
100 ifdef MODULE_PATH
101 ifeq ($(PRELOAD_MODULES),1)
102 EXT_OBJS += $(foreach mod, $(MODULE_LIST), $(MODULE_PATH)/$(mod)/$(mod).a)
103 SOURCES += preload.c
104 TO_CLEAN += preload.c
105 else # !PRELOAD_MODULES
106 LDFLAGS += $(EXPORT_DYNAMIC)
107 WHOLEA = -Wl,-whole-archive
108 NO_WHOLEA = -Wl,-no-whole-archive
109 endif # !PRELOAD_MODULES
111 preload.c:
112 $(LUA) $(TOPDIR)/build/mkpreload.lua $(MODULE_LIST) > preload.c
114 endif # MODULE_PATH
115 endif # MODULE_LIST
117 ifeq ($(RELOCATABLE),1)
118 DEFINES += -DCF_RELOCATABLE_BIN_LOCATION=\"$(BINDIR_)/$(EXECUTABLE)\"
119 endif
121 DEFINES += -DCF_EXECUTABLE=\"$(EXECUTABLE)\"
123 $(EXECUTABLE_): $(OBJS) $(EXT_OBJS)
124 $(CC) $(OBJS) $(WHOLEA) $(EXT_OBJS) $(NO_WHOLEA) $(LDFLAGS) -o $@
126 executable_install:
127 $(INSTALLDIR) $(DESTDIR)$(BINDIR_)
128 $(INSTALLBIN) $(EXECUTABLE_) $(DESTDIR)$(BINDIR_)
130 endif # EXECUTABLE
134 %.o: %.c
135 $(CC) $(CFLAGS) -c $< -o $@
137 ifdef MODULE
139 ifneq ($(PRELOAD_MODULES),1)
141 CFLAGS += -fPIC -DPIC
142 LD_SHAREDFLAGS=-shared
144 # notion might not link to Xext, so modules will have to link to it themselves
145 # if they need it:
146 LIBS += $(X11_LIBS)
148 $(MODULE).so: $(OBJS) $(EXT_OBJS)
149 $(CC) $(LD_SHAREDFLAGS) $(LDFLAGS) $(OBJS) $(EXT_OBJS) $(LIBS) -o $@
152 module_install: module_stub_install
153 $(INSTALLDIR) $(DESTDIR)$(MODULEDIR)
154 $(INSTALLBIN) $(MODULE).so $(DESTDIR)$(MODULEDIR)
156 else # PRELOAD_MODULES
158 PICOPT=-fPIC -DPIC
159 LINKOPT=-shared
161 $(MODULE).a: $(OBJS) $(EXT_OBJS)
162 $(AR) $(ARFLAGS) $@ $+
163 $(RANLIB) $@
165 module_install: module_stub_install
167 endif # PRELOAD_MODULES
169 module_stub_install:
170 $(INSTALLDIR) $(DESTDIR)$(LCDIR)
171 $(INSTALL) -m $(DATA_MODE) $(MODULE).lc $(DESTDIR)$(LCDIR)
173 ifndef MODULE_STUB
175 $(MODULE).lc:
176 echo "ioncore.load_module('$(MODULE)') package.loaded['$(MODULE)']=true" | $(LUAC) -o $@ -
177 else
179 LUA_SOURCES += $(MODULE_STUB)
181 endif #MODULE_STUB
183 endif# !MODULE
186 # Clean rules
187 ######################################
189 _clean:
190 $(RM) -f $(TO_CLEAN) core *.d $(OBJS)
192 _realclean:
193 $(RM) -f $(TO_REALCLEAN) $(TARGETS)
195 # Lua rules
196 ######################################
198 %.lc: %.lua
199 $(LUAC) -o $@ $<
201 lc_install:
202 $(INSTALLDIR) $(DESTDIR)$(LCDIR)
203 for i in $(LUA_COMPILED); do \
204 $(INSTALL) -m $(DATA_MODE) $$i $(DESTDIR)$(LCDIR); \
205 done
207 etc_install:
208 $(INSTALLDIR) $(DESTDIR)$(ETCDIR)
209 for i in $(ETC); do \
210 $(INSTALL) -m $(DATA_MODE) $$i $(DESTDIR)$(ETCDIR); \
211 done
213 # Dependencies
214 ######################################
216 CFLAGS += -MMD
217 -include *.d
219 # Subdirectories
220 ######################################
222 ifdef SUBDIRS
224 subdirs:
225 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done
227 subdirs-clean:
228 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done
230 subdirs-realclean:
231 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i realclean; done
233 subdirs-install:
234 set -e; for i in $(INSTALL_SUBDIRS); do $(MAKE) -C $$i install; done
236 endif
238 # Localisation
239 ######################################
241 TO_CLEAN += potfiles_c potfiles_lua
243 _potfiles:
244 echo "$(SOURCES)"|tr ' ' '\n' > potfiles_c
245 echo "$(LUA_SOURCES) $(ETC)"|tr ' ' '\n' > potfiles_lua
247 # Defaults
248 ######################################
250 INSTALL_STRIP ?= -s
251 INSTALLBIN ?= $(INSTALL) $(INSTALL_STRIP) -m $(BIN_MODE)