diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000000..ecb873f7a5 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,28 @@ +name: Static analysis + +on: + push: + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + clang-tidy-linux: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: "true" + + - name: Install clang-tidy + run: | + sudo apt-get update -y + sudo apt-get install -y clang-tidy + - name: Build with clang-tidy + run: | + mkdir linuxbuild && cd linuxbuild + cmake -DCLANG_TIDY_ENABLED=ON .. + make all diff --git a/.gitignore b/.gitignore index 03b524fd4a..cf0e12fd84 100644 --- a/.gitignore +++ b/.gitignore @@ -106,6 +106,8 @@ service/resource-directory/client/unittest/obj #vscode setting files .vscode +.clant-tidy + build python/plgd_headers.config diff --git a/CMakeLists.txt b/CMakeLists.txt index 53703cc995..350d0ad682 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,10 @@ cmake_minimum_required (VERSION 3.10) -project(iotivity-lite) +project(iotivity-lite VERSION 2.2.5) -# Patch mbedtls -set(OC_REAPPLY_MBEDTLS_PATCHES ON CACHE BOOL "") -if(OC_REAPPLY_MBEDTLS_PATCHES) - include(mbedtls-patch.cmake) - set(OC_REAPPLY_MBEDTLS_PATCHES OFF CACHE BOOL - "By default, mbedTLS patches are applied upon the first CMake Configure. Set this to ON to reapply the patches on the next configure." - FORCE - ) -endif() - -# Do not build anything except for the library -option(ENABLE_PROGRAMS "Build mbed TLS programs." OFF) -option(ENABLE_TESTING "Build mbed TLS tests." OFF) +include(GNUInstallDirs) # Installation directories for `install` command and pkgconfig file +######## Build configuration options ######## +set(BUILD_EXAMPLE_APPLICATIONS ON CACHE BOOL "Build example applications.") set(OC_DYNAMIC_ALLOCATION_ENABLED ON CACHE BOOL "Enable dynamic memory allocation within the OCF stack and MBedtls.") set(OC_SECURITY_ENABLED ON CACHE BOOL "Enable security.") set(OC_PKI_ENABLED ON CACHE BOOL "Enable PKI security.") @@ -24,213 +14,602 @@ set(OC_IDD_API_ENABLED ON CACHE BOOL "Enable the Introspection Device Data API." set(OC_TCP_ENABLED ON CACHE BOOL "Enable OCF communications over TCP. Necessary for Cloud communications.") set(OC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED OFF CACHE BOOL "Enable observation over oic/res resource.") set(OC_REPRESENTATION_REALLOC_ENCODING_ENABLED OFF CACHE BOOL "Enable realloc during encoding the representation.") +set(OC_COLLECTIONS_IF_CREATE_ENABLED OFF CACHE BOOL "Enable RT factory for collections.") +set(OC_MNT_ENABLED OFF CACHE BOOL "Enable maintenance resource.") +set(OC_SOFTWARE_UPDATE_ENABLED OFF CACHE BOOL "Enable software update resource.") +set(OC_WKCORE_ENABLED OFF CACHE BOOL "Enable well-known core resource.") +set(OC_OSCORE_ENABLED OFF CACHE BOOL "Enable oscore support.") +set(OC_IPV4_ENABLED OFF CACHE BOOL "Enable IPv4 support.") +set(OC_DNS_LOOKUP_IPV6_ENABLED OFF CACHE BOOL "Enable IPv6 DNS lookup.") + +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +set(CLANG_TIDY_ENABLED OFF CACHE BOOL "Enable clang-tidy analysis during compilation.") + +include(tools/clang-tidy.cmake) + +######## Define compiler flags ######## +set(PRIVATE_COMPILE_DEFINITIONS "") +set(PUBLIC_COMPILE_DEFINITIONS "") +if(OC_DYNAMIC_ALLOCATION_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_DYNAMIC_ALLOCATION") +endif() + +if(OC_SECURITY_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_SECURITY") +endif() + +if(OC_PKI_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_PKI") +endif() + +if(OC_DEBUG_ENABLED) + list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_DEBUG") +endif() + +if(OC_CLOUD_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_CLOUD") + set(OC_TCP_ENABLED ON) + set(OC_IPV4_ENABLED ON) +endif() + +if(OC_IDD_API_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_IDD_API") +endif() + +if(OC_TCP_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_TCP") +endif() + +if(OC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED) + list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_DISCOVERY_RESOURCE_OBSERVABLE") +endif() + +if(OC_REPRESENTATION_REALLOC_ENCODING_ENABLED) + list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_REP_ENCODING_REALLOC") +endif() + +if(OC_COLLECTIONS_IF_CREATE_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_COLLECTIONS_IF_CREATE") +endif() + +if(OC_MNT_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_MNT") +endif() -# If an mbedtls platform layer is defined, add it to the mbedtls list of libs -if(TARGET mbedtls-plat) - set(libs ${libs} mbedtls-plat) -endif() - -add_subdirectory(${PROJECT_SOURCE_DIR}/deps/mbedtls) - -target_include_directories(mbedcrypto PUBLIC - ${PROJECT_SOURCE_DIR} - ${PROJECT_SOURCE_DIR}/include - ${PROJECT_SOURCE_DIR}/deps/mbedtls/include -) - -if(TARGET mbedcrypto-plat) - target_link_libraries(mbedcrypto mbedcrypto-plat) -endif() - - -# Iotivity API -add_library(iotivity-api INTERFACE) - -target_sources(iotivity-api INTERFACE - ${PROJECT_SOURCE_DIR}/api/oc_base64.c - ${PROJECT_SOURCE_DIR}/api/oc_blockwise.c - ${PROJECT_SOURCE_DIR}/api/oc_buffer.c - ${PROJECT_SOURCE_DIR}/api/oc_client_api.c - ${PROJECT_SOURCE_DIR}/api/oc_clock.c - ${PROJECT_SOURCE_DIR}/api/oc_collection.c - ${PROJECT_SOURCE_DIR}/api/oc_core_res.c - ${PROJECT_SOURCE_DIR}/api/oc_discovery.c - ${PROJECT_SOURCE_DIR}/api/oc_endpoint.c - ${PROJECT_SOURCE_DIR}/api/oc_enums.c - ${PROJECT_SOURCE_DIR}/api/oc_helpers.c - ${PROJECT_SOURCE_DIR}/api/oc_main.c - ${PROJECT_SOURCE_DIR}/api/oc_mnt.c - ${PROJECT_SOURCE_DIR}/api/oc_network_events.c - ${PROJECT_SOURCE_DIR}/api/oc_rep.c - ${PROJECT_SOURCE_DIR}/api/oc_resource_factory.c - ${PROJECT_SOURCE_DIR}/api/oc_ri.c - ${PROJECT_SOURCE_DIR}/api/oc_server_api.c - ${PROJECT_SOURCE_DIR}/api/oc_session_events.c - ${PROJECT_SOURCE_DIR}/api/oc_swupdate.c - ${PROJECT_SOURCE_DIR}/api/oc_uuid.c - ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_compare.c +if(OC_SOFTWARE_UPDATE_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_SOFTWARE_UPDATE") +endif() + +if(OC_WKCORE_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_WKCORE") +endif() + +if(OC_OSCORE_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_OSCORE") +endif() + +if(OC_IPV4_ENABLED) + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_IPV4") +endif() + +if(OC_DNS_LOOKUP_IPV6_ENABLED) + list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_DNS_LOOKUP_IPV6") +endif() + +######## Gather source files ######## +file(GLOB COMMON_SRC ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_format.c - ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_parse.c - ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_tm.c ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_valid.c + ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_parse.c + ${PROJECT_SOURCE_DIR}/util/*.c ) -if(OC_CLOUD_ENABLED) - target_sources(iotivity-api INTERFACE - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud_manager.c - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud_rd.c - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud_resource.c - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud_store.c - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud.c - ${PROJECT_SOURCE_DIR}/api/cloud/rd_client.c - ${PROJECT_SOURCE_DIR}/api/cloud/oc_cloud_apis.c +if(OC_PKI_ENABLED) + list(APPEND COMMON_SRC + ${PROJECT_SOURCE_DIR}/api/c-timestamp/timestamp_tm.c ) endif() -target_include_directories(iotivity-api INTERFACE - ${PROJECT_SOURCE_DIR}/ - ${PROJECT_SOURCE_DIR}/api - ${PROJECT_SOURCE_DIR}/api/c-timestamp - ${PROJECT_SOURCE_DIR}/include +file(GLOB SERVER_SRC + ${PROJECT_SOURCE_DIR}/messaging/coap/*.c + ${PROJECT_SOURCE_DIR}/api/*.c ) -# Core functions used by Iotivity -add_library(iotivity-common INTERFACE) +set(CLIENT_SRC ${SERVER_SRC}) + +# Detect the platform and pick the right port +if(UNIX) + file(GLOB PORT_SRC port/linux/*.c) + set(PORT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/port/linux) +elseif(WIN32) + file(GLOB PORT_SRC port/windows/*.c) + set(PORT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/port/windows) +else() + message(ERROR "Can build only on Linux and Windows!") +endif() +list(APPEND SERVER_SRC ${PORT_SRC}) +list(APPEND CLIENT_SRC ${PORT_SRC}) + +if(OC_SECURITY_ENABLED) + file(GLOB SECURITY_SRC + ${PROJECT_SOURCE_DIR}/security/oc_*.c + ) + file(GLOB SECURITY_OBT_SRC + ${PROJECT_SOURCE_DIR}/security/oc_obt*.c + ) + list(REMOVE_ITEM SECURITY_SRC + ${SECURITY_OBT_SRC} + ) + list(APPEND SERVER_SRC ${SECURITY_SRC}) + list(APPEND CLIENT_SRC ${SECURITY_SRC}) + if(OC_DYNAMIC_ALLOCATION_ENABLED) + list(APPEND CLIENT_SRC ${SECURITY_OBT_SRC}) + endif() +endif() + +if(OC_CLOUD_ENABLED) + file(GLOB CLOUD_SRC api/cloud/*.c) + set(CLOUD_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/api/cloud) +endif() + +if(UNIX) + file(GLOB PYTHON_SRC python/*.c) +endif() + +######## Define link dependencies ######## +set(PRIVATE_LINK_LIBS "") +include(CheckLibraryExists) +check_library_exists(m ceil "" HAVE_LIB_M) +if(HAVE_LIB_M) + list(APPEND PRIVATE_LINK_LIBS m) +endif(HAVE_LIB_M) -# do not treat warnings as errors on Windows -# block should be defined after the target library if(MSVC) - target_compile_options(mbedtls PRIVATE /W1 /WX-) - target_compile_options(mbedx509 PRIVATE /W1 /WX-) - target_compile_options(mbedcrypto PRIVATE /W1 /WX-) - target_compile_options(iotivity-api INTERFACE /W1 /WX-) -endif() - -target_sources(iotivity-common INTERFACE - # Utilities that are used deep within Iotivity - ${PROJECT_SOURCE_DIR}/util/oc_etimer.c - ${PROJECT_SOURCE_DIR}/util/oc_list.c - ${PROJECT_SOURCE_DIR}/util/oc_memb.c - ${PROJECT_SOURCE_DIR}/util/oc_mem_trace.c - ${PROJECT_SOURCE_DIR}/util/oc_mmem.c - ${PROJECT_SOURCE_DIR}/util/oc_process.c - ${PROJECT_SOURCE_DIR}/util/oc_timer.c - # Security - ${PROJECT_SOURCE_DIR}/security/oc_acl.c - ${PROJECT_SOURCE_DIR}/security/oc_ael.c - ${PROJECT_SOURCE_DIR}/security/oc_audit.c - ${PROJECT_SOURCE_DIR}/security/oc_certs.c - ${PROJECT_SOURCE_DIR}/security/oc_cred.c - ${PROJECT_SOURCE_DIR}/security/oc_csr.c - ${PROJECT_SOURCE_DIR}/security/oc_doxm.c - ${PROJECT_SOURCE_DIR}/security/oc_keypair.c - ${PROJECT_SOURCE_DIR}/security/oc_obt.c - ${PROJECT_SOURCE_DIR}/security/oc_obt_certs.c - ${PROJECT_SOURCE_DIR}/security/oc_obt_otm_cert.c - ${PROJECT_SOURCE_DIR}/security/oc_obt_otm_justworks.c - ${PROJECT_SOURCE_DIR}/security/oc_obt_otm_randompin.c - ${PROJECT_SOURCE_DIR}/security/oc_pki.c - ${PROJECT_SOURCE_DIR}/security/oc_pstat.c - ${PROJECT_SOURCE_DIR}/security/oc_roles.c - ${PROJECT_SOURCE_DIR}/security/oc_sdi.c - ${PROJECT_SOURCE_DIR}/security/oc_sp.c - ${PROJECT_SOURCE_DIR}/security/oc_store.c - ${PROJECT_SOURCE_DIR}/security/oc_svr.c - ${PROJECT_SOURCE_DIR}/security/oc_tls.c - -) - -target_include_directories(iotivity-common INTERFACE - ${PROJECT_SOURCE_DIR}/ - ${PROJECT_SOURCE_DIR}/include - ${PROJECT_SOURCE_DIR}/deps/tinycbor/src - ${PROJECT_SOURCE_DIR}/security -) - -target_link_libraries(iotivity-common INTERFACE iotivity-port mbedtls tinycbor-master) - -# Iotivity's implementation of CoAP -add_library(iotivity-coap INTERFACE) - -target_sources(iotivity-coap INTERFACE - ${PROJECT_SOURCE_DIR}/messaging/coap/coap.c - ${PROJECT_SOURCE_DIR}/messaging/coap/coap_signal.c - ${PROJECT_SOURCE_DIR}/messaging/coap/engine.c - ${PROJECT_SOURCE_DIR}/messaging/coap/observe.c - ${PROJECT_SOURCE_DIR}/messaging/coap/separate.c - ${PROJECT_SOURCE_DIR}/messaging/coap/transactions.c -) - -target_include_directories(iotivity-coap INTERFACE - ${PROJECT_SOURCE_DIR}/messaging/coap/ -) - -target_link_libraries(iotivity-coap INTERFACE iotivity-port) + list(APPEND PRIVATE_LINK_LIBS iphlpapi.lib psapi.lib ws2_32.lib) +elseif(WIN32) + list(APPEND PRIVATE_LINK_LIBS iphlpapi psapi wsock32 ws2_32) +else() + find_package(Threads REQUIRED) + list(APPEND PRIVATE_LINK_LIBS Threads::Threads) +endif() -if(OC_DYNAMIC_ALLOCATION_ENABLED) - target_compile_definitions(mbedcrypto PUBLIC OC_DYNAMIC_ALLOCATION) +######## Object libraries ######## +include(deps/tinycbor.cmake) + +if(OC_SECURITY_ENABLED) + include(deps/mbedtls.cmake) + get_target_property(MBEDTLS_INCLUDE_DIR mbedtls INCLUDE_DIRECTORIES) endif() +# enable clang-tidy for iotivity targets +enable_clang_tidy() + +add_library(common-obj OBJECT ${COMMON_SRC}) +target_compile_definitions(common-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS}) +target_include_directories(common-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) + +add_library(client-obj OBJECT ${CLIENT_SRC}) +target_compile_definitions(client-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS} "OC_CLIENT") +target_include_directories(client-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) if(OC_SECURITY_ENABLED) - target_compile_definitions(mbedcrypto PUBLIC OC_SECURITY) + target_include_directories(client-obj PRIVATE ${MBEDTLS_INCLUDE_DIR}) endif() -if(OC_PKI_ENABLED) - target_compile_definitions(mbedcrypto PUBLIC OC_PKI) +add_library(server-obj OBJECT ${SERVER_SRC}) +target_compile_definitions(server-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS} "OC_SERVER") +target_include_directories(server-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) +if(OC_SECURITY_ENABLED) + target_include_directories(server-obj PRIVATE ${MBEDTLS_INCLUDE_DIR}) endif() -if(OC_DEBUG_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_DEBUG) - target_compile_definitions(mbedcrypto PUBLIC OC_DEBUG) +add_library(client-server-obj OBJECT ${CLIENT_SRC}) +target_compile_definitions(client-server-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS} "OC_CLIENT" "OC_SERVER") +target_include_directories(client-server-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) +if(OC_SECURITY_ENABLED) + target_include_directories(client-server-obj PRIVATE ${MBEDTLS_INCLUDE_DIR}) endif() if(OC_CLOUD_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_CLOUD) + add_library(cloud-obj OBJECT ${CLOUD_SRC}) + target_compile_definitions(cloud-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS}) + target_include_directories(cloud-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) + if(OC_SECURITY_ENABLED) + target_include_directories(cloud-obj PRIVATE ${MBEDTLS_INCLUDE_DIR}) + endif() endif() -if(OC_IDD_API_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_IDD_API) +if(UNIX) + add_library(python-obj OBJECT ${PYTHON_SRC}) + target_compile_definitions(python-obj PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} PUBLIC ${PUBLIC_COMPILE_DEFINITIONS} "OC_CLIENT") + target_include_directories(python-obj PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/port ${PORT_INCLUDE_DIR}) + if(OC_SECURITY_ENABLED) + target_include_directories(python-obj PRIVATE ${MBEDTLS_INCLUDE_DIR}) + endif() endif() -if(OC_TCP_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_TCP) +######## Compose static and shared libraries ######## +# Client +set(client-lib-obj + $ + $ + $ +) +if(OC_SECURITY_ENABLED) + list(APPEND client-lib-obj $) +endif() +add_library(client-static STATIC ${client-lib-obj}) +target_link_libraries(client-static PRIVATE ${PRIVATE_LINK_LIBS}) +target_compile_definitions(client-static PUBLIC + $ + $ +) +target_include_directories(client-static PUBLIC + $ + $ +) +if(OC_SECURITY_ENABLED) + target_include_directories(client-static PUBLIC $) endif() +set_target_properties(client-static PROPERTIES + OUTPUT_NAME "iotivity-lite-client-static" + VERSION ${PROJECT_VERSION} +) -if(OC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_DISCOVERY_RESOURCE_OBSERVABLE) +if(NOT MSVC) + # Since the library symbols are not explicitly exported, no proper DLL and import LIB are generated with MSVC + add_library(client-shared SHARED ${client-lib-obj}) + target_link_libraries(client-shared PRIVATE ${PRIVATE_LINK_LIBS}) + target_compile_definitions(client-shared PUBLIC + $ + $ + ) + target_include_directories(client-shared PUBLIC + $ + $ + ) + if(OC_SECURITY_ENABLED) + target_include_directories(client-shared PUBLIC $) + endif() + set_target_properties(client-shared PROPERTIES + OUTPUT_NAME "iotivity-lite-client" + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) endif() -if(OC_REPRESENTATION_REALLOC_ENCODING_ENABLED) - target_compile_definitions(iotivity-common INTERFACE OC_REP_ENCODING_REALLOC) +# Server +set(server-lib-obj + $ + $ + $ +) +if(OC_CLOUD_ENABLED) + list(APPEND server-lib-obj $) +endif() +if(OC_SECURITY_ENABLED) + list(APPEND server-lib-obj $) +endif() + +add_library(server-static STATIC ${server-lib-obj}) +target_link_libraries(server-static PRIVATE ${PRIVATE_LINK_LIBS}) +target_compile_definitions(server-static PUBLIC + $ + $ +) +target_include_directories(server-static PUBLIC + $ + $ +) +if(OC_SECURITY_ENABLED) + target_include_directories(server-static PUBLIC $) +endif() +if(OC_CLOUD_ENABLED) + target_include_directories(server-static PUBLIC $) +endif() +set_target_properties(server-static PROPERTIES + OUTPUT_NAME "iotivity-lite-server-static" + VERSION ${PROJECT_VERSION} +) + +if(NOT MSVC) + # Since the library symbols are not explicitly exported, no proper DLL and import LIB are generated with MSVC + add_library(server-shared SHARED ${server-lib-obj}) + target_link_libraries(server-shared PRIVATE ${PRIVATE_LINK_LIBS}) + target_compile_definitions(server-shared PUBLIC + $ + $ + ) + target_include_directories(server-shared PUBLIC + $ + $ + ) + if(OC_SECURITY_ENABLED) + target_include_directories(server-shared PUBLIC $) + endif() + if(OC_CLOUD_ENABLED) + target_include_directories(server-shared PUBLIC $) + endif() + set_target_properties(server-shared PROPERTIES + OUTPUT_NAME "iotivity-lite-server" + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) +endif() + +# Client-Server +set(client-server-lib-obj + $ + $ + $ +) +if(OC_CLOUD_ENABLED) + list(APPEND client-server-lib-obj $) +endif() +if(OC_SECURITY_ENABLED) + list(APPEND client-server-lib-obj $) +endif() +add_library(client-server-static STATIC ${client-server-lib-obj}) +target_link_libraries(client-server-static PRIVATE ${PRIVATE_LINK_LIBS}) +target_compile_definitions(client-server-static PUBLIC + $ + $ +) +target_include_directories(client-server-static PUBLIC + $ + $ +) +if(OC_SECURITY_ENABLED) + target_include_directories(client-server-static PUBLIC $) +endif() +if(OC_CLOUD_ENABLED) + target_include_directories(client-server-static PUBLIC $) +endif() +set_target_properties(client-server-static PROPERTIES + OUTPUT_NAME "iotivity-lite-client-server-static" + VERSION ${PROJECT_VERSION} +) + +if(NOT MSVC) + # Since the library symbols are not explicitly exported, no proper DLL and import LIB are generated with MSVC + add_library(client-server-shared SHARED ${client-server-lib-obj}) + target_link_libraries(client-server-shared PRIVATE ${PRIVATE_LINK_LIBS}) + target_compile_definitions(client-server-shared PUBLIC + $ + $ + ) + target_include_directories(client-server-shared PUBLIC + $ + $ + ) + if(OC_SECURITY_ENABLED) + target_include_directories(client-server-shared PUBLIC $) + endif() + if(OC_CLOUD_ENABLED) + target_include_directories(client-server-shared PUBLIC $) + endif() + set_target_properties(client-server-shared PROPERTIES + OUTPUT_NAME "iotivity-lite-client-server" + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) +endif() + +# Python client +if(UNIX) + set(client-python-lib-obj + $ + $ + $ + $ + ) + if(OC_SECURITY_ENABLED) + list(APPEND client-python-lib-obj $) + endif() + add_library(client-python-shared SHARED ${client-python-lib-obj}) + target_link_libraries(client-python-shared PRIVATE ${PRIVATE_LINK_LIBS}) + target_compile_definitions(client-python-shared PUBLIC + $ + $ + ) + target_include_directories(client-python-shared PUBLIC + $ + $ + ) + if(OC_SECURITY_ENABLED) + target_include_directories(client-python-shared PUBLIC $) + endif() + set_target_properties(client-python-shared PROPERTIES + OUTPUT_NAME "iotivity-lite-client-python" + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) endif() -# Client and server versions of Iotivity -add_library(iotivity-client) -target_compile_definitions(iotivity-client PUBLIC OC_CLIENT) -target_link_libraries(iotivity-client PRIVATE - iotivity-coap - iotivity-api +######## Units tests (UNIX only) ######## +include(CTest) +if(BUILD_TESTING AND UNIX) + enable_language(CXX) + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_CXX_EXTENSIONS OFF) + list(APPEND TEST_COMPILE_OPTIONS "-Wall" "-Wextra" "-fpermissive") + list(APPEND TEST_LINK_LIBS client-server-static gtest_main Threads::Threads) + if(CMAKE_COMPILER_IS_GNUCXX) + list(APPEND TEST_COMPILE_OPTIONS "-fprofile-arcs" "-ftest-coverage") + list(APPEND TEST_LINK_LIBS gcov) + endif() + + # Disable clang-tidy for gtest + disable_clang_tidy() + + # Build googletest + add_subdirectory(${PROJECT_SOURCE_DIR}/deps/gtest gtest) + + # Helper macro to build unit test + macro(package_add_test TESTNAME) + add_executable(${TESTNAME} ${ARGN}) + target_compile_options(${TESTNAME} PRIVATE ${TEST_COMPILE_OPTIONS}) + target_compile_features(${TESTNAME} PRIVATE cxx_nullptr) + target_compile_definitions(${TESTNAME} PRIVATE ${PUBLIC_COMPILE_DEFINITIONS} "OC_CLIENT" "OC_SERVER") + target_include_directories(${TESTNAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/deps/gtest/include) + target_include_directories(${TESTNAME} PRIVATE + ${PROJECT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/include + ${PORT_INCLUDE_DIR} + ${PROJECT_SOURCE_DIR}/messaging/coap + ) + if(OC_SECURITY_ENABLED) + target_include_directories(${TESTNAME} PRIVATE + ${PROJECT_SOURCE_DIR}/security + ${PROJECT_SOURCE_DIR}/deps/mbedtls/include + ) + endif() + if(OC_CLOUD_ENABLED) + target_include_directories(${TESTNAME} PRIVATE ${PROJECT_SOURCE_DIR}/api/cloud) + endif() + target_link_libraries(${TESTNAME} PRIVATE ${TEST_LINK_LIBS}) + add_test(NAME ${TESTNAME} COMMAND ${TESTNAME}) + set_target_properties(${TESTNAME} PROPERTIES FOLDER unittests) + endmacro() + + # Unit tests + file(GLOB APITEST_SRC api/unittest/*.cpp) + package_add_test(apitest ${APITEST_SRC}) + + file(GLOB PLATFORMTEST_SRC port/unittest/*.cpp) + file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/storage_test) + package_add_test(platformtest ${PLATFORMTEST_SRC}) + + file(GLOB MESSAGINGTEST_SRC messaging/coap/unittest/*.cpp) + package_add_test(messagingtest ${MESSAGINGTEST_SRC}) + + if(OC_SECURITY_ENABLED) + file(GLOB SECURITYTEST_SRC security/unittest/*.cpp) + package_add_test(securitytest ${SECURITYTEST_SRC}) + endif() + +# Currently disabled because it hangs on TestCloudManager.cloud_manager_start_initialized_f +# if(OC_CLOUD_ENABLED) +# file(GLOB CLOUDTEST_SRC api/cloud/unittest/*.cpp) +# package_add_test(cloudtest ${CLOUDTEST_SRC}) +# endif() + + # reenable clang-tidy for any remaining targets + enable_clang_tidy() +endif() + +######## Generate pkg-config and cmake package files ######## +foreach(cflag IN LISTS PUBLIC_COMPILE_DEFINITIONS) + string(APPEND extra_cflags "-D${cflag} ") +endforeach() + +# Generate pkg-config files +set(prefix ${CMAKE_INSTALL_PREFIX}) +set(exec_prefix "\${prefix}") +set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}") +set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") +set(version ${iotivity-lite_VERSION}) + +set(PKG_CONFIG_FILES + iotivity-lite-client.pc + iotivity-lite-server.pc + iotivity-lite-client-server.pc) +foreach(pkg-config-file IN LISTS PKG_CONFIG_FILES) + configure_file( + "${PROJECT_SOURCE_DIR}/port/linux/${pkg-config-file}.in" + ${pkg-config-file} + @ONLY) +endforeach() + +include(CMakePackageConfigHelpers) +configure_package_config_file(iotivity-liteConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/iotivity-liteConfig.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/iotivity-lite +) +write_basic_package_version_file(iotivity-liteConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion ) -target_link_libraries(iotivity-client PUBLIC iotivity-common) -add_library(iotivity-server) -target_compile_definitions(iotivity-server PUBLIC OC_SERVER) -target_link_libraries(iotivity-server PRIVATE - iotivity-coap - iotivity-api +######## Installation ######## +# Libraries +set(INSTALL_TARGETS + client-static client-shared + server-static server-shared + client-server-static client-server-shared +) +if(MSVC) + # Since the library symbols are not explicitly exported, no proper DLL and import LIB are generated with MSVC + set(INSTALL_TARGETS + client-static + server-static + client-server-static + ) +endif() +install(TARGETS ${INSTALL_TARGETS} + EXPORT iotivity-liteTARGETS + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT bin + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT dev ) -target_link_libraries(iotivity-server PUBLIC iotivity-common) -add_library(iotivity-server-client) -target_compile_definitions(iotivity-server-client PUBLIC OC_SERVER OC_CLIENT) -target_link_libraries(iotivity-server-client PRIVATE - iotivity-coap - iotivity-api +# pkg-config files +foreach(pkg-config-file IN LISTS PKG_CONFIG_FILES) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${pkg-config-file} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT dev) +endforeach() + +# CMake package +if(WIN32 AND NOT CYGWIN) + set(TARGETS_INSTALL_DIR CMake) + + # Add windows registry setting so cmake can find from installed location + set(CMAKE_PACKAGE_REGISTRY_KEY "HKCU\\\\Software\\\\Kitware\\\\CMake\\\\Packages\\\\iotivity-lite") + file(MD5 "${CMAKE_BINARY_DIR}/iotivity-liteConfig.cmake" INSTALL_HASH) + install(CODE "execute_process(COMMAND reg add ${CMAKE_PACKAGE_REGISTRY_KEY} /v ${INSTALL_HASH} /t REG_SZ /d \"${CMAKE_INSTALL_PREFIX}\" /f)" COMPONENT dev) +else() + set(TARGETS_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/iotivity-lite) +endif() +install(EXPORT iotivity-liteTARGETS + FILE + iotivity-liteTargets.cmake + NAMESPACE + iotivity-lite:: + DESTINATION + ${TARGETS_INSTALL_DIR} + COMPONENT dev +) +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/iotivity-liteConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/iotivity-liteConfigVersion.cmake + DESTINATION ${TARGETS_INSTALL_DIR} COMPONENT dev ) -target_link_libraries(iotivity-server-client PUBLIC iotivity-common) +# Header files +install(DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite COMPONENT dev + FILES_MATCHING PATTERN "*.h" +) +install(DIRECTORY ${PROJECT_SOURCE_DIR}/port + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite COMPONENT dev + FILES_MATCHING PATTERN "*.h" +) +install(FILES ${PORT_INCLUDE_DIR}/oc_config.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite COMPONENT dev +) +install(DIRECTORY util + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite COMPONENT dev + FILES_MATCHING PATTERN "*.h" +) +install(DIRECTORY messaging/coap/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite/messaging/coap COMPONENT dev + FILES_MATCHING PATTERN "*.h" + PATTERN "unittest" EXCLUDE +) +######## Code formatting ######## if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) # Add clang-format target add_custom_target(format @@ -239,7 +618,11 @@ if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) ) endif() -add_subdirectory(port) -add_subdirectory(apps) -add_subdirectory(onboarding_tool) -add_subdirectory(deps) +######## Sub-projects ######## +if(UNIX) + add_subdirectory(tests) +endif() +if(BUILD_EXAMPLE_APPLICATIONS) + add_subdirectory(onboarding_tool) + add_subdirectory(apps) +endif() diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index a8bc404790..7c693dfc35 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -3,69 +3,164 @@ project(iotivity-lite-apps) if(UNIX) add_executable(simpleserver ${PROJECT_SOURCE_DIR}/simpleserver.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - target_link_libraries(simpleserver - iotivity-server-client ) + target_link_libraries(simpleserver server-static) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleserver_creds) - add_executable(simpleserver_pki - ${PROJECT_SOURCE_DIR}/simpleserver_pki.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - target_link_libraries(simpleserver_pki - iotivity-server-client - ) - file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleserver_pki_creds) + if(OC_SECURITY_ENABLED AND OC_PKI_ENABLED) + add_executable(simpleserver_pki + ${PROJECT_SOURCE_DIR}/simpleserver_pki.c + ) + target_link_libraries(simpleserver_pki server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleserver_pki_creds) + endif() add_executable(simpleclient ${PROJECT_SOURCE_DIR}/simpleclient.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - target_link_libraries(simpleclient - iotivity-client ) + target_link_libraries(simpleclient client-static) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleclient_creds) - if(EXISTS ${PROJECT_SOURCE_DIR}/device_builder_server.c) - add_executable(device_builder_server - ${PROJECT_SOURCE_DIR}/device_builder_server.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c + add_executable(server + ${PROJECT_SOURCE_DIR}/server_linux.c + ) + target_link_libraries(server server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/server_creds) + + add_executable(client + ${PROJECT_SOURCE_DIR}/client_linux.c + ) + target_link_libraries(client client-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/client_creds) + + add_executable(server_block_linux + ${PROJECT_SOURCE_DIR}/server_block_linux.c + ) + target_link_libraries(server_block_linux server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/server_block_linux_creds) + + add_executable(client_block_linux + ${PROJECT_SOURCE_DIR}/client_block_linux.c + ) + target_link_libraries(client_block_linux client-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/client_block_linux_creds) + + add_executable(server_multithread_linux + ${PROJECT_SOURCE_DIR}/server_multithread_linux.c + ) + target_link_libraries(server_multithread_linux server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/server_multithread_linux_creds) + + add_executable(client_multithread_linux + ${PROJECT_SOURCE_DIR}/client_multithread_linux.c + ) + target_link_libraries(client_multithread_linux client-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/client_multithread_linux_creds) + + add_executable(multi_device_server + ${PROJECT_SOURCE_DIR}/multi_device_server_linux.c + ) + target_link_libraries(multi_device_server server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/multi_device_server_creds) + + add_executable(multi_device_client + ${PROJECT_SOURCE_DIR}/multi_device_client_linux.c + ) + target_link_libraries(multi_device_client client-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/multi_device_client_creds) + + add_executable(secure_mcast_server1 + ${PROJECT_SOURCE_DIR}/secure_mcast_server1.c + ) + target_link_libraries(secure_mcast_server1 server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/secure_mcast_server1_creds) + + add_executable(secure_mcast_server2 + ${PROJECT_SOURCE_DIR}/secure_mcast_server2.c + ) + target_link_libraries(secure_mcast_server2 server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/secure_mcast_server2_creds) + + add_executable(secure_mcast_client + ${PROJECT_SOURCE_DIR}/secure_mcast_client.c + ) + target_link_libraries(secure_mcast_client client-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/secure_mcast_client_creds) + + add_executable(smart_home_server_linux + ${PROJECT_SOURCE_DIR}/smart_home_server_linux.c + ) + target_link_libraries(smart_home_server_linux server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/smart_home_server_linux_creds) + + add_executable(smart_home_server_with_mock_swupdate + ${PROJECT_SOURCE_DIR}/smart_home_server_with_mock_swupdate.cpp + ) + target_link_libraries(smart_home_server_with_mock_swupdate server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/smart_home_server_with_mock_swupdate_creds) + + add_executable(server_rules + ${PROJECT_SOURCE_DIR}/server_rules.c + ) + target_link_libraries(server_rules server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/server_rules_creds) + + add_executable(server_certification_tests + ${PROJECT_SOURCE_DIR}/server_certification_tests.c + ) + target_link_libraries(server_certification_tests client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/server_certification_tests_creds) + + add_executable(client_certification_tests + ${PROJECT_SOURCE_DIR}/client_certification_tests.c + ) + target_link_libraries(client_certification_tests client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/client_certification_tests_creds) + + if(OC_CLOUD_ENABLED) + add_executable(cloud_server + ${PROJECT_SOURCE_DIR}/cloud_server.c ) - target_link_libraries(device_builder_server - iotivity-server-client + target_link_libraries(cloud_server client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/cloud_server_creds) + + add_executable(cloud_client + ${PROJECT_SOURCE_DIR}/cloud_client.c ) - file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/device_builder_server_creds) - endif() - - if(OC_TCP_ENABLED AND OC_CLOUD_ENABLED) + target_link_libraries(cloud_client client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/cloud_client_creds) + add_executable(cloud_proxy ${PROJECT_SOURCE_DIR}/cloud_proxy.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c ) - target_link_libraries(cloud_proxy - iotivity-server-client + target_link_libraries(cloud_proxy client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/cloud_proxy_creds) + + add_executable(cloud_tests + ${PROJECT_SOURCE_DIR}/cloud_certification_tests.c ) - file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleserver_creds) + target_link_libraries(cloud_tests client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/cloud_tests_creds) + endif() + + if(EXISTS ${PROJECT_SOURCE_DIR}/device_builder_server.c) + add_executable(device_builder_server + ${PROJECT_SOURCE_DIR}/device_builder_server.c + ) + target_link_libraries(device_builder_server client-server-static) + file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/device_builder_server_creds) endif() elseif(WIN32) add_executable(simpleserver ${PROJECT_SOURCE_DIR}/simpleserver_windows.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - target_link_libraries(simpleserver - iotivity-server-client ) + target_link_libraries(simpleserver client-server-static) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleserver_creds) add_executable(simpleclient ${PROJECT_SOURCE_DIR}/simpleclient_windows.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - target_link_libraries(simpleclient - iotivity-server-client ) + target_link_libraries(simpleclient client-server-static) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/simpleclient_creds) endif() @@ -75,5 +170,18 @@ add_custom_target(copy-pki-certs ALL ${PROJECT_SOURCE_DIR}/pki_certs ${PROJECT_BINARY_DIR}/pki_certs ) +add_dependencies(client-server-static copy-pki-certs) -add_dependencies(iotivity-port copy-pki-credentials) \ No newline at end of file +if(OC_IDD_API_ENABLED) + # copy introspection files, used by certification applications. + add_custom_target(copy-idd-files ALL + COMMAND ${CMAKE_COMMAND} -E copy + ${PROJECT_SOURCE_DIR}/server_certification_tests_IDD.cbor + ${PROJECT_SOURCE_DIR}/client_certification_tests_IDD.cbor + ${PROJECT_SOURCE_DIR}/server_rules_IDD.cbor + ${PROJECT_SOURCE_DIR}/cloud_proxy_IDD.cbor + ${PROJECT_SOURCE_DIR}/smart_home_server_linux_IDD.cbor + ${PROJECT_BINARY_DIR} + ) + add_dependencies(client-server-static copy-idd-files) +endif() diff --git a/apps/server_certification_tests.c b/apps/server_certification_tests.c index 3f95e152ea..2dc270ebc4 100644 --- a/apps/server_certification_tests.c +++ b/apps/server_certification_tests.c @@ -19,12 +19,12 @@ #include "oc_pki.h" #include "oc_swupdate.h" #include "port/oc_clock.h" -#include "rd_client.h" #include #include #include #ifdef OC_CLOUD +#include "rd_client.h" #include "oc_cloud.h" #endif diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt deleted file mode 100755 index 9a1e3b7b2e..0000000000 --- a/deps/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -project(iotivity-deps) - -add_library(tinycbor-master - ${PROJECT_SOURCE_DIR}/tinycbor/src/cborerrorstrings.c - ${PROJECT_SOURCE_DIR}/tinycbor/src/cborencoder.c - ${PROJECT_SOURCE_DIR}/tinycbor/src/cborencoder_close_container_checked.c - ${PROJECT_SOURCE_DIR}/tinycbor/src/cborparser.c - ${PROJECT_SOURCE_DIR}/tinycbor/src/cborpretty.c -) - -target_include_directories(tinycbor-master PUBLIC - ${PROJECT_SOURCE_DIR}/tinycbor/src -) \ No newline at end of file diff --git a/mbedtls-patch.cmake b/deps/mbedtls-patch.cmake similarity index 100% rename from mbedtls-patch.cmake rename to deps/mbedtls-patch.cmake diff --git a/deps/mbedtls.cmake b/deps/mbedtls.cmake new file mode 100755 index 0000000000..77a3996e06 --- /dev/null +++ b/deps/mbedtls.cmake @@ -0,0 +1,30 @@ +# Patch mbedtls +set(OC_REAPPLY_MBEDTLS_PATCHES ON CACHE BOOL "") +if(OC_REAPPLY_MBEDTLS_PATCHES) + include(${PROJECT_SOURCE_DIR}/deps/mbedtls-patch.cmake) + set(OC_REAPPLY_MBEDTLS_PATCHES OFF CACHE BOOL + "By default, mbedTLS patches are applied upon the first CMake Configure. Set this to ON to reapply the patches on the next configure." + FORCE + ) +endif() + +file(GLOB MBEDTLS_SRC + ${PROJECT_SOURCE_DIR}/deps/mbedtls/library/*.c +) +list(REMOVE_ITEM MBEDTLS_SRC + ${PROJECT_SOURCE_DIR}/deps/mbedtls/library/certs.c + ${PROJECT_SOURCE_DIR}/deps/mbedtls/library/memory_buffer_alloc.c + ${PROJECT_SOURCE_DIR}/deps/mbedtls/library/x509_crl.c +) +add_library(mbedtls OBJECT ${MBEDTLS_SRC}) +target_include_directories(mbedtls PRIVATE + ${PROJECT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/include + ${PORT_INCLUDE_DIR} + ${PROJECT_SOURCE_DIR}/deps/mbedtls/include +) +target_compile_definitions(mbedtls PUBLIC ${PUBLIC_COMPILE_DEFINITIONS} PRIVATE __OC_RANDOM) +# do not treat warnings as errors on Windows +if(MSVC) + target_compile_options(mbedtls PRIVATE /W1 /WX-) +endif() diff --git a/deps/tinycbor.cmake b/deps/tinycbor.cmake new file mode 100755 index 0000000000..6781a78440 --- /dev/null +++ b/deps/tinycbor.cmake @@ -0,0 +1,18 @@ +add_library(tinycbor-master OBJECT + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/cborerrorstrings.c + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/cborencoder.c + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/cborencoder_close_container_checked.c + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/cborparser.c + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/cborpretty.c +) + +target_include_directories(tinycbor-master PUBLIC + ${PROJECT_SOURCE_DIR}/deps/tinycbor/src +) + +target_compile_definitions(tinycbor-master PUBLIC ${PUBLIC_COMPILE_DEFINITIONS}) + +install(DIRECTORY ${PROJECT_SOURCE_DIR}/deps/tinycbor/src/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/iotivity-lite/deps/tinycbor/src COMPONENT dev + FILES_MATCHING PATTERN "*.h" +) diff --git a/iotivity-liteConfig.cmake.in b/iotivity-liteConfig.cmake.in new file mode 100644 index 0000000000..5dcbc52f84 --- /dev/null +++ b/iotivity-liteConfig.cmake.in @@ -0,0 +1,17 @@ +@PACKAGE_INIT@ + +# import our export targets to user of our library +include("${CMAKE_CURRENT_LIST_DIR}/iotivity-liteTargets.cmake") + +# If the CMakeLists.txt puts target dependencies to the public build INTERFACE +# (i.e. by using target_link_libraries(mytarget PUBLIC|INTERFACE Boost)) +# we need to find this dependent target libraries too. +# Example: +# +# include(CMakeFindDependencyMacro) +# find_dependency(Boost 1.55 REQUIRED COMPONENTS regex) +# find_dependency(RapidJSON 1.0 REQUIRED MODULE) +include(CMakeFindDependencyMacro) +find_dependency(Threads 1.0 REQUIRED MODULE) + +check_required_components(iotivity-lite) diff --git a/onboarding_tool/CMakeLists.txt b/onboarding_tool/CMakeLists.txt index bd2a02f5c4..0a267644ca 100644 --- a/onboarding_tool/CMakeLists.txt +++ b/onboarding_tool/CMakeLists.txt @@ -1,22 +1,18 @@ project(onboarding_tool) -if(UNIX) +if(UNIX AND OC_SECURITY_ENABLED AND OC_PKI_ENABLED) add_executable(onboarding_tool ${PROJECT_SOURCE_DIR}/obtmain.c - ${iotivity-lite_SOURCE_DIR}/api/oc_introspection.c - ) - - target_link_libraries(onboarding_tool - iotivity-server-client ) + target_link_libraries(onboarding_tool PRIVATE client-static) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/onboarding_tool_creds) # copy credentials add_custom_target(copy-pki-certs-obt ALL COMMAND ${CMAKE_COMMAND} -E copy_directory - ${iotivity-lite-apps_SOURCE_DIR}/pki_certs + ${PROJECT_SOURCE_DIR}/../apps/pki_certs ${PROJECT_BINARY_DIR}/pki_certs ) - add_dependencies(iotivity-port copy-pki-certs-obt) + add_dependencies(onboarding_tool copy-pki-certs-obt) endif() \ No newline at end of file diff --git a/port/CMakeLists.txt b/port/CMakeLists.txt deleted file mode 100644 index a1329de378..0000000000 --- a/port/CMakeLists.txt +++ /dev/null @@ -1,47 +0,0 @@ -# This CMakeLists can only build the port for Linux & Windows -if(UNIX OR WIN32) - project(iotivity-lite-port) - - # Detect the platform and pick the right port - if(UNIX) - set(PORT_DIR ${PROJECT_SOURCE_DIR}/linux) - elseif(WIN32) - set(PORT_DIR ${PROJECT_SOURCE_DIR}/windows) - endif() - - - add_library(iotivity-port INTERFACE) - - target_include_directories(iotivity-port INTERFACE - ${PORT_DIR} - ${PROJECT_SOURCE_DIR} - ) - - target_include_directories(mbedcrypto PUBLIC - ${PORT_DIR} - ${PROJECT_SOURCE_DIR} - ) - - target_sources(iotivity-port INTERFACE - ${PORT_DIR}/abort.c - ${PORT_DIR}/clock.c - ${PORT_DIR}/ipadapter.c - ${PORT_DIR}/random.c - ${PORT_DIR}/storage.c - ${PORT_DIR}/tcpadapter.c - ) - - if(WIN32) - target_sources(iotivity-port INTERFACE - ${PORT_DIR}/mutex.c - ${PORT_DIR}/network_addresses.c - ) - target_compile_definitions(iotivity-port INTERFACE OC_IPV4) - endif() - - if(UNIX) - target_link_libraries(iotivity-port INTERFACE pthread m) - elseif(WIN32) - target_link_libraries(iotivity-port INTERFACE bcrypt iphlpapi) - endif() -endif() \ No newline at end of file diff --git a/port/windows/ipadapter.c b/port/windows/ipadapter.c index f386b617cb..5d2c627d4e 100644 --- a/port/windows/ipadapter.c +++ b/port/windows/ipadapter.c @@ -359,10 +359,13 @@ update_mcast_socket(SOCKET mcast_sock, int sa_family, ifaddr_t *ifaddr_list) for (ifaddr = ifaddr_list; ifaddr; ifaddr = ifaddr->next) { if (sa_family == AF_INET6 && ifaddr->addr.ss_family == AF_INET6) { ret += add_mcast_sock_to_ipv6_mcast_group(mcast_sock, ifaddr->if_index); - } else if (sa_family == AF_INET && ifaddr->addr.ss_family == AF_INET) { + } +#ifdef OC_IPV4 + if (sa_family == AF_INET && ifaddr->addr.ss_family == AF_INET) { struct sockaddr_in *a = (struct sockaddr_in *)&ifaddr->addr; ret += add_mcast_sock_to_ipv4_mcast_group(mcast_sock, &a->sin_addr); } +#endif } if (!ifaddr_supplied) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000000..769d6d244a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,28 @@ +project(iotivity-lite-tests) + +add_executable(client_init_linux_test + ${PROJECT_SOURCE_DIR}/client_init_linux.c +) +target_link_libraries(client_init_linux_test PRIVATE client-static) + +add_executable(server_init_linux_test + ${PROJECT_SOURCE_DIR}/server_init_linux.c +) +target_link_libraries(server_init_linux_test PRIVATE server-static) + +add_executable(client_get_linux_test + ${PROJECT_SOURCE_DIR}/client_get_linux.c +) +target_link_libraries(client_get_linux_test PRIVATE client-server-static) + +find_package(PythonInterp) +if(PYTHONINTERP_FOUND) + set(test_bins "./client_init_linux_test" "./server_init_linux_test") + # client_get_linux_test hangs + #list(APPEND test_bins "./client_get_linux_test") + add_custom_target(check + COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../tools/check.py --tests="${test_bins}" + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Run tests ${test_bins}" + ) +endif(PYTHONINTERP_FOUND) \ No newline at end of file diff --git a/tools/.clang-tidy.in b/tools/.clang-tidy.in new file mode 100644 index 0000000000..b49cd2d8a0 --- /dev/null +++ b/tools/.clang-tidy.in @@ -0,0 +1,35 @@ +--- +Checks: "clang-diagnostic-*,clang-analyzer-*,bugprone-*" +WarningsAsErrors: "" # set to "*" to treat all warnings as errors +HeaderFilterRegex: "@IOTIVITY_DIR_NAMES_REGEX@" # all directories except deps +AnalyzeTemporaryDtors: false +FormatStyle: none +CheckOptions: + - key: cert-dcl16-c.NewSuffixes + value: "L;LL;LU;LLU" + - key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField + value: "0" + - key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors + value: "1" + - key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic + value: "1" + - key: google-readability-braces-around-statements.ShortStatementLines + value: "1" + - key: google-readability-function-size.StatementThreshold + value: "800" + - key: google-readability-namespace-comments.ShortNamespaceLines + value: "10" + - key: google-readability-namespace-comments.SpacesBeforeComments + value: "2" + - key: modernize-loop-convert.MaxCopySize + value: "16" + - key: modernize-loop-convert.MinConfidence + value: reasonable + - key: modernize-loop-convert.NamingStyle + value: CamelCase + - key: modernize-pass-by-value.IncludeStyle + value: llvm + - key: modernize-replace-auto-ptr.IncludeStyle + value: llvm + - key: modernize-use-nullptr.NullMacros + value: "NULL" diff --git a/tools/clang-tidy.cmake b/tools/clang-tidy.cmake new file mode 100644 index 0000000000..456187913f --- /dev/null +++ b/tools/clang-tidy.cmake @@ -0,0 +1,62 @@ +cmake_minimum_required (VERSION 3.10) + +include_guard(GLOBAL) + +if(UNIX AND CLANG_TIDY_ENABLED) + file(GLOB iotivity_directories LIST_DIRECTORIES true "${PROJECT_SOURCE_DIR}/*") + + set(iotivity_dirnames "") + foreach(dir ${iotivity_directories}) + if (NOT IS_DIRECTORY "${dir}") + continue() + endif() + + get_filename_component(dirname "${dir}" NAME_WE) + if (dirname) + # skip hidden directories + if (dirname MATCHES "^\\.") + continue() + endif() + + # skip third-party libs + if (dirname STREQUAL "deps") + continue() + endif() + + list(APPEND iotivity_dirnames ${dirname}) + endif() + endforeach() + + if(iotivity_dirnames) + get_filename_component(project_source_dirname "${PROJECT_SOURCE_DIR}" NAME_WE) + string(REPLACE ";" "|" iotivity_dirs_regexp "${iotivity_dirnames}") + set(IOTIVITY_DIR_NAMES_REGEX ".*/${project_source_dirname}/(${iotivity_dirs_regexp})/.*") + else() + set(IOTIVITY_DIR_NAMES_REGEX ".*") + endif() + + configure_file( + "${PROJECT_SOURCE_DIR}/tools/.clang-tidy.in" + "${PROJECT_SOURCE_DIR}/.clang-tidy" + @ONLY) +endif() + +# enable clang-tidy before defining targets you want to run analysis on +macro(enable_clang_tidy) + if(UNIX AND CLANG_TIDY_ENABLED) + # use clang-tidy during compilation if its available + find_program(CLANG_TIDY_BIN clang-tidy) + if(CLANG_TIDY_BIN) + set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_BIN}) + set(CMAKE_CXX_CLANG_TIDY ${CMAKE_C_CLANG_TIDY}) + else() + message(STATUS "clang-tidy not installed") + endif() + endif() +endmacro() + +# disable clang-tidy before defining targets you want to skip analysis on +macro(disable_clang_tidy) + set(CMAKE_C_CLANG_TIDY "") + set(CMAKE_CXX_CLANG_TIDY "") +endmacro()