polybar/cmake/common/utils.cmake

134 lines
3.7 KiB
CMake
Raw Normal View History

2016-06-15 03:32:35 +00:00
#
# Collection of cmake utility functions
#
2017-01-26 16:17:02 +00:00
# message_colored {{{
2016-06-15 03:32:35 +00:00
function(message_colored message_level text color)
string(ASCII 27 esc)
message(${message_level} "${esc}[${color}m${text}${esc}[0m")
endfunction()
2016-10-24 23:46:26 +00:00
# }}}
2017-01-26 16:17:02 +00:00
# colored_option {{{
2016-10-24 23:46:26 +00:00
2017-01-26 16:17:02 +00:00
function(colored_option text flag)
# Append version of option, if ${flag}_VERSION is set
set(version ${${flag}_VERSION})
if(NOT "${version}" STREQUAL "")
set(text "${text} (${version})")
endif()
2017-01-26 16:17:02 +00:00
if(${flag})
message_colored(STATUS "[X]${text}" "32;1")
2017-01-26 16:17:02 +00:00
else()
message_colored(STATUS "[ ]${text}" "37;2")
2016-10-24 23:46:26 +00:00
endif()
endfunction()
2016-06-15 03:32:35 +00:00
# }}}
2017-01-26 16:17:02 +00:00
# queryfont {{{
function(queryfont output_variable fontname)
2017-01-13 21:42:10 +00:00
set(multi_value_args FIELDS)
cmake_parse_arguments(ARG "" "" "${multi_value_args}" ${ARGN})
find_program(BIN_FCLIST fc-list)
if(NOT BIN_FCLIST)
message_colored(WARNING "Failed to locate `fc-list`" "33;1")
return()
endif()
string(REPLACE ";" " " FIELDS "${ARG_FIELDS}")
if(NOT FIELDS)
set(FIELDS family)
endif()
execute_process(
COMMAND sh -c "${BIN_FCLIST} : ${FIELDS}"
RESULT_VARIABLE status
OUTPUT_VARIABLE output
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
STRING(REGEX REPLACE ";" "\\\\;" output "${output}")
STRING(REGEX REPLACE "\n" ";" output "${output}")
STRING(TOLOWER "${output}" output)
foreach(match LISTS ${output})
if(${match} MATCHES ".*${fontname}.*$")
2017-01-26 16:17:02 +00:00
list(APPEND matches ${match})
2017-01-13 21:42:10 +00:00
endif()
endforeach()
2017-01-26 16:17:02 +00:00
if(matches)
list(GET matches 0 fst_match)
set(${output_variable} "${fst_match}" PARENT_SCOPE)
message(STATUS "Found font: ${fst_match}")
2017-01-13 21:42:10 +00:00
else()
2017-01-24 12:02:55 +00:00
message_colored(STATUS "Font not found: ${fontname}" "33;1")
2017-01-13 21:42:10 +00:00
endif()
endfunction()
2017-01-26 16:17:02 +00:00
# }}}
# querylib {{{
function(querylib flag type pkg out_library out_include_dirs)
if(${flag})
if(${type} STREQUAL "cmake")
find_package(${pkg} REQUIRED)
string(TOUPPER ${pkg} pkg_upper)
list(APPEND ${out_library} ${${pkg_upper}_LIBRARY})
list(APPEND ${out_include_dirs} ${${pkg_upper}_INCLUDE_DIR})
elseif(${type} STREQUAL "pkg-config")
find_package(PkgConfig REQUIRED)
pkg_check_modules(PKG_${flag} REQUIRED ${pkg})
# Set packet version so that it can be used in the summary
set(${flag}_VERSION ${PKG_${flag}_VERSION} PARENT_SCOPE)
2017-01-26 16:17:02 +00:00
list(APPEND ${out_library} ${PKG_${flag}_LIBRARIES})
list(APPEND ${out_include_dirs} ${PKG_${flag}_INCLUDE_DIRS})
else()
message(FATAL_ERROR "Invalid lookup type '${type}'")
endif()
set(${out_library} ${${out_library}} PARENT_SCOPE)
set(${out_include_dirs} ${${out_include_dirs}} PARENT_SCOPE)
endif()
endfunction()
# }}}
# checklib {{{
function(checklib flag type pkg)
if(NOT DEFINED ${flag})
if(${type} STREQUAL "cmake")
find_package(${pkg} QUIET)
set(${flag} ${${pkg}_FOUND} CACHE BOOL "")
elseif(${type} STREQUAL "pkg-config")
find_package(PkgConfig REQUIRED)
pkg_check_modules(PKG_${flag} QUIET ${pkg})
set(${flag} ${PKG_${flag}_FOUND} CACHE BOOL "")
elseif(${type} STREQUAL "binary")
find_program(BIN_${flag} ${pkg})
set(${flag} ${BIN_${flag}} CACHE BOOL "")
else()
message(FATAL_ERROR "Invalid lookup type '${type}'")
endif()
endif()
endfunction()
function(get_include_dirs output)
get_filename_component(generated_sources_dir ${CMAKE_BINARY_DIR}/generated-sources ABSOLUTE)
get_filename_component(include_dir ${CMAKE_SOURCE_DIR}/include ABSOLUTE)
set(${output} ${include_dir} ${generated_sources_dir} PARENT_SCOPE)
endfunction()
function(get_sources_dirs output)
get_filename_component(src_dir ${CMAKE_SOURCE_DIR}/src ABSOLUTE)
set(${output} ${src_dir} PARENT_SCOPE)
endfunction()
2017-01-26 16:17:02 +00:00
# }}}