diff --git a/.gitignore b/.gitignore index 33e50ef15..54be86b32 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,4 @@ doc __pycache__/ venv/ *.py[cod] +/_build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a1fdfaec..224a9a685 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,9 +29,10 @@ cmake_minimum_required(VERSION 3.13.1) project(ot-commissioner VERSION 1.0.0) -option(OT_COMM_COVERAGE "Enable coverage reporting" OFF) -option(OT_COMM_TEST "Build tests" ON) -option(OT_COMM_APP "Build the CLI App" ON) +option(OT_COMM_COVERAGE "Enable coverage reporting" OFF) +option(OT_COMM_TEST "Build tests" ON) +option(OT_COMM_APP "Build the CLI App" ON) +option(OT_COMM_USE_VENDORED_LIBS "Build using submodules instead of using find_package" ON) if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) @@ -41,13 +42,37 @@ if(NOT "${CMAKE_CXX_STANDARD}") set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) endif() - set(CMAKE_CXX_EXTENSIONS OFF) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wfatal-errors -Wno-missing-braces") + +if(OT_COMM_USE_VENDORED_LIBS) + # Defines for mbedtls + set(MBEDTLS_USER_CONFIG ${PROJECT_SOURCE_DIR}/third_party/mbedtls/mbedtls_user_config.h) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBEDTLS_USER_CONFIG_FILE='<${MBEDTLS_USER_CONFIG}>'") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBEDTLS_USER_CONFIG_FILE='<${MBEDTLS_USER_CONFIG}>'") + add_subdirectory(third_party EXCLUDE_FROM_ALL) +else() + find_package(fmt REQUIRED) + find_package(nlohmann_json REQUIRED) + find_package(mdns REQUIRED) + find_package(MbedTLS REQUIRED) + find_package(Libevent REQUIRED) + find_package(cose-c REQUIRED) + find_package(cn-cbor REQUIRED) + if (OT_COMM_TEST) + find_package(Catch2 REQUIRED) + endif() +endif() + add_subdirectory(src) if (OT_COMM_TEST) add_subdirectory(tests) endif() add_subdirectory(tools) -add_subdirectory(third_party EXCLUDE_FROM_ALL) + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..cbc406742 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# build with docker: +# docker build -t builder . + +FROM ubuntu:19.10 + +# install build tools and libreadline +RUN apt update -y && apt install -y python3 python3-pip cmake make gcc g++ libreadline-dev +RUN pip3 install conan + +# copy source +COPY . /tmp/. +WORKDIR /tmp/ + +# make sure build dir is there and is clean +RUN mkdir build || true && rm -rf build/* || true + +# install conan dependencies +RUN cd build && conan remote add gocarlos "https://api.bintray.com/conan/gocarlos/public-conan" && conan install .. --build missing + +# build using cmake and find package +RUN cd build && cmake .. -DOT_COMM_USE_VENDORED_LIBS=OFF && cmake --build . -j diff --git a/build.sh b/build.sh new file mode 100755 index 000000000..1177df33b --- /dev/null +++ b/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +rm -rf _build +mkdir _build +cd _build + +conan install .. --build missing +cmake .. -DOT_COMM_USE_VENDORED_LIBS=OFF +cmake --build . -j diff --git a/cmake/FindLibevent.cmake b/cmake/FindLibevent.cmake new file mode 100644 index 000000000..f4aed136b --- /dev/null +++ b/cmake/FindLibevent.cmake @@ -0,0 +1,53 @@ +# * Find Libevent (a cross event library) This module defines +# LIBEVENT_INCLUDE_DIR, where to find Libevent headers LIBEVENT_LIB, Libevent +# libraries Libevent_FOUND, If false, do not try to use libevent + +set(Libevent_EXTRA_PREFIXES /usr/local /opt/local "$ENV{HOME}") +foreach(prefix ${Libevent_EXTRA_PREFIXES}) + list(APPEND Libevent_INCLUDE_PATHS "${prefix}/include") + list(APPEND Libevent_LIB_PATHS "${prefix}/lib") +endforeach() + +find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${Libevent_INCLUDE_PATHS}) +find_library( + LIBEVENT_LIB + NAMES event + PATHS ${Libevent_LIB_PATHS}) +find_library( + LIBEVENT_PTHREAD_LIB + NAMES event_pthreads + PATHS ${Libevent_LIB_PATHS}) + +if(LIBEVENT_LIB + AND LIBEVENT_INCLUDE_DIR + AND LIBEVENT_PTHREAD_LIB) + set(Libevent_FOUND TRUE) + set(LIBEVENT_LIB ${LIBEVENT_LIB} ${LIBEVENT_PTHREAD_LIB}) +else() + set(Libevent_FOUND FALSE) +endif() + +if(Libevent_FOUND) + if(NOT Libevent_FIND_QUIETLY) + message(STATUS "Found libevent: ${LIBEVENT_LIB}") + endif() +else() + if(Libevent_FIND_REQUIRED) + message(FATAL_ERROR "Could NOT find libevent and libevent_pthread.") + endif() + message(STATUS "libevent and libevent_pthread NOT found.") +endif() + +mark_as_advanced(LIBEVENT_LIB LIBEVENT_INCLUDE_DIR) + +add_library(event_pthreads IMPORTED UNKNOWN) +set_target_properties(event_pthreads PROPERTIES IMPORTED_LOCATION + ${LIBEVENT_INCLUDE_DIR}) +set_target_properties(event_pthreads PROPERTIES INTERFACE_INCLUDE_DIRECTORIES + "${LIBEVENT_PTHREAD_LIB}") + +add_library(event_core IMPORTED UNKNOWN) +set_target_properties(event_core PROPERTIES IMPORTED_LOCATION + ${LIBEVENT_INCLUDE_DIR}) +set_target_properties(event_core PROPERTIES INTERFACE_INCLUDE_DIRECTORIES + "${LIBEVENT_LIB}") diff --git a/cmake/FindMbedTLS.cmake b/cmake/FindMbedTLS.cmake new file mode 100644 index 000000000..4b4e9312b --- /dev/null +++ b/cmake/FindMbedTLS.cmake @@ -0,0 +1,23 @@ +find_path(MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h) + +find_library(MBEDTLS_LIBRARY mbedtls) +find_library(MBEDX509_LIBRARY mbedx509) +find_library(MBEDCRYPTO_LIBRARY mbedcrypto) + +set(MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}") + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(MBEDTLS DEFAULT_MSG + MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY) + +mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY) + +add_library(mbedtls + IMPORTED + UNKNOWN) +set_target_properties(mbedtls + PROPERTIES IMPORTED_LOCATION + ${MBEDTLS_LIBRARIES}) +set_target_properties(mbedtls + PROPERTIES INTERFACE_INCLUDE_DIRECTORIES + "${MBEDTLS_INCLUDE_DIRS}") \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 000000000..c19cac7e2 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,21 @@ +# TODO: remove this and next line +# conan remote add gocarlos "https://api.bintray.com/conan/gocarlos/public-conan" + +[requires] +fmt/6.1.2 +libevent/2.1.11 +nlohmann_json/3.7.3 +mbedtls/2.16.3-gpl +mdns/20200331 + +cose-c/20200225@gocarlos/testing +cn-cbor/1.0.0 + +# testing +catch2/2.11.3 + +[generators] +cmake_find_package +cmake_paths + +[options] diff --git a/src/library/multicast_dns.hpp b/src/library/multicast_dns.hpp index 3e7680c6e..0fd6dbb0a 100644 --- a/src/library/multicast_dns.hpp +++ b/src/library/multicast_dns.hpp @@ -37,7 +37,11 @@ #include +#if __has_include("mdns/mdns.h") #include +#else +#include +#endif #include diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 445cbe56d..405bce08c 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -26,15 +26,19 @@ # POSSIBILITY OF SUCH DAMAGE. # -add_subdirectory(Catch2/repo) +if (OT_COMM_TEST) + add_subdirectory(Catch2/repo) +endif() set(coveralls OFF CACHE BOOL "Disbale generating coveralls data") add_subdirectory(cn-cbor/repo) add_subdirectory(COSE-C) add_subdirectory(fmtlib/repo) add_subdirectory(json/repo) - add_subdirectory(libevent) - add_subdirectory(mbedtls) add_subdirectory(mdns/repo) + +if(NOT TARGET mdns::mdns) + add_library(mdns::mdns ALIAS mdns) +endif() diff --git a/third_party/cn-cbor/repo b/third_party/cn-cbor/repo index 821797f94..df0f2e07f 160000 --- a/third_party/cn-cbor/repo +++ b/third_party/cn-cbor/repo @@ -1 +1 @@ -Subproject commit 821797f9421c04dc783561dc465066857839e108 +Subproject commit df0f2e07f13d8c6f07f0fc3ea40dbcd00a04b016 diff --git a/third_party/mdns/repo b/third_party/mdns/repo index 6d5189d04..dfcc7bc60 160000 --- a/third_party/mdns/repo +++ b/third_party/mdns/repo @@ -1 +1 @@ -Subproject commit 6d5189d0414d76345cdb2232a3d01527fb19886e +Subproject commit dfcc7bc60a139cd3efd2019b14cf286f2565e17e