Generator.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 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 disableProjectWarning( $warning )
  205. {
  206. array_push( self::$project_cur->disabledWarnings, $warning );
  207. }
  208. static function addProjectDefines( $args_array )
  209. {
  210. self::$project_cur->defines = array_merge( self::$project_cur->defines, $args_array );
  211. }
  212. static function addProjectLibDir( $dir )
  213. {
  214. array_push( self::$project_cur->lib_dirs, $dir );
  215. }
  216. static function addProjectLibInput( $lib )
  217. {
  218. array_push( self::$project_cur->libs, $lib );
  219. }
  220. static function includeLib( $lib )
  221. {
  222. foreach( self::$libGuard as $libName )
  223. if( $libName == $lib )
  224. return;
  225. array_push( self::$libGuard, $lib );
  226. // if currently in a project, delay the include
  227. if (Generator::inProjectConfig())
  228. {
  229. array_push( self::$project_cur->lib_includes, $lib );
  230. return;
  231. }
  232. // otherwise include it immediately
  233. require( Generator::getGeneratorLibsPath() . $lib . '.conf' );
  234. }
  235. static function addProjectDependency( $pd )
  236. {
  237. array_push( self::$project_cur->dependencies, $pd );
  238. }
  239. static function removeProjectDependency( $pd )
  240. {
  241. foreach (self::$project_cur->dependencies as $key => $value)
  242. {
  243. if ($value == $pd)
  244. {
  245. unset(self::$project_cur->dependencies[$key]);
  246. }
  247. }
  248. self::$project_cur->dependencies = array_values(self::$project_cur->dependencies);
  249. }
  250. static function addProjectReference( $refName, $version = "")
  251. {
  252. self::$project_cur->addReference( $refName, $version );
  253. }
  254. static function setProjectGUID( $guid )
  255. {
  256. self::$project_cur->guid = $guid;
  257. }
  258. static function setProjectModuleDefinitionFile ( $mdef )
  259. {
  260. self::$project_cur->moduleDefinitionFile = $mdef;
  261. }
  262. static function beginModule( $name )
  263. {
  264. if( !self::$module_cur )
  265. self::$module_cur = $name;
  266. else
  267. echo( "Generator::beginModule() - already in module!" );
  268. }
  269. static function endModule()
  270. {
  271. if( self::$module_cur )
  272. self::$module_cur = null;
  273. else
  274. trigger_error( "Generator::endModule() - no active module!", E_USER_ERROR );
  275. }
  276. static function inProjectConfig()
  277. {
  278. return self::$project_cur != null;
  279. }
  280. static function setProjectSubSystem( $subSystem )
  281. {
  282. self::$project_cur->setSubSystem( $subSystem );
  283. }
  284. static function beginProjectConfig( $name, $type, $guid, $game_dir, $output_name )
  285. {
  286. if( !self::$project_cur )
  287. {
  288. echo( " - begin project: " . $name . "=" . $guid . "\n" );
  289. self::$project_cur = new Project( $name, $type, $guid, $game_dir, $output_name );
  290. self::$config_projects[ $name ] = self::$project_cur;
  291. }
  292. else
  293. trigger_error( "Generator::beginProjectConfig() - a project is already open!", E_USER_ERROR );
  294. }
  295. static function endProjectConfig( $type )
  296. {
  297. //echo( "PT: " .$type. ":".self::$project_cur->type . "\n");
  298. if( self::$project_cur )
  299. {
  300. if( self::$project_cur->type == $type )
  301. {
  302. echo( " - end project " . self::$project_cur->name . "\n" );
  303. // Set project outputs
  304. self::$project_cur->outputs = BuildTarget::getInstances();
  305. // Allow project to optimize and validate state, etc
  306. self::$project_cur->validate();
  307. // Bit of flummery from original code, not sure what it is supposed to do -- neo
  308. if( $type == Project::$TYPE_LIB )
  309. {
  310. // Merge lib includes to global lib include array
  311. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  312. }
  313. else if ( $type == Project::$TYPE_SHARED_LIB )
  314. {
  315. // Merge lib includes into app include list
  316. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  317. }
  318. else if ( $type == Project::$TYPE_ACTIVEX )
  319. {
  320. // Merge lib includes into app include list
  321. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  322. }
  323. else if ( $type == Project::$TYPE_SAFARI )
  324. {
  325. // Merge lib includes into app include list
  326. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  327. }
  328. else if ( $type == Project::$TYPE_SHARED_APP )
  329. {
  330. // Merge lib includes into app include list
  331. self::$app_lib_includes = array_merge( self::$app_lib_includes, self::$project_cur->includes );
  332. }
  333. else if ( $type == Project::$TYPE_APP )
  334. {
  335. // Merge lib includes into app include list
  336. self::$project_cur->addIncludes( self::$app_lib_includes );
  337. }
  338. // Clear out sucker
  339. $p = self::$project_cur;
  340. self::$project_cur = null;
  341. // Now include any libraries included in the modules
  342. foreach( $p->lib_includes as $libName )
  343. require( Generator::getGeneratorLibsPath() . $libName . '.conf' );
  344. }
  345. else
  346. trigger_error( "Generator::endProjectConfig() - closing type mismatch!", E_USER_ERROR );
  347. }
  348. else
  349. trigger_error( "Generator::endProjectConfig() - no currently open project!", E_USER_ERROR );
  350. }
  351. static function beginActiveXConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  352. {
  353. self::beginProjectConfig( $lib_name, Project::$TYPE_ACTIVEX, $guid, $game_dir, $output_name );
  354. // Handle ActiveX specific setup (including ATL template processing, etc)
  355. $activex = new ActiveXWebPlugin();
  356. $activex->process(self::$project_cur);
  357. }
  358. static function endActiveXConfig()
  359. {
  360. self::endProjectConfig( Project::$TYPE_ACTIVEX );
  361. }
  362. static function beginSafariConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  363. {
  364. self::beginProjectConfig( $lib_name, Project::$TYPE_SAFARI, $guid, $game_dir, $output_name );
  365. // Handle Safari specific setup
  366. $safari = new SafariWebPlugin();
  367. $safari->process(self::$project_cur);
  368. }
  369. static function endSafariConfig()
  370. {
  371. self::endProjectConfig( Project::$TYPE_SAFARI );
  372. }
  373. static function beginNPPluginConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  374. {
  375. self::beginProjectConfig( $lib_name, Project::$TYPE_SHARED_LIB, $guid, $game_dir, $output_name );
  376. self::$project_cur->setUniformOutputFile();
  377. // Handle NP specific setup (resource template processing, etc)
  378. $NP = new NPWebPlugin();
  379. $NP->process(self::$project_cur);
  380. }
  381. static function endNPPluginConfig()
  382. {
  383. self::endProjectConfig( Project::$TYPE_SHARED_LIB );
  384. }
  385. static function beginSharedLibConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  386. {
  387. self::beginProjectConfig( $lib_name, Project::$TYPE_SHARED_LIB, $guid, $game_dir, $output_name );
  388. }
  389. static function endSharedLibConfig()
  390. {
  391. self::endProjectConfig( Project::$TYPE_SHARED_LIB );
  392. }
  393. static function beginCSProjectConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  394. {
  395. self::beginProjectConfig( $lib_name, Project::$TYPE_CSPROJECT, $guid, $game_dir, $output_name );
  396. }
  397. static function endCSProjectConfig()
  398. {
  399. self::endProjectConfig( Project::$TYPE_CSPROJECT );
  400. }
  401. static function beginLibConfig( $lib_name, $guid = '', $game_dir = 'game', $output_name = '' )
  402. {
  403. self::beginProjectConfig( $lib_name, Project::$TYPE_LIB, $guid, $game_dir, $output_name );
  404. }
  405. static function endLibConfig()
  406. {
  407. self::endProjectConfig( Project::$TYPE_LIB );
  408. }
  409. static function beginSharedAppConfig( $app_name, $guid = '', $game_dir = 'game', $output_name = '' )
  410. {
  411. self::beginProjectConfig( $app_name, Project::$TYPE_SHARED_APP, $guid, $game_dir, $output_name );
  412. }
  413. static function endSharedAppConfig()
  414. {
  415. self::endProjectConfig( Project::$TYPE_SHARED_APP);
  416. }
  417. static function beginAppConfig( $app_name, $guid = '', $game_dir = 'game', $output_name = '' )
  418. {
  419. self::beginProjectConfig( $app_name, Project::$TYPE_APP, $guid, $game_dir, $output_name );
  420. }
  421. static function endAppConfig()
  422. {
  423. self::endProjectConfig( Project::$TYPE_APP );
  424. }
  425. static function lookupProjectByName( $pname )
  426. {
  427. foreach( self::$config_projects as $projName => $proj )
  428. if( $projName == $pname )
  429. return $proj;
  430. return null;
  431. }
  432. // We encapsulate the meat of the project generator in generateProjects()
  433. // so that more complex .config files can generate more than one run of projects
  434. // without having to run a second instance of php
  435. static function generateProjects( $tpl )
  436. {
  437. // Alright, for each project scan and generate the file list.
  438. $projectFiles = array ();
  439. $rootPhpBuildDir = getcwd();
  440. foreach( self::$config_projects as $projName => $proj )
  441. {
  442. echo( " - Processing project '$projName'...\n" );
  443. $proj->generate( $tpl, self::$platform, $rootPhpBuildDir );
  444. }
  445. echo( "PROJECTS DONE\n\n" );
  446. chdir( $rootPhpBuildDir );
  447. }
  448. /////////////////////// SOLUTIONS /////////////////////////
  449. static function beginSolution( $name, $guid )
  450. {
  451. if( !self::$solution_cur )
  452. {
  453. self::$solution_cur = new Solution( $name, $guid );
  454. self::$solutions[ $name ] = self::$solution_cur;
  455. }
  456. else
  457. trigger_error( "Generator::beginSolution() - tried to start $name but already in the ".self::$solution_cur->name." solution!", E_USER_ERROR );
  458. }
  459. static function addSolutionProjectRef( $pname )
  460. {
  461. if( isset( self::$solution_cur ) )
  462. self::$solution_cur->addProjectRef( $pname );
  463. else
  464. trigger_error( "Generator::addSolutionProjectRef(): no such project - " . $pname . "\n", E_USER_ERROR );
  465. }
  466. static function addSolutionProjectRefExt( $pname, $ppath, $pguid )
  467. {
  468. if( isset( self::$solution_cur ) )
  469. self::$solution_cur->addSolutionProjectRefExt( $pname, $ppath, $pguid );
  470. else
  471. trigger_error( "Generator::addSolutionProjectRefExt(): no such project - " . $pname . "\n", E_USER_ERROR );
  472. }
  473. static function endSolution()
  474. {
  475. if( isset( self::$solution_cur ) )
  476. {
  477. self::$solution_cur->setOutputs( BuildTarget::getInstances() );
  478. self::$solution_cur = null;
  479. }
  480. else
  481. trigger_error( "Generator::endSolution(): no active solution!\n", E_USER_ERROR );
  482. }
  483. static function generateSolutions( $tpl )
  484. {
  485. echo( "GENERATING SOLUTIONS\n\n" );
  486. $rootPhpBuildDir = getcwd();
  487. // Process all solutions
  488. foreach( self::$solutions as $sname => $solution )
  489. {
  490. echo( " - Generating solution: " . $sname . "\n" );
  491. chdir( $rootPhpBuildDir );
  492. $solution->generate( $tpl, self::$platform, $rootPhpBuildDir );
  493. }
  494. chdir( $rootPhpBuildDir );
  495. }
  496. static function setDLLRuntime( $val )
  497. {
  498. self::$useDLLRuntime = $val;
  499. }
  500. }
  501. ?>