blob: f6bf25ca6c5980cbe0299227714779c310a1dad0 [file] [log] [blame] [raw]
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02001#!/usr/bin/perl
2
Manuel Pégourié-Gonnard50868a72014-05-09 13:01:21 +02003# Generate files for MS Visual Studio:
4# - for VS6: main project (library) file, individual app files, workspace
5# - for VS2010: main file, individual apps, solution file
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02006#
7# Must be run from PolarSSL root or scripts directory.
8# Takes no argument.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02009
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020010use warnings;
11use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020012use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020013
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020014my $vs6_dir = "visualc/VS6";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020015my $vs6_ext = "dsp";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020016my $vs6_app_tpl_file = "scripts/data_files/vs6-app-template.$vs6_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020017my $vs6_main_tpl_file = "scripts/data_files/vs6-main-template.$vs6_ext";
18my $vs6_main_file = "$vs6_dir/polarssl.$vs6_ext";
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020019my $vs6_wsp_tpl_file = "scripts/data_files/vs6-workspace-template.dsw";
20my $vs6_wsp_file = "$vs6_dir/polarssl.dsw";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020021
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020022my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020023my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020024my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020025my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
26my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020027my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
28my $vsx_sln_file = "$vsx_dir/PolarSSL.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020029
30my $programs_dir = 'programs';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020031my $header_dir = 'include/polarssl';
32my $source_dir = 'library';
33
34# Need windows line endings!
35my $vs6_file_tpl = <<EOT;
36# Begin Source File\r
37\r
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020038SOURCE=..\\..\\{NAME}\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020039# End Source File\r
40EOT
41
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020042my $vs6_wsp_entry_tpl = <<EOT;
43###############################################################################\r
44\r
45Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r
46\r
47Package=<5>\r
48{{{\r
49}}}\r
50\r
51Package=<4>\r
52{{{\r
53 Begin Project Dependency\r
54 Project_Dep_Name polarssl\r
55 End Project Dependency\r
56}}}\r
57\r
58EOT
59
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020060my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020061 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020062EOT
63my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020064 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020065EOT
66
67my $vsx_sln_app_entry_tpl = <<EOT;
68Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
69 ProjectSection(ProjectDependencies) = postProject\r
70 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
71 EndProjectSection\r
72EndProject\r
73EOT
74
75my $vsx_sln_conf_entry_tpl = <<EOT;
76 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
77 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
78 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
79 {GUID}.Debug|x64.Build.0 = Debug|x64\r
80 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
81 {GUID}.Release|Win32.Build.0 = Release|Win32\r
82 {GUID}.Release|x64.ActiveCfg = Release|x64\r
83 {GUID}.Release|x64.Build.0 = Release|x64\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020084EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020085
86exit( main() );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020087
88sub check_dirs {
89 return -d $vs6_dir
90 && -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020091 && -d $header_dir
92 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020093 && -d $programs_dir;
94}
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020095
96sub slurp_file {
97 my ($filename) = @_;
98
99 local $/ = undef;
100 open my $fh, '<', $filename or die "Could not read $filename\n";
101 my $content = <$fh>;
102 close $fh;
103
104 return $content;
105}
106
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200107sub content_to_file {
108 my ($content, $filename) = @_;
109
110 open my $fh, '>', $filename or die "Could not write to $filename\n";
111 print $fh $content;
112 close $fh;
113}
114
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200115sub gen_app_guid {
116 my ($path) = @_;
117
118 my $guid = md5_hex( "PolarSSL:$path" );
119 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
120
121 return $guid;
122}
123
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200124sub gen_app {
125 my ($path, $template, $dir, $ext) = @_;
126
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200127 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200128 $path =~ s!/!\\!g;
129 (my $appname = $path) =~ s/.*\\//;
130
131 my $content = $template;
132 $content =~ s/<PATHNAME>/$path/g;
133 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200134 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200135
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200136 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200137}
138
139sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200140 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200141 die "make list failed: $!\n" if $?;
142
143 return split /\s+/, $app_list;
144}
145
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200146sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200147 my @app_list = @_;
148
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200149 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
150 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200151
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200152 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200153 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
154 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
155 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200156}
157
158sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200159 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200160
161 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200162 for my $name (@names) {
163 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200164 $entries .= $entry;
165 }
166
167 return $entries;
168}
169
170sub gen_main_file {
171 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
172
173 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
174 my $source_entries = gen_entry_list( $src_tpl, @$sources );
175
176 my $out = slurp_file( $main_tpl );
177 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
178 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
179
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200180 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200181}
182
183sub gen_vs6_workspace {
184 my (@app_names) = @_;
185
186 map { s!.*/!! } @app_names;
187 my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names );
188
189 my $out = slurp_file( $vs6_wsp_tpl_file );
190 $out =~ s/APP_ENTRIES\r\n/$entries/m;
191
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200192 content_to_file( $out, $vs6_wsp_file );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200193}
194
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200195sub gen_vsx_solution {
196 my (@app_names) = @_;
197
198 my ($app_entries, $conf_entries);
199 for my $path (@app_names) {
200 my $guid = gen_app_guid( $path );
201 (my $appname = $path) =~ s!.*/!!;
202
203 my $app_entry = $vsx_sln_app_entry_tpl;
204 $app_entry =~ s/{APPNAME}/$appname/g;
205 $app_entry =~ s/{GUID}/$guid/g;
206
207 $app_entries .= $app_entry;
208
209 my $conf_entry = $vsx_sln_conf_entry_tpl;
210 $conf_entry =~ s/{GUID}/$guid/g;
211
212 $conf_entries .= $conf_entry;
213 }
214
215 my $out = slurp_file( $vsx_sln_tpl_file );
216 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
217 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
218
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200219 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200220}
221
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200222sub main {
223 if( ! check_dirs() ) {
224 chdir '..' or die;
225 check_dirs or die "Must but run from PolarSSL root or scripts dir\n";
226 }
227
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200228 my @app_list = get_app_list();
229 my @headers = <$header_dir/*.h>;
230 my @sources = <$source_dir/*.c>;
231 map { s!/!\\!g } @headers;
232 map { s!/!\\!g } @sources;
233
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200234 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200235
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200236 gen_main_file( \@headers, \@sources,
237 $vs6_file_tpl, $vs6_file_tpl,
238 $vs6_main_tpl_file, $vs6_main_file );
239 gen_main_file( \@headers, \@sources,
240 $vsx_hdr_tpl, $vsx_src_tpl,
241 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200242
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200243 gen_vs6_workspace( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200244
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200245 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200246
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200247 return 0;
248}