2
0

Generator.php 20 KB

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