1# Copyright (c) 2013 Hartmut Kaiser
2# Copyright (c) 2014 Thomas Heller
3# Copyright (c) 2016 John Biddiscombe
4#
5# Distributed under the Boost Software License, Version 1.0. (See accompanying
6# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8# ---------------------------------------------------------------------
9# Function to add variables to a list that will be later written out
10# to file hpx/config/defines.hpp
11# ---------------------------------------------------------------------
12# on startup, this is unset, but we'll set it to an empty string anyway
13set_property(GLOBAL PROPERTY HPX_CONFIG_DEFINITIONS "")
14set_property(GLOBAL PROPERTY HPX_CONFIG_COND_DEFINITIONS "")
15
16function(hpx_add_config_define definition)
17
18  # if(ARGN) ignores an argument "0"
19  set(Args ${ARGN})
20  list(LENGTH Args ArgsLen)
21  if(ArgsLen GREATER 0)
22    set_property(GLOBAL APPEND PROPERTY HPX_CONFIG_DEFINITIONS "${definition} ${ARGN}")
23  else()
24    set_property(GLOBAL APPEND PROPERTY HPX_CONFIG_DEFINITIONS "${definition}")
25  endif()
26
27endfunction()
28
29function(hpx_add_config_cond_define definition)
30
31  # if(ARGN) ignores an argument "0"
32  set(Args ${ARGN})
33  list(LENGTH Args ArgsLen)
34  if(ArgsLen GREATER 0)
35    set_property(GLOBAL APPEND PROPERTY HPX_CONFIG_COND_DEFINITIONS "${definition} ${ARGN}")
36  else()
37    set_property(GLOBAL APPEND PROPERTY HPX_CONFIG_COND_DEFINITIONS "${definition}")
38  endif()
39
40endfunction()
41
42# ---------------------------------------------------------------------
43# Function to add config defines to a list that depends on a namespace variable
44# #defines that match the namespace can later be written out to a file
45# ---------------------------------------------------------------------
46function(hpx_add_config_define_namespace)
47  set(options)
48  set(one_value_args DEFINE NAMESPACE)
49  set(multi_value_args VALUE)
50  cmake_parse_arguments(OPTION
51    "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
52
53  set(DEF_VAR HPX_CONFIG_DEFINITIONS_${OPTION_NAMESPACE})
54
55  # to avoid extra trailing spaces (no value), use an if check
56  if(OPTION_VALUE)
57    set_property(GLOBAL APPEND PROPERTY ${DEF_VAR} "${OPTION_DEFINE} ${OPTION_VALUE}")
58  else()
59    set_property(GLOBAL APPEND PROPERTY ${DEF_VAR} "${OPTION_DEFINE}")
60  endif()
61
62endfunction()
63
64# ---------------------------------------------------------------------
65# Function to write variables out from a global var that was set using
66# the config_define functions into a config file
67# ---------------------------------------------------------------------
68function(write_config_defines_file)
69  set(options)
70  set(one_value_args TEMPLATE NAMESPACE FILENAME)
71  set(multi_value_args)
72  cmake_parse_arguments(OPTION
73    "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
74
75  if (${OPTION_NAMESPACE} STREQUAL "default")
76    get_property(DEFINITIONS_VAR GLOBAL PROPERTY HPX_CONFIG_DEFINITIONS)
77    get_property(COND_DEFINITIONS_VAR GLOBAL PROPERTY HPX_CONFIG_COND_DEFINITIONS)
78  else()
79    get_property(DEFINITIONS_VAR GLOBAL PROPERTY
80      HPX_CONFIG_DEFINITIONS_${OPTION_NAMESPACE})
81  endif()
82
83  if(DEFINED DEFINITIONS_VAR)
84    list(SORT DEFINITIONS_VAR)
85    list(REMOVE_DUPLICATES DEFINITIONS_VAR)
86  endif()
87
88  set(hpx_config_defines "\n")
89  foreach(def ${DEFINITIONS_VAR})
90    set(hpx_config_defines "${hpx_config_defines}#define ${def}\n")#"
91  endforeach()
92
93  if(DEFINED COND_DEFINITIONS_VAR)
94    list(SORT COND_DEFINITIONS_VAR)
95    list(REMOVE_DUPLICATES COND_DEFINITIONS_VAR)
96    set(hpx_config_defines "${hpx_config_defines}\n")
97  endif()
98  foreach(def ${COND_DEFINITIONS_VAR})
99    string(FIND ${def} " " _pos)
100    if(NOT ${_pos} EQUAL -1)
101      string(SUBSTRING ${def} 0 ${_pos} defname)
102    else()
103      set(defname ${def})
104      string(STRIP ${defname} defname)
105    endif()
106    set(hpx_config_defines
107        "${hpx_config_defines}#if !defined(${defname})\n#define ${def}\n#endif\n")#"
108  endforeach()
109
110  # if the user has not specified a template, generate a proper header file
111  if (NOT OPTION_TEMPLATE)
112    string(TOUPPER ${OPTION_NAMESPACE} NAMESPACE_UPPER)
113    set(PREAMBLE
114      "//  Copyright (c) Ste||ar Group\n"
115      "//\n"
116      "//  Distributed under the Boost Software License, Version 1.0. (See accompanying\n"
117      "//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\n"
118      "\n"
119      "// Do not edit this file! It has been generated by the cmake configuration step.\n"
120      "\n"
121      "#ifndef HPX_CONFIG_${NAMESPACE_UPPER}_HPP\n"
122      "#define HPX_CONFIG_${NAMESPACE_UPPER}_HPP\n"
123    )
124    set(TEMP_FILENAME "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${NAMESPACE_UPPER}")
125    file(WRITE ${TEMP_FILENAME}
126        ${PREAMBLE}
127        ${hpx_config_defines}
128        "\n#endif\n"
129    )
130    configure_file("${TEMP_FILENAME}" "${OPTION_FILENAME}" COPYONLY)
131    file(REMOVE "${TEMP_FILENAME}")
132  else()
133    configure_file("${OPTION_TEMPLATE}"
134                   "${OPTION_FILENAME}"
135                   @ONLY)
136  endif()
137endfunction()
138