generate_visualc_files.pl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/usr/bin/env perl
  2. # Generate main file, individual apps and solution files for MS Visual Studio
  3. # 2010
  4. #
  5. # Must be run from mbedTLS root or scripts directory.
  6. # Takes no argument.
  7. #
  8. # Copyright The Mbed TLS Contributors
  9. # SPDX-License-Identifier: Apache-2.0
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. # not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. use warnings;
  23. use strict;
  24. use Digest::MD5 'md5_hex';
  25. my $vsx_dir = "visualc/VS2010";
  26. my $vsx_ext = "vcxproj";
  27. my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
  28. my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
  29. my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
  30. my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
  31. my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
  32. my $programs_dir = 'programs';
  33. my $mbedtls_header_dir = 'include/mbedtls';
  34. my $psa_header_dir = 'include/psa';
  35. my $source_dir = 'library';
  36. my $test_source_dir = 'tests/src';
  37. my $test_header_dir = 'tests/include/test';
  38. my $test_drivers_header_dir = 'tests/include/test/drivers';
  39. my $test_drivers_source_dir = 'tests/src/drivers';
  40. my @thirdparty_header_dirs = qw(
  41. 3rdparty/everest/include/everest
  42. );
  43. my @thirdparty_source_dirs = qw(
  44. 3rdparty/everest/library
  45. 3rdparty/everest/library/kremlib
  46. 3rdparty/everest/library/legacy
  47. );
  48. # Directories to add to the include path.
  49. # Order matters in case there are files with the same name in more than
  50. # one directory: the compiler will use the first match.
  51. my @include_directories = qw(
  52. include
  53. 3rdparty/everest/include/
  54. 3rdparty/everest/include/everest
  55. 3rdparty/everest/include/everest/vs2010
  56. 3rdparty/everest/include/everest/kremlib
  57. tests/include
  58. );
  59. my $include_directories = join(';', map {"../../$_"} @include_directories);
  60. # Directories to add to the include path when building the library, but not
  61. # when building tests or applications.
  62. my @library_include_directories = qw(
  63. library
  64. );
  65. my $library_include_directories =
  66. join(';', map {"../../$_"} (@library_include_directories,
  67. @include_directories));
  68. my @excluded_files = qw(
  69. 3rdparty/everest/library/Hacl_Curve25519.c
  70. );
  71. my %excluded_files = ();
  72. foreach (@excluded_files) { $excluded_files{$_} = 1 }
  73. # Need windows line endings!
  74. my $vsx_hdr_tpl = <<EOT;
  75. <ClInclude Include="..\\..\\{NAME}" />\r
  76. EOT
  77. my $vsx_src_tpl = <<EOT;
  78. <ClCompile Include="..\\..\\{NAME}" />\r
  79. EOT
  80. my $vsx_sln_app_entry_tpl = <<EOT;
  81. Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
  82. ProjectSection(ProjectDependencies) = postProject\r
  83. {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
  84. EndProjectSection\r
  85. EndProject\r
  86. EOT
  87. my $vsx_sln_conf_entry_tpl = <<EOT;
  88. {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
  89. {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
  90. {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
  91. {GUID}.Debug|x64.Build.0 = Debug|x64\r
  92. {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
  93. {GUID}.Release|Win32.Build.0 = Release|Win32\r
  94. {GUID}.Release|x64.ActiveCfg = Release|x64\r
  95. {GUID}.Release|x64.Build.0 = Release|x64\r
  96. EOT
  97. exit( main() );
  98. sub check_dirs {
  99. foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
  100. if (not (-d $d)) { return 0; }
  101. }
  102. return -d $vsx_dir
  103. && -d $mbedtls_header_dir
  104. && -d $psa_header_dir
  105. && -d $source_dir
  106. && -d $test_source_dir
  107. && -d $test_drivers_source_dir
  108. && -d $test_header_dir
  109. && -d $test_drivers_header_dir
  110. && -d $programs_dir;
  111. }
  112. sub slurp_file {
  113. my ($filename) = @_;
  114. local $/ = undef;
  115. open my $fh, '<', $filename or die "Could not read $filename\n";
  116. my $content = <$fh>;
  117. close $fh;
  118. return $content;
  119. }
  120. sub content_to_file {
  121. my ($content, $filename) = @_;
  122. open my $fh, '>', $filename or die "Could not write to $filename\n";
  123. print $fh $content;
  124. close $fh;
  125. }
  126. sub gen_app_guid {
  127. my ($path) = @_;
  128. my $guid = md5_hex( "mbedTLS:$path" );
  129. $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
  130. return $guid;
  131. }
  132. sub gen_app {
  133. my ($path, $template, $dir, $ext) = @_;
  134. my $guid = gen_app_guid( $path );
  135. $path =~ s!/!\\!g;
  136. (my $appname = $path) =~ s/.*\\//;
  137. my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
  138. if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
  139. $appname eq "query_compile_time_config" ) {
  140. $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
  141. }
  142. if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
  143. $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
  144. }
  145. my $content = $template;
  146. $content =~ s/<SOURCES>/$srcs/g;
  147. $content =~ s/<APPNAME>/$appname/g;
  148. $content =~ s/<GUID>/$guid/g;
  149. $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
  150. content_to_file( $content, "$dir/$appname.$ext" );
  151. }
  152. sub get_app_list {
  153. my $app_list = `cd $programs_dir && make list`;
  154. die "make list failed: $!\n" if $?;
  155. return split /\s+/, $app_list;
  156. }
  157. sub gen_app_files {
  158. my @app_list = @_;
  159. my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
  160. for my $app ( @app_list ) {
  161. gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
  162. }
  163. }
  164. sub gen_entry_list {
  165. my ($tpl, @names) = @_;
  166. my $entries;
  167. for my $name (@names) {
  168. (my $entry = $tpl) =~ s/{NAME}/$name/g;
  169. $entries .= $entry;
  170. }
  171. return $entries;
  172. }
  173. sub gen_main_file {
  174. my ($headers, $sources,
  175. $hdr_tpl, $src_tpl,
  176. $main_tpl, $main_out) = @_;
  177. my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
  178. my $source_entries = gen_entry_list( $src_tpl, @$sources );
  179. my $out = slurp_file( $main_tpl );
  180. $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
  181. $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
  182. $out =~ s/INCLUDE_DIRECTORIES\r\n/$library_include_directories/g;
  183. content_to_file( $out, $main_out );
  184. }
  185. sub gen_vsx_solution {
  186. my (@app_names) = @_;
  187. my ($app_entries, $conf_entries);
  188. for my $path (@app_names) {
  189. my $guid = gen_app_guid( $path );
  190. (my $appname = $path) =~ s!.*/!!;
  191. my $app_entry = $vsx_sln_app_entry_tpl;
  192. $app_entry =~ s/{APPNAME}/$appname/g;
  193. $app_entry =~ s/{GUID}/$guid/g;
  194. $app_entries .= $app_entry;
  195. my $conf_entry = $vsx_sln_conf_entry_tpl;
  196. $conf_entry =~ s/{GUID}/$guid/g;
  197. $conf_entries .= $conf_entry;
  198. }
  199. my $out = slurp_file( $vsx_sln_tpl_file );
  200. $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
  201. $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
  202. content_to_file( $out, $vsx_sln_file );
  203. }
  204. sub del_vsx_files {
  205. unlink glob "'$vsx_dir/*.$vsx_ext'";
  206. unlink $vsx_main_file;
  207. unlink $vsx_sln_file;
  208. }
  209. sub main {
  210. if( ! check_dirs() ) {
  211. chdir '..' or die;
  212. check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
  213. }
  214. # Remove old files to ensure that, for example, project files from deleted
  215. # apps are not kept
  216. del_vsx_files();
  217. my @app_list = get_app_list();
  218. my @header_dirs = (
  219. $mbedtls_header_dir,
  220. $psa_header_dir,
  221. $test_header_dir,
  222. $test_drivers_header_dir,
  223. $source_dir,
  224. @thirdparty_header_dirs,
  225. );
  226. my @headers = (map { <$_/*.h> } @header_dirs);
  227. my @source_dirs = (
  228. $source_dir,
  229. $test_source_dir,
  230. $test_drivers_source_dir,
  231. @thirdparty_source_dirs,
  232. );
  233. my @sources = (map { <$_/*.c> } @source_dirs);
  234. @headers = grep { ! $excluded_files{$_} } @headers;
  235. @sources = grep { ! $excluded_files{$_} } @sources;
  236. map { s!/!\\!g } @headers;
  237. map { s!/!\\!g } @sources;
  238. gen_app_files( @app_list );
  239. gen_main_file( \@headers, \@sources,
  240. $vsx_hdr_tpl, $vsx_src_tpl,
  241. $vsx_main_tpl_file, $vsx_main_file );
  242. gen_vsx_solution( @app_list );
  243. return 0;
  244. }