Skip to content

Commit

Permalink
Prametrize framework namespace name
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaszm committed Jun 18, 2024
1 parent cdea8f7 commit ca53c78
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 35 deletions.
14 changes: 13 additions & 1 deletion JsonGenerator/source/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
DOC_ISSUES = True
DEFAULT_DEFINITIONS_FILE = "../../ProxyStubGenerator/default.h"
FRAMEWORK_NAMESPACE = "Thunder"
INTERFACE_NAMESPACES = ["::Thunder::Exchange::JSONRPC", "::Thunder::Exchange"]
INTERFACE_NAMESPACES = ["::%s::Exchange::JSONRPC" % FRAMEWORK_NAMESPACE, "::%s::Exchange" % FRAMEWORK_NAMESPACE]
INTERFACES_SECTION = True
INTERFACE_SOURCE_LOCATION = None
INTERFACE_SOURCE_REVISION = None
Expand Down Expand Up @@ -66,6 +66,7 @@ class RpcFormat(Enum):


def Parse(cmdline):
global FRAMEWORK_NAMESPACE
global DEFAULT_DEFINITIONS_FILE
global INTERFACE_NAMESPACES
global JSON_INTERFACE_PATH
Expand Down Expand Up @@ -291,6 +292,13 @@ def Parse(cmdline):
help="override interface source file revision to the commit id specified")

ts_group = argparser.add_argument_group("Troubleshooting arguments (optional)")
doc_group.add_argument("--framework-namespace",
dest="framework_namespace",
metavar="NS",
type=str,
action="store",
default=FRAMEWORK_NAMESPACE,
help="set framework namespace")
ts_group.add_argument("--verbose",
dest="verbose",
action="store_true",
Expand Down Expand Up @@ -326,6 +334,10 @@ def Parse(cmdline):
INTERFACE_SOURCE_REVISION = args.source_revision
AUTO_PREFIX = args.auto_prefix

if args.framework_namespace:
FRAMEWORK_NAMESPACE = args.framework_namespace
INTERFACE_NAMESPACES = ["::%s::Exchange::JSONRPC" % FRAMEWORK_NAMESPACE, "::%s::Exchange" % FRAMEWORK_NAMESPACE]

if args.if_namespaces:
INTERFACE_NAMESPACES = args.if_namespaces

Expand Down
6 changes: 3 additions & 3 deletions JsonGenerator/source/header_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def StripFrameworkNamespace(identifier):
def StripInterfaceNamespace(identifier):
return str(identifier).replace(ns + "::", "")

interfaces = [i for i in CppInterface.FindInterfaceClasses(tree, ns, file, []) if (i.obj.is_json or (all and not i.obj.is_event))]
interfaces = [i for i in CppInterface.FindInterfaceClasses(tree, ns, file, ["::%s::Core::IUnknown" % config.FRAMEWORK_NAMESPACE]) if (i.obj.is_json or (all and not i.obj.is_event))]

def Build(face):
def _EvaluateRpcFormat(obj):
Expand Down Expand Up @@ -310,7 +310,7 @@ def GenerateObject(ctype, was_typdef):

return "object", { "properties": properties, "required": required }

if cppType.full_name == "::Thunder::Core::JSONRPC::Context":
if cppType.full_name == "::%s::Core::JSONRPC::Context" % config.FRAMEWORK_NAMESPACE:
result = "@context", {}
elif (cppType.vars and not cppType.methods) or not verify:
result = GenerateObject(cppType, isinstance(var.type.Type(), CppParser.Typedef))
Expand Down Expand Up @@ -934,7 +934,7 @@ def LoadInterface(file, log, all = False, include_paths = []):
includes = []

tree = CppParser.ParseFiles([os.path.join(os.path.dirname(os.path.realpath(__file__)),
posixpath.normpath(config.DEFAULT_DEFINITIONS_FILE)), file], include_paths, log)
posixpath.normpath(config.DEFAULT_DEFINITIONS_FILE)), file], config.FRAMEWORK_NAMESPACE, include_paths, log)

for ns in config.INTERFACE_NAMESPACES:
their_schemas, their_includes = LoadInterfaceInternal(file, tree, ns, log, all, include_paths)
Expand Down
21 changes: 10 additions & 11 deletions JsonGenerator/source/rpc_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,22 +1092,21 @@ def _Invoke(params, response, parent="", repsonse_parent="", const_cast=False):
# Return collected signatures, so the emited file can be prepended with
return prototypes

def EmitRpcCode(root, emit, header_file, source_file, data_emitted):

namespace = (root.schema["namespace"] if "namespace" in root.schema else "::Thunder::Exchange")
def __Namespace(root):
return (root.schema["namespace"] if "namespace" in root.schema else "::%s::Exchange" % config.FRAMEWORK_NAMESPACE)

prototypes = _EmitRpcCode(root, emit, namespace, header_file, source_file, data_emitted)
def EmitRpcCode(root, emit, header_file, source_file, data_emitted):
_namespace = __Namespace(root)
prototypes = _EmitRpcCode(root, emit, _namespace, header_file, source_file, data_emitted)

with emitter.Emitter(None, config.INDENT_SIZE) as prototypes_emitter:
_EmitRpcPrologue(root, prototypes_emitter, header_file, source_file, namespace, data_emitted, prototypes)
_EmitRpcPrologue(root, prototypes_emitter, header_file, source_file, _namespace, data_emitted, prototypes)
emit.Prepend(prototypes_emitter)

_EmitRpcEpilogue(root, emit, namespace)
_EmitRpcEpilogue(root, emit, _namespace)

def EmitRpcVersionCode(root, emit, header_file, source_file, data_emitted):

namespace = (root.schema["namespace"] if "namespace" in root.schema else "::Thunder::Exchange")

_EmitRpcPrologue(root, emit, header_file, source_file, namespace, data_emitted)
_namespace = __Namespace(root)
_EmitRpcPrologue(root, emit, header_file, source_file, _namespace, data_emitted)
_EmitVersionCode(emit, rpc_version.GetVersion(root.schema["info"] if "info" in root.schema else dict()))
_EmitRpcEpilogue(root, emit, namespace)
_EmitRpcEpilogue(root, emit, _namespace)
7 changes: 4 additions & 3 deletions ProxyStubGenerator/CppParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2500,14 +2500,15 @@ def ParseFile(source_file, includePaths = []):
return Parse(contents)


def ParseFiles(source_files, includePaths = [], log = None):
def ParseFiles(source_files, framework_namespace, includePaths = [], log = None):
contents = ""
for source_file in source_files:
if source_file:
quiet = (source_file[0] == "@")
contents += ReadFile((source_file[1:] if quiet else source_file), includePaths, quiet, "")
contents = contents.replace("__FRAMEWORK_NAMESPACE__", framework_namespace)

return Parse(contents,log)
return Parse(contents, log)


# -------------------------------------------------------------------------
Expand Down Expand Up @@ -2547,7 +2548,7 @@ def DumpTree(tree, ind=0):
# entry point

if __name__ == "__main__":
tree = ParseFiles([os.path.join(os.path.dirname(__file__), "default.h"), sys.argv[1]], sys.argv[2:], Log.Log("debug", True, True))
tree = ParseFiles([os.path.join(os.path.dirname(__file__), "default.h"), sys.argv[1]], "Thunder", sys.argv[2:], Log.Log("debug", True, True))
if isinstance(tree, Namespace):
DumpTree(tree)
else:
Expand Down
6 changes: 2 additions & 4 deletions ProxyStubGenerator/Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from . import CppParser
from ProxyStubGenerator import CppParser

class Interface():
def __init__(self, obj, iid, file):
self.obj = obj
self.id = iid
self.file = file


# Looks for interface clasess (ie. classes inheriting from Core::Unknown and specifying ID enum).
def FindInterfaceClasses(tree, interface_namespace, source_file, ancestors = [ "::Thunder::Core::IUnknown" ]):
def FindInterfaceClasses(tree, interface_namespace, source_file, ancestors):
interfaces = []

selected = []

def __Traverse(tree, faces):
Expand Down
20 changes: 19 additions & 1 deletion ProxyStubGenerator/Log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import sys
#!/usr/bin/env python3

# If not stated otherwise in this file or this component's license file the
# following copyright and licenses apply:
#
# Copyright 2020 Metrological
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

class Log:
Expand Down
27 changes: 20 additions & 7 deletions ProxyStubGenerator/StubGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import argparse
import copy
import glob
import CppParser
from collections import OrderedDict
import Log
import CppParser

NAME = "ProxyStubGenerator"

Expand All @@ -43,9 +43,10 @@
# static configuration
EMIT_COMMENT_WITH_PROTOTYPE = True
EMIT_COMMENT_WITH_STUB_ORDER = True
STUB_NAMESPACE = "::Thunder::ProxyStubs"
INTERFACE_NAMESPACES = ["::Thunder"]
CLASS_IUNKNOWN = "::Thunder::Core::IUnknown"
FRAMEWORK_NAMESPACE = "Thunder"
STUB_NAMESPACE = "::%s::ProxyStubs" % FRAMEWORK_NAMESPACE
INTERFACE_NAMESPACES = ["::%s" % FRAMEWORK_NAMESPACE]
CLASS_IUNKNOWN = "::%s::Core::IUnknown" % FRAMEWORK_NAMESPACE
PROXYSTUB_CPP_NAME = "ProxyStubs_%s.cpp"

ENABLE_CUSTOM_ALLOCATOR = False
Expand Down Expand Up @@ -518,7 +519,7 @@ def Convert(paramtype, retval, vars, hresult=False):
emit.Line("}")
emit.Line()

def Parse(source_file, includePaths = [], defaults = "", extra_includes = []):
def Parse(source_file, framework_namespace, includePaths = [], defaults = "", extra_includes = []):

log.Info("Parsing %s..." % source_file)

Expand All @@ -530,7 +531,7 @@ def Parse(source_file, includePaths = [], defaults = "", extra_includes = []):
files.extend(extra_includes)
files.append(source_file)

tree = CppParser.ParseFiles(files, includePaths, log)
tree = CppParser.ParseFiles(files, framework_namespace, includePaths, log)
if not isinstance(tree, CppParser.Namespace):
raise SkipFileError(source_file)

Expand Down Expand Up @@ -2162,6 +2163,12 @@ def GenerateIdentification(name):
action="append",
default=[],
help="set a namespace to look for interfaces in (default: %s)" % INTERFACE_NAMESPACES[0])
argparser.add_argument("--framework-namespace",
dest="framework_namespace",
metavar="NS",
action="store",
default=FRAMEWORK_NAMESPACE,
help="set framework namespace (default: %s)" % FRAMEWORK_NAMESPACE)
argparser.add_argument("--outdir",
dest="outdir",
metavar="DIR",
Expand Down Expand Up @@ -2212,6 +2219,12 @@ def GenerateIdentification(name):
scan_only = False
keep_incomplete = args.keep_incomplete

if args.framework_namespace:
FRAMEWORK_NAMESPACE = args.framework_namespace
STUB_NAMESPACE = "::%s::ProxyStubs" % FRAMEWORK_NAMESPACE
INTERFACE_NAMESPACES = ["::%s" % FRAMEWORK_NAMESPACE]
CLASS_IUNKNOWN = "::%s::Core::IUnknown" % FRAMEWORK_NAMESPACE

if args.if_namespaces:
INTERFACE_NAMESPACES = args.if_namespaces

Expand Down Expand Up @@ -2332,7 +2345,7 @@ def GenerateIdentification(name):
_extra_includes = [ os.path.join("@" + os.path.dirname(source_file), IDS_DEFINITIONS_FILE) ]
_extra_includes.extend(args.extra_includes)

tree = Parse(source_file, args.includePaths,
tree = Parse(source_file, FRAMEWORK_NAMESPACE, args.includePaths,
os.path.join("@" + os.path.dirname(os.path.realpath(__file__)), DEFAULT_DEFINITIONS_FILE),
_extra_includes)

Expand Down
1 change: 1 addition & 0 deletions ProxyStubGenerator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

from . import CppParser
from . import Interface
from . import Log
2 changes: 1 addition & 1 deletion ProxyStubGenerator/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace std {
typedef __stubgen_undetermined_integer clock_t;
}

namespace Thunder {
namespace __FRAMEWORK_NAMESPACE__ {

namespace Core {
typedef __stubgen_instance_id instance_id;
Expand Down
6 changes: 5 additions & 1 deletion cmake/FindJsonGenerator.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function(JsonGenerator)
endif()

set(optionsArgs CODE STUBS DOCS LEGACY_ALT AUTO_PREFIX NO_INCLUDES NO_WARNINGS NO_STYLE_WARNINGS DUPLICATE_OBJ_WARNINGS COPY_CTOR NO_REF_NAMES NO_INTERFACES_SECTION VERBOSE FORCE_GENERATE EMIT_INTERFACE_PATH )
set(oneValueArgs OUTPUT CPP_OUTPUT INDENT DEF_STRING DEF_INT_SIZE PATH FORMAT CPP_INTERFACE_PATH JSON_INTERFACE_PATH)
set(oneValueArgs OUTPUT CPP_OUTPUT INDENT DEF_STRING DEF_INT_SIZE PATH FORMAT CPP_INTERFACE_PATH JSON_INTERFACE_PATH FRAMEWORK_NAMESPACE)
set(multiValueArgs INPUT IFDIR CPPIFDIR INCLUDE_PATH NAMESPACE)

cmake_parse_arguments(Argument "${optionsArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
Expand Down Expand Up @@ -150,6 +150,10 @@ function(JsonGenerator)
list(APPEND _execute_command "--emit-json-interface-path" "${Argument_JSON_INTERFACE_PATH}")
endif()

if (Argument_FRAMEWORK_NAMESPACE)
list(APPEND _execute_command "--framework-namespace" "${Argument_FRAMEWORK_NAMESPACE}")
endif()

foreach(_namespace ${Argument_NAMESPACE})
list(APPEND _execute_command "--namespace" "${_namespace}")
endforeach(_namespace)
Expand Down
10 changes: 7 additions & 3 deletions cmake/FindProxyStubGenerator.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function(ProxyStubGenerator)
endif()

set(optionsArgs SECURE COHERENT TRACES VERBOSE NO_WARNINGS KEEP FORCE_GENERATE)
set(oneValueArgs OUTDIR)
set(oneValueArgs OUTDIR FRAMEWORK_NAMESPACE)
set(multiValueArgs INPUT INCLUDE INCLUDE_PATH NAMESPACE)

cmake_parse_arguments(Argument "${optionsArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
Expand Down Expand Up @@ -72,15 +72,19 @@ function(ProxyStubGenerator)
list(APPEND _execute_command "--force")
endif()

if(Argument_NAMESPACE)
list(APPEND _execute_command "--namespace" "${Argument_NAMESPACE}")
if(Argument_FRAMEWORK_NAMESPACE)
list(APPEND _execute_command "--framework-namespace" "${Argument_FRAMEWORK_NAMESPACE}")
endif()

if(Argument_OUTDIR)
file(MAKE_DIRECTORY "${Argument_OUTDIR}")
list(APPEND _execute_command "--outdir" "${Argument_OUTDIR}")
endif()

foreach(_namespace ${Argument_NAMESPACE})
list(APPEND _execute_command "--namespace" "${_namespace}")
endforeach(_namespace)

foreach(_include ${Argument_INCLUDE})
list(APPEND _execute_command "-I" "${_include}")
endforeach(_include)
Expand Down

0 comments on commit ca53c78

Please sign in to comment.