blob: 2ffdace40382d9bc22a26a027daa39e6405b7851 [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é-Gonnard41e8b622014-05-09 12:27:49 +0200106sub gen_app_guid {
107 my ($path) = @_;
108
109 my $guid = md5_hex( "PolarSSL:$path" );
110 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
111
112 return $guid;
113}
114
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200115sub gen_app {
116 my ($path, $template, $dir, $ext) = @_;
117
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200118 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200119 $path =~ s!/!\\!g;
120 (my $appname = $path) =~ s/.*\\//;
121
122 my $content = $template;
123 $content =~ s/<PATHNAME>/$path/g;
124 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200125 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200126
127 open my $app_fh, '>', "$dir/$appname.$ext";
128 print $app_fh $content;
129 close $app_fh;
130}
131
132sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200133 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200134 die "make list failed: $!\n" if $?;
135
136 return split /\s+/, $app_list;
137}
138
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200139sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200140 my @app_list = @_;
141
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200142 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
143 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200144
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200145 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200146 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
147 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
148 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200149}
150
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200151sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200152 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200153
154 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200155 for my $name (@names) {
156 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200157 $entries .= $entry;
158 }
159
160 return $entries;
161}
162
163sub gen_main_file {
164 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
165
166 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
167 my $source_entries = gen_entry_list( $src_tpl, @$sources );
168
169 my $out = slurp_file( $main_tpl );
170 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
171 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
172
173 open my $fh, '>', $main_out or die;
174 print $fh $out;
175 close $fh;
176}
177
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200178sub gen_vs6_workspace {
179 my (@app_names) = @_;
180
181 map { s!.*/!! } @app_names;
182 my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names );
183
184 my $out = slurp_file( $vs6_wsp_tpl_file );
185 $out =~ s/APP_ENTRIES\r\n/$entries/m;
186
187 open my $fh, '>', $vs6_wsp_file or die;
188 print $fh $out;
189 close $fh;
190}
191
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200192sub gen_vsx_solution {
193 my (@app_names) = @_;
194
195 my ($app_entries, $conf_entries);
196 for my $path (@app_names) {
197 my $guid = gen_app_guid( $path );
198 (my $appname = $path) =~ s!.*/!!;
199
200 my $app_entry = $vsx_sln_app_entry_tpl;
201 $app_entry =~ s/{APPNAME}/$appname/g;
202 $app_entry =~ s/{GUID}/$guid/g;
203
204 $app_entries .= $app_entry;
205
206 my $conf_entry = $vsx_sln_conf_entry_tpl;
207 $conf_entry =~ s/{GUID}/$guid/g;
208
209 $conf_entries .= $conf_entry;
210 }
211
212 my $out = slurp_file( $vsx_sln_tpl_file );
213 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
214 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
215
216 open my $fh, '>', $vsx_sln_file or die;
217 print $fh $out;
218 close $fh;
219}
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}