1 ## Haiku Generic Makefile Engine v2.6 ##
3 ## Does all the hard work for the Generic Makefile, which only has to specify
4 ## the project parameters.
6 ## Supports Generic Makefile v2.6.
7 ## Probably works with Generic Makefile v2.0+, but support is not guaranteed.
9 # Determine the CPU type
10 MACHINE=$(shell uname -m)
11 ifeq ($(MACHINE), BePC)
26 ifeq ($(APP_MIME_SIG), )
27 APP_MIME = x.vnd-Haiku-$(NAME)
30 ifeq ($(DRIVER_PATH), )
34 # Set the core tools if they're not already specified.
42 ifeq ($(strip $(TYPE)), DRIVER)
43 CFLAGS += -D_KERNEL_MODE=1 -fno-pic
48 # Set up optimization flags.
49 ifeq ($(strip $(OPTIMIZE)), FULL)
52 ifeq ($(strip $(OPTIMIZE)), SOME)
55 ifeq ($(strip $(OPTIMIZE)), NONE)
58 # OPTIMIZE isn't set, so set it to FULL
64 # Set up debug information flags.
65 ifeq ($(strip $(DEBUGGER)), TRUE)
69 CFLAGS += $(OPTIMIZER) $(DEBUG)
71 # Set up the warning flags.
72 ifeq ($(strip $(WARNINGS)), ALL)
73 CFLAGS += -Wall -Wno-multichar -Wno-ctor-dtor-privacy
75 ifeq ($(strip $(WARNINGS)), NONE)
80 # Set up the linker & linker flags.
81 ifeq ($(origin LD), default)
86 # Binary-type-specific linker flags.
87 ifeq ($(strip $(TYPE)), APP)
88 LDFLAGS += -Xlinker -soname=_APP_
90 ifeq ($(strip $(TYPE)), SHARED)
91 LDFLAGS += -shared -Xlinker -soname=$(NAME)
93 ifeq ($(strip $(TYPE)), DRIVER)
94 LDFLAGS += -nostdlib /boot/system/develop/lib/_KERNEL_ \
95 /boot/system/develop/lib/haiku_version_glue.o
100 # Get the compiler version.
101 CC_VER = $(word 1, $(subst -, , $(subst ., , $(shell $(CC) -dumpversion))))
103 # Set the object directory if it isn't already.
104 OBJ_DIR := objects.$(CPU)-$(CC)$(CC_VER)-$(if $(DEBUGGER),debug,release)
106 # Output the binary alongside the objects unless another directory is specified.
107 ifeq ($(TARGET_DIR), )
108 TARGET_DIR := $(OBJ_DIR)
111 # NOTE: make doesn't find the target if its name is enclosed in quotes.
112 ifeq ($(strip $(TYPE)), STATIC)
113 TARGET := $(TARGET_DIR)/$(NAME).a
115 TARGET := $(TARGET_DIR)/$(NAME)
118 # Psuedo-function for converting a list of source files in SRCS variable to a
119 # corresponding list of object files in $(OBJ_DIR)/xxx.o. The "function" strips
120 # off the src file suffix (.ccp or .c or whatever) and then strips off the
121 # off the directory name, leaving just the root file name. It then appends the
122 # .o suffix and prepends the $(OBJ_DIR)/ path
123 define SRCS_LIST_TO_OBJS
124 $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), \
125 $(basename $(notdir $(file))))))
128 define SRCS_LIST_TO_DEPENDS
129 $(addprefix $(OBJ_DIR)/, $(addsuffix .d, $(foreach file, $(SRCS), \
130 $(basename $(notdir $(file))))))
133 OBJS = $(SRCS_LIST_TO_OBJS)
134 DEPENDS = $(SRCS_LIST_TO_DEPENDS)
136 # Create a unique list of paths to our sourcefiles and resources.
137 SRC_PATHS += $(sort $(foreach file, $(SRCS) $(RSRCS) $(RDEFS), $(dir $(file))))
139 # Add source paths to VPATH if not already present.
141 VPATH += $(addprefix :, $(subst ,:, $(filter-out $($(subst, :, ,$(VPATH))), $(SRC_PATHS))))
143 # Set up the local & system include paths and C++ stdlibs.
144 ifneq (,$(filter $(CPU),x86 x86_64))
145 LOC_INCLUDES = $(foreach path, $(SRC_PATHS) $(LOCAL_INCLUDE_PATHS), $(addprefix -I, $(path)))
147 INCLUDES = $(LOC_INCLUDES)
149 INCLUDES += $(foreach path, $(SYSTEM_INCLUDE_PATHS), $(addprefix -I, $(path)))
151 STDCPPLIBS = stdc++.r4
154 INCLUDES += $(foreach path, $(SRC_PATHS) $(LOCAL_INCLUDE_PATHS), $(addprefix -iquote, $(path)))
155 INCLUDES += $(foreach path, $(SYSTEM_INCLUDE_PATHS), $(addprefix -isystem, $(path)))
157 STDCPPLIBS = stdc++ supc++
161 LOC_INCLUDES = $(foreach path, $(SRC_PATHS) $(LOCAL_INCLUDE_PATHS), $(addprefix -I, $(path)))
163 SYS_INCLUDES += $(foreach path, $(SYSTEM_INCLUDE_PATHS), $(addprefix -i , $(path)))
165 INCLUDES = $(LOC_INCLUDES) $(SYS_INCLUDES)
169 # Add the -L prefix to all of the library paths.
170 LINK_PATHS = $(foreach path, $(SRC_PATHS) $(LIBPATHS), \
171 $(addprefix -L, $(path)))
173 # Handle the additional libraries specified. If the libraries have a .so or
174 # a .a prefix, or if they are _APP_ or _KERNEL_, simply add them to the list.
175 LINK_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:(e.g. "be" becomes "-lbe").
178 LINK_LIBS += $(foreach lib, $(filter-out %.so %.a _APP_ _KERNEL_, $(LIBS)), $(addprefix -l, $(lib)))
180 # Add the linkpaths and linklibs to LDFLAGS.
181 LDFLAGS += $(LINK_PATHS) $(LINK_LIBS)
183 # Add the defines to CFLAGS.
184 CFLAGS += $(foreach define, $(DEFINES), $(addprefix -D, $(define)))
186 # Add the additional compiler flags to CFLAGS.
187 CFLAGS += $(COMPILER_FLAGS)
189 # Add the additional linkflags to LDFLAGS
190 LDFLAGS += $(LINKER_FLAGS)
192 # Use the archiving tools to create an an archive if we're building a static
193 # library, otherwise use the linker.
194 ifeq ($(strip $(TYPE)), STATIC)
195 BUILD_LINE = ar -cru "$(TARGET)" $(OBJS)
197 BUILD_LINE = $(LD) -o "$@" $(OBJS) $(LDFLAGS)
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. It then appends the
204 # the .rsrc suffix and prepends the $(OBJ_DIR)/ path.
205 define RDEFS_LIST_TO_RSRCS
206 $(addprefix $(OBJ_DIR)/, $(addsuffix .rsrc, $(foreach file, $(RDEFS), \
207 $(basename $(notdir $(file))))))
210 # Create the resource definitions instruction in case RDEFS is not empty.
214 RSRCS += $(RDEFS_LIST_TO_RSRCS)
217 # Create the resource instruction.
221 DO_RSRCS := $(XRES) -o $(TARGET) $(RSRCS)
224 # Set the directory for internationalization sources (catkeys) if it isn't
226 CATKEYS_DIR := locales
228 # Set the directory for internationalization resource data (catalogs) if it
230 CATALOGS_DIR := $(OBJ_DIR)/$(APP_MIME_SIG)
232 # Pseudo-function for converting a list of language codes in CATALOGS variable
233 # to a corresponding list of catkeys files in $(CATALOGS_DIR)/xx.catalog
234 # The "function" appends the .catalog suffix and prepends the
235 # $(CATALOGS_DIR)/ path.
236 define LOCALES_LIST_TO_CATALOGS
237 $(addprefix $(CATALOGS_DIR)/, $(addsuffix .catalog, $(foreach lang, $(LOCALES), $(lang))))
240 CATALOGS = $(LOCALES_LIST_TO_CATALOGS)
242 # Define the actual work to be done.
245 $(TARGET): $(OBJ_DIR) $(OBJS) $(RSRCS)
250 # Create OBJ_DIR if it doesn't exist.
252 @[ -d $(OBJ_DIR) ] || mkdir $(OBJ_DIR) >/dev/null 2>&1
254 # Create the localization sources directory if it doesn't exist.
256 @[ -d $(CATKEYS_DIR) ] || mkdir $(CATKEYS_DIR) >/dev/null 2>&1
258 # Create the localization data directory if it doesn't exist.
259 $(CATALOGS_DIR):: $(OBJ_DIR)
260 @[ -d $(CATALOGS_DIR) ] || mkdir $(CATALOGS_DIR) >/dev/null 2>&1
262 # Rules to create the dependency files.
264 mkdir -p $(OBJ_DIR); \
265 mkdepend $(LOC_INCLUDES) -p .c:$(OBJ_DIR)/%n.o -m -f "$@" $<
266 $(OBJ_DIR)/%.d : %.cpp
267 mkdir -p $(OBJ_DIR); \
268 mkdepend $(LOC_INCLUDES) -p .cpp:$(OBJ_DIR)/%n.o -m -f "$@" $<
269 $(OBJ_DIR)/%.d : %.cp
270 mkdir -p $(OBJ_DIR); \
271 mkdepend $(LOC_INCLUDES) -p .cp:$(OBJ_DIR)/%n.o -m -f "$@" $<
272 $(OBJ_DIR)/%.d : %.cc
273 mkdir -p $(OBJ_DIR); \
274 mkdepend $(LOC_INCLUDES) -p .cc:$(OBJ_DIR)/%n.o -m -f "$@" $<
276 mkdir -p $(OBJ_DIR); \
277 mkdepend $(LOC_INCLUDES) -p .C:$(OBJ_DIR)/%n.o -m -f "$@" $<
278 $(OBJ_DIR)/%.d : %.CC
279 mkdir -p $(OBJ_DIR); \
280 mkdepend $(LOC_INCLUDES) -p .CC:$(OBJ_DIR)/%n.o -m -f "$@" $<
281 $(OBJ_DIR)/%.d : %.CPP
282 mkdir -p $(OBJ_DIR); \
283 mkdepend $(LOC_INCLUDES) -p .CPP:$(OBJ_DIR)/%n.o -m -f "$@" $<
287 # Rules to make the object files.
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 "$@"
297 $(CC) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
298 $(OBJ_DIR)/%.o : %.CC
299 $(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
300 $(OBJ_DIR)/%.o : %.CPP
301 $(C++) -c $< $(INCLUDES) $(CFLAGS) -o "$@"
303 # Rules to compile the resource definition files.
304 $(OBJ_DIR)/%.rsrc : %.rdef
305 cat $< | $(CC) -E $(INCLUDES) $(CFLAGS) - | grep -v '^#' | $(RESCOMP) -I $(dir $<) -o "$@" -
306 $(OBJ_DIR)/%.rsrc : %.RDEF
307 cat $< | $(CC) -E $(INCLUDES) $(CFLAGS) - | grep -v '^#' | $(RESCOMP) -I $(dir $<) -o "$@" -
309 # Rule to compile localization data catalogs.
310 $(CATALOGS_DIR)/%.catalog : $(CATKEYS_DIR)/%.catkeys
311 linkcatkeys -o "$@" -s $(APP_MIME_SIG) -l $(notdir $(basename $@)) $<
313 # Rule to preprocess program sources into file ready for collecting catkeys.
314 $(OBJ_DIR)/$(NAME).pre : $(SRCS)
315 -cat $(SRCS) | $(CC) -E -x c++ $(INCLUDES) $(CFLAGS) -DB_COLLECTING_CATKEYS - > $(OBJ_DIR)/$(NAME).pre
317 # Rules to collect localization catkeys.
318 catkeys : $(CATKEYS_DIR)/en.catkeys
320 $(CATKEYS_DIR)/en.catkeys : $(CATKEYS_DIR) $(OBJ_DIR)/$(NAME).pre
321 collectcatkeys -s $(APP_MIME_SIG) $(OBJ_DIR)/$(NAME).pre -o $(CATKEYS_DIR)/en.catkeys
323 # Rule to create localization catalogs.
324 catalogs : $(CATALOGS_DIR) $(CATALOGS)
326 # Rules to handle lex/flex and yacc/bison files.
329 $(CC) -c $(INCLUDES) $(CFLAGS) lex.yy.c -o "$@"
332 $(CC) -c $(INCLUDES) $(CFLAGS) y.tab.c -o "$@"
334 # The generic "clean" command. (Deletes everything in the object folder.)
339 # Remove just the application from the object folder.
343 # Make it easy to install drivers for testing.
344 USER_BIN_PATH := $(shell finddir B_USER_NONPACKAGED_ADDONS_DIRECTORY)/kernel/drivers/bin
345 USER_DEV_PATH := $(shell finddir B_USER_NONPACKAGED_ADDONS_DIRECTORY)/kernel/drivers/dev
347 driverinstall :: default
348 ifeq ($(strip $(TYPE)), DRIVER)
349 copyattr --data $(TARGET) $(USER_BIN_PATH)/$(NAME)
350 mkdir -p $(USER_DEV_PATH)/$(DRIVER_PATH)
351 ln -sf $(USER_BIN_PATH)/$(NAME) $(USER_DEV_PATH)/$(DRIVER_PATH)/$(NAME)
355 ifeq ($(INSTALL_DIR), )
356 @echo "No install directory specified for \"$(NAME)\" (INSTALL_DIR is empty)" >&2
358 mkdir -p "$(INSTALL_DIR)"
359 cp $(TARGET) $(INSTALL_DIR)/$(NAME)
362 # Set the catalog installation directory if it isn't already.
363 CATALOG_INSTALL_DIR := $(shell finddir B_USER_NONPACKAGED_DATA_DIRECTORY)/locale/catalogs
365 # Rule to install localization resources catalogs.
366 catalogsinstall :: catalogs
367 mkdir -p "$(CATALOG_INSTALL_DIR)/$(APP_MIME_SIG)"
368 -cp $(CATALOGS_DIR)/*.catalog "$(CATALOG_INSTALL_DIR)/$(APP_MIME_SIG)"
370 # Alternate way of storing localization catalogs: bind them into the program
371 # executable's resources.
373 for lc in $(LOCALES); do linkcatkeys -o $(TARGET) -s $(APP_MIME_SIG) -tr -l $$lc $(CATKEYS_DIR)/$$lc.catkeys; done