forked from kyamagu/mexplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.m
141 lines (136 loc) · 4.19 KB
/
make.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function make(command, varargin)
%MAKE Build MEX files.
%
% make [command [options ...]]
%
% Build MEX files in the project. Customize this file to change how to build
% a project. By default, the script builds an example in the project directory,
% `make clean` delete any built binary, and
%
% Example
% -------
%
% make
% make clean
% make test
%
if nargin < 1, command = 'all'; end
root_dir = fileparts(mfilename('fullpath'));
switch command
case 'all'
targets = [getTarget(root_dir), getTestTargets(root_dir)];
arrayfun(@(target)buildTarget(target, varargin{:}), targets);
case 'clean'
clear mex;
targets = [getTarget(root_dir), getTestTargets(root_dir)];
deleteTargets(targets);
case 'test'
targets = getTestTargets(root_dir);
arrayfun(@(target)buildTarget(target, varargin{:}), targets);
run(fullfile(root_dir, 'test', 'testAll.m'));
otherwise
targets = [getTarget(root_dir), getTestTargets(root_dir)];
index = strcmp(strrep({targets.name}, [root_dir, filesep], ''), ...
strrep(command, root_dir, ''));
if ~any(index)
error('make:unknownTarget', 'No rule to make %s.', command);
end
buildTarget(targets(index), varargin{:});
end
end
function target = getTarget(root_dir)
%GETTARGET Get a build target.
target = struct( ...
'name', fullfile(root_dir, 'example', 'private', 'Database_'), ...
'sources', {{ ...
fullfile(root_dir, 'example', 'private', 'Database_.cc'), ...
fullfile(root_dir, 'example', 'private', 'Environment_.cc') ...
}}, ...
'options', sprintf('-I''%s''', fullfile(root_dir, 'include')) ...
);
end
function targets = getTestTargets(root_dir)
%GETTESTTARGETS Get test build targets.
options = sprintf('-I''%s''', fullfile(root_dir, 'include'));
targets = [ ...
struct( ...
'name', fullfile(root_dir, 'test', 'testArguments'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testArguments.cc') ...
}}, ...
'options', options ...
), ...
struct( ...
'name', fullfile(root_dir, 'test', 'testDispatch_'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testDispatch.cc') ...
}}, ...
'options', options ...
), ...
struct( ...
'name', fullfile(root_dir, 'test', 'testMxArray'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testMxArray.cc') ...
}}, ...
'options', options ...
), ...
struct( ...
'name', fullfile(root_dir, 'test', 'testMxTypes'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testMxTypes.cc') ...
}}, ...
'options', options ...
), ...
struct( ...
'name', fullfile(root_dir, 'test', 'testSession_'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testSession.cc') ...
}}, ...
'options', options ...
), ...
struct( ...
'name', fullfile(root_dir, 'test', 'testString_'), ...
'sources', {{ ...
fullfile(root_dir, 'test', 'testString.cc') ...
}}, ...
'options', options ...
) ...
];
end
function buildTarget(target, varargin)
%BUILDTARGET Build a single target.
if skipBuild(target)
return;
end
if isunix()
varargin = [varargin, 'CXXFLAGS="$CXXFLAGS -std=c++11"'];
end
command = sprintf('mex%s -output ''%s'' %s%s', ...
sprintf(' ''%s''', target.sources{:}), ...
target.name, ...
target.options, ...
sprintf(' %s', varargin{:}));
disp(command);
eval(command);
end
function flag = skipBuild(target)
%SKIPBUILD Check if the target is built and up-to-date.
output = [target.name, '.', mexext];
if ~exist(output, 'file')
flag = false;
return;
end
output_metadata = dir(output);
sources_metadata = cellfun(@dir, target.sources);
flag = all(output_metadata.datenum > [sources_metadata.datenum]);
end
function deleteTargets(targets)
%DELETETARGETS Delete target binaries.
binaries = strcat({targets.name}, '.', mexext);
for i = 1:numel(binaries)
if exist(binaries{i}, 'file')
fprintf('delete ''%s''\n', binaries{i});
delete(binaries{i});
end
end
end