Skip to content

Commit

Permalink
SWIG Improvements
Browse files Browse the repository at this point in the history
*) Extend SWIG support deeper into the BOARD class.
*) Move swig *.i files into a directory identified for SWIG, in preparation
   for a parallel universe involving Phoenix's SIP.
*) Move swig files which will be common to both eeschema and pcbnew into
   common/swig.
*) Sketch out a "common" python module, and plan on dovetailing that into a
   libkicad_shared.{dll,so}
*) Add common/swig/ki_exceptions.i and define a macro HANDLE_EXCEPTIONS()
   which is to be applied to any function which needs C++ to python
   exception translation.
*) Move the test for SWIG tool into top level CMakeLists.txt file for use
   in all python modules beyond pcbnew, i.e. eeschema and common.
*) Add SWIG_MODULE_pcbnew_EXTRA_DEPS which generates a better Makefile, one
   which rebuilds the swig generated *.cxx file when one of its dependencies
   change.
*) Re-architect the board.i file so that it can be split into multiple *.i
   files easily.
*) Make some KIWAY from python progress, in preparation for Modular KiCad
   phase III.
  • Loading branch information
liftoff-sr authored and stambaughw committed Sep 20, 2016
1 parent 964b5a1 commit 7311f07
Show file tree
Hide file tree
Showing 66 changed files with 1,151 additions and 317 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ set( INC_AFTER

# Find Python and other scripting resources
if( KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES )

# SWIG 3.0 or later require for C++11 support.
find_package( SWIG 3.0 REQUIRED )
include( ${SWIG_USE_FILE} )

# force a python version < 3.0
set( PythonInterp_FIND_VERSION 2.6 )
set( PythonLibs_FIND_VERSION 2.6 )
Expand Down
4 changes: 2 additions & 2 deletions CMakeModules/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@

/// A file extension with a leading '.' is a suffix, and this one is used on
/// top level program modules which implement the KIFACE.
#define KIFACE_SUFFIX wxT( "@KIFACE_SUFFIX@" )
#define KIFACE_PREFIX wxT( "@KIFACE_PREFIX@" )
#define KIFACE_SUFFIX "@KIFACE_SUFFIX@"
#define KIFACE_PREFIX "@KIFACE_PREFIX@"

/// Name of repo from which this build came.
#define KICAD_REPO_NAME "@KICAD_REPO_NAME@"
Expand Down
62 changes: 62 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ set( COMMON_SRCS
eda_dde.cpp
eda_doc.cpp
eda_pattern_match.cpp
exceptions.cpp
filter_reader.cpp
# findkicadhelppath.cpp.notused deprecated, use searchhelpfilefullpath.cpp
gbr_metadata.cpp
Expand Down Expand Up @@ -507,3 +508,64 @@ target_link_libraries( dsntest common ${wxWidgets_LIBRARIES} rt )
add_dependencies( dsntest lib-dependencies )

target_link_libraries( pcbcommon 3d-viewer )


# kiway.so
if( future AND KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES )
#file( MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/scripting )

set( SWIG_FLAGS
-I${CMAKE_CURRENT_SOURCE_DIR}/../include
-I${CMAKE_CURRENT_SOURCE_DIR}/swig/wx
)

if( DEBUG )
set( SWIG_FLAGS ${SWIG_FLAGS} -DDEBUG )
endif()

# call SWIG in C++ mode: https://cmake.org/cmake/help/v3.2/module/UseSWIG.html
set_source_files_properties( swig/kiway.i PROPERTIES CPLUSPLUS ON )

# collect CFLAGS , and pass them to swig later
get_directory_property( DirDefs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_DEFINITIONS )
foreach( d ${DirDefs} )
set( SWIG_FLAGS ${SWIG_FLAGS} -D${d} )
endforeach()

set( CMAKE_SWIG_FLAGS ${SWIG_FLAGS} )

#set( SWIG_OPTS -python -c++ -outdir ${CMAKE_CURRENT_BINARY_DIR} ${SWIG_FLAGS} )

include_directories( BEFORE ${INC_BEFORE} )
include_directories(
../common
${INC_AFTER}
)

set( SWIG_MODULE_kiway_EXTRA_DEPS
../common/swig/ki_exception.i
../common/swig/kicad.i
)

swig_add_module( kiway python
swig/kiway.i
#../common/kiway.cpp
#../common/project.cpp
#../common/pgm_base.cpp
#../common/
)

swig_link_libraries( kiway
common
bitmaps
${wxWidgets_LIBRARIES}
${PYTHON_LIBRARIES}
)

if( MAKE_LINK_MAPS )
set_target_properties( _kiway PROPERTIES
LINK_FLAGS "${TO_LINKER},-cref ${TO_LINKER},-Map=_kiway.so.map"
)
endif()

endif()
2 changes: 1 addition & 1 deletion common/footprint_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )

for( unsigned i = 0; i<m_errors.size(); ++i )
{
msg += wxT( "<p>" ) + m_errors[i].errorText + wxT( "</p>" );
msg += wxT( "<p>" ) + m_errors[i].Problem() + wxT( "</p>" );
}

dlg.AddHTML_Text( msg );
Expand Down
32 changes: 21 additions & 11 deletions common/kiway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,37 @@ void KIWAY::SetTop( wxFrame* aTop )
}


const wxString KIWAY::dso_full_path( FACE_T aFaceId )
const wxString KIWAY::dso_search_path( FACE_T aFaceId )
{
const wxChar* name;
const char* name;

switch( aFaceId )
{
case FACE_SCH: name = KIFACE_PREFIX wxT( "eeschema" ); break;
case FACE_PCB: name = KIFACE_PREFIX wxT( "pcbnew" ); break;
case FACE_CVPCB: name = KIFACE_PREFIX wxT( "cvpcb" ); break;
case FACE_GERBVIEW: name = KIFACE_PREFIX wxT( "gerbview" ); break;
case FACE_PL_EDITOR: name = KIFACE_PREFIX wxT( "pl_editor" ); break;
case FACE_PCB_CALCULATOR: name = KIFACE_PREFIX wxT( "pcb_calculator" ); break;
case FACE_BMP2CMP: name = KIFACE_PREFIX wxT( "bitmap2component" ); break;
case FACE_SCH: name = KIFACE_PREFIX "eeschema"; break;
case FACE_PCB: name = KIFACE_PREFIX "pcbnew"; break;
case FACE_CVPCB: name = KIFACE_PREFIX "cvpcb"; break;
case FACE_GERBVIEW: name = KIFACE_PREFIX "gerbview"; break;
case FACE_PL_EDITOR: name = KIFACE_PREFIX "pl_editor"; break;
case FACE_PCB_CALCULATOR: name = KIFACE_PREFIX "pcb_calculator"; break;
case FACE_BMP2CMP: name = KIFACE_PREFIX "bitmap2component"; break;

default:
wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
return wxEmptyString;
}

#ifndef __WXMAC__
wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
wxString path;

if( m_ctl & (KFCTL_STANDALONE | KFCTL_CPP_PROJECT_SUITE) )
{
// The 2 *.cpp program launchers: single_top.cpp and kicad.cpp expect
// the *.kiface's to reside in same diretory as their binaries do.
// Not so for python launcher, identified by KFCTL_PY_PROJECT_SUITE
path = wxStandardPaths::Get().GetExecutablePath();
}

wxFileName fn = path;
#else
// we have the dso's in main OSX bundle kicad.app/Contents/PlugIns
wxFileName fn = Pgm().GetExecutablePath();
Expand Down Expand Up @@ -159,7 +169,7 @@ KIFACE* KIWAY::KiFACE( FACE_T aFaceId, bool doLoad )
// DSO with KIFACE has not been loaded yet, does caller want to load it?
if( doLoad )
{
wxString dname = dso_full_path( aFaceId );
wxString dname = dso_search_path( aFaceId );

wxDynamicLibrary dso;

Expand Down
20 changes: 13 additions & 7 deletions common/pgm_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ PGM_BASE::PGM_BASE()
m_locale = NULL;
m_common_settings = NULL;

m_wx_app = NULL;
m_show_env_var_dialog = true;

setLanguageId( wxLANGUAGE_DEFAULT );
Expand All @@ -296,11 +295,11 @@ PGM_BASE::PGM_BASE()

PGM_BASE::~PGM_BASE()
{
destroy();
Destroy();
}


void PGM_BASE::destroy()
void PGM_BASE::Destroy()
{
// unlike a normal destructor, this is designed to be called more than once safely:

Expand All @@ -315,6 +314,13 @@ void PGM_BASE::destroy()
}


wxApp& PGM_BASE::App()
{
wxASSERT( wxTheApp );
return *wxTheApp;
}


void PGM_BASE::SetEditorName( const wxString& aFileName )
{
m_editor_name = aFileName;
Expand Down Expand Up @@ -383,7 +389,7 @@ const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEdit
}


bool PGM_BASE::initPgm()
bool PGM_BASE::InitPgm()
{
wxFileName pgm_name( App().argv[0] );

Expand Down Expand Up @@ -609,10 +615,10 @@ void PGM_BASE::loadCommonSettings()
}


void PGM_BASE::saveCommonSettings()
void PGM_BASE::SaveCommonSettings()
{
// m_common_settings is not initialized until fairly late in the
// process startup: initPgm(), so test before using:
// process startup: InitPgm(), so test before using:
if( m_common_settings )
{
wxString cur_dir = wxGetCwd();
Expand Down Expand Up @@ -846,7 +852,7 @@ void PGM_BASE::SetLocalEnvVariables( const ENV_VAR_MAP& aEnvVarMap )
if( m_common_settings )
m_common_settings->DeleteGroup( pathEnvVariables );

saveCommonSettings();
SaveCommonSettings();

// Overwrites externally defined environment variable until the next time the application
// is run.
Expand Down
7 changes: 7 additions & 0 deletions scripting/dlist.i → common/swig/dlist.i
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

/* DLIST python iteration code, to allow standard iteration over DLIST */

%include <dlist.h>

%{
#include <dlist.h>
%}


%extend DLIST
{
%pythoncode
Expand Down
65 changes: 65 additions & 0 deletions common/swig/ki_exception.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 2
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

%warnfilter(511) IO_ERROR;

%include ki_exception.h

%{
#include <ki_exception.h>
%}

%include exception.i

// Target a specific function with "C++ to python exception handling and
// translation code". Invoke this macro separately for each C++ function
// that throws. This is a less bulkier scheme than assuming that ALL C++
// functions throw, because they do not all throw.
%define HANDLE_EXCEPTIONS(function)
%exception function {
try
{
$action
}
catch( const IO_ERROR& e )
{
UTF8 str = e.Problem();

SWIG_exception( SWIG_IOError, str.c_str() );
return NULL;
}
catch( const std::exception& e )
{
std::string str = e.what();

SWIG_exception( SWIG_IOError, str.c_str() );
return NULL;
}
catch( ... )
{
SWIG_fail;
}
}
%enddef

29 changes: 14 additions & 15 deletions scripting/kicad.i → common/swig/kicad.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
%include <std_string.i>
%include <std_map.i>
%include <std_shared_ptr.i>
%include <std_set.i>

%include "ki_exception.i" // affects all that follow it

/*
http://www.swig.org/Doc3.0/CPlusPlus11.html
Expand Down Expand Up @@ -70,13 +73,13 @@ principle should be easily implemented by adapting the current STL containers.
%ignore operator <<;
%ignore operator=;

%include dlist.i

// headers/imports that must be included in the _wrapper.cpp at top

%{
#include <macros.h>
#include <cstddef>
#include <dlist.h>
#include <base_struct.h>
#include <class_eda_rect.h>
#include <common.h>
Expand All @@ -93,23 +96,20 @@ principle should be easily implemented by adapting the current STL containers.
%}

// all the wx wrappers for wxString, wxPoint, wxRect, wxChar ..
%include <wx.i>
%include wx.i

// header files that must be wrapped

%include <macros.h>
%include <core/typeinfo.h>
%include <dlist.h>
%include <base_struct.h>
%include <class_eda_rect.h>
%include <common.h>
%include <class_title_block.h>
%include <class_colors_design_settings.h>
%include <class_marker_base.h>
%include <eda_text.h>
%include macros.h
%include core/typeinfo.h
%include base_struct.h
%include class_eda_rect.h
%include common.h
%include class_title_block.h
%include class_colors_design_settings.h
%include class_marker_base.h
%include eda_text.h

// special iteration wrapper for DLIST objects
%include "dlist.i"

// std template mappings
%template(intVector) std::vector<int>;
Expand Down Expand Up @@ -156,4 +156,3 @@ principle should be easily implemented by adapting the current STL containers.

%}
}

Loading

0 comments on commit 7311f07

Please sign in to comment.