blob: 99139767491915ad7f3678b1c42c6fc39a73c61b [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02002
Andres AG5121d4b2018-04-11 20:35:19 -05003# Generate main file, individual apps and solution files for MS Visual Studio
4# 2010
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +00006# Must be run from mbedTLS root or scripts directory.
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02007# Takes no argument.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02008
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02009use warnings;
10use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020011use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020012
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020013my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020014my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020015my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020016my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000017my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020018my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000019my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020020
21my $programs_dir = 'programs';
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022my $header_dir = 'include/mbedtls';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020023my $source_dir = 'library';
24
25# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020026my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020027 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020028EOT
29my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020030 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020031EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020032
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020033my $vsx_sln_app_entry_tpl = <<EOT;
34Project("{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
38EndProject\r
39EOT
40
41my $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
50EOT
51
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020052exit( main() );
53
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020054sub check_dirs {
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020055 return -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020056 && -d $header_dir
57 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020058 && -d $programs_dir;
59}
60
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020061sub 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é-Gonnard411f73e2014-05-09 13:00:18 +020072sub 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é-Gonnard41e8b622014-05-09 12:27:49 +020080sub gen_app_guid {
81 my ($path) = @_;
82
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000083 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020084 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
85
86 return $guid;
87}
88
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020089sub gen_app {
90 my ($path, $template, $dir, $ext) = @_;
91
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020092 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020093 $path =~ s!/!\\!g;
94 (my $appname = $path) =~ s/.*\\//;
95
Andres Amaya Garciac6753cd2019-01-08 20:05:18 +000096 my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
Andres Amaya Garcia024694e2018-10-30 21:46:21 +000097 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
98 $appname eq "query_compile_time_config" ) {
Andres Amaya Garciac6753cd2019-01-08 20:05:18 +000099 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>\r";
Andres Amaya Garcia024694e2018-10-30 21:46:21 +0000100 }
101
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200102 my $content = $template;
Andres Amaya Garcia024694e2018-10-30 21:46:21 +0000103 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200104 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200105 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200106
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200107 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200108}
109
110sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200111 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200112 die "make list failed: $!\n" if $?;
113
114 return split /\s+/, $app_list;
115}
116
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200117sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200118 my @app_list = @_;
119
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200120 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200121
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200122 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200123 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
124 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200125}
126
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200127sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200128 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200129
130 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200131 for my $name (@names) {
132 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200133 $entries .= $entry;
134 }
135
136 return $entries;
137}
138
139sub gen_main_file {
140 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
141
142 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
143 my $source_entries = gen_entry_list( $src_tpl, @$sources );
144
145 my $out = slurp_file( $main_tpl );
146 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
147 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
148
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200149 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200150}
151
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200152sub gen_vsx_solution {
153 my (@app_names) = @_;
154
155 my ($app_entries, $conf_entries);
156 for my $path (@app_names) {
157 my $guid = gen_app_guid( $path );
158 (my $appname = $path) =~ s!.*/!!;
159
160 my $app_entry = $vsx_sln_app_entry_tpl;
161 $app_entry =~ s/{APPNAME}/$appname/g;
162 $app_entry =~ s/{GUID}/$guid/g;
163
164 $app_entries .= $app_entry;
165
166 my $conf_entry = $vsx_sln_conf_entry_tpl;
167 $conf_entry =~ s/{GUID}/$guid/g;
168
169 $conf_entries .= $conf_entry;
170 }
171
172 my $out = slurp_file( $vsx_sln_tpl_file );
173 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
174 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
175
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200176 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200177}
178
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000179sub del_vsx_files {
180 unlink glob "'$vsx_dir/*.$vsx_ext'";
181 unlink $vsx_main_file;
182 unlink $vsx_sln_file;
183}
184
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200185sub main {
186 if( ! check_dirs() ) {
187 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000188 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200189 }
190
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000191 # Remove old files to ensure that, for example, project files from deleted
192 # apps are not kept
193 del_vsx_files();
194
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200195 my @app_list = get_app_list();
196 my @headers = <$header_dir/*.h>;
197 my @sources = <$source_dir/*.c>;
198 map { s!/!\\!g } @headers;
199 map { s!/!\\!g } @sources;
200
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200201 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200202
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200203 gen_main_file( \@headers, \@sources,
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200204 $vsx_hdr_tpl, $vsx_src_tpl,
205 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200206
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200207 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200208
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200209 return 0;
210}