1# Various helper function and macros for building libtorrent
2
3include(FeatureSummary)
4
5# macro for issuing option() and add_feature_info() in a single call.
6# Synopsis:
7# feature_option(<option_and_feature_name> <option_and_feature_description> <default_option_value>)
8macro(feature_option _name _description _default)
9	option(${_name} "${_description}" ${_default})
10	add_feature_info(${_name} ${_name} "${_description}")
11endmacro()
12
13# function to add a simple build option which controls compile definition(s) for a target.
14# Synopsis:
15# target_optional_compile_definitions(<target> [FEATURE]
16#   NAME <name> DESCRIPTION <description> DEFAULT <default_value>
17#   [ENABLED [enabled_compile_definitions...]]
18#   [DISABLED [disabled_compile_defnitions...]]
19# )
20# NAME, DESCRIPTION and DEFAULT are passed to option() call
21# if FEATURE is given, they are passed to add_feature_info()
22# ENABLED lists compile definitions that will be set on <target> when option is enabled,
23# DISABLED lists definitions that will be set otherwise
24function(target_optional_compile_definitions _target _scope)
25	set(options FEATURE)
26	set(oneValueArgs NAME DESCRIPTION DEFAULT)
27	set(multiValueArgs ENABLED DISABLED)
28	cmake_parse_arguments(TOCD ${options} "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
29	option(${TOCD_NAME} "${TOCD_DESCRIPTION}" ${TOCD_DEFAULT})
30	if (${${TOCD_NAME}})
31		target_compile_definitions(${_target} ${_scope} ${TOCD_ENABLED})
32	else()
33		target_compile_definitions(${_target} ${_scope} ${TOCD_DISABLED})
34	endif()
35	if(${TOCD_FEATURE})
36		add_feature_info(${TOCD_NAME} ${TOCD_NAME} "${TOCD_DESCRIPTION}")
37	endif()
38endfunction()
39
40# a helper macro that calls find_package() and appends the package (if found) to the
41# _package_dependencies list, which can be used later to generate package config file
42macro(find_public_dependency _name)
43	find_package(${_name} ${ARGN})
44	string(TOUPPER "${_name}" _name_uppercased)
45	if (${_name}_FOUND OR ${_name_uppercased}_FOUND)
46		# Dependencies to be used below for generating Config.cmake file
47		# We don't need the 'REQUIRED' argument there
48		set(_args "${_name}")
49		list(APPEND _args "${ARGN}")
50		list(REMOVE_ITEM _args "REQUIRED")
51		list(REMOVE_ITEM _args "") # just in case
52		string(REPLACE ";" " " _args "${_args}")
53		list(APPEND _package_dependencies "${_args}")
54	endif()
55endmacro()
56
57# function for parsing version variables that are set in version.hpp file
58# the version identifiers there are defined as follows:
59# #define LIBTORRENT_VERSION_MAJOR 1
60# #define LIBTORRENT_VERSION_MINOR 2
61# #define LIBTORRENT_VERSION_TINY 0
62
63function(read_version _verFile _outVarMajor _outVarMinor _outVarTiny)
64	file(STRINGS ${_verFile} verFileContents REGEX ".+LIBTORRENT_VERSION_[A-Z]+.[0-9]+.*")
65# 	message(STATUS "version file contents: ${verFileContents}")
66	# the verFileContents variable contains something like the following:
67	# #define LIBTORRENT_VERSION_MAJOR 1;#define LIBTORRENT_VERSION_MINOR 2;#define LIBTORRENT_VERSION_TINY 0
68	set(_regex ".+_MAJOR +([0-9]+);.+_MINOR +([0-9]+);.+_TINY +([0-9]+)")
69	 # note quotes around _regex, they are needed because the variable contains semicolons
70	string(REGEX MATCH "${_regex}" _tmp "${verFileContents}")
71	if (NOT _tmp)
72		message(FATAL_ERROR "Could not detect project version number from ${_verFile}")
73	endif()
74
75# 	message(STATUS "Matched version string: ${_tmp}")
76
77	set(${_outVarMajor} ${CMAKE_MATCH_1} PARENT_SCOPE)
78	set(${_outVarMinor} ${CMAKE_MATCH_2} PARENT_SCOPE)
79	set(${_outVarTiny} ${CMAKE_MATCH_3} PARENT_SCOPE)
80endfunction()
81