File: striplibsfrommexfile.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (193 lines) | stat: -rw-r--r-- 6,900 bytes parent folder | download
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function striplibsfrommexfile(filename, testrun, alsorenameit)
% striplibsfrommexfile(filename [, testrun=0][, alsorenameit=1]);
%
% THIS FILE IS DEPRECATED/UNUSED/NO LONGER NEEDED AS OF Octave 5+, on
% Ubuntu 20.04-LTS+ and Debian 11+ as Octave's mex/mkoctfile command strips
% all libraries itself. Just left here for documentation, should a similar
% need arise in the future.
%
% Remove a certain set of dynamic library dependencies from MEX/OCT file
% given via 'filename'. Optional 'testrun' if set to non-zero value will
% perform a dry-run.
%
% The stripped libraries (see source of this file) are library dependencies
% added by default by Octave's build process for OCT/MEX files. They make
% sense for mathematical routines, but are totally redundant for PTB files.
%
% They also do harm, as they are versioned for some odd reason, so trying
% to load the mex files on Linux distributions with different versions of
% those libraries installed will fail.
%
% -> This script loads the binary code, searches for occurence of the
% superfluous library name strings and null's them out to remove the
% dependency. Then it rewrites the stripped mex binary.
%

if nargin < 2 || isempty(testrun)
    testrun = 0;
end

if nargin < 3 || isempty(alsorenameit)
    alsorenameit = 1;
end

if exist(filename, 'file')
    % Read original image:
    [fd, msg] = fopen(filename, 'r+');
    if fd == -1
        error('ERROR! MEX file %s could not be read [%s]! Can''t strip it!\n', filename, msg);
    end
    image = uint8(fread(fd));

    % Strip all following libraries from image:
    image = stripLibrary(image, 'libreadline');
    image = stripLibrary(image, 'libncurses');
    image = stripLibrary(image, 'libfft');
    image = stripLibrary(image, 'liblapack');
    image = stripLibrary(image, 'libblas');
    image = stripLibrary(image, 'libhdf');
    image = stripLibrary(image, 'libgfortran');
    image = stripLibrary(image, 'libz');
    image = stripLibrary(image, 'libcruft');
    image = stripLibrary(image, 'liboctave');

    % No renamings for Debian package version
    alsorenameit = 0;

    if alsorenameit
        % Rename the following libraries in image: We rename from .so.2 to .so,
        % etc. to make these mex files built on/against Octave-3/4 compatible with
        % at least Octave-3 and Octave-4. This works because while the
        % liboctinterp's so-name/version has changed, the api/abi of
        % Octave-4's C-Mex interface has not changed. Or so we hope until proven
        % wrong... -- Well, there was an ABI change between v4.2 and v4.4, in that
        % mwSize was redefined from unsigned long (uint32_t) to size_t, so on 64-Bit
        % architectures mwSize is no longer 32 bits, but 64 bits! As we pass values
        % around via mwSize*, this ends badly if one tries to use a mex file built
        % for < 4.4 with a >= 4.4 Octave and vice versa. But other than that, the
        % general principle still holds.
        if str2num(version ()(1)) == 6 && str2num(version ()(3)) >= 1
            % Octave 6.1 - liboctinterp.so.8:
            image = renameLibrary(image, 'liboctinterp.so.8', 'liboctinterp.so');
        elseif str2num(version ()(1)) == 5 && str2num(version ()(3)) == 2
            % Octave 5.2 - liboctinterp.so.7:
            image = renameLibrary(image, 'liboctinterp.so.7', 'liboctinterp.so');
        elseif str2num(version ()(1)) == 4 && str2num(version ()(3)) == 4
            % Octave 4.4.1 - liboctinterp.so.6:
            image = renameLibrary(image, 'liboctinterp.so.6', 'liboctinterp.so');
        elseif str2num(version ()(1)) == 4 && str2num(version ()(3)) == 2
            % Octave 4.2 - liboctinterp.so.4:
            image = renameLibrary(image, 'liboctinterp.so.4', 'liboctinterp.so');
        elseif str2num(version ()(1)) == 4 && str2num(version ()(3)) == 0
            % Octave 4.0 - liboctinterp.so.3:
            image = renameLibrary(image, 'liboctinterp.so.3', 'liboctinterp.so');
        else
            % Octave 3.8.x, maybe earlier Octave 3.x.y as well - liboctinterp.so.2:
            image = renameLibrary(image, 'liboctinterp.so.2', 'liboctinterp.so');
        end
    end

    % Write stripped image:
    frewind(fd);
    if ~testrun
        fwrite(fd, image);
    end

    if (fclose(fd) ~= 0)
        error('ERROR! MEX file %s could not be closed [%s]! Can''t strip it!\n', filename, ferror(fd));
    end

    fprintf('MEX file %s stripped from superfluous libraries.\n', filename);
else
    fprintf('ERROR! MEX file %s does not exist! Can''t strip it!\n', filename);
    exit(1);
end

return

function image = stripLibrary(image, library)

    dodebug = 0;

    % Find first character of library in image:
    pStart = strfind(char(image'), library);
    if ~isempty(pStart)
        % Iterate over all occurences, kill each of them:
        for cStart = pStart
            % Kill occurence at cStart:

            if dodebug
                disp(char(image(cStart:cStart + 20)))
            end

            % Start at the start:
            p = cStart;

            % Strip-Loop: Strip until zero-byte:
            while (image(p) ~= 0)
                % Null-out this character:
                image(p) = 0;
                % Next one...
                p = p + 1;
            end

            if dodebug
                disp(char(image(cStart:cStart + 20)))
            end
        end
    end

    if ~isa(image, 'uint8')
        error('After strip op, image is no longer of uint8 class as required! Stripping failed!!');
    end

return

function image = renameLibrary(image, oldlibrary, newlibrary)

    dodebug = 0;

    if length(newlibrary) > length(oldlibrary)
        error('New replacement library name longer than to be replaced old name! Forbidden.');
    end

    % Find first character of library in image:
    pStart = strfind(char(image'), oldlibrary);
    if ~isempty(pStart)
        % Iterate over all occurences, kill each of them:
        for cStart = pStart
            % Kill occurence at cStart:

            if dodebug
                disp(char(image(cStart:cStart + 20)))
            end

            % Start at the start:
            p = cStart;
            c = 1;

            % Rename-Loop: Replace chars until zero-byte:
            while (image(p) ~= 0)
                if c <= length(newlibrary)
                    % Replace this character:
                    image(p) = newlibrary(c);
                else
                    % 0-out this character:
                    image(p) = 0;
                end

                % Next one...
                p = p + 1;
                c = c + 1;
            end

            if dodebug
                disp(char(image(cStart:cStart + 20)))
            end
        end
    end

    if ~isa(image, 'uint8')
        error('After rename op, image is no longer of uint8 class as required! Renaming failed!!');
    end
return