xref: /aosp_15_r20/tools/netsim/cmake/netsim_dependencies.cmake (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
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")
50endif()
51set(CMAKE_CXX_STANDARD 17)
52set(CMAKE_CXX_STANDARD_REQUIRED ON)
53
54if(CMAKE_BUILD_TYPE STREQUAL "Debug")
55  add_definitions("-DANDROID_DEBUG")
56  if(NOT WINDOWS_MSVC_X86_64)
57    add_c_flag("-O0 -g3")
58  else()
59    add_c_flag("-Zi -Od")
60  endif()
61
62  if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CROSSCOMPILE)
63    if(NOT OPTION_ASAN AND OPTION_ASAN_IN_DEBUG)
64      set(OPTION_ASAN address)
65    endif()
66
67    if(OPTION_ASAN STREQUAL "thread" AND OPTION_COVERAGE_IN_DEBUG)
68      message(FATAL_ERROR "You cannot run tsan with code coverage enabled.")
69    endif()
70    if(NOT WINDOWS_MSVC_X86_64 AND OPTION_COVERAGE_IN_DEBUG)
71      message("Enabling code coverage")
72      # Build an instrumented version of the code  that generates coverage
73      # mapping to enable code coverage analysis
74      set(ANDROID_CODE_COVERAGE TRUE)
75      add_c_flag("-fcoverage-mapping")
76      add_c_flag("-fprofile-instr-generate")
77      add_c_flag("-fprofile-arcs")
78      add_c_flag("-ftest-coverage")
79      add_c_flag("--coverage")
80    endif()
81  endif()
82else()
83  set(CMAKE_INSTALL_DO_STRIP TRUE)
84  add_definitions("-DNDEBUG=1")
85  if(WINDOWS_MSVC_X86_64)
86    # clang-cl takes msvc based parameters, so -O3 is a nop
87    add_c_flag("-O2")
88  else()
89    add_c_flag("-O3 -g3")
90  endif()
91endif()
92
93# Target specific configurations that we do not want to do in the
94# toolchain.cmake Toolchain variables seem to be overwritten pending your cmake
95# version.
96if(LINUX_X86_64)
97  add_c_flag("-Werror")
98  add_c_flag("-Wno-deprecated-declarations") # Protobuf generates deprecation
99                                             # warnings for deprecated enums
100  # And the asm type if we are compiling with yasm
101  set(ANDROID_NASM_TYPE elf64)
102  # This should make sure we have sufficient information left to properly print
103  # std::string etc. see b/156534499 for details.
104  add_c_flag("-fno-limit-debug-info")
105elseif(LINUX_AARCH64)
106  set(ANDROID_NASM_TYPE elf64)
107  add_c_flag("-fpermissive")
108elseif(WINDOWS_MSVC_X86_64)
109  # And the asm type if we are compiling with yasm
110  set(ANDROID_NASM_TYPE win64)
111  set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
112elseif(DARWIN_X86_64 OR DARWIN_AARCH64)
113  # And the asm type if we are compiling with yasm
114  set(ANDROID_NASM_TYPE macho64)
115  # Always consider the source to be darwin.
116  add_definitions(-D_DARWIN_C_SOURCE=1)
117  add_c_flag("-Wno-everything")
118else()
119  message(FATAL_ERROR "Unknown target!")
120endif()
121
122prebuilt(Threads)
123
124# We need the auto generated header for some components, so let's set the
125# ANDROID_HW_CONFIG_H variable to point to the generated header. Those that need
126# it can add it to their sources list, and it will be there.
127set(HW_PROPERTIES_INI
128    ${EXTERNAL_QEMU}/android/emu/avd/src/android/avd/hardware-properties.ini)
129android_generate_hw_config()
130
131if(DARWIN_AARCH64 AND NOT Rust_COMPILER)
132  message(
133    STATUS
134      "On Apple sillicon attempting to use platform toolchain if available.")
135  list(APPEND CMAKE_MODULE_PATH
136       "${EXTERNAL_QEMU}/android/build/cmake/corrosion/cmake/")
137  find_package(Rust REQUIRED)
138  if(TARGET Rust::Rustc)
139    set(OPTION_ENABLE_SYSTEM_RUST TRUE)
140  else()
141    message(STATUS "Unable to derive local toolchain")
142    message(
143      FATAL_ERROR
144        "If you are a developer you can install rust with `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`"
145    )
146  endif()
147endif()
148
149if(WINDOWS_MSVC_X86_64)
150  # Set of msvc compat layer libraries.
151  add_subdirectory(${EXTERNAL_QEMU}/android/third_party/mman-win32 mman-win32)
152  add_subdirectory(${EXTERNAL_QEMU}/android/third_party/regex-win32 regex-win32)
153  add_subdirectory(${EXTERNAL_QEMU}/android/third_party/dirent-win32
154                   dirent-win32)
155endif()
156
157if(Rust_COMPILER OR OPTION_ENABLE_SYSTEM_RUST)
158  if(OPTION_ENABLE_SYSTEM_RUST)
159    message(STATUS "Attempting to use the system rust compiler")
160    use_system_rust_toolchain()
161  endif()
162
163  enable_vendorized_crates("${EXTERNAL_QEMU}/android/third_party/rust/crates")
164  add_subdirectory(${EXTERNAL_QEMU}/android/build/cmake/corrosion corrosion)
165  ensure_rust_version_is_compliant()
166endif()
167
168set(_gRPC_RE2_INCLUDE_DIR "${EXTERNAL_QEMU}/android/third_party/re2")
169set(_gRPC_RE2_LIBRARIES re2)
170set(NETSIM_EXT TRUE)
171
172# Let's bin place everything in the root, with the shared libs in the right
173# place
174set(DBG_INFO ${CMAKE_BINARY_DIR}/build/debug_info)
175set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib64)
176set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
177set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archives)
178set(CMAKE_PDB_OUTPUT_DIRECTORY ${DBG_INFO})
179# Feeling courageous? Set this to $ANDROID_SDK_ROOT
180if(DARWIN_X86_64 OR DARWIN_AARCH64)
181  set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/distribution/emulator)
182  set(CMAKE_INSTALL_CODESIGN ${CMAKE_BINARY_DIR}/distribution/_codesign)
183else()
184  set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/distribution/emulator)
185endif()
186
187# First make the protobuf and dependencies available to gRPC
188add_subdirectory(${EXTERNAL}/qemu/android/third_party/protobuf protobuf)
189
190add_subdirectory(${AOSP}/hardware/google/aemu/base aemu-base)
191add_subdirectory(${AOSP}/hardware/google/aemu/host-common host-common)
192add_subdirectory(${AOSP}/packages/modules/Bluetooth/tools/rootcanal rootcanal)
193add_subdirectory(${EXTERNAL_QEMU}/android/third_party/abseil-cpp abseil-cpp)
194add_subdirectory(${EXTERNAL_QEMU}/android/third_party/boringssl boringssl)
195add_subdirectory(${EXTERNAL_QEMU}/android/third_party/google-benchmark
196                 google-benchmark)
197add_subdirectory(${EXTERNAL_QEMU}/android/third_party/hostapd hostapd)
198add_subdirectory(${EXTERNAL_QEMU}/android/third_party/libslirp libslirp)
199add_subdirectory(${EXTERNAL_QEMU}/android/third_party/googletest/ gtest)
200add_subdirectory(${EXTERNAL_QEMU}/android/third_party/lz4 lz4)
201add_subdirectory(${EXTERNAL_QEMU}/android/third_party/re2 re2)
202add_subdirectory(${EXTERNAL_QEMU}/android/third_party/libselinux libselinux)
203add_subdirectory(${EXTERNAL_QEMU}/android/third_party/libsparse libsparse)
204add_subdirectory(${EXTERNAL_QEMU}/android/third_party/ext4_utils ext4_utils)
205add_subdirectory(${EXTERNAL}/cares cares)
206add_subdirectory(${EXTERNAL}/glib/glib glib2)
207add_subdirectory(${EXTERNAL}/grpc/emulator grpc)
208add_subdirectory(${EXTERNAL}/qemu/android/android-emu-base android-emu-base)
209add_subdirectory(${EXTERNAL}/qemu/android/android-net/android android-emu-net)
210add_subdirectory(${EXTERNAL}/qemu/android-qemu2-glue/netsim
211                 android-wifi-service)
212add_subdirectory(${EXTERNAL}/qemu/android/emu/base emu-base)
213add_subdirectory(${EXTERNAL}/qemu/android/emu/utils android-emu-utils)
214add_subdirectory(${EXTERNAL}/qemu/android/emu/files android-emu-files)
215add_subdirectory(${EXTERNAL}/qemu/android/emu/agents android-emu-agents)
216add_subdirectory(${EXTERNAL}/qemu/android/emu/proxy android-emu-proxy)
217add_subdirectory(${EXTERNAL}/webrtc/third_party/jsoncpp jsoncpp)
218
219# Short term fix for missing glib2 dll for Windows build
220if(WINDOWS_MSVC_X86_64)
221  install(TARGETS glib2_${ANDROID_TARGET_TAG} RUNTIME DESTINATION .
222          LIBRARY DESTINATION .)
223endif()
224
225if(NOT TARGET gfxstream-snapshot.headers)
226  # Fake dependency to satisfy linker
227  add_library(gfxstream-snapshot.headers INTERFACE)
228endif()
229
230if(CMAKE_BUILD_TYPE MATCHES DEBUG)
231  # This will help you find issues.
232  set(CMAKE_C_FLAGS "-fsanitize=address -fno-omit-frame-pointer -g3 -O0")
233  set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address")
234endif()
235
236if(LINUX_X86_64)
237  # Our linux headers are from 2013, and do not define newer socket options.
238  # (b/156635589)
239  target_compile_options(grpc PRIVATE -DSO_REUSEPORT=15)
240  target_compile_options(grpc_unsecure PRIVATE -DSO_REUSEPORT=15)
241endif()
242
243# Testing
244enable_testing()
245include(GoogleTest)
246