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 1e59fc7a0..b4601818c 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1578,26 +1578,43 @@ 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 + 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 # ---------------------------------------------------------------- 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 - 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; \ + $(eval $(call get_swig_scilab_args)) + $(SWIG) $(SWIGOPT) $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) + $(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 # ---------------------------------------------------------------- @@ -1605,21 +1622,11 @@ 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 - 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; \ + $(eval $(call get_swig_scilab_args)) + $(SWIG) $(SWIGOPT) -c++ $(SWIG_SCILAB_ARGS) $(INTERFACEPATH) + $(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 # ----------------------------------------------------------------- @@ -1627,7 +1634,8 @@ scilab_cpp: $(SRCS) # ----------------------------------------------------------------- scilab_run: - env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(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/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/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/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/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 23ee8a822..6a471fde0 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -3,9 +3,11 @@ %{ #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 a5f16f3f8..182fa0c61 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -1,19 +1,29 @@ lines(0); exec loader.sce; +SWIG_Init(); + +printf("\nTesting use of enums wrapped as Scilab functions\n"); -// 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("\nTesting use of enums with functions\n"); - 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); 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 diff --git a/Examples/test-suite/scilab/Makefile.in b/Examples/test-suite/scilab/Makefile.in index f7a2f33af..c39776217 100644 --- a/Examples/test-suite/scilab/Makefile.in +++ b/Examples/test-suite/scilab/Makefile.in @@ -4,69 +4,87 @@ 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++) # - 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) echo 'exit(1)' |$(SCILAB) -nwni -nb -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/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/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) 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/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) { diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index 657717cda..33cf288a6 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -376,3 +376,37 @@ %typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { SCILAB_TYPECHECK(isStringType) } //%apply int { size_t }; + +/* -----------------------------------------------------------------------------*/ +/* Constants and enums to Scilab variables +/* -----------------------------------------------------------------------------*/ + +%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; +%} + +%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 47322e76b..9da7a9f42 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() @@ -150,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); @@ -161,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"); @@ -172,18 +179,20 @@ 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); } } 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,11 +205,15 @@ 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"); 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()); @@ -208,13 +221,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 +570,27 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; + // If feature scilab:const enabled, constants & enums are wrapped to Scilab variables + if (GetFlag(node, "feature:scilab:const")) { + 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) { + 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); + 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); @@ -601,9 +636,30 @@ public: * enumvalueDeclaration() * --------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *node) { + static int iPreviousEnumValue = 0; - /* Force type to be an enum (See scitypemaps.swg) */ - Setattr(node, "type", "enum SWIG"); + 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); + } + } + } + } return Language::enumvalueDeclaration(node); }