#
# Copyright 2018, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# functions.cmake - helper functions for CMakeLists.txt
#
function(join SEP OUT VALUES)
string(REPLACE ";" "${SEP}" JOIN_TMP "${VALUES}")
set(${OUT} "${JOIN_TMP}" PARENT_SCOPE)
endfunction()
# prepends prefix to list of strings
function(prepend var prefix)
set(listVar "")
foreach(f ${ARGN})
list(APPEND listVar "${prefix}/${f}")
endforeach(f)
set(${var} "${listVar}" PARENT_SCOPE)
endfunction()
# Checks whether flag is supported by current C++ compiler and appends
# it to the relevant cmake variable.
# 1st argument is a flag
# 2nd (optional) argument is a build type (debug, release)
macro(add_flag flag)
string(REPLACE - _ flag2 ${flag})
string(REPLACE " " _ flag2 ${flag2})
string(REPLACE = "_" flag2 ${flag2})
set(check_name "CXX_HAS_${flag2}")
check_cxx_compiler_flag(${flag} ${check_name})
if (${${check_name}})
if (${ARGC} EQUAL 1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
else()
set(CMAKE_CXX_FLAGS_${ARGV1} "${CMAKE_CXX_FLAGS_${ARGV1}} ${flag}")
endif()
endif()
endmacro()
macro(add_sanitizer_flag flag)
set(SAVED_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} -fsanitize=${flag}")
check_cxx_compiler_flag("-fsanitize=${flag}" CXX_HAS_ASAN_UBSAN)
if(CXX_HAS_ASAN_UBSAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${flag}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${flag}")
else()
message("${flag} sanitizer not supported")
endif()
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
endmacro()
# Generates cppstyle-$name and cppformat-$name targets and attaches them
# as dependencies of global "cppformat" target.
# cppstyle-$name target verifies C++ style of files in current source dir.
# cppformat-$name target reformats files in current source dir.
# If more arguments are used, then they are used as files to be checked
# instead.
# ${name} must be unique.
function(add_cppstyle name)
if(NOT CLANG_FORMAT)
return()
endif()
if(${ARGC} EQUAL 1)
add_custom_target(cppstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
check
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)
add_custom_target(cppformat-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
format
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)
else()
add_custom_target(cppstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
check
${ARGN}
)
add_custom_target(cppformat-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
format
${ARGN}
)
endif()
add_dependencies(cppstyle cppstyle-${name})
add_dependencies(cppformat cppformat-${name})
endfunction()
# Generates check-whitespace-$name target and attaches it as a dependency
# of global "check-whitespace" target.
# ${name} must be unique.
function(add_check_whitespace name)
add_custom_target(check-whitespace-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/check_whitespace ${ARGN})
add_dependencies(check-whitespace check-whitespace-${name})
endfunction()
# Sets ${ret} to version of program specified by ${name} in major.minor.patch format
function(get_program_version name ret)
execute_process(COMMAND ${name} --version
OUTPUT_VARIABLE cmd_ret
ERROR_QUIET)
STRING(REGEX MATCH "([0-9]+.)([0-9]+.)([0-9]+)" VERSION ${cmd_ret})
SET(${ret} ${VERSION} PARENT_SCOPE)
endfunction()