HipHop build fixes:
[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 file list from the autoloader
44 global $wgAutoloadLocalClasses;
45 $files = array_merge(
46 array_values( $wgAutoloadLocalClasses ),
47 array_map( 'trim', file( "$sourceDir/extra-files" ) )
48 );
49 $files = array_unique( $files );
50 file_put_contents(
51 "$buildDir/file-list",
52 implode( "\n", $files ) . "\n" );
53
54 # Generate the C++
55 passthru(
56 'hphp' .
57 ' --target=cpp' .
58 ' --format=file' .
59 ' --input-dir=' . wfEscapeShellArg( $IP ) .
60 ' --input-list=' . wfEscapeShellArg( "$buildDir/file-list" ) .
61 ' --inputs=' . wfEscapeShellArg( "$buildDir/HipHopCompilerVersion.php" ) .
62 ' -c ' . wfEscapeShellArg( "$sourceDir/compiler.conf" ) .
63 ' --parse-on-demand=false' .
64 ' --program=mediawiki-hphp' .
65 ' --output-dir=' . wfEscapeShellArg( $outDir ) .
66 ' --log=3' );
67
68 # Sanity check, quickly make sure we've got an output directory
69 if( !is_dir( $outDir ) ) {
70 $this->error( "No output directory", true );
71 }
72
73 # Copy the generated C++ files into the source directory for cmake
74 $iter = new RecursiveIteratorIterator(
75 new RecursiveDirectoryIterator( $outDir ),
76 RecursiveIteratorIterator::SELF_FIRST );
77 $sourceFiles = array();
78 $regenerateMakefile = false;
79 $numFiles = 0;
80 $numFilesChanged = 0;
81 foreach ( $iter as $sourcePath => $file ) {
82 $name = substr( $sourcePath, strlen( $outDir ) + 1 );
83 $sourceFiles[$name] = true;
84 $destPath = "$persistentDir/$name";
85 if ( $file->isDir() ) {
86 if ( !is_dir( $destPath ) ) {
87 mkdir( $destPath );
88 }
89 continue;
90 }
91
92 $numFiles++;
93 # Remove any files that weren't touched, these may have been removed
94 # from file-list, we should not compile them
95 if ( $file->getMTime() < $startTime ) {
96 if ( file_exists( $destPath ) ) {
97 unlink( $destPath );
98 # Files removed, regenerate the makefile
99 $regenerateMakefile = true;
100 }
101 unlink( $sourcePath );
102 $numFilesChanged++;
103 continue;
104 }
105
106 if ( file_exists( $destPath ) ) {
107 $sourceHash = md5( file_get_contents( $sourcePath ) );
108 $destHash = md5( file_get_contents( $destPath ) );
109 if ( $sourceHash == $destHash ) {
110 continue;
111 }
112 } else {
113 # New files added, regenerate the makefile
114 $regenerateMakefile = true;
115 }
116 $numFilesChanged++;
117 copy( $sourcePath, $destPath );
118 }
119
120 echo "MediaWiki: $numFilesChanged files changed out of $numFiles\n";
121
122 if ( !file_exists( "$persistentDir/CMakeLists.txt" ) ) {
123 # Run cmake for the first time
124 $regenerateMakefile = true;
125 }
126
127 # Do our own version of $HPHP_HOME/bin/run.sh, which isn't so broken.
128 # HipHop's RELEASE mode seems to be stuck always on, so symbols get
129 # stripped. Also we will try keeping the generated .o files instead of
130 # throwing away hours of CPU time every time you make a typo.
131
132 chdir( $persistentDir );
133
134 if ( $regenerateMakefile ) {
135 copy( $_ENV['HPHP_HOME'] . '/bin/CMakeLists.base.txt',
136 "$persistentDir/CMakeLists.txt" );
137
138 if ( file_exists( "$persistentDir/CMakeCache.txt" ) ) {
139 unlink( "$persistentDir/CMakeCache.txt" );
140 }
141
142 $cmd = 'cmake' .
143 ' -D CMAKE_BUILD_TYPE:string=Debug' .
144 ' -D PROGRAM_NAME:string=mediawiki-hphp';
145
146 if ( file_exists( '/usr/bin/ccache' ) ) {
147 $cmd .= ' -D CMAKE_CXX_COMPILER:string=ccache' .
148 ' -D CMAKE_CXX_COMPILER_ARG1:string=' . wfEscapeShellArg( $cxx );
149 }
150
151 $cmd .= ' .';
152 echo "$cmd\n";
153 passthru( $cmd );
154 }
155
156 # Determine appropriate make concurrency
157 # Compilation can take a lot of memory, let's assume that that is limiting.
158 $mem = false;
159 foreach ( file( '/proc/meminfo' ) as $line ) {
160 if ( preg_match( '/^MemTotal:\s+(\d+)\s+kB/', $line, $m ) ) {
161 $mem = intval( $m[1] );
162 break;
163 }
164 }
165 if ( $mem ) {
166 $procs = floor( $mem / 1000000 );
167 $procs = $procs >= 1 ? $procs : 1; // No less than 1
168 } else {
169 $procs = 1;
170 }
171
172 # Run make. This is the slow step.
173 passthru( 'make -j' . wfEscapeShellArg( $procs ) );
174
175 $elapsed = time() - $startTime;
176
177 echo "Completed in ";
178 if ( $elapsed >= 3600 ) {
179 $hours = floor( $elapsed / 3600 );
180 echo $hours . 'h ';
181 $elapsed -= $hours * 3600;
182 }
183 if ( $elapsed >= 60 ) {
184 $minutes = floor( $elapsed / 60 );
185 echo $minutes . 'm ';
186 $elapsed -= $minutes * 60;
187 }
188 echo $elapsed . "s\n";
189 echo "The MediaWiki executable is at build/persistent/mediawiki-hphp\n";
190 }
191 }
192
193 $maintClass = 'MakeHipHop';
194 require_once( RUN_MAINTENANCE_IF_MAIN );