aboutsummaryrefslogtreecommitdiff
path: root/modules/ECMGenerateHeaders.cmake
blob: 7fac164925121054cef406853b37e0e54d3ca839 (plain)
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
# - Function to generate forward includes for your project
# This module provides the function ECM_GENERATE_HEADERS
#
# The ECM_GENERATE_HEADERS function is used to generate C/C++ forwarding header
# files for your project.
#
# To be more precise, there will be two kinds of headers being generated. Lowercase
# and CamelCase, for convenience. That means that for a class named "ClassA" in a
# module called "Module" we will get 2 different files: Module/ClassA and
# module/classa.h. Both these files will be including a "classa.h" file that is
# expected to be in the headers dir (see HEADERS_DIR argument below).
#
# ECM_GENERATE_HEADERS( ClassA ClassB ...
#     [MODULE_NAME name]
#     [OUTPUT_DIR path]
#     [REQUIRED_HEADERS variable])
#
# The MODULE_NAME argument is used to provide information about where the
# directories will be generated. By default, PROJECT_NAME will be used in both
# CamelCase and lowercase version.
#
# The OUTPUT_DIR argument specifies where the files will be generated; this
# should be within the build directory. By default, CMAKE_CURRENT_BINARY_DIR
# will be used.
#
# The REQUIRED_HEADERS argument will receive an output variable name where all
# the required headers will be appended so that they can be installed together
# with the generated ones.
#
# The RELATIVE argument will specify where are the original headers from the
# current directory.
#
#
# Copyright (c) 2013, Aleix Pol Gonzalez <aleixpol@blue-systems.com>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#

include(CMakeParseArguments)

function(ECM_GENERATE_HEADERS)
    set(oneValueArgs MODULE_NAME OUTPUT_DIR REQUIRED_HEADERS RELATIVE)
    cmake_parse_arguments(EGH "" "${oneValueArgs}" "" ${ARGN})
    if(NOT EGH_MODULE_NAME)
        set(EGH_MODULE_NAME ${PROJECT_NAME})
    endif()

    if(NOT EGH_OUTPUT_DIR)
        set(EGH_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
    endif()

    string(TOLOWER ${EGH_MODULE_NAME} lowercasemodule)
    foreach(_CLASSNAME ${EGH_UNPARSED_ARGUMENTS})
        string(TOLOWER ${_CLASSNAME} lowercaseclassname)
        set(REGULAR_HEADER_NAME ${EGH_OUTPUT_DIR}/${lowercasemodule}/${lowercaseclassname}.h)
        set(FANCY_HEADER_NAME ${EGH_OUTPUT_DIR}/${EGH_MODULE_NAME}/${_CLASSNAME})
        set(_actualheader "${CMAKE_CURRENT_SOURCE_DIR}/${EGH_RELATIVE}/${lowercaseclassname}.h")
        if (NOT EXISTS ${_actualheader})
            message(FATAL_ERROR "Could not find \"${_actualheader}\"")
        endif()
        if (NOT EXISTS ${REGULAR_HEADER_NAME})
            file(WRITE ${REGULAR_HEADER_NAME} "#include \"${_actualheader}\"\n")
        endif()
        if (NOT EXISTS ${FANCY_HEADER_NAME})
            file(WRITE ${FANCY_HEADER_NAME} "#include \"${lowercasemodule}/${lowercaseclassname}.h\"\n")
        endif()
        list(APPEND REQUIRED_HEADERS "${_actualheader}")
    endforeach()
    if (NOT EGH_REQUIRED_HEADERS STREQUAL "")
        set(${EGH_REQUIRED_HEADERS} ${${EGH_REQUIRED_HEADERS}} ${REQUIRED_HEADERS} PARENT_SCOPE)
    endif ()
endfunction()