From c4fb9f00c88c329deee9c41aeb7c8f8a2640b197 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 27 Aug 2013 11:57:43 +0200 Subject: [PATCH 01/17] Scilab: list helper getScilabListAndSize() --- Lib/scilab/scilist.swg | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/scilab/scilist.swg b/Lib/scilab/scilist.swg index fc73d463a..c585726e4 100644 --- a/Lib/scilab/scilist.swg +++ b/Lib/scilab/scilist.swg @@ -43,6 +43,27 @@ SWIG_GetScilabListSize(SciObject _obj, int *_piListSize) return SWIG_OK; } +SWIGINTERN int +SWIG_GetScilabListAndSize(SciObject _obj, int **_piListAddr, int *_piListSize) +{ + SciErr sciErr; + + sciErr = getVarAddressFromPosition(pvApiCtx, _obj, _piListAddr); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + sciErr = getListItemNumber(pvApiCtx, *_piListAddr, _piListSize); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + return SWIG_OK; +} SWIGINTERN int SWIG_CheckScilabList(SciObject _obj) From 6346803d41322c7f4f085e417106a3c218968886 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 27 Aug 2013 12:53:16 +0200 Subject: [PATCH 02/17] Scilab: fix debug target --- Examples/Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Makefile.in b/Examples/Makefile.in index 48483572b..59d02caa4 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1635,7 +1635,7 @@ scilab_run: # ----------------------------------------------------------------- scilab_debug: - @env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) $(SCILAB_STARTOPT) -f runme.sci + @env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) $(SCILAB_STARTOPT) -debug -f runme.sci # ----------------------------------------------------------------- # Scilab version From 362c7e7bce0c3b2b29fd7910e950f35741415d81 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Wed, 28 Aug 2013 17:00:23 +0200 Subject: [PATCH 03/17] Scilab: add %feature scilab:const (constants are wrapped by Scilab variables) --- Examples/scilab/constants/example.i | 12 ++++++++++ Examples/scilab/constants/runme.sci | 30 ++++++++++++++++------- Lib/scilab/scichar.swg | 34 ++++++++++++++++++++++++++ Lib/scilab/scidouble.swg | 12 ++++++++++ Lib/scilab/sciint.swg | 12 ++++++++++ Lib/scilab/scilab.swg | 1 + Lib/scilab/scimacros.swg | 5 ++++ Lib/scilab/sciruntime.swg | 3 ++- Lib/scilab/scitypemaps.swg | 28 ++++++++++++++++++++++ Source/Modules/scilab.cxx | 37 ++++++++++++++++++++++++----- 10 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 Lib/scilab/scimacros.swg diff --git a/Examples/scilab/constants/example.i b/Examples/scilab/constants/example.i index a79fb4ed9..fbdea586a 100644 --- a/Examples/scilab/constants/example.i +++ b/Examples/scilab/constants/example.i @@ -24,5 +24,17 @@ %constant int iconst = 37; %constant double fconst = 3.14; +/* Now constants are wrapped to Scilab variables */ +%scilabconst(1); + +#define ICONST2 12 +#define FCONST2 4.60 +#define CCONST3 'a' +#define CCONST4 '\n' +#define SCONST3 "Hello World" +#define SCONST4 "\"Hello World\"" + +%constant int iconst2 = 73; +%constant double fconst2 = 6.28; diff --git a/Examples/scilab/constants/runme.sci b/Examples/scilab/constants/runme.sci index 5109f857e..a1afec976 100644 --- a/Examples/scilab/constants/runme.sci +++ b/Examples/scilab/constants/runme.sci @@ -1,15 +1,17 @@ lines(0); exec loader.sce; +SWIG_Init(); -printf("ICONST = %i (should be 42)\n", ICONST_get()); -printf("FCONST = %f (should be 2.1828)\n", FCONST_get()); -printf("CCONST = %c (should be ''x'')\n", CCONST_get()); -printf("CCONST2 = %s (this should be on a new line)\n", CCONST2_get()); -printf("SCONST = %s (should be ''Hello World'')\n", SCONST_get()); -printf("SCONST2 = %s (should be "'""Hello World"""')\n", SCONST2_get()); -printf("EXPR = %f (should be 48.5484)\n", EXPR_get()); -printf("iconst = %i (should be 37)\n", iconst_get()); -printf("fconst = %f (should be 3.14)\n", fconst_get()); +printf("\nConstants are wrapped by functions:\n"); +printf("ICONST_get() = %i (should be 42)\n", ICONST_get()); +printf("FCONST_get() = %5.4f (should be 2.1828)\n", FCONST_get()); +printf("CCONST_get() = ''%c'' (should be ''x'')\n", CCONST_get()); +printf("CCONST2_get() = %s (this should be on a new line)\n", CCONST2_get()); +printf("SCONST_get() = ''%s'' (should be ''Hello World'')\n", SCONST_get()); +printf("SCONST2_get() = ''%s'' (should be "'""Hello World"""')\n", SCONST2_get()); +printf("EXPR_get() = %5.4f (should be 48.5484)\n", EXPR_get()); +printf("iconst_get() = %i (should be 37)\n", iconst_get()); +printf("fconst_get() = %3.2f (should be 3.14)\n", fconst_get()); try printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN_get()); @@ -22,4 +24,14 @@ catch printf("FOO is not defined (good)\n"); end +printf("\nNow constants are wrapped by Scilab variables (feature scilab:const):\n"); +printf("ICONST2 = %i (should be 12)\n", ICONST2); +printf("FCONST2 = %3.2f (should be 4.60)\n", FCONST2); +printf("CCONST3 = ''%c'' (should be ''a'')\n", CCONST3); +printf("CCONST4 = %s (this should be on a new line)\n", CCONST4); +printf("SCONST3 = ''%s'' (should be ''Hello World'')\n", SCONST3); +printf("SCONST4 = ''%s'' (should be "'""Hello World"""')\n", SCONST4); +printf("iconst2 = %i (should be 73)\n", iconst2); +printf("fconst2 = %3.2f (should be 6.28)\n", fconst2); + exit diff --git a/Lib/scilab/scichar.swg b/Lib/scilab/scichar.swg index 96e855929..e165b66d8 100644 --- a/Lib/scilab/scichar.swg +++ b/Lib/scilab/scichar.swg @@ -252,3 +252,37 @@ SwigScilabStringFromCharPtrArray(void *_pvApiCtx, int _iVarOut, char **_charPtrA return SWIG_NbInputArgument(pvApiCtx) + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(char), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(char)(void *_pvApiCtx, const char* _psVariableName, const char _cVariableValue) { + SciErr sciErr; + char sValue[2]; + const char* psStrings[1]; + + sValue[0] = _cVariableValue; + sValue[1] = '\0'; + psStrings[0] = sValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} +%fragment(SWIG_CreateScilabVariable_frag(charptr), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(charptr)(void *_pvApiCtx, const char* _psVariableName, const char* _psVariableValue) { + SciErr sciErr; + const char* psStrings[1]; + psStrings[0] = _psVariableValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scidouble.swg b/Lib/scilab/scidouble.swg index 0eb9d8ae4..94a69671a 100644 --- a/Lib/scilab/scidouble.swg +++ b/Lib/scilab/scidouble.swg @@ -118,3 +118,15 @@ SWIG_SciDouble_FromDoubleArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(double), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(double)(void *_pvApiCtx, const char* _psVariableName, const double _dVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfDouble(_pvApiCtx, _psVariableName, 1, 1, &_dVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/sciint.swg b/Lib/scilab/sciint.swg index f45fb8524..e2758eb74 100644 --- a/Lib/scilab/sciint.swg +++ b/Lib/scilab/sciint.swg @@ -196,3 +196,15 @@ SWIG_SciInt32_FromIntArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, int return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(int), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(int)(void *_pvApiCtx, const char* _psVariableName, const int _iVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfInteger32(_pvApiCtx, _psVariableName, 1, 1, &_iVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scilab.swg b/Lib/scilab/scilab.swg index ac24d159f..3b5f6e817 100644 --- a/Lib/scilab/scilab.swg +++ b/Lib/scilab/scilab.swg @@ -1,5 +1,6 @@ %include %include +%include %include %include diff --git a/Lib/scilab/scimacros.swg b/Lib/scilab/scimacros.swg new file mode 100644 index 000000000..669ca893f --- /dev/null +++ b/Lib/scilab/scimacros.swg @@ -0,0 +1,5 @@ + #define %scilabconst(flag) %feature("scilab:const","flag") + +// Create Scilab variable +#define SWIG_CreateScilabVariable_frag(Type...) %fragment_name(CreateScilabVariable, Type) +#define SWIG_CreateScilabVariable_dec(Type...) %symbol_name(CreateScilabVariable, Type) diff --git a/Lib/scilab/sciruntime.swg b/Lib/scilab/sciruntime.swg index e8b52b12b..86471bda4 100644 --- a/Lib/scilab/sciruntime.swg +++ b/Lib/scilab/sciruntime.swg @@ -78,7 +78,6 @@ SWIG_Scilab_ErrorMsg(int code, const char *mesg) /* Used for C++ enums */ //#define SWIG_AsVal_int(scilabValue, valuePointer) SWIG_SciDouble_AsInt(pvApiCtx, scilabValue, valuePointer, fname) - #if SCILAB_VERSION_54_OR_HIGHER #define SWIG_CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) #define SWIG_CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) @@ -272,6 +271,8 @@ SWIG_Scilab_SetModule(swig_module_info *swig_module) %init %{ #ifdef __cplusplus extern "C" +#endif int SWIG_Init(char *fname, unsigned long fname_len) { SWIG_InitializeModule(NULL); + SWIG_CreateScilabVariables(); %} diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index 657717cda..f6eeb27aa 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -376,3 +376,31 @@ %typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { SCILAB_TYPECHECK(isStringType) } //%apply int { size_t }; + +/* -----------------------------------------------------------------------------*/ +/* Constants +/* -----------------------------------------------------------------------------*/ + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int +%{ + if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(double)) double +%{ + if (SWIG_CreateScilabVariable_double(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(char)) char +%{ + if (SWIG_CreateScilabVariable_char(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(charptr)) char * +%{ + if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 7ec5849eb..8cb430c94 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -24,6 +24,7 @@ Scilab options\n\ -vbl sets the build verbose level (default 0)\n\n"; const char* SWIG_INIT_FUNCTION_NAME = "SWIG_Init"; +const char* SWIG_CREATE_VARIABLES_FUNCTION_NAME = "SWIG_CreateScilabVariables"; class SCILAB : public Language { protected: @@ -34,6 +35,8 @@ protected: File *wrappersSection; File *initSection; + String *variablesCode; + File *builderFile; String *builderCode; int builderFunctionCount; @@ -43,7 +46,6 @@ protected: String *ldflag; String* verboseBuildLevel; - public: /* ------------------------------------------------------------------------ * main() @@ -185,10 +187,12 @@ public: Printf(builderCode, "table = ["); - /* In C++ mode, add initialization function to builder table */ - if (CPlusPlus) { - Printf(builderCode, "\"%s\",\"%s\";", SWIG_INIT_FUNCTION_NAME, SWIG_INIT_FUNCTION_NAME); - } + /* add initialization function to builder table */ + addFunctionInBuilder(NewString(SWIG_INIT_FUNCTION_NAME), NewString(SWIG_INIT_FUNCTION_NAME)); + + // Open Scilab wrapper variables creation function + variablesCode = NewString(""); + Printf(variablesCode, "int %s() {\n", SWIG_CREATE_VARIABLES_FUNCTION_NAME); /* Emit code for children */ if (CPlusPlus) { @@ -201,6 +205,9 @@ public: Printf(wrappersSection, "}\n"); } + // Close Scilab wrapper variables creation function + Printf(variablesCode, " return SWIG_OK;\n}\n"); + /* Write all to the builder.sce file */ Printf(builderCode, "];\n"); Printf(builderCode, "if ~isempty(table) then\n"); @@ -213,13 +220,14 @@ public: Delete(builderFile); /* Close the init function (opened in sciinit.swg) */ - Printf(initSection, "return 0;\n}\n#endif\n"); + Printf(initSection, "return 0;\n}\n"); /* Write all to the wrapper file */ SwigType_emit_type_table(runtimeSection, wrappersSection); // Declare pointer types, ... (Ex: SWIGTYPE_p_p_double) Dump(runtimeSection, beginSection); Dump(headerSection, beginSection); Dump(wrappersSection, beginSection); + Dump(variablesCode, beginSection); Wrapper_pretty_print(initSection, beginSection); /* Cleanup files */ @@ -560,6 +568,23 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; + // Constants of simple type are wrapped to Scilab variables + if (GetFlag(node, "feature:scilab:const")) { + if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) { + constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); + if (constantTypemap != NULL) { + //String *wrapName = NewString(""); + //Printf(wrapName, "Swig%s", constantName); + Setattr(node, "wrap:name", constantName); + Replaceall(constantTypemap, "$result", constantName); + Replaceall(constantTypemap, "$value", constantValue); + emit_action_code(node, variablesCode, constantTypemap); + Delete(constantTypemap); + return SWIG_OK; + } + } + } + /* Create variables for member pointer constants, not suppported by typemaps (like Python wrapper does) */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(constantName); From 1b6fff9da50484cd3cfd54cf91d15224f25fef1e Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 29 Aug 2013 09:07:29 +0200 Subject: [PATCH 04/17] Scilab: fix portability issue in configure.ac --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 8e1419a36..c549d92e4 100644 --- a/configure.ac +++ b/configure.ac @@ -1031,7 +1031,7 @@ if test -n "$SCILAB"; then AC_MSG_CHECKING(for Scilab startup options) SCILABSTARTOPT="-nwni -nb" if test $SCILAB_VERSION -ge 54; then - SCILABSTARTOPT+=" -noatomsautoload" + SCILABSTARTOPT="$SCILABSTARTOPT -noatomsautoload" fi AC_MSG_RESULT($SCILABSTARTOPT) fi From f1d289925a49db5c212685a89317779d9042ce58 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 29 Aug 2013 18:14:59 +0200 Subject: [PATCH 05/17] Scilab: wrap enums to Scilab variables (if %feature scilab:const") --- Examples/scilab/enum/example.i | 2 ++ Examples/scilab/enum/runme.sci | 13 ++++++------ Lib/scilab/scitypemaps.swg | 8 +++++++- Source/Modules/scilab.cxx | 36 ++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 23ee8a822..634352b03 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,6 +1,8 @@ /* File : example.i */ %module example +%scilabconst(1); + %{ #include "example.h" %} diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index a5f16f3f8..e03fac505 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -1,18 +1,19 @@ lines(0); exec loader.sce; +SWIG_Init(); // Print out the value of some enums printf("*** color ***\n"); -printf(" RED = %i\n", RED_get()); -printf(" BLUE = %i\n", BLUE_get()); -printf(" GREEN = %i\n", GREEN_get()); +printf(" RED = %i\n", RED); +printf(" BLUE = %i\n", BLUE); +printf(" GREEN = %i\n", GREEN); printf("\nTesting use of enums with functions\n"); -enum_test(RED_get()); -enum_test(BLUE_get()); -enum_test(GREEN_get()); +enum_test(RED); +enum_test(BLUE); +enum_test(GREEN); enum_test(int32(1234)); exit diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index f6eeb27aa..33cf288a6 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -378,7 +378,7 @@ //%apply int { size_t }; /* -----------------------------------------------------------------------------*/ -/* Constants +/* Constants and enums to Scilab variables /* -----------------------------------------------------------------------------*/ %typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int @@ -404,3 +404,9 @@ if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK) return SWIG_ERROR; %} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) enum SWIGTYPE +%{ + if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 8cb430c94..3bcbdd0c7 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -568,15 +568,19 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; - // Constants of simple type are wrapped to Scilab variables + // If feature scilab:const enabled, constants & enums are wrapped to Scilab variables if (GetFlag(node, "feature:scilab:const")) { - if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) { + bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)); + bool isEnum = (Cmp(nodeType(node), "enumitem") == 0); + + if (isConstant || isEnum) { constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); if (constantTypemap != NULL) { - //String *wrapName = NewString(""); - //Printf(wrapName, "Swig%s", constantName); Setattr(node, "wrap:name", constantName); Replaceall(constantTypemap, "$result", constantName); + if (isEnum) { + constantValue = Getattr(node, "enumvalue"); + } Replaceall(constantTypemap, "$value", constantValue); emit_action_code(node, variablesCode, constantTypemap); Delete(constantTypemap); @@ -630,6 +634,30 @@ public: * enumvalueDeclaration() * --------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *node) { + static int iPreviousEnumValue = 0; + + if (GetFlag(node, "feature:scilab:const")) { + // Compute the "absolute" value of enum if needed + // (most of time enum values are a linked list of relative values) + String *enumValue = Getattr(node, "enumvalue"); + if (!enumValue) { + String *enumValueEx = Getattr(node, "enumvalueex"); + if (enumValueEx) { + String *firstenumitem = Getattr(node, "firstenumitem"); + if (firstenumitem) { + // First node, value is in enumValueEx + Setattr(node, "enumvalue", enumValueEx); + iPreviousEnumValue = atoi(Char(enumValueEx)); + } + else { + enumValue = NewString(""); + iPreviousEnumValue = iPreviousEnumValue + 1; + Printf(enumValue, "%d", iPreviousEnumValue); + Setattr(node, "enumvalue", enumValue); + } + } + } + } /* Force type to be an enum (See scitypemaps.swg) */ Setattr(node, "type", "enum SWIG"); From 74aebf252d65696a616c40f9c83bacbaee67efc0 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:03:20 +0200 Subject: [PATCH 06/17] Scilab: consider int as default type for STL container of values (so it works for container of enums) --- Lib/scilab/scisequence.swg | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Lib/scilab/scisequence.swg b/Lib/scilab/scisequence.swg index 9eefd94fb..56f6eb196 100644 --- a/Lib/scilab/scisequence.swg +++ b/Lib/scilab/scisequence.swg @@ -34,6 +34,11 @@ // %fragment(SWIG_Traits_Sequence_frag(ptr), "header", + fragment=SWIG_AsCheck_Sequence_frag(int), + fragment=SWIG_AsGet_Sequence_frag(int), + fragment=SWIG_AsSize_Sequence_frag(int), + fragment=SWIG_FromCreate_Sequence_frag(int), + fragment=SWIG_FromSet_Sequence_frag(int), fragment=SWIG_AsCheck_Sequence_frag(ptr), fragment=SWIG_AsGet_Sequence_frag(ptr), fragment=SWIG_AsSize_Sequence_frag(ptr), @@ -42,29 +47,29 @@ fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_as_sequence { static int check(SciObject obj) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsCheck_Sequence_dec(int)(obj); } static int get(SciObject obj, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsGet_Sequence_dec(int)(obj, (int **)sequence); } static int size(SciObject obj, int *size) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsSize_Sequence_dec(int)(obj, size); } }; template struct traits_from_sequence { static int create(int size, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromCreate_Sequence_dec(int)(size, (int **)sequence); } static SciObject set(int size, void *sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromSet_Sequence_dec(int)(size, (int *)sequence); } }; - // But supports containers of pointers + // For sequence of pointers template struct traits_as_sequence { static int check(SciObject obj) { @@ -127,25 +132,27 @@ namespace swig { // %fragment(SWIG_Traits_SequenceItem_frag(ptr), "header", + fragment=SWIG_AsVal_SequenceItem_frag(int), + fragment=SWIG_From_SequenceItem_frag(int), fragment=SWIG_AsVal_SequenceItem_frag(ptr), fragment=SWIG_From_SequenceItem_frag(ptr), fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T *pItemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsVal_SequenceItem_dec(int)(obj, (int *)pSequence, iItemIndex, (int *)pItemValue); } }; template struct traits_from_sequenceitem { static int from(void *pSequence, int iItemIndex, T itemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_From_SequenceItem_dec(int)((int *)pSequence, iItemIndex, (int)itemValue); } }; - // But supports containers of pointers + // Sequence of pointers template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T **pItemValue) { From 39bab12d2ca85c023495e84fdd8100231bb2585d Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:41:42 +0200 Subject: [PATCH 07/17] Scilab: add %scilabconst in enum example --- Examples/scilab/enum/example.c | 13 +++++++++ Examples/scilab/enum/example.i | 8 +++--- Examples/scilab/enum/runme.sci | 31 ++++++++++++++-------- Examples/scilab/enum/scilabconst_example.h | 3 +++ 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 Examples/scilab/enum/scilabconst_example.h diff --git a/Examples/scilab/enum/example.c b/Examples/scilab/enum/example.c index 6df9203ce..0dbe4cda7 100644 --- a/Examples/scilab/enum/example.c +++ b/Examples/scilab/enum/example.c @@ -1,6 +1,7 @@ /* File : example.c */ #include "example.h" +#include "scilabconst_example.h" #include void enum_test(color c) { @@ -14,3 +15,15 @@ void enum_test(color c) { printf("color = Unknown color!\n"); } } + +void scilabconst_enum_test(fruit f) { + if (f == APPLE) { + printf("fruit = APPLE\n"); + } else if (f == ORANGE) { + printf("fruit = ORANGE\n"); + } else if (f == LEMON) { + printf("fruit = LEMON\n"); + } else { + printf("fruit = Unknown fruit!\n"); + } +} diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 634352b03..6a471fde0 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,13 +1,13 @@ /* File : example.i */ %module example -%scilabconst(1); - %{ #include "example.h" +#include "scilabconst_example.h" %} -/* Let's just grab the original header file here */ - %include "example.h" +%scilabconst(1); + +%include "scilabconst_example.h" diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index e03fac505..182fa0c61 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -2,19 +2,28 @@ lines(0); exec loader.sce; SWIG_Init(); -// Print out the value of some enums +printf("\nTesting use of enums wrapped as Scilab functions\n"); + printf("*** color ***\n"); -printf(" RED = %i\n", RED); -printf(" BLUE = %i\n", BLUE); -printf(" GREEN = %i\n", GREEN); +printf(" RED = %i\n", RED_get()); +printf(" BLUE = %i\n", BLUE_get()); +printf(" GREEN = %i\n", GREEN_get()); - -printf("\nTesting use of enums with functions\n"); - -enum_test(RED); -enum_test(BLUE); -enum_test(GREEN); +enum_test(RED_get()); +enum_test(BLUE_get()); +enum_test(GREEN_get()); enum_test(int32(1234)); -exit +printf("\nTesting use of enums wrapped as Scilab variables\n"); +printf("*** fruit ***\n"); +printf(" APPLE = %i\n", APPLE); +printf(" ORANGE = %i\n", ORANGE); +printf(" LEMON = %i\n", LEMON); + +scilabconst_enum_test(APPLE); +scilabconst_enum_test(ORANGE); +scilabconst_enum_test(LEMON); +scilabconst_enum_test(int32(1234)); + +exit diff --git a/Examples/scilab/enum/scilabconst_example.h b/Examples/scilab/enum/scilabconst_example.h new file mode 100644 index 000000000..92dcaaf61 --- /dev/null +++ b/Examples/scilab/enum/scilabconst_example.h @@ -0,0 +1,3 @@ +typedef enum { APPLE, ORANGE, LEMON } fruit; + +void scilabconst_enum_test(fruit f); From 1ab2bd15b17fa54cfe824e87e6e73e3d18cacb29 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:46:45 +0200 Subject: [PATCH 08/17] Scilab: clean enum management code (no need to force enum type) --- Source/Modules/scilab.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 3bcbdd0c7..89d39d357 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -659,9 +659,6 @@ public: } } - /* Force type to be an enum (See scitypemaps.swg) */ - Setattr(node, "type", "enum SWIG"); - return Language::enumvalueDeclaration(node); } From fcfb90219e10f4bc7f6d564e2aac9c769958b427 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 27 Aug 2013 11:57:43 +0200 Subject: [PATCH 09/17] Scilab: list helper getScilabListAndSize() --- Lib/scilab/scilist.swg | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/scilab/scilist.swg b/Lib/scilab/scilist.swg index fc73d463a..c585726e4 100644 --- a/Lib/scilab/scilist.swg +++ b/Lib/scilab/scilist.swg @@ -43,6 +43,27 @@ SWIG_GetScilabListSize(SciObject _obj, int *_piListSize) return SWIG_OK; } +SWIGINTERN int +SWIG_GetScilabListAndSize(SciObject _obj, int **_piListAddr, int *_piListSize) +{ + SciErr sciErr; + + sciErr = getVarAddressFromPosition(pvApiCtx, _obj, _piListAddr); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + sciErr = getListItemNumber(pvApiCtx, *_piListAddr, _piListSize); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + return SWIG_OK; +} SWIGINTERN int SWIG_CheckScilabList(SciObject _obj) From 2cf606c6389b53417da134fe2bfca69e61605f98 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Wed, 28 Aug 2013 17:00:23 +0200 Subject: [PATCH 10/17] Scilab: add %feature scilab:const (constants are wrapped by Scilab variables) --- Examples/scilab/constants/example.i | 12 +++++++++ Examples/scilab/constants/runme.sci | 30 +++++++++++++++------- Lib/scilab/scichar.swg | 34 +++++++++++++++++++++++++ Lib/scilab/scidouble.swg | 12 +++++++++ Lib/scilab/sciint.swg | 12 +++++++++ Lib/scilab/scilab.swg | 1 + Lib/scilab/scimacros.swg | 5 ++++ Lib/scilab/sciruntime.swg | 3 ++- Lib/scilab/scitypemaps.swg | 28 +++++++++++++++++++++ Source/Modules/scilab.cxx | 39 +++++++++++++++++++++++------ 10 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 Lib/scilab/scimacros.swg diff --git a/Examples/scilab/constants/example.i b/Examples/scilab/constants/example.i index a79fb4ed9..fbdea586a 100644 --- a/Examples/scilab/constants/example.i +++ b/Examples/scilab/constants/example.i @@ -24,5 +24,17 @@ %constant int iconst = 37; %constant double fconst = 3.14; +/* Now constants are wrapped to Scilab variables */ +%scilabconst(1); + +#define ICONST2 12 +#define FCONST2 4.60 +#define CCONST3 'a' +#define CCONST4 '\n' +#define SCONST3 "Hello World" +#define SCONST4 "\"Hello World\"" + +%constant int iconst2 = 73; +%constant double fconst2 = 6.28; diff --git a/Examples/scilab/constants/runme.sci b/Examples/scilab/constants/runme.sci index 5109f857e..a1afec976 100644 --- a/Examples/scilab/constants/runme.sci +++ b/Examples/scilab/constants/runme.sci @@ -1,15 +1,17 @@ lines(0); exec loader.sce; +SWIG_Init(); -printf("ICONST = %i (should be 42)\n", ICONST_get()); -printf("FCONST = %f (should be 2.1828)\n", FCONST_get()); -printf("CCONST = %c (should be ''x'')\n", CCONST_get()); -printf("CCONST2 = %s (this should be on a new line)\n", CCONST2_get()); -printf("SCONST = %s (should be ''Hello World'')\n", SCONST_get()); -printf("SCONST2 = %s (should be "'""Hello World"""')\n", SCONST2_get()); -printf("EXPR = %f (should be 48.5484)\n", EXPR_get()); -printf("iconst = %i (should be 37)\n", iconst_get()); -printf("fconst = %f (should be 3.14)\n", fconst_get()); +printf("\nConstants are wrapped by functions:\n"); +printf("ICONST_get() = %i (should be 42)\n", ICONST_get()); +printf("FCONST_get() = %5.4f (should be 2.1828)\n", FCONST_get()); +printf("CCONST_get() = ''%c'' (should be ''x'')\n", CCONST_get()); +printf("CCONST2_get() = %s (this should be on a new line)\n", CCONST2_get()); +printf("SCONST_get() = ''%s'' (should be ''Hello World'')\n", SCONST_get()); +printf("SCONST2_get() = ''%s'' (should be "'""Hello World"""')\n", SCONST2_get()); +printf("EXPR_get() = %5.4f (should be 48.5484)\n", EXPR_get()); +printf("iconst_get() = %i (should be 37)\n", iconst_get()); +printf("fconst_get() = %3.2f (should be 3.14)\n", fconst_get()); try printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN_get()); @@ -22,4 +24,14 @@ catch printf("FOO is not defined (good)\n"); end +printf("\nNow constants are wrapped by Scilab variables (feature scilab:const):\n"); +printf("ICONST2 = %i (should be 12)\n", ICONST2); +printf("FCONST2 = %3.2f (should be 4.60)\n", FCONST2); +printf("CCONST3 = ''%c'' (should be ''a'')\n", CCONST3); +printf("CCONST4 = %s (this should be on a new line)\n", CCONST4); +printf("SCONST3 = ''%s'' (should be ''Hello World'')\n", SCONST3); +printf("SCONST4 = ''%s'' (should be "'""Hello World"""')\n", SCONST4); +printf("iconst2 = %i (should be 73)\n", iconst2); +printf("fconst2 = %3.2f (should be 6.28)\n", fconst2); + exit diff --git a/Lib/scilab/scichar.swg b/Lib/scilab/scichar.swg index 96e855929..e165b66d8 100644 --- a/Lib/scilab/scichar.swg +++ b/Lib/scilab/scichar.swg @@ -252,3 +252,37 @@ SwigScilabStringFromCharPtrArray(void *_pvApiCtx, int _iVarOut, char **_charPtrA return SWIG_NbInputArgument(pvApiCtx) + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(char), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(char)(void *_pvApiCtx, const char* _psVariableName, const char _cVariableValue) { + SciErr sciErr; + char sValue[2]; + const char* psStrings[1]; + + sValue[0] = _cVariableValue; + sValue[1] = '\0'; + psStrings[0] = sValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} +%fragment(SWIG_CreateScilabVariable_frag(charptr), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(charptr)(void *_pvApiCtx, const char* _psVariableName, const char* _psVariableValue) { + SciErr sciErr; + const char* psStrings[1]; + psStrings[0] = _psVariableValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scidouble.swg b/Lib/scilab/scidouble.swg index 0eb9d8ae4..94a69671a 100644 --- a/Lib/scilab/scidouble.swg +++ b/Lib/scilab/scidouble.swg @@ -118,3 +118,15 @@ SWIG_SciDouble_FromDoubleArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(double), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(double)(void *_pvApiCtx, const char* _psVariableName, const double _dVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfDouble(_pvApiCtx, _psVariableName, 1, 1, &_dVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/sciint.swg b/Lib/scilab/sciint.swg index f45fb8524..e2758eb74 100644 --- a/Lib/scilab/sciint.swg +++ b/Lib/scilab/sciint.swg @@ -196,3 +196,15 @@ SWIG_SciInt32_FromIntArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, int return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(int), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(int)(void *_pvApiCtx, const char* _psVariableName, const int _iVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfInteger32(_pvApiCtx, _psVariableName, 1, 1, &_iVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scilab.swg b/Lib/scilab/scilab.swg index ac24d159f..3b5f6e817 100644 --- a/Lib/scilab/scilab.swg +++ b/Lib/scilab/scilab.swg @@ -1,5 +1,6 @@ %include %include +%include %include %include diff --git a/Lib/scilab/scimacros.swg b/Lib/scilab/scimacros.swg new file mode 100644 index 000000000..669ca893f --- /dev/null +++ b/Lib/scilab/scimacros.swg @@ -0,0 +1,5 @@ + #define %scilabconst(flag) %feature("scilab:const","flag") + +// Create Scilab variable +#define SWIG_CreateScilabVariable_frag(Type...) %fragment_name(CreateScilabVariable, Type) +#define SWIG_CreateScilabVariable_dec(Type...) %symbol_name(CreateScilabVariable, Type) diff --git a/Lib/scilab/sciruntime.swg b/Lib/scilab/sciruntime.swg index e8b52b12b..86471bda4 100644 --- a/Lib/scilab/sciruntime.swg +++ b/Lib/scilab/sciruntime.swg @@ -78,7 +78,6 @@ SWIG_Scilab_ErrorMsg(int code, const char *mesg) /* Used for C++ enums */ //#define SWIG_AsVal_int(scilabValue, valuePointer) SWIG_SciDouble_AsInt(pvApiCtx, scilabValue, valuePointer, fname) - #if SCILAB_VERSION_54_OR_HIGHER #define SWIG_CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) #define SWIG_CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) @@ -272,6 +271,8 @@ SWIG_Scilab_SetModule(swig_module_info *swig_module) %init %{ #ifdef __cplusplus extern "C" +#endif int SWIG_Init(char *fname, unsigned long fname_len) { SWIG_InitializeModule(NULL); + SWIG_CreateScilabVariables(); %} diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index 657717cda..f6eeb27aa 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -376,3 +376,31 @@ %typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { SCILAB_TYPECHECK(isStringType) } //%apply int { size_t }; + +/* -----------------------------------------------------------------------------*/ +/* Constants +/* -----------------------------------------------------------------------------*/ + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int +%{ + if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(double)) double +%{ + if (SWIG_CreateScilabVariable_double(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(char)) char +%{ + if (SWIG_CreateScilabVariable_char(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(charptr)) char * +%{ + if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 47322e76b..c2f1b8c8d 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -24,6 +24,7 @@ Scilab options\n\ -vbl sets the build verbose level (default 0)\n\n"; const char *SWIG_INIT_FUNCTION_NAME = "SWIG_Init"; +const char *SWIG_CREATE_VARIABLES_FUNCTION_NAME = "SWIG_CreateScilabVariables"; class SCILAB:public Language { protected: @@ -34,6 +35,8 @@ protected: File *wrappersSection; File *initSection; + String *variablesCode; + File *builderFile; String *builderCode; int builderFunctionCount; @@ -41,9 +44,8 @@ protected: List *sourceFileList; String *cflag; String *ldflag; - + String *verboseBuildLevel; - public: /* ------------------------------------------------------------------------ * main() @@ -180,10 +182,12 @@ public: Printf(builderCode, "table = ["); - /* In C++ mode, add initialization function to builder table */ - if (CPlusPlus) { - Printf(builderCode, "\"%s\",\"%s\";", SWIG_INIT_FUNCTION_NAME, SWIG_INIT_FUNCTION_NAME); - } + /* add initialization function to builder table */ + addFunctionInBuilder(NewString(SWIG_INIT_FUNCTION_NAME), NewString(SWIG_INIT_FUNCTION_NAME)); + + // Open Scilab wrapper variables creation function + variablesCode = NewString(""); + Printf(variablesCode, "int %s() {\n", SWIG_CREATE_VARIABLES_FUNCTION_NAME); /* Emit code for children */ if (CPlusPlus) { @@ -196,6 +200,9 @@ public: Printf(wrappersSection, "}\n"); } + // Close Scilab wrapper variables creation function + Printf(variablesCode, " return SWIG_OK;\n}\n"); + /* Write all to the builder.sce file */ Printf(builderCode, "];\n"); Printf(builderCode, "if ~isempty(table) then\n"); @@ -208,13 +215,14 @@ public: Delete(builderFile); /* Close the init function (opened in sciinit.swg) */ - Printf(initSection, "return 0;\n}\n#endif\n"); + Printf(initSection, "return 0;\n}\n"); /* Write all to the wrapper file */ SwigType_emit_type_table(runtimeSection, wrappersSection); // Declare pointer types, ... (Ex: SWIGTYPE_p_p_double) Dump(runtimeSection, beginSection); Dump(headerSection, beginSection); Dump(wrappersSection, beginSection); + Dump(variablesCode, beginSection); Wrapper_pretty_print(initSection, beginSection); /* Cleanup files */ @@ -556,6 +564,23 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; + // Constants of simple type are wrapped to Scilab variables + if (GetFlag(node, "feature:scilab:const")) { + if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) { + constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); + if (constantTypemap != NULL) { + //String *wrapName = NewString(""); + //Printf(wrapName, "Swig%s", constantName); + Setattr(node, "wrap:name", constantName); + Replaceall(constantTypemap, "$result", constantName); + Replaceall(constantTypemap, "$value", constantValue); + emit_action_code(node, variablesCode, constantTypemap); + Delete(constantTypemap); + return SWIG_OK; + } + } + } + /* Create variables for member pointer constants, not suppported by typemaps (like Python wrapper does) */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(constantName); From ed135cb99c7101558f20ab7c30ed3abed2e99710 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 29 Aug 2013 18:14:59 +0200 Subject: [PATCH 11/17] Scilab: wrap enums to Scilab variables (if %feature scilab:const") --- Examples/scilab/enum/example.i | 2 ++ Examples/scilab/enum/runme.sci | 13 ++++++------ Lib/scilab/scitypemaps.swg | 8 +++++++- Source/Modules/scilab.cxx | 36 ++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 23ee8a822..634352b03 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,6 +1,8 @@ /* File : example.i */ %module example +%scilabconst(1); + %{ #include "example.h" %} diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index a5f16f3f8..e03fac505 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -1,18 +1,19 @@ lines(0); exec loader.sce; +SWIG_Init(); // Print out the value of some enums printf("*** color ***\n"); -printf(" RED = %i\n", RED_get()); -printf(" BLUE = %i\n", BLUE_get()); -printf(" GREEN = %i\n", GREEN_get()); +printf(" RED = %i\n", RED); +printf(" BLUE = %i\n", BLUE); +printf(" GREEN = %i\n", GREEN); printf("\nTesting use of enums with functions\n"); -enum_test(RED_get()); -enum_test(BLUE_get()); -enum_test(GREEN_get()); +enum_test(RED); +enum_test(BLUE); +enum_test(GREEN); enum_test(int32(1234)); exit diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index f6eeb27aa..33cf288a6 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -378,7 +378,7 @@ //%apply int { size_t }; /* -----------------------------------------------------------------------------*/ -/* Constants +/* Constants and enums to Scilab variables /* -----------------------------------------------------------------------------*/ %typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int @@ -404,3 +404,9 @@ if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK) return SWIG_ERROR; %} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) enum SWIGTYPE +%{ + if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index c2f1b8c8d..57acc5623 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -564,15 +564,19 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; - // Constants of simple type are wrapped to Scilab variables + // If feature scilab:const enabled, constants & enums are wrapped to Scilab variables if (GetFlag(node, "feature:scilab:const")) { - if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) { + bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)); + bool isEnum = (Cmp(nodeType(node), "enumitem") == 0); + + if (isConstant || isEnum) { constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); if (constantTypemap != NULL) { - //String *wrapName = NewString(""); - //Printf(wrapName, "Swig%s", constantName); Setattr(node, "wrap:name", constantName); Replaceall(constantTypemap, "$result", constantName); + if (isEnum) { + constantValue = Getattr(node, "enumvalue"); + } Replaceall(constantTypemap, "$value", constantValue); emit_action_code(node, variablesCode, constantTypemap); Delete(constantTypemap); @@ -626,6 +630,30 @@ public: * enumvalueDeclaration() * --------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *node) { + static int iPreviousEnumValue = 0; + + if (GetFlag(node, "feature:scilab:const")) { + // Compute the "absolute" value of enum if needed + // (most of time enum values are a linked list of relative values) + String *enumValue = Getattr(node, "enumvalue"); + if (!enumValue) { + String *enumValueEx = Getattr(node, "enumvalueex"); + if (enumValueEx) { + String *firstenumitem = Getattr(node, "firstenumitem"); + if (firstenumitem) { + // First node, value is in enumValueEx + Setattr(node, "enumvalue", enumValueEx); + iPreviousEnumValue = atoi(Char(enumValueEx)); + } + else { + enumValue = NewString(""); + iPreviousEnumValue = iPreviousEnumValue + 1; + Printf(enumValue, "%d", iPreviousEnumValue); + Setattr(node, "enumvalue", enumValue); + } + } + } + } /* Force type to be an enum (See scitypemaps.swg) */ Setattr(node, "type", "enum SWIG"); From 2610403d69ab8230bec0b1d3ecffd23c62315a99 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:03:20 +0200 Subject: [PATCH 12/17] Scilab: consider int as default type for STL container of values (so it works for container of enums) --- Lib/scilab/scisequence.swg | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Lib/scilab/scisequence.swg b/Lib/scilab/scisequence.swg index 9eefd94fb..56f6eb196 100644 --- a/Lib/scilab/scisequence.swg +++ b/Lib/scilab/scisequence.swg @@ -34,6 +34,11 @@ // %fragment(SWIG_Traits_Sequence_frag(ptr), "header", + fragment=SWIG_AsCheck_Sequence_frag(int), + fragment=SWIG_AsGet_Sequence_frag(int), + fragment=SWIG_AsSize_Sequence_frag(int), + fragment=SWIG_FromCreate_Sequence_frag(int), + fragment=SWIG_FromSet_Sequence_frag(int), fragment=SWIG_AsCheck_Sequence_frag(ptr), fragment=SWIG_AsGet_Sequence_frag(ptr), fragment=SWIG_AsSize_Sequence_frag(ptr), @@ -42,29 +47,29 @@ fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_as_sequence { static int check(SciObject obj) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsCheck_Sequence_dec(int)(obj); } static int get(SciObject obj, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsGet_Sequence_dec(int)(obj, (int **)sequence); } static int size(SciObject obj, int *size) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsSize_Sequence_dec(int)(obj, size); } }; template struct traits_from_sequence { static int create(int size, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromCreate_Sequence_dec(int)(size, (int **)sequence); } static SciObject set(int size, void *sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromSet_Sequence_dec(int)(size, (int *)sequence); } }; - // But supports containers of pointers + // For sequence of pointers template struct traits_as_sequence { static int check(SciObject obj) { @@ -127,25 +132,27 @@ namespace swig { // %fragment(SWIG_Traits_SequenceItem_frag(ptr), "header", + fragment=SWIG_AsVal_SequenceItem_frag(int), + fragment=SWIG_From_SequenceItem_frag(int), fragment=SWIG_AsVal_SequenceItem_frag(ptr), fragment=SWIG_From_SequenceItem_frag(ptr), fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T *pItemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsVal_SequenceItem_dec(int)(obj, (int *)pSequence, iItemIndex, (int *)pItemValue); } }; template struct traits_from_sequenceitem { static int from(void *pSequence, int iItemIndex, T itemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_From_SequenceItem_dec(int)((int *)pSequence, iItemIndex, (int)itemValue); } }; - // But supports containers of pointers + // Sequence of pointers template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T **pItemValue) { From 698e399717286e31eefc1b17ffeae4f973a817f6 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:41:42 +0200 Subject: [PATCH 13/17] Scilab: add %scilabconst in enum example --- Examples/scilab/enum/example.c | 13 +++++++++ Examples/scilab/enum/example.i | 8 +++--- Examples/scilab/enum/runme.sci | 31 ++++++++++++++-------- Examples/scilab/enum/scilabconst_example.h | 3 +++ 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 Examples/scilab/enum/scilabconst_example.h diff --git a/Examples/scilab/enum/example.c b/Examples/scilab/enum/example.c index 6df9203ce..0dbe4cda7 100644 --- a/Examples/scilab/enum/example.c +++ b/Examples/scilab/enum/example.c @@ -1,6 +1,7 @@ /* File : example.c */ #include "example.h" +#include "scilabconst_example.h" #include void enum_test(color c) { @@ -14,3 +15,15 @@ void enum_test(color c) { printf("color = Unknown color!\n"); } } + +void scilabconst_enum_test(fruit f) { + if (f == APPLE) { + printf("fruit = APPLE\n"); + } else if (f == ORANGE) { + printf("fruit = ORANGE\n"); + } else if (f == LEMON) { + printf("fruit = LEMON\n"); + } else { + printf("fruit = Unknown fruit!\n"); + } +} diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 634352b03..6a471fde0 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,13 +1,13 @@ /* File : example.i */ %module example -%scilabconst(1); - %{ #include "example.h" +#include "scilabconst_example.h" %} -/* Let's just grab the original header file here */ - %include "example.h" +%scilabconst(1); + +%include "scilabconst_example.h" diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index e03fac505..182fa0c61 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -2,19 +2,28 @@ lines(0); exec loader.sce; SWIG_Init(); -// Print out the value of some enums +printf("\nTesting use of enums wrapped as Scilab functions\n"); + printf("*** color ***\n"); -printf(" RED = %i\n", RED); -printf(" BLUE = %i\n", BLUE); -printf(" GREEN = %i\n", GREEN); +printf(" RED = %i\n", RED_get()); +printf(" BLUE = %i\n", BLUE_get()); +printf(" GREEN = %i\n", GREEN_get()); - -printf("\nTesting use of enums with functions\n"); - -enum_test(RED); -enum_test(BLUE); -enum_test(GREEN); +enum_test(RED_get()); +enum_test(BLUE_get()); +enum_test(GREEN_get()); enum_test(int32(1234)); -exit +printf("\nTesting use of enums wrapped as Scilab variables\n"); +printf("*** fruit ***\n"); +printf(" APPLE = %i\n", APPLE); +printf(" ORANGE = %i\n", ORANGE); +printf(" LEMON = %i\n", LEMON); + +scilabconst_enum_test(APPLE); +scilabconst_enum_test(ORANGE); +scilabconst_enum_test(LEMON); +scilabconst_enum_test(int32(1234)); + +exit diff --git a/Examples/scilab/enum/scilabconst_example.h b/Examples/scilab/enum/scilabconst_example.h new file mode 100644 index 000000000..92dcaaf61 --- /dev/null +++ b/Examples/scilab/enum/scilabconst_example.h @@ -0,0 +1,3 @@ +typedef enum { APPLE, ORANGE, LEMON } fruit; + +void scilabconst_enum_test(fruit f); From 9e5c351176cac546de8500d8d3e6a967fa5192ca Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:46:45 +0200 Subject: [PATCH 14/17] Scilab: clean enum management code (no need to force enum type) --- Source/Modules/scilab.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 57acc5623..1a1a63b26 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -655,9 +655,6 @@ public: } } - /* Force type to be an enum (See scitypemaps.swg) */ - Setattr(node, "type", "enum SWIG"); - return Language::enumvalueDeclaration(node); } From 360a565f7cf91dffff98661dfeedad21dcd04ca5 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 3 Sep 2013 14:11:45 +0200 Subject: [PATCH 15/17] Scilab: refactor & clean make command lines --- Examples/Makefile.in | 47 ++++++++++---------------- Examples/test-suite/scilab/Makefile.in | 4 ++- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/Examples/Makefile.in b/Examples/Makefile.in index 1e59fc7a0..4fd7d94a7 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1578,26 +1578,26 @@ SCILAB = @SCILAB@ SCILABOPT = SCILAB_STARTOPT = @SCILABSTARTOPT@ +# Returns the Swig Scilab command line args +define get_swig_scilab_args + SWIG_SCILAB_ARGS := -scilab $(SCILABOPT) + ifdef SRCS + SWIG_SCILAB_ARGS += -addsrc "$(SRCS)" + endif + ifdef INCLUDES + SWIG_SCILAB_ARGS += -addcflag "$(INCLUDES)" + endif +endef + # ---------------------------------------------------------------- # Build a C dynamically loadable module # ---------------------------------------------------------------- scilab: $(SRCS) - if test ! -z "$(SRCS)"; then \ - if test ! -z "$(INCLUDES)"; then \ - $(SWIG) -scilab $(SWIGOPT) $(SCILABOPT) -addsrc $(SRCS) -addcflag $(INCLUDES) $(INTERFACEPATH); \ - else \ - $(SWIG) -scilab $(SWIGOPT) $(SCILABOPT) -addsrc $(SRCS) $(INTERFACEPATH); \ - fi \ - else \ - if test ! -z "$(INCLUDES)"; then \ - $(SWIG) -scilab $(SWIGOPT) $(SCILABOPT) -addcflag $(INCLUDES) $(INTERFACEPATH); \ - else \ - $(SWIG) -scilab $(SWIGOPT) $(SCILABOPT) $(INTERFACEPATH); \ - fi \ - fi + $(eval $(call get_swig_scilab_args)) + $(SWIG) $(SWIGOPT) $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) if [ -f builder.sce ]; then \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH echo 'exit(1)' | $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ fi # ---------------------------------------------------------------- @@ -1605,21 +1605,10 @@ scilab: $(SRCS) # ---------------------------------------------------------------- scilab_cpp: $(SRCS) - if test ! -z "$(SRCS)"; then \ - if test ! -z "$(INCLUDES)"; then \ - $(SWIG) -scilab -c++ $(SWIGOPT) $(SCILABOPT) -addsrc $(SRCS) -addcflag $(INCLUDES) $(INTERFACEPATH); \ - else \ - $(SWIG) -scilab -c++ $(SWIGOPT) $(SCILABOPT) -addsrc $(SRCS) $(INTERFACEPATH); \ - fi \ - else \ - if test ! -z "$(INCLUDES)"; then \ - $(SWIG) -scilab -c++ $(SWIGOPT) $(SCILABOPT) -addcflag $(INCLUDES) $(INTERFACEPATH); \ - else \ - $(SWIG) -scilab -c++ $(SWIGOPT) $(SCILABOPT) $(INTERFACEPATH); \ - fi \ - fi + $(eval $(call get_swig_scilab_args)) + $(SWIG) $(SWIGOPT) -c++ $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) if [ -f builder.sce ]; then \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH echo 'exit(1)' | $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ fi # ----------------------------------------------------------------- @@ -1627,7 +1616,7 @@ scilab_cpp: $(SRCS) # ----------------------------------------------------------------- scilab_run: - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(RUNME).sci $(RUNPIPE) + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(RUNME).sci $(RUNPIPE) # ----------------------------------------------------------------- # Scilab version diff --git a/Examples/test-suite/scilab/Makefile.in b/Examples/test-suite/scilab/Makefile.in index f7a2f33af..efd83b554 100644 --- a/Examples/test-suite/scilab/Makefile.in +++ b/Examples/test-suite/scilab/Makefile.in @@ -4,11 +4,13 @@ LANGUAGE = scilab SCILAB = @SCILAB@ +SCILAB_STARTOPT = @SCILABSTARTOPT@ SCRIPTSUFFIX = _runme.sci srcdir = @srcdir@ top_srcdir = @top_srcdir@ top_builddir = @top_builddir@ + # Overridden variables here # None! # - member_funcptr_galore (C++) @@ -61,7 +63,7 @@ include $(srcdir)/../common.mk # a file is found which has _runme.sci appended after the testcase name. run_testcase = \ if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) echo 'exit(1)' |$(SCILAB) -nwni -nb -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ; ) \ + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ; ) \ fi; # Clean: remove the generated files From 7a81f55ac955672c05df95f8c6f352ff7b1e709b Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Wed, 4 Sep 2013 14:53:14 +0200 Subject: [PATCH 16/17] Scilab: take in account TARGET in example makefile, fix target in examples sub makefiles --- Examples/Makefile.in | 3 +++ Examples/scilab/class/Makefile | 2 +- Examples/scilab/constants/Makefile | 4 ++-- Examples/scilab/contract/Makefile | 2 +- Examples/scilab/enum/Makefile | 2 +- Examples/scilab/funcptr/Makefile | 2 +- Examples/scilab/matrix/Makefile | 2 +- Examples/scilab/matrix2/Makefile | 4 ++-- Examples/scilab/pointer/Makefile | 2 +- Examples/scilab/simple/Makefile | 2 +- Examples/scilab/std_list/Makefile | 2 +- Examples/scilab/std_set/Makefile | 2 +- Examples/scilab/std_vector/std_vector/Makefile | 2 +- .../std_vector/std_vector_as_function_argument/Makefile | 2 +- Examples/scilab/struct/Makefile | 2 +- Examples/scilab/template/Makefile | 2 +- Examples/scilab/variables/Makefile | 2 +- 17 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Examples/Makefile.in b/Examples/Makefile.in index 4fd7d94a7..e916c022a 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1587,6 +1587,9 @@ define get_swig_scilab_args ifdef INCLUDES SWIG_SCILAB_ARGS += -addcflag "$(INCLUDES)" endif + ifdef TARGET + SWIG_SCILAB_ARGS += -o "$(TARGET)" + endif endef # ---------------------------------------------------------------- diff --git a/Examples/scilab/class/Makefile b/Examples/scilab/class/Makefile index 75dfd0d10..7790fefde 100644 --- a/Examples/scilab/class/Makefile +++ b/Examples/scilab/class/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.cxx -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/constants/Makefile b/Examples/scilab/constants/Makefile index 07b3ce27c..064f409e5 100644 --- a/Examples/scilab/constants/Makefile +++ b/Examples/scilab/constants/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig -SRCS = example.i -TARGET = example +SRCS = +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/contract/Makefile b/Examples/scilab/contract/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/contract/Makefile +++ b/Examples/scilab/contract/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/enum/Makefile b/Examples/scilab/enum/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/enum/Makefile +++ b/Examples/scilab/enum/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/funcptr/Makefile b/Examples/scilab/funcptr/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/funcptr/Makefile +++ b/Examples/scilab/funcptr/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/matrix/Makefile b/Examples/scilab/matrix/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/matrix/Makefile +++ b/Examples/scilab/matrix/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/matrix2/Makefile b/Examples/scilab/matrix2/Makefile index 20bf0abd5..fd764f6d8 100644 --- a/Examples/scilab/matrix2/Makefile +++ b/Examples/scilab/matrix2/Makefile @@ -1,7 +1,7 @@ -TOP = ../.. +TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = matrixlib.c -TARGET = matrixlib +TARGET = matrixlib_wrap.c INTERFACE = matrixlib.i check: build diff --git a/Examples/scilab/pointer/Makefile b/Examples/scilab/pointer/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/pointer/Makefile +++ b/Examples/scilab/pointer/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/simple/Makefile b/Examples/scilab/simple/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/simple/Makefile +++ b/Examples/scilab/simple/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/std_list/Makefile b/Examples/scilab/std_list/Makefile index 9d13629fc..e40b840db 100644 --- a/Examples/scilab/std_list/Makefile +++ b/Examples/scilab/std_list/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.cpp -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/std_set/Makefile b/Examples/scilab/std_set/Makefile index 9d13629fc..e40b840db 100644 --- a/Examples/scilab/std_set/Makefile +++ b/Examples/scilab/std_set/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.cpp -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/std_vector/std_vector/Makefile b/Examples/scilab/std_vector/std_vector/Makefile index fe46d2360..29be821bc 100644 --- a/Examples/scilab/std_vector/std_vector/Makefile +++ b/Examples/scilab/std_vector/std_vector/Makefile @@ -1,7 +1,7 @@ TOP = ../../.. SWIG = $(TOP)/../preinst-swig SRCS = -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/std_vector/std_vector_as_function_argument/Makefile b/Examples/scilab/std_vector/std_vector_as_function_argument/Makefile index 0f816b1af..b65b9e2f7 100644 --- a/Examples/scilab/std_vector/std_vector_as_function_argument/Makefile +++ b/Examples/scilab/std_vector/std_vector_as_function_argument/Makefile @@ -1,7 +1,7 @@ TOP = ../../.. SWIG = $(TOP)/../preinst-swig SRCS = example.cpp -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/struct/Makefile b/Examples/scilab/struct/Makefile index 07b3ce27c..c63666df4 100644 --- a/Examples/scilab/struct/Makefile +++ b/Examples/scilab/struct/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.i -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build diff --git a/Examples/scilab/template/Makefile b/Examples/scilab/template/Makefile index 75dfd0d10..7790fefde 100644 --- a/Examples/scilab/template/Makefile +++ b/Examples/scilab/template/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.cxx -TARGET = example +TARGET = example_wrap.cxx INTERFACE = example.i check: build diff --git a/Examples/scilab/variables/Makefile b/Examples/scilab/variables/Makefile index e77423e49..0625933a3 100644 --- a/Examples/scilab/variables/Makefile +++ b/Examples/scilab/variables/Makefile @@ -1,7 +1,7 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig SRCS = example.c -TARGET = example +TARGET = example_wrap.c INTERFACE = example.i check: build From b4ed5625eec63dd47a1f08110eaf394ca147f134 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Wed, 4 Sep 2013 15:25:53 +0200 Subject: [PATCH 17/17] Scilab: parallelization of test-suite, remove configure cache --- .travis.yml | 2 +- Examples/Makefile.in | 26 +++++++-- Examples/test-suite/scilab/Makefile.in | 68 ++++++++++++++--------- Examples/test-suite/scilab/swigtest.quit | 15 +++-- Examples/test-suite/scilab/swigtest.start | 8 ++- Source/Modules/scilab.cxx | 12 +++- 6 files changed, 88 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0596c966b..e6f460751 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ matrix: - compiler: gcc env: SWIGLANG=python - compiler: gcc - env: SWIGLANG=scilab + env: SWIGLANG=scilab SWIGJOBS=-j4 allow_failures: # None before_install: diff --git a/Examples/Makefile.in b/Examples/Makefile.in index e916c022a..b4601818c 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1587,11 +1587,24 @@ define get_swig_scilab_args ifdef INCLUDES SWIG_SCILAB_ARGS += -addcflag "$(INCLUDES)" endif + ifdef OUTDIR + SWIG_SCILAB_ARGS += -outdir "$(OUTDIR)" + endif ifdef TARGET SWIG_SCILAB_ARGS += -o "$(TARGET)" endif endef +# Returns the output dir +define get_output_dir + ifdef OUTDIR + OUTPUT_DIR := $(OUTDIR) + else + OUTPUT_DIR := . + endif +endef + + # ---------------------------------------------------------------- # Build a C dynamically loadable module # ---------------------------------------------------------------- @@ -1599,8 +1612,9 @@ endef scilab: $(SRCS) $(eval $(call get_swig_scilab_args)) $(SWIG) $(SWIGOPT) $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) - if [ -f builder.sce ]; then \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ + $(eval $(call get_output_dir)) + if [ -f $(OUTPUT_DIR)/builder.sce ]; then \ + env LD_LIBRARY_PATH=$(OUTPUT_DIR):$$LD_LIBRARY_PATH MAKEFLAGS="-j1" $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(OUTPUT_DIR)/builder.sce; \ fi # ---------------------------------------------------------------- @@ -1610,8 +1624,9 @@ scilab: $(SRCS) scilab_cpp: $(SRCS) $(eval $(call get_swig_scilab_args)) $(SWIG) $(SWIGOPT) -c++ $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) - if [ -f builder.sce ]; then \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f builder.sce; \ + $(eval $(call get_output_dir)) + if [ -f $(OUTPUT_DIR)/builder.sce ]; then \ + env LD_LIBRARY_PATH=$(OUTPUT_DIR):$$LD_LIBRARY_PATH MAKEFLAGS="-j1" $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(OUTPUT_DIR)/builder.sce; \ fi # ----------------------------------------------------------------- @@ -1619,7 +1634,8 @@ scilab_cpp: $(SRCS) # ----------------------------------------------------------------- scilab_run: - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(RUNME).sci $(RUNPIPE) + $(eval $(call get_output_dir)) + env LD_LIBRARY_PATH=$(OUTPUT_DIR):$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(RUNME).sci $(RUNPIPE) # ----------------------------------------------------------------- # Scilab version diff --git a/Examples/test-suite/scilab/Makefile.in b/Examples/test-suite/scilab/Makefile.in index efd83b554..c39776217 100644 --- a/Examples/test-suite/scilab/Makefile.in +++ b/Examples/test-suite/scilab/Makefile.in @@ -17,58 +17,74 @@ top_builddir = @top_builddir@ # - member_pointer (C++) # - typemap_variables (C++) -# configure cache to speed up test run -CONF_CACHE=$(CURDIR)/test-suite.config.cache -CONFIG_SITE=$(CURDIR)/test-suite.config.site -export CONFIG_SITE +define get_output_dir + OUTDIR := $(CURDIR)/$(1).build +endef -enable_config_cache = \ - echo 'cache_file=$(CONF_CACHE)' > $(CONFIG_SITE) - -disable_config_cache: - rm -f $(CONF_CACHE) - rm -f $(CONFIG_SITE) - CONFIG_SITE= - -# need reset cache before multicpptest -reset_config_cache = \ - rm -f $(CONF_CACHE) - -# disable cache at the end of test-suite -# use trick for this: 'extra test cases' end target -EXTRA_TEST_CASES = disable_config_cache +define get_runme_script + RUNME_SCRIPT := $(srcdir)/$(SCRIPTPREFIX)$(1)$(SCRIPTSUFFIX) +endef include $(srcdir)/../common.mk +# Override make commands to specify OUTDIR +swig_and_compile_cpp = \ + $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile CXXSRCS="$(CXXSRCS)" \ + SWIG_LIB="$(SWIG_LIB)" SWIG="$(SWIG)" \ + INCLUDES="$(INCLUDES)" SWIGOPT= OUTDIR="$(OUTDIR)" \ + TARGET="$*_wrap.cxx" INTERFACEDIR="$(INTERFACEDIR)" INTERFACE="$*.i" \ + scilab_cpp + +swig_and_compile_c = \ + $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile CSRCS="$(CSRCS)" \ + SWIG_LIB="$(SWIG_LIB)" SWIG="$(SWIG)" \ + INCLUDES="$(INCLUDES)" SWIGOPT= OUTDIR="$(OUTDIR)" \ + TARGET="$*_wrap.c" INTERFACEDIR="$(INTERFACEDIR)" INTERFACE="$*.i" \ + scilab + +swig_and_compile_multi_cpp = \ + for f in `cat $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list` ; do \ + $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile CXXSRCS="$(CXXSRCS)" \ + SWIG_LIB="$(SWIG_LIB)" SWIG="$(SWIG)" \ + INCLUDES="$(INCLUDES)" SWIGOPT= OUTDIR="$(OUTDIR)" \ + TARGET="$*_wrap.cxx" INTERFACEDIR="$(INTERFACEDIR)" INTERFACE="$$f.i" \ + scilab_cpp; \ + done + # Rules for the different types of tests %.cpptest: $(setup) - $(enable_config_cache) + $(eval $(call get_output_dir,$*)) + mkdir -p $(OUTDIR) +$(swig_and_compile_cpp) $(run_testcase) %.ctest: $(setup) - $(enable_config_cache) + $(eval $(call get_output_dir,$*)) + mkdir -p $(OUTDIR) +$(swig_and_compile_c) $(run_testcase) %.multicpptest: $(setup) - $(reset_config_cache) + $(eval $(call get_output_dir,$*)) + mkdir -p $(OUTDIR) +$(swig_and_compile_multi_cpp) $(run_testcase) # Runs the testcase. A testcase is only run if # a file is found which has _runme.sci appended after the testcase name. run_testcase = \ - if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \ - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ; ) \ - fi; + $(eval $(call get_output_dir,$*)) \ + $(eval $(call get_runme_script,$*)) \ + if [ -f $(RUNME_SCRIPT) ]; then ( \ + env LD_LIBRARY_PATH=$(OUTDIR):$$LD_LIBRARY_PATH $(RUNTOOL) $(SCILAB) $(SCILAB_STARTOPT) -f $(RUNME_SCRIPT); )\ + fi # Clean: remove the generated files %.clean: - @rm -f builder.sce loader.sce cleaner.sce $*_wrap.c $*_wrap.cxx lib$*lib.* $(CONFIG_SITE) $(CONF_CACHE) + @rm -rf $*.build clean: $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile scilab_clean diff --git a/Examples/test-suite/scilab/swigtest.quit b/Examples/test-suite/scilab/swigtest.quit index d466ce624..aa2e5dd6f 100644 --- a/Examples/test-suite/scilab/swigtest.quit +++ b/Examples/test-suite/scilab/swigtest.quit @@ -1,10 +1,13 @@ // Clean files -exec("cleaner.sce", -1); -mdelete("builder.sce"); -mdelete("cleaner.sce"); -mdelete(swigtestname + "_wrap.c"); -mdelete(swigtestname + "_wrap.cxx"); -mdelete(swigtestname + ".i"); + +exec(fullfile(testbuilddir, "cleaner.sce"), -1); + +mdelete(fullfile(testbuilddir, "builder.sce")); +mdelete(fullfile(testbuilddir, "cleaner.sce")); +mdelete(fullfile(testbuilddir, swigtestname + "_wrap.c")); +mdelete(fullfile(testbuilddir, swigtestname + "_wrap.cxx")); +mdelete(fullfile(testbuilddir, swigtestname + ".i")); +removedir(testbuilddir); //mprintf("******************\n") //mprintf("* TEST SUCCEEDED *\n") diff --git a/Examples/test-suite/scilab/swigtest.start b/Examples/test-suite/scilab/swigtest.start index 12d4c924b..586a1bc62 100644 --- a/Examples/test-suite/scilab/swigtest.start +++ b/Examples/test-suite/scilab/swigtest.start @@ -4,15 +4,19 @@ lines(0); [units, typ, names] = file(1); swigtestname = strsubst(fileparts(names, "fname"), "_runme", ""); +// Test build dir +testbuilddir = swigtestname + ".build"; + // Does the library exists? If not then exit! -if ~isfile("lib" + swigtestname + "lib" + getdynlibext()) then +libname = "lib" + swigtestname + "lib" + getdynlibext(); +if ~isfile(fullfile(testbuilddir, libname)) then mprintf("*** LIBRARY NOT FOUND: %s ***\n", "lib" + swigtestname + "lib" + getdynlibext()); exit end // Load library try - exec("loader.sce", -1); + exec(fullfile(testbuilddir, "loader.sce"), -1); catch mprintf("*** LOADER EXECUTION FAILED ***\n"); exit diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 1a1a63b26..9da7a9f42 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -152,6 +152,11 @@ public: Printf(builderCode, "mode(-1);\n"); Printf(builderCode, "lines(0);\n"); /* Useful for automatic tests */ + // Scilab needs to be in the build directory + Printf(builderCode, "originaldir = pwd();\n"); + Printf(builderCode, "builddir = get_absolute_file_path('builder.sce');\n"); + Printf(builderCode, "cd(builddir);\n"); + Printf(builderCode, "ilib_verbose(%s);\n", verboseBuildLevel); Printf(builderCode, "ilib_name = \"%slib\";\n", moduleName); @@ -163,7 +168,7 @@ public: Printf(builderCode, "ldflags = \"\";\n"); } - Printf(builderCode, "cflags = [\"-g -I\" + get_absolute_file_path(\"builder.sce\")];\n"); + Printf(builderCode, "cflags = [\"-g -I\" + builddir];\n"); if (cflag != NULL) { Printf(builderCode, "includepath = \"%s\";\n", cflag); Printf(builderCode, "includepath = fullpath(part(includepath, 3:length(includepath)));\n"); @@ -174,9 +179,9 @@ public: for (int i = 0; i < Len(sourceFileList); i++) { String *sourceFile = Getitem(sourceFileList, i); if (i == 0) { - Printf(builderCode, "files = \"%s\";\n", sourceFile); + Printf(builderCode, "files = \"%s\";\n", sourceFile); } else { - Printf(builderCode, "files($ + 1) = \"%s\";\n", sourceFile); + Printf(builderCode, "files($ + 1) = \"%s\";\n", sourceFile); } } @@ -208,6 +213,7 @@ public: Printf(builderCode, "if ~isempty(table) then\n"); Printf(builderCode, " ilib_build(ilib_name, table, files, libs, [], ldflags, cflags);\n"); Printf(builderCode, "end\n"); + Printf(builderCode, "cd(originaldir);\n"); Printf(builderCode, "exit"); builderFile = NewFile(NewStringf("%sbuilder.sce", SWIG_output_directory()), "w", SWIG_output_files());