Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake] Fix using an fmt coming from a parent source tree #313

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
61 changes: 40 additions & 21 deletions cmake/find/FindDBus1.cmake
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
# Attempt to load DBus1 as CONFIG silently
find_package(DBus1 CONFIG QUIET)

# If DBus1 was not found, try to find it as MODULE
if(NOT DBus1_FOUND)

find_package(PkgConfig REQUIRED)
pkg_search_module(PC_DBUS REQUIRED dbus-1)

set(DBus1_LIBRARIES ${PC_DBUS_LIBRARIES})
set(DBus1_INCLUDE_DIRS ${PC_DBUS_INCLUDE_DIRS})
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(_dbus_hint QUIET dbus-1)
endif()

# setup imported target
add_library(dbus-1 SHARED IMPORTED)
set_property(TARGET dbus-1 APPEND PROPERTY IMPORTED_LOCATION ${PC_DBUS_LINK_LIBRARIES})
set_property(TARGET dbus-1 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DBus1_INCLUDE_DIRS})
find_path(DBus1_INCLUDE_DIRS
NAMES
dbus/dbus.h
HINTS
${_dbus_hint_INCLUDE_DIRS}
PATH_SUFFIXES
include/
include/dbus-1.0/
dbus-1.0/
)

set(DBus1_FOUND ${PC_DBUS_FOUND})
if(NOT DBus1_INCLUDE_DIRS)
set(DBus1_FOUND 0)
return()
endif()

# get_cmake_property(_variableNames VARIABLES)
# list (SORT _variableNames)
# foreach (_variableName ${_variableNames})
# message(STATUS "${_variableName}=${${_variableName}}")
# endforeach()
find_path(DBus1_ARCH_INCLUDE_DIRS
NAMES
dbus/dbus-arch-deps.h
HINTS
${_dbus_hint_INCLUDE_DIRS}
PATH_SUFFIXES
include/
include/dbus-1.0/
dbus-1.0/
lib/dbus-1.0/include/
lib64/dbus-1.0/include/
lib32/dbus-1.0/include/
)

if(NOT DBus1_ARCH_INCLUDE_DIRS)
set(DBus1_FOUND 0)
return()
endif()

# setup imported target
add_library(dbus-1-headers-only INTERFACE)
add_library(dbus-1::dbus-1-headers-only ALIAS dbus-1-headers-only)
set_property(TARGET dbus-1-headers-only APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DBus1_INCLUDE_DIRS} ${DBus1_ARCH_INCLUDE_DIRS})
set(DBus1_FOUND 1)
2 changes: 1 addition & 1 deletion cmake/parse_version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# <major>.<minor>.<patch>
# <major>.<minor>.<patch>-<flavor>
function(parse_version FILE PROJECT)
configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/VERSION") # Required to re-run cmake on version change
configure_file("${FILE}" "${CMAKE_CURRENT_BINARY_DIR}/VERSION_FILE") # Required to re-run cmake on version change
file(READ ${FILE} VERSION_STR)
string(STRIP ${VERSION_STR} VERSION_STR)
if(${VERSION_STR} MATCHES "([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?")
Expand Down
2 changes: 1 addition & 1 deletion cmake/prelude.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/find)

include(${CMAKE_CURRENT_LIST_DIR}/parse_version.cmake)
parse_version(${CMAKE_CURRENT_LIST_DIR}/../VERSION SIMPLEBLE)
parse_version("${CMAKE_CURRENT_LIST_DIR}/../VERSION" SIMPLEBLE)
112 changes: 72 additions & 40 deletions simpleble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/prelude.cmake)

project(
simpleble
VERSION ${SIMPLEBLE_VERSION}
VERSION "${SIMPLEBLE_VERSION}"
DESCRIPTION "The ultimate fully-fledged cross-platform library for Bluetooth Low Energy (BLE)."
HOMEPAGE_URL "https://github.com/OpenBluetoothToolbox/SimpleBLE"
LANGUAGES CXX
Expand All @@ -17,13 +17,15 @@ include(GenerateExportHeader)
include(GNUInstallDirs)

option(SIMPLEBLE_PLAIN "Use plain version of SimpleBLE" OFF)
option(SIMPLEBLE_INSTALL "Add install rules" ON)

if(NOT TARGET fmt::fmt-header-only)
option(LIBFMT_VENDORIZE "Enable vendorized libfmt" ON)
find_package(fmt REQUIRED)

if(TARGET fmt)
set_target_properties(fmt PROPERTIES EXCLUDE_FROM_ALL TRUE)
add_library(fmt::fmt-header-only ALIAS fmt)
endif()
endif()

Expand All @@ -32,6 +34,26 @@ if(SIMPLEBLE_TEST)
set(SIMPLEBLE_PLAIN ON)
endif()

if(UNIX AND NOT APPLE AND NOT WIN32 AND NOT SIMPLEBLE_PLAIN)
find_package(DBus1)
if(NOT TARGET dbus-1::dbus-1-headers-only)
return()
endif()
endif()

if(0)
if(NOT EXISTS "${CMAKE_BINARY_DIR}/Microsoft.Windows.SDK.CPP/")
file(DOWNLOAD
https://github.com/ossia/sdk/releases/download/sdk30/10.0.22621.0.zip
"${CMAKE_BINARY_DIR}/10.0.22621.0.zip"
)
file(ARCHIVE_EXTRACT
INPUT "${CMAKE_BINARY_DIR}/10.0.22621.0.zip"
DESTINATION "${CMAKE_BINARY_DIR}/win_sdk_headers/"
)
endif()
endif()

set(SIMPLEBLE_PRIVATE_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/include
Expand Down Expand Up @@ -81,7 +103,7 @@ add_library(simpleble::simpleble-c ALIAS simpleble-c)
set_target_properties(simpleble PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
CXX_STANDARD 17
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
POSITION_INDEPENDENT_CODE ON
Expand All @@ -92,12 +114,14 @@ set_target_properties(simpleble PROPERTIES
RELEASE_POSTFIX ""
RELWITHDEBINFO_POSTFIX "-relwithdebinfo"
MINSIZEREL_POSTFIX "-minsizerel"
DEBUG_POSTFIX "-debug")
DEBUG_POSTFIX "-debug"
UNITY_BUILD NO
)

set_target_properties(simpleble-c PROPERTIES
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
CXX_STANDARD 17
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
POSITION_INDEPENDENT_CODE ON
Expand All @@ -109,7 +133,9 @@ set_target_properties(simpleble-c PROPERTIES
RELEASE_POSTFIX ""
RELWITHDEBINFO_POSTFIX "-relwithdebinfo"
MINSIZEREL_POSTFIX "-minsizerel"
DEBUG_POSTFIX "-debug")
DEBUG_POSTFIX "-debug"
UNITY_BUILD NO
)

generate_export_header(
simpleble
Expand Down Expand Up @@ -159,11 +185,9 @@ if(SIMPLEBLE_PLAIN)
target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
elseif(UNIX AND NOT APPLE AND NOT WIN32)
message(STATUS "Linux Host Detected")

find_package(DBus1 REQUIRED)

if(NOT SIMPLEDBUS_LOG_LEVEL)
set(SIMPLEDBUS_LOG_LEVEL "FATAL")
endif()
Expand Down Expand Up @@ -219,7 +243,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
)

target_link_libraries(simpleble PUBLIC pthread)
target_link_libraries(simpleble PRIVATE ${DBus1_LIBRARIES})
target_link_libraries(simpleble PRIVATE dbus-1::dbus-1-headers-only)

target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../simplebluez/include
Expand Down Expand Up @@ -252,7 +276,13 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
list(APPEND PRIVATE_COMPILE_OPTIONS "/WX")
# /W1 -> Use the lowest level of warnings, as there are some unsafe functions that MSVC doesn't like.
# TODO: This should be removed once the warnings are fixed.
list(APPEND PRIVATE_COMPILE_OPTIONS "/W1")
list(APPEND PRIVATE_COMPILE_OPTIONS "/w")
else()
# target_include_directories(simpleble SYSTEM PRIVATE
# $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/win_sdk_headers/cppwinrt>
# $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/win_sdk_headers>
# )
target_link_libraries(simpleble PRIVATE RuntimeObject Shlwapi)
endif()

target_include_directories(simpleble PRIVATE
Expand Down Expand Up @@ -296,36 +326,38 @@ apply_build_options(simpleble-c
"${PRIVATE_LINK_OPTIONS}"
"${PUBLIC_LINK_OPTIONS}")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/simpleble.pc.in
${CMAKE_CURRENT_BINARY_DIR}/simpleble.pc @ONLY)

install(
TARGETS simpleble simpleble-c
EXPORT simpleble-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(
EXPORT simpleble-config
NAMESPACE simpleble::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/simpleble)

install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/simpleble/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble)

install(
DIRECTORY ${PROJECT_BINARY_DIR}/export/simpleble/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble)

install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/simpleble_c/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble_c)

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/simpleble.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
if(SIMPLEBLE_INSTALL)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/simpleble.pc.in
${CMAKE_CURRENT_BINARY_DIR}/simpleble.pc @ONLY)

install(
TARGETS simpleble simpleble-c
EXPORT simpleble-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(
EXPORT simpleble-config
NAMESPACE simpleble::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/simpleble)

install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/simpleble/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble)

install(
DIRECTORY ${PROJECT_BINARY_DIR}/export/simpleble/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble)

install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/simpleble_c/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simpleble_c)

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/simpleble.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()

if(SIMPLEBLE_TEST)
message(STATUS "Building Tests")
Expand Down
4 changes: 2 additions & 2 deletions simpleble/src/backends/windows/AdapterBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ AdapterBase::AdapterBase(std::string device_id)

// Parse manufacturer data
auto manufacturer_data = args.Advertisement().ManufacturerData();
for (auto& item : manufacturer_data) {
for (const auto& item : manufacturer_data) {
uint16_t company_id = item.CompanyId();
ByteArray manufacturer_data_buffer = ibuffer_to_bytearray(item.Data());
data.manufacturer_data[company_id] = manufacturer_data_buffer;
Expand Down Expand Up @@ -122,7 +122,7 @@ AdapterBase::AdapterBase(std::string device_id)

// Parse service uuids
auto service_data = args.Advertisement().ServiceUuids();
for (auto& service_guid : service_data) {
for (const auto& service_guid : service_data) {
std::string service_uuid = guid_to_uuid(service_guid);
data.service_data.emplace(std::make_pair(service_uuid, ByteArray()));
}
Expand Down
5 changes: 2 additions & 3 deletions simpleble/src/backends/windows/PeripheralBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ class PeripheralBase {
gatt_characteristic_t& _fetch_characteristic(const BluetoothUUID& service_uuid,
const BluetoothUUID& characteristic_uuid);

GattDescriptor PeripheralBase::_fetch_descriptor(const BluetoothUUID& service_uuid,
const BluetoothUUID& characteristic_uuid,
const BluetoothUUID& descriptor_uuid);
GattDescriptor _fetch_descriptor(const BluetoothUUID& service_uuid, const BluetoothUUID& characteristic_uuid,
const BluetoothUUID& descriptor_uuid);

void _subscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic,
std::function<void(ByteArray payload)> callback, GattCharacteristicProperties property,
Expand Down
2 changes: 1 addition & 1 deletion simpleble/src/backends/windows/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <sstream>

#include <sdkddkver.h>
#include "../winrt/roapi.h"
#include <roapi.h>

#include "LoggingInternal.h"

Expand Down
4 changes: 4 additions & 0 deletions simplebluez/src/interfaces/Adapter1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Adapter1::Adapter1(std::shared_ptr<SimpleDBus::Connection> conn, std::string pat
: SimpleDBus::Interface(conn, "org.bluez", path, "org.bluez.Adapter1") {}

void Adapter1::StartDiscovery() {
DiscoveryFilter f;
f.Transport = DiscoveryFilter::TransportType::LE;

SetDiscoveryFilter(f);
auto msg = create_method_call("StartDiscovery");
_conn->send_with_reply_and_block(msg);
}
Expand Down
3 changes: 2 additions & 1 deletion simpledbus/include/simpledbus/base/Connection.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <dbus/dbus.h>
#include <mutex>
#include "Library.h"
#include "Message.h"

namespace SimpleDBus {
Expand Down Expand Up @@ -30,6 +30,7 @@ class Connection {
std::string unique_name();

private:
const libdbus& dbus = libdbus::instance();
bool _initialized = false;

::DBusBusType _dbus_bus_type;
Expand Down
Loading