Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial C support to configfile.h, extension.h and resource.h #7889

Open
wants to merge 29 commits into
base: dev
Choose a base branch
from

Conversation

JCash
Copy link
Contributor

@JCash JCash commented Aug 16, 2023

We've added some first pure C api headers to our dmSDK: configfile.h, extension.h and resource.h.
This is a step in our current effort to add C# support to our engine.

⚠️
The old C++ api's should be mostly intact, however we did change passing some structs as pointers (previously passed as references).

While stricly a breaking change for extension developers.
Regular users might not notice it other than that they would have to update an extension version.

dmsdk/dlib/configfile.h

This file now only contains the C api.
For the C++ api, include dmsdk/dlib/configfile_gen.hpp

dmsdk/extension/extension.h

This file now only contains the C api.
For the C++ api, include dmsdk/extension/extension_gen.hpp

dmsdk/resource/resource.h

This file now only contains the C api.
For the C++ api, include dmsdk/resource/resource.hpp

  • We've updated the resource system to use pointers to structs in the public callbacks.
  • We've moved the old resource descriptor struct into a HResourceDescriptor type and functions.
  • We've moved the old resource type struct in a HResourceType type and functions.

Fixes #8924

PR checklist

  • Code
    • Add engine and/or editor unit tests.
    • New and changed code follows the overall code style of existing code
    • Add comments where needed
  • Documentation
    • Make sure that API documentation is updated in code comments
    • Make sure that manuals are updated (in github.com/defold/doc)
  • Prepare pull request and affected issue for automatic release notes generator
    • Pull request - Write a message that explains what this pull request does. What was the problem? How was it solved? What are the changes to APIs or the new APIs introduced? This message will be used in the generated release notes. Make sure it is well written and understandable for a user of Defold.
    • Pull request - Write a pull request title that in a sentence summarises what the pull request does. Do not include "Issue-1234 ..." in the title. This text will be used in the generated release notes.
    • Pull request - Link the pull request to the issue(s) it is closing. Use on of the approved closing keywords.
    • Affected issue - Assign the issue to a project. Do not assign the pull request to a project if there is an issue which the pull request closes.
    • Affected issue - Assign the "breaking change" label to the issue if introducing a breaking change.
    • Affected issue - Assign the "skip release notes" is the issue should not be included in the generated release notes.

JCash and others added 27 commits July 3, 2023 18:24
# Conflicts:
#	engine/extension/src/dmsdk/extension/extension.h
# Conflicts:
#	engine/liveupdate/src/liveupdate.cpp
#	share/extender/build_input.yml
# Conflicts:
#	engine/dlib/src/dmsdk/dlib/configfile.h
#	engine/extension/src/dmsdk/extension/extension.h
#	engine/extension/src/extension.cpp
@@ -0,0 +1,166 @@
;; Copyright 2020-2023 The Defold Foundation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an old branch, originally created for the Zig example/presentation

@@ -0,0 +1,30 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of how we'll generate api files from our C api's.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To regenerate the headers, we'll run:
./scripts/build.py gen_sdk_source

To keep things separated, we use "resource.h" for the C header, "resource.hpp" for the manually crafted C++ header, and "resource_gen.hpp" for the automatically generated header.
These are stored in the repository, as they don't need to be regenerated all the time (although it's currently quick). Another good reason is that it's easier to do hot fixes to the builds while this new workflow is developing.

@@ -30,65 +30,182 @@
#include "sys.h"
#include "static_assert.h"

namespace dmConfigFile
struct ConfigFileEntry
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still use C++ as the implementation file (at least for now), as it 1) saves time, and 2) we can use containers more easily.

* @typedef
* @name HConfigFile
*/
typedef struct ConfigFile* HConfigFile;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How new C handles will look like (quite similar to what we do today)

* }
*
*/
const char* ConfigFileGetString(HConfigFile config, const char* key, const char* default_value);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the ConfigFile is the prefix/namespace.

@@ -0,0 +1,260 @@
/*# Resource
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the resource.h more readable at-a-glance, I moved some of the documentation here.

@@ -0,0 +1,92 @@
// Copyright 2020-2024 The Defold Foundation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved some of the functionality to separate cpp files for easier reading

Comment on lines +111 to +113
c_includes = ['extension.h',
'configfile.h',
'resource.h']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We test build the C api headers using C compiler, to make sure it's strict C code.

Comment on lines +1071 to +1089
# ------------------------------------------------------------
# Gen source files ->

def _gen_sdk_source_lib(self, libname, args, cwd, info):
self._log('Generating source for %s' % libname)
libargs = args + ['-i', info]
run.env_command(self._form_env(), libargs, cwd = cwd)

def gen_sdk_source(self):
print("Generating source!")
cmd = self.get_python() + [os.path.normpath(join(self.defold_root, './scripts/dmsdk/gen_sdk.py'))]
for lib in ENGINE_LIBS:
cwd = 'engine/%s' % lib
info = join(self.defold_root, 'engine/%s/sdk_gen.json' % lib)
if os.path.exists(info):
self._gen_sdk_source_lib(lib, cmd, join(self.defold_root, cwd), info)

# <- Gen source files
# ------------------------------------------------------------
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code generation bit from the build.py

@@ -0,0 +1,224 @@
# Copyright 2020-2024 The Defold Foundation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these files are heavily modified from the Sokol bindgen tools.
However, at this point I feel that I've modified it so far, that it has little resemblance of the original scripts.

@JCash JCash marked this pull request as ready for review May 16, 2024 13:41
@JCash JCash changed the title Added experimental C support to extension.h Added initial C support to configfile.h, extension.h and resource.h May 16, 2024
@JCash JCash added the breaking change Issue introducing a breaking change label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Issue introducing a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create C++ api generator script
2 participants