# Makefile for busybox
#
# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
#
# Licensed under the GPL v2, see the file LICENSE in this tarball.

ifndef $(LIBBB_DIR)
LIBBB_DIR:=$(top_builddir)/libbb
endif
srcdir=$(top_srcdir)/libbb

LIBBB-n:=
LIBBB-y:= \
	ask_confirmation.c change_identity.c chomp.c \
	compare_string_array.c concat_path_file.c copy_file.c copyfd.c \
	crc32.c create_icmp_socket.c create_icmp6_socket.c \
	device_open.c dump.c error_msg.c error_msg_and_die.c \
	find_pid_by_name.c find_root_device.c fgets_str.c full_read.c \
	full_write.c get_last_path_component.c get_line_from_file.c \
	herror_msg.c herror_msg_and_die.c \
	human_readable.c inet_common.c inode_hash.c isdirectory.c \
	kernel_version.c last_char_is.c login.c \
	make_directory.c md5.c mode_string.c mtab_file.c \
	obscure.c parse_mode.c parse_number.c perror_msg.c \
	perror_msg_and_die.c get_console.c \
	process_escape_sequence.c procps.c \
	recursive_action.c remove_file.c \
	restricted_shell.c run_parts.c run_shell.c safe_read.c safe_write.c \
	safe_strncpy.c setup_environment.c sha1.c simplify_path.c \
	trim.c u_signal_names.c vdprintf.c verror_msg.c \
	vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \
	xgethostbyname.c xgethostbyname2.c xreadlink.c xgetlarg.c \
	get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \
	getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \
	perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \
	warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c \
	bb_do_delay.c

# conditionally compiled objects:
LIBBB-$(CONFIG_FEATURE_MOUNT_LOOP)+= loop.c
LIBBB-$(CONFIG_LOSETUP)+= loop.c
LIBBB-$(CONFIG_FEATURE_MTAB_SUPPORT)+= mtab.c
LIBBB-$(CONFIG_PASSWD)+= pw_encrypt.c
LIBBB-$(CONFIG_SULOGIN)+= pw_encrypt.c
LIBBB-$(CONFIG_FEATURE_HTTPD_AUTH_MD5)+= pw_encrypt.c
LIBBB-$(CONFIG_VLOCK)+= correct_password.c
LIBBB-$(CONFIG_SU)+= correct_password.c
LIBBB-$(CONFIG_LOGIN)+= correct_password.c
LIBBB-$(CONFIG_DF)+= find_mount_point.c
LIBBB-$(CONFIG_EJECT)+= find_mount_point.c
LIBBB-$(CONFIG_UUENCODE)+= uuencode.c
LIBBB-$(CONFIG_WGET)+= uuencode.c

# We shouldn't build xregcomp.c if we don't need it - this ensures we don't
# require regex.h to be in the include dir even if we don't need it thereby
# allowing us to build busybox even if uclibc regex support is disabled.

regex-y:=

regex-$(CONFIG_AWK) += xregcomp.c
regex-$(CONFIG_SED) += xregcomp.c
regex-$(CONFIG_LESS) += xregcomp.c
regex-$(CONFIG_DEVFSD) += xregcomp.c

# Sort has the happy side efect of returning a unique list
LIBBB-y += $(sort $(regex-y))

LIBBB-y:=$(patsubst %,$(srcdir)/%,$(LIBBB-y))

get-file-subparts = $(addsuffix .o,$(shell sed -n -e "s/^\#ifdef L_//p" ${1}))

# 1:N objects
LIBBB_MSRC0:=$(srcdir)/messages.c
LIBBB_MOBJ0:=$(call get-file-subparts, ${LIBBB_MSRC0})
LIBBB_MOBJ0:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ0))
$(LIBBB_MOBJ0):$(LIBBB_MSRC0)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC1:=$(srcdir)/xfuncs.c
LIBBB_MOBJ1:=$(call get-file-subparts, ${LIBBB_MSRC1})
LIBBB_MOBJ1:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ1))
$(LIBBB_MOBJ1):$(LIBBB_MSRC1)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC2:=$(srcdir)/printf.c
LIBBB_MOBJ2:=$(call get-file-subparts, ${LIBBB_MSRC2})
LIBBB_MOBJ2:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ2))
$(LIBBB_MOBJ2):$(LIBBB_MSRC2)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC3:=$(srcdir)/xgetularg.c
LIBBB_MOBJ3:=$(call get-file-subparts, ${LIBBB_MSRC3})
LIBBB_MOBJ3:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ3))
$(LIBBB_MOBJ3):$(LIBBB_MSRC3)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC4:=$(srcdir)/safe_strtol.c
LIBBB_MOBJ4:=$(call get-file-subparts, ${LIBBB_MSRC4})
LIBBB_MOBJ4:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ4))
$(LIBBB_MOBJ4):$(LIBBB_MSRC4)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC5:=$(srcdir)/bb_pwd.c
LIBBB_MOBJ5:=$(call get-file-subparts, ${LIBBB_MSRC5})
LIBBB_MOBJ5:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ5))
$(LIBBB_MOBJ5):$(LIBBB_MSRC5)
	$(compile.c) -DL_$(notdir $*)

LIBBB_MSRC6:=$(srcdir)/llist.c
LIBBB_MOBJ6:=$(call get-file-subparts, ${LIBBB_MSRC6})
LIBBB_MOBJ6:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ6))
$(LIBBB_MOBJ6):$(LIBBB_MSRC6)
	$(compile.c) -DL_$(notdir $*)

# We need the names of the object files built from MSRC for the L_ defines
LIBBB_ALL_MOBJ:=$(LIBBB_MOBJ0) $(LIBBB_MOBJ1) $(LIBBB_MOBJ2) $(LIBBB_MOBJ3) \
	$(LIBBB_MOBJ4) $(LIBBB_MOBJ5) $(LIBBB_MOBJ6)

LIBBB_ALL_MSRC:=$(LIBBB_MSRC0) $(LIBBB_MSRC1) $(LIBBB_MSRC2) $(LIBBB_MSRC3) \
	$(LIBBB_MSRC4) $(LIBBB_MSRC5) $(LIBBB_MSRC6)

LIBBB-y:=$(sort $(LIBBB-y) $(LIBBB_ALL_MSRC))

LIBBB_AR:=$(LIBBB_DIR)/libbb.a
libraries-y+=$(LIBBB_AR)

needcrypt-y:=
ifneq ($(filter $(srcdir)/pw_encrypt.c,$(LIBBB-y)),)
needcrypt-y:=y
else
ifneq ($(filter $(srcdir)/correct_password.c,$(LIBBB-y)),)
needcrypt-y:=y
endif
endif

ifeq ($(needcrypt-y),y)
  LIBRARIES := -lcrypt $(filter-out -lcrypt,$(LIBRARIES))
endif

# all 1:1 objects
LIBBB_OBJS:=$(patsubst $(srcdir)/%.c,$(LIBBB_DIR)/%.o, $(LIBBB-y))
$(LIBBB_DIR)/%.o: $(srcdir)/%.c
	$(compile.c)

LIBBB_SRC-a:=$(wildcard $(srcdir)/*.c)
LIBRARY_SRC-y+=$(LIBBB-y)
LIBRARY_SRC-a+=$(LIBBB_SRC-a)

# all defines needed for 1:N objects
LIBBB_DEFINE-y:=$(patsubst %,-DL_%,$(subst .o,,$(notdir $(LIBBB_ALL_MOBJ))))
LIBRARY_DEFINE-y+=$(LIBBB_DEFINE-y)
LIBRARY_DEFINE-a+=$(LIBBB_DEFINE-y)

$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_ALL_MOBJ)
	$(do_ar)