105105if isMATLABReleaseOlderThan(" R2024b" ), return , end
106106
107107%% MexTask
108- [compiler_opt , linker_opt ] = get_compiler_options();
109108
110109for s = get_mex_sources()
111110 src = s{1 };
112- name = stdlib .stem (src(1 ));
111+ [ ~ , name ] = fileparts (src(1 ));
113112
114113% name of MEX target function is name of first source file
115114 plan(" mex:" + name ) = matlab .buildtool .tasks .MexTask(src , pkg_root , ...
116115 Description= " Build MEX target " + name , ...
117- Options= [ compiler_opt , linker_opt ] );
116+ Options= get_compiler_options() );
118117end
119118
120119end
@@ -151,118 +150,6 @@ function publishTask(context)
151150end
152151
153152
154- function build_exe(context )
155-
156- for i = 1 : length(context .Task .Inputs )
157- src = context .Task .Inputs(i );
158- exe = context .Task .Outputs(i ).paths;
159- exe = exe(1 );
160-
161- ext = stdlib .suffix(src .paths );
162- switch ext
163- case " .c" , lang = " c" ;
164- case " .cpp" , lang = " c++" ;
165- case " .f90" , lang = " fortran" ;
166- otherwise , error(" unknown code suffix " + ext )
167- end
168-
169- [comp , shell , outFlag ] = get_build_cmd(lang );
170- if isempty(comp )
171- return
172- end
173- if i == 1 && ~isempty(shell )
174- disp(" Shell: " + shell )
175- end
176-
177- cmd = join([comp , src .paths , outFlag + exe ]);
178- if ~isempty(shell )
179- cmd = join([shell , " &&" , cmd ]);
180- end
181-
182- disp(cmd )
183- [s , msg ] = system(cmd );
184- assert(s == 0 , " Error %d: %s" , s , msg )
185- end
186-
187- end
188-
189-
190- function [comp , shell ] = get_compiler(lang )
191- arguments (Input )
192- lang (1 ,1 ) string {mustBeMember(lang , [" c" , " c++" , " fortran" ])}
193- end
194- arguments (Output )
195- comp string {mustBeScalarOrEmpty }
196- shell string {mustBeScalarOrEmpty }
197- end
198-
199- lang = lower(lang );
200-
201- co = mex .getCompilerConfigurations(lang );
202-
203- if isempty(co )
204- switch lang
205- case " fortran"
206- comp = getenv(" FC" );
207- if isempty(comp )
208- comp = get_fortran_compiler();
209- end
210- case " c++"
211- comp = getenv(" CXX" );
212- if isempty(comp )
213- disp(" set CXX environment variable to the C++ compiler path, or do 'mex -setup c++" )
214- end
215- case " c"
216- comp = getenv(" CC" );
217- if isempty(comp )
218- disp(" set CC environment variable to the C compiler path, or do 'mex -setup c'" )
219- end
220- end
221- else
222- comp = co .Details .CompilerExecutable ;
223- % disp(lang + " compiler: " + co.ShortName + " " + co.Name + " " + co.Version + " " + comp)
224- end
225-
226- shell = string .empty ;
227- if ispc()
228- if isempty(co )
229- if any(contains(comp , [" gcc" , " g++" , " gfortran" ]))
230- shell = " set PATH=" + fileparts(comp ) + pathsep + " %PATH%" ;
231- end
232- else
233- if startsWith(co .ShortName , [" INTEL" , " MSVC" ])
234- shell = join([strcat(' "' ,string(co .Details .CommandLineShell ),' "' ), ...
235- co .Details .CommandLineShellArg ], " " );
236- elseif startsWith(co .ShortName , " mingw64" )
237- shell = " set PATH=" + fileparts(comp ) + pathsep + " %PATH%" ;
238- end
239- end
240- end
241-
242- end
243-
244-
245- function [comp , shell , outFlag ] = get_build_cmd(lang )
246- arguments (Input )
247- lang (1 ,1 ) string {mustBeMember(lang , [" c" , " c++" , " fortran" ])}
248- end
249- arguments (Output )
250- comp string {mustBeScalarOrEmpty }
251- shell string {mustBeScalarOrEmpty }
252- outFlag (1 ,1 ) string
253- end
254-
255- [comp , shell ] = get_compiler(lang );
256-
257- if any(contains(shell , " Visual Studio" )) || endsWith(comp , " ifx.exe" )
258- outFlag = " /Fo" + tempdir + " /link /out:" ;
259- else
260- outFlag = " -o" ;
261- end
262-
263- end
264-
265-
266153function srcs = get_mex_sources(build_all )
267154arguments (Input )
268155 build_all (1 ,1 ) logical = false
@@ -286,77 +173,3 @@ function build_exe(context)
286173
287174end
288175
289-
290- function [compiler_opt , linker_opt ] = get_compiler_options()
291- arguments (Output )
292- compiler_opt (1 ,1 ) string
293- linker_opt (1 ,1 ) string
294- end
295-
296- cxx = mex .getCompilerConfigurations(' c++' );
297- flags = cxx .Details .CompilerFlags ;
298-
299- msvc = startsWith(cxx .ShortName , " MSVCPP" );
300-
301- std = " -std=c++17" ;
302- % mex() can't handle string.empty
303- linker_opt = " " ;
304-
305- if msvc
306- std = " /std:c++17" ;
307- % on Windows, Matlab doesn't register unsupported MSVC or oneAPI
308- elseif cxx .ShortName == " g++"
309- if ~stdlib .version_atleast(cxx .Version , " 8" )
310- warning(" g++ 8 or newer is required for MEX, detected g++" + cxx .Version )
311- end
312-
313- if ~stdlib .version_atleast(cxx .Version , " 9" )
314- linker_opt = " -lstdc++fs" ;
315- end
316- end
317-
318- opt = flags + " " + std ;
319- if msvc
320- compiler_opt = " COMPFLAGS=" + opt ;
321- else
322- compiler_opt = " CXXFLAGS=" + opt ;
323- end
324-
325- end
326-
327-
328- function comp = get_fortran_compiler()
329- arguments (Output )
330- comp string {mustBeScalarOrEmpty }
331- end
332-
333- if ismac()
334- p = ' /opt/homebrew/bin/' ;
335- disp(" on macOS, environment variables propagate in to GUI programs like Matlab by using 'launchctl setenv FC' and a reboot." )
336- disp(" if having trouble, try:" )
337- disp(" FC=gfortran matlab -batch 'buildtool exe'" )
338- elseif ispc()
339- p = getenv(' CMPLR_ROOT' );
340- if isempty(p )
341- p = getenv(" MW_MINGW64_LOC" );
342- end
343- if ~endsWith(p , [" bin" , " bin/" ])
344- p = p + " /bin" ;
345- end
346- else
347- p = ' ' ;
348- end
349-
350- comp = string .empty ;
351- for fc = [" flang" , " gfortran" , " ifx" ]
352- comp = stdlib .which(fc , p );
353- if ~isempty(comp )
354- % disp(lang + " compiler: " + comp)
355- setenv(" FC" , comp );
356- return
357- end
358- end
359-
360- disp(" to hint Fortran compiler, setenv('FC', <Fortran compiler path>), or do 'mex -setup fortran'" )
361-
362- end
0 commit comments