Prevent compile errors on OSX (#198)

build CHANGE prevent compile errors on OSX

* Prevent compile errors on OSX

clangtidy cmake files too
fix possible buffer overflow too

* Fix travis build error

The imported include path setting is only later needed!

* Fix libssh version detection

Cmake variable is now LIBSSH_VERSION!

* python: do not put generated files into the source tree

Untracked content shows up in git submodules, causing stuff to be marked
"dirty" by various git-level tools.

Tested by temporarily putting the resulting target within `ALL`. The
result is the same as without this change (and it remains unusable with
no generated index, at least when building out-of-tree, BTW).

* apply review  notes

* Prevent compile errors on OSX

clangtidy cmake files too
fix possible buffer overflow too

* Fix travis build error

The imported include path setting is only later needed!

* Fix libssh version detection

Cmake variable is now LIBSSH_VERSION!

* apply review  notes

Co-authored-by: Jan Kundrát <jkt@flaska.net>
diff --git a/.travis.yml b/.travis.yml
index d0450c2..31ebc60 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -27,7 +27,7 @@
   - if [ "$TRAVIS_OS_NAME" = "osx" ]; then bash .travis-deps-osx.sh; fi
 
 script:
-  - if [ "$TRAVIS_OS_NAME" = "osx" ]; then export OPENSSLFLAGS="-DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib -DOPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include"; fi
+  - if [ "$TRAVIS_OS_NAME" = "osx" ]; then export OPENSSLFLAGS="-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl"; fi
   - cd $TRAVIS_BUILD_DIR && mkdir build_none && cd build_none ; cmake -DENABLE_TLS=OFF -DENABLE_SSH=OFF -DENABLE_DNSSEC=OFF .. && make -j2 && ctest --output-on-failure
   - cd $TRAVIS_BUILD_DIR && mkdir build_tls && cd build_tls ; cmake $OPENSSLFLAGS -DENABLE_TLS=ON -DENABLE_SSH=OFF -DENABLE_DNSSEC=OFF .. && make -j2 && ctest --output-on-failure
   - cd $TRAVIS_BUILD_DIR && mkdir build_ssh && cd build_ssh ; cmake $OPENSSLFLAGS -DENABLE_TLS=OFF -DENABLE_SSH=ON -DENABLE_DNSSEC=OFF .. && make -j2 && ctest --output-on-failure
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b0f5334..dffaae5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,15 +1,18 @@
 cmake_minimum_required(VERSION 2.6)
-project(libnetconf2 C)
+
+project(libnetconf2 LANGUAGES C)
+
 include(GNUInstallDirs)
 include(CheckFunctionExists)
 include(CheckCSourceCompiles)
 include(CheckIncludeFile)
+
 if(POLICY CMP0075)
     cmake_policy(SET CMP0075 NEW)
 endif()
 
 # include custom Modules
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
 
 set(LIBNETCONF2_DESCRIPTION "NETCONF server and client library in C.")
 
@@ -26,13 +29,16 @@
 
 # set default build type if not specified by user
 if(NOT CMAKE_BUILD_TYPE)
-    set(CMAKE_BUILD_TYPE debug)
+    set(CMAKE_BUILD_TYPE Debug)
 endif()
 
-set(CMAKE_C_FLAGS         "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden -std=gnu11")
-set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_CXX_EXTENSIONS ON)
+add_compile_options(-Wall -Wextra -fvisibility=hidden)
+#TODO add_compile_options(-Wpedantic)
+
+
 set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
-set(CMAKE_C_FLAGS_DEBUG   "-g -O0")
 
 # Version of the project
 # Generic version of not only the library. Major version is reserved for really big changes of the project,
@@ -68,38 +74,48 @@
     set(ENABLE_DNSSEC OFF)
 endif()
 
-# package options
-find_program (DEB_BUILDER NAMES debuild)
-find_program (RPM_BUILDER NAMES rpmbuild)
+if(ENABLE_SSH)
+    find_library(LIBCRYPT crypt)
+    if(LIBCRYPT STREQUAL LIBCRYPT-NOTFOUND)
+        message(WARNING "LIBCRYPT not found! SSH, and TLS support disabled.")
+        set(ENABLE_SSH OFF)
+        set(ENABLE_TLS OFF)
+    endif()
+endif()
 
-if (NOT DEFINED ENV{TRAVIS_BRANCH})
+# package options
+find_program(DEB_BUILDER NAMES debuild)
+find_program(RPM_BUILDER NAMES rpmbuild)
+
+if(NOT DEFINED ENV{TRAVIS_BRANCH})
     execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
                     OUTPUT_VARIABLE GIT_BRANCH
                     OUTPUT_STRIP_TRAILING_WHITESPACE
                     ERROR_QUIET
-                   )
-    if (NOT GIT_BRANCH)
+    )
+    if(NOT GIT_BRANCH)
         set(ENV{TRAVIS_BRANCH} "master")
     else()
-        if (GIT_BRANCH MATCHES "master|devel")
+        if(GIT_BRANCH MATCHES "master|devel")
             set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
         else()
             set(ENV{TRAVIS_BRANCH} "master")
         endif()
     endif()
-    set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
+
+    set(GIT_BRANCH $ENV{TRAVIS_BRANCH}) # NOTE: used for configure_file too
 endif()
 
-if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
+if(GIT_BRANCH STREQUAL master)
     set(PACKAGE_NAME "libnetconf2")
     set(BRANCH "master")
     set(BUILD_TYPE "Package")
     set(CONFLICT_PACKAGE_NAME "libnetconf2-experimental")
     set(COMPAT_PACKAGES "")
-else ()
+else()
     set(PACKAGE_NAME "libnetconf2-experimental")
     set(BRANCH "devel")
-    set(BUILD_TYPE "debug")
+    set(BUILD_TYPE "Debug")
     set(CONFLICT_PACKAGE_NAME "libnetconf2")
     set(COMPAT_PACKAGES "-experimental")
 endif()
@@ -109,9 +125,9 @@
 configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
 configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
 
-if (NOT DEB_BUILDER)
+if(NOT DEB_BUILDER)
     message(WARNING "Missing tools (devscripts, debhelper package) for building deb package.\nYou won't be able to generate deb package from source code.\nCompiling libnetconf2 should still works fine.")
-else ()
+else()
     # target for local build deb package
     add_custom_target(build-deb
                       WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
@@ -120,9 +136,9 @@
     configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
 endif()
 
-if (NOT RPM_BUILDER)
+if(NOT RPM_BUILDER)
     message(WARNING "Missing tools (rpm package) for building rpm package. \nYou won't be able to generate rpm package from source code.\nCompiling libnetconf2 should still work fine.")
-else ()
+else()
     # target for local build rpm package
     string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
     add_custom_target(build-rpm
@@ -146,20 +162,21 @@
     src/time.c)
 
 if(ENABLE_SSH)
-    set(libsrc ${libsrc}
+    list(APPEND libsrc
         src/session_client_ssh.c
         src/session_server_ssh.c)
     set(SSH_MACRO "#ifndef NC_ENABLED_SSH\n#define NC_ENABLED_SSH\n#endif")
 endif()
 
 if(ENABLE_TLS)
-    set(libsrc ${libsrc}
+    list(APPEND libsrc
         src/session_client_tls.c
         src/session_server_tls.c)
     set(TLS_MACRO "#ifndef NC_ENABLED_TLS\n#define NC_ENABLED_TLS\n#endif")
 endif()
 
 set(headers
+    ${PROJECT_BINARY_DIR}/src/config.h
     src/log.h
     src/netconf.h
     src/session.h
@@ -170,11 +187,11 @@
     src/session_server.h
     src/session_server_ch.h)
 
-# libnetconf2 target
-add_library(netconf2 SHARED ${libsrc})
+# netconf2 target
+add_library(netconf2 SHARED ${libsrc} ${headers})
 set_target_properties(netconf2 PROPERTIES VERSION ${LIBNETCONF2_VERSION} SOVERSION ${LIBNETCONF2_SOVERSION_FULL})
 
-if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
+if((CMAKE_BUILD_TYPE STREQUAL Debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
     option(ENABLE_BUILD_TESTS "Build tests" ON)
     option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
 else()
@@ -196,22 +213,28 @@
 # dependencies - openssl
 if(ENABLE_TLS OR ENABLE_DNSSEC OR ENABLE_SSH)
     find_package(OpenSSL REQUIRED)
-    if (ENABLE_TLS)
+    if(ENABLE_TLS)
+        message(STATUS "OPENSSL found, required for TLS")
         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_TLS")
     endif()
+
+    #TODO target_link_libraries(netconf2 PUBLIC OpenSSL::SSL OpenSSL::Crypto)
     target_link_libraries(netconf2 ${OPENSSL_LIBRARIES})
     include_directories(${OPENSSL_INCLUDE_DIR})
 endif()
 
 # dependencies - libssh
 if(ENABLE_SSH)
+    option(LIBSSH_FIND_VERSION "check version too" ON)
     find_package(LibSSH 0.7.0 REQUIRED)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_SSH")
-    message(STATUS "LibSSH version ${LibSSH_VERSION} found")
-    if(LibSSH_VERSION VERSION_EQUAL 0.9.3)
-        message(FATAL_ERROR "LibSSH 0.9.3 includes regression bugs and libnetconf2 will NOT work properly, try to use an older version")
+    message(STATUS "LibSSH version ${LIBSSH_VERSION} found")
+
+    if(LIBSSH_VERSION VERSION_EQUAL 0.9.3)
+        message(FATAL_ERROR "LIBSSH 0.9.3 includes regression bugs and libnetconf2 will NOT work properly, try to use an older version")
     endif()
-    if(LibSSH_VERSION VERSION_LESS 0.8.0)
+
+    if(LIBSSH_VERSION VERSION_LESS 0.8.0)
         target_link_libraries(netconf2 "-L${LIBSSH_LIBRARY_DIR}" -lssh -lssh_threads -lcrypt)
     else()
         target_link_libraries(netconf2 "-L${LIBSSH_LIBRARY_DIR}" -lssh -lcrypt)
@@ -221,7 +244,7 @@
 endif()
 
 # dependencies - libval
-if (ENABLE_DNSSEC)
+if(ENABLE_DNSSEC)
     find_package(LibVAL REQUIRED)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_DNSSEC")
     target_link_libraries(netconf2 ${LIBVAL_LIBRARIES})
@@ -246,7 +269,7 @@
 # Python bindings
 if(ENABLE_PYTHON)
     add_subdirectory(python)
-endif(ENABLE_PYTHON)
+endif()
 
 # install library
 install(TARGETS netconf2 DESTINATION ${CMAKE_INSTALL_LIBDIR})
@@ -287,7 +310,7 @@
     if(CMOCKA_FOUND)
         enable_testing()
         add_subdirectory(tests)
-    endif(CMOCKA_FOUND)
+    endif()
 endif()
 
 configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_BINARY_DIR}/src/config.h" ESCAPE_QUOTES @ONLY)
diff --git a/CMakeModules/FindCMocka.cmake b/CMakeModules/FindCMocka.cmake
index 2dd9fc5..c8bd26f 100644
--- a/CMakeModules/FindCMocka.cmake
+++ b/CMakeModules/FindCMocka.cmake
@@ -35,15 +35,15 @@
         ${CMOCKA_ROOT_DIR}/include
 )
 
-if (CMOCKA_LIBRARY)
+if(CMOCKA_LIBRARY)
   set(CMOCKA_LIBRARIES
       ${CMOCKA_LIBRARIES}
       ${CMOCKA_LIBRARY}
   )
-endif (CMOCKA_LIBRARY)
+endif()
 
 include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR)
+find_package_handle_standard_args(CMOCKA DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR)
 
 # show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view
 mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES)
diff --git a/CMakeModules/FindLibSSH.cmake b/CMakeModules/FindLibSSH.cmake
index 49737ff..7e5fa33 100644
--- a/CMakeModules/FindLibSSH.cmake
+++ b/CMakeModules/FindLibSSH.cmake
@@ -31,10 +31,10 @@
 #  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
 
-if (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
+if(LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
   # in cache already
   set(LIBSSH_FOUND TRUE)
-else (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
+else()
 
   find_path(LIBSSH_INCLUDE_DIR
     NAMES
@@ -48,10 +48,11 @@
       ${CMAKE_INSTALL_PREFIX}/include
   )
 
-  find_library(SSH_LIBRARY
+  find_library(LIBSSH_LIBRARY
     NAMES
       ssh.so
       libssh.so
+      libssh.dylib
     PATHS
       /usr/lib
       /usr/local/lib
@@ -61,58 +62,63 @@
       ${CMAKE_INSTALL_PREFIX}/lib
   )
 
-  if (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
-    set(SSH_FOUND TRUE)
-  endif (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
+  if(LIBSSH_INCLUDE_DIR AND LIBSSH_LIBRARY)
+    set(LIBSSH_FOUND TRUE)
+    message(STATUS "LIBSSH Found: ${LIBSSH_LIBRARY}")
+  endif()
 
-  set(LIBSSH_INCLUDE_DIRS
-    ${LIBSSH_INCLUDE_DIR}
-  )
+  set(LIBSSH_INCLUDE_DIRS ${LIBSSH_INCLUDE_DIR})
+  message(STATUS "LIBSSH_INCLUDE_DIR: ${LIBSSH_INCLUDE_DIR}")
 
-  if (SSH_FOUND)
+  if(LIBSSH_FOUND)
     string(REPLACE "libssh.so" ""
       LIBSSH_LIBRARY_DIR
-      ${SSH_LIBRARY}
+      ${LIBSSH_LIBRARY}
+    )
+    string(REPLACE "libssh.dylib" ""
+      LIBSSH_LIBRARY_DIR
+      ${LIBSSH_LIBRARY_DIR}
     )
     string(REPLACE "ssh.so" ""
       LIBSSH_LIBRARY_DIR
       ${LIBSSH_LIBRARY_DIR}
     )
+    message(STATUS "LIBSSH_LIBRARY_DIR: ${LIBSSH_LIBRARY_DIR}")
 
-    if (LibSSH_FIND_VERSION)
+    if(LIBSSH_FIND_VERSION)
       file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
         REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
       # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
-      if (LIBSSH_VERSION_MAJOR)
+      if(LIBSSH_VERSION_MAJOR)
         string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
-	file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
+        file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
           REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
-	string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
-	file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
+        string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
+        file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
           REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
-	string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
+        string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
 
-	set(LibSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
+        set(LIBSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
 
-	include(FindPackageVersionCheck)
-	find_package_version_check(LibSSH DEFAULT_MSG)
-      else (LIBSSH_VERSION_MAJOR)
+        include(FindPackageVersionCheck)
+        find_package_version_check(LIBSSH DEFAULT_MSG)
+      else()
         message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
         set(LIBSSH_FOUND FALSE)
-      endif (LIBSSH_VERSION_MAJOR)
-    endif (LibSSH_FIND_VERSION)
-  endif (SSH_FOUND)
+      endif()
+    endif()
+  endif()
 
   # If the version is too old, but libs and includes are set,
   # find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
   # so we need this if() here.
-  if (LIBSSH_FOUND)
+  if(LIBSSH_FOUND)
     include(FindPackageHandleStandardArgs)
-    find_package_handle_standard_args(LibSSH DEFAULT_MSG LIBSSH_LIBRARY_DIR LIBSSH_INCLUDE_DIRS)
-  endif (LIBSSH_FOUND)
+    find_package_handle_standard_args(LIBSSH DEFAULT_MSG LIBSSH_LIBRARY_DIR LIBSSH_INCLUDE_DIRS)
+  endif()
 
   # show the LIBSSH_INCLUDE_DIRS and LIBSSH_LIBRARY_DIR variables only in the advanced view
   mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARY_DIR)
 
-endif (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
+endif()
 
diff --git a/CMakeModules/FindLibVAL.cmake b/CMakeModules/FindLibVAL.cmake
index 507bf8a..57d7db8 100644
--- a/CMakeModules/FindLibVAL.cmake
+++ b/CMakeModules/FindLibVAL.cmake
@@ -32,10 +32,10 @@
 #  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
 
-if (LIBVAL_LIBRARIES AND LIBVAL_INCLUDE_DIRS)
+if(LIBVAL_LIBRARIES AND LIBVAL_INCLUDE_DIRS)
   # in cache already
   set(LIBVAL_FOUND TRUE)
-else (LIBVAL_LIBRARIES AND LIBVAL_INCLUDE_DIRS)
+else()
 
   find_path(LIBVAL_INCLUDE_DIR
     NAMES
@@ -83,17 +83,20 @@
       ${CMAKE_INSTALL_PREFIX}/lib
   )
 
-  if (LIBVAL_INCLUDE_DIR AND LIBVAL_LIBRARY AND LIBSRES_LIBRARY)
+  if(LIBVAL_INCLUDE_DIR AND LIBVAL_LIBRARY AND LIBSRES_LIBRARY)
     set(LIBVAL_FOUND TRUE)
-  else (LIBVAL_INCLUDE_DIR AND LIBVAL_LIBRARY AND LIBSRES_LIBRARY)
+  else()
     set(LIBVAL_FOUND FALSE)
-  endif (LIBVAL_INCLUDE_DIR AND LIBVAL_LIBRARY AND LIBSRES_LIBRARY)
+  endif()
 
   set(LIBVAL_INCLUDE_DIRS ${LIBVAL_INCLUDE_DIR})
   set(LIBVAL_LIBRARIES ${LIBSRES_LIBRARY} ${LIBVAL_LIBRARY})
 
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(LIBVAL DEFAULT_MSG LIBVAL_LIBRARIES LIBVAL_INCLUDE_DIRS)
+
   # show the LIBVAL_INCLUDE_DIRS and LIBVAL_LIBRARIES variables only in the advanced view
   mark_as_advanced(LIBVAL_INCLUDE_DIRS LIBVAL_LIBRARIES)
 
-endif (LIBVAL_LIBRARIES AND LIBVAL_INCLUDE_DIRS)
+endif()
 
diff --git a/CMakeModules/FindLibYANG.cmake b/CMakeModules/FindLibYANG.cmake
index b3478b1..51fdc88 100644
--- a/CMakeModules/FindLibYANG.cmake
+++ b/CMakeModules/FindLibYANG.cmake
@@ -3,7 +3,7 @@
 #
 #  LIBYANG_FOUND - system has LibYANG
 #  LIBYANG_INCLUDE_DIRS - the LibYANG include directory
-#  LIBYANG_LIBRARIES - Link these to use LibSSH
+#  LIBYANG_LIBRARIES - Link these to use LibYANG
 #
 #  Author Radek Krejci <rkrejci@cesnet.cz>
 #  Copyright (c) 2015 CESNET, z.s.p.o.
@@ -17,7 +17,7 @@
 #  2. Redistributions in binary form must reproduce the copyright
 #     notice, this list of conditions and the following disclaimer in the
 #     documentation and/or other materials provided with the distribution.
-#  3. The name of the author may not be used to endorse or promote products 
+#  3. The name of the author may not be used to endorse or promote products
 #     derived from this software without specific prior written permission.
 #
 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
@@ -32,10 +32,10 @@
 #  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
 
-if (LIBYANG_LIBRARIES AND LIBYANG_INCLUDE_DIRS)
+if(LIBYANG_LIBRARIES AND LIBYANG_INCLUDE_DIRS)
   # in cache already
   set(LIBYANG_FOUND TRUE)
-else (LIBYANG_LIBRARIES AND LIBYANG_INCLUDE_DIRS)
+else()
 
   find_path(LIBYANG_INCLUDE_DIR
     NAMES
@@ -48,7 +48,7 @@
       ${CMAKE_INCLUDE_PATH}
       ${CMAKE_INSTALL_PREFIX}/include
   )
-  
+
   find_library(LIBYANG_LIBRARY
     NAMES
       yang
@@ -64,17 +64,20 @@
       ${CMAKE_INSTALL_PREFIX}/lib
   )
 
-  if (LIBYANG_INCLUDE_DIR AND LIBYANG_LIBRARY)
+  if(LIBYANG_INCLUDE_DIR AND LIBYANG_LIBRARY)
     set(LIBYANG_FOUND TRUE)
-  else (LIBYANG_INCLUDE_DIR AND LIBYANG_LIBRARY)
+  else()
     set(LIBYANG_FOUND FALSE)
-  endif (LIBYANG_INCLUDE_DIR AND LIBYANG_LIBRARY)
+  endif()
 
   set(LIBYANG_INCLUDE_DIRS ${LIBYANG_INCLUDE_DIR})
   set(LIBYANG_LIBRARIES ${LIBYANG_LIBRARY})
 
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(LIBYANG DEFAULT_MSG LIBYANG_LIBRARIES LIBYANG_INCLUDE_DIRS)
+
   # show the LIBYANG_INCLUDE_DIRS and LIBYANG_LIBRARIES variables only in the advanced view
   mark_as_advanced(LIBYANG_INCLUDE_DIRS LIBYANG_LIBRARIES)
 
-endif (LIBYANG_LIBRARIES AND LIBYANG_INCLUDE_DIRS)
+endif()
 
diff --git a/CMakeModules/FindPackageVersionCheck.cmake b/CMakeModules/FindPackageVersionCheck.cmake
index f0de790..48663cd 100644
--- a/CMakeModules/FindPackageVersionCheck.cmake
+++ b/CMakeModules/FindPackageVersionCheck.cmake
@@ -21,7 +21,7 @@
 #  2. Redistributions in binary form must reproduce the copyright
 #     notice, this list of conditions and the following disclaimer in the
 #     documentation and/or other materials provided with the distribution.
-#  3. The name of the author may not be used to endorse or promote products 
+#  3. The name of the author may not be used to endorse or promote products
 #     derived from this software without specific prior written permission.
 #
 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
@@ -41,49 +41,49 @@
   set(_AGE "old")
 
   if(${_NAME}_FIND_VERSION_EXACT)
-    if (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
+    if(${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
       # exact version found
       set(${_NAME_UPPER}_FOUND TRUE)
-    else (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
+    else()
       # exect version not found
       set(${_NAME_UPPER}_FOUND FALSE)
       # check if newer or older
-      if (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
+      if(${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
         set(_AGE "new")
-      else (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
+      else()
         set(_AGE "old")
-      endif (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
-    endif (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
-  else (${_NAME}_FIND_VERSION_EXACT)
-    if (${_NAME}_FIND_VERSION)
-      if (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
+      endif()
+    endif()
+  else()
+    if(${_NAME}_FIND_VERSION)
+      if(${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
         set(${_NAME_UPPER}_FOUND FALSE)
         set(_AGE "old")
-      else (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
+      else()
         set(${_NAME_UPPER}_FOUND TRUE)
-     endif (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
-    endif (${_NAME}_FIND_VERSION)
-  endif(${_NAME}_FIND_VERSION_EXACT)
+      endif()
+    endif()
+  endif()
 
-  if ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
-    if (${_NAME}_FIND_VERSION_EXACT)
+  if("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
+    if(${_NAME}_FIND_VERSION_EXACT)
       set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, version ${${_NAME}_FIND_VERSION} is required.")
-    else (${_NAME}_FIND_VERSION_EXACT)
+    else()
       set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, at least version ${${_NAME}_FIND_VERSION} is required.")
-    endif (${_NAME}_FIND_VERSION_EXACT)
-  else ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
+    endif()
+  else()
     set(_FAIL_MESSAGE "${_FAIL_MSG}")
-  endif ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
+  endif()
 
-  if (NOT ${_NAME_UPPER}_FOUND)
-    if (${_NAME}_FIND_REQUIRED)
+  if(NOT ${_NAME_UPPER}_FOUND)
+    if(${_NAME}_FIND_REQUIRED)
       message(FATAL_ERROR "${_FAIL_MESSAGE}")
-    else (${_NAME}_FIND_REQUIRED)
-      if (NOT ${_NAME}_FIND_QUIETLY)
+    else()
+      if(NOT ${_NAME}_FIND_QUIETLY)
         message(STATUS "${_FAIL_MESSAGE}")
-      endif (NOT ${_NAME}_FIND_QUIETLY)
-    endif (${_NAME}_FIND_REQUIRED)
-  endif (NOT ${_NAME_UPPER}_FOUND)
+      endif()
+    endif()
+  endif()
 
   set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
-endfunction(FIND_PACKAGE_VERSION_CHECK)
+endfunction()
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index d60eb64..16d6156 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -1,12 +1,12 @@
 find_program(PYTHON "python3")
 
-if (PYTHON)
+if(PYTHON)
     set(SETUP_PY_IN      "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
     set(SETUP_PY         "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
     set(PYAPI_BUILD_DIR  "${CMAKE_CURRENT_BINARY_DIR}/build")
     set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${PYAPI_BUILD_DIR})
-    
-    if(CMAKE_BUILD_TYPE STREQUAL debug)
+
+    if(CMAKE_BUILD_TYPE STREQUAL Debug)
         set(DEBUG "--debug")
     endif()
 
@@ -20,8 +20,8 @@
 
     configure_file(${SETUP_PY_IN} ${SETUP_PY})
     configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/Makefile.in ${CMAKE_CURRENT_BINARY_DIR}/docs/Makefile)
-	add_custom_target(pyapi ALL COMMAND ${PYTHON} ${SETUP_PY} build -b ${PYAPI_BUILD_DIR} ${DEBUG})
-	add_custom_target(pyapidoc COMMAND make -f ${CMAKE_CURRENT_BINARY_DIR}/docs/Makefile html)
+    add_custom_target(pyapi ALL COMMAND ${PYTHON} ${SETUP_PY} build -b ${PYAPI_BUILD_DIR} ${DEBUG})
+    add_custom_target(pyapidoc COMMAND make -f ${CMAKE_CURRENT_BINARY_DIR}/docs/Makefile html)
     execute_process(COMMAND ${PYTHON} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(plat_specific=True))"
         OUTPUT_VARIABLE PYTHON_MODULE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
     install(CODE "execute_process(COMMAND ${PYTHON} ${SETUP_PY} build -b ${PYAPI_BUILD_DIR} install --install-lib=\$ENV{DESTDIR}/${PYTHON_MODULE_PATH})")
diff --git a/python/setup.py.in b/python/setup.py.in
index c6cc1d4..6f912d9 100644
--- a/python/setup.py.in
+++ b/python/setup.py.in
@@ -2,7 +2,7 @@
 
 netconf2Module = Extension("netconf2",
                            sources=["${CMAKE_CURRENT_SOURCE_DIR}/netconf.c",
-                                    "${CMAKE_CURRENT_SOURCE_DIR}/session.c", 
+                                    "${CMAKE_CURRENT_SOURCE_DIR}/session.c",
                                     "${CMAKE_CURRENT_SOURCE_DIR}/ssh.c",
                                     "${CMAKE_CURRENT_SOURCE_DIR}/tls.c",
                                     "${CMAKE_CURRENT_SOURCE_DIR}/rpc.c",
@@ -13,7 +13,7 @@
                                     "${CMAKE_CURRENT_COURCE_DIR}/rpc.h"
                                    ],
                            libraries=["netconf2"],
-                           extra_compile_args=["-Wall", "-I${CMAKE_CURRENT_BINARY_DIR}", "-I${LIBYANG_INCLUDE_DIR}" @SSH_DEFINE@ @TLS_DEFINE@],
+                           extra_compile_args=["-Wall", "-I${CMAKE_CURRENT_BINARY_DIR}", "-I${LIBYANG_INCLUDE_DIR}", "-I${LIBSSH_INCLUDE_DIR}" @SSH_DEFINE@ @TLS_DEFINE@],
                            extra_link_args=["-L${CMAKE_CURRENT_BINARY_DIR}/.."],
                         )
 
diff --git a/src/io.c b/src/io.c
index 4c5ef55..cb294bf 100644
--- a/src/io.c
+++ b/src/io.c
@@ -813,8 +813,9 @@
 nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count)
 {
     int ret = 0, c;
-    char chunksize[20];
+    char chunksize[24];
 
+    // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=]
     if (session->version == NC_VERSION_11) {
         sprintf(chunksize, "\n#%zu\n", count);
         ret = nc_write(session, chunksize, strlen(chunksize));
diff --git a/src/session_server.c b/src/session_server.c
index a60f34b..239c7cf 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -1720,11 +1720,11 @@
 static int
 nc_accept_unix(struct nc_session *session, int sock)
 {
+#ifdef SO_PEERCRED
     const struct passwd *pw;
     struct ucred ucred;
     char *username;
     socklen_t len;
-
     session->ti_type = NC_TI_UNIX;
 
     len = sizeof(ucred);
@@ -1754,6 +1754,9 @@
     session->ti.unixsock.sock = sock;
 
     return 1;
+#else
+    return -1;
+#endif
 }
 
 API int
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index fb0dc85..412fbac 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -19,10 +19,14 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <pwd.h>
+#ifndef __APPLE__
 #include <shadow.h>
 #include <crypt.h>
+#endif
 #include <errno.h>
 #include <time.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "config.h"
 #include "session_server.h"
@@ -660,6 +664,7 @@
 static char *
 auth_password_get_pwd_hash(const char *username)
 {
+#ifndef __APPLE__
     struct passwd *pwd, pwd_buf;
     struct spwd *spwd, spwd_buf;
     char *pass_hash = NULL, buf[256];
@@ -698,6 +703,9 @@
     }
 
     return strdup(pass_hash);
+#else
+    return strdup("");
+#endif
 }
 
 static int
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 9cc1a0f..8807247 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,5 +1,3 @@
-cmake_minimum_required(VERSION 2.6)
-
 # list of all the tests in each directory
 set(tests test_io test_fd_comm test_init_destroy_client test_init_destroy_server test_time test_client_thread)
 set(client_tests test_client test_client_messages)
@@ -7,9 +5,9 @@
 # add -Wl,--wrap flags
 set(test test_client_ssh)
 set(${test}_mock_funcs connect ssh_connect ssh_userauth_none ssh_userauth_kbdint ssh_is_connected
-                       ssh_channel_open_session ssh_channel_request_subsystem ssh_channel_is_close ssh_channel_write
-                       ssh_channel_poll_timeout ssh_userauth_password nc_handshake_io nc_ctx_check_and_fill
-                       ssh_userauth_try_publickey ssh_userauth_publickey)
+    ssh_channel_open_session ssh_channel_request_subsystem ssh_channel_is_close ssh_channel_write
+    ssh_channel_poll_timeout ssh_userauth_password nc_handshake_io nc_ctx_check_and_fill
+    ssh_userauth_try_publickey ssh_userauth_publickey)
 set(${test}_wrap_link_flags "-Wl")
 foreach(mock_func IN LISTS ${test}_mock_funcs)
     set(${test}_wrap_link_flags "${${test}_wrap_link_flags},--wrap=${mock_func}")
@@ -23,13 +21,13 @@
 endforeach()
 
 #append tests depending on SSH/TLS
-if (ENABLE_SSH OR ENABLE_TLS)
+if(ENABLE_SSH OR ENABLE_TLS)
     list(APPEND tests test_server_thread)
-    if (ENABLE_SSH)
+    if(ENABLE_SSH)
         list(APPEND client_tests test_client_ssh)
     endif()
 
-    if (ENABLE_TLS)
+    if(ENABLE_TLS)
         list(APPEND client_tests test_client_tls)
     endif()
 endif()
@@ -42,6 +40,7 @@
 foreach(test_name IN LISTS tests)
     add_executable(${test_name} $<TARGET_OBJECTS:testobj> ${test_name}.c)
     target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2)
+    target_include_directories(${test_name} PRIVATE ${CMOCKA_INCLUDE_DIR})
     set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${${test_name}_wrap_link_flags}")
     add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
 endforeach()
@@ -49,24 +48,25 @@
 foreach(test_name IN LISTS client_tests)
     add_executable(${test_name} $<TARGET_OBJECTS:testobj> ./client/${test_name}.c)
     target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2)
+    target_include_directories(${test_name} PRIVATE ${CMOCKA_INCLUDE_DIR})
     set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${${test_name}_wrap_link_flags}")
     add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
 endforeach()
 
-if (ENABLE_VALGRIND_TESTS)
+if(ENABLE_VALGRIND_TESTS)
     find_program(valgrind_FOUND valgrind)
-    if (valgrind_FOUND)
-        foreach (test_name IN LISTS tests)
+    if(valgrind_FOUND)
+        foreach(test_name IN LISTS tests)
             add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1
-                                           --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name})
+                --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name})
         endforeach()
 
-        foreach (test_name IN LISTS client_tests)
+        foreach(test_name IN LISTS client_tests)
             add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1
-                                            --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name})
+                --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name})
         endforeach()
-    else (valgrind_FOUND)
-        Message("-- valgrind executable not found! Disabling memory leaks tests")
+    else()
+        message("-- valgrind executable not found! Disabling memory leaks tests")
     endif()
 endif()
 
diff --git a/tests/test_server_thread.c b/tests/test_server_thread.c
index c523e8c..5cc0114 100644
--- a/tests/test_server_thread.c
+++ b/tests/test_server_thread.c
@@ -36,6 +36,7 @@
 
 #define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
 
+#if _POSIX_BARRIERS >= 200112L
 pthread_barrier_t barrier;
 
 #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
@@ -645,10 +646,12 @@
     ++clients;
 #endif
 }
+#endif
 
 int
 main(void)
 {
+#if _POSIX_BARRIERS >= 200112L
     struct ly_ctx *ctx;
     int ret, i, clients = 0;
     pthread_t tids[thread_count];
@@ -751,6 +754,7 @@
 
     nc_server_destroy();
     ly_ctx_destroy(ctx, NULL);
+#endif
 
     return 0;
 }