Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
587 stars 739 forks source link

Flutter Plugin build fails due to incompatible argument types #2438

Closed kilbouri closed 1 year ago

kilbouri commented 1 year ago

Development Machine, OS, Compiler (and Other Relevant Toolchain Info)

Windows 10 Machine

Flutter 3.7.0 MSVC 19.34.31937 CMake 3.24.202208181-MSVC_2

SDK Version (Please Give Commit SHA if Manually Compiling)

Release LTS_01_2023_Ref01

Protocol

MQTT

Describe the Bug

Compiling with Flutter's CMake fails. See CMakeLists.txt and logs below:

CMakeLists.txt

# The Flutter tooling requires that developers have a version of Visual Studio
# installed that includes CMake 3.14 or later. You should not increase this
# version, as doing so will cause the plugin to fail to compile for some
# customers of the plugin.
cmake_minimum_required(VERSION 3.14)

# Project-level configuration.
set(PROJECT_NAME "iothub_device")
project(${PROJECT_NAME} LANGUAGES CXX)

# ##### Azure IoT SDK C Setup START #######

# Set Azure IoT SDK C settings
set(use_mqtt ON CACHE BOOL "Set mqtt on" FORCE)
set(skip_samples ON CACHE BOOL "Set slip_samples on" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "Set BUILD_TESTING off" FORCE)

# Add Azure IoT SDK C
add_subdirectory(azure-iot-sdk-c out)

compileAsC99()

# Conditionally use the SDK trusted certs in the samples
if(${use_sample_trusted_cert})
  add_definitions(-DSET_TRUSTED_CERT_IN_SAMPLES)
  include_directories(${PROJECT_SOURCE_DIR}/certs)
  set(iothub_c_files ${iothub_c_files} ${PROJECT_SOURCE_DIR}/certs/certs.c)
endif()

include_directories(.)

# ##### Azure IoT SDK C Setup END #######

# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "iothub_device_plugin")

# Any new source files that you add to the plugin should be added here.
list(APPEND PLUGIN_SOURCES
  "iothub_device_plugin.cpp"
  "iothub_device_plugin.h"
  "iothub_device_manager.cpp"
  "iothub_device_manager.h"
)

# Define the plugin library target. Its name must not be changed (see comment
# on PLUGIN_NAME above).
add_library(${PLUGIN_NAME} SHARED
  "include/iothub_device/iothub_device_plugin_c_api.h"
  "iothub_device_plugin_c_api.cpp"
  ${PLUGIN_SOURCES}
)

# Apply a standard set of build settings that are configured in the
# application-level CMakeLists.txt. This can be removed for plugins that want
# full control over build settings.
apply_standard_settings(${PLUGIN_NAME})

# Symbols are hidden by default to reduce the chance of accidental conflicts
# between plugins. This should not be removed; any symbols that should be
# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro.
set_target_properties(${PLUGIN_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)

# Source include directories and library dependencies. Add any plugin-specific
# dependencies here.
target_include_directories(${PLUGIN_NAME} INTERFACE
  "${CMAKE_CURRENT_SOURCE_DIR}/azure-iot-sdk-c"
  "${CMAKE_CURRENT_SOURCE_DIR}/include"
)
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)

# List of absolute paths to libraries that should be bundled with the plugin.
# This list could contain prebuilt libraries, or libraries created by an
# external build triggered from this build file.
set(iothub_device_bundled_libraries
  ""
  PARENT_SCOPE
)

Error logs (from flutter run -d windows):

CMake Deprecation Warning at flutter/ephemeral/.plugin_symlinks/iothub_device/windows/azure-iot-sdk-c/deps/azure-macro-utils-c/CMakeLists.txt:4 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

CMake Deprecation Warning at flutter/ephemeral/.plugin_symlinks/iothub_device/windows/azure-iot-sdk-c/deps/umock-c/CMakeLists.txt:4 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\consolelogger.c(71,151): error C2220: the following warning is treated as an error [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\consolelogger.c(71,151): warning C4133: 'function': incompatible types - from 'char [260]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\xlogging.c(123,38): error C2220: the following warning is treated as an error [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\xlogging.c(123,38): warning C4133: 'function': incompatible types - from 'char [8]' to 'LPCWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\xlogging.c(123,113): warning C4133: 'function': incompatible types - from 'char [260]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\xlogging.c(126,172): warning C4133: 'function': incompatible types - from 'char [260]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\string_utils.c(126,99): error C2220: the following warning is treated as an error [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\string_utils.c(126,99): warning C4133: 'function': incompatible types - from 'char [255]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\string_utils.c(134,91): warning C4133: 'function': incompatible types - from 'char [255]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(365,45): error C2220: the following warning is treated as an error [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(365,45): warning C4133: 'function': incompatible types - from 'SEC_CHAR *' to 'SEC_WCHAR *' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(668,83): warning C4133: 'function': incompatible types - from 'SEC_CHAR *' to 'SEC_WCHAR *' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(825,29): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 2 has type 'LPTSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(962,87): warning C4133: 'function': incompatible types - from 'SEC_CHAR *' to 'SEC_WCHAR *' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]
C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\adapters\tlsio_schannel.c(1011,29): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 2 has type 'LPTSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]

It is possible this issue is related to #2436, but I don't know enough C++/CMake to be sure.

kilbouri commented 1 year ago

I figured out how to build the library with CMake directly (just the library, not our Flutter app). Builds completely fine (as I expected) once I copied the recommended settings into the provided CMakeLists.txt:

set(use_mqtt ON CACHE BOOL "Set mqtt on" FORCE)
set(skip_samples ON CACHE BOOL "Set slip_samples on" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "Set BUILD_TESTING off" FORCE)

This tells me its an issue with my CMake configuration, but I don't know what. Any help in that department would be much appreciated.

ericwolz commented 1 year ago

C:\digitallab-libraries\iothub_device\example\windows\flutter\ephemeral\.plugin_symlinks\iothub_device\windows\azure-iot-sdk-c\c-utility\src\consolelogger.c(71,151): warning C4133: 'function': incompatible types - from 'char [260]' to 'LPWSTR' [C:\digitallab-libraries\iothub_device\example\build\windows\plugins\iothub_device\out\c-utility\aziotsharedutil.vcxproj]

LPWSTR being either a wide char or char depending on whether UNICODE is defined in your project. Try and undefine UNICODE to use the multibyte(ANSI) version.

kilbouri commented 1 year ago

That fixes the warnings, but leaves me with a couple questions:

  1. What is the cost of disabling Unicode in this case? Does the SDK no longer work with Unicode inputs?
  2. Flutter seems to require Unicode be enabled (makes sense). Is there a way to undefine it just for the SDK?
ericwolz commented 1 year ago

This SDK only supports multi-byte. This is purely controlled by the compiler UNICODE define. You can create a library for the IoT SDK files that a cmake creates a lib and override the UNICODE compiler define, and them import that lib into your Unicode application. there

ericwolz commented 1 year ago

you can remove the unicode directive in the SDK with the following cmake changes

# Add Azure IoT SDK C
remove_definitions(-DUNICODE)
add_subdirectory(azure-iot-sdk-c out)
add_definitions(-DUNICODE)
kilbouri commented 1 year ago

Something is up, I can't seem to remove the definitions. Closing this issue because its unrelated to the SDK at this point. Thank you very much for your help @ericwol-msft !