Skip to content

Commit 3ce76ff

Browse files
committed
incremental build for Matlab < R2024b too
1 parent 0ef7bb6 commit 3ce76ff

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

Readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ Matlab or
1010
users coming from other languages will benefit from the functionality contained within this user-developed, unofficial "stdlib" standard library of functions.
1111
These system, filesystem, and HDF5 / HDF4 / NetCDF functions are used across numerous projects.
1212

13-
Matlab &ge; R2022a is required to run the self-test suite.
1413
Matlab R2019b is the minimum required due to use of
1514
[arguments](https://www.mathworks.com/help/matlab/ref/arguments.html)
1615
syntax.
1716
URLs (e.g. https://, s3:// and similar) are treated as not existing.
1817

19-
Self-tests can be run from that matlab-stdlib/ directory:
18+
Self-tests can be run from that matlab-stdlib/ directory with Matlab R2022b or newer:
2019

2120
```matlab
2221
buildtool test
@@ -25,9 +24,9 @@ buildtool test
2524
Functions requiring or optionally benefiting from MEX are indicated in the
2625
[API Documentation](https://geospace-code.github.io/matlab-stdlib).
2726

28-
To build the optional high-performance
27+
Build the optional high-performance
2928
[MEX](https://www.mathworks.com/help/matlab/cpp-mex-file-applications.html)
30-
functions from the Matlab prompt:
29+
functions from the Matlab prompt in Matlab R2022b or newer:
3130

3231
```matlab
3332
buildtool mex

buildfile.m

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
% can't use SourceFiles= if "mex" Task was run, even if
1616
% plan("test").DisableIncremental = true;
1717
% this means incremental tests can't be used with MEX files (as of R2024b)
18+
19+
plan("clean") = matlab.buildtool.tasks.CleanTask;
1820
end
1921

2022
if ~isMATLABReleaseOlderThan("R2024a")
@@ -23,40 +25,45 @@
2325

2426
plan("publish") = matlab.buildtool.Task(Description="HTML inline doc generate", Actions=@publishTask);
2527

28+
%% MexTask
2629
bindir = fullfile(plan.RootFolder, pkg_name);
27-
if isMATLABReleaseOlderThan("R2024b")
28-
plan("mex") = matlab.buildtool.Task(Actions=@(~)legacyMexTask(bindir));
29-
else
30-
plan("clean") = matlab.buildtool.tasks.CleanTask;
30+
[compiler_id, compiler_opt] = get_compiler_options();
3131

32-
[compiler_id, compiler_opt] = get_compiler_options();
32+
if isMATLABReleaseOlderThan("R2024b")
33+
% dummy task to allow "buildtool mex" to build all MEX targets
34+
plan("mex") = matlab.buildtool.Task();
35+
mex_deps = string.empty;
36+
end
3337

34-
for s = get_mex_sources()
35-
src = s{1};
36-
[~, name] = fileparts(src(1));
38+
for s = get_mex_sources()
39+
src = s{1};
40+
[~, name] = fileparts(src(1));
3741

42+
% name of MEX target function is name of first source file
43+
if isMATLABReleaseOlderThan("R2024b")
44+
mex_name = "mex_" + name;
45+
% specifying .Inputs and .Outputs enables incremental builds
46+
% https://www.mathworks.com/help/matlab/matlab_prog/improve-performance-with-incremental-builds.html
47+
plan(mex_name) = matlab.buildtool.Task(Actions=@(context) legacyMexTask(context, compiler_id, compiler_opt));
48+
plan(mex_name).Inputs = src;
49+
plan(mex_name).Outputs = fullfile(bindir, name + "." + mexext());
50+
mex_deps(end+1) = mex_name; %#ok<AGROW>
51+
else
3852
plan("mex:" + name) = matlab.buildtool.tasks.MexTask(src, bindir, ...
3953
Options=[compiler_id, compiler_opt]);
4054
end
4155
end
4256

57+
if isMATLABReleaseOlderThan("R2024b")
58+
plan("mex").Dependencies = mex_deps;
4359
end
4460

61+
end
4562

46-
function legacyMexTask(bindir)
47-
48-
[compiler_id, compiler_opt] = get_compiler_options();
49-
50-
srcs = get_mex_sources();
5163

52-
%% build C++ mex
53-
% https://www.mathworks.com/help/matlab/ref/mex.html
54-
for s = srcs
55-
src = s{1};
56-
[~, name] = fileparts(src(1));
57-
disp("Building MEX target: " + name)
58-
mex(s{1}{:}, "-outdir", bindir, compiler_id, compiler_opt)
59-
end
64+
function legacyMexTask(context, compiler_id, compiler_opt)
65+
bindir = fileparts(context.Task.Outputs.Path);
66+
mex(context.Task.Inputs.Path, "-outdir", bindir, compiler_id, compiler_opt)
6067
end
6168

6269

0 commit comments

Comments
 (0)