generate_visualc_files.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 OR GPL-2.0-or-later
  10. #
  11. # This file is provided under the Apache License 2.0, or the
  12. # GNU General Public License v2.0 or later.
  13. #
  14. # **********
  15. # Apache License 2.0:
  16. #
  17. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. # not use this file except in compliance with the License.
  19. # You may obtain a copy of the License at
  20. #
  21. # http://www.apache.org/licenses/LICENSE-2.0
  22. #
  23. # Unless required by applicable law or agreed to in writing, software
  24. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. # See the License for the specific language governing permissions and
  27. # limitations under the License.
  28. #
  29. # **********
  30. #
  31. # **********
  32. # GNU General Public License v2.0 or later:
  33. #
  34. # This program is free software; you can redistribute it and/or modify
  35. # it under the terms of the GNU General Public License as published by
  36. # the Free Software Foundation; either version 2 of the License, or
  37. # (at your option) any later version.
  38. #
  39. # This program is distributed in the hope that it will be useful,
  40. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. # GNU General Public License for more details.
  43. #
  44. # You should have received a copy of the GNU General Public License along
  45. # with this program; if not, write to the Free Software Foundation, Inc.,
  46. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  47. #
  48. # **********
  49. use warnings;
  50. use strict;
  51. use Digest::MD5 'md5_hex';
  52. my $vsx_dir = "visualc/VS2010";
  53. my $vsx_ext = "vcxproj";
  54. my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
  55. my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
  56. my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
  57. my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
  58. my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
  59. my $programs_dir = 'programs';
  60. my $header_dir = 'include/mbedtls';
  61. my $source_dir = 'library';
  62. # Need windows line endings!
  63. my $vsx_hdr_tpl = <<EOT;
  64. <ClInclude Include="..\\..\\{NAME}" />\r
  65. EOT
  66. my $vsx_src_tpl = <<EOT;
  67. <ClCompile Include="..\\..\\{NAME}" />\r
  68. EOT
  69. my $vsx_sln_app_entry_tpl = <<EOT;
  70. Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
  71. ProjectSection(ProjectDependencies) = postProject\r
  72. {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
  73. EndProjectSection\r
  74. EndProject\r
  75. EOT
  76. my $vsx_sln_conf_entry_tpl = <<EOT;
  77. {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
  78. {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
  79. {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
  80. {GUID}.Debug|x64.Build.0 = Debug|x64\r
  81. {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
  82. {GUID}.Release|Win32.Build.0 = Release|Win32\r
  83. {GUID}.Release|x64.ActiveCfg = Release|x64\r
  84. {GUID}.Release|x64.Build.0 = Release|x64\r
  85. EOT
  86. exit( main() );
  87. sub check_dirs {
  88. return -d $vsx_dir
  89. && -d $header_dir
  90. && -d $source_dir
  91. && -d $programs_dir;
  92. }
  93. sub slurp_file {
  94. my ($filename) = @_;
  95. local $/ = undef;
  96. open my $fh, '<', $filename or die "Could not read $filename\n";
  97. my $content = <$fh>;
  98. close $fh;
  99. return $content;
  100. }
  101. sub content_to_file {
  102. my ($content, $filename) = @_;
  103. open my $fh, '>', $filename or die "Could not write to $filename\n";
  104. print $fh $content;
  105. close $fh;
  106. }
  107. sub gen_app_guid {
  108. my ($path) = @_;
  109. my $guid = md5_hex( "mbedTLS:$path" );
  110. $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
  111. return $guid;
  112. }
  113. sub gen_app {
  114. my ($path, $template, $dir, $ext) = @_;
  115. my $guid = gen_app_guid( $path );
  116. $path =~ s!/!\\!g;
  117. (my $appname = $path) =~ s/.*\\//;
  118. my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
  119. if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
  120. $appname eq "query_compile_time_config" ) {
  121. $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>";
  122. }
  123. my $content = $template;
  124. $content =~ s/<SOURCES>/$srcs/g;
  125. $content =~ s/<APPNAME>/$appname/g;
  126. $content =~ s/<GUID>/$guid/g;
  127. content_to_file( $content, "$dir/$appname.$ext" );
  128. }
  129. sub get_app_list {
  130. my $app_list = `cd $programs_dir && make list`;
  131. die "make list failed: $!\n" if $?;
  132. return split /\s+/, $app_list;
  133. }
  134. sub gen_app_files {
  135. my @app_list = @_;
  136. my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
  137. for my $app ( @app_list ) {
  138. gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
  139. }
  140. }
  141. sub gen_entry_list {
  142. my ($tpl, @names) = @_;
  143. my $entries;
  144. for my $name (@names) {
  145. (my $entry = $tpl) =~ s/{NAME}/$name/g;
  146. $entries .= $entry;
  147. }
  148. return $entries;
  149. }
  150. sub gen_main_file {
  151. my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
  152. my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
  153. my $source_entries = gen_entry_list( $src_tpl, @$sources );
  154. my $out = slurp_file( $main_tpl );
  155. $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
  156. $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
  157. content_to_file( $out, $main_out );
  158. }
  159. sub gen_vsx_solution {
  160. my (@app_names) = @_;
  161. my ($app_entries, $conf_entries);
  162. for my $path (@app_names) {
  163. my $guid = gen_app_guid( $path );
  164. (my $appname = $path) =~ s!.*/!!;
  165. my $app_entry = $vsx_sln_app_entry_tpl;
  166. $app_entry =~ s/{APPNAME}/$appname/g;
  167. $app_entry =~ s/{GUID}/$guid/g;
  168. $app_entries .= $app_entry;
  169. my $conf_entry = $vsx_sln_conf_entry_tpl;
  170. $conf_entry =~ s/{GUID}/$guid/g;
  171. $conf_entries .= $conf_entry;
  172. }
  173. my $out = slurp_file( $vsx_sln_tpl_file );
  174. $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
  175. $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
  176. content_to_file( $out, $vsx_sln_file );
  177. }
  178. sub del_vsx_files {
  179. unlink glob "'$vsx_dir/*.$vsx_ext'";
  180. unlink $vsx_main_file;
  181. unlink $vsx_sln_file;
  182. }
  183. sub main {
  184. if( ! check_dirs() ) {
  185. chdir '..' or die;
  186. check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
  187. }
  188. # Remove old files to ensure that, for example, project files from deleted
  189. # apps are not kept
  190. del_vsx_files();
  191. my @app_list = get_app_list();
  192. my @headers = <$header_dir/*.h>;
  193. my @sources = <$source_dir/*.c>;
  194. map { s!/!\\!g } @headers;
  195. map { s!/!\\!g } @sources;
  196. gen_app_files( @app_list );
  197. gen_main_file( \@headers, \@sources,
  198. $vsx_hdr_tpl, $vsx_src_tpl,
  199. $vsx_main_tpl_file, $vsx_main_file );
  200. gen_vsx_solution( @app_list );
  201. return 0;
  202. }