Skip to content

Commit

Permalink
Merge pull request #75 from eclipse-zenoh/cmake_3_16
Browse files Browse the repository at this point in the history
Fixes for cmake 3.16 support
  • Loading branch information
milyin authored Sep 21, 2023
2 parents e5e333e + 024da6d commit 9fec0f4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ project(
LANGUAGES C CXX
)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
include(helpers)

if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
include(cmake/helpers.cmake)
enable_testing()
set_default_build_type(Release)
configure_include_project(ZENOHCXX_ZENOHPICO zenohpico zenohpico "../zenoh-pico" zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "")
Expand Down
70 changes: 56 additions & 14 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
# functions and macros definitions
#

#
# Allow to find cmake modules in this directory
#
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}" ${CMAKE_MODULE_PATH})


#
# Show VARIABLE = value on configuration stage
Expand Down Expand Up @@ -66,6 +61,35 @@ function(debug_print var)
)
endfunction()


#
# initialize `dstvar` with `$<IF:genexpr_condition,srcvar_true,srcvar_false>` generator expression
# only if `srcvar_true` and `srcvar_false` are actually different and if multi-config generator is used.
# Otherwise `dstvar` is initialized with `srcvar_true` or `srcvar_false` value depending on `var_condition` variable.
#
# This is convenient to
# - remove visual garbage when generator expressions actually does nothing
# - avoid using generator expression if single-config generator is used
#
function(set_genexpr_condition dstvar var_condition genexpr_condition srcvar_true srcvar_false)
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(GENERATOR_IS_MULTI_CONFIG AND NOT("${srcvar_true}" STREQUAL "${srcvar_false}"))
list(JOIN srcvar_true "$<SEMICOLON>" srcvar_true)
list(JOIN srcvar_false "$<SEMICOLON>" srcvar_false)
set(${dstvar} $<IF:${genexpr_condition},${srcvar_true},${srcvar_false}> PARENT_SCOPE)
else()
if(DEFINED ${var_condition})
if (${${var_condition}})
set(${dstvar} "${srcvar_true}" PARENT_SCOPE)
else()
set(${dstvar} "${srcvar_false}" PARENT_SCOPE)
endif()
else()
set(${dstvar} "${srcvar_false}" PARENT_SCOPE)
endif()
endif()
endfunction()

#
# Copy necessary dlls to target runtime directory
#
Expand All @@ -84,6 +108,12 @@ endfunction()
macro(set_default_build_type config_type)
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(GENERATOR_IS_MULTI_CONFIG)
if(${CMAKE_VERSION} VERSION_LESS "3.20")
message(FATAL_ERROR "\n"
"You are using multi config generator '${CMAKE_GENERATOR}' and cmake ${CMAKE_VERSION}\n"
"Unfortuinately multi config generators are not supported by this script for CMake < 3.20 due to insufficient support of generator expressions in old CMake versions\n"
"Please consider upgrading your cmake or switching to single-config generator with `-G` option, like `cmake -G Ninja` or `cmake -G \"Unix Makefiles\"\n")
endif()
if(NOT DEFINED CMAKE_BUILD_TYPE) # if user passed argument '-DCMAKE_BUILD_TYPE=value', use it
set(CMAKE_BUILD_TYPE ${config_type})
endif()
Expand All @@ -103,24 +133,29 @@ macro(set_default_build_type config_type)
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE ${config_type})
endif()
status_print(CMAKE_BUILD_TYPE)
status_print(CMAKE_BUILD_TYPE)
endif()
endmacro()

#
# Add default set of libraries depending on platform
#
function(add_platfrom_libraries target)
function(get_required_static_libs variable)
# actually required list of libraries can be obtained by executing
# cargo rustc -- --print native-static-libs
# This command is not intented to be used in autoamted build yet,
# so actaul libraries are hardcoded for now
if(APPLE)
find_library(FFoundation Foundation)
find_library(FSecurity Security)
target_link_libraries(${target} PUBLIC ${FFoundation} ${FSecurity})
set(native_static_libs ${FFoundation} ${FSecurity})
elseif(UNIX)
target_link_libraries(${target} PUBLIC rt pthread m dl)
set(native_static_libs rt pthread m dl)
elseif(WIN32)
target_link_libraries(${target} PUBLIC ws2_32 crypt32 secur32 bcrypt ncrypt userenv ntdll iphlpapi runtimeobject)
set(native_static_libs ws2_32 crypt32 secur32 bcrypt ncrypt userenv ntdll iphlpapi runtimeobject)
endif()

set(${variable} ${native_static_libs} PARENT_SCOPE)
message(STATUS "${variable} = ${native_static_libs}")
endfunction()

#
Expand Down Expand Up @@ -186,14 +221,21 @@ endmacro()
function(include_project)
__include_project(${ARGN})
# recover functions which may be replaced by included project
include(${CMAKE_CURRENT_FUNCTION_LIST_FILE})
# Using here the policy https://cmake.org/cmake/help/v3.16/policy/CMP0017.html#policy:CMP0017
include(helpers OPTIONAL RESULT_VARIABLE helpers_included)
if (NOT helpers_included)
message(FATAL_ERROR "Failed to reinclude helpers.cmake after processing external project\n"
"You have to include helpers.cmake as a module to make the `include` operation above work:\n"
" set(CMAKE_MODULE_PATH \"path-to-this-helpers-cmake\" \${CMAKE_MODULE_PATH})\n"
" include(helpers)\n")
endif()
endfunction()

function(__include_project project_name)
function(include_project project_name)
include(FetchContent)
include(CMakeParseArguments)
cmake_parse_arguments(PARSE_ARGV 1 "ARG" "QUIET" "TARGET;PATH;PACKAGE;GIT_URL;GIT_TAG" "")
unset_if_empty(ARG_PATH ARG_TARGET ARG_PACKAGE ARG_GIT_URL)
unset_if_empty(ARG_PATH ARG_TARGET ARG_PACKAGE ARG_GIT_URL ARG_GIT_TAG)
if(NOT DEFINED ARG_TARGET)
message(FATAL_ERROR "Non-empty TARGET parameter is required")
endif()
Expand Down
3 changes: 2 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
# Settings when 'examples' is the root projet
cmake_minimum_required(VERSION 3.16)
project(zenohcxx_examples LANGUAGES C CXX)
include(../cmake/helpers.cmake)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake ${CMAKE_MODULE_PATH})
include(helpers)
set_default_build_type(Release)
configure_include_project(ZENOHCXX_ZENOHC zenohc zenohc::lib "../../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c" "")
configure_include_project(ZENOHPICO zenohpico zenohpico "../../zenoh-pico" zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "")
Expand Down
3 changes: 2 additions & 1 deletion install/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
HOMEPAGE_URL "https://github.com/eclipse-zenoh/zenoh-cpp"
LANGUAGES C CXX
)
include(../cmake/helpers.cmake)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake ${CMAKE_MODULE_PATH})
include(helpers)
include_project(zenohcxx TARGET zenohcxx PATH "..")
else()
message(STATUS "zenoh-cpp install")
Expand Down

0 comments on commit 9fec0f4

Please sign in to comment.