fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 2 | |
Andres AG | 5121d4b | 2018-04-11 20:35:19 -0500 | [diff] [blame] | 3 | # Generate main file, individual apps and solution files for MS Visual Studio |
| 4 | # 2010 |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 5 | # |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 6 | # Must be run from mbedTLS root or scripts directory. |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 7 | # Takes no argument. |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 8 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 9 | use warnings; |
| 10 | use strict; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 11 | use Digest::MD5 'md5_hex'; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 12 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 13 | my $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 14 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 15 | my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 16 | my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 17 | my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 18 | my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 19 | my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 20 | |
| 21 | my $programs_dir = 'programs'; |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 22 | my $header_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 23 | my $source_dir = 'library'; |
| 24 | |
| 25 | # Need windows line endings! |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 26 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 27 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 28 | EOT |
| 29 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 30 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 31 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 33 | my $vsx_sln_app_entry_tpl = <<EOT; |
| 34 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r |
| 35 | ProjectSection(ProjectDependencies) = postProject\r |
| 36 | {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r |
| 37 | EndProjectSection\r |
| 38 | EndProject\r |
| 39 | EOT |
| 40 | |
| 41 | my $vsx_sln_conf_entry_tpl = <<EOT; |
| 42 | {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r |
| 43 | {GUID}.Debug|Win32.Build.0 = Debug|Win32\r |
| 44 | {GUID}.Debug|x64.ActiveCfg = Debug|x64\r |
| 45 | {GUID}.Debug|x64.Build.0 = Debug|x64\r |
| 46 | {GUID}.Release|Win32.ActiveCfg = Release|Win32\r |
| 47 | {GUID}.Release|Win32.Build.0 = Release|Win32\r |
| 48 | {GUID}.Release|x64.ActiveCfg = Release|x64\r |
| 49 | {GUID}.Release|x64.Build.0 = Release|x64\r |
| 50 | EOT |
| 51 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 52 | exit( main() ); |
| 53 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 54 | sub check_dirs { |
Manuel Pégourié-Gonnard | 51f14be | 2015-05-14 13:04:03 +0200 | [diff] [blame] | 55 | return -d $vsx_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 56 | && -d $header_dir |
| 57 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 58 | && -d $programs_dir; |
| 59 | } |
| 60 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 61 | sub slurp_file { |
| 62 | my ($filename) = @_; |
| 63 | |
| 64 | local $/ = undef; |
| 65 | open my $fh, '<', $filename or die "Could not read $filename\n"; |
| 66 | my $content = <$fh>; |
| 67 | close $fh; |
| 68 | |
| 69 | return $content; |
| 70 | } |
| 71 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 72 | sub content_to_file { |
| 73 | my ($content, $filename) = @_; |
| 74 | |
| 75 | open my $fh, '>', $filename or die "Could not write to $filename\n"; |
| 76 | print $fh $content; |
| 77 | close $fh; |
| 78 | } |
| 79 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 80 | sub gen_app_guid { |
| 81 | my ($path) = @_; |
| 82 | |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 83 | my $guid = md5_hex( "mbedTLS:$path" ); |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 84 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 85 | |
| 86 | return $guid; |
| 87 | } |
| 88 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 89 | sub gen_app { |
| 90 | my ($path, $template, $dir, $ext) = @_; |
| 91 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 92 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 93 | $path =~ s!/!\\!g; |
| 94 | (my $appname = $path) =~ s/.*\\//; |
| 95 | |
| 96 | my $content = $template; |
| 97 | $content =~ s/<PATHNAME>/$path/g; |
| 98 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 99 | $content =~ s/<GUID>/$guid/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 101 | content_to_file( $content, "$dir/$appname.$ext" ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 105 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 106 | die "make list failed: $!\n" if $?; |
| 107 | |
| 108 | return split /\s+/, $app_list; |
| 109 | } |
| 110 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 111 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 112 | my @app_list = @_; |
| 113 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 114 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 116 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 117 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 118 | } |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 121 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 122 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 123 | |
| 124 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 125 | for my $name (@names) { |
| 126 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 127 | $entries .= $entry; |
| 128 | } |
| 129 | |
| 130 | return $entries; |
| 131 | } |
| 132 | |
| 133 | sub gen_main_file { |
| 134 | my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_; |
| 135 | |
| 136 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
| 137 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 138 | |
| 139 | my $out = slurp_file( $main_tpl ); |
| 140 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 141 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
| 142 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 143 | content_to_file( $out, $main_out ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 146 | sub gen_vsx_solution { |
| 147 | my (@app_names) = @_; |
| 148 | |
| 149 | my ($app_entries, $conf_entries); |
| 150 | for my $path (@app_names) { |
| 151 | my $guid = gen_app_guid( $path ); |
| 152 | (my $appname = $path) =~ s!.*/!!; |
| 153 | |
| 154 | my $app_entry = $vsx_sln_app_entry_tpl; |
| 155 | $app_entry =~ s/{APPNAME}/$appname/g; |
| 156 | $app_entry =~ s/{GUID}/$guid/g; |
| 157 | |
| 158 | $app_entries .= $app_entry; |
| 159 | |
| 160 | my $conf_entry = $vsx_sln_conf_entry_tpl; |
| 161 | $conf_entry =~ s/{GUID}/$guid/g; |
| 162 | |
| 163 | $conf_entries .= $conf_entry; |
| 164 | } |
| 165 | |
| 166 | my $out = slurp_file( $vsx_sln_tpl_file ); |
| 167 | $out =~ s/APP_ENTRIES\r\n/$app_entries/m; |
| 168 | $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m; |
| 169 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 170 | content_to_file( $out, $vsx_sln_file ); |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 173 | sub del_vsx_files { |
| 174 | unlink glob "'$vsx_dir/*.$vsx_ext'"; |
| 175 | unlink $vsx_main_file; |
| 176 | unlink $vsx_sln_file; |
| 177 | } |
| 178 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 179 | sub main { |
| 180 | if( ! check_dirs() ) { |
| 181 | chdir '..' or die; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 182 | check_dirs or die "Must but run from mbedTLS root or scripts dir\n"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 185 | # Remove old files to ensure that, for example, project files from deleted |
| 186 | # apps are not kept |
| 187 | del_vsx_files(); |
| 188 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 189 | my @app_list = get_app_list(); |
| 190 | my @headers = <$header_dir/*.h>; |
| 191 | my @sources = <$source_dir/*.c>; |
| 192 | map { s!/!\\!g } @headers; |
| 193 | map { s!/!\\!g } @sources; |
| 194 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 195 | gen_app_files( @app_list ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 197 | gen_main_file( \@headers, \@sources, |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 198 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 199 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 201 | gen_vsx_solution( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 203 | return 0; |
| 204 | } |