toolbox: Cleanup Makefile

* Do not split the makefile in two to support pre-lollipop trees. Go
  through and mark sections with PLATFORM_SDK_VERSION.
* setenforce: Correct the handling to prevent build warnings -- if
  busybox provides this tool, then still allow it to be built, but do
  not write a symlink.
* Correct indentation throughout
* Remove dead code (LOCAL_STATIC_LIBRARIES)

Change-Id: I1b13a0e0be78ea862f7d418d683407ff79d17e4f
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 93e96a8..9e7bbc4 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -1,171 +1,201 @@
-LOCAL_PATH:= system/core/toolbox/
-
-
-ifeq ($(PLATFORM_SDK_VERSION), 21)
-
-# Rule for lollipop
-common_cflags := \
-    -std=gnu99 \
-    -Werror -Wno-unused-parameter \
-    -I$(LOCAL_PATH)/upstream-netbsd/include/ \
-    -include bsd-compatibility.h
-
+LOCAL_PATH := system/core/toolbox
 include $(CLEAR_VARS)
 
 OUR_TOOLS := \
-	start \
-	stop \
-	getprop \
-	setprop
+    start \
+    stop \
+    getprop \
+    setprop
 
 # If busybox does not have SELinux support, provide these tools with toolbox.
 # Note that RECOVERY_BUSYBOX_TOOLS will be empty if TW_USE_TOOLBOX == true.
 ifeq ($(TWHAVE_SELINUX), true)
-	TOOLS_FOR_SELINUX := \
-		ls \
-		getenforce \
-		setenforce \
-		chcon \
-		restorecon \
-		runcon \
-		getsebool \
-		setsebool \
-		load_policy
-	OUR_TOOLS += $(filter-out $(RECOVERY_BUSYBOX_TOOLS), $(TOOLS_FOR_SELINUX))
-endif
+    TOOLS_FOR_SELINUX := \
+        ls \
+        getenforce \
+        chcon \
+        restorecon \
+        runcon \
+        getsebool \
+        setsebool \
+        load_policy
+    OUR_TOOLS += $(filter-out $(RECOVERY_BUSYBOX_TOOLS), $(TOOLS_FOR_SELINUX))
 
+    # toolbox setenforce is used during init, so it needs to be included here
+    # symlink is omitted at the very end if busybox already provides this
+    OUR_TOOLS += setenforce
+endif
 
 ifeq ($(TW_USE_TOOLBOX), true)
-BSD_TOOLS := \
-    cat \
-    chown \
-    cp \
-    dd \
-    du \
-    grep \
-    kill \
-    ln \
-    mv \
-    printenv \
-    rm \
-    rmdir \
-    sleep \
-    sync \
-
-OUR_TOOLS += \
-    chmod \
-    clear \
-    cmp \
-    date \
-    df \
-    dmesg \
-    getevent \
-    hd \
-    id \
-    ifconfig \
-    iftop \
-    insmod \
-    ioctl \
-    ionice \
-    log \
-    lsmod \
-    lsof \
-    md5 \
-    mkdir \
-    mknod \
-    mkswap \
-    mount \
-    nandread \
-    netstat \
-    newfs_msdos \
-    nohup \
-    notify \
-    ps \
-    readlink \
-    renice \
-    rmmod \
-    route \
-    schedtop \
-    sendevent \
-    smd \
-    swapoff \
-    swapon \
-    top \
-    touch \
-    umount \
-    uptime \
-    vmstat \
-    watchprops \
-    wipe
-    ifneq ($(TWHAVE_SELINUX), true)
-        TOOLS += ls
+    ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+        OUR_TOOLS += \
+            mknod \
+            nohup
+        BSD_TOOLS := \
+            cat \
+            chown \
+            cp \
+            dd \
+            du \
+            grep \
+            kill \
+            ln \
+            mv \
+            printenv \
+            rm \
+            rmdir \
+            sleep \
+            sync
+    else
+        OUR_TOOLS += \
+            cat \
+            chown \
+            dd \
+            du \
+            kill \
+            ln \
+            mv \
+            printenv \
+            rm \
+            rmdir \
+            setconsole \
+            sleep \
+            sync
     endif
-    LOCAL_SHARED_LIBRARIES += libcrypto
+
+    OUR_TOOLS += \
+        chmod \
+        clear \
+        cmp \
+        date \
+        df \
+        dmesg \
+        getevent \
+        hd \
+        id \
+        ifconfig \
+        iftop \
+        insmod \
+        ioctl \
+        ionice \
+        log \
+        lsmod \
+        lsof \
+        md5 \
+        mkdir \
+        mkswap \
+        mount \
+        nandread \
+        netstat \
+        newfs_msdos \
+        notify \
+        ps \
+        readlink \
+        renice \
+        rmmod \
+        route \
+        schedtop \
+        sendevent \
+        smd \
+        swapoff \
+        swapon \
+        top \
+        touch \
+        umount \
+        uptime \
+        vmstat \
+        watchprops \
+        wipe
+    ifneq ($(TWHAVE_SELINUX), true)
+        OUR_TOOLS += ls
+    endif
 endif
 
-ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-OUR_TOOLS += r
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
+        OUR_TOOLS += r
+    endif
 endif
 
-ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS) setenforce
-
 LOCAL_SRC_FILES := \
-    upstream-netbsd/lib/libc/gen/getbsize.c \
-    upstream-netbsd/lib/libc/gen/humanize_number.c \
-    upstream-netbsd/lib/libc/stdlib/strsuftoll.c \
-    upstream-netbsd/lib/libc/string/swab.c \
-    upstream-netbsd/lib/libutil/raise_default_signal.c \
-    dynarray.c \
-    pwcache.c \
-    $(patsubst %,%.c,$(OUR_TOOLS)) \
     toolbox.c \
+    $(patsubst %,%.c,$(OUR_TOOLS))
 
-ifeq (,$(filter $(LOCAL_SRC_FILES),setenforce.c))
-    LOCAL_SRC_FILES += setenforce.c
+ifneq ($(wildcard system/core/toolbox/dynarray.c),)
+    LOCAL_SRC_FILES += dynarray.c
 endif
 
-LOCAL_CFLAGS += $(common_cflags)
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    LOCAL_SRC_FILES += \
+        pwcache.c \
+        upstream-netbsd/lib/libc/gen/getbsize.c \
+        upstream-netbsd/lib/libc/gen/humanize_number.c \
+        upstream-netbsd/lib/libc/stdlib/strsuftoll.c \
+        upstream-netbsd/lib/libc/string/swab.c \
+        upstream-netbsd/lib/libutil/raise_default_signal.c
+endif
 
-LOCAL_C_INCLUDES += external/openssl/include
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    LOCAL_CFLAGS += \
+        -std=gnu99 \
+        -Werror -Wno-unused-parameter \
+        -I$(LOCAL_PATH)/upstream-netbsd/include \
+        -include bsd-compatibility.h
+endif
+
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    LOCAL_C_INCLUDES += external/openssl/include
+else
+    LOCAL_C_INCLUDES += bionic/libc/bionic
+endif
 
 LOCAL_SHARED_LIBRARIES += libcutils
 
-ifeq ($(TWHAVE_SELINUX), true)
-    LOCAL_SHARED_LIBRARIES += libselinux
-    LOCAL_STATIC_LIBRARIES += \
-        libtoolbox_cat \
-        libtoolbox_chown \
-        libtoolbox_cp \
-        libtoolbox_dd \
-        libtoolbox_grep \
-        libtoolbox_kill \
-        libtoolbox_ln \
-        libtoolbox_mv \
-        libtoolbox_printenv \
-        libtoolbox_rm \
-        libtoolbox_rmdir \
-        libtoolbox_sleep \
-        libtoolbox_sync
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    ifeq ($(TW_USE_TOOLBOX), true)
+        LOCAL_SHARED_LIBRARIES += libcrypto
+    endif
+else
+    LOCAL_SHARED_LIBRARIES += \
+        libc \
+        liblog
 endif
 
-# libusbhost is only used by lsusb, and that isn't usually included in toolbox.
-# The linker strips out all the unused library code in the normal case.
-LOCAL_STATIC_LIBRARIES := \
-    libusbhost \
+ifeq ($(TWHAVE_SELINUX), true)
+    LOCAL_SHARED_LIBRARIES += libselinux
+endif
 
-LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS))
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    # libusbhost is only used by lsusb, and that isn't usually included in toolbox.
+    # The linker strips out all the unused library code in the normal case.
+    LOCAL_STATIC_LIBRARIES := libusbhost
+    LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS))
+endif
 
 LOCAL_MODULE := toolbox_recovery
 LOCAL_MODULE_STEM := toolbox
 LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+LOCAL_MODULE_TAGS := optional
 
-# Including this will define $(intermediates).
-#
+# Including this will define $(intermediates) below
 include $(BUILD_EXECUTABLE)
 
 $(LOCAL_PATH)/toolbox.c: $(intermediates)/tools.h
 
+ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22))
+    ALL_TOOLS := $(BSD_TOOLS) $(OUR_TOOLS)
+else
+    ALL_TOOLS := $(OUR_TOOLS)
+endif
+
+ifeq ($(TWHAVE_SELINUX), true)
+    # toolbox setenforce is used during init in non-symlink form, so it was
+    # required to be included as part of the suite above. if busybox already
+    # provides setenforce, we can omit the toolbox symlink
+    TEMP_TOOLS := $(filter-out $(RECOVERY_BUSYBOX_TOOLS), $(ALL_TOOLS))
+    ALL_TOOLS := $(TEMP_TOOLS)
+endif
+
 TOOLS_H := $(intermediates)/tools.h
 $(TOOLS_H): PRIVATE_TOOLS := $(ALL_TOOLS)
 $(TOOLS_H): PRIVATE_CUSTOM_TOOL = echo "/* file generated automatically */" > $@ ; for t in $(PRIVATE_TOOLS) ; do echo "TOOL($$t)" >> $@ ; done
@@ -173,8 +203,7 @@
 $(TOOLS_H):
 	$(transform-generated-source)
 
-# Make #!/system/bin/toolbox launchers for each tool.
-#
+# Make /sbin/toolbox launchers for each tool
 SYMLINKS := $(addprefix $(TARGET_RECOVERY_ROOT_OUT)/sbin/,$(ALL_TOOLS))
 $(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE_STEM)
 $(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
@@ -188,173 +217,17 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_ADDITIONAL_DEPENDENCIES := $(SYMLINKS)
 include $(BUILD_PHONY_PACKAGE)
-SYMLINKS :=
-
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
-    $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
-
-else
-
-# Rule for older trees
-LOCAL_PATH:= system/core/toolbox/
-include $(CLEAR_VARS)
-
-TOOLS := \
-	start \
-	stop \
-	getprop \
-	setprop
-
-# If busybox does not have SELinux support, provide these tools with toolbox.
-# Note that RECOVERY_BUSYBOX_TOOLS will be empty if TW_USE_TOOLBOX == true.
-ifeq ($(TWHAVE_SELINUX), true)
-	TOOLS_FOR_SELINUX := \
-		ls \
-		getenforce \
-		setenforce \
-		chcon \
-		restorecon \
-		runcon \
-		getsebool \
-		setsebool \
-		load_policy
-	TOOLS += $(filter-out $(RECOVERY_BUSYBOX_TOOLS), $(TOOLS_FOR_SELINUX))
-endif
-
-ifeq ($(TW_USE_TOOLBOX), true)
-	TOOLS += \
-		mount \
-		cat \
-		ps \
-		kill \
-		ln \
-		insmod \
-		rmmod \
-		lsmod \
-		ifconfig \
-		setconsole \
-		rm \
-		mkdir \
-		rmdir \
-		getevent \
-		sendevent \
-		date \
-		wipe \
-		sync \
-		umount \
-		notify \
-		cmp \
-		dmesg \
-		route \
-		hd \
-		dd \
-		df \
-		watchprops \
-		log \
-		sleep \
-		renice \
-		printenv \
-		smd \
-		chmod \
-		chown \
-		newfs_msdos \
-		netstat \
-		ioctl \
-		mv \
-		schedtop \
-		top \
-		iftop \
-		id \
-		uptime \
-		vmstat \
-		nandread \
-		ionice \
-		touch \
-		lsof \
-		du \
-		md5 \
-		clear \
-		swapon \
-		swapoff \
-		mkswap \
-		readlink
-	ifneq ($(TWHAVE_SELINUX), true)
-		TOOLS += ls
-	endif
-endif
-
-LOCAL_SRC_FILES := \
-	toolbox.c \
-	$(patsubst %,%.c,$(TOOLS))
-
-ifneq ($(wildcard system/core/toolbox/dynarray.c),)
-    LOCAL_SRC_FILES += dynarray.c
-endif
-
-# reboot.c was removed in 4.4 kitkat
-#TOOLS += reboot
-
-#ifeq ($(BOARD_USES_BOOTMENU),true)
-#	LOCAL_SRC_FILES += ../../../external/bootmenu/libreboot/reboot.c
-#else
-#	LOCAL_SRC_FILES += reboot.c
-#endif
-
-LOCAL_C_INCLUDES := bionic/libc/bionic
-
-LOCAL_SHARED_LIBRARIES := \
-	libcutils \
-	liblog \
-	libc
-
-ifeq ($(TWHAVE_SELINUX), true)
-	LOCAL_SHARED_LIBRARIES += libselinux
-endif
-
-LOCAL_MODULE := toolbox_recovery
-LOCAL_MODULE_STEM := toolbox
-LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
-LOCAL_MODULE_TAGS := optional
-
-# Including this will define $(intermediates).
-#
-include $(BUILD_EXECUTABLE)
-
-$(LOCAL_PATH)/toolbox.c: $(intermediates)/tools.h
-
-TOOLS_H := $(intermediates)/tools.h
-$(TOOLS_H): PRIVATE_TOOLS := $(TOOLS)
-$(TOOLS_H): PRIVATE_CUSTOM_TOOL = echo "/* file generated automatically */" > $@ ; for t in $(PRIVATE_TOOLS) ; do echo "TOOL($$t)" >> $@ ; done
-$(TOOLS_H): $(LOCAL_PATH)/Android.mk
-$(TOOLS_H):
-	$(transform-generated-source)
-
-# Make #!/system/bin/toolbox launchers for each tool.
-#
-SYMLINKS := $(addprefix $(TARGET_RECOVERY_ROOT_OUT)/sbin/,$(TOOLS))
-$(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE_STEM)
-$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
-	@echo "Symlink: $@ -> $(TOOLBOX_BINARY)"
-	@mkdir -p $(dir $@)
-	@rm -rf $@
-	$(hide) ln -sf $(TOOLBOX_BINARY) $@
 
 ifneq (,$(filter $(PLATFORM_SDK_VERSION),16 17 18))
-ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
-
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
-	$(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
+    # Only needed if the build system lacks support for LOCAL_ADDITIONAL_DEPENDENCIES
+    ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
+    ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
+        $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
 endif
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := toolbox_symlinks
-LOCAL_MODULE_TAGS := optional
-LOCAL_ADDITIONAL_DEPENDENCIES := $(SYMLINKS)
-include $(BUILD_PHONY_PACKAGE)
 SYMLINKS :=
-
-endif
+ALL_TOOLS :=
+BSD_TOOLS :=
+OUR_TOOLS :=
+TEMP_TOOLS :=
+TOOLS_FOR_SELINUX :=