summaryrefslogtreecommitdiff
path: root/infrastructure/BoxPlatform.pm.in
blob: 8297b93b484c461dff4a018a8b6df0a4542a849c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package BoxPlatform;
use Exporter;
@ISA = qw/Exporter/;
@EXPORT = qw/$build_os $build_cpu $target_os $make_command $bsd_make $platform_define $platform_cpu $gcc_v3 $product_version $product_name $install_into_dir $sub_make_options $platform_compile_line_extra $platform_link_line_extra $platform_lib_files $platform_exe_ext $target_windows update_if_changed/;

BEGIN
{

	# which OS are we building under?
	$target_os = '@target_os@';
	$target_windows = 0;
	$target_windows = 1 if $target_os =~ m'^mingw32' 
		or $target_os eq "winnt";

	if ($^O eq "MSWin32" and not -x "/usr/bin/uname")
	{
		$build_os = "winnt";
		$build_cpu = "ix86";
	}
	else
	{
		$build_os = `uname`;
		chomp $build_os;
		$build_cpu = `uname -m`;
		chomp $build_cpu;
	}

	# Cygwin Builds usually something like CYGWIN_NT-5.0, CYGWIN_NT-5.1
	# Box Backup tried on Win2000,XP only :)

	$build_os = 'CYGWIN' if $build_os =~ m/CYGWIN/;

	$make_command = ($build_os eq 'Darwin') ? 'bsdmake' : ($build_os eq 'SunOS') ? 'gmake' : 'make';
	$bsd_make = ($build_os ne 'Linux' && $build_os ne 'CYGWIN' && $build_os ne "SunOS");

	# blank extra flags by default
	$platform_compile_line_extra = '@CPPFLAGS@ @CXXFLAGS@ @CXXFLAGS_STRICT@';
	$platform_compile_line_extra =~ s/ -O2//;
	$platform_link_line_extra = '@LDFLAGS@';
	$platform_lib_files = '@LIBS@';
	$platform_exe_ext = '@EXEEXT@';

	# get version
	if (! -r "VERSION.txt" and -r "../../VERSION.txt")
	{
		open VERSION,"../../VERSION.txt" or die "../../VERSION.txt: $!";
	}
	else
	{
		open VERSION,"VERSION.txt" or die "VERSION.txt: $!";
	}

	$product_version = <VERSION>;
	chomp $product_version;
	$product_name = <VERSION>;
	chomp $product_name;
	close VERSION;
	if($product_version eq 'USE_SVN_VERSION')
	{
		# for developers, use SVN version
		my $svnversion = `svnversion .`;
		chomp $svnversion;
		$svnversion =~ tr/0-9A-Za-z/_/c;
		open INFO,'svn info . |';
		my $svnurl;
		while(<INFO>)
		{
			if(m/^URL: (.+?)[\n\r]+/)
			{
				$svnurl = $1
			}
		}
		close INFO;
		$svnurl =~ m!box/(.+)$!;
		my $svndir = $1;
		$svndir =~ tr/0-9A-Za-z/_/c;
		$product_version = $svndir.'_'.$svnversion;
	}

	# where to put the files
	$install_into_dir = '@bindir_expanded@';

	# if it's Darwin,
	if($build_os eq 'Darwin')
	{
		# see how many processors there are, and set make flags accordingly
		my $cpus = `sysctl hw.ncpu`;
		if($cpus =~ m/hw.ncpu:\s(\d+)/ && $1 > 1)
		{
			print "$1 processors detected, will set make to perform concurrent jobs\n";
			$sub_make_options = ' -j '.($1 + 1);
		}
		
		# test for fink installation
		if(-d '/sw/include' && -d '/sw/lib')
		{
			print "Fink installation detected, will use headers and libraries\n\n\n";
			$platform_compile_line_extra = '-I/sw/include ';
			$platform_link_line_extra = '-L/sw/lib ';
		}
	}
}

sub make_flag
{
	if($bsd_make)
	{
		return "-D $_[0]"
	}
	return $_[0].'=1';
}

sub update_if_changed ($)
{
	my ($file) = @_;
	die "$file.new: not found" unless -r "$file.new";

	if (-r $file)
	{
		die "$file.new: not found" unless -r "$file.new";
		if (system("diff --brief $file $file.new") == 0)
		{
			unlink "$file.new";
			return;
		}
	}

	if (system("cp $file.new $file") != 0)
	{
		die "failed to copy $file.new to $file";
	}

	if (system("diff --brief $file $file.new") != 0)
	{
		die "$file and $file.new are still different";
	}

	unlink "$file.new";
	return;
}

1;