Search code examples
androidlinuxandroid-source

How to include util-linux in AOSP?


How do I include util-linux from here in AOSP? Where should I add this util-linux folder in AOSP so that I can build AOSP and these utils could be added in /system/bin/ or /system/xbin/? And is there any specific Makefile or Android.mk that I need to make it to compile with Android build?


Solution

  • I have added an application from util-linux package in AOSP.

    Here's how I did it. I added the util-linux (2.34) from https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.34/util-linux-2.34.tar.gz in external of AOSP folder.

    I just wanted to add chrt from util-linux. Therefore, I run the normal make command in util-linux to see what files are compiled for chrt. I took a note and created an Android.mk accordingly. I am sure there's a better way to do it hierarchically by making the library first and then make the chrt executable. However, this works too.

    I added the following Android.mk at external\util-linux\. The Android.mk looks like the following:

     LOCAL_PATH := $(call my-dir)
    
     include $(CLEAR_VARS)
     LOCAL_MODULE := sohamchrt
    
     LOCAL_SRC_FILES := schedutils/chrt.c lib/blkdev.c lib/canonicalize.c \
         lib/crc32.c lib/crc32c.c lib/idcache.c lib/fileutils.c \
         lib/ismounted.c lib/color-names.c lib/mangle.c lib/match.c lib/mbsalign.c \
         lib/mbsedit.c lib/md5.c lib/pager.c lib/parse-date.c lib/pwdutils.c lib/randutils.c \
         lib/setproctitle.c lib/strutils.c lib/timeutils.c lib/ttyutils.c lib/exec_shell.c \
         lib/strv.c lib/sha1.c lib/signames.c lib/linux_version.c lib/loopdev.c \
         lib/plymouth-ctrl.c lib/cpuset.c lib/path.c lib/procutils.c lib/sysfs.c
    
     LOCAL_CFLAGS = -DHAVE_NANOSLEEP -include config.h \
                    -isystem bionic/libc/upstream-openbsd/android/include
    
     LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
    
     include $(BUILD_EXECUTABLE)
    

    Now, when I run m -j8 iso_img, then Android tries to compile sohamchrt. I named the package sohamchrt because chrt is already provided by toybox which have limitations.

    The problem is util-linux is not fully compatible with bionic. So, I needed to make some changes to some .c and .h files such as fileutils.c, include/c.h, etc.

    After successful compilation, the binary sohamchrt appears in /system/bin when Android is running, and it works with no issues.

    This is also a guide on how you can add a C source file for a userspace executable binary in AOSP.