diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f65519e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/** diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..dfdbfc18 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.13) + +project(nearby CXX) + +option(nearby_USE_LOCAL_PROTOBUF + "Use local copy of protobuf library and compiler" OFF) + +option(nearby_USE_LOCAL_ABSL + "Use local copy of abseil-cpp library" OFF) + +# target_sources() may convert relative paths to absolute +cmake_policy(SET CMP0076 NEW) + +set (CMAKE_CXX_STANDARD 17) +set (CMAKE_CXX_STANDARD_REQUIRED ON) + +include(cmake/proto_defs.cmake) +include(cmake/local_build_setup.cmake) + +if (nearby_USE_LOCAL_PROTOBUF) + include(cmake/local_build_protobuf.cmake) +endif() + +include(cmake/local_setup_smhasher.cmake) + +find_package(Protobuf REQUIRED) + +enable_testing() + +if (NOT TARGET ukey2) +add_subdirectory(third_party/ukey2) +endif() +if (NOT TARGET gtest) +add_subdirectory(third_party/gtest) +endif() +if (nearby_USE_LOCAL_ABSL) + if (NOT TARGET absl::base) + add_subdirectory(third_party/absl) + endif() +else() + find_package(absl REQUIRED) +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/cpp) + +add_subdirectory(cpp/core) +add_subdirectory(cpp/platform) +add_subdirectory(proto) diff --git a/cmake/CMakeLists-smhasher.txt b/cmake/CMakeLists-smhasher.txt new file mode 100644 index 00000000..08761ae1 --- /dev/null +++ b/cmake/CMakeLists-smhasher.txt @@ -0,0 +1,14 @@ +project(smhasher CXX) + +cmake_minimum_required(VERSION 3.13) + +add_library(smhasher_murmur3 STATIC + cpp/src/smhasher/MurmurHash3.cpp +) + +target_include_directories(smhasher_murmur3 + PUBLIC + cpp/include + PRIVATE + cpp/include/smhasher +) diff --git a/cmake/local_build_protobuf.cmake b/cmake/local_build_protobuf.cmake new file mode 100644 index 00000000..c10f38d3 --- /dev/null +++ b/cmake/local_build_protobuf.cmake @@ -0,0 +1,28 @@ +if (NOT EXISTS ${TOOLS_INSTALL_PREFIX}/bin/protoc) + set(PKG_BUILD_ROOT ${TOOLS_BUILD_ROOT}/protobuf) + set(PKG_SRC_ROOT ${CMAKE_SOURCE_DIR}/third_party/protobuf) + execute_process( + COMMAND mkdir -p ${PKG_BUILD_ROOT} + ) + execute_process( + COMMAND cmake ${PKG_SRC_ROOT}/cmake + WORKING_DIRECTORY ${PKG_BUILD_ROOT} + ) + execute_process( + COMMAND make -j${N_CPUS} + WORKING_DIRECTORY ${PKG_BUILD_ROOT} + ) + execute_process( + COMMAND make check + WORKING_DIRECTORY ${PKG_BUILD_ROOT} + RESULT_VARIABLE test_exit_code + ERROR_QUIET + ) + if (NOT ${test_exit_code} EQUAL "0") + message(FATAL_ERROR "Protobuf tests failed; can't use this protobuf") + endif() + execute_process( + COMMAND /bin/bash -c "DESTDIR=${TOOLS_INSTALL_ROOT} make install" + WORKING_DIRECTORY ${PKG_BUILD_ROOT} + ) +endif() diff --git a/cmake/local_build_setup.cmake b/cmake/local_build_setup.cmake new file mode 100644 index 00000000..dd85a0a9 --- /dev/null +++ b/cmake/local_build_setup.cmake @@ -0,0 +1,12 @@ +include(ProcessorCount) +ProcessorCount(N_CPUS) + +if (N_CPUS EQUAL 0) + set (N_CPUS 1) +endif() + +set (TOOLS_ROOT ${CMAKE_BINARY_DIR}/stage) +set (TOOLS_BUILD_ROOT ${TOOLS_ROOT}/build) +set (TOOLS_INSTALL_ROOT ${TOOLS_ROOT}/install) +set (TOOLS_INSTALL_PREFIX ${TOOLS_INSTALL_ROOT}/usr/local) +set (CMAKE_FIND_ROOT_PATH ${TOOLS_INSTALL_ROOT}) diff --git a/cmake/local_setup_smhasher.cmake b/cmake/local_setup_smhasher.cmake new file mode 100644 index 00000000..cb4164dd --- /dev/null +++ b/cmake/local_setup_smhasher.cmake @@ -0,0 +1,22 @@ +set(PKG_STAGE_SRC_ROOT ${TOOLS_ROOT}/src/smhasher) +if (NOT EXISTS ${PKG_STAGE_SRC_ROOT}/CMakeLists.txt) + set(PKG_SRC_ROOT ${PROJECT_SOURCE_DIR}/third_party/smhasher) + execute_process( + COMMAND mkdir -p ${PKG_STAGE_SRC_ROOT}/cpp/src/smhasher + ) + execute_process( + COMMAND mkdir -p ${PKG_STAGE_SRC_ROOT}/cpp/include/smhasher + ) + execute_process( + COMMAND cp ${PKG_SRC_ROOT}/src/MurmurHash3.cpp ${PKG_STAGE_SRC_ROOT}/cpp/src/smhasher + ) + execute_process( + COMMAND cp ${PKG_SRC_ROOT}/src/MurmurHash3.h ${PKG_STAGE_SRC_ROOT}/cpp/include/smhasher + ) + execute_process( + COMMAND cp cmake/CMakeLists-smhasher.txt ${PKG_STAGE_SRC_ROOT}/CMakeLists.txt + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) +endif() + +add_subdirectory(${PKG_STAGE_SRC_ROOT}) diff --git a/cmake/proto_defs.cmake b/cmake/proto_defs.cmake new file mode 100644 index 00000000..d9a0d1cb --- /dev/null +++ b/cmake/proto_defs.cmake @@ -0,0 +1,28 @@ +function(add_cc_proto_library NAME) + set(single) + set(multi_args PROTOS INCS DEPS) + cmake_parse_arguments(PARSE_ARGV 1 args "" "${single}" "${multi_args}") + + protobuf_generate( + PROTOS ${args_PROTOS} + LANGUAGE cpp + OUT_VAR ${NAME}_var + ) + + add_library(${NAME} + ${${NAME}_var} + ) + + target_link_libraries(${NAME} + PUBLIC + ${Protobuf_LIBRARIES} + ${args_DEPS} + ) + + target_include_directories(${NAME} + PUBLIC + ${Protobuf_INCLUDE_DIRS} + ${args_INCS} + ${CMAKE_CURRENT_BINARY_DIR} + ) +endfunction() diff --git a/cpp/core/CMakeLists.txt b/cpp/core/CMakeLists.txt new file mode 100644 index 00000000..8f2a70c0 --- /dev/null +++ b/cpp/core/CMakeLists.txt @@ -0,0 +1,58 @@ +add_library(core STATIC) + +target_sources(core + PUBLIC + core.h +) + +target_include_directories(core + PUBLIC + ${PROJECT_SOURCE_DIR}/cpp +) + +target_link_libraries(core + PUBLIC + core_internal + platform_types +) + +add_library(core_types STATIC) + +target_sources(core_types + PRIVATE + payload.cc + strategy.cc + PUBLIC + listeners.h + options.h + params.h + payload.h + status.h + strategy.h +) + +target_link_libraries(core_types + PUBLIC + platform_api + platform_port_string + platform_types + platform_utils +) + +add_executable(core_build_test + check_compilation.cc +) + +target_link_libraries(core_build_test + PUBLIC + absl::strings + core + core_types + platform_impl_default_lock + platform_impl_sample + platform_port_string + platform_types + platform_utils +) + +add_subdirectory(internal) diff --git a/cpp/core/internal/CMakeLists.txt b/cpp/core/internal/CMakeLists.txt new file mode 100644 index 00000000..e9cc7350 --- /dev/null +++ b/cpp/core/internal/CMakeLists.txt @@ -0,0 +1,80 @@ +add_library(core_internal STATIC) + +target_sources(core_internal + PRIVATE + ble_advertisement.cc + bluetooth_device_name.cc + internal_payload.cc + internal_payload.h + loop_runner.cc + loop_runner.h + offline_frames.cc + offline_frames.h + PUBLIC + bandwidth_upgrade_handler.h + bandwidth_upgrade_manager.h + base_bandwidth_upgrade_handler.h + base_endpoint_channel.h + base_pcp_handler.h + ble_advertisement.h + ble_compat.h + ble_endpoint_channel.h + bluetooth_device_name.h + bluetooth_endpoint_channel.h + client_proxy.h + encryption_runner.h + endpoint_channel.h + endpoint_channel_manager.h + endpoint_manager.h + internal_payload_factory.h + medium_manager.h + offline_service_controller.h + p2p_cluster_pcp_handler.h + p2p_point_to_point_pcp_handler.h + p2p_star_pcp_handler.h + payload_manager.h + pcp.h + pcp_handler.h + pcp_manager.h + service_controller.h + service_controller_router.h + wifi_lan_upgrade_handler.h +) + +target_link_libraries(core_internal + PUBLIC + absl::strings + core_internal_mediums + core_types + platform_api + platform_port_down_cast + platform_port_string + platform_types + platform_utils + proto_connections_enums_cc_proto + proto_offline_wire_formats_cc_proto + ukey2 +) + +add_executable(core_internal_test + bluetooth_device_name_test.cc + ble_advertisement_test.cc +) + +add_test( + NAME core_internal_test + COMMAND core_internal_test +) + +target_link_libraries(core_internal_test + PUBLIC + core_internal + gtest + gtest_main + platform_impl_default_cond_var + platform_impl_default_lock + platform_port_string + platform_utils +) + +add_subdirectory(mediums) diff --git a/cpp/core/internal/mediums/CMakeLists.txt b/cpp/core/internal/mediums/CMakeLists.txt new file mode 100644 index 00000000..e9d3b84b --- /dev/null +++ b/cpp/core/internal/mediums/CMakeLists.txt @@ -0,0 +1,57 @@ +add_library(core_internal_mediums STATIC) + +target_sources(core_internal_mediums + PRIVATE + ble_advertisement.cc + ble_advertisement_header.cc + ble_packet.cc + ble_peripheral.cc + utils.cc + utils.h + PUBLIC + advertisement_read_result.h + ble.h + ble_advertisement.h + ble_advertisement_header.h + ble_packet.h + ble_peripheral.h + ble_v2.h + bloom_filter.h + bluetooth_classic.h + bluetooth_radio.h + discovered_peripheral_callback.h + discovered_peripheral_tracker.h + lost_entity_tracker.h + mediums.h + uuid.h +) + +target_link_libraries(core_internal_mediums + PUBLIC + absl::numeric + absl::strings + platform_api + platform_port_string + platform_types + platform_utils + smhasher_murmur3 +) + +add_executable(core_internal_mediums_test + advertisement_read_result_test.cc + ble_advertisement_header_test.cc + ble_advertisement_test.cc + ble_packet_test.cc + bloom_filter_test.cc + lost_entity_tracker_test.cc +) + +target_link_libraries(core_internal_mediums_test + PUBLIC + absl::time + core_internal_mediums + gtest + gtest_main + platform_impl_default + platform_utils +) diff --git a/cpp/platform/CMakeLists.txt b/cpp/platform/CMakeLists.txt new file mode 100644 index 00000000..de8405cc --- /dev/null +++ b/cpp/platform/CMakeLists.txt @@ -0,0 +1,79 @@ +add_library(platform_utils STATIC + base64_utils.cc + file_impl.cc + prng.cc + reliability_utils.cc +) + +target_sources(platform_utils + PUBLIC + base64_utils.h + cancelable_alarm.h + file_impl.h + pipe.h + prng.h + reliability_utils.h + synchronized.h +) + +target_link_libraries(platform_utils + PUBLIC + platform_types + platform_api + absl::strings +) + +add_library(platform_types STATIC + ptr.cc +) + +target_sources(platform_types + PUBLIC + byte_array.h + callable.h + cancelable.h + container_of.h + exception.h + logging.h + ptr.h + runnable.h +) + +target_link_libraries(platform_types + PUBLIC + absl::base + absl::strings +) + +add_executable(platform_test + byte_array_test.cc + container_of_test.cc + file_impl_test.cc + pipe_test.cc + prng_test.cc + ptr_test.cc +) + +target_link_libraries(platform_test + PUBLIC + absl::base + absl::strings + absl::time + gtest + gtest_main + platform_api + platform_impl_default_cond_var + platform_impl_default_lock + platform_types + platform_utils +) + +add_test( + NAME platform_test + COMMAND platform_test +) + +add_subdirectory(api) +add_subdirectory(impl/sample) +add_subdirectory(impl/default) +add_subdirectory(port) diff --git a/cpp/platform/api/CMakeLists.txt b/cpp/platform/api/CMakeLists.txt new file mode 100644 index 00000000..35cd7d06 --- /dev/null +++ b/cpp/platform/api/CMakeLists.txt @@ -0,0 +1,32 @@ +add_library(platform_api STATIC + atomic_boolean.h + atomic_reference.h + ble.h + ble_v2.h + bluetooth_adapter.h + bluetooth_classic.h + condition_variable.h + count_down_latch.h + executor.h + future.h + hash_utils.h + input_file.h + input_stream.h + lock.h + multi_thread_executor.h + output_file.h + output_stream.h + scheduled_executor.h + settable_future.h + single_thread_executor.h + socket.h + submittable_executor.h + system_clock.h + thread_utils.h + wifi.h +) + +target_link_libraries(platform_api + PUBLIC + platform_types +) diff --git a/cpp/platform/impl/default/CMakeLists.txt b/cpp/platform/impl/default/CMakeLists.txt new file mode 100644 index 00000000..353b2be0 --- /dev/null +++ b/cpp/platform/impl/default/CMakeLists.txt @@ -0,0 +1,53 @@ +add_library(platform_impl_default STATIC) + +target_sources(platform_impl_default + PRIVATE + default_condition_variable.cc + default_lock.cc + default_platform.cc + PUBLIC + default_condition_variable.h + default_lock.h + default_platform.h +) + +target_include_directories(platform_impl_default + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_link_libraries(platform_impl_default + PUBLIC + platform_api + platform_types +) + +add_library(platform_impl_default_lock STATIC) + +target_sources(platform_impl_default_lock + PRIVATE + default_lock.cc + PUBLIC + default_lock.h +) + +target_link_libraries(platform_impl_default_lock + PUBLIC + platform_api +) + +add_library(platform_impl_default_cond_var STATIC) + +target_sources(platform_impl_default_cond_var + PRIVATE + default_condition_variable.cc + PUBLIC + default_condition_variable.h +) + +target_link_libraries(platform_impl_default_cond_var + PUBLIC + platform_api + platform_impl_default_lock + platform_types +) diff --git a/cpp/platform/impl/sample/CMakeLists.txt b/cpp/platform/impl/sample/CMakeLists.txt new file mode 100644 index 00000000..0944ac4f --- /dev/null +++ b/cpp/platform/impl/sample/CMakeLists.txt @@ -0,0 +1,18 @@ +add_library(platform_impl_sample STATIC) + +target_sources(platform_impl_sample + PRIVATE + sample_wifi_medium.cc + PUBLIC + sample_platform.h + sample_wifi_medium.h +) + +target_link_libraries(platform_impl_sample + PUBLIC + absl::time + platform_api + platform_port_string + platform_types + platform_utils +) diff --git a/cpp/platform/port/CMakeLists.txt b/cpp/platform/port/CMakeLists.txt new file mode 100644 index 00000000..18dbe0c3 --- /dev/null +++ b/cpp/platform/port/CMakeLists.txt @@ -0,0 +1,30 @@ +add_library(platform_port_config_private INTERFACE) + +target_sources(platform_port_config_private + INTERFACE + config.h +) + +add_library(platform_port_string INTERFACE) + +target_sources(platform_port_string + INTERFACE + string.h +) + +target_link_libraries(platform_port_string + INTERFACE + platform_port_config_private +) + +add_library(platform_port_down_cast INTERFACE) + +target_sources(platform_port_down_cast + INTERFACE + down_cast.h +) + +target_link_libraries(platform_port_down_cast + INTERFACE + platform_port_config_private +) diff --git a/proto/CMakeLists.txt b/proto/CMakeLists.txt new file mode 100644 index 00000000..7eb4378b --- /dev/null +++ b/proto/CMakeLists.txt @@ -0,0 +1,49 @@ +add_cc_proto_library( + proto_bootstrap_enums_cc_proto + PROTOS bootstrap_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_connections_enums_cc_proto + PROTOS connections_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_discovery_enums_cc_proto + PROTOS discovery_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_magic_pair_enums_cc_proto + PROTOS magic_pair_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_nearby_client_enums_cc_proto + PROTOS nearby_client_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_nearby_event_codes_cc_proto + PROTOS nearby_event_codes.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_setup_enums_cc_proto + PROTOS setup_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_cc_proto_library( + proto_sharing_enums_cc_proto + PROTOS sharing_enums.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/.. +) + +add_subdirectory(connections) diff --git a/proto/connections/CMakeLists.txt b/proto/connections/CMakeLists.txt new file mode 100644 index 00000000..0b36c455 --- /dev/null +++ b/proto/connections/CMakeLists.txt @@ -0,0 +1,5 @@ +add_cc_proto_library( + proto_offline_wire_formats_cc_proto + PROTOS offline_wire_formats.proto + INCS ${CMAKE_CURRENT_BINARY_DIR}/../.. +)