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