Azure / azure-sdk-for-c

This repository is for active development of the Azure SDK for Embedded C. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-c.
MIT License
224 stars 120 forks source link

Docs say AZ_PLATFORM_IMPL can be set to "USER", yet the code does not support that #1519

Closed FreddieChopin closed 3 years ago

FreddieChopin commented 3 years ago

Describe the bug The documentation at https://github.com/Azure/azure-sdk-for-c/blob/ecd408e860dd931fd93a78adb7e05a07b36a60df/README.md#cmake-options say that AZ_PLATFORM_IMPL CMake option can be set to "USER" and then you should also provide AZ_USER_PLATFORM_IMPL_NAME with the name of the library which implements the platform code. Yet the code in https://github.com/Azure/azure-sdk-for-c/blob/028dc79a7defbae3b57ca883a3b39f108c3c586e/CMakeLists.txt#L70 and in https://github.com/Azure/azure-sdk-for-c/blob/f38f6ed374dca70c0d7612c4e757edee000e82a9/sdk/src/azure/platform/CMakeLists.txt#L9 does not support that option. If the user selects it, then he/she gets the "noplatform" library.

Without strange CMake trickery it is then NOT possible to override az_platform_clock_msec() and az_platform_sleep_msec() functions provided by the noplatform library.

The ability to easily provide your own platform library WITHOUT having to fork and modify this repo is a very convenient feature. From the repo history I would assume that this feature was not yet fully implemented.

Expected behavior It should be easy to provide your own platform/porting code, without forking this repo and having to keep your own (modified) copy.

I can provide a pull-request for that if this would make it quicker to solve. I do also understand that currently the platform library is actually not used in the "main" code (these two functions seem to be used only be some HTTP internals), but it would be nice to have this feature ready whenever you decide to use them somewhere else too.

RickWinter commented 3 years ago

Community PRs are always welcomed and will be reviewed/accepted when applicable. Please read the contributing.md guide to become familar with Microsoft community contribution guidelines.

vhvb1989 commented 3 years ago

Hello @FreddieChopin

I have created a PR (https://github.com/Azure/azure-sdk-for-c/pull/1524) for this. It needs to be reviewed by the team.

After that PR, CMake should allow you to choose an external platform implementation library to be used to build az_core with it.

Here is a way how you can create a platform implementation outside Azure SDK repo:

Let's use fetch dependency to get az_core.

this is available here: https://github.com/Azure/azure-sdk-for-c/blob/master/cmake-modules/AddAzureSDKforC.cmake

You can replace this line for the AddAzureSDKforC file content or set up the cmake-modules folder with it.

include(AddAzureSDKforC)

create my platform impl lib with the c file

add_library(my_platform STATIC ${CMAKE_CURRENT_LIST_DIR}/implement.c )

az_core must be available. Either installed in the system, consumed from vcpkg or fetched by CMake.

The simplest is the last one, fetching az_core with CMake.

target_link_libraries(my_platform PRIVATE az_core )

make sure that users can consume the project as a library.

add_library (my::platform ALIAS my_platform)

Define the install scripts for how you want to install this lib into the system


And for the implementation, it would be something like the no_platform.c
```c
// headers from az_core 
#include <azure/core/az_platform.h>
#include <azure/core/internal/az_precondition_internal.h>

#include <azure/core/_az_cfg.h>

// Implement methods from az_platform here:

AZ_NODISCARD az_result az_platform_clock_msec(int64_t* out_clock_msec)
{
  _az_PRECONDITION_NOT_NULL(out_clock_msec);
  *out_clock_msec = 0;
  return AZ_ERROR_DEPENDENCY_NOT_PROVIDED;
}

AZ_NODISCARD az_result az_platform_sleep_msec(int32_t milliseconds)
{
  (void)milliseconds;
  return AZ_ERROR_DEPENDENCY_NOT_PROVIDED;
}

Set up your installating script with export so your library can be found calling find_package (see: https://foonathan.net/2016/03/cmake-install/)

Then you can build az_core setting -DAZ_PLATFORM_IMPL=CUSTOM -DAZ_CUSTOM_PLATFORM_IMPL_NAME=my_implementation

As a workaround, while the PR gets approved, you can try/test your implementation by updating the no_platform.c file and building the Azure SDK for C with it.

Also, if you want to skip the set up for exporting the library, you can create your implementation as a subproject of the Azure SDK for C.

FreddieChopin commented 3 years ago

I have created a PR (#1524) for this. It needs to be reviewed by the team.

OK, let's see how it goes! Thx!