-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGeneration.cmake
More file actions
55 lines (48 loc) · 2.94 KB
/
CodeGeneration.cmake
File metadata and controls
55 lines (48 loc) · 2.94 KB
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
# This file contains functions used by CMakeUnitFramework to generate code.
# If set, added as prefix to export API macros. Useful for injection of custom pragma statements.
set (UNIT_FRAMEWORK_API_MACRO_EXPORT_PREFIX "")
# Writes given content to given file unless it already contains the same content.
function (file_write_if_not_equal TARGET_FILE CONTENT)
set (CURRENT_CONTENT)
if (EXISTS "${TARGET_FILE}")
file (READ "${TARGET_FILE}" CURRENT_CONTENT)
endif ()
if (NOT CONTENT STREQUAL CURRENT_CONTENT)
file (WRITE "${TARGET_FILE}" "${CONTENT}")
endif ()
endfunction ()
# Generic utility function for generating API headers for Windows dllexport/dllimport support.
# Arguments:
# - API_MACRO: name of the macro that will be used as API declaration macro.
# - EXPORT_MACRO: name of the macro that is only added to targets that export definitions of declared API.
# - OUTPUT_FILE: path to the output file.
function (generate_api_header)
cmake_parse_arguments (GENERATE "" "API_MACRO;EXPORT_MACRO;OUTPUT_FILE" "" ${ARGV})
if (DEFINED GENERATE_UNPARSED_ARGUMENTS OR
NOT DEFINED GENERATE_API_MACRO OR
NOT DEFINED GENERATE_EXPORT_MACRO OR
NOT DEFINED GENERATE_OUTPUT_FILE)
message (FATAL_ERROR "Incorrect function arguments!")
endif ()
set (CONTENT)
string (APPEND CONTENT "// AUTOGENERATED BY BUILD SYSTEM, DO NOT MODIFY.\n\n")
string (APPEND CONTENT "#pragma once\n\n")
string (APPEND CONTENT "#if defined(_MSC_VER)\n")
string (APPEND CONTENT "# if defined(${GENERATE_EXPORT_MACRO})\n")
string (APPEND CONTENT "// NOLINTNEXTLINE(readability-identifier-naming): API macro is named the same way as target for better readability.\n")
string (APPEND CONTENT "# define ${GENERATE_API_MACRO} ${UNIT_FRAMEWORK_API_MACRO_EXPORT_PREFIX} __declspec(dllexport)\n")
string (APPEND CONTENT "# else\n")
string (APPEND CONTENT "// NOLINTNEXTLINE(readability-identifier-naming): API macro is named the same way as target for better readability.\n")
string (APPEND CONTENT "# define ${GENERATE_API_MACRO} __declspec(dllimport)\n")
string (APPEND CONTENT "# endif\n")
string (APPEND CONTENT "#else\n")
string (APPEND CONTENT "# if defined(${GENERATE_EXPORT_MACRO})\n")
string (APPEND CONTENT "// NOLINTNEXTLINE(readability-identifier-naming): API macro is named the same way as target for better readability.\n")
string (APPEND CONTENT "# define ${GENERATE_API_MACRO} ${UNIT_FRAMEWORK_API_MACRO_EXPORT_PREFIX}\n")
string (APPEND CONTENT "# else\n")
string (APPEND CONTENT "// NOLINTNEXTLINE(readability-identifier-naming): API macro is named the same way as target for better readability.\n")
string (APPEND CONTENT "# define ${GENERATE_API_MACRO}\n")
string (APPEND CONTENT "# endif\n")
string (APPEND CONTENT "#endif\n")
file_write_if_not_equal ("${GENERATE_OUTPUT_FILE}" "${CONTENT}")
endfunction ()