HipHop build support files.
[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 = 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 ' --config=' . wfEscapeShellArg( "$sourceDir/compiler.conf" ) .
52 ' --parse-on-demand=false' .
53 ' --program=mediawiki-hphp' .
54 ' --output-dir=' . wfEscapeShellArg( $outDir ) .
55 ' --log=3' );
56
57 # Copy the generated C++ files into the source directory for cmake
58 $iter = new RecursiveIteratorIterator(
59 new RecursiveDirectoryIterator( $outDir ),
60 RecursiveIteratorIterator::SELF_FIRST );
61 $sourceFiles = array();
62 $regenerateMakefile = false;
63 $numFiles = 0;
64 $numFilesChanged = 0;
65 foreach ( $iter as $sourcePath => $file ) {
66 $name = substr( $sourcePath, strlen( $outDir ) + 1 );
67 $sourceFiles[$name] = true;
68 $destPath = "$persistentDir/$name";
69 if ( $file->isDir() ) {
70 if ( !is_dir( $destPath ) ) {
71 mkdir( $destPath );
72 }
73 continue;
74 }
75
76 $numFiles++;
77 # Remove any files that weren't touched, these may have been removed
78 # from file-list, we should not compile them
79 if ( $file->getMTime() < $startTime ) {
80 if ( file_exists( $destPath ) ) {
81 unlink( $destPath );
82 # Files removed, regenerate the makefile
83 $regenerateMakefile = true;
84 }
85 unlink( $sourcePath );
86 $numFilesChanged++;
87 continue;
88 }
89
90 if ( file_exists( $destPath ) ) {
91 $sourceHash = md5( file_get_contents( $sourcePath ) );
92 $destHash = md5( file_get_contents( $destPath ) );
93 if ( $sourceHash == $destHash ) {
94 continue;
95 }
96 } else {
97 # New files added, regenerate the makefile
98 $regenerateMakefile = true;
99 }
100 $numFilesChanged++;
101 copy( $sourcePath, $destPath );
102 }
103
104 echo "MediaWiki: $numFilesChanged files changed out of $numFiles\n";
105
106 if ( !file_exists( "$persistentDir/CMakeLists.txt" ) ) {
107 # Run cmake for the first time
108 $regenerateMakefile = true;
109 }
110
111 # Do our own version of $HPHP_HOME/bin/run.sh, which isn't so broken.
112 # HipHop's RELEASE mode seems to be stuck always on, so symbols get
113 # stripped. Also we will try keeping the generated .o files instead of
114 # throwing away hours of CPU time every time you make a typo.
115
116 chdir( $persistentDir );
117
118 if ( $regenerateMakefile ) {
119 copy( $_ENV['HPHP_HOME'] . '/bin/CMakeLists.base.txt',
120 "$persistentDir/CMakeLists.txt" );
121
122 if ( file_exists( "$persistentDir/CMakeCache.txt" ) ) {
123 unlink( "$persistentDir/CMakeCache.txt" );
124 }
125
126 $cmd = 'cmake' .
127 ' -D CMAKE_BUILD_TYPE:string=Debug' .
128 ' -D PROGRAM_NAME:string=mediawiki-hphp';
129
130 if ( file_exists( '/usr/bin/ccache' ) ) {
131 $cmd .= ' -D CMAKE_CXX_COMPILER:string=ccache' .
132 ' -D CMAKE_CXX_COMPILER_ARG1:string=' . wfEscapeShellArg( $cxx );
133 }
134
135 $cmd .= ' .';
136 echo "$cmd\n";
137 passthru( $cmd );
138 }
139
140 # Run make. This is the slow step.
141 passthru( 'make' );
142
143 $elapsed = time() - $startTime;
144
145 echo "Completed in ";
146 if ( $elapsed >= 3600 ) {
147 $hours = floor( $elapsed / 3600 );
148 echo $hours . 'h ';
149 $elapsed -= $hours * 3600;
150 }
151 if ( $elapsed >= 60 ) {
152 $minutes = floor( $elapsed / 60 );
153 echo $minutes . 'm ';
154 $elapsed -= $minutes * 60;
155 }
156 echo $elapsed . "s\n";
157 echo "The MediaWiki executable is at build/persistent/mediawiki-hphp\n";
158 }
159 }
160
161 $maintClass = 'MakeHipHop';
162 require_once( RUN_MAINTENANCE_IF_MAIN );