4 require( dirname( __FILE__ ) . '/../Maintenance.php' );
6 class MakeHipHop extends Maintenance {
11 $sourceDir = realpath( dirname( __FILE__ ) );
12 $IP = realpath( "$sourceDir/../.." );
13 $buildDir = "$sourceDir/build";
14 $outDir = "$buildDir/hiphop-output";
15 $persistentDir = "$buildDir/persistent" ;
17 if ( !is_dir( $buildDir ) ) {
18 mkdir( $buildDir, 0777, true );
20 if ( !is_dir( $persistentDir ) ) {
21 mkdir( $persistentDir, 0777, true );
24 # With the CentOS RPMs, you just get g++44, no g++, so we have to
26 if ( isset( $_ENV['CXX'] ) ) {
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` ) );
36 "$buildDir/HipHopCompilerVersion.php",
38 "function wfHipHopCompilerVersion() {\n" .
39 "return " . var_export( $version, true ) . ";\n" .
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 ) .
57 # Sanity check, quickly make sure we've got an output directory
58 if( !is_dir( $outDir ) ) {
59 $this->error( "No output directory", true );
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;
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 ) ) {
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 ) ) {
87 # Files removed, regenerate the makefile
88 $regenerateMakefile = true;
90 unlink( $sourcePath );
95 if ( file_exists( $destPath ) ) {
96 $sourceHash = md5( file_get_contents( $sourcePath ) );
97 $destHash = md5( file_get_contents( $destPath ) );
98 if ( $sourceHash == $destHash ) {
102 # New files added, regenerate the makefile
103 $regenerateMakefile = true;
106 copy( $sourcePath, $destPath );
109 echo "MediaWiki: $numFilesChanged files changed out of $numFiles\n";
111 if ( !file_exists( "$persistentDir/CMakeLists.txt" ) ) {
112 # Run cmake for the first time
113 $regenerateMakefile = true;
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.
121 chdir( $persistentDir );
123 if ( $regenerateMakefile ) {
124 copy( $_ENV['HPHP_HOME'] . '/bin/CMakeLists.base.txt',
125 "$persistentDir/CMakeLists.txt" );
127 if ( file_exists( "$persistentDir/CMakeCache.txt" ) ) {
128 unlink( "$persistentDir/CMakeCache.txt" );
132 ' -D CMAKE_BUILD_TYPE:string=Debug' .
133 ' -D PROGRAM_NAME:string=mediawiki-hphp';
135 if ( file_exists( '/usr/bin/ccache' ) ) {
136 $cmd .= ' -D CMAKE_CXX_COMPILER:string=ccache' .
137 ' -D CMAKE_CXX_COMPILER_ARG1:string=' . wfEscapeShellArg( $cxx );
145 # Determine appropriate make concurrency
146 # Compilation can take a lot of memory, let's assume that that is limiting.
148 foreach ( file( '/proc/meminfo' ) as $line ) {
149 if ( preg_match( '/^MemTotal:\s+(\d+)\s+kB/', $line, $m ) ) {
150 $mem = intval( $m[1] );
155 $procs = floor( $mem / 1000000 );
156 $procs = $procs >= 1 ? $procs : 1; // No less than 1
161 # Run make. This is the slow step.
162 passthru( 'make -j' . wfEscapeShellArg( $procs ) );
164 $elapsed = time() - $startTime;
166 echo "Completed in ";
167 if ( $elapsed >= 3600 ) {
168 $hours = floor( $elapsed / 3600 );
170 $elapsed -= $hours * 3600;
172 if ( $elapsed >= 60 ) {
173 $minutes = floor( $elapsed / 60 );
174 echo $minutes . 'm ';
175 $elapsed -= $minutes * 60;
177 echo $elapsed . "s\n";
178 echo "The MediaWiki executable is at build/persistent/mediawiki-hphp\n";
182 $maintClass = 'MakeHipHop';
183 require_once( RUN_MAINTENANCE_IF_MAIN );