blob: 5038cece6b61402ad2a0398a1631b8d4b937f6ad [file] [log] [blame]
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02001#!/usr/bin/perl
2
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02003# create individual project files for example programs
4# for VS6 and VS2010
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
6# Must be run from PolarSSL root or scripts directory.
7# 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 $vs6_dir = "visualc/VS6";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020014my $vs6_ext = "dsp";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020015my $vs6_app_tpl_file = "scripts/data_files/vs6-app-template.$vs6_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020016my $vs6_main_tpl_file = "scripts/data_files/vs6-main-template.$vs6_ext";
17my $vs6_main_file = "$vs6_dir/polarssl.$vs6_ext";
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020018my $vs6_wsp_tpl_file = "scripts/data_files/vs6-workspace-template.dsw";
19my $vs6_wsp_file = "$vs6_dir/polarssl.dsw";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020020
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020021my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020022my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020023my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020024my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
25my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020026my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
27my $vsx_sln_file = "$vsx_dir/PolarSSL.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020028
29my $programs_dir = 'programs';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020030my $header_dir = 'include/polarssl';
31my $source_dir = 'library';
32
33# Need windows line endings!
34my $vs6_file_tpl = <<EOT;
35# Begin Source File\r
36\r
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020037SOURCE=..\\..\\{NAME}\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020038# End Source File\r
39EOT
40
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020041my $vs6_wsp_entry_tpl = <<EOT;
42###############################################################################\r
43\r
44Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r
45\r
46Package=<5>\r
47{{{\r
48}}}\r
49\r
50Package=<4>\r
51{{{\r
52 Begin Project Dependency\r
53 Project_Dep_Name polarssl\r
54 End Project Dependency\r
55}}}\r
56\r
57EOT
58
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020059my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020060 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020061EOT
62my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020063 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020064EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020065
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020066my $vsx_sln_app_entry_tpl = <<EOT;
67Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
68 ProjectSection(ProjectDependencies) = postProject\r
69 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
70 EndProjectSection\r
71EndProject\r
72EOT
73
74my $vsx_sln_conf_entry_tpl = <<EOT;
75 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
76 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
77 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
78 {GUID}.Debug|x64.Build.0 = Debug|x64\r
79 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
80 {GUID}.Release|Win32.Build.0 = Release|Win32\r
81 {GUID}.Release|x64.ActiveCfg = Release|x64\r
82 {GUID}.Release|x64.Build.0 = Release|x64\r
83EOT
84
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020085exit( main() );
86
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020087sub check_dirs {
88 return -d $vs6_dir
89 && -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020090 && -d $header_dir
91 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020092 && -d $programs_dir;
93}
94
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020095sub slurp_file {
96 my ($filename) = @_;
97
98 local $/ = undef;
99 open my $fh, '<', $filename or die "Could not read $filename\n";
100 my $content = <$fh>;
101 close $fh;
102
103 return $content;
104}
105
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200106sub content_to_file {
107 my ($content, $filename) = @_;
108
109 open my $fh, '>', $filename or die "Could not write to $filename\n";
110 print $fh $content;
111 close $fh;
112}
113
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200114sub gen_app_guid {
115 my ($path) = @_;
116
117 my $guid = md5_hex( "PolarSSL:$path" );
118 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
119
120 return $guid;
121}
122
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200123sub gen_app {
124 my ($path, $template, $dir, $ext) = @_;
125
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200126 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200127 $path =~ s!/!\\!g;
128 (my $appname = $path) =~ s/.*\\//;
129
130 my $content = $template;
131 $content =~ s/<PATHNAME>/$path/g;
132 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200133 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200134
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200135 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200136}
137
138sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200139 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200140 die "make list failed: $!\n" if $?;
141
142 return split /\s+/, $app_list;
143}
144
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200145sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200146 my @app_list = @_;
147
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200148 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
149 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200150
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200151 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200152 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
153 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
154 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200155}
156
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200157sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200158 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200159
160 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200161 for my $name (@names) {
162 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200163 $entries .= $entry;
164 }
165
166 return $entries;
167}
168
169sub gen_main_file {
170 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
171
172 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
173 my $source_entries = gen_entry_list( $src_tpl, @$sources );
174
175 my $out = slurp_file( $main_tpl );
176 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
177 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
178
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200179 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200180}
181
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200182sub gen_vs6_workspace {
183 my (@app_names) = @_;
184
185 map { s!.*/!! } @app_names;
186 my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names );
187
188 my $out = slurp_file( $vs6_wsp_tpl_file );
189 $out =~ s/APP_ENTRIES\r\n/$entries/m;
190
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200191 content_to_file( $out, $vs6_wsp_file );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200192}
193
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200194sub gen_vsx_solution {
195 my (@app_names) = @_;
196
197 my ($app_entries, $conf_entries);
198 for my $path (@app_names) {
199 my $guid = gen_app_guid( $path );
200 (my $appname = $path) =~ s!.*/!!;
201
202 my $app_entry = $vsx_sln_app_entry_tpl;
203 $app_entry =~ s/{APPNAME}/$appname/g;
204 $app_entry =~ s/{GUID}/$guid/g;
205
206 $app_entries .= $app_entry;
207
208 my $conf_entry = $vsx_sln_conf_entry_tpl;
209 $conf_entry =~ s/{GUID}/$guid/g;
210
211 $conf_entries .= $conf_entry;
212 }
213
214 my $out = slurp_file( $vsx_sln_tpl_file );
215 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
216 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
217
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200218 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200219}
220
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200221sub main {
222 if( ! check_dirs() ) {
223 chdir '..' or die;
224 check_dirs or die "Must but run from PolarSSL root or scripts dir\n";
225 }
226
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200227 my @app_list = get_app_list();
228 my @headers = <$header_dir/*.h>;
229 my @sources = <$source_dir/*.c>;
230 map { s!/!\\!g } @headers;
231 map { s!/!\\!g } @sources;
232
233 print "Generating apps files... ";
234 gen_app_files( @app_list );
235 print "done.\n";
236
237 print "Generating main files... ";
238 gen_main_file( \@headers, \@sources,
239 $vs6_file_tpl, $vs6_file_tpl,
240 $vs6_main_tpl_file, $vs6_main_file );
241 gen_main_file( \@headers, \@sources,
242 $vsx_hdr_tpl, $vsx_src_tpl,
243 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200244 print "done.\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200245
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200246 print "Generating VS6 workspace file... ";
247 gen_vs6_workspace( @app_list );
248 print "done.\n";
249
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200250 print "Generating VS2010 solution file... ";
251 gen_vsx_solution( @app_list );
252 print "done.\n";
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200253
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200254 return 0;
255}