add_library(libext2_uuid STATIC
	e2fsprogs/lib/uuid/clear.c
	e2fsprogs/lib/uuid/compare.c
	e2fsprogs/lib/uuid/copy.c
	e2fsprogs/lib/uuid/gen_uuid.c
	e2fsprogs/lib/uuid/isnull.c
	e2fsprogs/lib/uuid/pack.c
	e2fsprogs/lib/uuid/parse.c
	e2fsprogs/lib/uuid/unpack.c
	e2fsprogs/lib/uuid/unparse.c
	e2fsprogs/lib/uuid/uuid_time.c)

target_include_directories(libext2_uuid PUBLIC
	e2fsprogs/lib)

add_library(libext2fs STATIC
	e2fsprogs/lib/blkid/cache.c
	e2fsprogs/lib/blkid/dev.c
	e2fsprogs/lib/blkid/devname.c
	e2fsprogs/lib/blkid/devno.c
	e2fsprogs/lib/blkid/getsize.c
	e2fsprogs/lib/blkid/llseek.c
	e2fsprogs/lib/blkid/probe.c
	e2fsprogs/lib/blkid/read.c
	e2fsprogs/lib/blkid/resolve.c
	e2fsprogs/lib/blkid/save.c
	e2fsprogs/lib/blkid/tag.c
	e2fsprogs/lib/e2p/encoding.c
	e2fsprogs/lib/e2p/feature.c
	e2fsprogs/lib/e2p/hashstr.c
	e2fsprogs/lib/e2p/mntopts.c
	e2fsprogs/lib/e2p/ostype.c
	e2fsprogs/lib/e2p/parse_num.c
	e2fsprogs/lib/e2p/uuid.c
	e2fsprogs/lib/et/com_err.c
	e2fsprogs/lib/et/error_message.c
	e2fsprogs/lib/et/et_name.c
	e2fsprogs/lib/ext2fs/alloc.c
	e2fsprogs/lib/ext2fs/alloc_sb.c
	e2fsprogs/lib/ext2fs/alloc_stats.c
	e2fsprogs/lib/ext2fs/alloc_tables.c
	e2fsprogs/lib/ext2fs/atexit.c
	e2fsprogs/lib/ext2fs/badblocks.c
	e2fsprogs/lib/ext2fs/bb_inode.c
	e2fsprogs/lib/ext2fs/bitmaps.c
	e2fsprogs/lib/ext2fs/bitops.c
	e2fsprogs/lib/ext2fs/blkmap64_ba.c
	e2fsprogs/lib/ext2fs/blkmap64_rb.c
	e2fsprogs/lib/ext2fs/blknum.c
	e2fsprogs/lib/ext2fs/block.c
	e2fsprogs/lib/ext2fs/bmap.c
	e2fsprogs/lib/ext2fs/closefs.c
	e2fsprogs/lib/ext2fs/crc16.c
	e2fsprogs/lib/ext2fs/crc32c.c
	e2fsprogs/lib/ext2fs/csum.c
	e2fsprogs/lib/ext2fs/dirblock.c
	e2fsprogs/lib/ext2fs/dir_iterate.c
	e2fsprogs/lib/ext2fs/expanddir.c
	e2fsprogs/lib/ext2fs/ext2_err.c
	e2fsprogs/lib/ext2fs/ext_attr.c
	e2fsprogs/lib/ext2fs/extent.c
	e2fsprogs/lib/ext2fs/fallocate.c
	e2fsprogs/lib/ext2fs/fileio.c
	e2fsprogs/lib/ext2fs/freefs.c
	e2fsprogs/lib/ext2fs/gen_bitmap64.c
	e2fsprogs/lib/ext2fs/gen_bitmap.c
	e2fsprogs/lib/ext2fs/get_num_dirs.c
	e2fsprogs/lib/ext2fs/getenv.c
	e2fsprogs/lib/ext2fs/getsectsize.c
	e2fsprogs/lib/ext2fs/getsize.c
	e2fsprogs/lib/ext2fs/hashmap.c
	e2fsprogs/lib/ext2fs/i_block.c
	e2fsprogs/lib/ext2fs/ind_block.c
	e2fsprogs/lib/ext2fs/initialize.c
	e2fsprogs/lib/ext2fs/inline.c
	e2fsprogs/lib/ext2fs/inline_data.c
	e2fsprogs/lib/ext2fs/inode.c
	e2fsprogs/lib/ext2fs/io_manager.c
	e2fsprogs/lib/ext2fs/ismounted.c
	e2fsprogs/lib/ext2fs/link.c
	e2fsprogs/lib/ext2fs/dirhash.c
	e2fsprogs/lib/ext2fs/llseek.c
	e2fsprogs/lib/ext2fs/lookup.c
	e2fsprogs/lib/ext2fs/mkdir.c
	e2fsprogs/lib/ext2fs/mkjournal.c
	e2fsprogs/lib/ext2fs/mmp.c
	e2fsprogs/lib/ext2fs/namei.c
	e2fsprogs/lib/ext2fs/newdir.c
	e2fsprogs/lib/ext2fs/nls_utf8.c
	e2fsprogs/lib/ext2fs/openfs.c
	e2fsprogs/lib/ext2fs/orphan.c
	e2fsprogs/lib/ext2fs/progress.c
	e2fsprogs/lib/ext2fs/punch.c
	e2fsprogs/lib/ext2fs/rbtree.c
	e2fsprogs/lib/ext2fs/read_bb.c
	e2fsprogs/lib/ext2fs/read_bb_file.c
	e2fsprogs/lib/ext2fs/res_gdt.c
	e2fsprogs/lib/ext2fs/rw_bitmaps.c
	e2fsprogs/lib/ext2fs/sha512.c
	e2fsprogs/lib/ext2fs/sparse_io.c
	e2fsprogs/lib/ext2fs/symlink.c
	e2fsprogs/lib/ext2fs/undo_io.c
	e2fsprogs/lib/ext2fs/unix_io.c
	e2fsprogs/lib/ext2fs/valid_blk.c
	e2fsprogs/lib/support/devname.c
	e2fsprogs/lib/support/dict.c
	e2fsprogs/lib/support/mkquota.c
	e2fsprogs/lib/support/parse_qtype.c
	e2fsprogs/lib/support/plausible.c
	e2fsprogs/lib/support/prof_err.c
	e2fsprogs/lib/support/profile.c
	e2fsprogs/lib/support/quotaio.c
	e2fsprogs/lib/support/quotaio_tree.c
	e2fsprogs/lib/support/quotaio_v2.c
	e2fsprogs/misc/create_inode_libarchive.c
	e2fsprogs/misc/create_inode.c)

target_include_directories(libext2fs PRIVATE
	e2fsprogs/lib e2fsprogs/lib/ext2fs core/libsparse/include)

target_link_libraries(libext2fs PUBLIC
	libext2_uuid)

add_executable("${ANDROID_MKE2FS_NAME}"
	e2fsprogs/misc/default_profile.c
	e2fsprogs/misc/mke2fs.c
	e2fsprogs/misc/mk_hugefiles.c
	e2fsprogs/misc/util.c)

target_link_libraries("${ANDROID_MKE2FS_NAME}"
	libext2fs libsparse libbase libzip liblog libutil Threads::Threads z)
target_include_directories("${ANDROID_MKE2FS_NAME}" PRIVATE
	e2fsprogs/lib)

# fails to build due to https://android-review.googlesource.com/c/platform/system/core/+/2760169
if(NOT APPLE)
add_executable(e2fsdroid
	e2fsprogs/contrib/android/e2fsdroid.c
	e2fsprogs/contrib/android/basefs_allocator.c
	e2fsprogs/contrib/android/block_range.c
	e2fsprogs/contrib/android/base_fs.c
	e2fsprogs/contrib/android/fsmap.c
	e2fsprogs/contrib/android/block_list.c
	e2fsprogs/contrib/android/perms.c)

include(CheckIncludeFile)
CHECK_INCLUDE_FILE("sys/types.h" HAVE_SYS_TYPES_H)

if(HAVE_SYS_TYPES_H)
	target_compile_definitions(e2fsdroid PUBLIC HAVE_SYS_TYPES_H)
endif(HAVE_SYS_TYPES_H)

target_link_libraries(e2fsdroid
	libext2fs libsparse libzip libcutils liblog libutil
	libbase libselinux libsepol z PkgConfig::libpcre2-8 pthread)
target_include_directories(e2fsdroid PRIVATE
	e2fsprogs/lib e2fsprogs/lib/ext2fs selinux/libselinux/include
	core/libcutils/include e2fsprogs/misc)
endif()

add_executable(ext2simg
	e2fsprogs/contrib/android/ext2simg.c)

target_link_libraries(ext2simg
	libext2fs libsparse libbase libzip liblog libutil z pthread)
target_include_directories(ext2simg PRIVATE
	e2fsprogs/lib core/libsparse/include)
