diff --git a/CMakeLists.txt b/CMakeLists.txt index 283159634b..5bff343c62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ option(build_python "builds the Python native iothub_client module" OFF) option(build_javawrapper "builds the native iothub_client library for java C wrapper" OFF) option(dont_use_uploadtoblob "set dont_use_uploadtoblob to ON if the functionality of upload to blob is to be excluded, OFF otherwise. It requires HTTP" OFF) option(no_logging "disable logging" OFF) +option(use_installed_dependencies "set use_installed_dependencies to ON to use installed packages instead of building dependencies from submodules" OFF) option(use_firmware_update "build the Raspberry PI firmware_update sample" OFF) #setting nuget_e2e_tests will only generate e2e tests to run with nuget packages. Install-packages from Package Manager Console in VS before building the projects @@ -77,11 +78,9 @@ endif() if (LINUX) if (CMAKE_C_COMPILER_ID STREQUAL "GNU") - if((NOT "${build_python}" STREQUAL "OFF") OR ("${build_javawrapper}" STREQUAL "ON")) - # now all static libraries use PIC flag for Python shared lib - set(CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}") - set(CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}") - endif() + # now all static libraries use PIC flag for Python shared lib + set(CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}") + set(CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}") endif() endif() @@ -94,14 +93,13 @@ if(NOT "${compileOption_CXX}" STREQUAL "OFF") set(CMAKE_CXX_FLAGS "${compileOption_CXX} ${CMAKE_CXX_FLAGS}") endif() -add_subdirectory(c-utility) -add_subdirectory(uamqp) -add_subdirectory(umqtt) - -enable_testing() +include("dependencies.cmake") +if(NOT ${skip_unittests}) + include("dependencies-test.cmake") +endif() -include_directories(${SHARED_UTIL_INC_FOLDER}/azure_c_shared_utility) +enable_testing() #this project uses several other projects that are build not by these CMakeFiles #this project also targets several OSes @@ -136,66 +134,7 @@ if (NOT DEFINED ARCHITECTURE OR ARCHITECTURE STREQUAL "") endif() message(STATUS "iothub architecture: ${ARCHITECTURE}") -function(linkUAMQP whatExecutableIsBuilding) - include_directories(${UAMQP_INC_FOLDER}) - - if(WIN32) - #windows needs this define - add_definitions(-D_CRT_SECURE_NO_WARNINGS) - add_definitions(-DGB_MEASURE_MEMORY_FOR_THIS -DGB_DEBUG_ALLOC) - - target_link_libraries(${whatExecutableIsBuilding} uamqp aziotsharedutil ws2_32 secur32) - - if(${use_openssl} OR ${use_wsio}) - target_link_libraries(${whatExecutableIsBuilding} $ENV{OpenSSLDir}/lib/ssleay32.lib $ENV{OpenSSLDir}/lib/libeay32.lib) - - file(COPY $ENV{OpenSSLDir}/bin/libeay32.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug) - file(COPY $ENV{OpenSSLDir}/bin/ssleay32.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug) - endif() - else() - target_link_libraries(${whatExecutableIsBuilding} uamqp aziotsharedutil ssl crypto) - endif() - - if(${use_wsio}) - target_link_libraries(${whatExecutableIsBuilding} websockets) - endif() -endfunction(linkUAMQP) - -function(includeMqtt) - include_directories(${MQTT_INC_FOLDER}) -endfunction(includeMqtt) - -function(linkMqttLibrary whatExecutableIsBuilding) - includeMqtt() - target_link_libraries(${whatExecutableIsBuilding} umqtt) -endfunction(linkMqttLibrary) - -function(includeHttp) -endfunction(includeHttp) - -function(linkHttp whatExecutableIsBuilding) - includeHttp() - if(WIN32) - if(WINCE) - target_link_libraries(${whatExecutableIsBuilding} crypt32.lib) - target_link_libraries(${whatExecutableIsBuilding} ws2.lib) - else() - target_link_libraries(${whatExecutableIsBuilding} winhttp.lib) - endif() - else() - target_link_libraries(${whatExecutableIsBuilding} curl) - endif() -endfunction(linkHttp) - -function(linkWebSockets whatExecutableIsBuilding) - if(${use_wsio}) - target_link_libraries(${whatExecutableIsBuilding} websockets) - endif() -endfunction(linkWebSockets) - -function(linkSharedUtil whatIsBuilding) - target_link_libraries(${whatIsBuilding} aziotsharedutil) -endfunction(linkSharedUtil) +include("configs/azure_iot_sdksFunctions.cmake") macro(compileAsC99) if (CMAKE_VERSION VERSION_LESS "3.1") @@ -247,3 +186,37 @@ endif() if("${build_javawrapper}" STREQUAL "ON") add_subdirectory(../javawrapper/device/iothub_client_javawrapper javawrapper) endif() + + +if(${use_installed_dependencies}) + #Set CMAKE_INSTALL_LIBDIR if not defined + include(GNUInstallDirs) + + #Install azure_iot_sdks + set(package_location "cmake") + + include(CMakePackageConfigHelpers) + + configure_file("configs/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" + COPYONLY + ) + + install(EXPORT azure_iot_sdksTargets + FILE + "${PROJECT_NAME}Targets.cmake" + DESTINATION + ${package_location} + ) + + install( + FILES + "configs/${PROJECT_NAME}Config.cmake" + "configs/${PROJECT_NAME}Functions.cmake" + DESTINATION + ${package_location} + ) + +else() + message(WARNING "This package may only be installed when 'use_installed_dependencies' is ON") +endif() \ No newline at end of file diff --git a/configs/azure_iot_sdksConfig.cmake b/configs/azure_iot_sdksConfig.cmake new file mode 100644 index 0000000000..b9c62e8d0b --- /dev/null +++ b/configs/azure_iot_sdksConfig.cmake @@ -0,0 +1,10 @@ +#Copyright (c) Microsoft. All rights reserved. +#Licensed under the MIT license. See LICENSE file in the project root for full license information. + +include("${CMAKE_CURRENT_LIST_DIR}/azure_iot_sdksTargets.cmake") + +get_target_property(IOTHUB_CLIENT_INCLUDES iothub_client INTERFACE_INCLUDE_DIRECTORIES) + +set(IOTHUB_CLIENT_INCLUDES ${IOTHUB_CLIENT_INCLUDES} CACHE INTERNAL "") + +include("${CMAKE_CURRENT_LIST_DIR}/azure_iot_sdksFunctions.cmake") \ No newline at end of file diff --git a/configs/azure_iot_sdksFunctions.cmake b/configs/azure_iot_sdksFunctions.cmake new file mode 100644 index 0000000000..29264f4418 --- /dev/null +++ b/configs/azure_iot_sdksFunctions.cmake @@ -0,0 +1,63 @@ +#Copyright (c) Microsoft. All rights reserved. +#Licensed under the MIT license. See LICENSE file in the project root for full license information. + +function(linkUAMQP whatExecutableIsBuilding) + include_directories(${UAMQP_INC_FOLDER}) + + if(WIN32) + #windows needs this define + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_definitions(-DGB_MEASURE_MEMORY_FOR_THIS -DGB_DEBUG_ALLOC) + + target_link_libraries(${whatExecutableIsBuilding} uamqp aziotsharedutil ws2_32 secur32) + + if($ OR $) + target_link_libraries(${whatExecutableIsBuilding} $ENV{OpenSSLDir}/lib/ssleay32.lib $ENV{OpenSSLDir}/lib/libeay32.lib) + + file(COPY $ENV{OpenSSLDir}/bin/libeay32.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug) + file(COPY $ENV{OpenSSLDir}/bin/ssleay32.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug) + endif() + else() + target_link_libraries(${whatExecutableIsBuilding} uamqp aziotsharedutil ssl crypto) + endif() + + if($) + target_link_libraries(${whatExecutableIsBuilding} websockets) + endif() +endfunction(linkUAMQP) + +function(includeMqtt) + include_directories(${MQTT_INC_FOLDER}) +endfunction(includeMqtt) + +function(linkMqttLibrary whatExecutableIsBuilding) + includeMqtt() + target_link_libraries(${whatExecutableIsBuilding} umqtt) +endfunction(linkMqttLibrary) + +function(includeHttp) +endfunction(includeHttp) + +function(linkHttp whatExecutableIsBuilding) + includeHttp() + if(WIN32) + if(WINCE) + target_link_libraries(${whatExecutableIsBuilding} crypt32.lib) + target_link_libraries(${whatExecutableIsBuilding} ws2.lib) + else() + target_link_libraries(${whatExecutableIsBuilding} winhttp.lib) + endif() + else() + target_link_libraries(${whatExecutableIsBuilding} curl) + endif() +endfunction(linkHttp) + +function(linkWebSockets whatExecutableIsBuilding) + if($) + target_link_libraries(${whatExecutableIsBuilding} websockets) + endif() +endfunction(linkWebSockets) + +function(linkSharedUtil whatIsBuilding) + target_link_libraries(${whatIsBuilding} aziotsharedutil) +endfunction(linkSharedUtil) \ No newline at end of file diff --git a/dependencies-test.cmake b/dependencies-test.cmake new file mode 100644 index 0000000000..338ea1afcb --- /dev/null +++ b/dependencies-test.cmake @@ -0,0 +1,14 @@ +#Copyright (c) Microsoft. All rights reserved. +#Licensed under the MIT license. See LICENSE file in the project root for full license information. + +if(${use_installed_dependencies}) + #These need to be set for the functions included by c-utility + set(SHARED_UTIL_SRC_FOLDER "${CMAKE_CURRENT_LIST_DIR}/c-utility/src" CACHE INTERNAL "") + set(SHARED_UTIL_ADAPTER_FOLDER "${CMAKE_CURRENT_LIST_DIR}/c-utility/adapters") + set(SHARED_UTIL_FOLDER "${CMAKE_CURRENT_LIST_DIR}/c-utility") + set_platform_files("${CMAKE_CURRENT_LIST_DIR}/c-utility") + if(NOT umock_c_FOUND) + find_package(umock_c REQUIRED CONFIG) + include_directories(${UMOCK_C_INCLUDES}) + endif() +endif() diff --git a/dependencies.cmake b/dependencies.cmake new file mode 100644 index 0000000000..a257eb8772 --- /dev/null +++ b/dependencies.cmake @@ -0,0 +1,26 @@ +#Copyright (c) Microsoft. All rights reserved. +#Licensed under the MIT license. See LICENSE file in the project root for full license information. + +if(${use_installed_dependencies}) + if(NOT azure_c_shared_utility_FOUND) + find_package(azure_c_shared_utility REQUIRED CONFIG) + endif() + if(NOT uamqp_FOUND) + find_package(uamqp REQUIRED CONFIG) + endif() + if(NOT umqtt_FOUND) + find_package(umqtt REQUIRED CONFIG) + endif() + if(${use_wsio}) + if(NOT libwebsockets_FOUND) + find_package(libwebsockets REQUIRED CONFIG) + endif() + endif() + + include_directories(${AZURE_C_SHARED_UTILITY_INCLUDES}/azure_c_shared_utility) +else() + add_subdirectory(c-utility) + add_subdirectory(uamqp) + add_subdirectory(umqtt) + include_directories(${SHARED_UTIL_INC_FOLDER}/azure_c_shared_utility) +endif() \ No newline at end of file diff --git a/iothub_client/CMakeLists.txt b/iothub_client/CMakeLists.txt index aeb6603ab7..891fb2f12d 100644 --- a/iothub_client/CMakeLists.txt +++ b/iothub_client/CMakeLists.txt @@ -171,6 +171,7 @@ if(NOT ${dont_use_uploadtoblob}) include_directories(../parson) endif() +include_directories(${AZURE_C_SHARED_UTILITY_INCLUDES}) include_directories(${SHARED_UTIL_INC_FOLDER}) if (WINCE) @@ -207,6 +208,7 @@ if(${use_http}) endif() if(${use_amqp}) + include_directories(${UAMQP_INCLUDES}) include_directories(${IOTHUB_CLIENT_AMQP_TRANSPORT_INC_FOLDER} ${UAMQP_INC_FOLDER}) add_library(iothub_client_amqp_transport ${iothub_client_amqp_transport_c_files} @@ -221,6 +223,7 @@ if(${use_amqp}) endif() if(${use_mqtt}) + include_directories(${UMQTT_INCLUDES}) include_directories(${IOTHUB_CLIENT_MQTT_TRANSPORT_INC_FOLDER} ${MQTT_INC_FOLDER}) add_library(iothub_client_mqtt_transport ${iothub_client_mqtt_transport_c_files} @@ -283,18 +286,28 @@ endif() endif() if(NOT IN_OPENWRT) -# Disable tests for OpenWRT -if(NOT ${skip_unittests}) -add_subdirectory(tests) -endif() + # Disable tests for OpenWRT + if(NOT ${skip_unittests}) + add_subdirectory(tests) + endif() endif() -if(WIN32) +if(${use_installed_dependencies}) + #Set CMAKE_INSTALL_LIBDIR if not defined + include(GNUInstallDirs) + + if(NOT DEFINED CMAKE_INSTALL_LIBDIR) + set(CMAKE_INSTALL_LIBDIR "lib") + endif() + + install(TARGETS ${iothub_client_libs} EXPORT azure_iot_sdksTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/../bin + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot + ) + install(FILES ${iothub_client_h_install_files} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot) + else() - install (TARGETS - ${iothub_client_libs} - DESTINATION lib) - install (FILES - ${iothub_client_h_install_files} - DESTINATION include/azureiot) -endif (WIN32) + message(WARNING "This package may only be installed when 'use_installed_dependencies' is ON") +endif() \ No newline at end of file diff --git a/iothub_service_client/CMakeLists.txt b/iothub_service_client/CMakeLists.txt index bb5bab39d8..6f5b04e509 100644 --- a/iothub_service_client/CMakeLists.txt +++ b/iothub_service_client/CMakeLists.txt @@ -42,6 +42,7 @@ endif() include_directories(${SHARED_UTIL_INC_FOLDER}) include_directories(${UAMQP_INC_FOLDER}) +include_directories(${UAMQP_INCLUDES}) set(IOTHUB_SERVICE_CLIENT_INC_FOLDER ${CMAKE_CURRENT_LIST_DIR}/inc CACHE INTERNAL "This is the include folder for iothub_service_client" FORCE) @@ -68,7 +69,28 @@ if (NOT ${ARCHITECTURE} STREQUAL "ARM") endif() if(NOT IN_OPENWRT) -# Disable tests for OpenWRT -add_subdirectory(tests) + # Disable tests for OpenWRT + if(NOT ${skip_unittests}) + add_subdirectory(tests) + endif() endif() +if(${use_installed_dependencies}) + #Set CMAKE_INSTALL_LIBDIR if not defined + include(GNUInstallDirs) + + if(NOT DEFINED CMAKE_INSTALL_LIBDIR) + set(CMAKE_INSTALL_LIBDIR "lib") + endif() + + install(TARGETS iothub_service_client EXPORT azure_iot_sdksTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/../bin + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot + ) + install(FILES ${iothub_service_client_h_files} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot) + +else() + message(WARNING "This package may only be installed when 'use_installed_dependencies' is ON") +endif() \ No newline at end of file diff --git a/readme.md b/readme.md index 2c59abfaaf..747393acca 100644 --- a/readme.md +++ b/readme.md @@ -96,3 +96,44 @@ Contains tools that are currently used in testing the client libraries: Mocking ### /tools Miscellaneous tools: compilembed, mbed_build, traceabilitytool (checks spec requirements vs code implementation). + +## Installation and Use +- Clone azure-iot-sdks by: +``` +git clone --recursive https://github.com/Azure/azure-iot-sdks.git +``` +- Create a folder build under azure-iot-sdks +- Switch to the build folder and run + cmake .. + +Optionally, you may choose to install azure-iot-sdks on your machine: + +1. Switch to the *cmake* folder and run + ``` + cmake -Duse_installed_dependencies=ON ../ + ``` + ``` + cmake --build . --target install + ``` + + or install using the follow commands for each platform: + + On Linux: + ``` + sudo make install + ``` + + On Windows: + ``` + msbuild /m INSTALL.vcxproj + ``` + +2. Use it in your project (if installed) + ``` + find_package(azure_iot_sdks REQUIRED CONFIG) + target_link_library(yourlib iothub_client iothub_service_client serializer) + ``` + +_This requires that azure-c-shared-utility, azure-uamqp-c, and azure-umqtt-c are installed (through CMake) on your machine._ + +_If running tests, this requires that umock-c, azure-ctest, and azure-c-testrunnerswitcher are installed (through CMake) on your machine._ diff --git a/serializer/CMakeLists.txt b/serializer/CMakeLists.txt index ad4e5831f1..c65a3160e3 100644 --- a/serializer/CMakeLists.txt +++ b/serializer/CMakeLists.txt @@ -59,6 +59,7 @@ set(SERIALIZER_INC_FOLDER ${CMAKE_CURRENT_LIST_DIR}/inc CACHE INTERNAL "this is include_directories(../parson) include_directories(${SERIALIZER_INC_FOLDER} ${SHARED_UTIL_INC_FOLDER}) +include_directories(${AZURE_C_SHARED_UTILITY_INCLUDES}) IF(WIN32) #windows needs this define @@ -80,12 +81,28 @@ else() endif() if(NOT IN_OPENWRT) -# Disable tests and samples for OpenWRT -add_subdirectory(tests) + # Disable tests and samples for OpenWRT + if(NOT ${skip_unittests}) + add_subdirectory(tests) + endif() endif() -if(WIN32) +if(${use_installed_dependencies}) + include(GNUInstallDirs) + + #Set CMAKE_INSTALL_LIBDIR if not defined + if(NOT DEFINED CMAKE_INSTALL_LIBDIR) + set(CMAKE_INSTALL_LIBDIR "lib") + endif() + + install(TARGETS serializer EXPORT azure_iot_sdksTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/../bin + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot + ) + install(FILES ${serializer_h_files} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/azureiot) + else() - install (TARGETS serializer DESTINATION lib) - install (FILES ${serializer_h_files} DESTINATION include/azureiot) -endif (WIN32) + message(WARNING "This package may only be installed when 'use_installed_dependencies' is ON") +endif() \ No newline at end of file