RNA: support compiling rna files as C++ code #108290

Merged
Jacques Lucke merged 29 commits from JacquesLucke/blender:rna-cpp into main 2023-06-09 11:45:46 +02:00
Member

Compiling files in makesrna as C code is often a bit annoying when converting other files to C++ (#103343). That's because one often had to introduce additional C wrappers.

The goal of this patch is to support compiling rna files as C++ code. This mostly required changes in makesrna.c to make the generated code compatible with C and C++. The cmake code had to change a bit as well because we need to pass different compiler options for C++ code to avoid some warnings.

This patch also converts a few rna files to C++ already, although that is mostly just for testing. The rest of the files are expected to be converted after this patch is merged.

It's possible, even likely, that makesrna.c has to change a little bit more to support C++, but that is hard to know without converting all files first.

This patch also contains code from @LazyDodo and @LukasTonne.

Compiling files in `makesrna` as C code is often a bit annoying when converting other files to C++ (#103343). That's because one often had to introduce additional C wrappers. The goal of this patch is to support compiling rna files as C++ code. This mostly required changes in `makesrna.c` to make the generated code compatible with C and C++. The cmake code had to change a bit as well because we need to pass different compiler options for C++ code to avoid some warnings. This patch also converts a few rna files to C++ already, although that is mostly just for testing. The rest of the files are expected to be converted after this patch is merged. It's possible, even likely, that `makesrna.c` has to change a little bit more to support C++, but that is hard to know without converting all files first. This patch also contains code from @LazyDodo and @LukasTonne.
Jacques Lucke added 13 commits 2023-05-25 19:34:03 +02:00
Author
Member

@blender-bot build

@blender-bot build
Jacques Lucke added 4 commits 2023-05-25 21:13:03 +02:00
Author
Member

@blender-bot build

@blender-bot build
Member

Figure out how to get rid of cc1plus: warning: command-line option ‘-Wno-missing-prototypes’ is valid for C/ObjC but not for C++. Somehow I have to tell cmake that it should use -Wno-missing-prototypes only for .c files.

something like this will do it:

diff --git a/source/blender/makesrna/intern/CMakeLists.txt b/source/blender/makesrna/intern/CMakeLists.txt
index f96705189e4..f8f934bddaf 100644
--- a/source/blender/makesrna/intern/CMakeLists.txt
+++ b/source/blender/makesrna/intern/CMakeLists.txt
@@ -134,21 +134,42 @@ set_source_files_properties(${GENSRC} PROPERTIES GENERATED TRUE)
 # CFLAGS for Generated Files
 #
 # less strict flags for generated source
+set(GENSRC_FLAGS)
 set(GENSRC_CFLAGS)
+set(GENSRC_CXXFLAGS)
 if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
-  set(GENSRC_CFLAGS "-Wno-missing-prototypes -Wno-missing-declarations")
+  set(GENSRC_CFLAGS "-Wno-missing-prototypes")
+  set(GENSRC_FLAGS "-Wno-missing-declarations")
 endif()
 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
-  string(APPEND GENSRC_CFLAGS " -Wno-missing-variable-declarations")
+  string(APPEND GENSRC_FLAGS " -Wno-missing-variable-declarations")
 elseif(MSVC)
   # Restore warn C4100 (unreferenced formal parameter) back to w4
   remove_cc_flag(/w34100)
 endif()

+set(GENSRC_CXX ${GENSRC})
+list(FILTER GENSRC_CXX INCLUDE REGEX ".*\.cc$")
+set(GENSRC_C ${GENSRC})
+list(FILTER GENSRC_C INCLUDE REGEX ".*\.c$")
+
+if(GENSRC_FLAGS)
+  set_source_files_properties(${GENSRC} PROPERTIES COMPILE_FLAGS "${GENSRC_FLAGS}")
+endif()
+
 if(GENSRC_CFLAGS)
-  set_source_files_properties(${GENSRC} PROPERTIES COMPILE_FLAGS "${GENSRC_CFLAGS}")
+  set_source_files_properties(${GENSRC_C} PROPERTIES COMPILE_FLAGS "${GENSRC_CFLAGS}")
 endif()
+
+if(GENSRC_CXXFLAGS)
+  set_source_files_properties(${GENSRC_CXX} PROPERTIES COMPILE_FLAGS "${GENSRC_CXXFLAGS}")
+endif()
+
+unset(GENSRC_C)
+unset(GENSRC_CXX)
+unset(GENSRC_FLAGS)
 unset(GENSRC_CFLAGS)
+unset(GENSRC_CXXFLAGS)


I admit i went a bit overboard, there's currently no use case for CXX specific flags, so you could take that bit out, it felt incomplete without it though...

> Figure out how to get rid of cc1plus: warning: command-line option ‘-Wno-missing-prototypes’ is valid for C/ObjC but not for C++. Somehow I have to tell cmake that it should use -Wno-missing-prototypes only for .c files. something like this will do it: ```Diff diff --git a/source/blender/makesrna/intern/CMakeLists.txt b/source/blender/makesrna/intern/CMakeLists.txt index f96705189e4..f8f934bddaf 100644 --- a/source/blender/makesrna/intern/CMakeLists.txt +++ b/source/blender/makesrna/intern/CMakeLists.txt @@ -134,21 +134,42 @@ set_source_files_properties(${GENSRC} PROPERTIES GENERATED TRUE) # CFLAGS for Generated Files # # less strict flags for generated source +set(GENSRC_FLAGS) set(GENSRC_CFLAGS) +set(GENSRC_CXXFLAGS) if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang")) - set(GENSRC_CFLAGS "-Wno-missing-prototypes -Wno-missing-declarations") + set(GENSRC_CFLAGS "-Wno-missing-prototypes") + set(GENSRC_FLAGS "-Wno-missing-declarations") endif() if(CMAKE_C_COMPILER_ID MATCHES "Clang") - string(APPEND GENSRC_CFLAGS " -Wno-missing-variable-declarations") + string(APPEND GENSRC_FLAGS " -Wno-missing-variable-declarations") elseif(MSVC) # Restore warn C4100 (unreferenced formal parameter) back to w4 remove_cc_flag(/w34100) endif() +set(GENSRC_CXX ${GENSRC}) +list(FILTER GENSRC_CXX INCLUDE REGEX ".*\.cc$") +set(GENSRC_C ${GENSRC}) +list(FILTER GENSRC_C INCLUDE REGEX ".*\.c$") + +if(GENSRC_FLAGS) + set_source_files_properties(${GENSRC} PROPERTIES COMPILE_FLAGS "${GENSRC_FLAGS}") +endif() + if(GENSRC_CFLAGS) - set_source_files_properties(${GENSRC} PROPERTIES COMPILE_FLAGS "${GENSRC_CFLAGS}") + set_source_files_properties(${GENSRC_C} PROPERTIES COMPILE_FLAGS "${GENSRC_CFLAGS}") endif() + +if(GENSRC_CXXFLAGS) + set_source_files_properties(${GENSRC_CXX} PROPERTIES COMPILE_FLAGS "${GENSRC_CXXFLAGS}") +endif() + +unset(GENSRC_C) +unset(GENSRC_CXX) +unset(GENSRC_FLAGS) unset(GENSRC_CFLAGS) +unset(GENSRC_CXXFLAGS) ``` I admit i went a bit overboard, there's currently no use case for CXX specific flags, so you could take that bit out, it felt incomplete without it though...
Member

has some linkage issues with MSVC

diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index f25833eb6b2..39e709a46e5 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -3561,7 +3561,7 @@ static void rna_generate_internal_property_prototypes(BlenderRNA *UNUSED(brna),
     for (prop = base->cont.properties.first; prop; prop = prop->next) {
       fprintf(f,
               "%s%s rna_%s_%s;\n",
-              "extern ",
+              "RNA_EXTERN_C ",
               rna_property_structname(prop->type),
               base->identifier,
               prop->identifier);

does the trick

has some linkage issues with MSVC ```Diff diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c index f25833eb6b2..39e709a46e5 100644 --- a/source/blender/makesrna/intern/makesrna.c +++ b/source/blender/makesrna/intern/makesrna.c @@ -3561,7 +3561,7 @@ static void rna_generate_internal_property_prototypes(BlenderRNA *UNUSED(brna), for (prop = base->cont.properties.first; prop; prop = prop->next) { fprintf(f, "%s%s rna_%s_%s;\n", - "extern ", + "RNA_EXTERN_C ", rna_property_structname(prop->type), base->identifier, prop->identifier); ``` does the trick
Jacques Lucke added 3 commits 2023-05-26 09:38:58 +02:00
Author
Member

@blender-bot build

@blender-bot build
Jacques Lucke added 4 commits 2023-05-30 10:55:28 +02:00
Jacques Lucke added 1 commit 2023-05-30 11:05:55 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
91657203a8
add changes from lukas
Author
Member

@blender-bot build

@blender-bot build

I looked at doing this a while ago as well, and wrote these to automate some of the monkey work:

sed -i -E 's/RNA_def_property_flag\(parm, ([^)]*)\)/RNA_def_property_flag(parm, PropertyFlag(\1))/g' *.cc
sed -i -E 's/RNA_def_property_flag\(prop, ([^)]*)\)/RNA_def_property_flag(prop, PropertyFlag(\1))/g' *.cc

sed -i -E 's/RNA_def_property_clear_flag\(parm, ([^),]+),/RNA_def_property_clear_flag(parm, PropertyFlag(\1),/g' *.cc
sed -i -E 's/RNA_def_property_clear_flag\(prop, ([^)]*)\)/RNA_def_property_clear_flag(prop, PropertyFlag(\1))/g' *.cc

sed -i -E 's/RNA_def_parameter_flags\(parm, ([^),]+), ([^)]*)\)/RNA_def_parameter_flags(parm, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc
sed -i -E 's/RNA_def_parameter_flags\(prop, ([^),]+), ([^)]*)\)/RNA_def_parameter_flags(prop, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc

sed -i -E 's/RNA_def_parameter_clear_flags\(parm, ([^),]+), ([^)]*)\)/RNA_def_parameter_clear_flags(parm, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc
sed -i -E 's/RNA_def_parameter_clear_flags\(prop, ([^),]+), ([^)]*)\)/RNA_def_parameter_clear_flags(prop, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc

sed -i -E 's/RNA_def_property_override_flag\(prop, ([^)]*)\)/RNA_def_property_override_flag(prop, PropertyOverrideFlag(\1))/g' *.cc

sed -i -E 's/  PROPOVERRIDE_NO_COMPARISON \| PROPOVERRIDE_OVERRIDABLE_LIBRARY\)/  PropertyOverrideFlag(PROPOVERRIDE_NO_COMPARISON \| PROPOVERRIDE_OVERRIDABLE_LIBRARY))/g' *.cc

sed -i -E 's/UNUSED\(([[:alnum:]_]+)\)/\/\* \1 \*\//g' *.cc
sed -i 's/\<NULL\>/nullptr/g' *.cc

sed -i -E "s/([[:alnum:]_]+) \*([[:alnum:]_]+) = ([[:alnum:]_]*)ptr->data;/\1 *\2 = static_cast<\1*>\(\3ptr->data\);/g" *.cc
sed -i -E "s/const ([[:alnum:]_]+) \*([[:alnum:]_]+) = ([[:alnum:]_]*)ptr->data;/const \1 *\2 = static_cast<const \1*>\(\3ptr->data\);/g" *.cc
I looked at doing this a while ago as well, and wrote these to automate some of the monkey work: ```bash sed -i -E 's/RNA_def_property_flag\(parm, ([^)]*)\)/RNA_def_property_flag(parm, PropertyFlag(\1))/g' *.cc sed -i -E 's/RNA_def_property_flag\(prop, ([^)]*)\)/RNA_def_property_flag(prop, PropertyFlag(\1))/g' *.cc sed -i -E 's/RNA_def_property_clear_flag\(parm, ([^),]+),/RNA_def_property_clear_flag(parm, PropertyFlag(\1),/g' *.cc sed -i -E 's/RNA_def_property_clear_flag\(prop, ([^)]*)\)/RNA_def_property_clear_flag(prop, PropertyFlag(\1))/g' *.cc sed -i -E 's/RNA_def_parameter_flags\(parm, ([^),]+), ([^)]*)\)/RNA_def_parameter_flags(parm, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc sed -i -E 's/RNA_def_parameter_flags\(prop, ([^),]+), ([^)]*)\)/RNA_def_parameter_flags(prop, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc sed -i -E 's/RNA_def_parameter_clear_flags\(parm, ([^),]+), ([^)]*)\)/RNA_def_parameter_clear_flags(parm, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc sed -i -E 's/RNA_def_parameter_clear_flags\(prop, ([^),]+), ([^)]*)\)/RNA_def_parameter_clear_flags(prop, PropertyFlag(\1), ParameterFlag(\2))/g' *.cc sed -i -E 's/RNA_def_property_override_flag\(prop, ([^)]*)\)/RNA_def_property_override_flag(prop, PropertyOverrideFlag(\1))/g' *.cc sed -i -E 's/ PROPOVERRIDE_NO_COMPARISON \| PROPOVERRIDE_OVERRIDABLE_LIBRARY\)/ PropertyOverrideFlag(PROPOVERRIDE_NO_COMPARISON \| PROPOVERRIDE_OVERRIDABLE_LIBRARY))/g' *.cc sed -i -E 's/UNUSED\(([[:alnum:]_]+)\)/\/\* \1 \*\//g' *.cc sed -i 's/\<NULL\>/nullptr/g' *.cc sed -i -E "s/([[:alnum:]_]+) \*([[:alnum:]_]+) = ([[:alnum:]_]*)ptr->data;/\1 *\2 = static_cast<\1*>\(\3ptr->data\);/g" *.cc sed -i -E "s/const ([[:alnum:]_]+) \*([[:alnum:]_]+) = ([[:alnum:]_]*)ptr->data;/const \1 *\2 = static_cast<const \1*>\(\3ptr->data\);/g" *.cc ```
Jacques Lucke added 1 commit 2023-05-30 11:46:01 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
72075bd454
add range check for c++
Author
Member

@blender-bot build

@blender-bot build
Jacques Lucke requested review from Brecht Van Lommel 2023-05-30 11:55:09 +02:00
Jacques Lucke requested review from Campbell Barton 2023-05-30 11:55:09 +02:00
Jacques Lucke requested review from Sergey Sharybin 2023-05-30 11:55:09 +02:00
Jacques Lucke requested review from Ray molenkamp 2023-05-30 11:55:09 +02:00
Jacques Lucke changed title from WIP: RNA: compile rna files as C++ code to RNA: support compiling rna files as C++ code 2023-05-30 11:55:25 +02:00
Sergey Sharybin approved these changes 2023-05-30 12:37:13 +02:00
Sergey Sharybin left a comment
Owner

That's great!

Can't spot anything wrong in the code, and build and tests are passing.

This patch also contains code from @LazyDodo and @LukasTonne.

You probably already know, but for that you can use Co-authored-by field in commit message,

That's great! Can't spot anything wrong in the code, and build and tests are passing. > This patch also contains code from @LazyDodo and @LukasTonne. You probably already know, but for that you can use `Co-authored-by` field in commit message,
Brecht Van Lommel approved these changes 2023-05-30 12:52:49 +02:00

Generally looks good, minor notes but not blockers:

source/blender/makesrna/intern/rna_lattice.cc:178:28: warning: enumerated and non-enumerated type in conditional expression [-Wextra]
  178 |   return (lt->key == NULL) ? PROP_EDITABLE : 0;
      |          ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
[ 93%: -51] Building CXX object source/blender/makesrna/intern/CMakeFiles/bf_rna.dir/rna_meta_gen.cc.o
In file included from /src/cmake_debug/source/blender/makesrna/intern/rna_meta_gen.cc:33:
source/blender/makesrna/intern/rna_meta.cc: In function ‘int rna_Meta_texspace_editable(PointerRNA*, const char**)’:
source/blender/makesrna/intern/rna_meta.cc:43:54: warning: enumerated and non-enumerated type in conditional expression [-Wextra]
   43 |   return (mb->texspace_flag & MB_TEXSPACE_FLAG_AUTO) ? 0 : PROP_EDITABLE;

Some of the listbase loops that involve static_cast<..> are fairly verbose now and might use LISTBASE_FOREACH.

Generally looks good, minor notes but not blockers: ``` source/blender/makesrna/intern/rna_lattice.cc:178:28: warning: enumerated and non-enumerated type in conditional expression [-Wextra] 178 | return (lt->key == NULL) ? PROP_EDITABLE : 0; | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ [ 93%: -51] Building CXX object source/blender/makesrna/intern/CMakeFiles/bf_rna.dir/rna_meta_gen.cc.o In file included from /src/cmake_debug/source/blender/makesrna/intern/rna_meta_gen.cc:33: source/blender/makesrna/intern/rna_meta.cc: In function ‘int rna_Meta_texspace_editable(PointerRNA*, const char**)’: source/blender/makesrna/intern/rna_meta.cc:43:54: warning: enumerated and non-enumerated type in conditional expression [-Wextra] 43 | return (mb->texspace_flag & MB_TEXSPACE_FLAG_AUTO) ? 0 : PROP_EDITABLE; ``` Some of the listbase loops that involve `static_cast<..>` are fairly verbose now and might use `LISTBASE_FOREACH`.
Campbell Barton approved these changes 2023-06-02 07:50:52 +02:00
Jacques Lucke added 1 commit 2023-06-08 21:43:51 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
ca18671a42
Merge branch 'main' into rna-cpp
Author
Member

@blender-bot build

@blender-bot build
Author
Member

Some of the listbase loops that involve static_cast<..> are fairly verbose now and might use LISTBASE_FOREACH.

That's the case in most files that are converted to C++. Generally, it's much less risky to just add static_cast instead of rewriting loops to use LISTBASE_FOREACH. It's sometimes difficult to determine if using LISTBASE_FOREACH changes the behavior or not, so generally this should be done in a separate cleanup commit.

> Some of the listbase loops that involve `static_cast<..>` are fairly verbose now and might use `LISTBASE_FOREACH`. That's the case in most files that are converted to C++. Generally, it's much less risky to just add `static_cast` instead of rewriting loops to use `LISTBASE_FOREACH`. It's sometimes difficult to determine if using `LISTBASE_FOREACH` changes the behavior or not, so generally this should be done in a separate cleanup commit.
Jacques Lucke added 2 commits 2023-06-09 11:42:16 +02:00
Jacques Lucke merged commit d154ebfa83 into main 2023-06-09 11:45:46 +02:00
Jacques Lucke deleted branch rna-cpp 2023-06-09 11:45:48 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#108290
No description provided.