Project.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. //-----------------------------------------------------------------------------
  3. // Copyright (c) 2012 GarageGames, LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. ///
  24. /// Project info
  25. ///
  26. class Project
  27. {
  28. public static $TYPE_APP = 'app';
  29. public static $TYPE_SHARED_APP = 'sharedapp';
  30. public static $TYPE_LIB = 'lib';
  31. public static $TYPE_SHARED_LIB = 'shared';
  32. public static $TYPE_ACTIVEX = 'activex';
  33. public static $TYPE_SAFARI = 'safari';
  34. public static $TYPE_CSPROJECT = 'csproj';
  35. public $name; // Project name
  36. public $guid; // Project GUID
  37. public $type; // Application or Library?
  38. public $dir_list; // What directories are we checking in?
  39. public $outputs; // List of outputs we want to generate.
  40. public $game_dir; // Base product path
  41. public $defines; // Preprocessor directives
  42. public $disabledWarnings; // Additional warnings to disable
  43. public $includes; // Additional include paths
  44. public $libs; // Additional libraries to link against
  45. public $libsDebug; // Additional Debug build libraries to link against
  46. public $libsIgnore; // Ignore Specific Default Libraries
  47. public $lib_dirs; // Additional library search paths
  48. public $lib_includes; // libs to include (generated by modules)
  49. public $additionalExePath; // Additional section to inject into executable path
  50. public $dependencies; // Projects this project depends on
  51. public $references; // for managed projects, references to required assemblies
  52. public $moduleDefinitionFile; // definition file to control shared library exports on windows
  53. public $projectFileExt;
  54. public $commandDebug = "";
  55. public $commandOptimized = "";
  56. public $commandRelease = "";
  57. public $argsDebug = "";
  58. public $argsOptimized = "";
  59. public $argsRelease = "";
  60. public $projSubSystem = 2; // support for Windows/Console/Assembly linker subsystem (1 - Console, 2 - Windows, 3 - Assembly)
  61. private static $xUID = 1; // used for unique file IDs for Xcode projects
  62. public $uniformOutputFile = 0; // debug/release builds use same filename (necessary for np plugin)
  63. // $additionalExePath, $lib_dirs, $libs, all appear to be unused. [pauls 11/9/2007]
  64. public function Project( $name, $type, $guid = '', $game_dir = 'game', $output_name = '' )
  65. {
  66. if (strlen($output_name) == 0)
  67. $output_name = $name;
  68. $this->name = $name;
  69. $this->outputName = $output_name;
  70. $this->guid = $guid;
  71. $this->type = $type;
  72. $this->game_dir = $game_dir;
  73. $this->dir_list = array();
  74. $this->defines = array();
  75. $this->includes = array();
  76. $this->libs = array();
  77. $this->libsDebug = array();
  78. $this->libsIgnore = array();
  79. $this->lib_dirs = array();
  80. $this->lib_includes = array();
  81. $this->outputs = array();
  82. $this->dependencies = array();
  83. $this->disabledWarnings = array();
  84. $this->references = array();
  85. }
  86. public function isApp()
  87. {
  88. return $this->type == self::$TYPE_APP;
  89. }
  90. public function isSharedApp()
  91. {
  92. return $this->type == self::$TYPE_SHARED_APP;
  93. }
  94. public function isLib()
  95. {
  96. return $this->type == self::$TYPE_LIB;
  97. }
  98. public function isSharedLib()
  99. {
  100. return $this->type == self::$TYPE_SHARED_LIB;
  101. }
  102. public function isCSProject()
  103. {
  104. return $this->type == self::$TYPE_CSPROJECT;
  105. }
  106. public function isActiveX()
  107. {
  108. return $this->type == self::$TYPE_ACTIVEX;
  109. }
  110. public function isSafari()
  111. {
  112. return $this->type == self::$TYPE_SAFARI;
  113. }
  114. public function setUniformOutputFile()
  115. {
  116. return $this->uniformOutputFile = 1;
  117. }
  118. public function setSubSystem( $subSystem )
  119. {
  120. $this->projSubSystem = $subSystem;
  121. }
  122. public function validate()
  123. {
  124. // Sort the path list
  125. sort( $this->dir_list );
  126. // Make sure we don't have any duplicate paths
  127. $this->dir_list = array_unique( $this->dir_list );
  128. }
  129. public function addReference($refName, $version = "")
  130. {
  131. $this->references[$refName] = $version;
  132. }
  133. public function addIncludes( $includes )
  134. {
  135. $this->includes = array_merge( $includes, $this->includes );
  136. }
  137. public function validateDependencies()
  138. {
  139. $pguids = array();
  140. foreach( $this->dependencies as $pname )
  141. {
  142. $p = Generator::lookupProjectByName( $pname );
  143. if( $p )
  144. array_push( $pguids, $p->guid );
  145. else
  146. trigger_error( "Project dependency not found: " .$pname, E_USER_ERROR );
  147. }
  148. // todo: change to dependencyGuids
  149. $this->dependencies = $pguids;
  150. }
  151. private function generateXUID()
  152. {
  153. return sprintf( "%023X", Project::$xUID++ );
  154. }
  155. private function createFileEntry( $output, $curPath, $curFile )
  156. {
  157. // See if we need to reject it based on our rules..
  158. if( $output->ruleReject( $curFile ) )
  159. return null;
  160. // Get the extension - is it one of our allowed values?
  161. if( !$output->allowedFileExt( $curFile ) )
  162. return null;
  163. // Cool - note in the list!
  164. $newEntry = new stdClass();
  165. $newEntry->name = $curFile;
  166. $newEntry->path = FileUtil::collapsePath( $curPath . "/" . $curFile );
  167. if ( !FileUtil::isAbsolutePath( $newEntry->path ) )
  168. {
  169. // This could be consolidated into a single OR statement but it is easier to
  170. // read as two separate if's
  171. if ( !Generator::$absPath )
  172. $newEntry->path = $output->project_rel_path . $newEntry->path;
  173. if ( Generator::$absPath && !stristr($newEntry->path, Generator::$absPath) )
  174. $newEntry->path = $output->project_rel_path . $newEntry->path;
  175. }
  176. // Store a project-unique ID here for Xcode projects
  177. // It will be appended by a single char in the templates.
  178. $newEntry->hash = Project::generateXUID();
  179. return $newEntry;
  180. }
  181. function generateFileList( &$projectFiles, $outputName, &$output )
  182. {
  183. $projName = $this->name;
  184. $projectFiles[ $projName ] = array();
  185. foreach( $this->dir_list as $dir )
  186. {
  187. $dir = FileUtil::normalizeSlashes( $dir );
  188. // Build the path.
  189. if ( FileUtil::isAbsolutePath( $dir ) )
  190. $curPath = $dir;
  191. else
  192. $curPath = FileUtil::collapsePath( $output->base_dir . $dir );
  193. $pathWalk = &$projectFiles[ $projName ];
  194. if ( Generator::$absPath )
  195. {
  196. if ( stristr($curPath, getEngineSrcDir()) || stristr($curPath, getLibSrcDir()) )
  197. $curPath = Generator::$absPath . "/". str_replace("../", "", $curPath);
  198. }
  199. // Check if its a file or a directory.
  200. // If its a file just add it directly and build a containng filter/folder structure,
  201. // for it else if a dir add all files in it.
  202. if( is_file( $curPath ) )
  203. {
  204. // Get the file name
  205. $curFile = basename( $curPath );
  206. $curPath = dirname( $curPath );
  207. //echo( "FILE: " . $curFile . " PATH: " . $curPath . "\n" );
  208. }
  209. if( is_dir( $curPath ) )
  210. {
  211. //echo( "DIR: " . $curPath . "\n" );
  212. // Get the array we'll be adding things to...
  213. $pathParts = explode( '/', FileUtil::collapsePath( $dir ) );
  214. foreach( $pathParts as $part )
  215. {
  216. // Skip parts that are relative paths - only want meaningful directories.
  217. if( $part == '..' )
  218. continue;
  219. if( !is_array( $pathWalk[ $part ] ) )
  220. $pathWalk[ $part ] = array();
  221. $pathWalk = &$pathWalk[ $part ];
  222. }
  223. // Open directory.
  224. //echo( "SCANNING: " . $curPath . "\n");
  225. $dirHdl = opendir( $curPath );
  226. if( !$dirHdl )
  227. {
  228. echo( "Path " . $curPath . " not found, giving up.\n" );
  229. return false;
  230. }
  231. // Iterate over all the files in the path if not a single file spec.
  232. if( !$curFile )
  233. {
  234. while( $curFile = readdir( $dirHdl ) )
  235. {
  236. // Skip out if it's an uninteresting dir...
  237. if( $curFile == '.' || $curFile == '..' || $curFile == '.svn' || $curFile == 'CVS' )
  238. continue;
  239. $newEntry = $this->createFileEntry( $output, $curPath, $curFile );
  240. if( $newEntry )
  241. $pathWalk[] = $newEntry;
  242. }
  243. }
  244. else
  245. {
  246. $newEntry = $this->createFileEntry( $output, $curPath, $curFile );
  247. if( $newEntry )
  248. $pathWalk = $newEntry;
  249. $curFile = '';
  250. }
  251. // Clean up after ourselves!
  252. closedir( $dirHdl );
  253. }
  254. }
  255. FileUtil::trimFileList( $projectFiles );
  256. // Uncomment me to see the structure the file lister is returning.
  257. //print_r($projectFiles);
  258. return true;
  259. }
  260. private function setTemplateParams( $tpl, $output, &$projectFiles )
  261. {
  262. // Set the template delimiters
  263. $tpl->left_delimiter = $output->ldelim ? $output->ldelim : '{';
  264. $tpl->right_delimiter = $output->rdelim ? $output->rdelim : '}';
  265. $gameProjectName = getGameProjectName();
  266. // Evaluate template into a file.
  267. $tpl->assign_by_ref( 'projSettings', $this );
  268. $tpl->assign_by_ref( 'projOutput', $output );
  269. $tpl->assign_by_ref( 'fileArray', $projectFiles );
  270. $tpl->assign_by_ref( 'projName', $this->name );
  271. $tpl->assign_by_ref( 'projOutName', $this->outputName );
  272. $tpl->assign_by_ref( 'gameFolder', $this->game_dir );
  273. $tpl->assign_by_ref( 'GUID', $this->guid );
  274. $tpl->assign_by_ref( 'projDefines', $this->defines );
  275. $tpl->assign_by_ref( 'projDisabledWarnings', $this->disabledWarnings );
  276. $tpl->assign_by_ref( 'projIncludes', $this->includes );
  277. $tpl->assign_by_ref( 'projLibs', $this->libs );
  278. $tpl->assign_by_ref( 'projLibsDebug',$this->libsDebug);
  279. $tpl->assign_by_ref( 'projLibsIgnore',$this->libsIgnore);
  280. $tpl->assign_by_ref( 'projLibDirs', $this->lib_dirs );
  281. $tpl->assign_by_ref( 'projDepend', $this->dependencies );
  282. $tpl->assign_by_ref( 'gameProjectName', $gameProjectName );
  283. $tpl->assign_by_ref( 'projModuleDefinitionFile', $this->moduleDefinitionFile );
  284. $tpl->assign_by_ref( 'projSubSystem', $this->projSubSystem );
  285. if (Generator::$useDLLRuntime)
  286. {
  287. // /MD and /MDd
  288. $tpl->assign( 'projRuntimeRelease', 2 );
  289. $tpl->assign( 'projRuntimeDebug', 3 );
  290. }
  291. else
  292. {
  293. // /MT and /MTd
  294. $tpl->assign( 'projRuntimeRelease', 0 );
  295. $tpl->assign( 'projRuntimeDebug', 1 );
  296. }
  297. if (!$this->commandDebug && ( $this->isSharedLib() || $this->isSharedApp() ))
  298. {
  299. $command = "$(TargetDir)\\".$this->outputName;
  300. $tpl->assign( 'commandDebug' , $command."_DEBUG.exe");
  301. $tpl->assign( 'commandRelease' , $command.".exe");
  302. $tpl->assign( 'commandOptimized' , $command."_OPTIMIZEDDEBUG.exe");
  303. }
  304. else
  305. {
  306. $tpl->assign_by_ref( 'commandDebug' , $this->commandDebug);
  307. $tpl->assign_by_ref( 'commandRelease' , $this->commandRelease);
  308. $tpl->assign_by_ref( 'commandOptimized' , $this->commandOptimized);
  309. }
  310. $tpl->assign_by_ref( 'argsDebug' , $this->argsDebug);
  311. $tpl->assign_by_ref( 'argsRelease' , $this->argsRelease);
  312. $tpl->assign_by_ref( 'argsOptimized' , $this->argsOptimized);
  313. $ptypes = array();
  314. $projectDepends = array();
  315. foreach ($this->dependencies as $pname)
  316. {
  317. $p = Generator::lookupProjectByName( $pname );
  318. $projectDepends[$pname] = $p;
  319. if ( $p )
  320. $ptypes[$pname] = $p->isSharedLib() || $p->isSafari();
  321. }
  322. $tpl->assign_by_ref( 'projTypes', $ptypes );
  323. $tpl->assign_by_ref( 'projectDepends', $projectDepends );
  324. // Assign some handy paths for the template to reference
  325. $tpl->assign( 'projectOffset', $output->project_rel_path );
  326. if ( Generator::$absPath )
  327. $tpl->assign( 'srcDir', Generator::$absPath . "/". str_replace("../", "", getAppEngineSrcDir()) );
  328. else
  329. $tpl->assign( 'srcDir', $output->project_rel_path . getAppEngineSrcDir() );
  330. if ( Generator::$absPath )
  331. $tpl->assign( 'libDir', Generator::$absPath . "/". str_replace("../", "", getAppLibSrcDir()) );
  332. else
  333. $tpl->assign( 'libDir', $output->project_rel_path . getAppLibSrcDir() );
  334. if ( Generator::$absPath )
  335. $tpl->assign( 'binDir', Generator::$absPath . "/". str_replace("../", "", getAppEngineBinDir()) );
  336. else
  337. $tpl->assign( 'binDir', $output->project_rel_path . getAppEngineBinDir() );
  338. $tpl->assign( 'uniformOutputFile', $this->uniformOutputFile);
  339. }
  340. private function conditionDirectories( $output, &$projectFiles )
  341. {
  342. foreach ($this->includes as &$include)
  343. {
  344. if ( !FileUtil::isAbsolutePath( $include ) )
  345. $include = $output->project_rel_path . $include;
  346. }
  347. foreach ($this->lib_dirs as &$libDirs)
  348. {
  349. if ( !FileUtil::isAbsolutePath( $libDirs ) )
  350. $libDirs = $output->project_rel_path . $libDirs;
  351. }
  352. if ( Generator::$absPath )
  353. {
  354. foreach ($this->includes as &$include)
  355. {
  356. if ( stristr($include, getEngineSrcDir()) || stristr($include, getLibSrcDir()) )
  357. $include = Generator::$absPath . "/". str_replace("../", "", $include);
  358. }
  359. foreach ($this->lib_dirs as &$libDirs)
  360. {
  361. if ( stristr($libDirs, getEngineSrcDir()) || stristr($libDirs, getLibSrcDir()) )
  362. $libDirs = Generator::$absPath . "/". str_replace("../", "", $libDirs);
  363. }
  364. }
  365. }
  366. public function generate( $tpl, $platform, $base_dir )
  367. {
  368. // Alright, for each project scan and generate the file list.
  369. $projectFiles = array ();
  370. $rootPhpBuildDir = getcwd();
  371. // Iterate over this project's outputs.
  372. foreach( $this->outputs as $outputName => $output )
  373. {
  374. $saved_includes = $this->includes;
  375. $saved_lib_dirs = $this->lib_dirs;
  376. //print_r( $output );
  377. // Supported platform?
  378. if( !$output->supportsPlatform( $platform ) )
  379. {
  380. //echo( " # Skipping output: '$outputName'.\n" );
  381. continue;
  382. }
  383. // Get to the right working directory (first go back to root, then to relative)
  384. chdir( $base_dir );
  385. //echo( " - Changing CWD to " . $output->output_dir . "\n" );
  386. // echo(" (From: " . getcwd() . ")\n");
  387. if( !FileUtil::prepareOutputDir( $output->output_dir ) )
  388. continue;
  389. //echo( " - Scanning directory for output '.$outputName.'...\n" );
  390. if( !$this->generateFileList( $projectFiles, $outputName, $output ) )
  391. {
  392. echo( "File list generation failed. Giving up on this project.\n" );
  393. continue;
  394. }
  395. // Do any special work on the include/lib directories that we need
  396. $this->conditionDirectories( $output, $projectFiles[ $this->name ] );
  397. $this->projectFileExt = $output->output_ext;
  398. if ( $this->isCSProject() )
  399. $this->projectFileExt = ".csproj"; // always csproj C# project under VS/MonoDevelop
  400. $outfile = $output->project_dir . $this->name . $this->projectFileExt;
  401. echo( " o Writing project file " . $outfile . "\n" );
  402. $this->setTemplateParams( $tpl, $output, $projectFiles[ $this->name ] );
  403. // To put a bandaid on the tools/player output dir problem
  404. // CodeReview: This should be in the template. -- BJG, 3/13/2007
  405. // Moved into templates -- neo
  406. // Write file
  407. $outdir = dirname( $outfile );
  408. if( !file_exists( $outdir ) )
  409. mkdir_r( $outdir, 0777 );
  410. if( $hdl = fopen( $outfile, 'w' ) )
  411. {
  412. if ($this->isApp())
  413. $template = $output->template_app;
  414. else if ($this->isLib())
  415. $template = $output->template_lib;
  416. else if ($this->isSharedLib())
  417. $template = $output->template_shared_lib;
  418. else if ($this->isSharedApp())
  419. $template = $output->template_shared_app;
  420. else if ($this->isActiveX())
  421. $template = $output->template_activex;
  422. else if ($this->isSafari())
  423. $template = $output->template_activex; //rename template?
  424. else if ($this->isCSProject())
  425. $template = $output->template_csproj;
  426. fputs( $hdl, $tpl->fetch( $template ) );
  427. fclose( $hdl );
  428. }
  429. else
  430. trigger_error( "Could not write output file: " . $output->outputFile, E_USER_ERROR );
  431. if ($output->template_user)
  432. {
  433. $outfile = $output->project_dir . $this->name . $this->projectFileExt .'.'.getenv("COMPUTERNAME").'.'.getenv("USERNAME").'.user';
  434. if( !file_exists( $outfile ) )
  435. {
  436. if( $hdl = fopen( $outfile, 'w' ) )
  437. {
  438. $template = $output->template_user;
  439. fputs( $hdl, $tpl->fetch( $template ) );
  440. fclose( $hdl );
  441. }
  442. else
  443. trigger_error( "Could not write output file: " . $outfile, E_USER_ERROR );
  444. }
  445. }
  446. // Build the .filters file used by VS2010.
  447. if ( $output->template_filter )
  448. {
  449. $filterData = new FilterData();
  450. array_walk( $projectFiles[ $this->name ], array($filterData, 'callback'), '' );
  451. $tpl->assign_by_ref('Folders', $filterData->folders);
  452. $tpl->assign_by_ref('SrcFiles', $filterData->srcFiles);
  453. $tpl->assign_by_ref('IncFiles', $filterData->incFiles);
  454. $tpl->assign_by_ref('OtherFiles', $filterData->otherFiles);
  455. $tpl->register_function( 'gen_uuid', 'gen_uuid' );
  456. $outfile = $output->project_dir . $this->name . $this->projectFileExt . '.filters';
  457. if ( $hdl = fopen( $outfile, 'w' ) )
  458. {
  459. fputs( $hdl, $tpl->fetch( $output->template_filter ) );
  460. fclose( $hdl );
  461. }
  462. }
  463. $this->includes = $saved_includes;
  464. $this->lib_dirs = $saved_lib_dirs;
  465. }
  466. }
  467. }
  468. class FilterData
  469. {
  470. public $folders = array();
  471. public $srcFiles = array();
  472. public $incFiles = array();
  473. public $otherFiles = array();
  474. public function callback( $value, $key, $dir )
  475. {
  476. if ( is_array( $value ) )
  477. {
  478. if ( $dir != '' )
  479. $dirpath = $dir . '\\' . $key;
  480. else
  481. $dirpath = $key;
  482. array_push( $this->folders, $dirpath );
  483. array_walk( $value, array($this, 'callback'), $dirpath );
  484. return;
  485. }
  486. $path = str_replace( '/', '\\', $value->path );
  487. $ext = strrchr( $path, '.' );
  488. if ( $ext == FALSE )
  489. return;
  490. if ( strcasecmp( $ext, '.c' ) == 0 ||
  491. strcasecmp( $ext, '.cpp' ) == 0 ||
  492. strcasecmp( $ext, '.cc' ) == 0 )
  493. $this->srcFiles[$path] = $dir;
  494. else if ( strcasecmp( $ext, '.h' ) == 0 ||
  495. strcasecmp( $ext, '.hpp' ) == 0 ||
  496. strcasecmp( $ext, '.inl' ) == 0 )
  497. $this->incFiles[$path] = $dir;
  498. else
  499. $this->otherFiles[$path] = $dir;
  500. }
  501. } // class FilterData
  502. ?>