1set(BLUETOOTH_EMULATION True) 2get_filename_component(AOSP "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE) 3set(EXTERNAL ${AOSP}/external) 4set(EXTERNAL_QEMU ${EXTERNAL}/qemu) 5set(ANDROID_QEMU2_TOP_DIR ${EXTERNAL_QEMU}) 6 7if(NOT Python_EXECUTABLE) 8 find_package(Python3 COMPONENTS Interpreter) 9 if(NOT Python3_FOUND) 10 message(FATAL_ERROR "A python interpreter is required. ") 11 endif() 12 set(Python_EXECUTABLE ${Python3_EXECUTABLE}) 13endif() 14 15message(STATUS "Using Python: ${Python_EXECUTABLE}") 16if(NOT DEFINED ANDROID_TARGET_TAG) 17 message( 18 WARNING 19 "You should invoke the cmake generator with a proper toolchain from ${EXTERNAL_QEMU}/android/build/cmake, " 20 "Trying to infer toolchain, this might not work.") 21 list(APPEND CMAKE_MODULE_PATH "${EXTERNAL_QEMU}/android/build/cmake/") 22 include(toolchain) 23 _get_host_tag(TAG) 24 toolchain_configure_tags(${TAG}) 25endif() 26 27include(android) 28include(prebuilts) 29 30# Append the given flags to the existing CMAKE_C_FLAGS. Be careful as these 31# flags are global and used for every target! Note this will not do anything 32# under vs for now 33function(add_c_flag FLGS) 34 foreach(FLAG ${FLGS}) 35 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAG}" PARENT_SCOPE) 36 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}" PARENT_SCOPE) 37 endforeach() 38endfunction() 39 40function(add_cxx_flag FLGS) 41 foreach(FLAG ${FLGS}) 42 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}" PARENT_SCOPE) 43 endforeach() 44endfunction() 45 46if(WINDOWS_MSVC_X86_64) 47 add_cxx_flag("-std:c++17") 48else() 49 add_cxx_flag("-std=c++17") 50 add_cxx_flag("-fno-exceptions") 51endif() 52set(CMAKE_CXX_STANDARD 17) 53set(CMAKE_CXX_STANDARD_REQUIRED ON) 54 55if(CMAKE_BUILD_TYPE STREQUAL "Debug") 56 add_definitions("-DANDROID_DEBUG") 57 if(NOT WINDOWS_MSVC_X86_64) 58 add_c_flag("-O0 -g3") 59 else() 60 add_c_flag("-Zi -Od") 61 endif() 62 63 if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CROSSCOMPILE) 64 if(NOT OPTION_ASAN AND OPTION_ASAN_IN_DEBUG) 65 set(OPTION_ASAN address) 66 endif() 67 68 if(OPTION_ASAN STREQUAL "thread" AND OPTION_COVERAGE_IN_DEBUG) 69 message(FATAL_ERROR "You cannot run tsan with code coverage enabled.") 70 endif() 71 if(NOT WINDOWS_MSVC_X86_64 AND OPTION_COVERAGE_IN_DEBUG) 72 message("Enabling code coverage") 73 # Build an instrumented version of the code that generates coverage 74 # mapping to enable code coverage analysis 75 set(ANDROID_CODE_COVERAGE TRUE) 76 add_c_flag("-fcoverage-mapping") 77 add_c_flag("-fprofile-instr-generate") 78 add_c_flag("-fprofile-arcs") 79 add_c_flag("-ftest-coverage") 80 add_c_flag("--coverage") 81 endif() 82 endif() 83else() 84 set(CMAKE_INSTALL_DO_STRIP TRUE) 85 add_definitions("-DNDEBUG=1") 86 if(WINDOWS_MSVC_X86_64) 87 # clang-cl takes msvc based parameters, so -O3 is a nop 88 add_c_flag("-O2") 89 else() 90 add_c_flag("-O3 -g3") 91 endif() 92endif() 93 94# Target specific configurations that we do not want to do in the 95# toolchain.cmake Toolchain variables seem to be overwritten pending your cmake 96# version. 97if(LINUX_X86_64) 98 add_c_flag("-Werror") 99 add_c_flag("-Wno-deprecated-declarations") # Protobuf generates deprecation 100 # warnings for deprecated enums 101 # And the asm type if we are compiling with yasm 102 set(ANDROID_NASM_TYPE elf64) 103 # This should make sure we have sufficient information left to properly print 104 # std::string etc. see b/156534499 for details. 105 add_c_flag("-fno-limit-debug-info") 106elseif(LINUX_AARCH64) 107 set(ANDROID_NASM_TYPE elf64) 108 add_c_flag("-fpermissive") 109elseif(WINDOWS_MSVC_X86_64) 110 # And the asm type if we are compiling with yasm 111 set(ANDROID_NASM_TYPE win64) 112 set(CMAKE_SHARED_LIBRARY_PREFIX "lib") 113elseif(DARWIN_X86_64 OR DARWIN_AARCH64) 114 # And the asm type if we are compiling with yasm 115 set(ANDROID_NASM_TYPE macho64) 116 # Always consider the source to be darwin. 117 add_definitions(-D_DARWIN_C_SOURCE=1) 118 add_c_flag("-Wno-everything") 119else() 120 message(FATAL_ERROR "Unknown target!") 121endif() 122 123prebuilt(Threads) 124 125if(DARWIN_AARCH64 AND NOT Rust_COMPILER) 126 message( 127 STATUS 128 "On Apple sillicon attempting to use platform toolchain if available.") 129 list(APPEND CMAKE_MODULE_PATH 130 "${EXTERNAL_QEMU}/android/build/cmake/corrosion/cmake/") 131 find_package(Rust REQUIRED) 132 if(TARGET Rust::Rustc) 133 set(OPTION_ENABLE_SYSTEM_RUST TRUE) 134 else() 135 message(STATUS "Unable to derive local toolchain") 136 message( 137 FATAL_ERROR 138 "If you are a developer you can install rust with `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`" 139 ) 140 endif() 141endif() 142 143if(WINDOWS_MSVC_X86_64) 144 # Set of msvc compat layer libraries. 145 add_subdirectory(${EXTERNAL_QEMU}/android/third_party/mman-win32 mman-win32) 146 add_subdirectory(${EXTERNAL_QEMU}/android/third_party/regex-win32 regex-win32) 147 add_subdirectory(${EXTERNAL_QEMU}/android/third_party/dirent-win32 148 dirent-win32) 149endif() 150 151if(Rust_COMPILER OR OPTION_ENABLE_SYSTEM_RUST) 152 if(OPTION_ENABLE_SYSTEM_RUST) 153 message(STATUS "Attempting to use the system rust compiler") 154 use_system_rust_toolchain() 155 endif() 156 157 enable_vendorized_crates("${EXTERNAL_QEMU}/android/third_party/rust/crates") 158 add_subdirectory(${EXTERNAL_QEMU}/android/build/cmake/corrosion corrosion) 159 ensure_rust_version_is_compliant() 160endif() 161 162set(_gRPC_RE2_INCLUDE_DIR "${EXTERNAL_QEMU}/android/third_party/re2") 163set(_gRPC_RE2_LIBRARIES re2) 164set(NETSIM_EXT TRUE) 165 166# Let's bin place everything in the root, with the shared libs in the right 167# place 168set(DBG_INFO ${CMAKE_BINARY_DIR}/build/debug_info) 169set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib64) 170set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) 171set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archives) 172set(CMAKE_PDB_OUTPUT_DIRECTORY ${DBG_INFO}) 173# Feeling courageous? Set this to $ANDROID_SDK_ROOT 174if(DARWIN_X86_64 OR DARWIN_AARCH64) 175 set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/distribution/emulator) 176 set(CMAKE_INSTALL_CODESIGN ${CMAKE_BINARY_DIR}/distribution/_codesign) 177else() 178 set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/distribution/emulator) 179endif() 180 181# First make the protobuf and dependencies available to gRPC 182add_subdirectory(${EXTERNAL}/qemu/android/third_party/protobuf protobuf) 183 184add_subdirectory(${AOSP}/hardware/google/aemu/base aemu-base) 185add_subdirectory(${AOSP}/hardware/google/aemu/host-common host-common) 186add_subdirectory(${AOSP}/packages/modules/Bluetooth/tools/rootcanal rootcanal) 187add_subdirectory(${EXTERNAL_QEMU}/android/third_party/abseil-cpp abseil-cpp) 188add_subdirectory(${EXTERNAL_QEMU}/android/third_party/boringssl boringssl) 189add_subdirectory(${EXTERNAL_QEMU}/android/third_party/google-benchmark 190 google-benchmark) 191add_subdirectory(${EXTERNAL_QEMU}/android/third_party/hostapd hostapd) 192add_subdirectory(${EXTERNAL_QEMU}/android/third_party/libslirp libslirp) 193add_subdirectory(${EXTERNAL_QEMU}/android/third_party/googletest/ gtest) 194add_subdirectory(${EXTERNAL_QEMU}/android/third_party/lz4 lz4) 195add_subdirectory(${EXTERNAL_QEMU}/android/third_party/re2 re2) 196add_subdirectory(${EXTERNAL}/cares cares) 197add_subdirectory(${EXTERNAL}/glib/glib glib2) 198add_subdirectory(${EXTERNAL}/grpc/emulator grpc) 199add_subdirectory(${EXTERNAL}/qemu/android/android-emu-base android-emu-base) 200add_subdirectory(${EXTERNAL}/qemu/android/android-net/android android-emu-net) 201add_subdirectory(${EXTERNAL}/qemu/android/emu/base emu-base) 202add_subdirectory(${EXTERNAL}/qemu/android/emu/utils android-emu-utils) 203add_subdirectory(${EXTERNAL}/webrtc/third_party/jsoncpp jsoncpp) 204 205# Short term fix for missing glib2 dll for Windows build 206if(WINDOWS_MSVC_X86_64) 207 install(TARGETS glib2_${ANDROID_TARGET_TAG} RUNTIME DESTINATION . 208 LIBRARY DESTINATION .) 209endif() 210 211if(NOT TARGET gfxstream-snapshot.headers) 212 # Fake dependency to satisfy linker 213 add_library(gfxstream-snapshot.headers INTERFACE) 214endif() 215 216if(CMAKE_BUILD_TYPE MATCHES DEBUG) 217 # This will help you find issues. 218 set(CMAKE_C_FLAGS "-fsanitize=address -fno-omit-frame-pointer -g3 -O0") 219 set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address") 220endif() 221 222if(LINUX_X86_64) 223 # Our linux headers are from 2013, and do not define newer socket options. 224 # (b/156635589) 225 target_compile_options(grpc PRIVATE -DSO_REUSEPORT=15) 226 target_compile_options(grpc_unsecure PRIVATE -DSO_REUSEPORT=15) 227endif() 228 229# Testing 230enable_testing() 231include(GoogleTest) 232