generate_visualc_files.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. use warnings;
  8. use strict;
  9. use Digest::MD5 'md5_hex';
  10. my $vsx_dir = "visualc/VS2010";
  11. my $vsx_ext = "vcxproj";
  12. my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
  13. my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
  14. my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
  15. my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
  16. my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
  17. my $programs_dir = 'programs';
  18. my $header_dir = 'include/mbedtls';
  19. my $source_dir = 'library';
  20. # Need windows line endings!
  21. my $vsx_hdr_tpl = <<EOT;
  22. <ClInclude Include="..\\..\\{NAME}" />\r
  23. EOT
  24. my $vsx_src_tpl = <<EOT;
  25. <ClCompile Include="..\\..\\{NAME}" />\r
  26. EOT
  27. my $vsx_sln_app_entry_tpl = <<EOT;
  28. Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
  29. ProjectSection(ProjectDependencies) = postProject\r
  30. {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
  31. EndProjectSection\r
  32. EndProject\r
  33. EOT
  34. my $vsx_sln_conf_entry_tpl = <<EOT;
  35. {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
  36. {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
  37. {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
  38. {GUID}.Debug|x64.Build.0 = Debug|x64\r
  39. {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
  40. {GUID}.Release|Win32.Build.0 = Release|Win32\r
  41. {GUID}.Release|x64.ActiveCfg = Release|x64\r
  42. {GUID}.Release|x64.Build.0 = Release|x64\r
  43. EOT
  44. exit( main() );
  45. sub check_dirs {
  46. return -d $vsx_dir
  47. && -d $header_dir
  48. && -d $source_dir
  49. && -d $programs_dir;
  50. }
  51. sub slurp_file {
  52. my ($filename) = @_;
  53. local $/ = undef;
  54. open my $fh, '<', $filename or die "Could not read $filename\n";
  55. my $content = <$fh>;
  56. close $fh;
  57. return $content;
  58. }
  59. sub content_to_file {
  60. my ($content, $filename) = @_;
  61. open my $fh, '>', $filename or die "Could not write to $filename\n";
  62. print $fh $content;
  63. close $fh;
  64. }
  65. sub gen_app_guid {
  66. my ($path) = @_;
  67. my $guid = md5_hex( "mbedTLS:$path" );
  68. $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
  69. return $guid;
  70. }
  71. sub gen_app {
  72. my ($path, $template, $dir, $ext) = @_;
  73. my $guid = gen_app_guid( $path );
  74. $path =~ s!/!\\!g;
  75. (my $appname = $path) =~ s/.*\\//;
  76. my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
  77. if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
  78. $appname eq "query_compile_time_config" ) {
  79. $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>\r";
  80. }
  81. my $content = $template;
  82. $content =~ s/<SOURCES>/$srcs/g;
  83. $content =~ s/<APPNAME>/$appname/g;
  84. $content =~ s/<GUID>/$guid/g;
  85. content_to_file( $content, "$dir/$appname.$ext" );
  86. }
  87. sub get_app_list {
  88. my $app_list = `cd $programs_dir && make list`;
  89. die "make list failed: $!\n" if $?;
  90. return split /\s+/, $app_list;
  91. }
  92. sub gen_app_files {
  93. my @app_list = @_;
  94. my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
  95. for my $app ( @app_list ) {
  96. gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
  97. }
  98. }
  99. sub gen_entry_list {
  100. my ($tpl, @names) = @_;
  101. my $entries;
  102. for my $name (@names) {
  103. (my $entry = $tpl) =~ s/{NAME}/$name/g;
  104. $entries .= $entry;
  105. }
  106. return $entries;
  107. }
  108. sub gen_main_file {
  109. my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
  110. my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
  111. my $source_entries = gen_entry_list( $src_tpl, @$sources );
  112. my $out = slurp_file( $main_tpl );
  113. $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
  114. $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
  115. content_to_file( $out, $main_out );
  116. }
  117. sub gen_vsx_solution {
  118. my (@app_names) = @_;
  119. my ($app_entries, $conf_entries);
  120. for my $path (@app_names) {
  121. my $guid = gen_app_guid( $path );
  122. (my $appname = $path) =~ s!.*/!!;
  123. my $app_entry = $vsx_sln_app_entry_tpl;
  124. $app_entry =~ s/{APPNAME}/$appname/g;
  125. $app_entry =~ s/{GUID}/$guid/g;
  126. $app_entries .= $app_entry;
  127. my $conf_entry = $vsx_sln_conf_entry_tpl;
  128. $conf_entry =~ s/{GUID}/$guid/g;
  129. $conf_entries .= $conf_entry;
  130. }
  131. my $out = slurp_file( $vsx_sln_tpl_file );
  132. $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
  133. $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
  134. content_to_file( $out, $vsx_sln_file );
  135. }
  136. sub del_vsx_files {
  137. unlink glob "'$vsx_dir/*.$vsx_ext'";
  138. unlink $vsx_main_file;
  139. unlink $vsx_sln_file;
  140. }
  141. sub main {
  142. if( ! check_dirs() ) {
  143. chdir '..' or die;
  144. check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
  145. }
  146. # Remove old files to ensure that, for example, project files from deleted
  147. # apps are not kept
  148. del_vsx_files();
  149. my @app_list = get_app_list();
  150. my @headers = <$header_dir/*.h>;
  151. my @sources = <$source_dir/*.c>;
  152. map { s!/!\\!g } @headers;
  153. map { s!/!\\!g } @sources;
  154. gen_app_files( @app_list );
  155. gen_main_file( \@headers, \@sources,
  156. $vsx_hdr_tpl, $vsx_src_tpl,
  157. $vsx_main_tpl_file, $vsx_main_file );
  158. gen_vsx_solution( @app_list );
  159. return 0;
  160. }