PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/FieldTripBufferClients/FieldTripBufferMatlab/compile.m

https://bitbucket.org/jezhill/dsi2000
Objective C | 160 lines | 143 code | 17 blank | 0 comment | 26 complexity | fc4ba3242b8a7c056fe38ff094c16743 MD5 | raw file
  1. function compile(cc, libpath)
  2. % COMPILE is used for compiling and linking the 'buffer' MEX file.
  3. %
  4. % On Linux and MacOS X, you should just be able to call this function without arguments.
  5. %
  6. % On Windows, you can select a compiler using one of the following options:
  7. % compile('lcc') - LCC compiler (shipped with Matlab on 32bit platforms)
  8. % compile('bcb') - Borland C++ Builder
  9. % compile('bcc55') - Borland C++ 5.5 (free command line tools)
  10. % compile('mingw') - MinGW (GCC port without cygwin dependency)
  11. % compile('vc') - Visual Studio C++ 2005 or 2008
  12. %
  13. % Please note that this script does NOT set up your MEX environment for you, so in case
  14. % you haven't selected the C compiler on Windows yet, you need to type 'mex -setup' first
  15. % to choose either the Borland or Microsoft compiler. If you want to use MinGW, you also
  16. % need to install Gnumex (http://gnumex.sourceforget.net), which comes with its own
  17. % procedure for setting up the MEX environment.
  18. % Copyright (C) 2010-2012, Donders Centre for Cognitive Neuroimaging, Nijmegen, NL
  19. %
  20. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  21. % for the documentation and details.
  22. %
  23. % FieldTrip is free software: you can redistribute it and/or modify
  24. % it under the terms of the GNU General Public License as published by
  25. % the Free Software Foundation, either version 3 of the License, or
  26. % (at your option) any later version.
  27. %
  28. % FieldTrip is distributed in the hope that it will be useful,
  29. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. % GNU General Public License for more details.
  32. %
  33. % You should have received a copy of the GNU General Public License
  34. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  35. %
  36. % $Id: compile.m 7123 2012-12-06 21:21:38Z roboos $
  37. % You can tweak this a bit for setting platform-independent options, e.g for optimisation
  38. % or debugging. The default is to enable debug information (-g).
  39. % The -I../src is for including the header files of the buffer C library.
  40. cflags = '-I../src -g';
  41. if ispc
  42. % On Windows, we need to link against different libraries
  43. % If you are having problems compiling the buffer, you can try to tweak
  44. % the variables 'extra_cflags' and 'ldflags' specific to your compiler,
  45. % or you can add your own compiler flags.
  46. if strcmp(computer,'PCWIN64')
  47. extra_cflags = '-I../../external/pthreads-win64/include';
  48. amd64 = true;
  49. else
  50. extra_cflags = '-I../../external/pthreads-win32/include';
  51. amd64 = false;
  52. end
  53. suffix = 'obj';
  54. if nargin<1
  55. cc = 'LCC';
  56. if amd64
  57. error('Running on 64-bit Windows. Please setup your compiler environment for either Visual C++ or MinGW64, and then call compile(''vc'') or compile(''mingw'').');
  58. end
  59. end
  60. switch upper(cc)
  61. case 'BCB'
  62. ldflags = '-L../../external/pthreads-win32/lib -lpthreadVC2.bcb';
  63. case 'BCC55'
  64. ldflags = '-L../../external/pthreads-win32/lib -lpthreadVC2_bcc55';
  65. case 'MINGW'
  66. % For MinGW/Gnumex, it seems to be easier to just directly refer to the archives, since
  67. % the MEX tools expect libraries to end with .lib, whereas MinGW uses the .a suffix.
  68. if amd64
  69. ldflags = '../../external/pthreads-win64/lib/libpthread.a';
  70. ws2lib = 'C:/mingw64/x86_64-w64-mingw32/lib/libws2_32.a';
  71. else
  72. ldflags = '../../external/pthreads-win32/lib/libpthreadGC2.a';
  73. ws2lib = 'C:/mingw/lib/libws2_32.a';
  74. end
  75. if nargin<2
  76. if ~exist(ws2lib,'file')
  77. fprintf(1,'Library file WS2_32 does not exist in guessed location %s.\n', ws2lib);
  78. fprintf(1,'Please re-run this script with a second argument pointing to your MinGW\n');
  79. fprintf(1,'installation''s LIB folder, e.g., compile(''mingw'',''D:\MinGW\lib''\n');
  80. error('unsuccesful');
  81. end
  82. else
  83. ws2lib = [libpath '/libws2_32.a'];
  84. if ~exist(ws2lib,'file')
  85. fprintf(1,'Library file %s does not exist.\n', ws2lib);
  86. error('unsuccesful');
  87. end
  88. end
  89. ldflags = [ldflags ' ' ws2lib];
  90. case 'VC'
  91. if amd64
  92. ldflags = '-L../../external/pthreads-win64/lib -lpthreadVC2 ws2_32.lib';
  93. else
  94. ldflags = '-L../../external/pthreads-win32/lib -lpthreadVC2 ws2_32.lib ';
  95. end
  96. case 'LCC'
  97. ldflags = '-L../../external/pthreads-win32/lib -lpthreadGC2_lcc';
  98. ldflags = [ldflags ' "' matlabroot '\sys\lcc\lib\wsock32.lib"'];
  99. %ldflags = [ldflags ' "' matlabroot '\sys\lcc\lib\kernel32.lib"'];
  100. otherwise
  101. error 'Unsupported compiler - select one of LCC, VC, MINGW, BCC55, BCB';
  102. end
  103. else
  104. % On POSIX systems such as MacOS X and Linux, the following should work without tweaking
  105. ldflags = '-lpthread';
  106. extra_cflags = '';
  107. suffix = 'o';
  108. end
  109. % This is the list of C files from the low-level buffer implementation that we need
  110. libfuncs = {'util', 'printstruct', 'clientrequest', ...
  111. 'tcprequest', 'tcpserver', 'tcpsocket', ...
  112. 'extern', 'dmarequest', 'endianutil', 'cleanup'};
  113. % If you want to add a new helper function to the MEX file, you should just add
  114. % its name here (without the .c suffix)
  115. helpers = {'buffer_gethdr', 'buffer_getdat', 'buffer_getevt', ...
  116. 'buffer_puthdr', 'buffer_putdat', 'buffer_putevt', ...
  117. 'buffer_flushhdr', 'buffer_flushdat', 'buffer_flushevt', ...
  118. 'buffer_waitdat', 'buffer_mxutils'};
  119. %%%% Please do not change anything below this line %%%%
  120. % this will become the list of objects files for inclusion during linking
  121. allObjects = '';
  122. % This is for compiling all the library functions (no linking yet).
  123. for i=1:length(libfuncs)
  124. fprintf(1,'Compiling library functions in %s.c ...\n', libfuncs{i});
  125. cmd = sprintf('mex -c %s %s ../src/%s.c', cflags, extra_cflags, libfuncs{i});
  126. eval(cmd);
  127. % append newly created object file to the list of files we need to link
  128. allObjects = sprintf('%s %s.%s', allObjects, libfuncs{i}, suffix);
  129. end
  130. fprintf(1,'\n');
  131. % This is for compiling all the helper functions (no linking yet).
  132. for i=1:length(helpers)
  133. fprintf(1,'Compiling helper functions in %s.c ...\n', helpers{i});
  134. cmd = sprintf('mex -c %s %s %s.c', cflags, extra_cflags, helpers{i});
  135. eval(cmd);
  136. % append newly created object file to the list of files we need to link
  137. allObjects = sprintf('%s %s.%s', allObjects, helpers{i}, suffix);
  138. end
  139. % This is for compiling buffer.c and linking everything into the MEX file.
  140. fprintf(1,'Compiling and linking MEX file:\n');
  141. cmd = sprintf('mex %s %s buffer.c %s %s',cflags,extra_cflags,allObjects,ldflags);
  142. disp(cmd)
  143. eval(cmd);