generate_visualc_files.pl 4.8 KB

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