Skip to content

py: Change makemoduledefs process so it uses output of qstr extraction. #8714

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

Conversation

dpgeorge
Copy link
Member

This cleans up the parsing of MP_REGISTER_MODULE() and generation of genhdr/moduledefs.h so that it uses the same process as compressed error string messages, using the output of qstr extraction.

This makes sure all MP_REGISTER_MODULE()'s that are part of the build are correctly picked up. Previously the extraction would miss some (eg if you had a mod.c file in the board directory for an stm32 board).

Build speed seems more or less unchanged.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label May 31, 2022
@@ -34,7 +34,11 @@
#include "py/runtime.h"
#include "py/builtin.h"

#ifndef NO_QSTR
// Only include module definitions when not doing qstr extraction, because the
// qstr extraction stage also generates this module definition header file.
Copy link
Member Author

Choose a reason for hiding this comment

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

@jimmo I'm not sure about this... now that moduledefs.h is generated after qstr extraction, how do all the module qstr names actually get found?

It works though... I guess because qstr extraction sees all the MP_REGISTER_MODULE() definitions now.

Copy link
Member Author

Choose a reason for hiding this comment

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

This now works correctly after removing the 3rd arg of MP_REGISTER_MODULE().

@@ -421,7 +421,9 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
// param obj_module: mp_obj_module_t instance
// prarm enabled_define: used as `#if (enabled_define) around entry`

#ifndef NO_QSTR
Copy link
Member Author

Choose a reason for hiding this comment

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

Due to this, qstr extraction will see MP_REGISTER_MODULE and it's qstr contents.

But does that mean qstrs for module names will always be included, even if that module is disabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

This has been fixed by removing the 3rd arg of MP_REGISTER_MODULE(). This macro is now only included if the surrounding #if / #endif wrapper (if it exists) is enabled.

@dpgeorge
Copy link
Member Author

Follow up to #8566.

@dpgeorge
Copy link
Member Author

To solve my problems above, I think the best way forward is to just remove the 3rd argument to MP_REGISTER_MODULE(). And then make sure this macro is always inside any #if that controls the module's inclusion in the build. Because this macro will now be pre-processed (by cpp) it'll be included if and only if it's enabled. That will also work with qstr extraction, the qstr for the module will only be included if the module itself is enabled.

@dpgeorge dpgeorge force-pushed the py-makemoduledefs-source-from-qstr-extraction branch 2 times, most recently from 6e66288 to 48f1ae1 Compare May 31, 2022 13:11
@dpgeorge
Copy link
Member Author

I've now pushed a commit which removes the 3rd argument to MP_REGISTER_MODULE().

@dpgeorge
Copy link
Member Author

@stinos I need help converting the msvc build instructions. From the first commit here you should be able to see the (not too large) changes to the .mk and .cmake files. That should give a good desrciption of what needs to change.

Basically moduledefs.h relies on moduledefs.collected relies on moduledefs.split relies on qstr.i.last.

@stinos
Copy link
Contributor

stinos commented May 31, 2022

Sure, here's a patch:

diff --git a/ports/windows/msvc/genhdr.targets b/ports/windows/msvc/genhdr.targets
index b53894f21..84520078f 100644
--- a/ports/windows/msvc/genhdr.targets
+++ b/ports/windows/msvc/genhdr.targets
@@ -14,6 +14,7 @@
     <PyQstrDefs>$(PySrcDir)qstrdefs.h</PyQstrDefs>
     <QstrDefsCollected>$(DestDir)qstrdefscollected.h</QstrDefsCollected>
     <QstrGen>$(DestDir)qstrdefs.generated.h</QstrGen>
+    <ModuleDefsCollected>$(DestDir)/moduledefs.collected</ModuleDefsCollected>
     <PyPython Condition="'$(PyPython)' == ''">$(MICROPY_CPYTHON3)</PyPython>
     <PyPython Condition="'$(PyPython)' == ''">python</PyPython>
     <CLToolExe Condition="'$(CLToolExe)' == ''">cl.exe</CLToolExe>
@@ -99,17 +100,17 @@ using(var outFile = System.IO.File.CreateText(OutputFile)) {
     <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat qstr _ $(DestDir)qstr $(QstrDefsCollected)"/>         </Target>

-  <Target Name="MakeModuleDefs" DependsOnTargets="MakeDestDir">
+  <Target Name="CollectModuleDefs" DependsOnTargets="MakeQstrDefs" Inputs="$(DestDir)qstr.i.last" Outputs="$(ModuleDefsCollected)">
+    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split module $(DestDir)qstr.i.last $(DestDir)module _"/>
+    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat module _ $(DestDir)module $(ModuleDefsCollected)"/>
+  </Target>
+
+  <Target Name="MakeModuleDefs" DependsOnTargets="CollectModuleDefs" Inputs="$(ModuleDefsCollected)" Outputs="$(DestDir)moduledefs.h">
     <PropertyGroup>
       <DestFile>$(DestDir)moduledefs.h</DestFile>
       <TmpFile>$(DestFile).tmp</TmpFile>
     </PropertyGroup>
-    <ItemGroup>
-      <PyUserModuleFiles Include="@(ClCompile)">
-        <Path>$([System.String]::new('%(FullPath)').Replace('$(PyBaseDir)', ''))</Path>
-      </PyUserModuleFiles>
-    </ItemGroup>
-    <Exec Command="$(PyPython) $(PySrcDir)makemoduledefs.py --vpath=&quot;., $(PyBaseDir), $(PyUserCModules)&quot; @(PyUserModuleFiles->'%(Path)', ' ') > $(TmpFile)"/>
+    <Exec Command="$(PyPython) $(PySrcDir)makemoduledefs.py $(ModuleDefsCollected) > $(TmpFile)"/>
     <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
   </Target>

Passes all tests, doesn't generate modufledefs when source changed but 'QSTR not updated' or when 'Module registrations not updated', only when 'Module registrations updated', so looks ok.

@dpgeorge dpgeorge force-pushed the py-makemoduledefs-source-from-qstr-extraction branch from 48f1ae1 to 9ab4075 Compare May 31, 2022 14:50
@dpgeorge
Copy link
Member Author

Thank you very much! I incorporated that into the first commit.

@dpgeorge dpgeorge force-pushed the py-makemoduledefs-source-from-qstr-extraction branch from 9ab4075 to 4dd8328 Compare May 31, 2022 15:08
dpgeorge added 2 commits June 2, 2022 16:29
This cleans up the parsing of MP_REGISTER_MODULE() and generation of
genhdr/moduledefs.h so that it uses the same process as compressed error
string messages, using the output of qstr extraction.

This makes sure all MP_REGISTER_MODULE()'s that are part of the build are
correctly picked up.  Previously the extraction would miss some (eg if you
had a mod.c file in the board directory for an stm32 board).

Build speed is more or less unchanged.

Thanks to @stinos for the ports/windows/msvc/genhdr.targets changes.

Signed-off-by: Damien George <damien@micropython.org>
It's no longer needed because this macro is now processed after
preprocessing the source code via cpp (in the qstr extraction stage), which
means unused MP_REGISTER_MODULE's are filtered out by the preprocessor.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the py-makemoduledefs-source-from-qstr-extraction branch from 4dd8328 to efe23ac Compare June 2, 2022 06:34
@dpgeorge dpgeorge merged commit efe23ac into micropython:master Jun 2, 2022
@dpgeorge dpgeorge deleted the py-makemoduledefs-source-from-qstr-extraction branch June 2, 2022 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants