SWIG 3.0.3 fails to build scripting interface

Bug #1407167 reported by Blair Bonnett
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
KiCad
Fix Released
High
Blair Bonnett

Bug Description

Just tried to compile a scripting build of KiCad BZR5348 after a recent upgrade of SWIG from 3.0.2 to 3.0.3. This fails on the scripting interface with the following errors:

[ 64%] Swig source
/home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:48: Error: Unknown SWIG preprocessor directive: first
/home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:56: Error: Unknown SWIG preprocessor directive: when
/home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:141: Error: Unknown SWIG preprocessor directive: Get
/home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:145: Error: Unknown SWIG preprocessor directive: Convert
/home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:146: Error: Unknown SWIG preprocessor directive: Same
/home/blair/sources/kicad/product/pcbnew/scripting/board.i:44: Error: Unknown SWIG preprocessor directive: add
/home/blair/sources/kicad/product/pcbnew/scripting/board.i:45: Error: Unknown SWIG preprocessor directive: the
/home/blair/sources/kicad/product/pcbnew/scripting/module.i:36: Error: Unknown SWIG preprocessor directive: def
/home/blair/sources/kicad/product/pcbnew/scripting/module.i:37: Error: Unknown SWIG preprocessor directive: return
/home/blair/sources/kicad/product/pcbnew/scripting/module.i:40: Error: Unknown SWIG preprocessor directive: add
/home/blair/sources/kicad/product/pcbnew/scripting/module.i:41: Error: Unknown SWIG preprocessor directive: the
pcbnew/CMakeFiles/_pcbnew.dir/build.make:53: recipe for target 'pcbnew/pcbnewPYTHON_wrap.cxx' failed

This appears to be due to a change in SWIG (https://github.com/swig/swig/issues/217) whereby invalid preprocessor commands are now treated as errors rather than being silently ignored as before. The lines reporting errors are all Python-style comments (i.e., start with #) which SWIG treats as directives. According to https://github.com/swig/swig/issues/217#issuecomment-61752587:

    > Change the lines which start with # but which aren't preprocessor
    > statements to not start with # - SWIG allows C++-style comments,
    > so you can use // instead on those lines.

I have changed these lines as suggested and am currently rebuilding to see if that fixes it.

Blair Bonnett (bcb)
description: updated
description: updated
Revision history for this message
Wayne Stambaugh (stambaughw) wrote : Re: [Bug 1407167] [NEW] SWIG 3.0.3 fails to build scripting interface
Download full text (7.5 KiB)

I just ran into this as well. If your fix works, please post the patch
and I will commit it. Thanks.

On 1/2/2015 4:57 PM, Blair Bonnett wrote:
> Public bug reported:
>
> Just tried to compile a scripting build of KiCad BZR5348 after a recent
> upgrade of SWIG from 3.0.2 to 3.0.3. This fails on the scripting
> interface with the following errors:
>
> [ 64%] Swig source
> /home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:48: Error: Unknown SWIG preprocessor directive: first
> /home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:56: Error: Unknown SWIG preprocessor directive: when
> /home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:141: Error: Unknown SWIG preprocessor directive: Get
> /home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:145: Error: Unknown SWIG preprocessor directive: Convert
> /home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:146: Error: Unknown SWIG preprocessor directive: Same
> /home/blair/sources/kicad/product/pcbnew/scripting/board.i:44: Error: Unknown SWIG preprocessor directive: add
> /home/blair/sources/kicad/product/pcbnew/scripting/board.i:45: Error: Unknown SWIG preprocessor directive: the
> /home/blair/sources/kicad/product/pcbnew/scripting/module.i:36: Error: Unknown SWIG preprocessor directive: def
> /home/blair/sources/kicad/product/pcbnew/scripting/module.i:37: Error: Unknown SWIG preprocessor directive: return
> /home/blair/sources/kicad/product/pcbnew/scripting/module.i:40: Error: Unknown SWIG preprocessor directive: add
> /home/blair/sources/kicad/product/pcbnew/scripting/module.i:41: Error: Unknown SWIG preprocessor directive: the
> pcbnew/CMakeFiles/_pcbnew.dir/build.make:53: recipe for target 'pcbnew/pcbnewPYTHON_wrap.cxx' failed
>
> This appears to be due to a change in SWIG
> (https://github.com/swig/swig/issues/217) whereby invalid preprocessor
> commands are now treated as errors rather than being silently ignored as
> before. The lines reporting errors are all Python-style comments (i.e.,
> start with #) which SWIG treats as directives. According to
> https://github.com/swig/swig/issues/217#issuecomment-61752587:
>
> > Change the lines which start with # but which aren't preprocessor
> > statements to not start with # - SWIG allows C++-style comments,
> > so you can use // instead on those lines.
>
> I have changed these lines as suggested and am currently rebuilding to
> see if that fixes it.
>
> ** Affects: kicad
> Importance: High
> Assignee: Blair Bonnett (blair-bonnett)
> Status: In Progress
>
> ** Description changed:
>
> Just tried to compile a scripting build of KiCad BZR5348 after a recent
> upgrade of SWIG from 3.0.2 to 3.0.3. This fails on the scripting
> interface with the following errors:
>
> [ 64%] Swig source
> /home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:48: Error: Unknown SWIG preprocessor directive: first
> /home/blair/sources/kicad/product/pcbnew/../scripting/dlist.i:56: Error: Unknown SWIG preprocessor directive: when
> /home/blair/sources/kicad/product/pcbnew/../scripting/kicad.i:141: Error: Unknown SWIG preprocessor directive: Get
> /home/blair/sou...

Read more...

Revision history for this message
Blair Bonnett (bcb) wrote :

While that compiled OK, the /* ... */ comments were copied into pcbnew.py causing errors. This would be why JP changed them in r5214...

From https://github.com/swig/swig/issues/221#issuecomment-54469375, the correct solution is to wrap the %pythoncode blocks in %{ ... %} rather than just braces. This protects the block from the preprocessor. I have done this to the offending blocks.

There is another required change -- the version check in the pcbnew.py file that SWIG outputs now has spaces between the numbers which causes the fix_swig_imports.py script to fail. I've patched that as well.

This compiles on r5348, but the resulting library does not work. Running make qa gives the error:

    File "/home/blair/build/kicad/product/local/build/pcbnew/pcbnew.py", line 1657, in EDA_TEXT
        def Draw(self, aClipBox, aDC, aOffset, aColor, aDrawMode, aDisplay_mode=LINE, aAnchor_color=UNSPECIFIED_COLOR):
 NameError: name 'UNSPECIFIED_COLOR' is not defined

Interestingly, UNSPECIFIED_COLOR is available in the shared library (_pcbnew.so) but its not copied to the pcbnew.py importer like other constants. If you manually fix this then the next error is

    File "/home/blair/build/kicad/product/local/build/pcbnew/pcbnew.py", line 12531, in MODULE_List
        def GetPadCount(self, aIncludeNPTH=INCLUDE_NPTH):
 NameError: name 'INCLUDE_NPTH' is not defined

This enum value is available as _pcbnew.MODULE_INCLUDE_NPTH, and manually editing pcbnew to import this as INCLUDE_NPTH fixes this. With these two manual changes make qa passes.

I've never used SWIG before so I'm flying somewhat blind, but I'll see if I can figure out why these aren't being copied into pcbnew.py. If this is obvious to anybody who knows SWIG, please yell out. In the meantime, the attached patch gets it to a point where compilation succeeds even if the library doesn't work.

Revision history for this message
Blair Bonnett (bcb) wrote :

I take that back. They are copied in to pcbnew.py. However, they are copied after the function definition that uses them as a default value, and since they don't exist at the time the function is defined the error occurs.

Revision history for this message
jean-pierre charras (jp-charras) wrote :

I fixed this issue, using a trick. Your patch helped me. Thanks.

Changed in kicad:
status: In Progress → Fix Committed
Jon Neal (reportingsjr)
Changed in kicad:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.