From 0bec5d309ea921e287cc96e5e2832acc7ffbc418 Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Mon, 24 Jun 2013 19:17:28 +0200
Subject: [PATCH] Compilation: it is now possible to compile functions and
 release the license from within Matlab

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>
---
 1_preprocessing/gtPreprocessing.m           | 12 ++++---
 1_preprocessing/gtSetup.m                   |  2 +-
 2_difspot/gtSetupSegmentation.m             |  2 +-
 2_difspot/gtSetupSegmentation_doublethr.m   |  3 +-
 5_reconstruction/gtSetupForwardSimulation.m |  2 +-
 5_reconstruction/gtSetupReconstruction.m    |  2 +-
 zUtil_OAR/gtCompileFunctions.m              |  4 ++-
 zUtil_OAR/gtExternalCompileFunctions.m      | 12 +++++++
 zUtil_OAR/gt_mcc.m                          | 35 ++++++++++-----------
 9 files changed, 43 insertions(+), 31 deletions(-)
 create mode 100644 zUtil_OAR/gtExternalCompileFunctions.m

diff --git a/1_preprocessing/gtPreprocessing.m b/1_preprocessing/gtPreprocessing.m
index e2fa2cd9..d74461a7 100644
--- a/1_preprocessing/gtPreprocessing.m
+++ b/1_preprocessing/gtPreprocessing.m
@@ -44,11 +44,13 @@ function gtPreprocessing()
 check=inputwdefault('Recompile functions for OAR? Not normally needed! [y/n]', 'n');
 if strcmpi(check,'y')
     disp('recompiling all functions')
-    gt_mcc('gtSequenceMedianRefs')
-    gt_mcc('gtCreateAbsLive');
-    gt_mcc('gtAbsMedianLive');
-    gt_mcc('gtMovingMedianLive');
-    gt_mcc('gtCreateFullLive');
+    funcsToCompile = { ...
+        'gtSequenceMedianRefs', ...
+        'gtCreateAbsLive', ...
+        'gtAbsMedianLive', ...
+        'gtMovingMedianLive', ...
+        'gtCreateFullLive' };
+    gtExternalCompileFunctions(funcsToCompile{:});
 end
 
 
diff --git a/1_preprocessing/gtSetup.m b/1_preprocessing/gtSetup.m
index 22c9454f..bb373afc 100644
--- a/1_preprocessing/gtSetup.m
+++ b/1_preprocessing/gtSetup.m
@@ -454,7 +454,7 @@ close('all');
 check = inputwdefault('Recompile copy function for OAR? Not normally needed! [y/n]', 'n');
 if strcmpi(check, 'y')
     disp('recompiling gtCopyCorrectUndistortCondor');
-    gt_mcc('gtCopyCorrectUndistortCondor');
+    gtExternalCompileFunctions('gtCopyCorrectUndistortCondor');
 end
 
 disp(' ');
diff --git a/2_difspot/gtSetupSegmentation.m b/2_difspot/gtSetupSegmentation.m
index b0417fa8..8ef745f6 100644
--- a/2_difspot/gtSetupSegmentation.m
+++ b/2_difspot/gtSetupSegmentation.m
@@ -77,7 +77,7 @@ parameters.seg.background_subtract = true;
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 check = inputwdefault('Recompile gtSegmentDiffractionBlobs? (not normally needed) [y/n]', 'n');
 if strcmpi(check, 'y')
-    gt_mcc('gtSegmentDiffractionBlobs');
+    gtExternalCompileFunctions('gtSegmentDiffractionBlobs');
 end
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/2_difspot/gtSetupSegmentation_doublethr.m b/2_difspot/gtSetupSegmentation_doublethr.m
index 7b14d614..e7aa1eae 100644
--- a/2_difspot/gtSetupSegmentation_doublethr.m
+++ b/2_difspot/gtSetupSegmentation_doublethr.m
@@ -115,8 +115,7 @@ save(parameters_name, 'parameters');
 check = inputwdefault('Recompile functions for OAR? Not normally needed! [y/n]', 'n');
 if strcmpi(check, 'y')
     disp('Recompiling all functions...');
-    gt_mcc('gtSeedThreshold_doublethr');
-    gt_mcc('gtSegmentDiffractionBlobs_doublethr');
+    gtExternalCompileFunctions('gtSeedThreshold_doublethr', 'gtSegmentDiffractionBlobs_doublethr');
 end
 
 
diff --git a/5_reconstruction/gtSetupForwardSimulation.m b/5_reconstruction/gtSetupForwardSimulation.m
index 443f6d8f..d1aff49c 100644
--- a/5_reconstruction/gtSetupForwardSimulation.m
+++ b/5_reconstruction/gtSetupForwardSimulation.m
@@ -15,7 +15,7 @@ gtDBConnect();
 
 check=inputwdefault('Do you want to recompile the function ''gtForwardSimulate_v2''? [y/n]','n');
 if strcmpi(check,'y')
-    gt_mcc('gtForwardSimulate_v2');
+    gtExternalCompileFunctions('gtForwardSimulate_v2');
 end
 
 nof_phases = length(parameters.cryst);
diff --git a/5_reconstruction/gtSetupReconstruction.m b/5_reconstruction/gtSetupReconstruction.m
index c3b89e83..bad93d60 100644
--- a/5_reconstruction/gtSetupReconstruction.m
+++ b/5_reconstruction/gtSetupReconstruction.m
@@ -39,7 +39,7 @@ end
 
 check = inputwdefault('Do you want to recompile the function ''gtChangeThreshold3D''? [y/n]', 'n');
 if strcmpi(check,'y')
-    gt_mcc('gtChangeThreshold3D');
+    gtExternalCompileFunctions('gtChangeThreshold3D');
 end
 
 parameters = [];
diff --git a/zUtil_OAR/gtCompileFunctions.m b/zUtil_OAR/gtCompileFunctions.m
index 8ac889a3..fb775bff 100644
--- a/zUtil_OAR/gtCompileFunctions.m
+++ b/zUtil_OAR/gtCompileFunctions.m
@@ -34,7 +34,9 @@ function gtCompileFunctions( force, mfiles, funcs_to_compile )
             disp(['Compiling function: ' funcName{1}]);
 
             comp_file_dir = fileparts(comp_file_path);
-            if (~gt_mcc(funcName{1}, 'out_path', comp_file_dir))
+            try
+                gt_mcc(funcName{1}, 'out_path', comp_file_dir)
+            catch mexc
                 error_log(1, end+1) = funcName;
             end
         end
diff --git a/zUtil_OAR/gtExternalCompileFunctions.m b/zUtil_OAR/gtExternalCompileFunctions.m
new file mode 100644
index 00000000..c3e2ada6
--- /dev/null
+++ b/zUtil_OAR/gtExternalCompileFunctions.m
@@ -0,0 +1,12 @@
+function gtExternalCompileFunctions(varargin)
+    global GT_MATLAB_HOME
+    cmd = ['python ' fullfile(GT_MATLAB_HOME, 'dct_launch.py') ' compile_matlab'];
+    cmd = [cmd sprintf(' -c %s', varargin{:})];
+
+    [status, msg] = system(cmd, '-echo');
+    if (status)
+        errorMsg = sprintf('Command: %s\nExited with code: %d, and message:\n%s', ...
+            cmd, status, msg);
+        gtError('gtExternalCompileFunctions:runtime_error', errorMsg)
+    end
+end
diff --git a/zUtil_OAR/gt_mcc.m b/zUtil_OAR/gt_mcc.m
index b96cf20a..33de6f29 100644
--- a/zUtil_OAR/gt_mcc.m
+++ b/zUtil_OAR/gt_mcc.m
@@ -1,7 +1,7 @@
-function compiled_fine = gt_mcc(myfunctionname, varargin)
+function gt_mcc(myfunctionname, varargin)
 % gt_mcc.m Compiles an executable and puts it in the right place
     if (isempty(whos('global', 'GT_MATLAB_HOME')))
-        error('COMPILE:no_such_variable', ...
+        gtError('COMPILE:no_such_variable', ...
               ['GT_MATLAB_HOME variable doesn''t exist in global workspace, '...
                'your environment is not sane, and you need to either ' ...
                're-initialise or re-run matlab.'])
@@ -12,23 +12,21 @@ function compiled_fine = gt_mcc(myfunctionname, varargin)
         mcc_vers = textscan(v.Version, '%s','delimiter','.');
         mcc_vers_minor = str2double(mcc_vers{1}{2});
         mcc_vers_major = str2double(mcc_vers{1}{1});
-        compiled_fine = true; % In case of error, we will re modify it
     catch mexc
-        gtPrintException(mexc, 'Error: Is the Matlab Compiler installed?')
-        compiled_fine = false;
-        return
+        disp('Error: Is the Matlab Compiler installed?')
+        rethrow(mexc)
     end
 
     if ( mcc_vers_major < 4 || (mcc_vers_major == 4 && mcc_vers_minor < 8))
-        disp('ERROR: Compilation using Matlab versions before R2008 is not supported')
-        compiled_fine = false;
-        return
+        gtError('gt_mcc:wrong_matlab_version', ...
+            'Compilation using Matlab versions before R2008 is not supported')
     end
 
     % Parsing arguments
     conf = [];
     conf.not_really = false;
     conf.out_path = '';
+    conf.licence_warning = false;
 
     conf = parse_pv_pairs(conf, varargin);
 
@@ -40,16 +38,16 @@ function compiled_fine = gt_mcc(myfunctionname, varargin)
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     % Modify the M name if necessary
-    if ~strcmp(myfunctionname(end-1:end), '.m')
+    [~, ~, ext] = fileparts(myfunctionname);
+    if ~strcmp(ext, '.m')
         myfunctionname = [myfunctionname '.m'];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     % check the code is visible at the moment
     if ~exist(myfunctionname, 'file')
-        disp('Cannot find your function - is it in the path?')
-        compiled_fine = false;
-        return
+        gtError('gt_mcc:wrong_file_name', ...
+            'Cannot find your function - is it in the path?')
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -57,7 +55,7 @@ function compiled_fine = gt_mcc(myfunctionname, varargin)
     globalVars = whos('global');
     eval(['global' sprintf(' %s', globalVars.('name'))]);
     if (isempty(whos('global', 'GT_DB')))
-        error('COMPILE:no_such_variable', ...
+        gtError('COMPILE:no_such_variable', ...
               ['GT_DB variable doesn''t exist in global workspace, '...
                'your environment is not sane, and you need to either ' ...
                're-initialise or re-run matlab.'])
@@ -91,11 +89,8 @@ function compiled_fine = gt_mcc(myfunctionname, varargin)
                 disp('Could not get a compiler license.');
                 disp('Surf to http://compweb/public/nice/matlab_licence.php');
                 disp('and call the person with the compiler license to see if you could use it!');
-            else
-                gtPrintException(mexc);
             end
-            compiled_fine = false;
-            return
+            rethrow(mexc)
         end
     end
 
@@ -105,5 +100,7 @@ function compiled_fine = gt_mcc(myfunctionname, varargin)
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     % And let the user know everything is finished
-    disp('PLEASE RESTART MATLAB TO FREE THE COMPILER FOR SOMEONE ELSE!');
+    if (conf.licence_warning)
+        disp('PLEASE RESTART MATLAB TO FREE THE COMPILER FOR SOMEONE ELSE!');
+    end
 end
-- 
GitLab