1## Haiku Generic Makefile Engine v2.6 ##
2
3## Does all the hard work for the Generic Makefile, which only has to specify
4## the project parameters.
5
6## Supports Generic Makefile v2.6.
7##   Probably works with Generic Makefile v2.0+, but support is not guaranteed.
8
9# Determine the CPU type
10MACHINE=$(shell uname -m)
11ifeq ($(MACHINE), BePC)
12	CPU = x86
13else
14	CPU = $(MACHINE)
15endif
16
17# Set up defaults.
18ifeq ($(NAME), )
19	NAME = NameThisApp
20endif
21
22ifeq ($(TYPE), )
23	TYPE = APP
24endif
25
26ifeq ($(APP_MIME_SIG), )
27	APP_MIME = x.vnd-Haiku-$(NAME)
28endif
29
30ifeq ($(DRIVER_PATH), )
31	DRIVER_PATH = misc
32endif
33
34# Set the core tools if they're not already specified.
35MIMESET	:= mimeset
36XRES	:= xres
37RESCOMP	:= rc
38CC		:= $(CC)
39C++		:= $(CXX)
40
41# Set up CFLAGS.
42ifeq ($(strip $(TYPE)), DRIVER)
43	CFLAGS	+= -D_KERNEL_MODE=1
44else
45	CFLAGS +=
46endif
47
48# Set up optimization flags.
49ifeq ($(strip $(OPTIMIZE)), FULL)
50	OPTIMIZER	= -O3
51else
52ifeq ($(strip $(OPTIMIZE)), SOME)
53	OPTIMIZER	= -O1
54else
55ifeq ($(strip $(OPTIMIZE)), NONE)
56	OPTIMIZER	= -O0
57else
58#	OPTIMIZE isn't set, so set it to FULL
59	OPTIMIZER	= -O3
60endif
61endif
62endif
63
64# Set up debug information flags.
65ifeq ($(strip $(DEBUGGER)), TRUE)
66	DEBUG += -g
67	OPTIMIZER = -O0
68endif
69CFLAGS += $(OPTIMIZER) $(DEBUG)
70
71# Set up the warning flags.
72ifeq ($(strip $(WARNINGS)), ALL)
73	CFLAGS += -Wall -Wno-multichar -Wno-ctor-dtor-privacy
74else
75ifeq ($(strip $(WARNINGS)), NONE)
76	CFLAGS += -w
77endif
78endif
79
80# Set up the linker & linker flags.
81ifeq ($(origin LD), default)
82	LD			:= $(CC)
83endif
84LDFLAGS		+= $(DEBUG)
85
86# Binary-type-specific linker flags.
87ifeq ($(strip $(TYPE)), APP)
88	LDFLAGS += -Xlinker -soname=_APP_
89else
90ifeq ($(strip $(TYPE)), SHARED)
91	LDFLAGS += -shared -Xlinker -soname=$(NAME)
92else
93ifeq ($(strip $(TYPE)), DRIVER)
94	LDFLAGS += -nostdlib /boot/system/develop/lib/_KERNEL_ \
95				/boot/system/develop/lib/haiku_version_glue.o
96ifeq ($(CPU), x86_64)
97	LDFLAGS += -z max-page-size=0x1000
98endif
99endif
100endif
101endif
102
103# Get the compiler version.
104CC_VER = $(word 1, $(subst -, , $(subst ., , $(shell $(CC) -dumpversion))))
105
106# Set the object directory if it isn't already.
107OBJ_DIR := objects.$(CPU)-$(CC)$(CC_VER)-$(if $(DEBUGGER),debug,release)
108
109# Output the binary alongside the objects unless another directory is specified.
110ifeq ($(TARGET_DIR), )
111	TARGET_DIR	:= $(OBJ_DIR)
112endif
113
114# NOTE: make doesn't find the target if its name is enclosed in quotes.
115ifeq ($(strip $(TYPE)), STATIC)
116	TARGET		:= $(TARGET_DIR)/$(NAME).a
117else
118	TARGET		:= $(TARGET_DIR)/$(NAME)
119endif
120
121# Psuedo-function for converting a list of source files in SRCS variable to a
122# corresponding list of object files in $(OBJ_DIR)/xxx.o. The "function" strips
123# off the src file suffix (.ccp or .c or whatever) and then strips off the
124# off the directory name, leaving just the root file name. It then appends the
125# .o suffix and prepends the $(OBJ_DIR)/ path
126define SRCS_LIST_TO_OBJS
127	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), \
128	$(basename $(notdir $(file))))))
129endef
130
131define SRCS_LIST_TO_DEPENDS
132	$(addprefix $(OBJ_DIR)/, $(addsuffix .d, $(foreach file, $(SRCS), \
133	$(basename $(notdir $(file))))))
134endef
135
136OBJS = $(SRCS_LIST_TO_OBJS)
137DEPENDS = $(SRCS_LIST_TO_DEPENDS)
138
139# Create a unique list of paths to our sourcefiles and resources.
140SRC_PATHS += $(sort $(foreach file, $(SRCS) $(RSRCS) $(RDEFS), $(dir $(file))))
141
142# Add source paths to VPATH if not already present.
143VPATH :=
144VPATH += $(addprefix :, $(subst  ,:, $(filter-out $($(subst, :, ,$(VPATH))), $(SRC_PATHS))))
145
146# Set up the local & system include paths and C++ stdlibs.
147LOC_INCLUDES = $(foreach path, $(SRC_PATHS) $(LOCAL_INCLUDE_PATHS), $(addprefix -I, $(path)))
148ifeq ($(CC_VER), 2)
149	INCLUDES = $(LOC_INCLUDES)
150	INCLUDES += -I-
151	INCLUDES += $(foreach path, $(SYSTEM_INCLUDE_PATHS), $(addprefix -I, $(path)))
152
153	STDCPPLIBS = stdc++.r4
154else
155	INCLUDES = -iquote./
156	INCLUDES += $(foreach path, $(SRC_PATHS) $(LOCAL_INCLUDE_PATHS), $(addprefix -iquote, $(path)))
157	INCLUDES += $(foreach path, $(SYSTEM_INCLUDE_PATHS), $(addprefix -isystem, $(path)))
158
159	STDCPPLIBS = stdc++ supc++
160endif
161
162
163# Add the -L prefix to all of the library paths.
164LINK_PATHS = $(foreach path, $(SRC_PATHS) $(LIBPATHS), \
165	$(addprefix -L, $(path)))
166
167# Handle the additional libraries specified. If the libraries have a .so or
168# a .a prefix, or if they are _APP_ or _KERNEL_, simply add them to the list.
169LINK_LIBS += $(filter %.so %.a _APP_ _KERNEL_, $(LIBS))
170# If the libraries do not have suffixes and are not _APP_ or _KERNEL_,
171# prepend -l to each name:(e.g. "be" becomes "-lbe").
172LINK_LIBS += $(foreach lib, $(filter-out %.so %.a _APP_ _KERNEL_, $(LIBS)), $(addprefix -l, $(lib)))
173
174# Add the linkpaths and linklibs to LDFLAGS.
175LDFLAGS += $(LINK_PATHS)  $(LINK_LIBS)
176
177# Add the defines to CFLAGS.
178CFLAGS += $(foreach define, $(DEFINES), $(addprefix -D, $(define)))
179
180# Add the additional compiler flags to CFLAGS.
181CFLAGS += $(COMPILER_FLAGS)
182
183# Add the additional linkflags to LDFLAGS
184LDFLAGS += $(LINKER_FLAGS)
185
186# Use the archiving tools to create an an archive if we're building a static
187# library, otherwise use the linker.
188ifeq ($(strip $(TYPE)), STATIC)
189	BUILD_LINE = ar -cru "$(TARGET)" $(OBJS)
190else
191	BUILD_LINE = $(LD) -o "$@" $(OBJS) $(LDFLAGS)
192endif
193
194# Pseudo-function for converting a list of resource definition files in RDEFS
195# variable to a corresponding list of object files in $(OBJ_DIR)/xxx.rsrc.
196# The "function" strips off the rdef file suffix (.rdef) and then strips
197# of the directory name, leaving just the root file name. It then appends the
198# the .rsrc suffix and prepends the $(OBJ_DIR)/ path.
199define RDEFS_LIST_TO_RSRCS
200	$(addprefix $(OBJ_DIR)/, $(addsuffix .rsrc, $(foreach file, $(RDEFS), \
201	$(basename $(notdir $(file))))))
202endef
203
204# Create the resource definitions instruction in case RDEFS is not empty.
205ifeq ($(RDEFS), )
206	RSRCS +=
207else
208	RSRCS += $(RDEFS_LIST_TO_RSRCS)
209endif
210
211# Create the resource instruction.
212ifeq ($(RSRCS), )
213	DO_RSRCS :=
214else
215	DO_RSRCS := $(XRES) -o $(TARGET) $(RSRCS)
216endif
217
218# Set the directory for internationalization sources (catkeys) if it isn't
219# already.
220CATKEYS_DIR	:= locales
221
222# Set the directory for internationalization resource data (catalogs) if it
223# isn't already.
224CATALOGS_DIR := $(OBJ_DIR)/$(APP_MIME_SIG)
225
226# Pseudo-function for converting a list of language codes in CATALOGS variable
227# to a corresponding list of catkeys files in $(CATALOGS_DIR)/xx.catalog
228# The "function" appends the .catalog suffix and prepends the
229# $(CATALOGS_DIR)/ path.
230define LOCALES_LIST_TO_CATALOGS
231	$(addprefix $(CATALOGS_DIR)/, $(addsuffix .catalog, $(foreach lang, $(LOCALES), $(lang))))
232endef
233
234CATALOGS = $(LOCALES_LIST_TO_CATALOGS)
235
236# Define the actual work to be done.
237default: $(TARGET)
238
239$(TARGET):	$(OBJ_DIR) $(OBJS) $(RSRCS)
240	$(BUILD_LINE)
241	$(DO_RSRCS)
242	$(MIMESET) -f "$@"
243
244# Create OBJ_DIR if it doesn't exist.
245$(OBJ_DIR)::
246	@[ -d $(OBJ_DIR) ] || mkdir $(OBJ_DIR) >/dev/null 2>&1
247
248# Create the localization sources directory if it doesn't exist.
249$(CATKEYS_DIR)::
250	@[ -d $(CATKEYS_DIR) ] || mkdir $(CATKEYS_DIR) >/dev/null 2>&1
251
252# Create the localization data directory if it doesn't exist.
253$(CATALOGS_DIR):: $(OBJ_DIR)
254	@[ -d $(CATALOGS_DIR) ] || mkdir $(CATALOGS_DIR) >/dev/null 2>&1
255
256# Rules to create the dependency files.
257$(OBJ_DIR)/%.d : %.c
258	mkdir -p $(OBJ_DIR); \
259	mkdepend $(LOC_INCLUDES) -p .c:$(OBJ_DIR)/%n.o -m -f "$@" $<
260$(OBJ_DIR)/%.d : %.cpp
261	mkdir -p $(OBJ_DIR); \
262	mkdepend $(LOC_INCLUDES) -p .cpp:$(OBJ_DIR)/%n.o -m -f "$@" $<
263$(OBJ_DIR)/%.d : %.cp
264	mkdir -p $(OBJ_DIR); \
265	mkdepend $(LOC_INCLUDES) -p .cp:$(OBJ_DIR)/%n.o -m -f "$@" $<
266$(OBJ_DIR)/%.d : %.cc
267	mkdir -p $(OBJ_DIR); \
268	mkdepend $(LOC_INCLUDES) -p .cc:$(OBJ_DIR)/%n.o -m -f "$@" $<
269$(OBJ_DIR)/%.d : %.cxx
270	mkdir -p $(OBJ_DIR); \
271	mkdepend $(LOC_INCLUDES) -p .cxx:$(OBJ_DIR)/%n.o -m -f "$@" $<
272$(OBJ_DIR)/%.d : %.C
273	mkdir -p $(OBJ_DIR); \
274	mkdepend $(LOC_INCLUDES) -p .C:$(OBJ_DIR)/%n.o -m -f "$@" $<
275$(OBJ_DIR)/%.d : %.CC
276	mkdir -p $(OBJ_DIR); \
277	mkdepend $(LOC_INCLUDES) -p .CC:$(OBJ_DIR)/%n.o -m -f "$@" $<
278$(OBJ_DIR)/%.d : %.CPP
279	mkdir -p $(OBJ_DIR); \
280	mkdepend $(LOC_INCLUDES) -p .CPP:$(OBJ_DIR)/%n.o -m -f "$@" $<
281$(OBJ_DIR)/%.d : %.CXX
282	mkdir -p $(OBJ_DIR); \
283	mkdepend $(LOC_INCLUDES) -p .CXX:$(OBJ_DIR)/%n.o -m -f "$@" $<
284
285-include $(DEPENDS)
286
287# Rules to make the object files.
288$(OBJ_DIR)/%.o : %.c
289	$(CC) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
290$(OBJ_DIR)/%.o : %.cpp
291	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
292$(OBJ_DIR)/%.o : %.cp
293	$(CC) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
294$(OBJ_DIR)/%.o : %.cc
295	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
296$(OBJ_DIR)/%.o : %.cxx
297	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
298$(OBJ_DIR)/%.o : %.C
299	$(CC) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
300$(OBJ_DIR)/%.o : %.CC
301	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
302$(OBJ_DIR)/%.o : %.CPP
303	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
304$(OBJ_DIR)/%.o : %.CXX
305	$(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
306
307# Rules to compile the resource definition files.
308$(OBJ_DIR)/%.rsrc : %.rdef
309	cat $< | $(CC) -E $(INCLUDES) $(CFLAGS) - | grep -av '^#' | $(RESCOMP) -I $(dir $<) -o "$@" -
310$(OBJ_DIR)/%.rsrc : %.RDEF
311	cat $< | $(CC) -E $(INCLUDES) $(CFLAGS) - | grep -av '^#' | $(RESCOMP) -I $(dir $<) -o "$@" -
312
313# Rule to compile localization data catalogs.
314$(CATALOGS_DIR)/%.catalog : $(CATKEYS_DIR)/%.catkeys
315	linkcatkeys -o "$@" -s $(APP_MIME_SIG) -l $(notdir $(basename $@)) $<
316
317# Rule to preprocess program sources into file ready for collecting catkeys.
318$(OBJ_DIR)/$(NAME).pre : $(SRCS)
319	-cat $(SRCS) | $(CC) -E -x c++ $(INCLUDES) $(CFLAGS) -DB_COLLECTING_CATKEYS - | grep -av '^#' > $(OBJ_DIR)/$(NAME).pre
320
321# Rules to collect localization catkeys.
322catkeys : $(CATKEYS_DIR)/en.catkeys
323
324$(CATKEYS_DIR)/en.catkeys : $(CATKEYS_DIR) $(OBJ_DIR)/$(NAME).pre
325	collectcatkeys -s $(APP_MIME_SIG) $(OBJ_DIR)/$(NAME).pre -o $(CATKEYS_DIR)/en.catkeys
326
327# Rule to create localization catalogs.
328catalogs : $(CATALOGS_DIR) $(CATALOGS)
329
330# Rules to handle lex/flex and yacc/bison files.
331$(OBJ_DIR)/%.o: %.l
332	flex $<
333	$(CC) -c $(INCLUDES) $(CFLAGS) lex.yy.c -o "$@"
334$(OBJ_DIR)/%.o: %.y
335	bison -d -y $<
336	$(CC) -c $(INCLUDES) $(CFLAGS) y.tab.c -o "$@"
337
338# The generic "clean" command. (Deletes everything in the object folder.)
339.PHONY: clean
340clean:
341	-rm -rf "$(OBJ_DIR)"
342
343# Remove just the application from the object folder.
344rmapp ::
345	-rm -f $(TARGET)
346
347# Make it easy to install drivers for testing.
348USER_BIN_PATH := $(shell finddir B_USER_NONPACKAGED_ADDONS_DIRECTORY)/kernel/drivers/bin
349USER_DEV_PATH := $(shell finddir B_USER_NONPACKAGED_ADDONS_DIRECTORY)/kernel/drivers/dev
350
351driverinstall :: default
352ifeq ($(strip $(TYPE)), DRIVER)
353	copyattr --data $(TARGET) $(USER_BIN_PATH)/$(NAME)
354	mkdir -p $(USER_DEV_PATH)/$(DRIVER_PATH)
355	ln -sf $(USER_BIN_PATH)/$(NAME) $(USER_DEV_PATH)/$(DRIVER_PATH)/$(NAME)
356endif
357
358install :: default
359ifeq ($(INSTALL_DIR), )
360	@echo "No install directory specified for \"$(NAME)\" (INSTALL_DIR is empty)" >&2
361else
362	mkdir -p "$(INSTALL_DIR)"
363	cp $(TARGET) "$(INSTALL_DIR)"/$(NAME)
364endif
365
366# Set the catalog installation directory if it isn't already.
367CATALOG_INSTALL_DIR := $(shell finddir B_USER_NONPACKAGED_DATA_DIRECTORY)/locale/catalogs
368
369# Rule to install localization resources catalogs.
370catalogsinstall :: catalogs
371	mkdir -p "$(CATALOG_INSTALL_DIR)/$(APP_MIME_SIG)"
372	-cp $(CATALOGS_DIR)/*.catalog "$(CATALOG_INSTALL_DIR)/$(APP_MIME_SIG)"
373
374# Alternate way of storing localization catalogs: bind them into the program
375# executable's resources.
376bindcatalogs :
377	for lc in $(LOCALES); do linkcatkeys -o $(TARGET) -s $(APP_MIME_SIG) -tr -l $$lc $(CATKEYS_DIR)/$$lc.catkeys; done
378