protobuf_generate_cpp(ADB_APP_PROCESSES_PROTO_SRCS ADB_APP_PROCESSES_PROTO_HDRS
	adb/proto/app_processes.proto)
protobuf_generate_cpp(ADB_HOST_PROTO_SRCS ADB_HOST_PROTO_HDRS
	adb/proto/adb_host.proto)
protobuf_generate_cpp(ADB_KEY_TYPE_PROTO_SRCS ADB_KEY_TYPE_PROTO_HDRS
	adb/proto/key_type.proto)
protobuf_generate_cpp(ADB_KNOWN_HOSTS_PROTO_SRCS ADB_KNOWN_HOSTS_PROTO_HDRS
	adb/proto/adb_known_hosts.proto)
protobuf_generate_cpp(ADB_PAIRING_PROTO_SRCS ADB_PAIRING_PROTO_HDRS
	adb/proto/pairing.proto)

# No fastdeploy because it requires deployagent.inc
set(libadb_SOURCES
	adb/client/adb_client.cpp
	adb/client/adb_install.cpp
	adb/client/adb_wifi.cpp
	adb/client/auth.cpp
	adb/client/bugreport.cpp
	adb/client/commandline.cpp
	adb/client/console.cpp
	adb/client/detach.cpp
	adb/client/discovered_services.cpp
	adb/client/file_sync_client.cpp
	adb/client/incremental.cpp
	adb/client/incremental_server.cpp
	adb/client/incremental_utils.cpp
	adb/client/line_printer.cpp
	adb/client/main.cpp
	adb/client/pairing/pairing_client.cpp
	adb/client/usb_libusb.cpp
	adb/client/usb_libusb_device.cpp
	adb/client/usb_libusb_hotplug.cpp
	adb/client/usb_libusb_inhouse_hotplug.cpp
	adb/client/transport_emulator.cpp
	adb/client/mdns_tracker.cpp
	adb/client/mdns_utils.cpp
	adb/client/transport_mdns.cpp
	adb/client/transport_usb.cpp
	adb/client/pairing/pairing_client.cpp
	adb/pairing_auth/aes_128_gcm.cpp
	adb/pairing_auth/pairing_auth.cpp
	adb/pairing_connection/pairing_connection.cpp
	adb/services.cpp
	adb/socket_spec.cpp
	adb/sysdeps_unix.cpp
	adb/sysdeps/errno.cpp
	adb/sysdeps/posix/network.cpp
	${ADB_APP_PROCESSES_PROTO_SRCS} ${ADB_APP_PROCESSES_PROTO_HDRS}
	${ADB_HOST_PROTO_SRCS} ${ADB_HOST_PROTO_HDRS}
	${ADB_KNOWN_HOSTS_PROTO_SRCS} ${ADB_KNOWN_HOSTS_PROTO_HDRS}
	${ADB_KEY_TYPE_PROTO_SRCS} ${ADB_KEY_TYPE_PROTO_HDRS}
	${ADB_PAIRING_PROTO_SRCS} ${ADB_PAIRING_PROTO_HDRS})

if(APPLE)
	list(APPEND libadb_SOURCES
		adb/client/usb_osx.cpp)
else()
	list(APPEND libadb_SOURCES
		adb/client/usb_linux.cpp
		adb/client/usb_linux_netlink.cpp)
endif()

add_library(libadb STATIC ${libadb_SOURCES})
target_compile_definitions(libadb PRIVATE -D_GNU_SOURCE)
target_compile_definitions(libadb PUBLIC -DADB_HOST=1)
target_include_directories(libadb PUBLIC
	boringssl/include
	adb
	adb/crypto/include
	adb/pairing_auth/include
	adb/pairing_connection/include
	adb/tls/include
	core/include
	core/libcrypto_utils/include
	core/libcutils/include
	libbase/include
	libziparchive/include)

if(ANDROID_TOOLS_USE_BUNDLED_LIBUSB)
	target_compile_definitions(libadb PUBLIC ANDROID_TOOLS_USE_BUNDLED_LIBUSB)
	target_include_directories(libadb PUBLIC libusb)
endif()

target_link_libraries(libadb PUBLIC fmt::fmt)

add_library(libadb_crypto_defaults STATIC
	adb/crypto/key.cpp
	adb/crypto/rsa_2048_key.cpp
	adb/crypto/x509_generator.cpp
	${ADB_KEY_TYPE_PROTO_HDRS})

target_include_directories(libadb_crypto_defaults PUBLIC
	adb
	adb/crypto/include
	boringssl/include
	core/libcrypto_utils/include
	libbase/include)


add_library(libadb_tls_connection_defaults STATIC
	adb/tls/adb_ca_list.cpp
	adb/tls/tls_connection.cpp)

target_include_directories(libadb_tls_connection_defaults PUBLIC
	adb
	adb/tls/include
	boringssl/include
	libbase/include)

add_library(liblog STATIC
	logging/liblog/log_event_list.cpp
	logging/liblog/log_event_write.cpp
	logging/liblog/logger_name.cpp
	logging/liblog/logger_read.cpp
	logging/liblog/logger_write.cpp
	logging/liblog/properties.cpp
	logging/liblog/logprint.cpp)

target_compile_definitions(liblog PRIVATE
	-DLIBLOG_LOG_TAG=1006 -D_XOPEN_SOURCE=700 -DFAKE_LOG_DEVICE=1)
target_include_directories(liblog PUBLIC
	core/include
	logging/liblog/include
	core/libcutils/include
	libbase/include)

set(libcutils_SOURCES
	core/libcutils/android_get_control_file.cpp
	core/libcutils/ashmem-host.cpp
	core/libcutils/config_utils.cpp
	core/libcutils/fs.cpp
	core/libcutils/hashmap.cpp
	core/libcutils/iosched_policy.cpp
	core/libcutils/load_file.cpp
	core/libcutils/multiuser.cpp
	core/libcutils/native_handle.cpp
	core/libcutils/properties.cpp
	core/libcutils/record_stream.cpp
	core/libcutils/socket_inaddr_any_server_unix.cpp
	core/libcutils/socket_local_client_unix.cpp
	core/libcutils/socket_local_server_unix.cpp
	core/libcutils/socket_network_client_unix.cpp
	core/libcutils/sockets_utils.cpp
	core/libcutils/sockets_unix.cpp
	core/libcutils/sockets.cpp
	core/libcutils/str_parms.cpp
	core/libcutils/strlcpy.c
	core/libcutils/trace-host.cpp)

if (NOT APPLE AND NOT WIN32)
	list(APPEND libcutils_SOURCES
		core/libcutils/canned_fs_config.cpp
		core/libcutils/fs_config.cpp)
endif()

add_library(libcutils STATIC ${libcutils_SOURCES})
target_compile_definitions(libcutils PRIVATE -D_GNU_SOURCE)
target_include_directories(libcutils PRIVATE
	logging/liblog/include core/libutils/include)
target_include_directories(libcutils PUBLIC
	core/libcutils/include libbase/include)

add_library(libdiagnoseusb STATIC
	core/diagnose_usb/diagnose_usb.cpp)
target_include_directories(libdiagnoseusb PUBLIC
	core/diagnose_usb/include)

target_include_directories(libdiagnoseusb PUBLIC
	core/include libbase/include)

add_library(libadb_sysdeps STATIC
	adb/sysdeps/env.cpp)
target_include_directories(libadb_sysdeps PUBLIC
	libbase/include
	adb)

add_library(libcrypto STATIC
	core/libcrypto_utils/android_pubkey.cpp)

target_include_directories(libcrypto PUBLIC
	core/libcrypto_utils/include boringssl/include)

add_executable(adb
	  adb/adb.cpp
	  adb/adb_io.cpp
	  adb/adb_listeners.cpp
	  adb/adb_trace.cpp
	  adb/adb_unique_fd.cpp
	  adb/adb_utils.cpp
	  adb/fdevent/fdevent.cpp
	  adb/fdevent/fdevent_poll.cpp
	  adb/fdevent/fdevent_epoll.cpp
	  adb/shell_service_protocol.cpp
	  adb/sockets.cpp
	  adb/socket_spec.cpp
	  adb/transport.cpp
	  adb/transport_fd.cpp
	  adb/types.cpp
	  ${ADB_KEY_TYPE_PROTO_HDRS})

target_include_directories(adb PRIVATE
	core/include libbase/include adb
	core/libcrypto_utils/include boringssl/include)
target_link_libraries(adb
	libadb
	libadb_crypto_defaults
	libadb_tls_connection_defaults
	libbase
	libcrypto
	libadb_sysdeps
	libcutils
	libdiagnoseusb
	liblog
	libzip
	crypto
	ssl
	protobuf::libprotobuf
	PkgConfig::libbrotlicommon
	PkgConfig::libbrotlidec
	PkgConfig::libbrotlienc
	PkgConfig::liblz4
	Threads::Threads
	z
	PkgConfig::libzstd)

if(ANDROID_TOOLS_USE_BUNDLED_LIBUSB)
	target_link_libraries(adb android_tools_libusb)
else()
	target_link_libraries(adb PkgConfig::libusb-1.0)
endif()

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

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/adb/docs/user/adb.1)
	install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/adb/docs/user/adb.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
endif()
