Dont allow $procs to be 0
[lhc/web/wiklou.git] / maintenance / hiphop / make
1 #!/usr/bin/hphpi -f
2 <?php
3
4 require( dirname( __FILE__ ) . '/../Maintenance.php' );
5
6 class MakeHipHop extends Maintenance {
7
8 function execute() {
9 $startTime = time();
10
11 $sourceDir = realpath( dirname( __FILE__ ) );
12 $IP = realpath( "$sourceDir/../.." );
13 $buildDir = "$sourceDir/build";
14 $outDir = "$buildDir/hiphop-output";
15 $persistentDir = "$buildDir/persistent" ;
16
17 if ( !is_dir( $buildDir ) ) {
18 mkdir( $buildDir, 0777, true );
19 }
20 if ( !is_dir( $persistentDir ) ) {
21 mkdir( $persistentDir, 0777, true );
22 }
23
24 # With the CentOS RPMs, you just get g++44, no g++, so we have to
25 # use the environment
26 if ( isset( $_ENV['CXX'] ) ) {
27 $cxx = $_ENV['CXX'];
28 } else {
29 $cxx = 'g++';
30 }
31
32 # Create a function that provides the HipHop compiler version, and
33 # doesn't exist when MediaWiki is invoked in interpreter mode.
34 $version = str_replace( PHP_EOL, ' ', trim( `hphp --version` ) );
35 file_put_contents(
36 "$buildDir/HipHopCompilerVersion.php",
37 "<" . "?php\n" .
38 "function wfHipHopCompilerVersion() {\n" .
39 "return " . var_export( $version, true ) . ";\n" .
40 "}\n"
41 );
42
43 # Generate the C++
44 passthru(
45 'hphp' .
46 ' --target=cpp' .
47 ' --format=file' .
48 ' --input-dir=' . wfEscapeShellArg( $IP ) .
49 ' --input-list=' . wfEscapeShellArg( "$sourceDir/file-list.small" ) .
50 ' --inputs=' . wfEscapeShellArg( "$buildDir/HipHopCompilerVersion.php" ) .
51 ' -c ' . wfEscapeShellArg( "$sourceDir/compiler.conf" ) .
52 ' --parse-on-demand=false' .
53 ' --program=mediawiki-hphp' .
54 ' --output-dir=' . wfEscapeShellArg( $outDir ) .
55 ' --log=3' );
56
57 # Sanity check, quickly make sure we've got an output directory
58 if( !is_dir( $outDir ) ) {
59 $this->error( "No output directory", true );
60 }
61
62 # Copy the generated C++ files into the source directory for cmake
63 $iter = new RecursiveIteratorIterator(
64 new RecursiveDirectoryIterator( $outDir ),
65 RecursiveIteratorIterator::SELF_FIRST );
66 $sourceFiles = array();
67 $regenerateMakefile = false;
68 $numFiles = 0;
69 $numFilesChanged = 0;
70 foreach ( $iter as $sourcePath => $file ) {
71 $name = substr( $sourcePath, strlen( $outDir ) + 1 );
72 $sourceFiles[$name] = true;
73 $destPath = "$persistentDir/$name";
74 if ( $file->isDir() ) {
75 if ( !is_dir( $destPath ) ) {
76 mkdir( $destPath );
77 }
78 continue;
79 }
80
81 $numFiles++;
82 # Remove any files that weren't touched, these may have been removed
83 # from file-list, we should not compile them
84 if ( $file->getMTime() < $startTime ) {
85 if ( file_exists( $destPath ) ) {
86 unlink( $destPath );
87 # Files removed, regenerate the makefile
88 $regenerateMakefile = true;
89 }
90 unlink( $sourcePath );
91 $numFilesChanged++;
92 continue;
93 }
94
95 if ( file_exists( $destPath ) ) {
96 $sourceHash = md5( file_get_contents( $sourcePath ) );
97 $destHash = md5( file_get_contents( $destPath ) );
98 if ( $sourceHash == $destHash ) {
99 continue;
100 }
101 } else {
102 # New files added, regenerate the makefile
103 $regenerateMakefile = true;
104 }
105 $numFilesChanged++;
106 copy( $sourcePath, $destPath );
107 }
108
109 echo "MediaWiki: $numFilesChanged files changed out of $numFiles\n";
110
111 if ( !file_exists( "$persistentDir/CMakeLists.txt" ) ) {
112 # Run cmake for the first time
113 $regenerateMakefile = true;
114 }
115
116 # Do our own version of $HPHP_HOME/bin/run.sh, which isn't so broken.
117 # HipHop's RELEASE mode seems to be stuck always on, so symbols get
118 # stripped. Also we will try keeping the generated .o files instead of
119 # throwing away hours of CPU time every time you make a typo.
120
121 chdir( $persistentDir );
122
123 if ( $regenerateMakefile ) {
124 copy( $_ENV['HPHP_HOME'] . '/bin/CMakeLists.base.txt',
125 "$persistentDir/CMakeLists.txt" );
126
127 if ( file_exists( "$persistentDir/CMakeCache.txt" ) ) {
128 unlink( "$persistentDir/CMakeCache.txt" );
129 }
130
131 $cmd = 'cmake' .
132 ' -D CMAKE_BUILD_TYPE:string=Debug' .
133 ' -D PROGRAM_NAME:string=mediawiki-hphp';
134
135 if ( file_exists( '/usr/bin/ccache' ) ) {
136 $cmd .= ' -D CMAKE_CXX_COMPILER:string=ccache' .
137 ' -D CMAKE_CXX_COMPILER_ARG1:string=' . wfEscapeShellArg( $cxx );
138 }
139
140 $cmd .= ' .';
141 echo "$cmd\n";
142 passthru( $cmd );
143 }
144
145 # Determine appropriate make concurrency
146 # Compilation can take a lot of memory, let's assume that that is limiting.
147 $mem = false;
148 foreach ( file( '/proc/meminfo' ) as $line ) {
149 if ( preg_match( '/^MemTotal:\s+(\d+)\s+kB/', $line, $m ) ) {
150 $mem = intval( $m[1] );
151 break;
152 }
153 }
154 if ( $mem ) {
155 $procs = floor( $mem / 1000000 );
156 $procs = $procs >= 1 ? $procs : 1; // No less than 1
157 } else {
158 $procs = 1;
159 }
160
161 # Run make. This is the slow step.
162 passthru( 'make -j' . wfEscapeShellArg( $procs ) );
163
164 $elapsed = time() - $startTime;
165
166 echo "Completed in ";
167 if ( $elapsed >= 3600 ) {
168 $hours = floor( $elapsed / 3600 );
169 echo $hours . 'h ';
170 $elapsed -= $hours * 3600;
171 }
172 if ( $elapsed >= 60 ) {
173 $minutes = floor( $elapsed / 60 );
174 echo $minutes . 'm ';
175 $elapsed -= $minutes * 60;
176 }
177 echo $elapsed . "s\n";
178 echo "The MediaWiki executable is at build/persistent/mediawiki-hphp\n";
179 }
180 }
181
182 $maintClass = 'MakeHipHop';
183 require_once( RUN_MAINTENANCE_IF_MAIN );