From 3fc7ec72d38a8b7487bf962cd164ca8267b222c2 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Mon, 2 Mar 2026 22:39:54 -0400 Subject: [PATCH] openai_gen_model now depends on db_gen_cpp_headers --- cmake/CppBeSSOT.cmake | 24 +++++------ cmake/dbGenCpp.cmake | 2 + cmake/dbGenerationCommon.cmake | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/cmake/CppBeSSOT.cmake b/cmake/CppBeSSOT.cmake index 8705798..1d15517 100644 --- a/cmake/CppBeSSOT.cmake +++ b/cmake/CppBeSSOT.cmake @@ -76,21 +76,19 @@ function(cppbessot_add_generated_libraries) cppbessot_get_schema_dir_path(_version_dir "${CPPB_SCHEMA_DIR}") set(_cpp_include_dir "${_version_dir}/generated-cpp-source/include") + cppbessot_get_expected_cpp_model_outputs(_expected_model_headers _expected_model_sources "${CPPB_SCHEMA_DIR}") file(GLOB _model_include_dirs LIST_DIRECTORIES true "${_cpp_include_dir}/*/model") - file(GLOB _model_sources CONFIGURE_DEPENDS - "${_version_dir}/generated-cpp-source/src/model/*.cpp") - if(_model_sources) - add_library(cppBeSsotOpenAiModelGen STATIC ${_model_sources}) - set_target_properties(cppBeSsotOpenAiModelGen PROPERTIES - OUTPUT_NAME "cppBeSsotOpenAiModelGen" - POSITION_INDEPENDENT_CODE ON) - target_include_directories(cppBeSsotOpenAiModelGen PUBLIC "${_cpp_include_dir}") - _cppbessot_try_link_nlohmann(cppBeSsotOpenAiModelGen) - add_library(cppbessot::openai_model_gen ALIAS cppBeSsotOpenAiModelGen) - else() - message(WARNING "No generated C++ model sources found for ${CPPB_SCHEMA_DIR}; skipping libcppBeSsotOpenAiModelGen.") - endif() + set_source_files_properties(${_expected_model_headers} ${_expected_model_sources} + PROPERTIES GENERATED TRUE) + add_library(cppBeSsotOpenAiModelGen STATIC ${_expected_model_sources}) + add_dependencies(cppBeSsotOpenAiModelGen db_gen_cpp_headers) + set_target_properties(cppBeSsotOpenAiModelGen PROPERTIES + OUTPUT_NAME "cppBeSsotOpenAiModelGen" + POSITION_INDEPENDENT_CODE ON) + target_include_directories(cppBeSsotOpenAiModelGen PUBLIC "${_cpp_include_dir}") + _cppbessot_try_link_nlohmann(cppBeSsotOpenAiModelGen) + add_library(cppbessot::openai_model_gen ALIAS cppBeSsotOpenAiModelGen) file(GLOB _sqlite_odb_sources CONFIGURE_DEPENDS "${_version_dir}/generated-odb-source/sqlite/*-odb.cxx") diff --git a/cmake/dbGenCpp.cmake b/cmake/dbGenCpp.cmake index 57ae49e..7410f97 100644 --- a/cmake/dbGenCpp.cmake +++ b/cmake/dbGenCpp.cmake @@ -23,6 +23,7 @@ function(cppbessot_add_db_gen_cpp_target schema_dir) set(_template_dir "${_module_root}/openapi/templates/cpp-odb-json") set(_template_config "${_template_dir}/config.yaml") set(_output_dir "${_version_dir}/generated-cpp-source") + file(GLOB_RECURSE _template_inputs CONFIGURE_DEPENDS "${_template_dir}/*") add_custom_target(db_gen_cpp_headers COMMAND ${CMAKE_COMMAND} -E make_directory "${_output_dir}" @@ -33,6 +34,7 @@ function(cppbessot_add_db_gen_cpp_target schema_dir) -c "${_template_config}" -o "${_output_dir}" --global-property models + DEPENDS "${_openapi_file}" ${_template_inputs} COMMENT "Generating C++ model headers/sources for ${schema_dir}" VERBATIM ) diff --git a/cmake/dbGenerationCommon.cmake b/cmake/dbGenerationCommon.cmake index 184fd73..1061e71 100644 --- a/cmake/dbGenerationCommon.cmake +++ b/cmake/dbGenerationCommon.cmake @@ -102,3 +102,76 @@ function(cppbessot_get_model_headers_glob out_var schema_dir) cppbessot_get_schema_dir_path(_schema_dir_path "${schema_dir}") set(${out_var} "${_schema_dir_path}/generated-cpp-source/include/*/model/*.h" PARENT_SCOPE) endfunction() + +function(cppbessot_get_openapi_schema_names out_var schema_dir) + # Purpose: Parse top-level component schema names from a schema directory's OpenAPI file. + # Inputs: + # - out_var: Parent-scope variable name to receive the schema names. + # - schema_dir: Schema directory basename. + # Outputs: + # - (PARENT_SCOPE): List of top-level component schema names. + cppbessot_get_schema_dir_path(_schema_dir_path "${schema_dir}") + set(_openapi_file "${_schema_dir_path}/openapi/openapi.yaml") + if(NOT EXISTS "${_openapi_file}") + message(FATAL_ERROR "OpenAPI file does not exist: ${_openapi_file}") + endif() + + file(STRINGS "${_openapi_file}" _openapi_lines) + set(_schema_names) + set(_in_components FALSE) + set(_in_schemas FALSE) + + foreach(_line IN LISTS _openapi_lines) + if(_in_schemas) + if(_line MATCHES "^[^ ]" OR _line MATCHES "^ [^ ]") + set(_in_schemas FALSE) + elseif(_line MATCHES "^ ([A-Za-z_][A-Za-z0-9_]*)[ \t]*:[ \t]*$") + list(APPEND _schema_names "${CMAKE_MATCH_1}") + endif() + endif() + + if(_in_components AND NOT _in_schemas) + if(_line MATCHES "^[^ ]") + set(_in_components FALSE) + elseif(_line MATCHES "^ schemas:[ \t]*$") + set(_in_schemas TRUE) + endif() + endif() + + if(_line MATCHES "^components:[ \t]*$") + set(_in_components TRUE) + endif() + endforeach() + + if(NOT _schema_names) + message(FATAL_ERROR + "No component schema names were found in ${_openapi_file}.") + endif() + + set(${out_var} "${_schema_names}" PARENT_SCOPE) +endfunction() + +function(cppbessot_get_expected_cpp_model_outputs out_headers_var out_sources_var schema_dir) + # Purpose: Infer generated C++ model headers and sources from OpenAPI schema names. + # Inputs: + # - out_headers_var: Parent-scope variable name to receive expected headers. + # - out_sources_var: Parent-scope variable name to receive expected sources. + # - schema_dir: Schema directory basename. + # Outputs: + # - (PARENT_SCOPE): Expected generated model headers. + # - (PARENT_SCOPE): Expected generated model sources. + cppbessot_get_schema_dir_path(_schema_dir_path "${schema_dir}") + cppbessot_get_openapi_schema_names(_schema_names "${schema_dir}") + + set(_headers) + set(_sources) + foreach(_schema_name IN LISTS _schema_names) + list(APPEND _headers + "${_schema_dir_path}/generated-cpp-source/include/cppbessot/model/${_schema_name}.h") + list(APPEND _sources + "${_schema_dir_path}/generated-cpp-source/src/model/${_schema_name}.cpp") + endforeach() + + set(${out_headers_var} "${_headers}" PARENT_SCOPE) + set(${out_sources_var} "${_sources}" PARENT_SCOPE) +endfunction()