Generator.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. ///
  25. /// Static Generator class
  26. ///
  27. require_once( "FileUtil.php" );
  28. require_once( "Solution.php" );
  29. require_once( "Project.php" );
  30. require_once( "BuildTarget.php" );
  31. require_once( "Torque3D.php");
  32. require_once( "WindowsRegistry.php");
  33. require_once( "WebPlugin.php");
  34. require_once( "ActiveXWebPlugin.php");
  35. require_once( "NPWebPlugin.php");
  36. require_once( "SafariWebPlugin.php");
  37. class T3D_Generator
  38. {
  39. public static $app_name;
  40. public static $paths = array();
  41. public static $prefs = array();
  42. public static $config_projects = array();
  43. public static $app_lib_includes = array(); // An accumulative list of includes that are needed by libs and subsequently needed by the application
  44. public static $platform = 'win32';
  45. public static $solutions = array();
  46. public static $libGuard = array(); // protect against libraries included multiple times across modules
  47. public static $absPath = NULL;
  48. public static $gameProjectName = NULL;
  49. public static $toolBuild = true;
  50. public static $watermarkBuild = false;
  51. public static $purchaseScnBuild = false;
  52. public static $demoBuild = false;
  53. public static $objectLimitBuild = false;
  54. public static $timeOutBuild = false;
  55. public static $useDLLRuntime = false;
  56. private static $solution_cur;
  57. private static $project_cur;
  58. private static $module_cur;
  59. static function init( $torqueRoot )
  60. {
  61. // If the torque root is absolute then store and use it.
  62. if ( realpath( $torqueRoot ) == $torqueRoot ||
  63. realpath( $torqueRoot ) == str_replace("/", "\\", $torqueRoot) )
  64. {
  65. self::$absPath = str_replace( "\\", "/", $torqueRoot );
  66. $torqueRoot = self::$absPath;
  67. }
  68. if ( self::$absPath )
  69. {
  70. self::$paths[ 'engineLib' ] = '../../../Engine/lib/';
  71. self::$paths[ 'engineSrc' ] = '../../../Engine/source/';
  72. self::$paths[ 'engineBin' ] = '../../../Engine/bin/';
  73. }
  74. else
  75. {
  76. self::$paths[ 'engineLib' ] = $torqueRoot . '/../Engine/lib/';
  77. self::$paths[ 'engineSrc' ] = $torqueRoot . '/../Engine/source/';
  78. self::$paths[ 'engineBin' ] = $torqueRoot . '/../Engine/bin/';
  79. }
  80. self::$paths[ 'modules' ] = $torqueRoot . '/Tools/projectGenerator/modules/';
  81. self::$paths[ 'libs' ] = $torqueRoot . '/Tools/projectGenerator/libs/';
  82. self::$platform = 'win32';
  83. }
  84. static function setGameProjectName($name)
  85. {
  86. self::$gameProjectName = $name;
  87. }
  88. static function getGameProjectName()
  89. {
  90. return self::$gameProjectName;
  91. }
  92. static function setToolBuild($tb)
  93. {
  94. self::$toolBuild = $tb;
  95. }
  96. static function getToolBuild()
  97. {
  98. return self::$toolBuild;
  99. }
  100. static function setWatermarkBuild($wb)
  101. {
  102. self::$watermarkBuild = $wb;
  103. }
  104. static function getWatermarkBuild()
  105. {
  106. return self::$watermarkBuild;
  107. }
  108. static function setPurchaseScreenBuild($psb)
  109. {
  110. self::$purchaseScnBuild = $psb;
  111. }
  112. static function getPurchaseScreenBuild()
  113. {
  114. return self::$purchaseScnBuild;
  115. }
  116. static function setDemoBuild($db)
  117. {
  118. self::$demoBuild = $db;
  119. }
  120. static function getDemoBuild()
  121. {
  122. return self::$demoBuild;
  123. }
  124. static function setObjectLimitBuild($olb)
  125. {
  126. self::$objectLimitBuild = $olb;
  127. }
  128. static function getObjectLimitBuild()
  129. {
  130. return self::$objectLimitBuild;
  131. }
  132. static function setTimeOutBuild($tob)
  133. {
  134. self::$timeOutBuild = $tob;
  135. }
  136. static function getTimeOutBuild()
  137. {
  138. return self::$timeOutBuild;
  139. }
  140. static function isApp()
  141. {
  142. return self::$project_cur->isApp();
  143. }
  144. static function getGeneratorLibsPath()
  145. {
  146. return self::$paths[ 'libs' ];
  147. }
  148. static function getGeneratorModulesPath()
  149. {
  150. return self::$paths['modules'];
  151. }
  152. static function getEngineSrcDir()
  153. {
  154. return self::$paths['engineSrc'];
  155. }
  156. static function getLibSrcDir()
  157. {
  158. return self::$paths['engineLib'];
  159. }
  160. static function getEngineBinDir()
  161. {
  162. return self::$paths['engineBin'];
  163. }
  164. static function addSrcDirRecursive($basePath, $dirPath)
  165. {
  166. $ignore = array( '.', '..', '.svn', '_svn', 'CVS' );
  167. $absPath = realpath($argv[1])."/buildFiles/";
  168. array_push( self::$project_cur->dir_list, $basePath.'/'.$dirPath );
  169. $dirHandle = opendir( $absPath.$basePath.'/'.$dirPath);
  170. while( $file = readdir( $dirHandle ) )
  171. {
  172. if( !in_array( $file, $ignore ) )
  173. {
  174. if( is_dir( $absPath.$basePath.'/'.$dirPath.'/'.$file ) )
  175. self::addSrcDirRecursive( $basePath, "$dirPath/$file");
  176. }
  177. }
  178. closedir( $dirHandle );
  179. }
  180. static function addSrcDir( $dir, $recurse = false )
  181. {
  182. if (!$recurse)
  183. array_push( self::$project_cur->dir_list, $dir );
  184. else
  185. {
  186. self::addSrcDirRecursive($dir, "");
  187. }
  188. }
  189. static function addSrcFile( $file )
  190. {
  191. array_push( self::$project_cur->dir_list, $file );
  192. }
  193. static function addIncludePath( $path )
  194. {
  195. array_push( self::$project_cur->includes, $path );
  196. }
  197. static function addProjectDefine( $d, $v )
  198. {
  199. if (!$v)
  200. array_push( self::$project_cur->defines, $d );
  201. else
  202. array_push( self::$project_cur->defines, $d."=".$v );
  203. }
  204. static function isDefined( $d )
  205. {
  206. foreach( self::$project_cur->defines as $v )
  207. {
  208. if( $v === $d )
  209. return true;
  210. else if( strpos( $v, $d . "=" ) === 0 )
  211. return true;
  212. }
  213. return false;
  214. }
  215. static function disableProjectWarning( $warning )
  216. {
  217. array_push( self::$project_cur->disabledWarnings, $warning );
  218. }
  219. static function addProjectDefines( $args_array )
  220. {
  221. self::$project_cur->defines = array_merge( self::$project_cur->defines, $args_array );
  222. }
  223. static function addProjectLibDir( $dir )
  224. {
  225. array_push( self::$project_cur->lib_dirs, $dir );
  226. }
  227. static function addProjectLibInput( $lib, $libDebug = null )
  228. {
  229. array_push( self::$project_cur->libs, $lib );
  230. array_push( self::$project_cur->libsDebug, $libDebug != null ? $libDebug : $lib );
  231. }
  232. static function addProjectIgnoreDefaultLib( $lib )
  233. {
  234. array_push( self::$project_cur->libsIgnore, $lib );
  235. }
  236. static function includeLib( $lib )
  237. {
  238. foreach( self::$libGuard as $libName )
  239. if( $libName == $lib )
  240. return;
  241. array_push( self::$libGuard, $lib );
  242. // if currently in a project, delay the include
  243. if (T3D_Generator::inProjectConfig())
  244. {
  245. array_push( self::$project_cur->lib_includes, $lib );
  246. return;
  247. }
  248. // otherwise include it immediately
  249. require( T3D_Generator::getGeneratorLibsPath() . $lib . '.conf' );
  250. }
  251. static function addProjectDependency( $pd )
  252. {
  253. array_push( self::$project_cur->dependencies, $pd );
  254. }
  255. static function removeProjectDependency( $pd )
  256. {
  257. foreach (self::$project_cur->dependencies as $key => $value)
  258. {
  259. if ($value == $pd)
  260. {
  261. unset(self::$project_cur->dependencies[$key]);
  262. }
  263. }
  264. self::$project_cur->dependencies = array_values(self::$project_cur->dependencies);
  265. }
  266. static function addProjectReference( $refName, $version = "")
  267. {
  268. self::$project_cur->addReference( $refName, $version );
  269. }
  270. static function setProjectGUID( $guid )
  271. {
  272. self::$project_cur->guid = $guid;
  273. }
  274. static function setProjectModuleDefinitionFile ( $mdef )
  275. {
  276. self::$project_cur->moduleDefinitionFile = $mdef;
  277. }
  278. static function copyFileToProject( $sourcePath, $projectDestPath )
  279. {
  280. // Create the array to hold the source and destination
  281. $paths = array();
  282. array_push( $paths, $sourcePath );
  283. array_push( $paths, $projectDestPath );
  284. // Add to the project
  285. array_push( self::$project_cur->fileCopyPaths, $paths );
  286. }
  287. static function beginModule( $name )
  288. {
  289. if( !self::$module_cur )
  290. self::$module_cur = $name;
  291. else
  292. echo( "T3D_Generator::beginModule() - already in module!" );
  293. }
  294. static function endModule()
  295. {
  296. if( self::$module_cur )
  297. self::$module_cur = null;
  298. else
  299. trigger_error( "T3D_Generator::endModule() - no active module!", E_USER_ERROR );
  300. }
  301. static function inProjectConfig()
  302. {
  303. return self::$project_cur != null;
  304. }
  305. static function setProjectSubSystem( $subSystem )
  306. {
  307. self::$project_cur->setSubSystem( $subSystem );
  308. }
  309. static function beginProjectConfig( $name, $type, $guid, $game_dir, $output_name )
  310. {
  311. if( !self::$project_cur )
  312. {
  313. echo( " - begin project: " . $name . "=" . $guid . "\n" );
  314. self::$project_cur = new Project( $name, $type, $guid, $game_dir, $output_name );
  315. self::$config_projects[ $name ] = self::$project_cur;
  316. }
  317. else
  318. trigger_error( "T3D_Generator::beginProjectConfig() - a project is already open!", E_USER_ERROR );
  319. }
  320. static function endProjectConfig( $type )
  321. {
  322. //echo( "PT: " .$type. ":".self::$project_cur->type . "\n");
  323. if( self::$project_cur )
  324. {
  325. if( self::$project_cur->type == $type )
  326. {
  327. echo( " - end project " . self::$project_cur->name . "\n" );
  328. // Set project outputs
  329. self::$project_cur->outputs = BuildTarget::getInstances();
  330. // Allow project to optimize and validate state, etc
  331. self::$project_cur->validate();
  332. // Bit of flummery from original code, not sure what it is supposed to do -- neo
  333. if( $type == Project::$TYPE_LIB )
  334. {
  335. // Merge lib includes to global lib include array
  336. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  337. }
  338. else if ( $type == Project::$TYPE_SHARED_LIB )
  339. {
  340. // Merge lib includes into app include list
  341. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  342. }
  343. else if ( $type == Project::$TYPE_ACTIVEX )
  344. {
  345. // Merge lib includes into app include list
  346. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  347. }
  348. else if ( $type == Project::$TYPE_SAFARI )
  349. {
  350. // Merge lib includes into app include list
  351. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  352. }
  353. else if ( $type == Project::$TYPE_SHARED_APP )
  354. {
  355. // Merge lib includes into app include list
  356. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  357. }
  358. else if ( $type == Project::$TYPE_APP )
  359. {
  360. // Merge lib includes into app include list
  361. self::$project_cur->addIncludes( self::$app_lib_includes );
  362. }
  363. // Clear out sucker
  364. $p = self::$project_cur;
  365. self::$project_cur = null;
  366. // Now include any libraries included in the modules
  367. foreach( $p->lib_includes as $libName )
  368. require( T3D_Generator::getGeneratorLibsPath() . $libName . '.conf' );
  369. }
  370. else
  371. trigger_error( "T3D_Generator::endProjectConfig() - closing type mismatch!", E_USER_ERROR );
  372. }
  373. else
  374. trigger_error( "T3D_Generator::endProjectConfig() - no currently open project!", E_USER_ERROR );
  375. }
  376. static function beginActiveXConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  377. {
  378. self::beginProjectConfig( $lib_name, Project::$TYPE_ACTIVEX, $guid, $game_dir, $output_name );
  379. // Handle ActiveX specific setup (including ATL template processing, etc)
  380. $activex = new ActiveXWebPlugin();
  381. $activex->process(self::$project_cur);
  382. }
  383. static function endActiveXConfig()
  384. {
  385. self::endProjectConfig( Project::$TYPE_ACTIVEX );
  386. }
  387. static function beginSafariConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  388. {
  389. self::beginProjectConfig( $lib_name, Project::$TYPE_SAFARI, $guid, $game_dir, $output_name );
  390. // Handle Safari specific setup
  391. $safari = new SafariWebPlugin();
  392. $safari->process(self::$project_cur);
  393. }
  394. static function endSafariConfig()
  395. {
  396. self::endProjectConfig( Project::$TYPE_SAFARI );
  397. }
  398. static function beginNPPluginConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  399. {
  400. self::beginProjectConfig( $lib_name, Project::$TYPE_SHARED_LIB, $guid, $game_dir, $output_name );
  401. self::$project_cur->setUniformOutputFile();
  402. // Handle NP specific setup (resource template processing, etc)
  403. $NP = new NPWebPlugin();
  404. $NP->process(self::$project_cur);
  405. }
  406. static function endNPPluginConfig()
  407. {
  408. self::endProjectConfig( Project::$TYPE_SHARED_LIB );
  409. }
  410. static function beginSharedLibConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  411. {
  412. self::beginProjectConfig( $lib_name, Project::$TYPE_SHARED_LIB, $guid, $game_dir, $output_name );
  413. }
  414. static function endSharedLibConfig()
  415. {
  416. self::endProjectConfig( Project::$TYPE_SHARED_LIB );
  417. }
  418. static function beginCSProjectConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  419. {
  420. self::beginProjectConfig( $lib_name, Project::$TYPE_CSPROJECT, $guid, $game_dir, $output_name );
  421. }
  422. static function endCSProjectConfig()
  423. {
  424. self::endProjectConfig( Project::$TYPE_CSPROJECT );
  425. }
  426. static function beginLibConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  427. {
  428. self::beginProjectConfig( $lib_name, Project::$TYPE_LIB, $guid, $game_dir, $output_name );
  429. }
  430. static function endLibConfig()
  431. {
  432. self::endProjectConfig( Project::$TYPE_LIB );
  433. }
  434. static function beginSharedAppConfig( $app_name, $guid = '', $game_dir = 'game', $output_name = '' )
  435. {
  436. self::beginProjectConfig( $app_name, Project::$TYPE_SHARED_APP, $guid, $game_dir, $output_name );
  437. }
  438. static function endSharedAppConfig()
  439. {
  440. self::endProjectConfig( Project::$TYPE_SHARED_APP);
  441. }
  442. static function beginAppConfig( $app_name, $guid = '', $game_dir = 'game', $output_name = '' )
  443. {
  444. self::beginProjectConfig( $app_name, Project::$TYPE_APP, $guid, $game_dir, $output_name );
  445. }
  446. static function endAppConfig()
  447. {
  448. self::endProjectConfig( Project::$TYPE_APP );
  449. }
  450. static function lookupProjectByName( $pname )
  451. {
  452. foreach( self::$config_projects as $projName => $proj )
  453. if( $projName == $pname )
  454. return $proj;
  455. return null;
  456. }
  457. // We encapsulate the meat of the project generator in generateProjects()
  458. // so that more complex .config files can generate more than one run of projects
  459. // without having to run a second instance of php
  460. static function generateProjects( $tpl )
  461. {
  462. // Alright, for each project scan and generate the file list.
  463. $projectFiles = array ();
  464. $rootPhpBuildDir = getcwd();
  465. foreach( self::$config_projects as $projName => $proj )
  466. {
  467. echo( " - Processing project '$projName'...\n" );
  468. $proj->generate( $tpl, self::$platform, $rootPhpBuildDir );
  469. }
  470. echo( "PROJECTS DONE\n\n" );
  471. chdir( $rootPhpBuildDir );
  472. }
  473. /////////////////////// SOLUTIONS /////////////////////////
  474. static function beginSolution( $name, $guid )
  475. {
  476. if( !self::$solution_cur )
  477. {
  478. self::$solution_cur = new Solution( $name, $guid );
  479. self::$solutions[ $name ] = self::$solution_cur;
  480. }
  481. else
  482. trigger_error( "T3D_Generator::beginSolution() - tried to start $name but already in the ".self::$solution_cur->name." solution!", E_USER_ERROR );
  483. }
  484. static function addSolutionProjectRef( $pname )
  485. {
  486. if( isset( self::$solution_cur ) )
  487. self::$solution_cur->addProjectRef( $pname );
  488. else
  489. trigger_error( "T3D_Generator::addSolutionProjectRef(): no such project - " . $pname . "\n", E_USER_ERROR );
  490. }
  491. static function addSolutionProjectRefExt( $pname, $ppath, $pguid )
  492. {
  493. if( isset( self::$solution_cur ) )
  494. self::$solution_cur->addSolutionProjectRefExt( $pname, $ppath, $pguid );
  495. else
  496. trigger_error( "T3D_Generator::addSolutionProjectRefExt(): no such project - " . $pname . "\n", E_USER_ERROR );
  497. }
  498. static function endSolution()
  499. {
  500. if( isset( self::$solution_cur ) )
  501. {
  502. self::$solution_cur->setOutputs( BuildTarget::getInstances() );
  503. self::$solution_cur = null;
  504. }
  505. else
  506. trigger_error( "T3D_Generator::endSolution(): no active solution!\n", E_USER_ERROR );
  507. }
  508. static function generateSolutions( $tpl )
  509. {
  510. echo( "GENERATING SOLUTIONS\n\n" );
  511. $rootPhpBuildDir = getcwd();
  512. // Process all solutions
  513. foreach( self::$solutions as $sname => $solution )
  514. {
  515. echo( " - Generating solution: " . $sname . "\n" );
  516. chdir( $rootPhpBuildDir );
  517. $solution->generate( $tpl, self::$platform, $rootPhpBuildDir );
  518. }
  519. chdir( $rootPhpBuildDir );
  520. }
  521. static function setDLLRuntime( $val )
  522. {
  523. self::$useDLLRuntime = $val;
  524. }
  525. }
  526. ?>