Skip to content

Commit

Permalink
Allow config #defines to be written out to custom config/defines.hpp …
Browse files Browse the repository at this point in the history
…files

The existing cmake macro can be used to generate #defines for the global
hpx/config/defines.hpp file by using

  hpx_add_config_define(HPX_HAVE_LOGGING)
  hpx_add_config_define(HPX_HAVE_MAX_CPU_COUNT ${HPX_WITH_MAX_CPU_COUNT})
  ...

and then written to hpx/config/defines.hpp using a new macro and
specifying the default namespace

write_config_defines_file(
  TEMPLATE "${PROJECT_SOURCE_DIR}/cmake/templates/config_defines.hpp.in"
  NAMESPACE default
  FILENAME "${CMAKE_BINARY_DIR}/hpx/config/defines.hpp")

This keeps the existing behaviour unchanged.
However a new NAMESPACE feature is supported to allow the user to add

  hpx_add_config_define_namespace(
    DEFINE    HPX_PARCELPORT_VERBS_DEVICE
    VALUE     "\"${HPX_PARCELPORT_VERBS_DEVICE}\""
    NAMESPACE parcelport_verbs)

  hpx_add_config_define_namespace(
    DEFINE    HPX_PARCELPORT_VERBS_INTERFACE
    VALUE     "\"${HPX_PARCELPORT_VERBS_INTERFACE}\""
    NAMESPACE parcelport_verbs)

and then to generate a defines.hpp file as follows

  write_config_defines_file(
    TEMPLATE ""
    NAMESPACE "parcelport_verbs"
    FILENAME "${CMAKE_BINARY_DIR}/hpx/config/parcelport_verbs_defines.hpp")

when no template is specified, the function will generate a copyright
block, and include guard #ifndef...#endif section with the #defines between.
In the above example, the following would be generated from the variables

  #define HPX_PARCELPORT_VERBS_DEVICE "mlx5_0"
  #define HPX_PARCELPORT_VERBS_INTERFACE "ib0"

This feature allows a module (e.g. parcelport) to generate #defines without
writing to the global defines.hpp file and triggering full rebuilds on every change.
  • Loading branch information
biddisco committed Nov 17, 2016
1 parent ed55f66 commit a63c2f1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 17 deletions.
24 changes: 8 additions & 16 deletions CMakeLists.txt
Expand Up @@ -1654,27 +1654,19 @@ add_plugin_modules()
################################################################################
# Configure the header to include all compile definitions
################################################################################
get_property(HPX_CONFIG_DEFINITIONS_VAR GLOBAL PROPERTY HPX_CONFIG_DEFINITIONS)

list(SORT HPX_CONFIG_DEFINITIONS_VAR)
list(REMOVE_DUPLICATES HPX_CONFIG_DEFINITIONS_VAR)

set(hpx_config_defines "\n")
foreach(def ${HPX_CONFIG_DEFINITIONS_VAR})
set(hpx_config_defines "${hpx_config_defines}#define ${def} ${${def}_define}\n")#"
endforeach()

# Generate a defines.hpp to be used in the build directory ...
set(HPX_DEFINES_PREFIX ${HPX_BUILD_PREFIX})
configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/config_defines.hpp.in"
"${CMAKE_BINARY_DIR}/hpx/config/defines.hpp"
@ONLY)
write_config_defines_file(
TEMPLATE "${PROJECT_SOURCE_DIR}/cmake/templates/config_defines.hpp.in"
NAMESPACE default
FILENAME "${CMAKE_BINARY_DIR}/hpx/config/defines.hpp")

# Generate a defines.hpp to be used in the install directory ...
set(HPX_DEFINES_PREFIX ${HPX_PREFIX})
configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/config_defines.hpp.in"
"${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/hpx/config/defines.hpp"
@ONLY)
write_config_defines_file(
TEMPLATE "${PROJECT_SOURCE_DIR}/cmake/templates/config_defines.hpp.in"
NAMESPACE default
FILENAME "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/hpx/config/defines.hpp")

# Configure hpxrun.py
configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/hpxrun.py.in"
Expand Down
80 changes: 79 additions & 1 deletion cmake/HPX_AddDefinitions.cmake
@@ -1,9 +1,14 @@
# Copyright (c) 2013 Hartmut Kaiser
# Copyright (c) 2014 Thomas Heller
# Copyright (c) 2016 John Biddiscombe
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

# ---------------------------------------------------------------------
# Function to add variables to a list that will be later written out
# to file hpx/config/defines.hpp
# ---------------------------------------------------------------------
# on startup, this is unset, but we'll set it to an empty string anyway
set_property(GLOBAL PROPERTY HPX_CONFIG_DEFINITIONS "")

Expand All @@ -15,6 +20,79 @@ function(hpx_add_config_define definition)
set_property(GLOBAL APPEND PROPERTY HPX_CONFIG_DEFINITIONS "${definition}")
endif()

get_property(HPX_CONFIG_DEFINITIONS_VAR GLOBAL PROPERTY HPX_CONFIG_DEFINITIONS)
endfunction()

# ---------------------------------------------------------------------
# Function to add config defines to a list that depends on a namespace variable
# #defines that match the namespace can later be written out to a file
# ---------------------------------------------------------------------
function(hpx_add_config_define_namespace)
set(options)
set(one_value_args DEFINE NAMESPACE)
set(multi_value_args VALUE)
cmake_parse_arguments(OPTION
"${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})

set(DEF_VAR HPX_CONFIG_DEFINITIONS_${OPTION_NAMESPACE})

# to avoid extra trailing spaces (no value), use an if check
if(OPTION_VALUE)
set_property(GLOBAL APPEND PROPERTY ${DEF_VAR} "${OPTION_DEFINE} ${OPTION_VALUE}")
else()
set_property(GLOBAL APPEND PROPERTY ${DEF_VAR} "${OPTION_DEFINE}")
endif()

endfunction()

# ---------------------------------------------------------------------
# Function to write variables out from a global var that was set using
# the config_define functions into a config file
# ---------------------------------------------------------------------
function(write_config_defines_file)
set(options)
set(one_value_args TEMPLATE NAMESPACE FILENAME)
set(multi_value_args)
cmake_parse_arguments(OPTION
"${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})

if (${OPTION_NAMESPACE} STREQUAL "default")
get_property(DEFINITIONS_VAR GLOBAL PROPERTY
HPX_CONFIG_DEFINITIONS)
else()
get_property(DEFINITIONS_VAR GLOBAL PROPERTY
HPX_CONFIG_DEFINITIONS_${OPTION_NAMESPACE})
endif()

list(SORT DEFINITIONS_VAR)
list(REMOVE_DUPLICATES DEFINITIONS_VAR)

set(hpx_config_defines "\n")
foreach(def ${DEFINITIONS_VAR})
set(hpx_config_defines "${hpx_config_defines}#define ${def} ${${def}_define}\n")#"
endforeach()

# if the user has not specified a template, generate a proper header file
if (NOT OPTION_TEMPLATE)
string(TOUPPER ${OPTION_NAMESPACE} NAMESPACE_UPPER)
set(PREAMBLE
"// Copyright (c) Ste||ar Group\n"
"//\n"
"// Distributed under the Boost Software License, Version 1.0. (See accompanying\n"
"// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\n"
"\n"
"// Do not edit this file! It has been generated by the cmake configuration step.\n"
"\n"
"#ifndef HPX_CONFIG_${NAMESPACE_UPPER}_HPP\n"
"#define HPX_CONFIG_${NAMESPACE_UPPER}_HPP\n"
)
file(WRITE ${OPTION_FILENAME}
${PREAMBLE}
${hpx_config_defines}
"\n#endif\n"
)
else()
configure_file("${OPTION_TEMPLATE}"
"${OPTION_FILENAME}"
@ONLY)
endif()
endfunction()

0 comments on commit a63c2f1

Please sign in to comment.