add_library(libzip STATIC
	libziparchive/zip_archive.cc
	libziparchive/zip_archive_stream_entry.cc
	libziparchive/zip_cd_entry_map.cc
	libziparchive/zip_error.cpp
	libziparchive/zip_writer.cc)

target_compile_definitions(libzip PRIVATE -DZLIB_CONST)
target_include_directories(libzip PUBLIC
	libziparchive/include
	libziparchive/incfs_support/include
	core/include
	logging/liblog/include
	libbase/include)

add_library(libutil STATIC
	core/libutils/FileMap.cpp
	core/libutils/JenkinsHash.cpp
	core/libutils/LightRefBase.cpp
	core/libutils/NativeHandle.cpp
	core/libutils/Printer.cpp
	core/libutils/StopWatch.cpp
	core/libutils/SystemClock.cpp
	core/libutils/Threads.cpp
	core/libutils/Timers.cpp
	core/libutils/Tokenizer.cpp
	core/libutils/misc.cpp)

target_include_directories(libutil PUBLIC
	core/include
	logging/liblog/include
	core/libutils/include
	libbase/include)

add_library(libext4 STATIC
	extras/ext4_utils/ext4_utils.cpp
	extras/ext4_utils/wipe.cpp
	extras/ext4_utils/ext4_sb.cpp)

target_include_directories(libext4 PUBLIC
	core/libsparse/include core/include selinux/libselinux/include
	extras/ext4_utils/include libbase/include)

# Only add common sources from libselinux_defaults and libselinux
# See the file list in selinux/libselinux/Android.bp
add_library(libselinux STATIC
	selinux/libselinux/src/booleans.c
	selinux/libselinux/src/callbacks.c
	selinux/libselinux/src/freecon.c
	selinux/libselinux/src/hashtab.c
	selinux/libselinux/src/label_backends_android.c
	selinux/libselinux/src/label.c
	selinux/libselinux/src/label_support.c
	selinux/libselinux/src/matchpathcon.c
	selinux/libselinux/src/setrans_client.c
	selinux/libselinux/src/sha1.c
	selinux/libselinux/src/label_file.c
	selinux/libselinux/src/regex.c)

if(HAVE_STRLCPY)
	target_compile_definitions(libselinux PRIVATE HAVE_STRLCPY)
endif()

target_compile_definitions(libselinux PRIVATE
	-DAUDITD_LOG_TAG=1003 -D_GNU_SOURCE -DBUILD_HOST -DUSE_PCRE2
	-DNO_PERSISTENTLY_STORED_PATTERNS -DDISABLE_SETRANS
	-DDISABLE_BOOL -DNO_MEDIA_BACKEND -DNO_X_BACKEND -DNO_DB_BACKEND
	-DPCRE2_CODE_UNIT_WIDTH=8)
target_include_directories(libselinux PUBLIC
	selinux/libselinux/include selinux/libsepol/include)

add_library(libsepol
	selinux/libsepol/src/assertion.c
	selinux/libsepol/src/avrule_block.c
	selinux/libsepol/src/avtab.c
	selinux/libsepol/src/conditional.c
	selinux/libsepol/src/constraint.c
	selinux/libsepol/src/context.c
	selinux/libsepol/src/context_record.c
	selinux/libsepol/src/debug.c
	selinux/libsepol/src/ebitmap.c
	selinux/libsepol/src/expand.c
	selinux/libsepol/src/hashtab.c
	selinux/libsepol/src/hierarchy.c
	selinux/libsepol/src/kernel_to_common.c
	selinux/libsepol/src/mls.c
	selinux/libsepol/src/policydb.c
	selinux/libsepol/src/policydb_convert.c
	selinux/libsepol/src/policydb_public.c
	selinux/libsepol/src/services.c
	selinux/libsepol/src/sidtab.c
	selinux/libsepol/src/symtab.c
	selinux/libsepol/src/util.c
	selinux/libsepol/src/write.c
	selinux/libsepol/src/optimize.c)

if(HAVE_REALLOCARRAY)
	target_compile_definitions(libsepol PRIVATE HAVE_REALLOCARRAY)
endif()

target_include_directories(libsepol PUBLIC
	selinux/libsepol/include)

set(fastboot_SOURCES
	core/fastboot/bootimg_utils.cpp
	core/fastboot/fastboot.cpp
	core/fastboot/fastboot_driver.cpp
	core/fastboot/filesystem.cpp
	core/fastboot/fs.cpp
	core/fastboot/h2h_workaround.cpp
	core/fastboot/main.cpp
	core/fastboot/socket.cpp
	core/fastboot/storage.cpp
	core/fastboot/super_flash_helper.cpp
	core/fastboot/task.cpp
	core/fastboot/tcp.cpp
	core/fastboot/udp.cpp
	core/fastboot/vendor_boot_img_utils.cpp
	core/fastboot/util.cpp)

if(APPLE)
	list(APPEND fastboot_SOURCES
		core/fastboot/usb_osx.cpp)
else()
	list(APPEND fastboot_SOURCES
		core/fastboot/usb_linux.cpp)
endif()

add_executable(fastboot ${fastboot_SOURCES})
target_include_directories(fastboot PRIVATE
	libbase/include core/include core/adb core/libsparse/include
	extras/ext4_utils/include extras/f2fs_utils
	core/libziparchive/include mkbootimg/include/bootimg
	fs_mgr/liblp/include fs_mgr/libstorage_literals avb)
target_compile_definitions(fastboot PRIVATE
	-D_GNU_SOURCE -D_XOPEN_SOURCE=700 -DUSE_F2FS
	-DANDROID_MKE2FS_NAME="${ANDROID_MKE2FS_NAME}")
target_link_libraries(fastboot
	libsparse libzip libcutils liblog liblp libutil
	libbase libext4 libselinux libsepol libdiagnoseusb crypto
	z PkgConfig::libpcre2-8 Threads::Threads dl)

if(APPLE)
	target_link_libraries(fastboot
		"-framework CoreFoundation"
		"-framework IOKit")
endif()
