Skip to content

Commit

Permalink
Allow for autogen bypass, partial fix for #264 #252 #207, ref #303
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
  • Loading branch information
timkpaine committed Jul 19, 2024
1 parent 4a0a80c commit 460fe5a
Show file tree
Hide file tree
Showing 7 changed files with 758 additions and 51 deletions.
34 changes: 21 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,34 @@ function(csp_autogen MODULE_NAME DEST_FILENAME HEADER_NAME_OUTVAR SOURCE_NAME_OU

cmake_path(SET CSP_AUTOGEN_MODULE_PATH NORMALIZE "${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py")
cmake_path(SET CSP_AUTOGEN_DESTINATION_FOLDER NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen")
cmake_path(SET CSP_AUTOTGEN_CPP_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOTGEN_H_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h")
cmake_path(SET CSP_AUTOGEN_CPP_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOGEN_CPP_MAYBE_EXISTING NORMALIZE "${CMAKE_CURRENT_SOURCE_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOGEN_H_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h")
cmake_path(SET CSP_AUTOGEN_H_MAYBE_EXISTING NORMALIZE "${CMAKE_CURRENT_SOURCE_DIR}/csp_autogen/${DEST_FILENAME}.h")

if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE};${CMAKE_SOURCE_DIR};%PYTHONPATH% )
else()
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib:${CMAKE_SOURCE_DIR}:$$PYTHONPATH )
endif()

add_custom_command(OUTPUT "${CSP_AUTOTGEN_CPP_OUT}" "${CSP_AUTOTGEN_H_OUT}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CSP_AUTOGEN_PYTHONPATH}" ${Python_EXECUTABLE} ${CSP_AUTOGEN_MODULE_PATH} -m ${MODULE_NAME} -d ${CSP_AUTOGEN_DESTINATION_FOLDER} -o ${DEST_FILENAME} ${CSP_AUTOGEN_EXTRA_ARGS}
COMMENT "generating csp c++ types from module ${MODULE_NAME}"
DEPENDS mkdir_autogen_${MODULE_NAME}
${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py
${CMAKE_SOURCE_DIR}/${MODULE_FILENAME}
csptypesimpl
)

set(${SOURCE_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h" PARENT_SCOPE )
if (EXISTS "${CSP_AUTOGEN_CPP_MAYBE_EXISTING}" AND EXISTS "${CSP_AUTOGEN_H_MAYBE_EXISTING}")
# Files exist in-source
set(${SOURCE_NAME_OUTVAR} "${CSP_AUTOGEN_CPP_MAYBE_EXISTING}" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CSP_AUTOGEN_H_MAYBE_EXISTING}" PARENT_SCOPE )
else()
add_custom_command(OUTPUT "${CSP_AUTOGEN_CPP_OUT}" "${CSP_AUTOGEN_H_OUT}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CSP_AUTOGEN_PYTHONPATH}" ${Python_EXECUTABLE} ${CSP_AUTOGEN_MODULE_PATH} -m ${MODULE_NAME} -d ${CSP_AUTOGEN_DESTINATION_FOLDER} -o ${DEST_FILENAME} ${CSP_AUTOGEN_EXTRA_ARGS}
COMMENT "generating csp c++ types from module ${MODULE_NAME}"
DEPENDS mkdir_autogen_${MODULE_NAME}
${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py
${CMAKE_SOURCE_DIR}/${MODULE_FILENAME}
csptypesimpl
)

set(${SOURCE_NAME_OUTVAR} "${CSP_AUTOGEN_CPP_OUT}" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CSP_AUTOGEN_H_OUT}" PARENT_SCOPE )
endif()
endfunction()


Expand Down
32 changes: 20 additions & 12 deletions cpp/cmake/modules/Findcsp_autogen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ function(csp_autogen MODULE_NAME DEST_FILENAME HEADER_NAME_OUTVAR SOURCE_NAME_OU

cmake_path(SET CSP_AUTOGEN_MODULE_PATH NORMALIZE "${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py")
cmake_path(SET CSP_AUTOGEN_DESTINATION_FOLDER NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen")
cmake_path(SET CSP_AUTOTGEN_CPP_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOTGEN_H_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h")
cmake_path(SET CSP_AUTOGEN_CPP_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOGEN_CPP_MAYBE_EXISTING NORMALIZE "${CMAKE_CURRENT_SOURCE_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOGEN_H_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h")
cmake_path(SET CSP_AUTOGEN_H_MAYBE_EXISTING NORMALIZE "${CMAKE_CURRENT_SOURCE_DIR}/csp_autogen/${DEST_FILENAME}.h")

if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE};${CMAKE_SOURCE_DIR};%PYTHONPATH% )
else()
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib:${CMAKE_SOURCE_DIR}:$$PYTHONPATH )
endif()

add_custom_command(OUTPUT "${CSP_AUTOTGEN_CPP_OUT}" "${CSP_AUTOTGEN_H_OUT}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CSP_AUTOGEN_PYTHONPATH}" ${Python_EXECUTABLE} ${CSP_AUTOGEN_MODULE_PATH} -m ${MODULE_NAME} -d ${CSP_AUTOGEN_DESTINATION_FOLDER} -o ${DEST_FILENAME} ${CSP_AUTOGEN_EXTRA_ARGS}
COMMENT "generating csp c++ types from module ${MODULE_NAME}"
DEPENDS mkdir_autogen_${MODULE_NAME}
${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py
${CMAKE_SOURCE_DIR}/${MODULE_FILENAME}
csptypesimpl
)
if (EXISTS "${CSP_AUTOGEN_CPP_MAYBE_EXISTING}" AND EXISTS "${CSP_AUTOGEN_H_MAYBE_EXISTING}")
# Files exist in-source
set(${SOURCE_NAME_OUTVAR} "${CSP_AUTOGEN_CPP_MAYBE_EXISTING}" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CSP_AUTOGEN_H_MAYBE_EXISTING}" PARENT_SCOPE )
else()
add_custom_command(OUTPUT "${CSP_AUTOGEN_CPP_OUT}" "${CSP_AUTOGEN_H_OUT}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CSP_AUTOGEN_PYTHONPATH}" ${Python_EXECUTABLE} ${CSP_AUTOGEN_MODULE_PATH} -m ${MODULE_NAME} -d ${CSP_AUTOGEN_DESTINATION_FOLDER} -o ${DEST_FILENAME} ${CSP_AUTOGEN_EXTRA_ARGS}
COMMENT "generating csp c++ types from module ${MODULE_NAME}"
DEPENDS mkdir_autogen_${MODULE_NAME}
${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py
${CMAKE_SOURCE_DIR}/${MODULE_FILENAME}
csptypesimpl
)

set(${SOURCE_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h" PARENT_SCOPE )
set(${SOURCE_NAME_OUTVAR} "${CSP_AUTOGEN_CPP_OUT}" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CSP_AUTOGEN_H_OUT}" PARENT_SCOPE )
endif()
endfunction()
87 changes: 87 additions & 0 deletions cpp/csp/adapters/websocket/csp_autogen/websocket_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

// AUTOGENERATED BY CSP_AUTOGEN
// DO NOT MODIFY DIRECTLY
// command: python csp/build/csp_autogen.py -m csp.adapters.websocket_types -d cpp/csp/adapters/websocket/csp_autogen/ -o websocket_types

#include "websocket_types.h"
#include <csp/python/Common.h>
#include <csp/python/PyStruct.h>
#include <csp/python/PyCspEnum.h>
#include <iostream>
#include <stdlib.h>
#include <Python.h>

namespace csp::autogen
{

#define _offsetof( C, M ) ( ( char * ) &( ( C * ) nullptr ) -> M - ( char * ) 0 )

static void assert_or_die( bool assertion, const char * error )
{
if( !assertion )
{
std::cerr << "Fatal error on import of " << __FILE__ << ": " << error << std::endl;
if( PyErr_Occurred() )
PyErr_Print();
abort();
}
}


bool WebsocketStatus::static_init()
{
if( Py_IsInitialized() )
{
csp::python::AcquireGIL gil;

// initialize EnumMeta from python type if we're in python
PyObject * pymodule = PyImport_ImportModule( "csp.adapters.websocket_types" );
assert_or_die( pymodule != nullptr, "failed to import struct module csp.adapters.websocket_types" );

PyObject * enumType = PyObject_GetAttrString(pymodule, "WebsocketStatus" );
assert_or_die( enumType != nullptr, "failed to find num type WebsocketStatus in module csp.adapters.websocket_types" );

// should add some assertion here..
csp::python::PyCspEnumMeta * pymeta = ( csp::python::PyCspEnumMeta * ) enumType;
s_meta = pymeta -> enumMeta;
}

return true;
}

bool static_init_WebsocketStatus = WebsocketStatus::static_init();
std::shared_ptr<csp::CspEnumMeta> WebsocketStatus::s_meta;
WebsocketStatus WebsocketStatus::ACTIVE = WebsocketStatus::create("ACTIVE");
WebsocketStatus WebsocketStatus::GENERIC_ERROR = WebsocketStatus::create("GENERIC_ERROR");
WebsocketStatus WebsocketStatus::CONNECTION_FAILED = WebsocketStatus::create("CONNECTION_FAILED");
WebsocketStatus WebsocketStatus::CLOSED = WebsocketStatus::create("CLOSED");
WebsocketStatus WebsocketStatus::MESSAGE_SEND_FAIL = WebsocketStatus::create("MESSAGE_SEND_FAIL");


bool WebsocketHeaderUpdate::static_init()
{

if( Py_IsInitialized() )
{
//Note that windows requires we grab the GIL since the windows DLL loading code releases GIL
csp::python::AcquireGIL gil;

// initialize StructMeta from python type if we're in python
PyObject * pymodule = PyImport_ImportModule( "csp.adapters.websocket_types" );
assert_or_die( pymodule != nullptr, "failed to import struct module csp.adapters.websocket_types" );

PyObject * structType = PyObject_GetAttrString(pymodule, "WebsocketHeaderUpdate" );
assert_or_die( structType != nullptr, "failed to find struct type WebsocketHeaderUpdate in module csp.adapters.websocket_types" );

// should add some assertion here..
csp::python::PyStructMeta * pymeta = ( csp::python::PyStructMeta * ) structType;
s_meta = pymeta -> structMeta;
}

return true;
}

bool static_init_WebsocketHeaderUpdate = WebsocketHeaderUpdate::static_init();
csp::StructMetaPtr WebsocketHeaderUpdate::s_meta;

}
Loading

0 comments on commit 460fe5a

Please sign in to comment.