Skip to content

Commit ecf54a3

Browse files
committed
cmake: Add support for builtin codegen target
Additionally, this change removes unnecessary braces in the `if()` command for improved robustness, readability and consistency with CMake guidelines.
1 parent a8c78a0 commit ecf54a3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
# - CMake 3.26.5, https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/
1010
cmake_minimum_required(VERSION 3.22)
1111

12-
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
12+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
1313
message(FATAL_ERROR "In-source builds are not allowed.")
1414
endif()
1515

16+
if(POLICY CMP0171)
17+
# `codegen` is a reserved target name.
18+
# See: https://cmake.org/cmake/help/latest/policy/CMP0171.html
19+
cmake_policy(SET CMP0171 NEW)
20+
endif()
21+
1622
#=============================
1723
# Project / Package metadata
1824
#=============================

cmake/module/TargetDataSources.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ macro(set_add_custom_command_options)
77
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
88
set(DEPENDS_EXPLICIT_OPT DEPENDS_EXPLICIT_ONLY)
99
endif()
10+
set(CODEGEN_OPT "")
11+
if(POLICY CMP0171)
12+
cmake_policy(GET CMP0171 _cmp0171_status)
13+
if(_cmp0171_status STREQUAL "NEW")
14+
set(CODEGEN_OPT CODEGEN)
15+
endif()
16+
unset(_cmp0171_status)
17+
endif()
1018
endmacro()
1119

1220
# Specifies JSON data files to be processed into corresponding
@@ -20,6 +28,7 @@ function(target_json_data_sources target)
2028
COMMAND ${CMAKE_COMMAND} -DJSON_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${json_file} -DHEADER_PATH=${header} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake
2129
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${json_file} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake
2230
VERBATIM
31+
${CODEGEN_OPT}
2332
${DEPENDS_EXPLICIT_OPT}
2433
)
2534
target_sources(${target} PRIVATE ${header})
@@ -38,6 +47,7 @@ function(target_raw_data_sources target)
3847
COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_file} -DHEADER_PATH=${header} -DRAW_NAMESPACE=${__NAMESPACE} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
3948
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_file} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
4049
VERBATIM
50+
${CODEGEN_OPT}
4151
${DEPENDS_EXPLICIT_OPT}
4252
)
4353
target_sources(${target} PRIVATE ${header})

0 commit comments

Comments
 (0)