From 1aaf1f22df138b03699f550e02595b1458c8f650 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Fri, 22 Jan 2021 16:10:44 -0800 Subject: [PATCH] Sample for fetchContent (#1392) * Sample for fetchContent --- samples/README.md | 10 ++++ .../cmake-fetch-content/CMakeLists.txt | 30 ++++++++++++ .../integration/cmake-fetch-content/LICENSE | 21 ++++++++ .../integration/cmake-fetch-content/README.md | 42 ++++++++++++++++ .../cmake-fetch-content/src/main.cpp | 49 +++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 samples/integration/cmake-fetch-content/CMakeLists.txt create mode 100644 samples/integration/cmake-fetch-content/LICENSE create mode 100644 samples/integration/cmake-fetch-content/README.md create mode 100644 samples/integration/cmake-fetch-content/src/main.cpp diff --git a/samples/README.md b/samples/README.md index 70459b60b..4c8e8c249 100644 --- a/samples/README.md +++ b/samples/README.md @@ -3,6 +3,7 @@ Developers like to learn by looking at code, and so the Azure SDK comes with a myriad of code samples in the form of short code snippets, sample applications, and how-to guides. This document describes where to find all these resources. ## Structure of the Repository + The Azure SDK repository is organized in the following folder structure, with the main sample locations highlighted using **bold** font. `/samples` (this folder)
@@ -17,17 +18,26 @@ The Azure SDK repository is organized in the following folder structure, with th             `/test`
## Getting Started (a.k.a. `Hello World`) Samples + Each package folder contains a package-specific `README.md` file. Most of these `README` files contain `Hello World` code samples illustrating basic usage of the the APIs contained in the package. For example, you can find `Hello World` samples for the `azure-storage-blobs` package [here](https://github.com/Azure/azure-sdk-for-cpp/blob/master/sdk/storage/README.md#code-samples). ## Package Samples and How-To Guides + Each package folder contains a subfolder called `/samples` with additional code samples. These samples can be either short programs contained in `*.c` files, or more complete how-to guides (code samples and some commentary) contained in `*.md` files. You can find shortcuts to main how-to guides in the [**How-To Guides List**](#how-to-guide-list) section below. +## Integration Samples + +Simple applications that illustrate the different approaches to integrate the Azure SDK for C++ to your application or library. Each sample contains a README with a description and explanation. + ## Sample Applications + Sometimes we want to illustrate how several APIs or even packages work together in a context of a more complete program. For these cases, we created sample applications that you can look at, download, compile, and execute. These application samples are located on [https://docs.microsoft.com/samples/](https://docs.microsoft.com/samples/). ## How-To Guide List + This section lists how-to guides for the most commonly used APIs and most common scenarios, i.e. this section does not attempt to be a complete directory of guides contained in this repository. #### General How-To Guides + - How to configure, access, and analyze **logging** information (TODO) diff --git a/samples/integration/cmake-fetch-content/CMakeLists.txt b/samples/integration/cmake-fetch-content/CMakeLists.txt new file mode 100644 index 000000000..b9954e223 --- /dev/null +++ b/samples/integration/cmake-fetch-content/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +# Project set up +cmake_minimum_required(VERSION 3.13) +project(Application-using-storage-blobs LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# +# Fetch content +# +include(FetchContent) +FetchContent_Declare(azuresdkforcpp + # Set the SDK url path and release TAG + GIT_REPOSITORY https://github.com/Azure/azure-sdk-for-cpp.git + GIT_TAG azure-storage-files-datalake_12.0.0-beta.6) +FetchContent_GetProperties(azuresdkforcpp) +if(NOT azuresdkforcpp_POPULATED) + FetchContent_Populate(azuresdkforcpp) + add_subdirectory(${azuresdkforcpp_SOURCE_DIR} ${azuresdkforcpp_BINARY_DIR} EXCLUDE_FROM_ALL) +endif() + +add_executable ( + application + src/main +) + +# Link to Azure SDK +target_link_libraries(application Azure::azure-storage-blobs) diff --git a/samples/integration/cmake-fetch-content/LICENSE b/samples/integration/cmake-fetch-content/LICENSE new file mode 100644 index 000000000..51b6a76e5 --- /dev/null +++ b/samples/integration/cmake-fetch-content/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. All rights reserved. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. diff --git a/samples/integration/cmake-fetch-content/README.md b/samples/integration/cmake-fetch-content/README.md new file mode 100644 index 000000000..2415e48da --- /dev/null +++ b/samples/integration/cmake-fetch-content/README.md @@ -0,0 +1,42 @@ +# Integrating the Azure SDK for C++ into your application using CMake fetch content + +This application shows how to integrate the Azure SDK for C++ in your application. It uses CMake fetch content functionality to clone the Azure SDK for C++ repo into your `build` directory. This approach is useful when using CMake to build your application. + +## Pre-requisites + +Install the [Azure SDK for C++ dependencies](https://github.com/Azure/azure-sdk-for-cpp/blob/master/CONTRIBUTING.md#third-party-dependencies). + +- CMake project (min version 3.13). +- C++ version 14 or greater. + +## Build + +```bash +# +# Building the application. +# Instructions from application root directory. +# + +# Create build directory just the first time. +mkdir build +cd build + +# Generate and build +# This code assumes that the SDK dependencies were installed with VCPKG +cmake -DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake .. +cmake -build . +``` + +## Run application + +Review source code header for `environment variables` that must be set up before running the app. + +```bash +# +# Running the Application +# Instructions from inside the build directory. +# + +# Run binary (.exe on Windows) +./application +``` diff --git a/samples/integration/cmake-fetch-content/src/main.cpp b/samples/integration/cmake-fetch-content/src/main.cpp new file mode 100644 index 000000000..27bcc047a --- /dev/null +++ b/samples/integration/cmake-fetch-content/src/main.cpp @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +/** + * @file + * @brief Application that consumes the Azure SDK for C++. + * + * @remark Set environment variable `STORAGE_CONNECTION_STRING` before running the application. + * + */ + +#include + +#include +#include + +using namespace Azure::Storage::Blobs; + +int main(int argc, char* argv[]) +{ + (void)argc; + (void)argv; + + /**************** Container sdk client ************************/ + /**************** Create container ************************/ + try + { + auto containerClient = BlobContainerClient::CreateFromConnectionString( + std::getenv("STORAGE_CONNECTION_STRING"), "sample"); + + containerClient.CreateIfNotExists(); + + /**************** Container sdk client ************************/ + /**************** list Blobs (one page) ******************/ + auto response = containerClient.ListBlobsSinglePage(); + auto blobListPage = response.ExtractValue(); + for (auto blob : blobListPage.Items) + { + std::cout << blob.Name << std::endl; + } + } + catch (std::exception& e) + { + std::cout << e.what(); + return 1; + } + + return 0; +}