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