view CodeGeneration/CppCodeGeneration.py @ 61:97047be8435a

fix reuse, add 1.12.9 SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 14 Aug 2025 16:42:02 +0200
parents 1873eecbc592
children 156e942b136e
line wrap: on
line source

#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2023-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
# SPDX-License-Identifier: GPL-3.0-or-later

# Java plugin for Orthanc
# Copyright (C) 2023-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import CodeModel

import argparse
import json
import os
import pystache
import sys


ROOT = os.path.dirname(os.path.realpath(__file__))


##
## Parse the command-line arguments
##

ORTHANC_SDK_DEFAULT_VERSION = CodeModel.ReadOrthancSdkDefaultVersion(os.path.join(ROOT, '..', 'OrthancSDKVersion.cmake'))

parser = argparse.ArgumentParser(description = 'Generate C++ native functions to wrap the Orthanc SDK in Java.')
parser.add_argument('--sdk',
                    default = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                           '../Resources/Orthanc/Sdk-%s/orthanc/OrthancCPlugin.h' % ORTHANC_SDK_DEFAULT_VERSION),
                    help = 'Path to the Orthanc SDK')
parser.add_argument('--model',
                    default = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                           '../Resources/Orthanc/OrthancPluginCodeModel.json'),
                    help = 'Input code model, as generated by the orthanc project')
parser.add_argument('--target',
                    default = 'NativeSDK.cpp',
                    help = 'Target file')

args = parser.parse_args()

SDK_VERSION = CodeModel.ReadOrthancSdkVersion(args.sdk)

renderer = pystache.Renderer(
    escape = lambda u: u,  # No escaping
)



print('** Generating the C++ native functions to wrap Orthanc SDK %d.%d.%d **' % (SDK_VERSION[0], SDK_VERSION[1], SDK_VERSION[2]))

model = CodeModel.Load(args.model)

with open(os.path.join(ROOT, 'CppNativeSDK.mustache'), 'r') as f:
    template = f.read()

    with open(args.target, 'w') as g:
        functions = []

        for f in model['native_functions']:
            if CodeModel.IsPrimitiveAvailable(SDK_VERSION, f):
                functions.append(f)

        s = renderer.render(template, {
            'functions' : functions,
        })

        s = s.splitlines()
        s = filter(lambda l: not l.isspace() or len(l) == 0, s)

        g.write('\n'.join(s))