blob: 76fc59f1db378e52c3733314f0a49493e5392164 [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é-Gonnard2d34fe32014-05-07 17:51:20 +020026
27my $programs_dir = 'programs';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020028my $header_dir = 'include/polarssl';
29my $source_dir = 'library';
30
31# Need windows line endings!
32my $vs6_file_tpl = <<EOT;
33# Begin Source File\r
34\r
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020035SOURCE=..\\..\\{NAME}\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020036# End Source File\r
37EOT
38
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020039my $vs6_wsp_entry_tpl = <<EOT;
40###############################################################################\r
41\r
42Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r
43\r
44Package=<5>\r
45{{{\r
46}}}\r
47\r
48Package=<4>\r
49{{{\r
50 Begin Project Dependency\r
51 Project_Dep_Name polarssl\r
52 End Project Dependency\r
53}}}\r
54\r
55EOT
56
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020057my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020058 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020059EOT
60my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020061 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020062EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020063
64exit( main() );
65
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020066sub check_dirs {
67 return -d $vs6_dir
68 && -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020069 && -d $header_dir
70 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020071 && -d $programs_dir;
72}
73
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020074sub slurp_file {
75 my ($filename) = @_;
76
77 local $/ = undef;
78 open my $fh, '<', $filename or die "Could not read $filename\n";
79 my $content = <$fh>;
80 close $fh;
81
82 return $content;
83}
84
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020085sub gen_app_guid {
86 my ($path) = @_;
87
88 my $guid = md5_hex( "PolarSSL:$path" );
89 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
90
91 return $guid;
92}
93
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020094sub gen_app {
95 my ($path, $template, $dir, $ext) = @_;
96
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020097 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020098 $path =~ s!/!\\!g;
99 (my $appname = $path) =~ s/.*\\//;
100
101 my $content = $template;
102 $content =~ s/<PATHNAME>/$path/g;
103 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200104 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200105
106 open my $app_fh, '>', "$dir/$appname.$ext";
107 print $app_fh $content;
108 close $app_fh;
109}
110
111sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200112 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200113 die "make list failed: $!\n" if $?;
114
115 return split /\s+/, $app_list;
116}
117
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200118sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200119 my @app_list = @_;
120
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200121 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
122 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200123
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200124 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200125 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
126 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
127 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200128}
129
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200130sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200131 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200132
133 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200134 for my $name (@names) {
135 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200136 $entries .= $entry;
137 }
138
139 return $entries;
140}
141
142sub gen_main_file {
143 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
144
145 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
146 my $source_entries = gen_entry_list( $src_tpl, @$sources );
147
148 my $out = slurp_file( $main_tpl );
149 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
150 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
151
152 open my $fh, '>', $main_out or die;
153 print $fh $out;
154 close $fh;
155}
156
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200157sub gen_vs6_workspace {
158 my (@app_names) = @_;
159
160 map { s!.*/!! } @app_names;
161 my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names );
162
163 my $out = slurp_file( $vs6_wsp_tpl_file );
164 $out =~ s/APP_ENTRIES\r\n/$entries/m;
165
166 open my $fh, '>', $vs6_wsp_file or die;
167 print $fh $out;
168 close $fh;
169}
170
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200171sub main {
172 if( ! check_dirs() ) {
173 chdir '..' or die;
174 check_dirs or die "Must but run from PolarSSL root or scripts dir\n";
175 }
176
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200177 my @app_list = get_app_list();
178 my @headers = <$header_dir/*.h>;
179 my @sources = <$source_dir/*.c>;
180 map { s!/!\\!g } @headers;
181 map { s!/!\\!g } @sources;
182
183 print "Generating apps files... ";
184 gen_app_files( @app_list );
185 print "done.\n";
186
187 print "Generating main files... ";
188 gen_main_file( \@headers, \@sources,
189 $vs6_file_tpl, $vs6_file_tpl,
190 $vs6_main_tpl_file, $vs6_main_file );
191 gen_main_file( \@headers, \@sources,
192 $vsx_hdr_tpl, $vsx_src_tpl,
193 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200194 print "done.\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200195
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200196 print "Generating VS6 workspace file... ";
197 gen_vs6_workspace( @app_list );
198 print "done.\n";
199
200
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200201 return 0;
202}