File: DunePathHelper.cmake

package info (click to toggle)
dune-common 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,112 kB
  • sloc: cpp: 44,714; python: 3,480; sh: 1,590; makefile: 17
file content (103 lines) | stat: -rw-r--r-- 3,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception

# Some helper functions for people developing the CMake build system
# to get quick and easy access to path variables of Dune modules.
#
# .. cmake_function:: dune_module_path
#
#    .. cmake_param:: MODULE
#       :single:
#       :required:
#
#       The name of the module.
#
#    .. cmake_param:: RESULT
#       :single:
#       :required:
#
#       The name of the variable to export the result.
#
#    .. cmake_param:: CMAKE_MODULES
#       :option:
#
#       Set to return the path to cmake modules
#
#    .. cmake_param:: BUILD_DIR
#       :option:
#
#       Set to return the path to the build directory
#
#    .. cmake_param:: SOURCE_DIR
#       :option:
#
#       Set to return the include path of the module
#
#    .. cmake_param:: SCRIPT_DIR
#       :option:
#
#       Set to return the CMake script dir
#
#
#    Returns the specified path of the given module. This differs
#    whether it is called from the actual module, or from a module
#    requiring or suggesting this module. One and only one type of path
#    may be requested.
#
include_guard(GLOBAL)

function(dune_module_path)
  # Parse Arguments
  set(OPTION CMAKE_MODULES BUILD_DIR SOURCE_DIR SCRIPT_DIR)
  set(SINGLE MODULE RESULT)
  cmake_parse_arguments(PATH "${OPTION}" "${SINGLE}" "" ${ARGN})
  if(PATH_UNPARSED_ARGUMENTS)
    message(WARNING "Unparsed arguments in dune_module_path: This often indicates typos!")
  endif()

  # Check whether one and only one path type was set.
  set(OPTION_FOUND 0)
  foreach(opt ${OPTION})
    if(${PATH_${opt}})
      if(OPTION_FOUND)
        message(FATAL_ERROR "Cannot request two different paths from dune_module_path")
      else()
        set(OPTION_FOUND 1)
      endif()
    endif()
  endforeach()
  if(NOT OPTION_FOUND)
    message(FATAL_ERROR "Cannot determine type of requested path!")
  endif()

  # Set the requested paths for the cmake module path
  if(PATH_CMAKE_MODULES)
    set(IF_CURRENT_MOD ${PROJECT_SOURCE_DIR}/cmake/modules)
    set(IF_NOT_CURRENT_MOD ${${PATH_MODULE}_MODULE_PATH})
  endif()

  # Set the requested paths for the cmake script path
  if(PATH_SCRIPT_DIR)
    set(IF_CURRENT_MOD ${PROJECT_SOURCE_DIR}/cmake/scripts)
    set(IF_NOT_CURRENT_MOD ${${PATH_MODULE}_SCRIPT_DIR})
  endif()

  # Set the requested paths for the build directory
  if(PATH_BUILD_DIR)
    set(IF_CURRENT_MOD ${PROJECT_BINARY_DIR})
    set(IF_NOT_CURRENT_MOD ${${PATH_MODULE}_DIR})
  endif()

  # Set the requested paths for the include directory
  if(PATH_SOURCE_DIR)
    set(IF_CURRENT_MOD ${PROJECT_SOURCE_DIR})
    set(IF_NOT_CURRENT_MOD ${${PATH_MODULE}_PREFIX})
  endif()

  # Now set the path in the outer scope!
  if(PROJECT_NAME STREQUAL ${PATH_MODULE})
    set(${PATH_RESULT} ${IF_CURRENT_MOD} PARENT_SCOPE)
  else()
    set(${PATH_RESULT} ${IF_NOT_CURRENT_MOD} PARENT_SCOPE)
  endif()
endfunction()