1function gbcovmake
2%GBCOVMAKE compile the MATLAB interface for statement coverage testing
3%
4% See also: gbcover, gbcov_edit
5
6% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
7% SPDX-License-Identifier: GPL-3.0-or-later
8
9if verLessThan ('matlab', '9.4')
10    error ('MATLAB 9.4 (R2018a) or later is required') ;
11end
12
13warning ('off', 'MATLAB:MKDIR:DirectoryExists') ;
14mkdir ('tmp/@GrB/') ;
15mkdir ('tmp/@GrB/private') ;
16mkdir ('tmp/@GrB/util') ;
17mkdir ('tmp/cover') ;
18warning ('on', 'MATLAB:MKDIR:DirectoryExists') ;
19
20% copy all m-files into tmp/@GrB
21mfiles = dir ('../../@GrB/*.m') ;
22for k = 1:length (mfiles)
23    copyfile ([(mfiles (k).folder) '/' (mfiles (k).name)], 'tmp/@GrB/') ;
24end
25
26% copy all private m-files into tmp/@GrB/private
27mfiles = dir ('../../@GrB/private/*.m') ;
28for k = 1:length (mfiles)
29    copyfile ([(mfiles (k).folder) '/' (mfiles (k).name)], 'tmp/@GrB/private') ;
30end
31
32% copy the *.h files
33copyfile ('../../@GrB/private/util/*.h', 'tmp/@GrB/util') ;
34
35% copy and edit the mexfunction/*.c files
36cfiles = dir ('../../@GrB/private/mexfunctions/*.c') ;
37count = gbcov_edit (cfiles, 0, 'tmp/@GrB/private') ;
38
39% copy and edit the util/*.c files
40ufiles = [ dir('../../@GrB/private/util/*.c') ; dir('*.c') ] ;
41count = gbcov_edit (ufiles, count, 'tmp/@GrB/util') ;
42
43% create the gbfinish.c file and place in tmp/@GrB/util
44f = fopen ('tmp/@GrB/util/gbcovfinish.c', 'w') ;
45fprintf (f, '#include "gb_matlab.h"\n') ;
46fprintf (f, 'int64_t gbcov [GBCOV_MAX] ;\n') ;
47fprintf (f, 'int gbcov_max = %d ;\n', count) ;
48fclose (f) ;
49
50% compile the modified MATLAB interface
51
52% use -R2018a for the new interleaved complex API
53flags = '-g -R2018a -DGBCOV' ;
54
55try
56    if (strncmp (computer, 'GLNX', 4))
57        % remove -ansi from CFLAGS and replace it with -std=c11
58        cc = mex.getCompilerConfigurations ('C', 'Selected') ;
59        env = cc.Details.SetEnv ;
60        c1 = strfind (env, 'CFLAGS=') ;
61        q = strfind (env, '"') ;
62        q = q (q > c1) ;
63        if (~isempty (c1) && length (q) > 1)
64            c2 = q (2) ;
65            cflags = env (c1:c2) ;  % the CFLAGS="..." string
66            ansi = strfind (cflags, '-ansi') ;
67            if (~isempty (ansi))
68                cflags = [cflags(1:ansi-1) '-std=c11' cflags(ansi+5:end)] ;
69                flags = [flags ' ' cflags] ;
70                fprintf ('compiling with -std=c11 instead of default -ansi\n') ;
71            end
72        end
73    end
74catch
75end
76
77libraries = '-L../../../../../../build -L. -L/usr/local/lib -lgraphblas' ;
78
79if (~ismac && isunix)
80    flags = [ flags   ' CFLAGS="$CXXFLAGS -fopenmp -fPIC -Wno-pragmas" '] ;
81    flags = [ flags ' CXXFLAGS="$CXXFLAGS -fopenmp -fPIC -Wno-pragmas" '] ;
82    flags = [ flags  ' LDFLAGS="$LDFLAGS  -fopenmp -fPIC" '] ;
83end
84
85inc = ...
86'-I. -I../util -I../../../../../../Include -I../../../../../../Source -I../../../../../../Source/Template' ;
87
88here = pwd ;
89cd tmp/@GrB/private
90try
91
92    % compile util files
93    cfiles = dir ('../util/*.c') ;
94
95    objlist = '' ;
96    for k = 1:length (cfiles)
97        % get the full cfile filename
98        cfile = [(cfiles (k).folder) '/' (cfiles (k).name)] ;
99        % get the object file name
100        ofile = cfiles(k).name ;
101        objfile = [ ofile(1:end-2) '.o' ] ;
102        objlist = [ objlist ' ' objfile ] ; %#ok<*AGROW>
103        % compile the cfile
104        mexcmd = sprintf ('mex -c %s -silent %s %s', flags, inc, cfile) ;
105        fprintf ('%s\n', cfile) ;
106        % fprintf ('%s\n', mexcmd) ;
107        eval (mexcmd) ;
108    end
109
110    mexfunctions = dir ('*.c') ;
111
112    % compile the mexFunctions
113    for k = 1:length (mexfunctions)
114
115        % get the mexFunction filename and modification time
116        mexfunc = mexfunctions (k).name ;
117        mexfunction = [(mexfunctions (k).folder) '/' mexfunc] ;
118
119        % compile the mexFunction
120        mexcmd = sprintf ('mex -silent %s %s %s %s %s', ...
121            flags, inc, mexfunction, objlist, libraries) ;
122        fprintf ('%s\n', mexfunction) ;
123        % fprintf ('%s\n', mexcmd) ;
124        eval (mexcmd) ;
125    end
126
127catch me
128    disp (me.message)
129end
130cd (here)
131
132