@@ -11,6 +11,20 @@ set(CMAKE_CXX_EXTENSIONS OFF)
1111set (CMAKE_CXX_MODULE_STD 26)
1212set (CMAKE_CXX_MODULE_EXTENSIONS OFF )
1313
14+ option (CPP_CORE_ENABLE_AST_EXPORT "Enable clang-based JSON AST export for the FFI headers" ON )
15+ set (
16+ CPP_CORE_AST_JSON_OUTPUT
17+ "${CMAKE_BINARY_DIR } /ast/cpp_core_ffi_ast.json"
18+ CACHE FILEPATH
19+ "Output path for the generated FFI AST JSON file"
20+ )
21+ set (
22+ CPP_CORE_AST_CLANGXX
23+ ""
24+ CACHE FILEPATH
25+ "Path to the clang++ executable used for AST JSON export"
26+ )
27+
1428if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
1529 message (FATAL_ERROR "cpp-core currently requires GCC 16.1+ with the GCC std::meta reflection implementation." )
1630endif ()
@@ -77,6 +91,64 @@ if(BUILD_TESTING)
7791 target_link_libraries (cpp_core_compile_tests PRIVATE cpp_core_strict_warnings )
7892endif ()
7993
94+ if (CPP_CORE_ENABLE_AST_EXPORT)
95+ file (
96+ GLOB _cpp_core_ast_interface_headers
97+ CONFIGURE_DEPENDS
98+ "${CMAKE_CURRENT_SOURCE_DIR } /include/cpp_core/interface/*.h"
99+ )
100+
101+ set (
102+ _cpp_core_ast_headers
103+ "${CMAKE_CURRENT_SOURCE_DIR } /include/cpp_core/serial.h"
104+ "${CMAKE_CURRENT_SOURCE_DIR } /include/cpp_core/error_callback.h"
105+ "${CMAKE_CURRENT_SOURCE_DIR } /include/cpp_core/module_api.h"
106+ "${CMAKE_CURRENT_SOURCE_DIR } /include/cpp_core/version.hpp"
107+ ${_cpp_core_ast_interface_headers}
108+ )
109+
110+ if (CPP_CORE_AST_CLANGXX)
111+ set (_cpp_core_ast_clangxx "${CPP_CORE_AST_CLANGXX} " )
112+ else ()
113+ find_program (_cpp_core_ast_clangxx NAMES clang++ )
114+ endif ()
115+
116+ if (NOT _cpp_core_ast_clangxx)
117+ message (
118+ FATAL_ERROR
119+ "CPP_CORE_ENABLE_AST_EXPORT=ON requires clang++. "
120+ "Install clang++ or set CPP_CORE_AST_CLANGXX to an explicit path."
121+ )
122+ endif ()
123+
124+ set (_cpp_core_ast_wrapper "${CMAKE_CURRENT_BINARY_DIR } /ast/cpp_core_ffi_ast.cpp" )
125+ get_filename_component (_cpp_core_ast_json_dir "${CPP_CORE_AST_JSON_OUTPUT} " DIRECTORY )
126+
127+ file (
128+ GENERATE
129+ OUTPUT "${_cpp_core_ast_wrapper} "
130+ CONTENT "#include <cpp_core/serial.h>\n "
131+ )
132+
133+ add_custom_command (
134+ OUTPUT "${CPP_CORE_AST_JSON_OUTPUT} "
135+ COMMAND ${CMAKE_COMMAND } -E make_directory "${_cpp_core_ast_json_dir} "
136+ COMMAND
137+ ${CMAKE_COMMAND }
138+ -DCPP_CORE_AST_CLANGXX=${_cpp_core_ast_clangxx}
139+ -DCPP_CORE_AST_INCLUDE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/include
140+ -DCPP_CORE_AST_OUTPUT=${CPP_CORE_AST_JSON_OUTPUT}
141+ -DCPP_CORE_AST_SOURCE=${_cpp_core_ast_wrapper}
142+ -DCPP_CORE_AST_STANDARD=${CMAKE_CXX_STANDARD}
143+ -P ${CMAKE_CURRENT_SOURCE_DIR } /cmake/export_ast_json.cmake
144+ DEPENDS "${_cpp_core_ast_wrapper} " ${_cpp_core_ast_headers}
145+ COMMENT "Exporting FFI header AST JSON with clang++"
146+ VERBATIM
147+ )
148+
149+ add_custom_target (cpp_core_ast_json DEPENDS "${CPP_CORE_AST_JSON_OUTPUT} " )
150+ endif ()
151+
80152# Install rules --------------------------------------------------------------
81153include (GNUInstallDirs )
82154
0 commit comments