make.js 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272
  1. // Based on https://github.com/Kode/kmake by RobDangerous
  2. // ██╗ ██╗██████╗
  3. // ██║ ██║██╔══██╗
  4. // ██║ ██║██████╔╝
  5. // ██║ ██║██╔══██╗
  6. // ███████╗██║██████╔╝
  7. // ╚══════╝╚═╝╚═════╝
  8. import * as std from "std";
  9. import * as os from "os";
  10. let path_sep = "/";
  11. let other_path_sep = "\\";
  12. if (os_platform() === "win32") {
  13. path_sep = "\\";
  14. other_path_sep = "/";
  15. }
  16. let binpath = path_resolve(scriptArgs[0]);
  17. let toolsdir = binpath.substring(0, binpath.lastIndexOf(path_sep));
  18. let makedir = path_join(toolsdir, "..", "..");
  19. let __dirname = makedir;
  20. let irondir = path_join(makedir, "..");
  21. function get_binary_data(p) {
  22. return fs_readfile(irondir + "/sources/backends/data/" + p);
  23. }
  24. function get_text_data(p) {
  25. return fs_readfile(irondir + "/sources/backends/data/" + p);
  26. }
  27. function fs_exists(p) {
  28. return os.stat(p)[0] != null;
  29. }
  30. function fs_mkdir(p) {
  31. os.mkdir(p);
  32. }
  33. function array_remove(arr, e) {
  34. let i = arr.indexOf(e);
  35. if (i != -1) {
  36. arr.splice(i, 1);
  37. }
  38. }
  39. function fs_readdir(p) {
  40. let dirs = os.readdir(p)[0];
  41. array_remove(dirs, ".");
  42. array_remove(dirs, "..");
  43. return dirs;
  44. }
  45. function fs_copyfile(from, to) {
  46. let f = std.open(from, "rb");
  47. f.seek(0, std.SEEK_END);
  48. let size = f.tell();
  49. let u8 = new Uint8Array(size);
  50. f.seek(0, std.SEEK_SET);
  51. f.read(u8.buffer, 0, size);
  52. f.close();
  53. f = std.open(to, "wb");
  54. f.write(u8.buffer, 0, size);
  55. f.close();
  56. }
  57. function fs_isdir(p) {
  58. return (os.stat(p)[0].mode & os.S_IFMT) == os.S_IFDIR;
  59. }
  60. function fs_mtime(p) {
  61. return os.stat(p)[0].mtime;
  62. }
  63. function fs_readfile(p) {
  64. return std.loadFile(p);
  65. }
  66. function fs_writefile(p, data) {
  67. let f = std.open(p, "w");
  68. f.puts(data); // utf8
  69. f.close();
  70. }
  71. function parent_dir(dir) {
  72. return dir.substring(0, dir.lastIndexOf(path_sep));
  73. }
  74. function fs_ensuredir(dir) {
  75. if (dir != "" && !fs_exists(dir)) {
  76. fs_ensuredir(parent_dir(dir));
  77. fs_mkdir(dir);
  78. }
  79. }
  80. function fs_copydir(from, to) {
  81. fs_ensuredir(to);
  82. let files = fs_readdir(from);
  83. for (let file of files) {
  84. if (fs_isdir(path_join(from, file))) {
  85. fs_copydir(path_join(from, file), path_join(to, file));
  86. }
  87. else {
  88. fs_copyfile(path_join(from, file), path_join(to, file));
  89. }
  90. }
  91. }
  92. function os_popen(exe, params = [], ops = {}) {
  93. params.unshift(exe);
  94. let res = { stdout: "" };
  95. let cwd;
  96. if (ops.cwd) {
  97. cwd = os_cwd();
  98. os.chdir(ops.cwd);
  99. }
  100. let p = std.popen(params.join(" "), "r");
  101. res.stdout = p.readAsString();
  102. p.close();
  103. if (ops.cwd) {
  104. os.chdir(cwd);
  105. }
  106. return res;
  107. }
  108. function os_exec(exe, params = [], ops = {}) {
  109. params.unshift(exe);
  110. let res = { status: 0 };
  111. if (os_platform() === "win32") {
  112. res = amake.os_exec_win(params, ops); // { status, stdout }
  113. }
  114. else {
  115. res.status = os.exec(params, ops);
  116. }
  117. return res;
  118. }
  119. function os_platform() {
  120. return os.platform;
  121. }
  122. function os_cwd() {
  123. return os.getcwd()[0];
  124. }
  125. function os_env(s) {
  126. return std.getenv(s);
  127. }
  128. function os_argv() {
  129. return scriptArgs;
  130. }
  131. function os_cpus_length() {
  132. // return os.cpus().length;
  133. return 8;
  134. }
  135. function os_chmod(p, m) {
  136. if (os_platform() === "win32") {
  137. return;
  138. }
  139. os_exec("chmod", [m, p]);
  140. }
  141. function os_exit(c) {
  142. std.exit(c);
  143. }
  144. function crypto_random_uuid() {
  145. let u = Date.now().toString(16) + Math.random().toString(16) + "0".repeat(16);
  146. let guid = [u.substring(0, 8), u.substring(8, 12), "4000-8" + u.substring(13, 16), u.substring(16, 28)].join("-");
  147. return guid;
  148. }
  149. // https://github.com/kawanet/sha1-uint8array
  150. var SHA1="undefined"!=typeof exports?exports:{};!function(t){var r=[1518500249,1859775393,-1894007588,-899497514],i={sha1:1};SHA1.createHash=function(t){if(t&&!i[t]&&!i[t.toLowerCase()])throw new Error("Digest method not supported");return new s};var n,s=function(){function t(){this.A=1732584193,this.B=-271733879,this.C=-1732584194,this.D=271733878,this.E=-1009589776,this.t=0,this.i=0,(!n||e>=8e3)&&(n=new ArrayBuffer(8e3),e=0),this.h=new Uint8Array(n,e,80),this.o=new Int32Array(n,e,20),e+=80}return t.prototype.update=function(t){if("string"==typeof t)return this.u(t);if(null==t)throw new TypeError("Invalid type: "+typeof t);var r=t.byteOffset,i=t.byteLength,n=i/64|0,s=0;if(n&&!(3&r)&&!(this.t%64)){for(var h=new Int32Array(t.buffer,r,16*n);n--;)this.v(h,s>>2),s+=64;this.t+=s}if(1!==t.BYTES_PER_ELEMENT&&t.buffer){var e=new Uint8Array(t.buffer,r+s,i-s);return this.p(e)}return s===i?this:this.p(t,s)},t.prototype.p=function(t,r){var i=this.h,n=this.o,s=t.length;for(r|=0;r<s;){for(var h=this.t%64,e=h;r<s&&e<64;)i[e++]=t[r++];e>=64&&this.v(n),this.t+=e-h}return this},t.prototype.u=function(t){for(var r=this.h,i=this.o,n=t.length,s=this.i,h=0;h<n;){for(var e=this.t%64,f=e;h<n&&f<64;){var o=0|t.charCodeAt(h++);o<128?r[f++]=o:o<2048?(r[f++]=192|o>>>6,r[f++]=128|63&o):o<55296||o>57343?(r[f++]=224|o>>>12,r[f++]=128|o>>>6&63,r[f++]=128|63&o):s?(o=((1023&s)<<10)+(1023&o)+65536,r[f++]=240|o>>>18,r[f++]=128|o>>>12&63,r[f++]=128|o>>>6&63,r[f++]=128|63&o,s=0):s=o}f>=64&&(this.v(i),i[0]=i[16]),this.t+=f-e}return this.i=s,this},t.prototype.v=function(t,i){var n=this,s=n.A,e=n.B,f=n.C,w=n.D,y=n.E,A=0;for(i|=0;A<16;)h[A++]=o(t[i++]);for(A=16;A<80;A++)h[A]=u(h[A-3]^h[A-8]^h[A-14]^h[A-16]);for(A=0;A<80;A++){var p=A/20|0,d=a(s)+v(p,e,f,w)+y+h[A]+r[p]|0;y=w,w=f,f=c(e),e=s,s=d}this.A=s+this.A|0,this.B=e+this.B|0,this.C=f+this.C|0,this.D=w+this.D|0,this.E=y+this.E|0},t.prototype.digest=function(t){var r=this.h,i=this.o,n=this.t%64|0;for(r[n++]=128;3&n;)r[n++]=0;if((n>>=2)>14){for(;n<16;)i[n++]=0;n=0,this.v(i)}for(;n<16;)i[n++]=0;var s=8*this.t,h=(4294967295&s)>>>0,e=(s-h)/4294967296;return e&&(i[14]=o(e)),h&&(i[15]=o(h)),this.v(i),"hex"===t?this.I():this.U()},t.prototype.I=function(){var t=this,r=t.A,i=t.B,n=t.C,s=t.D,h=t.E;return f(r)+f(i)+f(n)+f(s)+f(h)},t.prototype.U=function(){var t=this,r=t.A,i=t.B,n=t.C,s=t.D,h=t.E,e=t.h,f=t.o;return f[0]=o(r),f[1]=o(i),f[2]=o(n),f[3]=o(s),f[4]=o(h),e.slice(0,20)},t}(),h=new Int32Array(80),e=0,f=function(t){return(t+4294967296).toString(16).substr(-8)},o=254===new Uint8Array(new Uint16Array([65279]).buffer)[0]?function(t){return t}:function(t){return t<<24&4278190080|t<<8&16711680|t>>8&65280|t>>24&255},u=function(t){return t<<1|t>>>31},a=function(t){return t<<5|t>>>27},c=function(t){return t<<30|t>>>2};function v(t,r,i,n){return 0===t?r&i|~r&n:2===t?r&i|r&n|i&n:r^i^n}}();
  151. function uuidv5(path, namespace) {
  152. let hash = SHA1.createHash("sha1");
  153. hash.update(namespace);
  154. hash.update(path);
  155. let value = hash.digest("hex");
  156. return value.substring(0, 8) + "-" + value.substring(8, 12) + "-" + value.substring(12, 16) + "-" + value.substring(16, 20) + "-" + value.substring(20, 32);
  157. }
  158. function path_join() {
  159. let args = Array.from(arguments);
  160. return path_normalize(args.join(path_sep));
  161. }
  162. function path_isabs(p) {
  163. return p[0] == "/" || p[1] == ":" || (p[0] == "\\" && p[1] == "\\");
  164. }
  165. function _path_resolve(base, relative) {
  166. let stack = base.split("/");
  167. let parts = relative.split("/");
  168. for (let i = 0; i < parts.length; i++) {
  169. if (parts[i] == ".") {
  170. continue;
  171. }
  172. if (parts[i] == "..") {
  173. stack.pop();
  174. }
  175. else {
  176. stack.push(parts[i]);
  177. }
  178. }
  179. return stack.join("/");
  180. }
  181. function path_resolve() {
  182. let args = Array.from(arguments);
  183. if (!path_isabs(args[0])) {
  184. args.unshift(os_cwd());
  185. }
  186. let i = args.length - 1;
  187. let p = args[i];
  188. p = path_normalize(p);
  189. while (!path_isabs(p)) {
  190. i--;
  191. p = _path_resolve(args[i], p);
  192. p = path_normalize(p);
  193. }
  194. return p;
  195. }
  196. function path_relative(from, to) {
  197. let a = from.split(path_sep);
  198. let b = to.split(path_sep);
  199. while (a[0] == b[0]) {
  200. a.shift();
  201. b.shift();
  202. if (a.length == 0 || b.length == 0) {
  203. break;
  204. }
  205. }
  206. let base = "";
  207. for (let i = 0; i < a.length; ++i) {
  208. base += ".." + path_sep;
  209. }
  210. base += b.join(path_sep);
  211. return base;
  212. }
  213. function path_normalize(p) {
  214. p = p.replaceAll(other_path_sep, path_sep);
  215. while (p.indexOf(path_sep + path_sep) != -1) {
  216. p = p.replaceAll(path_sep + path_sep, path_sep);
  217. }
  218. if (p.endsWith(path_sep)) {
  219. p = p.substring(0, p.length - 1);
  220. }
  221. let ar = p.split(path_sep);
  222. let i = 0;
  223. while (i < ar.length) {
  224. if (i > 0 && ar[i] == ".." && ar[i - 1] != "..") {
  225. ar.splice(i - 1, 2);
  226. i--;
  227. }
  228. else {
  229. i++;
  230. }
  231. }
  232. return ar.join(path_sep);
  233. }
  234. function exe_ext() {
  235. return os_platform() == "win32" ? ".exe" : "";
  236. }
  237. function path_extname(p) {
  238. return p.substring(p.lastIndexOf("."), p.length);
  239. }
  240. function path_basename(p) {
  241. return p.substring(p.lastIndexOf(path_sep) + 1, p.length);
  242. }
  243. function path_basename_noext(p) {
  244. return p.substring(p.lastIndexOf(path_sep) + 1, p.lastIndexOf("."));
  245. }
  246. function path_dirname(p) {
  247. return p.substring(0, p.lastIndexOf(path_sep));
  248. }
  249. function sys_dir() {
  250. if (os_platform() === "linux") {
  251. // if (os_arch() === "arm64") return "linux_arm64";
  252. return "linux_x64";
  253. }
  254. else if (os_platform() === "win32") {
  255. return "windows_x64";
  256. }
  257. else {
  258. return "macos";
  259. }
  260. }
  261. function matches(text, pattern) {
  262. let regexstring = pattern.replace(/\./g, "\\.").replace(/\*\*/g, ".?").replace(/\*/g, "[^/]*").replace(/\?/g, "*");
  263. let regex = new RegExp("^" + regexstring + "$", "g");
  264. return regex.test(text);
  265. }
  266. function stringify(p) {
  267. return p.replaceAll("\\", "/");
  268. }
  269. // ███████╗██╗ ██╗██████╗ ██████╗ ██████╗ ████████╗███████╗██████╗ ███████╗
  270. // ██╔════╝╚██╗██╔╝██╔══██╗██╔═══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗██╔════╝
  271. // █████╗ ╚███╔╝ ██████╔╝██║ ██║██████╔╝ ██║ █████╗ ██████╔╝███████╗
  272. // ██╔══╝ ██╔██╗ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ██╔══╝ ██╔══██╗╚════██║
  273. // ███████╗██╔╝ ██╗██║ ╚██████╔╝██║ ██║ ██║ ███████╗██║ ██║███████║
  274. // ╚══════╝╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝
  275. class Exporter {
  276. constructor() {
  277. this.path = null;
  278. this.outFile = null;
  279. }
  280. write_file(file) {
  281. this.path = file;
  282. this.outFile = "";
  283. }
  284. close_file() {
  285. fs_writefile(this.path, this.outFile);
  286. this.outFile = "";
  287. }
  288. p(line = "", indent = 0) {
  289. let tabs = "";
  290. for (let i = 0; i < indent; ++i) {
  291. tabs += "\t";
  292. }
  293. this.outFile += tabs + line + "\n";
  294. }
  295. nice_path(from, to, filepath) {
  296. return filepath;
  297. // let absolute = path_normalize(filepath);
  298. // if (!path_isabs(absolute)) {
  299. // absolute = path_resolve(from, filepath);
  300. // }
  301. // return path_relative(to, absolute);
  302. }
  303. }
  304. function get_dir_from_string(file, base) {
  305. file = file.replace(/\\/g, '/');
  306. if (file.indexOf("/") >= 0) {
  307. let dir = file.substr(0, file.lastIndexOf("/"));
  308. return path_join(base, path_relative(base, dir)).replace(/\\/g, "/");
  309. }
  310. else {
  311. return base;
  312. }
  313. }
  314. function get_dir(file) {
  315. return get_dir_from_string(file.file, file.projectName);
  316. }
  317. class VisualStudioExporter extends Exporter {
  318. constructor() {
  319. super();
  320. }
  321. get_debug_dir(from, project) {
  322. let debugdir = project.get_debug_dir();
  323. if (path_isabs(debugdir)) {
  324. debugdir = debugdir.replace(/\//g, "\\");
  325. }
  326. else {
  327. debugdir = path_resolve(from, debugdir).replace(/\//g, "\\");
  328. }
  329. return debugdir;
  330. }
  331. export_user_file(from, to, project, platform) {
  332. if (project.get_debug_dir() === "")
  333. return;
  334. this.write_file(path_resolve(to, project.get_safe_name() + ".vcxproj.user"));
  335. this.p('<?xml version="1.0" encoding="utf-8"?>');
  336. this.p('<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">');
  337. this.p('<PropertyGroup>', 1);
  338. if (platform === "windows") {
  339. this.p('<LocalDebuggerWorkingDirectory>' + this.get_debug_dir(from, project) + '</LocalDebuggerWorkingDirectory>', 2);
  340. this.p('<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>', 2);
  341. project.cmdArgs.push(this.get_debug_dir(from, project));
  342. if (project.cmdArgs.length > 0) {
  343. this.p('<LocalDebuggerCommandArguments>' + project.cmdArgs.join(' ') + '</LocalDebuggerCommandArguments>', 2);
  344. }
  345. }
  346. this.p('</PropertyGroup>', 1);
  347. this.p('</Project>');
  348. this.close_file();
  349. }
  350. write_project_declarations(project, solutionUuid) {
  351. this.p('Project("{' + solutionUuid.toUpperCase() + '}") = "' + project.get_safe_name() + '", "' + project.get_safe_name() + '.vcxproj", "{' + project.get_uuid().toString().toUpperCase() + '}"');
  352. if (project.getSubProjects().length > 0) {
  353. this.p('ProjectSection(ProjectDependencies) = postProject', 1);
  354. for (let proj of project.getSubProjects()) {
  355. this.p('{' + proj.get_uuid().toString().toUpperCase() + '} = {' + proj.get_uuid().toString().toUpperCase() + '}', 2);
  356. }
  357. this.p('EndProjectSection', 1);
  358. }
  359. this.p('EndProject');
  360. for (let proj of project.getSubProjects())
  361. this.write_project_declarations(proj, solutionUuid);
  362. }
  363. get_configs() {
  364. return ["Debug", "Develop", "Release"];
  365. }
  366. get_systems() {
  367. return ["x64"];
  368. }
  369. write_project_builds(project, platform) {
  370. for (let config of this.get_configs()) {
  371. for (let system of this.get_systems()) {
  372. this.p('{' + project.get_uuid().toString().toUpperCase() + '}.' + config + '|' + system + '.ActiveCfg = ' + config + '|' + system, 2);
  373. this.p('{' + project.get_uuid().toString().toUpperCase() + '}.' + config + '|' + system + '.Build.0 = ' + config + '|' + system, 2);
  374. }
  375. }
  376. for (let proj of project.getSubProjects())
  377. this.write_project_builds(proj, platform);
  378. }
  379. export_solution(project) {
  380. let from = path_resolve(".");
  381. let to = path_resolve("build");
  382. let platform = goptions.target;
  383. this.write_file(path_resolve(to, project.get_safe_name() + '.sln'));
  384. if (goptions.visualstudio === 'vs2022') {
  385. this.p('Microsoft Visual Studio Solution File, Format Version 12.00');
  386. this.p('# Visual Studio Version 17');
  387. this.p('VisualStudioVersion = 17.0.31903.59');
  388. this.p('MinimumVisualStudioVersion = 10.0.40219.1');
  389. }
  390. let solutionUuid = crypto_random_uuid();
  391. this.write_project_declarations(project, solutionUuid);
  392. this.p('Global');
  393. this.p('GlobalSection(SolutionConfigurationPlatforms) = preSolution', 1);
  394. for (let config of this.get_configs()) {
  395. for (let system of this.get_systems()) {
  396. this.p(config + '|' + system + ' = ' + config + '|' + system, 2);
  397. }
  398. }
  399. this.p('EndGlobalSection', 1);
  400. this.p('GlobalSection(ProjectConfigurationPlatforms) = postSolution', 1);
  401. this.write_project_builds(project, platform);
  402. this.p('EndGlobalSection', 1);
  403. this.p('GlobalSection(SolutionProperties) = preSolution', 1);
  404. this.p('HideSolutionNode = FALSE', 2);
  405. this.p('EndGlobalSection', 1);
  406. this.p('EndGlobal');
  407. this.close_file();
  408. this.export_project(from, to, project, platform, false, goptions);
  409. this.export_filters(from, to, project, platform);
  410. this.export_user_file(from, to, project, platform);
  411. if (platform === 'windows') {
  412. this.export_resource_script(to);
  413. export_ico(project.icon, path_resolve(to, 'icon.ico'), from);
  414. }
  415. }
  416. export_resource_script(to) {
  417. this.write_file(path_resolve(to, "resources.rc"));
  418. this.p('107 ICON "icon.ico"');
  419. this.close_file();
  420. }
  421. pretty_dir(dir) {
  422. let pretty_dir = dir;
  423. while (pretty_dir.startsWith("../")) {
  424. pretty_dir = pretty_dir.substring(3);
  425. }
  426. return pretty_dir.replace(/\//g, "\\");
  427. }
  428. item_group(from, to, project, type, filter) {
  429. let lastdir = "";
  430. this.p('<ItemGroup>', 1);
  431. for (let file of project.getFiles()) {
  432. let dir = get_dir(file);
  433. if (dir !== lastdir)
  434. lastdir = dir;
  435. if (filter(file)) {
  436. let filepath = "";
  437. if (project.noFlatten && !path_isabs(file.file)) {
  438. filepath = path_resolve(path_join(project.basedir, file.file));
  439. }
  440. else {
  441. filepath = this.nice_path(from, to, file.file);
  442. }
  443. this.p('<' + type + ' Include="' + filepath + '">', 2);
  444. this.p('<Filter>' + this.pretty_dir(dir) + '</Filter>', 3);
  445. this.p('</' + type + '>', 2);
  446. }
  447. }
  448. this.p('</ItemGroup>', 1);
  449. }
  450. export_filters(from, to, project, platform) {
  451. for (let proj of project.getSubProjects())
  452. this.export_filters(from, to, proj, platform);
  453. this.write_file(path_resolve(to, project.get_safe_name() + '.vcxproj.filters'));
  454. this.p('<?xml version="1.0" encoding="utf-8"?>');
  455. this.p('<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">');
  456. let lastdir = '';
  457. let dirs = [];
  458. for (let file of project.getFiles()) {
  459. let dir = get_dir(file);
  460. if (dir !== lastdir) {
  461. let subdir = dir;
  462. while (subdir.indexOf('/') >= 0) {
  463. subdir = subdir.substr(0, subdir.lastIndexOf('/'));
  464. if (!dirs.includes(subdir))
  465. dirs.push(subdir);
  466. }
  467. dirs.push(dir);
  468. lastdir = dir;
  469. }
  470. }
  471. let assets = [];
  472. this.p('<ItemGroup>', 1);
  473. for (let dir of dirs) {
  474. let pretty = this.pretty_dir(dir);
  475. if (pretty !== '..') {
  476. this.p('<Filter Include="' + pretty + '">', 2);
  477. this.p('<UniqueIdentifier>{' + crypto_random_uuid().toString().toUpperCase() + '}</UniqueIdentifier>', 3);
  478. this.p('</Filter>', 2);
  479. }
  480. }
  481. this.p('</ItemGroup>', 1);
  482. this.item_group(from, to, project, 'ClInclude', (file) => {
  483. return file.file.endsWith(".h");
  484. });
  485. this.item_group(from, to, project, 'ClCompile', (file) => {
  486. return file.file.endsWith(".cpp") || file.file.endsWith(".c") || file.file.endsWith(".cc");
  487. });
  488. this.item_group(from, to, project, 'CustomBuild', (file) => {
  489. return file.file.endsWith(".hlsl");
  490. });
  491. if (platform === "windows") {
  492. this.item_group(from, to, project, "ResourceCompile", (file) => {
  493. return file.file.endsWith(".rc");
  494. });
  495. }
  496. this.p('</Project>');
  497. this.close_file();
  498. }
  499. get_platform_toolset() {
  500. // return 'v143';
  501. return 'ClangCL';
  502. }
  503. configuration(config, indent, project) {
  504. this.p('<PropertyGroup Condition="\'$(Configuration)\'==\'' + config + '\'" Label="Configuration">', indent);
  505. this.p('<ConfigurationType>Application</ConfigurationType>', indent + 1);
  506. this.p('<UseDebugLibraries>' + (config === "Release" ? "false" : "true") + '</UseDebugLibraries>', indent + 1);
  507. this.p('<PlatformToolset>' + this.get_platform_toolset() + '</PlatformToolset>', indent + 1);
  508. this.p('<PreferredToolArchitecture>x64</PreferredToolArchitecture>', indent + 1);
  509. if (config === "Release" && project.lto) {
  510. this.p('<WholeProgramOptimization>true</WholeProgramOptimization>', indent + 1);
  511. }
  512. this.p('<CharacterSet>Unicode</CharacterSet>', indent + 1);
  513. this.p('</PropertyGroup>', indent);
  514. }
  515. get_optimization(config) {
  516. switch (config) {
  517. case "Debug":
  518. default:
  519. return "Disabled";
  520. case "Develop":
  521. return "Full";
  522. case "Release":
  523. return "MaxSpeed";
  524. }
  525. }
  526. cStd(project) {
  527. switch (project.cStd.toLowerCase()) {
  528. case "c99":
  529. return "";
  530. case "c11":
  531. return "stdc11";
  532. case "c17":
  533. return "stdc17";
  534. case "c2x":
  535. return "stdc17";
  536. }
  537. }
  538. cppStd(project) {
  539. switch (project.cppStd.toLowerCase()) {
  540. case "c++17":
  541. return "stdcpp17";
  542. case "c++23":
  543. return "stdcpplatest";
  544. }
  545. }
  546. item_definition(config, system, includes, debugDefines, releaseDefines, indent, debuglibs, releaselibs, project) {
  547. this.p('<ItemDefinitionGroup Condition="\'$(Configuration)|$(Platform)\'==\'' + config + '|' + system + '\'">', indent);
  548. this.p('<ClCompile>', indent + 1);
  549. this.p('<AdditionalIncludeDirectories>' + includes + '</AdditionalIncludeDirectories>', indent + 2);
  550. this.p('<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>', indent + 2);
  551. this.p('<WarningLevel>Level3</WarningLevel>', indent + 2);
  552. this.p('<Optimization>' + this.get_optimization(config) + '</Optimization>', indent + 2);
  553. if (config === 'Release') {
  554. this.p('<FunctionLevelLinking>true</FunctionLevelLinking>', indent + 2);
  555. this.p('<IntrinsicFunctions>true</IntrinsicFunctions>', indent + 2);
  556. }
  557. this.p('<PreprocessorDefinitions>' + (config === 'Release' ? releaseDefines : debugDefines) + ((system === 'x64') ? 'SYS_64;' : '') + 'WIN32;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>', indent + 2);
  558. this.p('<RuntimeLibrary>' + (config === 'Release' ? 'MultiThreaded' : 'MultiThreadedDebug') + '</RuntimeLibrary>', indent + 2);
  559. this.p('<MultiProcessorCompilation>true</MultiProcessorCompilation>', indent + 2);
  560. this.p('<MinimalRebuild>false</MinimalRebuild>', indent + 2);
  561. if (config === 'Develop') {
  562. this.p('<BasicRuntimeChecks>Default</BasicRuntimeChecks>', indent + 2);
  563. }
  564. let cStd = this.cStd(project);
  565. this.p('<LanguageStandard_C>' + cStd + '</LanguageStandard_C>', indent + 2);
  566. let cppStd = this.cppStd(project);
  567. this.p('<LanguageStandard>' + cppStd + '</LanguageStandard>', indent + 2);
  568. this.p('</ClCompile>', indent + 1);
  569. this.p('<Link>', indent + 1);
  570. if (project.name == "amake") { // TODO
  571. this.p('<SubSystem>Console</SubSystem>', indent + 2);
  572. }
  573. else {
  574. this.p('<SubSystem>Windows</SubSystem>', indent + 2);
  575. }
  576. this.p('<GenerateDebugInformation>true</GenerateDebugInformation>', indent + 2);
  577. if (config === 'Release') {
  578. this.p('<EnableCOMDATFolding>true</EnableCOMDATFolding>', indent + 2);
  579. this.p('<OptimizeReferences>true</OptimizeReferences>', indent + 2);
  580. }
  581. let libs = config === 'Release' ? releaselibs : debuglibs;
  582. this.p('<AdditionalDependencies>' + libs + 'kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>', indent + 2);
  583. this.p('</Link>', indent + 1);
  584. this.p('<Manifest>', indent + 1);
  585. this.p('<EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>', indent + 2);
  586. this.p('</Manifest>', indent + 1);
  587. this.p('</ItemDefinitionGroup>', indent);
  588. }
  589. globals(indent) {
  590. if (goptions.visualstudio === 'vs2022') {
  591. this.p('<VCProjectVersion>16.0</VCProjectVersion>', indent);
  592. this.p('<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>', indent);
  593. }
  594. }
  595. extension_settings(indent) {
  596. this.p('<Import Project="$(VCTargetsPath)\\BuildCustomizations\\masm.props" />', indent);
  597. }
  598. extension_targets(indent) {
  599. this.p('<Import Project="$(VCTargetsPath)\\BuildCustomizations\\masm.targets"/>', indent);
  600. }
  601. export_project(from, to, project, platform, cmd, options) {
  602. for (let proj of project.getSubProjects()) {
  603. this.export_project(from, to, proj, platform, cmd, options);
  604. }
  605. this.write_file(path_resolve(to, project.get_safe_name() + '.vcxproj'));
  606. this.p('<?xml version="1.0" encoding="utf-8"?>');
  607. this.p('<Project DefaultTargets="Build" ' + 'xmlns="http://schemas.microsoft.com/developer/msbuild/2003">');
  608. this.p('<ItemGroup Label="ProjectConfigurations">', 1);
  609. for (let system of this.get_systems()) {
  610. for (let config of this.get_configs()) {
  611. this.p('<ProjectConfiguration Include="' + config + '|' + system + '">', 2);
  612. this.p('<Configuration>' + config + '</Configuration>', 3);
  613. this.p('<Platform>' + system + '</Platform>', 3);
  614. this.p('</ProjectConfiguration>', 2);
  615. }
  616. }
  617. this.p('</ItemGroup>', 1);
  618. this.p('<PropertyGroup Label="Globals">', 1);
  619. this.p('<ProjectGuid>{' + project.get_uuid().toString().toUpperCase() + '}</ProjectGuid>', 2);
  620. this.globals(2);
  621. this.p('</PropertyGroup>', 1);
  622. this.p('<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.Default.props" />', 1);
  623. for (let config of this.get_configs()) {
  624. for (let system of this.get_systems()) {
  625. this.configuration(config, 1, project);
  626. }
  627. }
  628. this.p('<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props" />', 1);
  629. this.p('<ImportGroup Label="ExtensionSettings">', 1);
  630. this.extension_settings(2);
  631. this.p('</ImportGroup>', 1);
  632. this.p('<PropertyGroup Label="UserMacros" />', 1);
  633. if (project.get_executable_name()) {
  634. this.p('<PropertyGroup>', 1);
  635. this.p('<TargetName>' + project.get_executable_name() + '</TargetName>', 2);
  636. this.p('</PropertyGroup>', 1);
  637. }
  638. if (platform === 'windows') {
  639. for (let system of this.get_systems()) {
  640. this.p('<ImportGroup Label="PropertySheets" Condition="\'$(Platform)\'==\'' + system + '\'">', 1);
  641. this.p('<Import Project="$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props" Condition="exists(\'$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\')" Label="LocalAppDataPlatform" />', 2);
  642. this.p('</ImportGroup>', 1);
  643. }
  644. }
  645. let debugDefines = "_DEBUG;";
  646. let releaseDefines = "NDEBUG;";
  647. for (let define of project.getDefines()) {
  648. debugDefines += define + ";";
  649. releaseDefines += define + ";";
  650. }
  651. let incstring = "";
  652. let includedirs = project.getIncludeDirs();
  653. for (let include of includedirs) {
  654. let relativized = path_relative(to, path_resolve(from, include));
  655. if (relativized === "") {
  656. relativized = ".";
  657. }
  658. incstring += relativized + ";";
  659. }
  660. if (incstring.length > 0)
  661. incstring = incstring.substr(0, incstring.length - 1);
  662. let debuglibs = "";
  663. for (let proj of project.getSubProjects()) {
  664. if (proj.noFlatten) {
  665. debuglibs += project.basedir + "\\build\\x64\\Debug\\" + proj.get_safe_name() + ".lib;";
  666. }
  667. else {
  668. debuglibs += "Debug\\" + proj.get_safe_name() + ".lib;";
  669. }
  670. }
  671. for (let lib of project.getLibs()) {
  672. if (fs_exists(path_resolve(from, lib + ".lib"))) {
  673. debuglibs += path_relative(to, path_resolve(from, lib)) + ".lib;";
  674. }
  675. else {
  676. debuglibs += lib + ".lib;";
  677. }
  678. }
  679. let releaselibs = "";
  680. for (let proj of project.getSubProjects()) {
  681. if (proj.noFlatten) {
  682. releaselibs += project.basedir + "\\build\\x64\\Release\\" + proj.get_safe_name() + ".lib;";
  683. }
  684. else {
  685. releaselibs += "Release\\" + proj.get_safe_name() + ".lib;";
  686. }
  687. }
  688. for (let proj of project.getSubProjects())
  689. releaselibs += "Release\\" + proj.get_safe_name() + ".lib;";
  690. for (let lib of project.getLibs()) {
  691. if (fs_exists(path_resolve(from, lib + ".lib"))) {
  692. releaselibs += path_relative(to, path_resolve(from, lib)) + ".lib;";
  693. }
  694. else {
  695. releaselibs += lib + ".lib;";
  696. }
  697. }
  698. for (let config of this.get_configs()) {
  699. for (let system of this.get_systems()) {
  700. this.item_definition(config, system, incstring, debugDefines, releaseDefines, 2, debuglibs, releaselibs, project);
  701. }
  702. }
  703. this.p('<ItemGroup>', 1);
  704. for (let file of project.getFiles()) {
  705. let filepath = "";
  706. if (project.noFlatten && !path_isabs(file.file)) {
  707. filepath = path_resolve(project.basedir + "/" + file.file);
  708. }
  709. else {
  710. filepath = this.nice_path(from, to, file.file);
  711. }
  712. if (file.file.endsWith(".h"))
  713. this.p('<ClInclude Include="' + filepath + '" />', 2);
  714. }
  715. this.p('</ItemGroup>', 1);
  716. this.p('<ItemGroup>', 1);
  717. let objects = {};
  718. for (let fileobject of project.getFiles()) {
  719. let file = fileobject.file;
  720. if (file.endsWith(".cpp") || file.endsWith(".c") || file.endsWith("cc")) {
  721. let name = file.toLowerCase();
  722. if (name.indexOf("/") >= 0)
  723. name = name.substr(name.lastIndexOf("/") + 1);
  724. name = name.substr(0, name.lastIndexOf("."));
  725. let filepath = "";
  726. if (project.noFlatten && !path_isabs(file)) {
  727. filepath = path_resolve(project.basedir + "/" + file);
  728. }
  729. else {
  730. filepath = this.nice_path(from, to, file);
  731. }
  732. if (!objects[name]) {
  733. this.p('<ClCompile Include="' + filepath + '" />', 2);
  734. objects[name] = true;
  735. }
  736. else {
  737. while (objects[name]) {
  738. name = name + '_';
  739. }
  740. this.p('<ClCompile Include="' + filepath + '">', 2);
  741. this.p('<ObjectFileName>$(IntDir)\\' + name + '.obj</ObjectFileName>', 3);
  742. this.p('</ClCompile>', 2);
  743. objects[name] = true;
  744. }
  745. }
  746. }
  747. this.p('</ItemGroup>', 1);
  748. this.p('<ItemGroup>', 1);
  749. for (let file of project.getFiles()) {
  750. if (file.file.endsWith('.natvis')) {
  751. this.p('<Natvis Include="' + this.nice_path(from, to, file.file) + '"/>', 2);
  752. }
  753. }
  754. this.p('</ItemGroup>', 1);
  755. if (platform === "windows") {
  756. this.p('<ItemGroup>', 1);
  757. for (let file of project.customs) {
  758. this.p('<CustomBuild Include="' + this.nice_path(from, to, file.file) + '">', 2);
  759. this.p('<FileType>Document</FileType>', 2);
  760. this.p('<Command>' + file.command + '</Command>', 2);
  761. this.p('<Outputs>' + file.output + '</Outputs>', 2);
  762. this.p('<Message>%(Filename)%(Extension)</Message>', 2);
  763. this.p('</CustomBuild>', 2);
  764. }
  765. this.p('</ItemGroup>');
  766. this.p('<ItemGroup>', 1);
  767. this.p('<None Include="icon.ico" />', 2);
  768. this.p('</ItemGroup>', 1);
  769. this.p('<ItemGroup>', 1);
  770. this.p('<ResourceCompile Include="resources.rc" />', 2);
  771. for (let file of project.getFiles()) {
  772. if (file.file.endsWith('.rc')) {
  773. this.p('<ResourceCompile Include="' + this.nice_path(from, to, file.file) + '" />', 2);
  774. }
  775. }
  776. this.p('</ItemGroup>', 1);
  777. }
  778. this.p('<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.targets" />', 1);
  779. this.p('<ImportGroup Label="ExtensionTargets">', 1);
  780. this.extension_targets(2);
  781. this.p('</ImportGroup>', 1);
  782. this.p('</Project>');
  783. this.close_file();
  784. }
  785. }
  786. class WasmExporter extends Exporter {
  787. constructor() {
  788. super();
  789. this.compile_commands = new CompilerCommandsExporter();
  790. let compiler = "clang";
  791. let compilerFlags = "--target=wasm32 -nostdlib -matomics -mbulk-memory";
  792. this.make = new MakeExporter(compiler, compiler, compilerFlags, compilerFlags, '--target=wasm32 -nostdlib -matomics -mbulk-memory "-Wl,--import-memory,--shared-memory"', '.wasm');
  793. }
  794. export_solution(project) {
  795. this.make.export_solution(project);
  796. this.compile_commands.export_solution(project);
  797. }
  798. }
  799. function new_path_id(path) {
  800. return uuidv5(path, "7448ebd8-cfc8-4f45-8b3d-5df577ceea6d").toUpperCase();
  801. }
  802. function get_dir2(file) {
  803. if (file.file.indexOf("/") >= 0) {
  804. let dir = file.file.substr(0, file.file.lastIndexOf("/"));
  805. return path_join(file.projectName, path_relative(file.projectDir, dir)).replace(/\\/g, "/");
  806. }
  807. else {
  808. return file.projectName;
  809. }
  810. }
  811. class Directory {
  812. constructor(dirname) {
  813. this.dirname = dirname;
  814. this.id = new_path_id(dirname);
  815. }
  816. getName() {
  817. return this.dirname;
  818. }
  819. getLastName() {
  820. if (this.dirname.indexOf("/") < 0)
  821. return this.dirname;
  822. return this.dirname.substr(this.dirname.lastIndexOf("/") + 1);
  823. }
  824. getId() {
  825. return this.id;
  826. }
  827. }
  828. class File {
  829. constructor(filename, dir) {
  830. this.filename = filename;
  831. this.dir = dir;
  832. this.buildid = new_path_id(dir + filename + "_buildid");
  833. this.fileid = new_path_id(dir + filename + "_fileid");
  834. }
  835. getBuildId() {
  836. return this.buildid;
  837. }
  838. getFileId() {
  839. return this.fileid;
  840. }
  841. isBuildFile() {
  842. return this.filename.endsWith(".c") || this.filename.endsWith(".cpp") || this.filename.endsWith(".m") || this.filename.endsWith(".mm") || this.filename.endsWith(".cc") || this.filename.endsWith(".metal");
  843. }
  844. getName() {
  845. return this.filename;
  846. }
  847. getLastName() {
  848. if (this.filename.indexOf("/") < 0)
  849. return this.filename;
  850. return this.filename.substr(this.filename.lastIndexOf("/") + 1);
  851. }
  852. get_dir() {
  853. return this.dir;
  854. }
  855. toString() {
  856. return this.getName();
  857. }
  858. }
  859. class Framework {
  860. constructor(name) {
  861. this.name = name;
  862. this.buildid = new_path_id(name + "_buildid");
  863. this.fileid = new_path_id(name + "_fileid");
  864. this.localPath = null;
  865. }
  866. toString() {
  867. if (this.name.indexOf(".") < 0)
  868. return this.name + ".framework";
  869. else
  870. return this.name;
  871. }
  872. getBuildId() {
  873. return this.buildid.toString().toUpperCase();
  874. }
  875. getFileId() {
  876. return this.fileid.toString().toUpperCase();
  877. }
  878. }
  879. function findDirectory(dirname, directories) {
  880. for (let dir of directories) {
  881. if (dir.getName() === dirname) {
  882. return dir;
  883. }
  884. }
  885. return null;
  886. }
  887. function addDirectory(dirname, directories) {
  888. let dir = findDirectory(dirname, directories);
  889. if (dir === null) {
  890. dir = new Directory(dirname);
  891. directories.push(dir);
  892. while (dirname.indexOf("/") >= 0) {
  893. dirname = dirname.substr(0, dirname.lastIndexOf("/"));
  894. addDirectory(dirname, directories);
  895. }
  896. }
  897. return dir;
  898. }
  899. class IconImage {
  900. constructor(idiom, size, scale) {
  901. this.idiom = idiom;
  902. this.size = size;
  903. this.scale = scale;
  904. }
  905. }
  906. class XCodeExporter extends Exporter {
  907. constructor() {
  908. super();
  909. }
  910. exportWorkspace(to, project) {
  911. let dir = path_resolve(to, project.get_safe_name() + ".xcodeproj", "project.xcworkspace");
  912. fs_ensuredir(dir);
  913. this.write_file(path_resolve(to, project.get_safe_name() + ".xcodeproj", "project.xcworkspace", "contents.xcworkspacedata"));
  914. this.p('<?xml version="1.0" encoding="UTF-8"?>');
  915. this.p('<Workspace');
  916. this.p('version = "1.0">');
  917. this.p('<FileRef');
  918. this.p('location = "self:' + project.get_safe_name() + '.xcodeproj">');
  919. this.p('</FileRef>');
  920. this.p('</Workspace>');
  921. this.close_file();
  922. }
  923. export_solution(project) {
  924. let from = path_resolve(".");
  925. let to = path_resolve("build");
  926. let platform = goptions.target;
  927. let xdir = path_resolve(to, project.get_safe_name() + ".xcodeproj");
  928. fs_ensuredir(xdir);
  929. this.exportWorkspace(to, project);
  930. function add_icons(icons, idiom, sizes, scales) {
  931. for (let i = 0; i < sizes.length; ++i) {
  932. icons.push(new IconImage(idiom, sizes[i], scales[i]));
  933. }
  934. }
  935. let icons = [];
  936. if (platform === "ios") {
  937. add_icons(icons, "iphone", [20, 20, 29, 29, 40, 40, 60, 60], [2, 3, 2, 3, 2, 3, 2, 3]);
  938. add_icons(icons, "ipad", [20, 20, 29, 29, 40, 40, 76, 76, 83.5], [1, 2, 1, 2, 1, 2, 1, 2, 2]);
  939. icons.push(new IconImage("ios-marketing", 1024, 1));
  940. }
  941. else {
  942. add_icons(icons, "mac", [16, 16, 32, 32, 128, 128, 256, 256, 512, 512], [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
  943. }
  944. let iconsdir = path_resolve(to, "Images.xcassets", "AppIcon.appiconset");
  945. fs_ensuredir(iconsdir);
  946. this.write_file(path_resolve(to, "Images.xcassets", "AppIcon.appiconset", "Contents.json"));
  947. this.p('{');
  948. this.p('"images" : [', 1);
  949. for (let i = 0; i < icons.length; ++i) {
  950. let icon = icons[i];
  951. this.p('{', 2);
  952. this.p('"idiom" : "' + icon.idiom + '",', 3);
  953. this.p('"size" : "' + icon.size + 'x' + icon.size + '",', 3);
  954. this.p('"filename" : "' + icon.idiom + icon.scale + 'x' + icon.size + '.png",', 3);
  955. this.p('"scale" : "' + icon.scale + 'x"', 3);
  956. if (i === icons.length - 1)
  957. this.p('}', 2);
  958. else
  959. this.p('},', 2);
  960. }
  961. this.p('],', 1);
  962. this.p('"info" : {', 1);
  963. this.p('"version" : 1,', 2);
  964. this.p('"author" : "xcode"', 2);
  965. this.p('}', 1);
  966. this.p('}');
  967. this.close_file();
  968. for (let i = 0; i < icons.length; ++i) {
  969. let icon = icons[i];
  970. export_png_icon(project.icon, path_resolve(to, 'Images.xcassets', 'AppIcon.appiconset', icon.idiom + icon.scale + 'x' + icon.size + '.png'), icon.size * icon.scale, icon.size * icon.scale, from);
  971. }
  972. let plistname = "";
  973. let files = [];
  974. let directories = [];
  975. for (let fileobject of project.getFiles()) {
  976. let filename = fileobject.file;
  977. if (filename.endsWith(".plist")) {
  978. plistname = filename;
  979. }
  980. let dir = addDirectory(get_dir2(fileobject), directories);
  981. let file = new File(filename, dir);
  982. files.push(file);
  983. }
  984. if (plistname.length === 0) {
  985. console.log("Error: no plist found");
  986. }
  987. let frameworks = [];
  988. for (let lib of project.getLibs()) {
  989. frameworks.push(new Framework(lib));
  990. }
  991. let target_options = {
  992. bundle: 'org.$(PRODUCT_NAME)',
  993. version: "1.0",
  994. build: "1",
  995. organizationName: "Armory3D",
  996. developmentTeam: ""
  997. };
  998. if (project.target_options && project.target_options.ios) {
  999. let userOptions = project.target_options.ios;
  1000. if (userOptions.bundle)
  1001. target_options.bundle = userOptions.bundle;
  1002. if (userOptions.version)
  1003. target_options.version = userOptions.version;
  1004. if (userOptions.build)
  1005. target_options.build = userOptions.build;
  1006. if (userOptions.organizationName)
  1007. target_options.organizationName = userOptions.organizationName;
  1008. if (userOptions.developmentTeam)
  1009. target_options.developmentTeam = userOptions.developmentTeam;
  1010. }
  1011. let projectId = new_path_id("_projectId");
  1012. let appFileId = new_path_id("_appFileId");
  1013. let frameworkBuildId = new_path_id("_frameworkBuildId");
  1014. let sourceBuildId = new_path_id("_sourceBuildId");
  1015. let frameworksGroupId = new_path_id("_frameworksGroupId");
  1016. let productsGroupId = new_path_id("_productsGroupId");
  1017. let mainGroupId = new_path_id("_mainGroupId");
  1018. let targetId = new_path_id("_targetId");
  1019. let nativeBuildConfigListId = new_path_id("_nativeBuildConfigListId");
  1020. let projectBuildConfigListId = new_path_id("_projectBuildConfigListId");
  1021. let debugId = new_path_id("_debugId");
  1022. let releaseId = new_path_id("_releaseId");
  1023. let nativeDebugId = new_path_id("_nativeDebugId");
  1024. let nativeReleaseId = new_path_id("_nativeReleaseId");
  1025. let debugDirFileId = new_path_id("_debugDirFileId");
  1026. let debugDirBuildId = new_path_id("_debugDirBuildId");
  1027. let resourcesBuildId = new_path_id("_resourcesBuildId");
  1028. let iconFileId = new_path_id("_iconFileId");
  1029. let iconBuildId = new_path_id("_iconBuildId");
  1030. this.write_file(path_resolve(to, project.get_safe_name() + ".xcodeproj", "project.pbxproj"));
  1031. this.p('// !$*UTF8*$!');
  1032. this.p('{');
  1033. this.p('archiveVersion = 1;', 1);
  1034. this.p('classes = {', 1);
  1035. this.p('};', 1);
  1036. this.p('objectVersion = 46;', 1);
  1037. this.p('objects = {', 1);
  1038. this.p();
  1039. this.p('/* Begin PBXBuildFile section */');
  1040. for (let framework of frameworks) {
  1041. this.p(framework.getBuildId() + ' /* ' + framework.toString() + ' in Frameworks */ = {isa = PBXBuildFile; fileRef = ' + framework.getFileId() + ' /* ' + framework.toString() + ' */; };', 2);
  1042. }
  1043. this.p(debugDirBuildId + ' /* Deployment in Resources */ = {isa = PBXBuildFile; fileRef = ' + debugDirFileId + ' /* Deployment */; };', 2);
  1044. for (let file of files) {
  1045. if (file.isBuildFile()) {
  1046. this.p(file.getBuildId() + ' /* ' + file.toString() + ' in Sources */ = {isa = PBXBuildFile; fileRef = ' + file.getFileId() + ' /* ' + file.toString() + ' */; };', 2);
  1047. }
  1048. }
  1049. this.p(iconBuildId + ' /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ' + iconFileId + ' /* Images.xcassets */; };', 2);
  1050. this.p('/* End PBXBuildFile section */');
  1051. this.p();
  1052. this.p('/* Begin PBXFileReference section */');
  1053. let executable_name = project.get_safe_name();
  1054. if (project.get_executable_name()) {
  1055. executable_name = project.get_executable_name();
  1056. }
  1057. this.p(appFileId + ' /* ' + project.get_safe_name() + '.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "' + executable_name + '.app"; sourceTree = BUILT_PRODUCTS_DIR; };', 2);
  1058. for (let framework of frameworks) {
  1059. if (framework.toString().endsWith('.framework')) {
  1060. // Local framework - a directory is specified
  1061. if (framework.toString().indexOf('/') >= 0) {
  1062. framework.localPath = path_resolve(from, framework.toString());
  1063. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ' + framework.toString() + '; path = ' + framework.localPath + '; sourceTree = "<absolute>"; };', 2);
  1064. }
  1065. // XCode framework
  1066. else {
  1067. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ' + framework.toString() + '; path = System/Library/Frameworks/' + framework.toString() + '; sourceTree = SDKROOT; };', 2);
  1068. }
  1069. }
  1070. else if (framework.toString().endsWith('.dylib')) {
  1071. if (framework.toString().indexOf('/') >= 0) {
  1072. framework.localPath = path_resolve(from, framework.toString());
  1073. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */ = {isa = PBXFileReference; lastKnownFileType = compiled.mach-o.dylib; name = ' + framework.toString() + '; path = ' + framework.localPath + '; sourceTree = "<absolute>"; };', 2);
  1074. }
  1075. else {
  1076. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */ = {isa = PBXFileReference; lastKnownFileType = compiled.mach-o.dylib; name = ' + framework.toString() + '; path = usr/lib/' + framework.toString() + '; sourceTree = SDKROOT; };', 2);
  1077. }
  1078. }
  1079. else {
  1080. framework.localPath = path_resolve(from, framework.toString());
  1081. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = ' + framework.toString() + '; path = ' + framework.localPath + '; sourceTree = "<group>"; };', 2);
  1082. }
  1083. }
  1084. this.p(debugDirFileId + ' /* Deployment */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Deployment; path = "' + path_resolve(from, project.get_debug_dir()) + '"; sourceTree = "<group>"; };', 2);
  1085. for (let file of files) {
  1086. let filetype = "unknown";
  1087. let fileencoding = "";
  1088. if (file.getName().endsWith(".plist"))
  1089. filetype = "text.plist.xml";
  1090. if (file.getName().endsWith(".h"))
  1091. filetype = "sourcecode.c.h";
  1092. if (file.getName().endsWith(".m"))
  1093. filetype = "sourcecode.c.objc";
  1094. if (file.getName().endsWith(".c"))
  1095. filetype = "sourcecode.c.c";
  1096. if (file.getName().endsWith(".cpp"))
  1097. filetype = "sourcecode.c.cpp";
  1098. if (file.getName().endsWith(".cc"))
  1099. filetype = "sourcecode.c.cpp";
  1100. if (file.getName().endsWith(".mm"))
  1101. filetype = "sourcecode.c.objcpp";
  1102. if (file.getName().endsWith(".metal")) {
  1103. filetype = "sourcecode.metal";
  1104. fileencoding = "fileEncoding = 4; ";
  1105. }
  1106. if (!file.getName().endsWith(".DS_Store")) {
  1107. this.p(file.getFileId() + ' /* ' + file.toString() + ' */ = {isa = PBXFileReference; ' + fileencoding + 'lastKnownFileType = ' + filetype + '; name = "' + file.getLastName() + '"; path = "' + path_resolve(from, file.toString()) + '"; sourceTree = "<group>"; };', 2);
  1108. }
  1109. }
  1110. this.p(iconFileId + ' /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };', 2);
  1111. this.p('/* End PBXFileReference section */');
  1112. this.p();
  1113. this.p('/* Begin PBXFrameworksBuildPhase section */');
  1114. this.p(frameworkBuildId + ' /* Frameworks */ = {', 2);
  1115. this.p('isa = PBXFrameworksBuildPhase;', 3);
  1116. this.p('buildActionMask = 2147483647;', 3);
  1117. this.p('files = (', 3);
  1118. for (let framework of frameworks) {
  1119. this.p(framework.getBuildId() + ' /* ' + framework.toString() + ' in Frameworks */,', 4);
  1120. }
  1121. this.p(');', 3);
  1122. this.p('runOnlyForDeploymentPostprocessing = 0;', 3);
  1123. this.p('};', 2);
  1124. this.p('/* End PBXFrameworksBuildPhase section */');
  1125. this.p();
  1126. this.p('/* Begin PBXGroup section */');
  1127. this.p(mainGroupId + ' = {', 2);
  1128. this.p('isa = PBXGroup;', 3);
  1129. this.p('children = (', 3);
  1130. this.p(iconFileId + ' /* Images.xcassets */,', 4);
  1131. this.p(debugDirFileId + ' /* Deployment */,', 4);
  1132. for (let dir of directories) {
  1133. if (dir.getName().indexOf('/') < 0)
  1134. this.p(dir.getId() + ' /* ' + dir.getName() + ' */,', 4);
  1135. }
  1136. this.p(frameworksGroupId + ' /* Frameworks */,', 4);
  1137. this.p(productsGroupId + ' /* Products */,', 4);
  1138. this.p(');', 3);
  1139. this.p('sourceTree = "<group>";', 3);
  1140. this.p('};', 2);
  1141. this.p(productsGroupId + ' /* Products */ = {', 2);
  1142. this.p('isa = PBXGroup;', 3);
  1143. this.p('children = (', 3);
  1144. this.p(appFileId + ' /* ' + project.get_safe_name() + '.app */,', 4);
  1145. this.p(');', 3);
  1146. this.p('name = Products;', 3);
  1147. this.p('sourceTree = "<group>";', 3);
  1148. this.p('};', 2);
  1149. this.p(frameworksGroupId + ' /* Frameworks */ = {', 2);
  1150. this.p('isa = PBXGroup;', 3);
  1151. this.p('children = (', 3);
  1152. for (let framework of frameworks) {
  1153. this.p(framework.getFileId() + ' /* ' + framework.toString() + ' */,', 4);
  1154. }
  1155. this.p(');', 3);
  1156. this.p('name = Frameworks;', 3);
  1157. this.p('sourceTree = "<group>";', 3);
  1158. this.p('};', 2);
  1159. for (let dir of directories) {
  1160. this.p(dir.getId() + ' /* ' + dir.getName() + ' */ = {', 2);
  1161. this.p('isa = PBXGroup;', 3);
  1162. this.p('children = (', 3);
  1163. for (let dir2 of directories) {
  1164. if (dir2 === dir)
  1165. continue;
  1166. if (dir2.getName().startsWith(dir.getName())) {
  1167. if (dir2.getName().substr(dir.getName().length + 1).indexOf('/') < 0)
  1168. this.p(dir2.getId() + ' /* ' + dir2.getName() + ' */,', 4);
  1169. }
  1170. }
  1171. for (let file of files) {
  1172. if (file.get_dir() === dir && !file.getName().endsWith('.DS_Store'))
  1173. this.p(file.getFileId() + ' /* ' + file.toString() + ' */,', 4);
  1174. }
  1175. this.p(');', 3);
  1176. if (dir.getName().indexOf('/') < 0) {
  1177. this.p('path = ../;', 3);
  1178. this.p('name = "' + dir.getLastName() + '";', 3);
  1179. }
  1180. else
  1181. this.p('name = "' + dir.getLastName() + '";', 3);
  1182. this.p('sourceTree = "<group>";', 3);
  1183. this.p('};', 2);
  1184. }
  1185. this.p('/* End PBXGroup section */');
  1186. this.p();
  1187. this.p('/* Begin PBXNativeTarget section */');
  1188. this.p(targetId + ' /* ' + project.get_safe_name() + ' */ = {', 2);
  1189. this.p('isa = PBXNativeTarget;', 3);
  1190. this.p('buildConfigurationList = ' + nativeBuildConfigListId + ' /* Build configuration list for PBXNativeTarget "' + project.get_safe_name() + '" */;', 3);
  1191. this.p('buildPhases = (', 3);
  1192. this.p(sourceBuildId + ' /* Sources */,', 4);
  1193. this.p(frameworkBuildId + ' /* Frameworks */,', 4);
  1194. this.p(resourcesBuildId + ' /* Resources */,', 4);
  1195. this.p(');', 3);
  1196. this.p('buildRules = (', 3);
  1197. this.p(');', 3);
  1198. this.p('dependencies = (', 3);
  1199. this.p(');', 3);
  1200. this.p('name = "' + project.getName() + '";', 3);
  1201. this.p('productName = "' + project.getName() + '";', 3);
  1202. this.p('productReference = ' + appFileId + ' /* ' + project.get_safe_name() + '.app */;', 3);
  1203. if (project.name == "amake") { // TODO
  1204. this.p('productType = "com.apple.product-type.tool";', 3);
  1205. }
  1206. else {
  1207. this.p('productType = "com.apple.product-type.application";', 3);
  1208. }
  1209. this.p('};', 2);
  1210. this.p('/* End PBXNativeTarget section */');
  1211. this.p();
  1212. this.p('/* Begin PBXProject section */');
  1213. this.p(projectId + ' /* Project object */ = {', 2);
  1214. this.p('isa = PBXProject;', 3);
  1215. this.p('attributes = {', 3);
  1216. this.p('LastUpgradeCheck = 1230;', 4);
  1217. this.p('ORGANIZATIONNAME = "' + target_options.organizationName + '";', 4);
  1218. this.p('TargetAttributes = {', 4);
  1219. this.p(targetId + ' = {', 5);
  1220. this.p('CreatedOnToolsVersion = 6.1.1;', 6);
  1221. if (target_options.developmentTeam) {
  1222. this.p('DevelopmentTeam = ' + target_options.developmentTeam + ';', 6);
  1223. }
  1224. this.p('};', 5);
  1225. this.p('};', 4);
  1226. this.p('};', 3);
  1227. this.p('buildConfigurationList = ' + projectBuildConfigListId + ' /* Build configuration list for PBXProject "' + project.get_safe_name() + '" */;', 3);
  1228. this.p('compatibilityVersion = "Xcode 3.2";', 3);
  1229. this.p('developmentRegion = en;', 3);
  1230. this.p('hasScannedForEncodings = 0;', 3);
  1231. this.p('knownRegions = (', 3);
  1232. this.p('en,', 4);
  1233. this.p('Base,', 4);
  1234. this.p(');', 3);
  1235. this.p('mainGroup = ' + mainGroupId + ';', 3);
  1236. this.p('productRefGroup = ' + productsGroupId + ' /* Products */;', 3);
  1237. this.p('projectDirPath = "";', 3);
  1238. this.p('projectRoot = "";', 3);
  1239. this.p('targets = (', 3);
  1240. this.p(targetId + ' /* ' + project.get_safe_name() + ' */,', 4);
  1241. this.p(');', 3);
  1242. this.p('};', 2);
  1243. this.p('/* End PBXProject section */');
  1244. this.p();
  1245. this.p('/* Begin PBXResourcesBuildPhase section */');
  1246. this.p(resourcesBuildId + ' /* Resources */ = {', 2);
  1247. this.p('isa = PBXResourcesBuildPhase;', 3);
  1248. this.p('buildActionMask = 2147483647;', 3);
  1249. this.p('files = (', 3);
  1250. this.p(debugDirBuildId + ' /* Deployment in Resources */,', 4);
  1251. this.p(iconBuildId + ' /* Images.xcassets in Resources */,', 4);
  1252. this.p(');', 3);
  1253. this.p('runOnlyForDeploymentPostprocessing = 0;', 3);
  1254. this.p('};', 2);
  1255. this.p('/* End PBXResourcesBuildPhase section */');
  1256. this.p();
  1257. this.p('/* Begin PBXSourcesBuildPhase section */');
  1258. this.p(sourceBuildId + ' /* Sources */ = {', 2);
  1259. this.p('isa = PBXSourcesBuildPhase;', 3);
  1260. this.p('buildActionMask = 2147483647;', 3);
  1261. this.p('files = (', 3);
  1262. for (let file of files) {
  1263. if (file.isBuildFile())
  1264. this.p(file.getBuildId() + ' /* ' + file.toString() + ' in Sources */,', 4);
  1265. }
  1266. this.p(');', 3);
  1267. this.p('runOnlyForDeploymentPostprocessing = 0;');
  1268. this.p('};');
  1269. this.p('/* End PBXSourcesBuildPhase section */');
  1270. this.p();
  1271. this.p('/* Begin XCBuildConfiguration section */');
  1272. this.p(debugId + ' /* Debug */ = {', 2);
  1273. this.p('isa = XCBuildConfiguration;', 3);
  1274. this.p('buildSettings = {', 3);
  1275. this.p('ALWAYS_SEARCH_USER_PATHS = NO;', 4);
  1276. this.p('CLANG_CXX_LANGUAGE_STANDARD = "' + project.cppStd + '";', 4);
  1277. this.p('CLANG_CXX_LIBRARY = "compiler-default";', 4);
  1278. this.p('CLANG_ENABLE_MODULES = YES;', 4);
  1279. this.p('CLANG_ENABLE_OBJC_ARC = YES;', 4);
  1280. this.p('CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;', 4);
  1281. this.p('CLANG_WARN_BOOL_CONVERSION = YES;', 4);
  1282. this.p('CLANG_WARN_COMMA = YES;', 4);
  1283. this.p('CLANG_WARN_CONSTANT_CONVERSION = YES;', 4);
  1284. this.p('CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;', 4);
  1285. this.p('CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;', 4);
  1286. this.p('CLANG_WARN_EMPTY_BODY = YES;', 4);
  1287. this.p('CLANG_WARN_ENUM_CONVERSION = YES;', 4);
  1288. this.p('CLANG_WARN_INFINITE_RECURSION = YES;', 4);
  1289. this.p('CLANG_WARN_INT_CONVERSION = YES;', 4);
  1290. this.p('CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;', 4);
  1291. this.p('CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;', 4);
  1292. this.p('CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;', 4);
  1293. this.p('CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;', 4);
  1294. this.p('CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;', 4);
  1295. this.p('CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;', 4);
  1296. this.p('CLANG_WARN_STRICT_PROTOTYPES = YES;', 4);
  1297. this.p('CLANG_WARN_SUSPICIOUS_MOVE = YES;', 4);
  1298. this.p('CLANG_WARN_UNREACHABLE_CODE = YES;', 4);
  1299. this.p('CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;', 4);
  1300. if (platform === 'ios') {
  1301. this.p('"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";', 4);
  1302. }
  1303. else {
  1304. this.p('CODE_SIGN_IDENTITY = "-";', 4);
  1305. // this.p('"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";', 4);
  1306. }
  1307. this.p('COPY_PHASE_STRIP = NO;', 4);
  1308. this.p('ENABLE_STRICT_OBJC_MSGSEND = YES;', 4);
  1309. this.p('ENABLE_TESTABILITY = YES;', 4);
  1310. this.p('GCC_C_LANGUAGE_STANDARD = "' + project.cStd + '";', 4);
  1311. this.p('GCC_DYNAMIC_NO_PIC = NO;', 4);
  1312. this.p('GCC_NO_COMMON_BLOCKS = YES;', 4);
  1313. this.p('GCC_OPTIMIZATION_LEVEL = 0;', 4);
  1314. this.p('GCC_PREPROCESSOR_DEFINITIONS = (', 4);
  1315. this.p('"DEBUG=1",', 5);
  1316. for (let define of project.getDefines()) {
  1317. if (define.indexOf('=') >= 0)
  1318. this.p('"' + define.replace(/\"/g, '\\\\\\"') + '",', 5);
  1319. else
  1320. this.p(define + ',', 5);
  1321. }
  1322. this.p('"$(inherited)",', 5);
  1323. this.p(');', 4);
  1324. this.p('GCC_SYMBOLS_PRIVATE_EXTERN = NO;', 4);
  1325. this.p('GCC_WARN_64_TO_32_BIT_CONVERSION = YES;', 4);
  1326. this.p('GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;', 4);
  1327. this.p('GCC_WARN_UNDECLARED_SELECTOR = YES;', 4);
  1328. this.p('GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;', 4);
  1329. this.p('GCC_WARN_UNUSED_FUNCTION = YES;', 4);
  1330. this.p('GCC_WARN_UNUSED_VARIABLE = YES;', 4);
  1331. if (platform === 'ios') {
  1332. this.p('IPHONEOS_DEPLOYMENT_TARGET = 16.0;', 4);
  1333. }
  1334. else {
  1335. this.p('MACOSX_DEPLOYMENT_TARGET = 13.0;', 4);
  1336. }
  1337. this.p('MTL_ENABLE_DEBUG_INFO = YES;', 4);
  1338. this.p('ONLY_ACTIVE_ARCH = YES;', 4);
  1339. if (platform === 'ios') {
  1340. this.p('SDKROOT = iphoneos;', 4);
  1341. this.p('TARGETED_DEVICE_FAMILY = "1,2";', 4);
  1342. }
  1343. else {
  1344. this.p('SDKROOT = macosx;', 4);
  1345. }
  1346. this.p('};', 3);
  1347. this.p('name = Debug;', 3);
  1348. this.p('};', 2);
  1349. this.p(releaseId + ' /* Release */ = {', 2);
  1350. this.p('isa = XCBuildConfiguration;', 3);
  1351. this.p('buildSettings = {', 3);
  1352. this.p('ALWAYS_SEARCH_USER_PATHS = NO;', 4);
  1353. this.p('CLANG_CXX_LANGUAGE_STANDARD = "' + project.cppStd + '";', 4);
  1354. this.p('CLANG_CXX_LIBRARY = "compiler-default";', 4);
  1355. this.p('CLANG_ENABLE_MODULES = YES;', 4);
  1356. this.p('CLANG_ENABLE_OBJC_ARC = YES;', 4);
  1357. this.p('CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;', 4);
  1358. this.p('CLANG_WARN_BOOL_CONVERSION = YES;', 4);
  1359. this.p('CLANG_WARN_COMMA = YES;', 4);
  1360. this.p('CLANG_WARN_CONSTANT_CONVERSION = YES;', 4);
  1361. this.p('CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;', 4);
  1362. this.p('CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;', 4);
  1363. this.p('CLANG_WARN_EMPTY_BODY = YES;', 4);
  1364. this.p('CLANG_WARN_ENUM_CONVERSION = YES;', 4);
  1365. this.p('CLANG_WARN_INFINITE_RECURSION = YES;', 4);
  1366. this.p('CLANG_WARN_INT_CONVERSION = YES;', 4);
  1367. this.p('CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;', 4);
  1368. this.p('CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;', 4);
  1369. this.p('CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;', 4);
  1370. this.p('CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;', 4);
  1371. this.p('CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;', 4);
  1372. this.p('CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;', 4);
  1373. this.p('CLANG_WARN_STRICT_PROTOTYPES = YES;', 4);
  1374. this.p('CLANG_WARN_SUSPICIOUS_MOVE = YES;', 4);
  1375. this.p('CLANG_WARN_UNREACHABLE_CODE = YES;', 4);
  1376. this.p('CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;', 4);
  1377. if (platform === 'ios') {
  1378. this.p('"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";', 4);
  1379. }
  1380. else {
  1381. this.p('CODE_SIGN_IDENTITY = "-";', 4);
  1382. // this.p('"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";', 4);
  1383. }
  1384. this.p('COPY_PHASE_STRIP = YES;', 4);
  1385. if (platform === 'macos') {
  1386. this.p('DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";', 4);
  1387. }
  1388. this.p('ENABLE_NS_ASSERTIONS = NO;', 4);
  1389. this.p('ENABLE_STRICT_OBJC_MSGSEND = YES;', 4);
  1390. this.p('GCC_C_LANGUAGE_STANDARD = "' + project.cStd + '";', 4);
  1391. this.p('GCC_NO_COMMON_BLOCKS = YES;', 4);
  1392. this.p('GCC_PREPROCESSOR_DEFINITIONS = (', 4);
  1393. this.p('NDEBUG,', 5);
  1394. for (let define of project.getDefines()) {
  1395. if (define.indexOf('=') >= 0)
  1396. this.p('"' + define.replace(/\"/g, '\\\\\\"') + '",', 5);
  1397. else
  1398. this.p(define + ',', 5);
  1399. }
  1400. this.p('"$(inherited)",', 5);
  1401. this.p(');', 4);
  1402. this.p('GCC_WARN_64_TO_32_BIT_CONVERSION = YES;', 4);
  1403. this.p('GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;', 4);
  1404. this.p('GCC_WARN_UNDECLARED_SELECTOR = YES;', 4);
  1405. this.p('GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;', 4);
  1406. this.p('GCC_WARN_UNUSED_FUNCTION = YES;', 4);
  1407. this.p('GCC_WARN_UNUSED_VARIABLE = YES;', 4);
  1408. if (platform === 'ios') {
  1409. this.p('IPHONEOS_DEPLOYMENT_TARGET = 16.0;', 4);
  1410. }
  1411. else {
  1412. this.p('MACOSX_DEPLOYMENT_TARGET = 13.0;', 4);
  1413. }
  1414. this.p('MTL_ENABLE_DEBUG_INFO = NO;', 4);
  1415. this.p('ONLY_ACTIVE_ARCH = YES;', 4);
  1416. if (platform === 'ios') {
  1417. this.p('SDKROOT = iphoneos;', 4);
  1418. this.p('TARGETED_DEVICE_FAMILY = "1,2";', 4);
  1419. this.p('VALIDATE_PRODUCT = YES;', 4);
  1420. }
  1421. else {
  1422. this.p('SDKROOT = macosx;', 4);
  1423. }
  1424. this.p('};', 3);
  1425. this.p('name = Release;', 3);
  1426. this.p('};', 2);
  1427. this.p(nativeDebugId + ' /* Debug */ = {', 2);
  1428. this.p('isa = XCBuildConfiguration;', 3);
  1429. this.p('buildSettings = {', 3);
  1430. this.p('ARCHS = arm64;', 4);
  1431. this.p('ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;', 4);
  1432. this.p('CODE_SIGN_STYLE = Automatic;', 4);
  1433. if (platform === 'macos') {
  1434. this.p('COMBINE_HIDPI_IMAGES = YES;', 4);
  1435. }
  1436. this.p('ENABLE_HARDENED_RUNTIME = YES;', 4);
  1437. this.p('FRAMEWORK_SEARCH_PATHS = (', 4);
  1438. this.p('"$(inherited)",', 5);
  1439. // Search paths to local frameworks
  1440. for (let framework of frameworks) {
  1441. if (framework.localPath != null)
  1442. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1443. }
  1444. this.p(');', 4);
  1445. this.p('HEADER_SEARCH_PATHS = (', 4);
  1446. this.p('"$(inherited)",', 5);
  1447. this.p('"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",', 5);
  1448. for (let projectpath of project.getIncludeDirs())
  1449. this.p('"' + path_resolve(from, projectpath).replace(/ /g, '\\\\ ') + '",', 5);
  1450. this.p(');', 4);
  1451. this.p('LIBRARY_SEARCH_PATHS = (', 4);
  1452. for (let framework of frameworks) {
  1453. if ((framework.toString().endsWith('.dylib') || framework.toString().endsWith('.a')) && framework.localPath != null) {
  1454. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1455. }
  1456. }
  1457. this.p(');', 4);
  1458. this.p('INFOPLIST_EXPAND_BUILD_SETTINGS = "YES";', 4);
  1459. this.p('INFOPLIST_FILE = "' + path_resolve(from, plistname) + '";', 4);
  1460. this.p('LD_RUNPATH_SEARCH_PATHS = (', 4);
  1461. this.p('"$(inherited)",', 5);
  1462. if (platform === 'ios') {
  1463. this.p('"@executable_path/Frameworks",', 5);
  1464. }
  1465. for (let framework of frameworks) {
  1466. if (framework.toString().endsWith('.dylib') && framework.localPath != null) {
  1467. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1468. }
  1469. }
  1470. this.p(');', 4);
  1471. if (project.cFlags.length > 0) {
  1472. this.p('OTHER_CFLAGS = (', 4);
  1473. for (let cFlag of project.cFlags) {
  1474. this.p('"' + cFlag + '",', 5);
  1475. }
  1476. this.p(');', 4);
  1477. }
  1478. if (project.cppFlags.length > 0) {
  1479. this.p('OTHER_CPLUSPLUSFLAGS = (', 4);
  1480. for (let cppFlag of project.cppFlags) {
  1481. this.p('"' + cppFlag + '",', 5);
  1482. }
  1483. this.p(');', 4);
  1484. }
  1485. this.p('PRODUCT_BUNDLE_IDENTIFIER = "' + target_options.bundle + '";', 4);
  1486. this.p('BUNDLE_VERSION = "' + target_options.version + '";', 4);
  1487. this.p('BUILD_VERSION = "' + target_options.build + '";', 4);
  1488. this.p('CODE_SIGN_IDENTITY = "-";', 4);
  1489. this.p('PRODUCT_NAME = "$(TARGET_NAME)";', 4);
  1490. this.p('};', 3);
  1491. this.p('name = Debug;', 3);
  1492. this.p('};', 2);
  1493. this.p(nativeReleaseId + ' /* Release */ = {', 2);
  1494. this.p('isa = XCBuildConfiguration;', 3);
  1495. this.p('buildSettings = {', 3);
  1496. this.p('ARCHS = arm64;', 4);
  1497. this.p('ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;', 4);
  1498. this.p('CODE_SIGN_STYLE = Automatic;', 4);
  1499. if (platform === 'macos') {
  1500. this.p('COMBINE_HIDPI_IMAGES = YES;', 4);
  1501. }
  1502. this.p('ENABLE_HARDENED_RUNTIME = YES;', 4);
  1503. this.p('FRAMEWORK_SEARCH_PATHS = (', 4);
  1504. this.p('"$(inherited)",', 5);
  1505. // Search paths to local frameworks
  1506. for (let framework of frameworks) {
  1507. if (framework.localPath != null)
  1508. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1509. }
  1510. this.p(');', 4);
  1511. this.p('HEADER_SEARCH_PATHS = (', 4);
  1512. this.p('"$(inherited)",', 5);
  1513. this.p('"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",', 5);
  1514. for (let p of project.getIncludeDirs())
  1515. this.p('"' + path_resolve(from, p).replace(/ /g, '\\\\ ') + '",', 5);
  1516. this.p(');', 4);
  1517. this.p('LIBRARY_SEARCH_PATHS = (', 4);
  1518. for (let framework of frameworks) {
  1519. if ((framework.toString().endsWith('.dylib') || framework.toString().endsWith('.a')) && framework.localPath != null) {
  1520. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1521. }
  1522. }
  1523. this.p(');', 4);
  1524. this.p('INFOPLIST_EXPAND_BUILD_SETTINGS = "YES";', 4);
  1525. this.p('INFOPLIST_FILE = "' + path_resolve(from, plistname) + '";', 4);
  1526. this.p('LD_RUNPATH_SEARCH_PATHS = (', 4);
  1527. this.p('"$(inherited)",', 5);
  1528. if (platform === 'ios') {
  1529. this.p('"@executable_path/Frameworks",', 5);
  1530. }
  1531. for (let framework of frameworks) {
  1532. if (framework.toString().endsWith('.dylib') && framework.localPath != null) {
  1533. this.p(framework.localPath.substr(0, framework.localPath.lastIndexOf('/')) + ',', 5);
  1534. }
  1535. }
  1536. this.p(');', 4);
  1537. if (project.cFlags.length > 0) {
  1538. this.p('OTHER_CFLAGS = (', 4);
  1539. for (let cFlag of project.cFlags) {
  1540. this.p('"' + cFlag + '",', 5);
  1541. }
  1542. this.p(');', 4);
  1543. }
  1544. if (project.cppFlags.length > 0) {
  1545. this.p('OTHER_CPLUSPLUSFLAGS = (', 4);
  1546. for (let cppFlag of project.cppFlags) {
  1547. this.p('"' + cppFlag + '",', 5);
  1548. }
  1549. this.p(');', 4);
  1550. }
  1551. this.p('PRODUCT_BUNDLE_IDENTIFIER = "' + target_options.bundle + '";', 4);
  1552. this.p('BUNDLE_VERSION = "' + target_options.version + '";', 4);
  1553. this.p('BUILD_VERSION = "' + target_options.build + '";', 4);
  1554. this.p('CODE_SIGN_IDENTITY = "-";', 4);
  1555. this.p('PRODUCT_NAME = "$(TARGET_NAME)";', 4);
  1556. this.p('};', 3);
  1557. this.p('name = Release;', 3);
  1558. this.p('};', 2);
  1559. this.p('/* End XCBuildConfiguration section */');
  1560. this.p();
  1561. this.p('/* Begin XCConfigurationList section */');
  1562. this.p(projectBuildConfigListId + ' /* Build configuration list for PBXProject "' + project.get_safe_name() + '" */ = {', 2);
  1563. this.p('isa = XCConfigurationList;', 3);
  1564. this.p('buildConfigurations = (', 3);
  1565. this.p(debugId + ' /* Debug */,', 4);
  1566. this.p(releaseId + ' /* Release */,', 4);
  1567. this.p(');', 3);
  1568. this.p('defaultConfigurationIsVisible = 0;', 3);
  1569. this.p('defaultConfigurationName = Release;', 3);
  1570. this.p('};', 2);
  1571. this.p(nativeBuildConfigListId + ' /* Build configuration list for PBXNativeTarget "' + project.get_safe_name() + '" */ = {', 2);
  1572. this.p('isa = XCConfigurationList;', 3);
  1573. this.p('buildConfigurations = (', 3);
  1574. this.p(nativeDebugId + ' /* Debug */,', 4);
  1575. this.p(nativeReleaseId + ' /* Release */,', 4);
  1576. this.p(');', 3);
  1577. this.p('defaultConfigurationIsVisible = 0;', 3);
  1578. this.p('defaultConfigurationName = Release;', 3);
  1579. this.p('};', 2);
  1580. this.p('/* End XCConfigurationList section */');
  1581. this.p('};', 1);
  1582. this.p('rootObject = ' + projectId + ' /* Project object */;', 1);
  1583. this.p('}');
  1584. this.close_file();
  1585. }
  1586. }
  1587. class MakeExporter extends Exporter {
  1588. constructor(ccompiler, cppcompiler, cFlags, cppFlags, linkerFlags, outputExtension, libsLine = null) {
  1589. super();
  1590. if (ccompiler == "tcc") {
  1591. let tccdir = makedir + "/tcc"
  1592. linkerFlags = "-lc -lm -pthread";
  1593. ccompiler = tccdir + "/bin/" + sys_dir() + "/tcc";
  1594. ccompiler += " -DSTBI_NO_SIMD -DSINFL_NO_SIMD -DIRON_NOSIMD -w";
  1595. ccompiler += " -I" + tccdir + "/include -B" + tccdir + "/bin/" + sys_dir();
  1596. cppcompiler = ccompiler;
  1597. }
  1598. this.ccompiler = ccompiler;
  1599. this.cppcompiler = cppcompiler;
  1600. this.cFlags = cFlags;
  1601. this.cppFlags = cppFlags;
  1602. this.linkerFlags = linkerFlags;
  1603. this.outputExtension = outputExtension;
  1604. if (libsLine != null) {
  1605. this.libsLine = libsLine;
  1606. }
  1607. }
  1608. libsLine(project) {
  1609. let libs = "";
  1610. for (let lib of project.getLibs()) {
  1611. libs += " -l" + lib;
  1612. }
  1613. return libs;
  1614. }
  1615. export_solution(project) {
  1616. let from = path_resolve(".");
  1617. let to = path_resolve("build");
  1618. let objects = {};
  1619. let ofiles = {};
  1620. let output_path = path_resolve(to, goptions.build_path);
  1621. fs_ensuredir(output_path);
  1622. for (let fileobject of project.getFiles()) {
  1623. let file = fileobject.file;
  1624. if (file.endsWith(".cpp") || file.endsWith(".c") || file.endsWith(".cc")) {
  1625. let name = file.toLowerCase();
  1626. if (name.indexOf("/") >= 0) {
  1627. name = name.substr(name.lastIndexOf("/") + 1);
  1628. }
  1629. name = name.substr(0, name.lastIndexOf("."));
  1630. if (!objects[name]) {
  1631. objects[name] = true;
  1632. ofiles[file] = name;
  1633. }
  1634. else {
  1635. while (objects[name]) {
  1636. name = name + "_";
  1637. }
  1638. objects[name] = true;
  1639. ofiles[file] = name;
  1640. }
  1641. }
  1642. }
  1643. let ofilelist = "";
  1644. for (let o in objects) {
  1645. ofilelist += o + ".o ";
  1646. }
  1647. this.write_file(path_resolve(output_path, "makefile"));
  1648. let incline = "-I./ "; // local directory to pick up the precompiled headers
  1649. for (let inc of project.getIncludeDirs()) {
  1650. inc = path_relative(output_path, path_resolve(from, inc));
  1651. incline += "-I" + inc + " ";
  1652. }
  1653. this.p("INC=" + incline);
  1654. this.p("LIB=" + this.linkerFlags + this.libsLine(project));
  1655. let defline = "";
  1656. for (let def of project.getDefines()) {
  1657. defline += "-D" + def.replace(/\"/g, '\\"') + " ";
  1658. }
  1659. if (!goptions.debug) {
  1660. defline += "-DNDEBUG ";
  1661. }
  1662. this.p("DEF=" + defline);
  1663. this.p();
  1664. let cline = this.cFlags;
  1665. cline = "-std=" + project.cStd + " ";
  1666. for (let flag of project.cFlags) {
  1667. cline += flag + ' ';
  1668. }
  1669. this.p("CFLAGS=" + cline);
  1670. let cppline = this.cppFlags;
  1671. cppline = "-std=" + project.cppStd + " ";
  1672. for (let flag of project.cppFlags) {
  1673. cppline += flag + " ";
  1674. }
  1675. this.p("CPPFLAGS=" + cppline);
  1676. let optimization = "";
  1677. if (!goptions.debug) {
  1678. optimization = "-O2";
  1679. }
  1680. else
  1681. optimization = "-g";
  1682. let executable_name = project.get_safe_name();
  1683. if (project.get_executable_name()) {
  1684. executable_name = project.get_executable_name();
  1685. }
  1686. this.p(executable_name + this.outputExtension + ": " + ofilelist);
  1687. let output = '-o "' + executable_name + this.outputExtension + '"';
  1688. this.p('\t' + this.cppcompiler + ' ' + output + ' ' + optimization + ' ' + ofilelist + ' $(LIB)');
  1689. for (let fileobject of project.getFiles()) {
  1690. let file = fileobject.file;
  1691. if (file.endsWith(".c") || file.endsWith(".cpp") || file.endsWith(".cc")) {
  1692. this.p();
  1693. let name = ofiles[file];
  1694. let realfile = path_relative(output_path, path_resolve(from, file));
  1695. this.p("-include " + name + ".d");
  1696. this.p(name + ".o: " + realfile);
  1697. let compiler = this.cppcompiler;
  1698. let flags = '$(CPPFLAGS)';
  1699. if (file.endsWith(".c")) {
  1700. compiler = this.ccompiler;
  1701. flags = '$(CFLAGS)';
  1702. }
  1703. this.p('\t' + compiler + ' ' + optimization + ' $(INC) $(DEF) -MD ' + flags + ' -c ' + realfile + ' -o ' + name + '.o');
  1704. }
  1705. }
  1706. this.close_file();
  1707. }
  1708. }
  1709. class LinuxExporter extends Exporter {
  1710. constructor() {
  1711. super();
  1712. let compilerFlags = "";
  1713. let linkerFlags = "-static-libgcc -static-libstdc++ -pthread";
  1714. if (goptions.ccompiler == "gcc") {
  1715. compilerFlags += "-flto";
  1716. linkerFlags += " -flto";
  1717. }
  1718. this.make = new MakeExporter(goptions.ccompiler, goptions.cppcompiler, compilerFlags, compilerFlags, linkerFlags, '');
  1719. this.compile_commands = new CompilerCommandsExporter();
  1720. }
  1721. export_solution(project) {
  1722. this.make.export_solution(project);
  1723. this.compile_commands.export_solution(project);
  1724. }
  1725. }
  1726. class AndroidExporter extends Exporter {
  1727. constructor() {
  1728. super();
  1729. this.compile_commands = new CompilerCommandsExporter();
  1730. }
  1731. export_solution(project) {
  1732. let from = path_resolve(".");
  1733. let to = path_resolve("build");
  1734. this.safe_name = project.get_safe_name();
  1735. let outdir = path_join(to.toString(), this.safe_name);
  1736. fs_ensuredir(outdir);
  1737. let target_options = {
  1738. package: "org.armory3d",
  1739. installLocation: "internalOnly",
  1740. versionCode: 1,
  1741. versionName: "1.0",
  1742. compileSdkVersion: 33,
  1743. minSdkVersion: 24,
  1744. targetSdkVersion: 33,
  1745. screenOrientation: "sensor",
  1746. permissions: [],
  1747. disableStickyImmersiveMode: false,
  1748. metadata: [],
  1749. abiFilters: []
  1750. };
  1751. if (project.target_options != null && project.target_options.android != null) {
  1752. let userOptions = project.target_options.android;
  1753. for (let key in userOptions) {
  1754. if (userOptions[key] == null)
  1755. continue;
  1756. switch (key) {
  1757. default:
  1758. target_options[key] = userOptions[key];
  1759. }
  1760. }
  1761. }
  1762. fs_writefile(path_join(outdir, 'build.gradle.kts'), get_text_data('android/build.gradle.kts'));
  1763. fs_writefile(path_join(outdir, 'gradle.properties'), get_text_data('android/gradle.properties'));
  1764. fs_writefile(path_join(outdir, 'gradlew'), get_text_data('android/gradlew'));
  1765. if (os_platform() !== 'win32') {
  1766. os_chmod(path_join(outdir, 'gradlew'), "+x");
  1767. }
  1768. fs_writefile(path_join(outdir, 'gradlew.bat'), get_text_data('android/gradlew.bat'));
  1769. let settings = get_text_data('android/settings.gradle.kts');
  1770. settings = settings.replace(/{name}/g, project.getName());
  1771. fs_writefile(path_join(outdir, 'settings.gradle.kts'), settings);
  1772. fs_ensuredir(path_join(outdir, 'app'));
  1773. fs_writefile(path_join(outdir, 'app', 'proguard-rules.pro'), get_text_data('android/app/proguard-rules.pro'));
  1774. this.write_app_gradle(project, outdir, from, target_options);
  1775. this.write_cmake_lists(project, outdir, from);
  1776. fs_ensuredir(path_join(outdir, 'app', 'src'));
  1777. fs_ensuredir(path_join(outdir, 'app', 'src', 'main'));
  1778. this.write_manifest(outdir, target_options);
  1779. let strings = get_text_data('android/main/res/values/strings.xml');
  1780. strings = strings.replace(/{name}/g, project.getName());
  1781. fs_ensuredir(path_join(outdir, 'app', 'src', 'main', 'res', 'values'));
  1782. fs_writefile(path_join(outdir, 'app', 'src', 'main', 'res', 'values', 'strings.xml'), strings);
  1783. this.export_icons(project.icon, outdir, from, to);
  1784. fs_ensuredir(path_join(outdir, 'gradle', 'wrapper'));
  1785. fs_writefile(path_join(outdir, 'gradle', 'wrapper', 'gradle-wrapper.jar'), get_binary_data('android/gradle/wrapper/gradle-wrapper.jar'));
  1786. fs_writefile(path_join(outdir, 'gradle', 'wrapper', 'gradle-wrapper.properties'), get_text_data('android/gradle/wrapper/gradle-wrapper.properties'));
  1787. fs_copydir(path_resolve(from, project.get_debug_dir()), path_resolve(to, this.safe_name, 'app', 'src', 'main', 'assets'));
  1788. this.compile_commands.export_solution(project);
  1789. }
  1790. write_app_gradle(project, outdir, from, target_options) {
  1791. let cflags = '';
  1792. for (let flag of project.cFlags)
  1793. cflags += flag + ' ';
  1794. let cppflags = '';
  1795. for (let flag of project.cppFlags)
  1796. cppflags += flag + ' ';
  1797. let gradle = get_text_data('android/app/build.gradle.kts');
  1798. gradle = gradle.replace(/{package}/g, target_options.package);
  1799. gradle = gradle.replace(/{versionCode}/g, target_options.versionCode.toString());
  1800. gradle = gradle.replace(/{versionName}/g, target_options.versionName);
  1801. gradle = gradle.replace(/{compileSdkVersion}/g, target_options.compileSdkVersion.toString());
  1802. gradle = gradle.replace(/{minSdkVersion}/g, target_options.minSdkVersion.toString());
  1803. gradle = gradle.replace(/{targetSdkVersion}/g, target_options.targetSdkVersion.toString());
  1804. let arch = '';
  1805. if (target_options.abiFilters.length > 0) {
  1806. for (let item of target_options.abiFilters) {
  1807. if (arch.length === 0) {
  1808. arch = '"' + item + '"';
  1809. }
  1810. else {
  1811. arch = arch + ', "' + item + '"';
  1812. }
  1813. }
  1814. arch = `ndk { abiFilters += listOf(${arch}) }`;
  1815. }
  1816. else {
  1817. switch (goptions.arch) {
  1818. case 'default':
  1819. arch = '';
  1820. break;
  1821. case 'arm8':
  1822. arch = 'arm64-v8a';
  1823. break;
  1824. }
  1825. if (goptions.arch !== 'default') {
  1826. arch = `ndk {abiFilters += listOf("${arch}")}`;
  1827. }
  1828. }
  1829. gradle = gradle.replace(/{architecture}/g, arch);
  1830. // Looks like these should go into CMakeLists.txt now..
  1831. // gradle = gradle.replace(/{cflags}/g, cflags);
  1832. // cppflags = '-frtti -fexceptions ' + cppflags;
  1833. // cppflags = '-std=' + project.cppStd + ' ' + cppflags;
  1834. // gradle = gradle.replace(/{cppflags}/g, cppflags);
  1835. let javasources = '';
  1836. for (let dir of project.getJavaDirs()) {
  1837. javasources += '"' + path_relative(path_join(outdir, 'app'), path_resolve(from, dir)).replace(/\\/g, '/') + '", ';
  1838. }
  1839. javasources += '"' + path_join(irondir, 'sources', 'backends', 'data', 'android_java').replace(/\\/g, '/') + '"';
  1840. gradle = gradle.replace(/{javasources}/g, javasources);
  1841. fs_writefile(path_join(outdir, 'app', 'build.gradle.kts'), gradle);
  1842. }
  1843. write_cmake_lists(project, outdir, from) {
  1844. let cmake = get_text_data('android/app/CMakeLists.txt');
  1845. let debugDefines = '';
  1846. for (let def of project.getDefines()) {
  1847. debugDefines += ' -D' + def.replace(/\"/g, '\\\\\\\"');
  1848. }
  1849. cmake = cmake.replace(/{debug_defines}/g, debugDefines);
  1850. let releaseDefines = '';
  1851. for (let def of project.getDefines()) {
  1852. releaseDefines += ' -D' + def.replace(/\"/g, '\\\\\\\"');
  1853. }
  1854. cmake = cmake.replace(/{release_defines}/g, releaseDefines);
  1855. let includes = '';
  1856. for (let inc of project.getIncludeDirs()) {
  1857. includes += ' "' + path_resolve(inc).replace(/\\/g, '/') + '"\n';
  1858. }
  1859. cmake = cmake.replace(/{includes}/g, includes);
  1860. let files = '';
  1861. for (let file of project.getFiles()) {
  1862. if (file.file.endsWith('.c') || file.file.endsWith('.cc')
  1863. || file.file.endsWith('.cpp') || file.file.endsWith('.h')) {
  1864. if (path_isabs(file.file)) {
  1865. files += ' "' + path_resolve(file.file).replace(/\\/g, '/') + '"\n';
  1866. }
  1867. else {
  1868. files += ' "' + path_resolve(path_join(from, file.file)).replace(/\\/g, '/') + '"\n';
  1869. }
  1870. }
  1871. }
  1872. cmake = cmake.replace(/{files}/g, files);
  1873. let libraries1 = '';
  1874. let libraries2 = '';
  1875. for (let lib of project.getLibs()) {
  1876. libraries1 += 'find_library(' + lib + '-lib ' + lib + ')\n';
  1877. libraries2 += ' ${' + lib + '-lib}\n';
  1878. }
  1879. cmake = cmake.replace(/{libraries1}/g, libraries1).replace(/{libraries2}/g, libraries2);
  1880. let cmakePath = path_join(outdir, 'app', 'CMakeLists.txt');
  1881. fs_writefile(cmakePath, cmake);
  1882. }
  1883. write_manifest(outdir, target_options) {
  1884. let manifest = get_text_data('android/main/AndroidManifest.xml');
  1885. manifest = manifest.replace(/{package}/g, target_options.package);
  1886. manifest = manifest.replace(/{installLocation}/g, target_options.installLocation);
  1887. manifest = manifest.replace(/{versionCode}/g, target_options.versionCode.toString());
  1888. manifest = manifest.replace(/{versionName}/g, target_options.versionName);
  1889. manifest = manifest.replace(/{screenOrientation}/g, target_options.screenOrientation);
  1890. manifest = manifest.replace(/{targetSdkVersion}/g, target_options.targetSdkVersion);
  1891. manifest = manifest.replace(/{permissions}/g, target_options.permissions.map((p) => { return '\n\t<uses-permission android:name="' + p + '"/>'; }).join(''));
  1892. let metadata = target_options.disableStickyImmersiveMode ? '\n\t\t<meta-data android:name="disableStickyImmersiveMode" android:value="true"/>' : '';
  1893. for (let meta of target_options.metadata) {
  1894. metadata += '\n\t\t' + meta;
  1895. }
  1896. manifest = manifest.replace(/{metadata}/g, metadata);
  1897. fs_ensuredir(path_join(outdir, 'app', 'src', 'main'));
  1898. fs_writefile(path_join(outdir, 'app', 'src', 'main', 'AndroidManifest.xml'), manifest);
  1899. }
  1900. export_icons(icon, outdir, from, to) {
  1901. let folders = ['mipmap-mdpi', 'mipmap-hdpi', 'mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi'];
  1902. let dpis = [48, 72, 96, 144, 192];
  1903. for (let i = 0; i < dpis.length; ++i) {
  1904. let folder = folders[i];
  1905. let dpi = dpis[i];
  1906. fs_ensuredir(path_join(outdir, 'app', 'src', 'main', 'res', folder));
  1907. export_png_icon(icon, path_resolve(to, this.safe_name, 'app', 'src', 'main', 'res', folder, 'ic_launcher.png'), dpi, dpi, from);
  1908. export_png_icon(icon, path_resolve(to, this.safe_name, 'app', 'src', 'main', 'res', folder, 'ic_launcher_round.png'), dpi, dpi, from);
  1909. }
  1910. }
  1911. }
  1912. class CompilerCommandsExporter extends Exporter {
  1913. constructor() {
  1914. super();
  1915. }
  1916. export_solution(project) {
  1917. let from = path_resolve(".");
  1918. let to = path_resolve("build");
  1919. let platform = goptions.target;
  1920. from = path_resolve(os_cwd(), from);
  1921. this.write_file(path_resolve(to, 'compile_commands.json'));
  1922. let includes = [];
  1923. for (let inc of project.getIncludeDirs()) {
  1924. includes.push('-I');
  1925. includes.push(path_resolve(from, inc));
  1926. }
  1927. let defines = [];
  1928. for (let def of project.getDefines()) {
  1929. defines.push('-D');
  1930. defines.push(def.replace(/\"/g, '\\"'));
  1931. }
  1932. let objects = {};
  1933. let ofiles = {};
  1934. for (let fileobject of project.getFiles()) {
  1935. let file = fileobject.file;
  1936. if (file.endsWith('.cpp') || file.endsWith('.c') || file.endsWith('.cc')) {
  1937. let name = file.toLowerCase();
  1938. if (name.indexOf('/') >= 0)
  1939. name = name.substr(name.lastIndexOf('/') + 1);
  1940. name = name.substr(0, name.lastIndexOf('.'));
  1941. if (!objects[name]) {
  1942. objects[name] = true;
  1943. ofiles[file] = name;
  1944. }
  1945. else {
  1946. while (objects[name]) {
  1947. name = name + '_';
  1948. }
  1949. objects[name] = true;
  1950. ofiles[file] = name;
  1951. }
  1952. }
  1953. }
  1954. let default_args = [];
  1955. if (platform === 'android') {
  1956. default_args.push('--target=aarch64-none-linux-android21');
  1957. default_args.push('-DANDROID');
  1958. function ndkFromSdkRoot() {
  1959. let _a = os_env('ANDROID_HOME');
  1960. let sdkEnv = _a !== null ? _a : os_env('ANDROID_SDK_ROOT');
  1961. if (!sdkEnv)
  1962. return null;
  1963. let ndk_dir = path_join(sdkEnv, 'ndk');
  1964. if (!fs_exists(ndk_dir)) {
  1965. return null;
  1966. }
  1967. let ndks = fs_readdir(ndk_dir);
  1968. ndks = ndks.filter(item => !item.startsWith("."));
  1969. if (ndks.length < 1) {
  1970. return null;
  1971. }
  1972. return path_join(ndk_dir, ndks[0]);
  1973. }
  1974. let _a = os_env('ANDROID_NDK');
  1975. let android_ndk = _a !== null ? _a : ndkFromSdkRoot();
  1976. if (android_ndk) {
  1977. let host_tag = '';
  1978. switch (os_platform()) {
  1979. case 'linux':
  1980. host_tag = 'linux-x86_64';
  1981. break;
  1982. case 'darwin':
  1983. host_tag = 'darwin-x86_64';
  1984. break;
  1985. case 'win32':
  1986. host_tag = 'windows-x86_64';
  1987. break;
  1988. }
  1989. let ndk_toolchain = path_join(android_ndk, `toolchains/llvm/prebuilt/${host_tag}`);
  1990. if (host_tag !== '' && fs_exists(ndk_toolchain)) {
  1991. default_args.push(`--gcc-toolchain=${ndk_toolchain}`);
  1992. default_args.push(`--sysroot=${ndk_toolchain}/sysroot`);
  1993. }
  1994. else {
  1995. // fallback to the first found toolchain
  1996. let toolchains = fs_readdir(path_join(android_ndk, `toolchains/llvm/prebuilt/`));
  1997. if (toolchains.length > 0) {
  1998. let host_tag = toolchains[0];
  1999. let ndk_toolchain = path_join(android_ndk, `toolchains/llvm/prebuilt/${host_tag}`);
  2000. default_args.push(`--gcc-toolchain=${ndk_toolchain}`);
  2001. default_args.push(`--sysroot=${ndk_toolchain}/sysroot`);
  2002. console.log(`Found android ndk toolchain in ${ndk_toolchain}.`);
  2003. }
  2004. }
  2005. }
  2006. }
  2007. let commands = [];
  2008. for (let fileobject of project.getFiles()) {
  2009. let file = fileobject.file;
  2010. if (file.endsWith('.c') || file.endsWith('.cpp') || file.endsWith('.cc')) {
  2011. let args = ['/usr/bin/clang', '-c', '-o', (goptions.debug ? 'Debug' : 'Release') + ofiles[file] + '.o'];
  2012. if (file.endsWith('.c')) {
  2013. args.push('-std=c99');
  2014. }
  2015. args.push(...default_args);
  2016. args.push(path_resolve(from, file));
  2017. let command = {
  2018. directory: from,
  2019. file: path_resolve(from, file),
  2020. output: path_resolve(to, ofiles[file] + '.o'),
  2021. arguments: args.concat(includes).concat(defines)
  2022. };
  2023. commands.push(command);
  2024. }
  2025. }
  2026. this.p(JSON.stringify(commands));
  2027. this.close_file();
  2028. }
  2029. }
  2030. // ███╗ ███╗ █████╗ ██╗ ██╗███████╗
  2031. // ████╗ ████║██╔══██╗██║ ██╔╝██╔════╝
  2032. // ██╔████╔██║███████║█████╔╝ █████╗
  2033. // ██║╚██╔╝██║██╔══██║██╔═██╗ ██╔══╝
  2034. // ██║ ╚═╝ ██║██║ ██║██║ ██╗███████╗
  2035. // ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
  2036. function export_ico(icon, to, from) {
  2037. if (!fs_exists(path_join(from, icon))) {
  2038. from = irondir;
  2039. icon = "icon.png";
  2040. }
  2041. if (fs_exists(to) && fs_mtime(to) > fs_mtime(from)) {
  2042. return;
  2043. }
  2044. amake.export_ico(path_join(from, icon), to);
  2045. }
  2046. function export_png_icon(icon, to, width, height, from) {
  2047. if (!fs_exists(path_join(from, icon))) {
  2048. from = irondir;
  2049. icon = "icon.png";
  2050. }
  2051. if (fs_exists(to) && fs_mtime(to) > fs_mtime(from)) {
  2052. return;
  2053. }
  2054. amake.export_png(path_join(from, icon), to, width, height);
  2055. }
  2056. function contains_define(array, value) {
  2057. return array.indexOf(value) > -1;
  2058. }
  2059. function contains_fancy_define(array, value) {
  2060. let name = value.substring(0, value.indexOf("="));
  2061. for (let element of array) {
  2062. let index = element.indexOf("=");
  2063. if (index >= 0) {
  2064. let otherName = element.substring(0, index);
  2065. if (name === otherName) {
  2066. return true;
  2067. }
  2068. }
  2069. }
  2070. return false;
  2071. }
  2072. function load_project(directory, is_root_project) {
  2073. __dirname = path_resolve(directory);
  2074. if (is_root_project) {
  2075. globalThis.platform = goptions.target;
  2076. globalThis.graphics = goptions.graphics;
  2077. globalThis.flags = {
  2078. name: "Armory",
  2079. package: "org.armory3d",
  2080. dirname: __dirname,
  2081. release: os_argv().indexOf("--debug") == -1,
  2082. with_d3dcompiler: false,
  2083. with_nfd: false,
  2084. with_compress: false,
  2085. with_image_write: false,
  2086. with_video_write: false,
  2087. with_audio: false,
  2088. with_iron: false,
  2089. with_eval: false,
  2090. with_gamepad: false,
  2091. embed: false
  2092. };
  2093. }
  2094. let project = eval("function _(){" + fs_readfile(path_resolve(directory, "project.js")) + "} _();");
  2095. if (is_root_project) {
  2096. try {
  2097. export_iron_project(project, goptions);
  2098. }
  2099. catch (error) {
  2100. console.log(error);
  2101. os_exit(1);
  2102. }
  2103. }
  2104. return project;
  2105. }
  2106. function search_files2(current_dir, pattern) {
  2107. let result = [];
  2108. if (!fs_exists(current_dir)) {
  2109. return result;
  2110. }
  2111. current_dir = path_join(current_dir); ////
  2112. let files = fs_readdir(current_dir);
  2113. for (let f in files) {
  2114. let file = path_join(current_dir, files[f]);
  2115. if (fs_isdir(file))
  2116. continue;
  2117. file = path_relative(current_dir, file);
  2118. if (matches(stringify(file), stringify(pattern))) {
  2119. result.push(path_join(current_dir, stringify(file)));
  2120. }
  2121. }
  2122. if (pattern.endsWith("**")) {
  2123. let dirs = fs_readdir(current_dir);
  2124. for (let d of dirs) {
  2125. let dir = path_join(current_dir, d);
  2126. if (d.startsWith('.'))
  2127. continue;
  2128. if (!fs_isdir(dir))
  2129. continue;
  2130. result = result.concat(search_files2(dir, pattern));
  2131. }
  2132. }
  2133. return result;
  2134. }
  2135. class AssetConverter {
  2136. constructor(exporter, options, asset_matchers) {
  2137. this.exporter = exporter;
  2138. this.options = options;
  2139. this.asset_matchers = asset_matchers;
  2140. }
  2141. static replace_pattern(pattern, value, filepath, from) {
  2142. let base_path = from;
  2143. let dir_value = path_relative(base_path, path_dirname(filepath));
  2144. if (base_path.length > 0 && base_path[base_path.length - 1] === path_sep
  2145. && dir_value.length > 0 && dir_value[dir_value.length - 1] !== path_sep) {
  2146. dir_value += path_sep;
  2147. }
  2148. let dir_regex = dir_value === ''
  2149. ? /{dir}\//g
  2150. : /{dir}/g;
  2151. return pattern.replace(/{name}/g, value).replace(dir_regex, dir_value);
  2152. }
  2153. static create_export_info(filepath, keep_ext, options, from) {
  2154. let name_value = path_basename_noext(filepath);
  2155. let destination = path_basename_noext(filepath);
  2156. if (keep_ext || options.noprocessing) {
  2157. destination += path_extname(filepath);
  2158. }
  2159. if (options.destination) {
  2160. destination = AssetConverter.replace_pattern(options.destination, destination, filepath, from);
  2161. }
  2162. if (keep_ext) {
  2163. name_value += path_extname(filepath);
  2164. }
  2165. if (options.name) {
  2166. name_value = AssetConverter.replace_pattern(options.name, name_value, filepath, from);
  2167. }
  2168. return { name: name_value, destination: path_normalize(destination) };
  2169. }
  2170. watch(match, options) {
  2171. match = path_normalize(match);
  2172. let basedir = match.substring(0, match.lastIndexOf(path_sep));
  2173. let pattern = match;
  2174. if (path_isabs(pattern)) {
  2175. pattern = path_relative(basedir, pattern);
  2176. }
  2177. let files = search_files2(basedir, pattern);
  2178. let self = this;
  2179. let parsed_files = [];
  2180. let index = 0;
  2181. for (let file of files) {
  2182. console.log('Exporting asset ' + (index + 1) + ' of ' + files.length + ' (' + path_basename(file) + ').');
  2183. let ext = path_extname(file).toLowerCase();
  2184. switch (ext) {
  2185. case '.png':
  2186. case '.jpg':
  2187. case '.hdr': {
  2188. let export_info = AssetConverter.create_export_info(file, false, options, ".");
  2189. let images;
  2190. if (options.noprocessing) {
  2191. images = self.exporter.copy_blob(file, export_info.destination, globalThis.flags.embed && !options.noembed);
  2192. }
  2193. else {
  2194. images = self.exporter.copy_image(file, export_info.destination, globalThis.flags.embed && !options.noembed);
  2195. }
  2196. parsed_files.push({ name: export_info.name, from: file, type: 'image', files: images, original_width: options.original_width, original_height: options.original_height, noembed: options.noembed });
  2197. break;
  2198. }
  2199. default: {
  2200. let export_info = AssetConverter.create_export_info(file, true, options, ".");
  2201. let blobs = self.exporter.copy_blob(file, export_info.destination, globalThis.flags.embed && !options.noembed);
  2202. parsed_files.push({ name: export_info.name, from: file, type: 'blob', files: blobs, original_width: undefined, original_height: undefined, noembed: options.noembed });
  2203. break;
  2204. }
  2205. }
  2206. index += 1;
  2207. }
  2208. return parsed_files;
  2209. }
  2210. run() {
  2211. let files = [];
  2212. for (let matcher of this.asset_matchers) {
  2213. files = files.concat(this.watch(matcher.match, matcher.options));
  2214. }
  2215. return files;
  2216. }
  2217. }
  2218. class CompiledShader {
  2219. constructor() {
  2220. this.files = [];
  2221. }
  2222. }
  2223. function shader_find_type(options) {
  2224. if (options.graphics === 'vulkan') {
  2225. return 'spirv';
  2226. }
  2227. else if (options.graphics === 'metal') {
  2228. return 'metal';
  2229. }
  2230. else if (options.graphics === 'direct3d12') {
  2231. return 'hlsl';
  2232. }
  2233. }
  2234. class ShaderCompiler {
  2235. constructor(exporter, to, temp, options, shader_matchers) {
  2236. this.exporter = exporter;
  2237. this.type = shader_find_type(options);
  2238. this.options = options;
  2239. this.to = to;
  2240. this.temp = temp;
  2241. this.shader_matchers = shader_matchers;
  2242. }
  2243. watch(match, options) {
  2244. match = path_normalize(match);
  2245. let basedir = match.substring(0, match.lastIndexOf(path_sep));
  2246. let pattern = match;
  2247. if (path_isabs(pattern)) {
  2248. pattern = path_relative(basedir, pattern);
  2249. }
  2250. let shaders = search_files2(basedir, pattern);
  2251. let self = this;
  2252. let compiled_shaders = [];
  2253. let index = 0;
  2254. for (let shader of shaders) {
  2255. console.log('Compiling shader ' + (index + 1) + ' of ' + shaders.length + ' (' + path_basename(shader) + ').');
  2256. let compiled_shader = null;
  2257. try {
  2258. compiled_shader = self.compile_shader(shader, options);
  2259. }
  2260. catch (error) {
  2261. console.log('Compiling shader ' + (index + 1) + ' of ' + shaders.length + ' (' + path_basename(shader) + ') failed:');
  2262. console.log(error);
  2263. }
  2264. if (compiled_shader === null) {
  2265. compiled_shader = new CompiledShader();
  2266. }
  2267. let type = self.type;
  2268. if (type == "hlsl") {
  2269. type = "d3d11";
  2270. }
  2271. let base = path_resolve('build', 'temp', path_basename_noext(shader));
  2272. compiled_shader.files = [base + '.vert.' + type, base + '.frag.' + type];
  2273. compiled_shader.name = AssetConverter.create_export_info(shader, false, options, ".").name;
  2274. compiled_shaders.push(compiled_shader);
  2275. ++index;
  2276. }
  2277. return compiled_shaders;
  2278. }
  2279. run() {
  2280. let shaders = [];
  2281. for (let matcher of this.shader_matchers) {
  2282. shaders = shaders.concat(this.watch(matcher.match, matcher.options));
  2283. }
  2284. return shaders;
  2285. }
  2286. compile_shader(file, options) {
  2287. let from = file;
  2288. let to = path_join(this.to, path_basename_noext(file) + '.' + this.type);
  2289. let from_time = 0;
  2290. let to_time;
  2291. if (fs_exists(from)) from_time = fs_mtime(from);
  2292. if (fs_exists(to)) to_time = fs_mtime(to);
  2293. if (options.noprocessing) {
  2294. if (!to_time || to_time < from_time) {
  2295. fs_copyfile(from, to);
  2296. }
  2297. let compiled_shader = new CompiledShader();
  2298. return compiled_shader;
  2299. }
  2300. if (!from_time || (to_time && to_time > from_time)) {
  2301. return null;
  2302. }
  2303. else {
  2304. fs_ensuredir(this.temp);
  2305. // from = path_resolve(from);
  2306. // to = path_resolve(to);
  2307. // this.temp = path_resolve(this.temp);
  2308. amake.ashader(this.type, from, to);
  2309. let compiled_shader = new CompiledShader();
  2310. return compiled_shader;
  2311. }
  2312. }
  2313. }
  2314. function export_k(from, to) {
  2315. to += ".k";
  2316. if (fs_exists(to) && fs_mtime(to) > fs_mtime(from)) {
  2317. return "k";
  2318. }
  2319. fs_ensuredir(path_dirname(to));
  2320. amake.export_k(from, to);
  2321. }
  2322. class IronExporter {
  2323. constructor(project, options) {
  2324. this.options = options;
  2325. this.sources = [];
  2326. }
  2327. ts_options(defines) {
  2328. defines.push("arm_" + this.options.graphics);
  2329. defines.push("arm_" + goptions.target);
  2330. return {
  2331. from: ".",
  2332. sources: this.sources,
  2333. defines: defines
  2334. };
  2335. }
  2336. export() {
  2337. fs_ensuredir(path_join("build", "out"));
  2338. }
  2339. copy_image(from, to, embed) {
  2340. let to_full = path_join("build", "out", to);
  2341. if (embed) {
  2342. to_full = path_join("build", "temp", to);
  2343. }
  2344. export_k(from, to_full);
  2345. return [to + ".k"];
  2346. }
  2347. copy_blob(from, to, embed) {
  2348. fs_ensuredir(path_join("build", "out", path_dirname(to)));
  2349. let to_full = path_join("build", "out", to);
  2350. if (embed &&
  2351. !to.endsWith(".txt") &&
  2352. !to.endsWith(".md") &&
  2353. !to.endsWith(".json") &&
  2354. !to.endsWith(".js")
  2355. ) {
  2356. fs_ensuredir(path_join("build", "temp", path_dirname(to)));
  2357. to_full = path_join("build", "temp", to);
  2358. }
  2359. fs_copyfile(from, to_full);
  2360. return [to];
  2361. }
  2362. add_source_directory(path) {
  2363. this.sources.push(path);
  2364. }
  2365. }
  2366. function ts_contains_define(define) {
  2367. let b = false;
  2368. for (let s of globalThis.options.defines) {
  2369. if (define.includes(s)) {
  2370. b = true;
  2371. break;
  2372. };
  2373. }
  2374. if (define.includes("!")) {
  2375. b = !b;
  2376. }
  2377. return b;
  2378. }
  2379. function ts_preprocessor(file, file_path) {
  2380. let stack = [];
  2381. let found = [];
  2382. let lines = file.split("\n");
  2383. for (let i = 0; i < lines.length; ++i) {
  2384. let line = lines[i].trimStart();
  2385. if (line.startsWith("///if")) {
  2386. let define = line.substr(6);
  2387. stack.push(ts_contains_define(define));
  2388. found.push(stack[stack.length - 1]);
  2389. }
  2390. else if (line.startsWith("///elseif")) {
  2391. let define = line.substr(10);
  2392. if (!found[found.length - 1] && ts_contains_define(define)) {
  2393. stack[stack.length - 1] = true;
  2394. found[found.length - 1] = true;
  2395. }
  2396. else {
  2397. stack[stack.length - 1] = false;
  2398. }
  2399. }
  2400. else if (line.startsWith("///else")) {
  2401. stack[stack.length - 1] = !found[found.length - 1];
  2402. }
  2403. else if (line.startsWith("///end")) {
  2404. stack.pop();
  2405. found.pop();
  2406. }
  2407. else if (stack.length > 0) {
  2408. let comment = false;
  2409. for (let b of stack) {
  2410. if (!b) {
  2411. comment = true;
  2412. break;
  2413. }
  2414. }
  2415. if (comment) {
  2416. lines[i] = "///" + lines[i];
  2417. }
  2418. }
  2419. if (lines[i].indexOf("__ID__") > -1 && !lines[i].startsWith("declare")) {
  2420. // #define ID__(x, y) x ":" #y
  2421. // #define ID_(x, y) ID__(x, y)
  2422. // #define ID ID_(__FILE__, __LINE__)
  2423. lines[i] = lines[i].replace("__ID__", "\"" + path_basename(file_path) + ":" + i + "\"");
  2424. }
  2425. }
  2426. return lines.join("\n");
  2427. }
  2428. function write_ts_project(projectdir, options) {
  2429. let tsdata = {
  2430. include: []
  2431. };
  2432. let main_ts = null;
  2433. for (let i = 0; i < options.sources.length; ++i) {
  2434. let src = options.sources[i];
  2435. let files = fs_readdir(src);
  2436. if (src.endsWith(".ts")) {
  2437. // Add file instead of dir
  2438. files = [src.substring(src.lastIndexOf(path_sep) + 1, src.length)];
  2439. src = src.substring(0, src.lastIndexOf(path_sep));
  2440. }
  2441. for (let file of files) {
  2442. if (file.endsWith(".ts")) {
  2443. // Prevent duplicates, keep the newly added file
  2444. for (let included of tsdata.include){
  2445. if (path_basename(included) == file) {
  2446. tsdata.include.splice(tsdata.include.indexOf(included), 1);
  2447. break;
  2448. }
  2449. }
  2450. tsdata.include.push(src + path_sep + file);
  2451. if (file == "main.ts") {
  2452. main_ts = src + path_sep + file;
  2453. }
  2454. }
  2455. }
  2456. }
  2457. // Include main.ts last
  2458. if (main_ts != null) {
  2459. tsdata.include.splice(tsdata.include.indexOf(main_ts), 1);
  2460. tsdata.include.push(main_ts);
  2461. }
  2462. fs_ensuredir(projectdir);
  2463. fs_writefile(path_join(projectdir, 'tsconfig.json'), JSON.stringify(tsdata, null, 4));
  2464. // alang compiler
  2465. globalThis.options = options;
  2466. let source = '';
  2467. let file_paths = tsdata.include;
  2468. for (let file_path of file_paths) {
  2469. let file = fs_readfile(file_path);
  2470. file = ts_preprocessor(file, file_path);
  2471. source += file;
  2472. }
  2473. if (goptions.alangjs) {
  2474. globalThis.std = std;
  2475. globalThis.fs_readfile = fs_readfile;
  2476. globalThis.fs_writefile = fs_writefile;
  2477. globalThis.flags.alang_source = source;
  2478. globalThis.flags.alang_output = os_cwd() + path_sep + "build" + path_sep + "iron.c";
  2479. let alang = irondir + '/tools/amake/alang.js';
  2480. (1, eval)(fs_readfile(alang));
  2481. }
  2482. else {
  2483. // let alang_input = os_cwd() + path_sep + "build" + path_sep + "iron.ts";
  2484. // fs_writefile(alang_input, source);
  2485. let alang_output = os_cwd() + path_sep + "build" + path_sep + "iron.c";
  2486. let start = Date.now();
  2487. amake.alang(source, alang_output);
  2488. console.log("alang took " + (Date.now() - start) + "ms.");
  2489. }
  2490. }
  2491. function export_project_files(name, options, exporter, defines) {
  2492. let ts_options = exporter.ts_options(defines);
  2493. write_ts_project("build", ts_options);
  2494. exporter.export();
  2495. return name;
  2496. }
  2497. function export_iron_project(project, options) {
  2498. let temp = path_join("build", "temp");
  2499. fs_ensuredir(temp);
  2500. let exporter = new IronExporter(project, options);
  2501. fs_ensuredir(path_join("build", "out"));
  2502. for (let source of project.sources) {
  2503. exporter.add_source_directory(source);
  2504. }
  2505. let asset_converter = new AssetConverter(exporter, options, project.asset_matchers);
  2506. let assets = asset_converter.run();
  2507. let shaderdir = path_join("build", "out", "data");
  2508. if (globalThis.flags.embed) {
  2509. shaderdir = path_join("build", "temp");
  2510. }
  2511. fs_ensuredir(shaderdir);
  2512. let exported_shaders = [];
  2513. let shader_compiler = new ShaderCompiler(exporter, shaderdir, temp, options, project.shader_matchers);
  2514. exported_shaders = shader_compiler.run();
  2515. // Write embed.h
  2516. if (globalThis.flags.embed) {
  2517. let embed_files = [];
  2518. for (let asset of assets) {
  2519. if (asset.noembed ||
  2520. asset.from.endsWith(".txt") ||
  2521. asset.from.endsWith(".md") ||
  2522. asset.from.endsWith(".json") ||
  2523. asset.from.endsWith(".js")) {
  2524. continue;
  2525. }
  2526. embed_files.push(path_resolve("build", "temp", asset.files[0]));
  2527. }
  2528. for (let shader of exported_shaders) {
  2529. embed_files.push(shader.files[0]);
  2530. embed_files.push(shader.files[1]);
  2531. }
  2532. if (embed_files.length > 0) {
  2533. let embed_header = "#pragma once\n";
  2534. for (let file of embed_files) {
  2535. embed_header += "const unsigned char " + path_basename(file).replaceAll(".", "_") + "[] = {\n"
  2536. if (platform === "windows") {
  2537. file = file.replaceAll("\\", "/");
  2538. }
  2539. embed_header += "#embed \"" + file + "\"\n";
  2540. embed_header += "};\n"
  2541. }
  2542. embed_header += "char *embed_keys[] = {\n"
  2543. for (let file of embed_files) {
  2544. embed_header += "\"./data/" + path_basename(file) + "\",\n";
  2545. }
  2546. embed_header += "};\n"
  2547. embed_header += "const unsigned char *embed_values[] = {\n"
  2548. for (let file of embed_files) {
  2549. embed_header += path_basename(file).replaceAll(".", "_") + ",\n";
  2550. }
  2551. embed_header += "};\n"
  2552. embed_header += "const int embed_sizes[] = {\n";
  2553. for (let file of embed_files) {
  2554. embed_header += "sizeof(" + path_basename(file).replaceAll(".", "_") + "),\n"
  2555. }
  2556. embed_header += "};\n"
  2557. embed_header += "int embed_count = " + embed_files.length + ";\n";
  2558. fs_writefile(path_join("build", "embed.h"), embed_header);
  2559. }
  2560. }
  2561. export_project_files(project.name, options, exporter, project.defines);
  2562. }
  2563. class Project {
  2564. constructor(name) {
  2565. this.cppStd = "c++17";
  2566. this.cStd = "c11";
  2567. this.cmdArgs = [];
  2568. this.cFlags = [];
  2569. this.cppFlags = [];
  2570. this.icon = "icon.png";
  2571. this.lto = true;
  2572. this.noFlatten = true;
  2573. this.name = name;
  2574. this.safe_name = name.replace(/[^A-z0-9\-\_]/g, "-");
  2575. this.version = "1.0";
  2576. this.debugdir = "build/out";
  2577. this.basedir = __dirname;
  2578. this.uuid = crypto_random_uuid();
  2579. this.files = [];
  2580. this.customs = [];
  2581. this.javadirs = [];
  2582. this.subProjects = [];
  2583. this.includedirs = [];
  2584. this.defines = [];
  2585. this.libs = [];
  2586. this.includes = [];
  2587. this.target_options = {
  2588. android: {},
  2589. };
  2590. this.executable_name = null;
  2591. this.sources = [];
  2592. this.asset_matchers = [];
  2593. this.shader_matchers = [];
  2594. }
  2595. get_executable_name() {
  2596. return this.executable_name;
  2597. }
  2598. flatten_subprojects() {
  2599. for (let sub of this.subProjects) {
  2600. sub.noFlatten = false;
  2601. sub.flatten_subprojects();
  2602. }
  2603. }
  2604. flatten() {
  2605. this.noFlatten = false;
  2606. this.flatten_subprojects();
  2607. }
  2608. internal_flatten() {
  2609. let out = [];
  2610. for (let sub of this.subProjects) {
  2611. sub.internal_flatten();
  2612. }
  2613. for (let sub of this.subProjects) {
  2614. if (sub.noFlatten) {
  2615. out.push(sub);
  2616. }
  2617. else {
  2618. if (!sub.lto) {
  2619. this.lto = false;
  2620. }
  2621. if (sub.icon) {
  2622. this.icon = sub.icon;
  2623. }
  2624. let subbasedir = sub.basedir;
  2625. for (let tkey of Object.keys(sub.target_options)) {
  2626. let target = sub.target_options[tkey];
  2627. for (let key of Object.keys(target)) {
  2628. let options = this.target_options[tkey];
  2629. let option = target[key];
  2630. if (options[key] == null)
  2631. options[key] = option;
  2632. // push library properties to current array instead
  2633. else if (Array.isArray(options[key]) && Array.isArray(option)) {
  2634. for (let value of option) {
  2635. if (!options[key].includes(value))
  2636. options[key].push(value);
  2637. }
  2638. }
  2639. }
  2640. }
  2641. for (let d of sub.defines) {
  2642. if (d.indexOf("=") >= 0) {
  2643. if (!contains_fancy_define(this.defines, d)) {
  2644. this.defines.push(d);
  2645. }
  2646. }
  2647. else {
  2648. if (!contains_define(this.defines, d)) {
  2649. this.defines.push(d);
  2650. }
  2651. }
  2652. }
  2653. for (let file of sub.files) {
  2654. let absolute = file.file;
  2655. if (!path_isabs(absolute)) {
  2656. absolute = path_join(subbasedir, file.file);
  2657. }
  2658. this.files.push({ file: absolute.replace(/\\/g, "/"), options: file.options, projectDir: subbasedir, projectName: sub.name });
  2659. }
  2660. for (let custom of sub.customs) {
  2661. let absolute = custom.file;
  2662. if (!path_isabs(absolute)) {
  2663. absolute = path_join(subbasedir, custom.file);
  2664. }
  2665. this.customs.push({ file: absolute.replace(/\\/g, "/"), command: custom.command, output: custom.output });
  2666. }
  2667. for (let i of sub.includedirs)
  2668. if (!this.includedirs.includes(path_resolve(subbasedir, i)))
  2669. this.includedirs.push(path_resolve(subbasedir, i));
  2670. for (let j of sub.javadirs)
  2671. if (!this.javadirs.includes(path_resolve(subbasedir, j)))
  2672. this.javadirs.push(path_resolve(subbasedir, j));
  2673. for (let lib of sub.libs) {
  2674. if (!this.libs.includes(lib))
  2675. this.libs.push(lib);
  2676. }
  2677. for (let flag of sub.cFlags) {
  2678. if (!this.cFlags.includes(flag)) {
  2679. this.cFlags.push(flag);
  2680. }
  2681. }
  2682. for (let flag of sub.cppFlags) {
  2683. if (!this.cppFlags.includes(flag)) {
  2684. this.cppFlags.push(flag);
  2685. }
  2686. }
  2687. }
  2688. }
  2689. this.subProjects = out;
  2690. }
  2691. getName() {
  2692. return this.name;
  2693. }
  2694. get_safe_name() {
  2695. return this.safe_name;
  2696. }
  2697. get_uuid() {
  2698. return this.uuid;
  2699. }
  2700. addCFlag(flag) {
  2701. this.cFlags.push(flag);
  2702. }
  2703. addCFlags() {
  2704. for (let i = 0; i < arguments.length; ++i) {
  2705. if (typeof arguments[i] === "string") {
  2706. this.addCFlag(arguments[i]);
  2707. }
  2708. }
  2709. }
  2710. add_file_for_real(file, options) {
  2711. for (let index in this.files) {
  2712. if (this.files[index].file === file) {
  2713. this.files[index] = { file: file, options: options, projectDir: this.basedir, projectName: this.name };
  2714. return;
  2715. }
  2716. }
  2717. this.files.push({ file: file, options: options, projectDir: this.basedir, projectName: this.name });
  2718. }
  2719. search_files(current) {
  2720. if (current === undefined) {
  2721. for (let sub of this.subProjects)
  2722. sub.search_files(undefined);
  2723. this.search_files(this.basedir);
  2724. for (let includeobject of this.includes) {
  2725. if (path_isabs(includeobject.file) && includeobject.file.includes("**")) {
  2726. let starIndex = includeobject.file.indexOf("**");
  2727. let endIndex = includeobject.file.substring(0, starIndex).replace(/\\/g, "/").lastIndexOf("/");
  2728. this.search_files(includeobject.file.substring(0, endIndex));
  2729. }
  2730. if (includeobject.file.startsWith("../")) {
  2731. let start = "../";
  2732. while (includeobject.file.startsWith(start)) {
  2733. start += "../";
  2734. }
  2735. this.search_files(path_resolve(this.basedir, start));
  2736. }
  2737. }
  2738. return;
  2739. }
  2740. let files = fs_readdir(current);
  2741. for (let f in files) {
  2742. let file = path_join(current, files[f]);
  2743. let follow = true;
  2744. try {
  2745. if (fs_isdir(file)) {
  2746. follow = false;
  2747. }
  2748. }
  2749. catch (err) {
  2750. follow = false;
  2751. }
  2752. if (!follow) {
  2753. continue;
  2754. }
  2755. file = path_relative(this.basedir, file);
  2756. for (let includeobject of this.includes) {
  2757. let include = includeobject.file;
  2758. if (path_isabs(include)) {
  2759. let inc = include;
  2760. inc = path_relative(this.basedir, inc);
  2761. include = inc;
  2762. }
  2763. if (matches(stringify(file), stringify(include))) {
  2764. this.add_file_for_real(stringify(file), includeobject.options);
  2765. }
  2766. }
  2767. }
  2768. let dirs = fs_readdir(current);
  2769. for (let d of dirs) {
  2770. let dir = path_join(current, d);
  2771. if (d.startsWith("."))
  2772. continue;
  2773. let follow = true;
  2774. try {
  2775. if (!fs_isdir(dir)) {
  2776. follow = false;
  2777. }
  2778. }
  2779. catch (err) {
  2780. follow = false;
  2781. }
  2782. if (!follow) {
  2783. continue;
  2784. }
  2785. this.search_files(dir);
  2786. }
  2787. }
  2788. add_cfiles(file, options) {
  2789. this.includes.push({ file: file, options: options });
  2790. }
  2791. add_define(define) {
  2792. if (contains_define(this.defines, define)) {
  2793. return;
  2794. }
  2795. this.defines.push(define);
  2796. }
  2797. add_include_dir(include) {
  2798. if (this.includedirs.includes(include))
  2799. return;
  2800. this.includedirs.push(include);
  2801. }
  2802. add_lib(lib) {
  2803. this.libs.push(lib);
  2804. }
  2805. add_assets(match, options) {
  2806. if (!options)
  2807. options = {};
  2808. if (!path_isabs(match)) {
  2809. let base = stringify(path_resolve(this.basedir));
  2810. if (!base.endsWith('/')) {
  2811. base += '/';
  2812. }
  2813. match = base + match.replace(/\\/g, '/');
  2814. }
  2815. this.asset_matchers.push({ match: match, options: options });
  2816. }
  2817. add_tsfiles(source) {
  2818. this.sources.push(path_resolve(path_join(this.basedir, source)));
  2819. }
  2820. add_shaders(match, options) {
  2821. if (!options)
  2822. options = {};
  2823. if (!path_isabs(match)) {
  2824. let base = stringify(path_resolve(this.basedir));
  2825. if (!base.endsWith('/')) {
  2826. base += '/';
  2827. }
  2828. match = base + match.replace(/\\/g, '/');
  2829. }
  2830. this.shader_matchers.push({ match: match, options: options });
  2831. }
  2832. getFiles() {
  2833. return this.files;
  2834. }
  2835. getJavaDirs() {
  2836. return this.javadirs;
  2837. }
  2838. getSubProjects() {
  2839. return this.subProjects;
  2840. }
  2841. getIncludeDirs() {
  2842. return this.includedirs;
  2843. }
  2844. getDefines() {
  2845. return this.defines;
  2846. }
  2847. getLibs() {
  2848. return this.libs;
  2849. }
  2850. get_debug_dir() {
  2851. return this.debugdir;
  2852. }
  2853. add_project(directory) {
  2854. let from = path_isabs(directory) ? directory : path_join(this.basedir, directory);
  2855. if (!fs_exists(from)) {
  2856. return;
  2857. }
  2858. let project = load_project(from, false);
  2859. this.subProjects.push(project);
  2860. this.asset_matchers = this.asset_matchers.concat(project.asset_matchers);
  2861. this.sources = this.sources.concat(project.sources);
  2862. this.shader_matchers = this.shader_matchers.concat(project.shader_matchers);
  2863. this.defines = this.defines.concat(project.defines);
  2864. return project;
  2865. }
  2866. static create(directory) {
  2867. let project = load_project(path_resolve(directory), true);
  2868. let defines = [];
  2869. for (let define of defines) {
  2870. project.add_define(define);
  2871. }
  2872. return project;
  2873. }
  2874. }
  2875. function export_amake_project() {
  2876. console.log('Creating ' + goptions.target + ' project files.');
  2877. let project = Project.create(".");
  2878. if (goptions.graphics === "metal") {
  2879. project.add_cfiles(path_join("build", 'sources', '*'), {});
  2880. }
  2881. project.search_files(undefined);
  2882. project.internal_flatten();
  2883. fs_ensuredir("build");
  2884. let exporter = null;
  2885. if (goptions.ccompiler === "tcc") {
  2886. exporter = new MakeExporter(goptions.ccompiler, goptions.cppcompiler, "", "", "", "");
  2887. }
  2888. else if (goptions.target === 'ios' || goptions.target === 'macos') {
  2889. exporter = new XCodeExporter();
  2890. }
  2891. else if (goptions.target === 'android') {
  2892. exporter = new AndroidExporter();
  2893. }
  2894. else if (goptions.target === 'wasm') {
  2895. exporter = new WasmExporter();
  2896. }
  2897. else if (goptions.target === 'linux') {
  2898. exporter = new LinuxExporter();
  2899. }
  2900. else {
  2901. exporter = new VisualStudioExporter();
  2902. }
  2903. exporter.export_solution(project);
  2904. return project;
  2905. }
  2906. function compile_project(make, project) {
  2907. if (make.status != 0) {
  2908. os_exit(1);
  2909. }
  2910. let executable_name = project.get_safe_name();
  2911. if (project.get_executable_name()) {
  2912. executable_name = project.get_executable_name();
  2913. }
  2914. if (goptions.target === "linux") {
  2915. let from = path_resolve(path_join("build", goptions.build_path), executable_name);
  2916. let to = path_resolve(".", project.get_debug_dir(), executable_name);
  2917. fs_copyfile(from, to);
  2918. os_chmod(to, "+x");
  2919. }
  2920. else if (goptions.target === "windows") {
  2921. let from = path_join("x64", goptions.debug ? "Debug" : "Release", executable_name + ".exe");
  2922. let to = path_resolve("..", project.get_debug_dir(), executable_name + ".exe");
  2923. fs_copyfile(from, to);
  2924. }
  2925. if (goptions.run) {
  2926. if (goptions.target === "macos") {
  2927. os_exec("build/" + (goptions.debug ? "Debug" : "Release") + "/" + project.name + ".app/Contents/MacOS/" + project.name, [], { cwd: "build" });
  2928. }
  2929. else if (goptions.target === "linux") {
  2930. os_exec(path_resolve(".", project.get_debug_dir(), executable_name), [], { cwd: path_resolve(".", project.get_debug_dir()) });
  2931. }
  2932. else if (goptions.target === "windows") {
  2933. os_exec(path_resolve("..", project.get_debug_dir(), executable_name), [], { cwd: path_resolve(".", project.get_debug_dir()) });
  2934. }
  2935. }
  2936. }
  2937. function main() {
  2938. console.log('Using Iron from ' + irondir);
  2939. goptions.build_path = goptions.debug ? 'Debug' : 'Release';
  2940. let project = export_amake_project();
  2941. let project_name = project.get_safe_name();
  2942. if (goptions.compile && project_name !== '') {
  2943. console.log('Compiling...');
  2944. let make = null;
  2945. if (goptions.target == 'linux' || goptions.target == 'wasm' || goptions.ccompiler == "tcc") {
  2946. let cores = os_cpus_length();
  2947. make = os_exec('make', ['-j', cores.toString()], { cwd: path_join("build", goptions.build_path) });
  2948. }
  2949. else if (goptions.target == 'macos' || goptions.target == 'ios') {
  2950. let xcode_options = ['-configuration', goptions.debug ? 'Debug' : 'Release', '-project', project_name + '.xcodeproj'];
  2951. make = os_exec('xcodebuild', xcode_options, { cwd: "build" });
  2952. }
  2953. else if (goptions.target == 'windows') {
  2954. let vswhere = path_join(os_env('ProgramFiles(x86)'), 'Microsoft Visual Studio', 'Installer', 'vswhere.exe');
  2955. let vsvars = os_exec(vswhere, ['-products', '*', '-latest', '-find', 'VC\\Auxiliary\\Build\\vcvars64.bat']).stdout.trim();
  2956. fs_writefile(path_join("build", 'build.bat'), '@call "' + vsvars + '"\n' + '@MSBuild.exe "' + path_resolve("build", project_name + '.vcxproj') + '" /m /clp:ErrorsOnly /p:Configuration=' + (goptions.debug ? 'Debug' : 'Release') + ',Platform=x64');
  2957. make = os_exec('build.bat', [], { cwd: "build" });
  2958. }
  2959. else if (goptions.target == 'android') {
  2960. let gradlew = (os_platform() === 'win32') ? 'gradlew.bat' : 'bash';
  2961. let args = (os_platform() === 'win32') ? [] : ['gradlew'];
  2962. args.push('assemble' + (goptions.debug ? 'Debug' : 'Release'));
  2963. make = os_exec(gradlew, args, { cwd: path_join("build", project_name) });
  2964. }
  2965. if (make !== null) {
  2966. compile_project(make, project);
  2967. }
  2968. }
  2969. }
  2970. function default_target() {
  2971. if (os_platform() === 'linux') {
  2972. return 'linux';
  2973. }
  2974. else if (os_platform() === 'win32') {
  2975. return 'windows';
  2976. }
  2977. else {
  2978. return 'macos';
  2979. }
  2980. }
  2981. let goptions = {
  2982. target: default_target(),
  2983. graphics: 'default',
  2984. visualstudio: 'vs2022',
  2985. compile: false,
  2986. run: false,
  2987. debug: false,
  2988. ccompiler: 'clang',
  2989. cppcompiler: 'clang++',
  2990. arch: 'default',
  2991. alangjs: false,
  2992. js: false,
  2993. ashader: false,
  2994. };
  2995. let args = scriptArgs;
  2996. for (let i = 1; i < args.length; ++i) {
  2997. let arg = args[i];
  2998. if (arg.startsWith("--")) {
  2999. let name = arg.substring(2);
  3000. let value = true;
  3001. if (i < args.length - 1 && !args[i + 1].startsWith("--")) {
  3002. ++i;
  3003. value = args[i];
  3004. }
  3005. goptions[name] = value;
  3006. }
  3007. }
  3008. if (goptions.js) {
  3009. globalThis.std = std;
  3010. globalThis.fs_readfile = fs_readfile;
  3011. globalThis.fs_writefile = fs_writefile;
  3012. globalThis.fs_exists = fs_exists;
  3013. globalThis.fs_readdir = fs_readdir;
  3014. (1, eval)(fs_readfile(goptions.js));
  3015. std.exit();
  3016. }
  3017. if (goptions.ashader) {
  3018. let type = args[3];
  3019. let from = args[4];
  3020. let to = args[5];
  3021. amake.ashader(type, from, to);
  3022. std.exit();
  3023. }
  3024. if (goptions.run) {
  3025. goptions.compile = true;
  3026. }
  3027. if (goptions.graphics === "default") {
  3028. if (os_platform() === "win32") {
  3029. goptions.graphics = "direct3d12";
  3030. }
  3031. else if (os_platform() === "darwin") {
  3032. goptions.graphics = "metal";
  3033. }
  3034. else {
  3035. goptions.graphics = "vulkan";
  3036. }
  3037. }
  3038. let start = Date.now();
  3039. main();
  3040. console.log("Done in " + (Date.now() - start) + "ms.");