1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use XML::Twig;
6use File::Copy;
7use File::Temp 'tempfile';
8
9#
10# This script generates:
11#   new clamav-config.h.
12#   new visual studio project files for each library and program.
13#
14
15#########################################################
16# HACK HERE  HACK HERE  HACK HERE  HACK HERE  HACK HERE #
17#########################################################
18
19use constant DEBUG => 0;
20
21### CLAMAV-CONFIG.H MACROS ###
22# - Set to the proper win32 value or -1 to undef - #
23my %CONF = (
24    'AC_APPLE_UNIVERSAL_BUILD' => -1,
25    'ANONYMOUS_MAP' => -1,
26    'BIND_8_COMPAT' => -1,
27    'CLAMAVGROUP' => '"clamav"',
28    'CLAMAVUSER' => '"clamav"',
29    'CLAMUKO' => -1,
30    'CL_DEBUG' => -1,
31    'CL_BCUNSIGNED' => -1,
32    'CL_EXPERIMENTAL' => -1,
33    'CL_THREAD_SAFE' => '1',
34    'CONFDIR' => -1, #'"C:\\\\ClamAV"',
35    'CURSES_INCLUDE' => -1,
36    'C_AIX' => -1,
37    'C_BEOS' => -1,
38    'C_BIGSTACK' => -1,
39    'C_BSD' => -1,
40    'C_DARWIN' => -1,
41    'C_GNU_HURD' => -1,
42    'C_HPUX' => -1,
43    'C_INTERIX' => -1,
44    'C_IRIX' => -1,
45    'C_KFREEBSD_GNU' => -1,
46    'C_LINUX' => -1,
47    'C_OS2' => -1,
48    'C_OSF' => -1,
49    'C_QNX6' => -1,
50    'C_SOLARIS' => -1,
51    'DATADIR' => -1, #'"C:\\\\ClamAV\\\\db"',
52    'DEFAULT_FD_SETSIZE' => '1024',
53    'FDPASS_NEED_XOPEN' => -1,
54    'FILEBUFF' => '8192',
55    'FRESHCLAM_DNS_FIX' => -1,
56    'FRESHCLAM_NO_CACHE' => -1,
57    'HAVE_ARGZ_ADD' => -1,
58    'HAVE_ARGZ_APPEND' => -1,
59    'HAVE_ARGZ_COUNT' => -1,
60    'HAVE_ARGZ_CREATE_SEP' => -1,
61    'HAVE_ARGZ_H' => -1,
62    'HAVE_ARGZ_INSERT' => -1,
63    'HAVE_ARGZ_NEXT' => -1,
64    'HAVE_ARGZ_STRINGIFY' => -1,
65    'HAVE_ATTRIB_ALIGNED' => -1,
66    'HAVE_ATTRIB_PACKED' => -1,
67    'HAVE_BZLIB_H' => '1',
68    'HAVE_CLOSEDIR' => '1',
69    'HAVE_CTIME_R' => '1',
70    'HAVE_CTIME_R_2' => '1',
71    'HAVE_CTIME_R_3' => -1,
72    'HAVE_DECL_CYGWIN_CONV_PATH' => -1,
73    'HAVE_DIRENT_H' => '1',
74    'HAVE_DLD' => -1,
75    'HAVE_DLD_H' => -1,
76    'HAVE_DLERROR' => -1,
77    'HAVE_DLFCN_H' => '1',
78    'HAVE_DL_H' => -1,
79    'HAVE_DYLD' => -1,
80    'HAVE_ENABLE_EXTENDED_FILE_STDIO' => -1,
81    'HAVE_ERROR_T' => -1,
82    'HAVE_FD_PASSING' => -1,
83    'HAVE_FSEEKO' => '1',
84    'HAVE_GETADDRINFO' => '1',
85    'HAVE_GETPAGESIZE' => '1',
86    'HAVE_GRP_H' => -1,
87    'HAVE_ICONV' => -1,
88    'HAVE_INET_NTOP' => '1',
89    'HAVE_INITGROUPS' => -1,
90    'HAVE_INTTYPES_H' => '1',
91    'HAVE_IN_ADDR_T' => -1,
92    'HAVE_IN_PORT_T' => '1',
93    'HAVE_JSON' => '1',
94    'HAVE_LIBCHECK' => -1,
95    'HAVE_LIBDL' => '1',
96    'HAVE_LIBDLLOADER' => '1',
97    'HAVE_LIBMILTER_MFAPI_H' => -1,
98    'HAVE_LIBNCURSES' => -1,
99    'HAVE_LIBPDCURSES' => -1,
100    'HAVE_PCRE' => '1',
101    'USING_PCRE2' => '1',
102    'HAVE_LIBXML2' => '1',
103    'HAVE_LIBZ' => '1',
104    'HAVE_LIMITS_H' => '1',
105    'HAVE_LTDL' => '1',
106    'HAVE_MACH_O_DYLD_H' => -1,
107    'HAVE_MADVISE' => -1,
108    'HAVE_MALLINFO' => -1,
109    'HAVE_MALLOC_H' => '1',
110    'HAVE_MEMCPY' => '1',
111    'HAVE_MEMORY_H' => '1',
112    'HAVE_MKSTEMP' => '1',
113    'HAVE_MMAP' => -1,
114    'HAVE_NDIR_H' => -1,
115    'HAVE_OPENDIR' => '1',
116    'HAVE_POLL' => '1',
117    'HAVE_POLL_H' => -1,
118    'HAVE_PRAGMA_PACK' => '1',
119    'HAVE_PRAGMA_PACK_HPPA' => -1,
120    'HAVE_PRELOADED_SYMBOLS' => -1,
121    'HAVE_PTHREAD_YIELD' => '1',
122    'HAVE_PWD_H' => -1,
123    'HAVE_READDIR' => '1',
124    'HAVE_READDIR_R_2' => -1,
125    'HAVE_READDIR_R_3' => -1,
126    'HAVE_RECVMSG' => '1',
127    'HAVE_RESOLV_H' => '1',
128    'HAVE_SAR' => '1',
129    'HAVE_SCHED_YIELD' => -1,
130    'HAVE_SENDMSG' => '1',
131    'HAVE_SETGROUPS' => -1,
132    'HAVE_SETSID' => '1',
133    'HAVE_SHL_LOAD' => -1,
134    'HAVE_SNPRINTF' => '1',
135    'HAVE_STDBOOL_H' => '1',
136    'HAVE_STDINT_H' => -1,
137    'HAVE_STDLIB_H' => '1',
138    'HAVE_STRCASESTR' => -1,
139    'HAVE_STRERROR_R' => '1',
140    'HAVE_STRINGS_H' => -1,
141    'HAVE_STRING_H' => '1',
142    'HAVE_STRLCAT' => -1,
143    'HAVE_STRLCPY' => -1,
144    'HAVE_STRNDUP' => -1,
145    'HAVE_STRNSTR' => -1,
146    'HAVE_SYSCONF_SC_PAGESIZE' => -1,
147    'HAVE_SYSTEM_TOMMATH' => -1,
148    'HAVE_SYS_DL_H' => -1,
149    'HAVE_SYS_FILIO_H' => -1,
150    'HAVE_SYS_INTTYPES_H' => -1,
151    'HAVE_SYS_INT_TYPES_H' => -1,
152    'HAVE_SYS_MMAN_H' => -1,
153    'HAVE_SYS_PARAM_H' => -1,
154    'HAVE_SYS_SELECT_H' => -1,
155    'HAVE_SYS_STAT_H' => '1',
156    'HAVE_SYS_TIMES_H' => -1,
157    'HAVE_SYS_TYPES_H' => '1',
158    'HAVE_SYS_UIO_H' => -1,
159    'HAVE_TERMIOS_H' => -1,
160    'HAVE_UNAME_SYSCALL' => -1,
161    'HAVE_UNISTD_H' => -1,
162    'HAVE_VSNPRINTF' => '1',
163    'HAVE_WORKING_ARGZ' => -1,
164    'HAVE__INTERNAL__SHA_COLLECT' => -1,
165    'LIBCLAMAV_FULLVER' => '"9.0.1"',
166    'LIBCLAMAV_MAJORVER' => '9',
167    'LIBCLAMAV_FULLVER' => '"2.0.0"',
168    'LIBCLAMAV_MAJORVER' => '2',
169    'LTDL_DLOPEN_DEPLIBS' => -1,
170    'LT_DLSEARCH_PATH' => '""',
171    'LT_LIBEXT' => '"dll"',
172    'LT_LIBPREFIX' => -1,
173    'LT_MODULE_EXT' => '".dll"',
174    'LT_MODULE_PATH_VAR' => '"LD_LIBRARY_PATH"',
175    'LT_OBJDIR' => '""',
176    'NDEBUG' => -1,
177    'NEED_USCORE' => -1,
178    'NOBZ2PREFIX' => -1,
179    'NO_FD_SET' => -1,
180    'PACKAGE' => 'PACKAGE_NAME',
181    'PACKAGE_BUGREPORT' => '"https://github.com/Cisco-Talos/clamav/issues"',
182    'PACKAGE_NAME' => '"ClamAV"',
183    'PACKAGE_STRING' => '"ClamAV 0.103.5"',
184    'PACKAGE_TARNAME' => '"clamav"',
185    'PACKAGE_URL' => '"https://www.clamav.net/"',
186    'PACKAGE_VERSION' => '"0.103.5"',
187    'SCANBUFF' => '131072',
188    'SETPGRP_VOID' => '1',
189    'SIZEOF_INT' => '4',
190    'SIZEOF_LONG' => '4',
191    'SIZEOF_LONG_LONG' => '8',
192    'SIZEOF_SHORT' => '2',
193    'SIZEOF_VOID_P' => -1,
194    'STDC_HEADERS' => '1',
195    'SUPPORT_IPv6' => '1',
196    'USE_MPOOL' => '1',
197    'USE_SYSLOG' => -1,
198    'VERSION_SUFFIX' => '""',
199    'WORDS_BIGENDIAN' => '0',
200    '_LARGEFILE_SOURCE' => -1,
201    '_POSIX_PII_SOCKET' => -1,
202    '_REENTRANT' => '1',
203    '_THREAD_SAFE' => -1,
204    '__error_t_defined' => -1,
205    'const' => -1,
206    'error_t' => -1,
207    'inline' => '_inline',
208    'off_t' => -1,
209    'restrict' => -1,
210    'socklen_t' => -1,
211    'HAVE_SYS_FANOTIFY_H' => -1
212    );
213
214
215### PROJECT FILES ###
216# - makefile: path to Makefile.am from the root of the repo
217# - sections: section of Makefile.am to parse (without _SOURCES or _la_SOURCES)
218# - output: path to the output vcxproj file
219# - makefile_only: *optional* regex to allow exclusion of certain files from the vcxproj (use double escapes)
220# - vcxproj_only: *optional* regex to allow inclusion of certain files into the vcxproj (use double escapes)
221
222my @PROJECTS = (
223    # LIBCLAMAV #
224    {makefile => 'libclamav', sections => ['libclamav', 'libclamav_internal_utils'], output => 'win32/libclamav.vcxproj', vcxproj_only => '(3rdparty\\\\|compat\\\\|getopt\\.c|misc\\.c)'},
225
226    # LIBFRESHCLAM #
227    {makefile => 'libfreshclam', sections => ['libfreshclam'], output => 'win32/libfreshclam.vcxproj', vcxproj_only => '(3rdparty\\\\|compat\\\\|getopt\\.c|misc\\.c)'},
228
229    # LIBCLAMUNRAR_IFACE #
230    {makefile => 'libclamav', sections => ['libclamunrar_iface'], output => 'win32/libclamunrar_iface.vcxproj', vcxproj_only => 'compat\\\\'},
231
232    # LIBCLAMUNRAR #
233    {makefile => 'libclamav', sections => ['libclamunrar'], output => 'win32/libclamunrar.vcxproj'},
234
235    # LIBCLAMAVCXX #
236    {makefile => 'libclamav/c++', sections => ['libclamavcxx'], output => 'win32/libclamavcxx.vcxproj'},
237
238    # CLAMSCAN #
239    {makefile => 'clamscan', sections => ['clamscan'], output => 'win32/clamscan.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$'},
240
241    # CLAMDSCAN #
242    {makefile => 'clamdscan', sections => ['clamdscan'], output => 'win32/clamdscan.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$'},
243
244    # CLAMDSUBMIT #
245    {makefile => 'clamdsubmit', sections => ['clamdsubmit'], output => 'win32/clamdsubmit.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$'},
246
247    # CLAMD #
248    {makefile => 'clamd', sections => ['clamd'], output => 'win32/clamd.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c|(daz|clam)uko.*)$'},
249
250    # FRESHCLAM #
251    {makefile => 'freshclam', sections => ['freshclam'], output => 'win32/freshclam.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$', vcxproj_only => 'compat\\\\'},
252
253    # CLAMCONF #
254    {makefile => 'clamconf', sections => ['clamconf'], output => 'win32/clamconf.vcxproj', makefile_only => '(optparser\\.c$|getopt\\.c)$'},
255
256    # CLAMBC #
257    {makefile => 'clambc', sections => ['clambc'], output => 'win32/clambc.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$'},
258
259    # LLVMsystem #
260    {makefile => 'libclamav/c++', sections => ['libllvmsystem'], output => 'win32/LLVMsystem.vcxproj'},
261
262    # LLVMcodegen #
263    {makefile => 'libclamav/c++', sections => ['libllvmcodegen'], output => 'win32/LLVMcodegen.vcxproj'},
264
265    # LLVMx86codegen #
266    {makefile => 'libclamav/c++', sections => ['libllvmx86codegen'], output => 'win32/LLVMx86codegen.vcxproj'},
267
268    # LLVMjit #
269    {makefile => 'libclamav/c++', sections => ['libllvmjit'], output => 'win32/LLVMjit.vcxproj'},
270
271    # sigtool #
272    {makefile => 'sigtool', sections => ['sigtool'], output => 'win32/sigtool.vcxproj', makefile_only => '(optparser\\.c|getopt\\.c)$'},
273
274    );
275
276###########################################################
277# STOP HACKING HERE  STOP HACKING HERE  STOP HACKING HERE #
278###########################################################
279
280
281
282
283my %ref_files;
284my %files;
285my $exclude;
286my $do_patch = 0;
287
288sub file {
289    my ($twig, $file) = @_;
290    my $fname = $file->{'att'}->{'Include'};
291    return unless $fname =~ /^.*\.c(pp)?$/;
292    return if defined($exclude) && $fname =~ /$exclude/;
293    $file->delete unless !$do_patch || exists $ref_files{$fname};
294    $files{$fname} = 1;
295}
296
297$do_patch = $#ARGV == 0 && $ARGV[0] eq '--regen';
298die("Usage:\nupdate-win32.pl [--regen]\n\nChecks the win32 build system and regenerates it if --regen is given\n\n") if $#ARGV == 0 && $ARGV[0] eq '--help';
299my $BASE_DIR = `git rev-parse --git-dir`;
300chomp($BASE_DIR);
301die "This script only works in a GIT repository\n" unless $BASE_DIR;
302$BASE_DIR = "$BASE_DIR/..";
303my $VER = `git describe --always`;
304chomp($VER);
305die "Cannot determine git version via git-describe\n" unless $VER && !$?;
306$VER = "devel-$VER";
307my $w = 0;
308
309print "Processing clamav-config.h...\n";
310
311open IN, "< $BASE_DIR/clamav-config.h.in" || die "Cannot find clamav-config.h.in: $!\n";
312$do_patch and open OUT, "> $BASE_DIR/win32/clamav-config.h" || die "Cannot open clamav-config.h: $!\n";
313$do_patch and  print OUT "/* clamav-config.h.  Generated from clamav-config.h.in by update-win32.  */\n\n";
314while(<IN>) {
315    if(!/^#\s*undef (.*)/) {
316	$do_patch and print OUT $_;
317	next;
318    }
319    if($1 eq 'VERSION') {
320	$do_patch and print OUT "#define VERSION \"$VER\"\n";
321	next;
322    }
323    if(!exists($CONF{$1})) {
324	warn "Warning: clamav-config.h option '$1' is unknown. Please take a second to update this script.\n";
325	$do_patch and print OUT "/* #undef $1 */\n";
326	$w++;
327	next;
328    }
329    if($CONF{$1} eq -1) {
330	$do_patch and print OUT "/* #undef $1 */\n";
331    } else {
332	$do_patch and print OUT "#define $1 $CONF{$1}\n";
333    }
334}
335close IN;
336if($do_patch) {
337    close OUT;
338    print "clamav-config.h generated ($w warnings)\n";
339} else {
340    print "clamav-config.h.in parsed ($w warnings)\n";
341}
342foreach (@PROJECTS) {
343    my %proj = %$_;
344    %files = ();
345    %ref_files = ();
346    my $got = 0;
347    $exclude = $proj{'vcxproj_only'};
348    print "Parsing $proj{'output'}...\n";
349    open IN, "$proj{'makefile'}/Makefile.am" or die "Cannot open $proj{'makefile'}/Makefile.am\n";
350    while(<IN>) {
351	my ($trail, $fname);
352	if($got == 0) {
353	    next unless /^(.*?)(?:_la)?_SOURCES\s*\+?=\s*(.*?)\s*(\\)?\s*$/;
354	    next unless grep {$_ eq $1} (@{$proj{'sections'}});
355	    $got = 1;
356	    $trail = $3;
357	    $fname = $2;
358	} else {
359	    /^\s*(.*?)(\s*\\)?$/;
360	    $trail = $2;
361	    $fname = $1;
362	}
363	if($fname =~ /\.c(pp)?$/) {
364	    if($fname =~ s/^(\$\(top_srcdir\)|\.\.)\///) {
365		$fname = "../$fname";
366	    } else {
367		$fname = "../$proj{'makefile'}/$fname";
368	    }
369            $fname =~ y/\//\\/;
370	    $ref_files{$fname} = 1 unless defined($proj{'makefile_only'}) && $fname =~ /$proj{'makefile_only'}/;
371	}
372	$got = 0 unless $trail;
373    }
374    close IN;
375
376    my $xml = XML::Twig->new( keep_encoding => 1, twig_handlers => { 'ItemGroup/ClCompile' => \&file }, pretty_print => 'record' );
377    $xml->parsefile("$BASE_DIR/$proj{'output'}");
378
379    my @missing_in_vcxproj = grep ! exists $files{$_}, keys %ref_files;
380    my @missing_in_makefile = grep ! exists $ref_files{$_}, keys %files;
381
382    if($do_patch) {
383	if($#missing_in_vcxproj >=0) {
384	    my $group = $xml->root;
385	    while($group = $group->next_elt('ItemGroup')) {
386		last if $group->has_child('ClCompile');
387	    }
388	    if(!defined($group)) {
389		$group = $xml->root('ItemGroup');
390		$group->paste($xml->root);
391	    }
392	    foreach (@missing_in_vcxproj) {
393		my $addfile = $xml->root->new('ClCompile');
394		$addfile->set_att('Include' => $_);
395		$addfile->paste($group);
396		warn "Warning: File $_ not in $proj{'output'}: added!\n" foreach @missing_in_vcxproj;
397	    }
398	}
399	warn "Warning: File $_ not in $proj{'makefile'}/Makefile.am: deleted!\n" foreach @missing_in_makefile;
400	my ($fh, $filename) = tempfile();
401	$xml->print($fh);
402	close $fh;
403	move($filename, "$proj{'output'}");
404	print "Regenerated $proj{'output'} (".($#missing_in_vcxproj + $#missing_in_makefile + 2)." changes)\n";
405    } else {
406	warn "Warning: File $_ not in $proj{'output'}\n" foreach @missing_in_vcxproj;
407	warn "Warning: File $_ not in $proj{'makefile'}/Makefile.am\n" foreach @missing_in_makefile;
408	print "Parsed $proj{'output'} (".($#missing_in_vcxproj + $#missing_in_makefile + 2)." warnings)\n";
409    }
410}
411