2929
3030%% MexTask
3131bindir = fullfile(plan .RootFolder , pkg_name );
32- [compiler_id , compiler_opt , linker_opt ] = get_compiler_options();
32+ [compiler_opt , linker_opt ] = get_compiler_options();
3333
3434if isMATLABReleaseOlderThan(" R2024b" )
3535 % dummy task to allow "buildtool mex" to build all MEX targets
4646 mex_name = " mex_" + name ;
4747 % specifying .Inputs and .Outputs enables incremental builds
4848 % https://www.mathworks.com/help/matlab/matlab_prog/improve-performance-with-incremental-builds.html
49- plan(mex_name ) = matlab .buildtool .Task(Actions = @(context ) legacyMexTask(context , compiler_id , compiler_opt , linker_opt ));
49+ plan(mex_name ) = matlab .buildtool .Task(Actions = @(context ) legacyMexTask(context , compiler_opt , linker_opt ));
5050 plan(mex_name ).Inputs = src ;
5151 plan(mex_name ).Outputs = fullfile(bindir , name + " ." + mexext());
5252 mex_deps(end + 1 ) = mex_name ; % #ok<AGROW>
5353 else
5454 plan(" mex:" + name ) = matlab .buildtool .tasks .MexTask(src , bindir , ...
55- Options= [compiler_id , compiler_opt , linker_opt ]);
55+ Options= [compiler_opt , linker_opt ]);
5656 end
5757end
5858
6363end
6464
6565
66- function legacyMexTask(context , compiler_id , compiler_opt , linker_opt )
66+ function legacyMexTask(context , compiler_opt , linker_opt )
6767bindir = fileparts(context .Task .Outputs .Path );
68- mex(context .Task .Inputs .Path , " -outdir" , bindir , compiler_id , compiler_opt , linker_opt )
68+ mex(context .Task .Inputs .Path , " -outdir" , bindir , compiler_opt{ : } , linker_opt )
6969end
7070
7171
@@ -130,7 +130,7 @@ function publishTask(context)
130130end
131131
132132
133- function [compiler_id , compiler_opt , linker_opt ] = get_compiler_options()
133+ function [compiler_opt , linker_opt ] = get_compiler_options()
134134
135135cxx = mex .getCompilerConfigurations(' c++' );
136136flags = cxx .Details .CompilerFlags ;
@@ -141,6 +141,14 @@ function publishTask(context)
141141% mex() can't handle string.empty
142142compiler_id = " " ;
143143linker_opt = " " ;
144+
145+ % this override is mostly for CI. Ensure auto-compiler flags are still correct if using this.
146+ cxxenv = getenv(" CXXMEX" );
147+ if ~isempty(cxxenv )
148+ compiler_id = " CXX=" + cxxenv ;
149+ disp(" MEX compiler override: " + compiler_id )
150+ end
151+
144152if msvc
145153 std = " /std:c++17" ;
146154 % on Windows, Matlab doesn't register unsupported MSVC or oneAPI
@@ -151,7 +159,7 @@ function publishTask(context)
151159 end
152160 end
153161elseif isunix
154- if cxx .ShortName == " g++" && ~stdlib .version_atleast(cxx .Version , " 9" )
162+ if ~strlength( compiler_id ) && cxx .ShortName == " g++" && ~stdlib .version_atleast(cxx .Version , " 9" )
155163 linker_opt = " -lstdc++fs" ;
156164 end
157165end
@@ -163,4 +171,8 @@ function publishTask(context)
163171 compiler_opt = " CXXFLAGS=" + opt ;
164172end
165173
174+ if strlength(compiler_id )
175+ compiler_opt = [compiler_id , compiler_opt ];
176+ end
177+
166178end
0 commit comments