-
Notifications
You must be signed in to change notification settings - Fork 16
/
CheckPublicHeaders.cmake
89 lines (78 loc) · 2.84 KB
/
CheckPublicHeaders.cmake
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
# ============================================================================
# Copyright (c) 2011-2012 University of Pennsylvania
# Copyright (c) 2013-2014 Andreas Schuh
# All rights reserved.
#
# See COPYING file for license information or visit
# http://opensource.andreasschuh.com/cmake-basis/download.html#license
# ============================================================================
##############################################################################
# @file CheckPublicHeaders.cmake
# @brief CMake script used to check whether public headers were added/removed.
#
# This script removes the deprecated public headers from the build tree and
# then throws a fatal error if public header files were added or removed to
# the project.
#
# @ingroup CMakeUtilities
##############################################################################
# ----------------------------------------------------------------------------
# check arguments
if (NOT CMAKE_FILE)
message (FATAL_ERROR "Missing argument CMAKE_FILE!")
endif ()
if (NOT REFERENCE_FILE)
message (FATAL_ERROR "Missing argument REFERENCE_FILE!")
endif ()
if (NOT BINARY_INCLUDE_DIR)
message (FATAL_ERROR "Missing argument BINARY_INCLUDE_DIR!")
endif ()
if (NOT PROJECT_INCLUDE_DIRS)
message (FATAL_ERROR "Missing argument PROJECT_INCLUDE_DIRS!")
endif ()
if (NOT VARIABLE_NAME)
set (VARIABLE_NAME "PUBLIC_HEADERS")
endif ()
# ----------------------------------------------------------------------------
# compare files
execute_process (
COMMAND ${CMAKE_COMMAND} -E compare_files "${CMAKE_FILE}" "${REFERENCE_FILE}"
RESULT_VARIABLE EXIT_CODE
OUTPUT_QUIET
ERROR_QUIET
)
if (EXIT_CODE EQUAL 0)
set (CMAKE_FILE_DIFFERS FALSE)
else ()
set (CMAKE_FILE_DIFFERS TRUE)
endif ()
# ----------------------------------------------------------------------------
# remove obsolete public headers
if (CMAKE_FILE_DIFFERS)
unset (${VARIABLE_NAME})
include ("${CMAKE_FILE}")
set (_HEADERS "${${VARIABLE_NAME}}")
unset (${VARIABLE_NAME})
include ("${REFERENCE_FILE}")
foreach (H IN LISTS _HEADERS)
list (FIND ${VARIABLE_NAME} "${H}" IDX)
if (IDX EQUAL -1)
# TODO: this hard coded /include path may break for custom include directories
string (REGEX REPLACE "^.*/include/" "" H "${H}")
string (REGEX REPLACE "\\.in$" "" H "${H}")
file (REMOVE "${BINARY_INCLUDE_DIR}/${H}")
endif ()
endforeach ()
endif ()
# ----------------------------------------------------------------------------
# remove files if different
if (CMAKE_FILE_DIFFERS)
file (REMOVE "${CMAKE_FILE}")
file (REMOVE "${REFERENCE_FILE}")
file (REMOVE "${REFERENCE_FILE}.update")
endif ()
# ----------------------------------------------------------------------------
# fatal error if files added/removed
if (CMAKE_FILE_DIFFERS AND ERRORMSG)
message (FATAL_ERROR "${ERRORMSG}")
endif ()