Add comments to fork() logic for readibility
[notion/jeffpc.git] / build / rules.mk
blob24be08ccb0cc663c0502d7b40052349edd63e353
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-depend
38 .PHONY: subdirs-install
39 .PHONY: _install
40 .PHONY: _depend
41 .PHONY: _exports
43 all: subdirs _exports $(TARGETS)
45 clean: subdirs-clean _clean
47 realclean: subdirs-realclean _clean _realclean
49 depend: subdirs-depend _depend
51 install: subdirs-install _install
54 ifdef MAKE_EXPORTS
56 # Exports
57 ######################################
59 EXPORTS_C = exports.c
60 EXPORTS_H = exports.h
62 DEPEND_DEPENDS += $(EXPORTS_H)
64 TO_CLEAN := $(TO_CLEAN) $(EXPORTS_C) $(EXPORTS_H)
66 _exports: $(EXPORTS_C)
68 $(EXPORTS_H): $(EXPORTS_C)
70 $(EXPORTS_C): $(SOURCES) $(MKEXPORTS_EXTRA_DEPS)
71 $(MKEXPORTS) -module $(MAKE_EXPORTS) -o $(EXPORTS_C) -h $(EXPORTS_H) \
72 $(SOURCES) $(MKEXPORTS_EXTRAS)
74 # Exports documentation
75 ######################################
77 EXPORTS_DOC = exports.tex
79 TO_CLEAN := $(TO_CLEAN) $(EXPORTS_DOC)
81 _exports_doc: $(EXPORTS_DOC)
83 $(EXPORTS_DOC): $(SOURCES) $(LUA_SOURCES) $(MKEXPORTS_EXTRA_DEPS)
84 $(MKEXPORTS) -mkdoc -module $(MAKE_EXPORTS) -o $(EXPORTS_DOC) \
85 $(SOURCES) $(LUA_SOURCES) $(MKEXPORTS_EXTRAS)
87 else # !MAKE_EXPORTS
89 EXPORTS_C =
90 EXPORTS_H =
91 EXPORTS_DOC =
93 endif # !MAKE_EXPORTS
96 # Compilation and linking
97 ######################################
99 OBJS=$(subst .c,.o,$(SOURCES) $(EXPORTS_C))
102 ifdef EXECUTABLE
104 ifdef MODULE_LIST
105 ifdef MODULE_PATH
106 ifeq ($(PRELOAD_MODULES),1)
107 EXT_OBJS += $(foreach mod, $(MODULE_LIST), $(MODULE_PATH)/$(mod)/$(mod).a)
108 DEPEND_DEPENDS += preload.c
109 SOURCES += preload.c
110 TO_CLEAN += preload.c
111 else # !PRELOAD_MODULES
112 LDFLAGS += $(EXPORT_DYNAMIC)
113 WHOLEA = -Wl,-whole-archive
114 NO_WHOLEA = -Wl,-no-whole-archive
115 endif # !PRELOAD_MODULES
117 preload.c:
118 $(LUA) $(TOPDIR)/build/mkpreload.lua $(MODULE_LIST) > preload.c
120 endif # MODULE_PATH
121 endif # MODULE_LIST
123 ifeq ($(RELOCATABLE),1)
124 DEFINES += -DCF_RELOCATABLE_BIN_LOCATION=\"$(BINDIR_)/$(EXECUTABLE)\"
125 endif
127 DEFINES += -DCF_EXECUTABLE=\"$(EXECUTABLE)\"
129 $(EXECUTABLE_): $(OBJS) $(EXT_OBJS)
130 $(CC) $(OBJS) $(WHOLEA) $(EXT_OBJS) $(NO_WHOLEA) $(LDFLAGS) -o $@
132 executable_install:
133 $(INSTALLDIR) $(DESTDIR)$(BINDIR_)
134 $(INSTALLBIN) $(EXECUTABLE_) $(DESTDIR)$(BINDIR_)
136 endif # EXECUTABLE
139 ifdef MODULE
141 ifneq ($(PRELOAD_MODULES),1)
143 CC_PICFLAGS=-fPIC -DPIC
144 LD_SHAREDFLAGS=-shared
146 %.o: %.c $(EXPORTS_H)
147 $(CC) $(CC_PICFLAGS) $(CFLAGS) -c $< -o $@
149 # notion might not link to Xext, so modules will have to link to it themselves
150 # if they need it:
151 LIBS += $(X11_LIBS)
153 $(MODULE).so: $(OBJS) $(EXT_OBJS)
154 $(CC) $(LD_SHAREDFLAGS) $(LDFLAGS) $(OBJS) $(EXT_OBJS) $(LIBS) -o $@
157 module_install: module_stub_install
158 $(INSTALLDIR) $(DESTDIR)$(MODULEDIR)
159 $(INSTALLBIN) $(MODULE).so $(DESTDIR)$(MODULEDIR)
161 else # PRELOAD_MODULES
163 PICOPT=-fPIC -DPIC
164 LINKOPT=-shared
166 %.o: %.c $(EXPORTS_H)
167 $(CC) $(CFLAGS) -c $< -o $@
169 $(MODULE).a: $(OBJS) $(EXT_OBJS)
170 $(AR) $(ARFLAGS) $@ $+
171 $(RANLIB) $@
173 module_install: module_stub_install
175 endif # PRELOAD_MODULES
177 module_stub_install:
178 $(INSTALLDIR) $(DESTDIR)$(LCDIR)
179 $(INSTALL) -m $(DATA_MODE) $(MODULE).lc $(DESTDIR)$(LCDIR)
181 ifndef MODULE_STUB
183 $(MODULE).lc:
184 echo "ioncore.load_module('$(MODULE)') package.loaded['$(MODULE)']=true" | $(LUAC) -o $@ -
185 else
187 LUA_SOURCES += $(MODULE_STUB)
189 endif #MODULE_STUB
191 else # !MODULE
194 %.o: %.c $(EXPORTS_H)
195 $(CC) $(CFLAGS) -c $< -o $@
198 endif# !MODULE
201 # Clean rules
202 ######################################
204 _clean:
205 $(RM) -f $(TO_CLEAN) core $(DEPEND_FILE) $(OBJS)
207 _realclean:
208 $(RM) -f $(TO_REALCLEAN) $(TARGETS)
210 # Lua rules
211 ######################################
213 %.lc: %.lua
214 $(LUAC) -o $@ $<
216 lc_install:
217 $(INSTALLDIR) $(DESTDIR)$(LCDIR)
218 for i in $(LUA_COMPILED); do \
219 $(INSTALL) -m $(DATA_MODE) $$i $(DESTDIR)$(LCDIR); \
220 done
222 etc_install:
223 $(INSTALLDIR) $(DESTDIR)$(ETCDIR)
224 for i in $(ETC); do \
225 $(INSTALL) -m $(DATA_MODE) $$i $(DESTDIR)$(ETCDIR); \
226 done
228 # Dependencies
229 ######################################
231 ifdef SOURCES
233 _depend: $(DEPEND_DEPENDS)
234 $(MAKE_DEPEND)
236 ifeq ($(DEPEND_FILE),$(wildcard $(DEPEND_FILE)))
237 include $(DEPEND_FILE)
238 endif
240 endif
242 # Subdirectories
243 ######################################
245 ifdef SUBDIRS
247 subdirs:
248 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done
250 subdirs-depend:
251 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i depend; done
253 subdirs-clean:
254 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done
256 subdirs-realclean:
257 set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i realclean; done
259 subdirs-install:
260 set -e; for i in $(INSTALL_SUBDIRS); do $(MAKE) -C $$i install; done
262 endif
264 # Localisation
265 ######################################
267 TO_CLEAN += potfiles_c potfiles_lua
269 _potfiles:
270 echo "$(SOURCES)"|tr ' ' '\n' > potfiles_c
271 echo "$(LUA_SOURCES) $(ETC)"|tr ' ' '\n' > potfiles_lua
273 # Defaults
274 ######################################
276 INSTALL_STRIP ?= -s
277 INSTALLBIN ?= $(INSTALL) $(INSTALL_STRIP) -m $(BIN_MODE)