CheckPublicHeaders.cmake
Go to the documentation of this file.
1 # ============================================================================
2 # Copyright (c) 2011-2012 University of Pennsylvania
3 # Copyright (c) 2013-2016 Andreas Schuh
4 # All rights reserved.
5 #
6 # See COPYING file for license information or visit
7 # https://cmake-basis.github.io/download.html#license
8 # ============================================================================
9 
10 ##############################################################################
11 # @file CheckPublicHeaders.cmake
12 # @brief CMake script used to check whether public headers were added/removed.
13 #
14 # This script removes the deprecated public headers from the build tree and
15 # then throws a fatal error if public header files were added or removed to
16 # the project.
17 #
18 # @ingroup CMakeUtilities
19 ##############################################################################
20 
21 # ----------------------------------------------------------------------------
22 # check arguments
23 if (NOT CMAKE_FILE)
24  message (FATAL_ERROR "Missing argument CMAKE_FILE!")
25 endif ()
26 
27 if (NOT REFERENCE_FILE)
28  message (FATAL_ERROR "Missing argument REFERENCE_FILE!")
29 endif ()
30 
32  message (FATAL_ERROR "Missing argument BINARY_INCLUDE_DIR!")
33 endif ()
34 
36  message (FATAL_ERROR "Missing argument PROJECT_INCLUDE_DIRS!")
37 endif ()
38 
39 if (NOT VARIABLE_NAME)
40  set (VARIABLE_NAME "PUBLIC_HEADERS")
41 endif ()
42 
43 # ----------------------------------------------------------------------------
44 # compare files
45 execute_process (
46  COMMAND ${CMAKE_COMMAND} -E compare_files "${CMAKE_FILE}" "${REFERENCE_FILE}"
47  RESULT_VARIABLE EXIT_CODE
48  OUTPUT_QUIET
49  ERROR_QUIET
50 )
51 
52 if (EXIT_CODE EQUAL 0)
53  set (CMAKE_FILE_DIFFERS FALSE)
54 else ()
55  set (CMAKE_FILE_DIFFERS TRUE)
56 endif ()
57 
58 # ----------------------------------------------------------------------------
59 # remove obsolete public headers
61  unset (${VARIABLE_NAME})
62  include ("${CMAKE_FILE}")
63  set (_HEADERS "${${VARIABLE_NAME}}")
64  unset (${VARIABLE_NAME})
65  include ("${REFERENCE_FILE}")
66  foreach (H IN LISTS _HEADERS)
67  list (FIND ${VARIABLE_NAME} "${H}" IDX)
68  if (IDX EQUAL -1)
69  # TODO: this hard coded /include path may break for custom include directories
70  string (REGEX REPLACE "^.*/include/" "" H "${H}")
71  string (REGEX REPLACE "\\.in$" "" H "${H}")
72  file (REMOVE "${BINARY_INCLUDE_DIR}/${H}")
73  endif ()
74  endforeach ()
75 endif ()
76 
77 # ----------------------------------------------------------------------------
78 # remove files if different
79 if (CMAKE_FILE_DIFFERS)
80  file (REMOVE "${CMAKE_FILE}")
81  file (REMOVE "${REFERENCE_FILE}")
82  file (REMOVE "${REFERENCE_FILE}.update")
83 endif ()
84 
85 # ----------------------------------------------------------------------------
86 # fatal error if files added/removed
87 if (CMAKE_FILE_DIFFERS AND ERRORMSG)
88  message (FATAL_ERROR "${ERRORMSG}")
89 endif ()
cmake _HEADERS
cmake CMAKE_FILE_DIFFERS
cmake PROJECT_INCLUDE_DIRS
Absolute paths to directories of public header files in source tree.
cmake BINARY_INCLUDE_DIR
Absolute path to output directory for configured public header files.
cmake COMMAND
cmake VARIABLE_NAME
if(oldcoutbuf)