1
0

request.rs 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184
  1. //! # [`BuildRequest`] - the core of the build process
  2. //!
  3. //! The [`BuildRequest`] object is the core of the build process. It contains all the resolved arguments
  4. //! flowing in from the CLI, dioxus.toml, env vars, and the workspace.
  5. //!
  6. //! Every BuildRequest is tied to a given workspace and BuildArgs. For simplicity's sake, the BuildArgs
  7. //! struct is used to represent the CLI arguments and all other configuration is basically just
  8. //! extra CLI arguments, but in a configuration format.
  9. //!
  10. //! When [`BuildRequest::build`] is called, it will prepare its work directory in the target folder
  11. //! and then start running the build process. A [`BuildContext`] is required to customize this
  12. //! build process, containing a channel for progress updates and the build mode.
  13. //!
  14. //! The [`BuildMode`] is extremely important since it influences how the build is performed. Most
  15. //! "normal" builds just use [`BuildMode::Base`], but we also support [`BuildMode::Fat`] and
  16. //! [`BuildMode::Thin`]. These builds are used together to power the hot-patching and fast-linking
  17. //! engine.
  18. //! - BuildMode::Base: A normal build generated using `cargo rustc`
  19. //! - BuildMode::Fat: A "fat" build where all dependency rlibs are merged into a static library
  20. //! - BuildMode::Thin: A "thin" build that dynamically links against the artifacts produced by the "fat" build
  21. //!
  22. //! The BuildRequest is also responsible for writing the final build artifacts to disk. This includes
  23. //!
  24. //! - Writing the executable
  25. //! - Processing assets from the artifact
  26. //! - Writing any metadata or configuration files (Info.plist, AndroidManifest.xml)
  27. //! - Bundle splitting (for wasm) and wasm-bindgen
  28. //!
  29. //! In some cases, the BuildRequest also handles the linking of the final executable. Specifically,
  30. //! - For Android, we use `dx` as an opaque linker to dynamically find the true android linker
  31. //! - For hotpatching, the CLI manually links the final executable with a stub file
  32. //!
  33. //! ## Build formats:
  34. //!
  35. //! We support building for the most popular platforms:
  36. //! - Web via wasm-bindgen
  37. //! - macOS via app-bundle
  38. //! - iOS via app-bundle
  39. //! - Android via gradle
  40. //! - Linux via app-image
  41. //! - Windows via exe, msi/msix
  42. //!
  43. //! Note that we are missing some setups that we *should* support:
  44. //! - PWAs, WebWorkers, ServiceWorkers
  45. //! - Web Extensions
  46. //! - Linux via flatpak/snap
  47. //!
  48. //! There are some less popular formats that we might want to support eventually:
  49. //! - TVOS, watchOS
  50. //! - OpenHarmony
  51. //!
  52. //! Also, some deploy platforms have their own bespoke formats:
  53. //! - Cloudflare workers
  54. //! - AWS Lambda
  55. //!
  56. //! Currently, we defer most of our deploy-based bundling to Tauri bundle, though we should migrate
  57. //! to just bundling everything ourselves. This would require us to implement code-signing which
  58. //! is a bit of a pain, but fortunately a solved process (<https://github.com/rust-mobile/xbuild>).
  59. //!
  60. //! ## Build Structure
  61. //!
  62. //! Builds generally follow the same structure everywhere:
  63. //! - A main executable
  64. //! - Sidecars (alternate entrypoints, framewrok plugins, etc)
  65. //! - Assets (images, fonts, etc)
  66. //! - Metadata (Info.plist, AndroidManifest.xml)
  67. //! - Glue code (java, kotlin, javascript etc)
  68. //! - Entitlements for code-signing and verification
  69. //!
  70. //! We need to be careful to not try and put a "round peg in a square hole," but most platforms follow
  71. //! the same pattern.
  72. //!
  73. //! As such, we try to assemble a build directory that's somewhat sensible:
  74. //! - A main "staging" dir for a given app
  75. //! - Per-profile dirs (debug/release)
  76. //! - A platform dir (ie web/desktop/android/ios)
  77. //! - The "bundle" dir which is basically the `.app` format or `wwww` dir.
  78. //! - The "executable" dir where the main exe is housed
  79. //! - The "assets" dir where the assets are housed
  80. //! - The "meta" dir where stuff like Info.plist, AndroidManifest.xml, etc are housed
  81. //!
  82. //! There's also some "quirky" folders that need to be stable between builds but don't influence the
  83. //! bundle itself:
  84. //! - session_cache_dir which stores stuff like window position
  85. //!
  86. //! ### Web:
  87. //!
  88. //! Create a folder that is somewhat similar to an app-image (exe + asset)
  89. //! The server is dropped into the `web` folder, even if there's no `public` folder.
  90. //! If there's no server (SPA), we still use the `web` folder, but it only contains the
  91. //! public folder.
  92. //!
  93. //! ```
  94. //! web/
  95. //! server
  96. //! assets/
  97. //! public/
  98. //! index.html
  99. //! wasm/
  100. //! app.wasm
  101. //! glue.js
  102. //! snippets/
  103. //! ...
  104. //! assets/
  105. //! logo.png
  106. //! ```
  107. //!
  108. //! ### Linux:
  109. //!
  110. //! <https://docs.appimage.org/reference/appdir.html#ref-appdir>
  111. //! current_exe.join("Assets")
  112. //! ```
  113. //! app.appimage/
  114. //! AppRun
  115. //! app.desktop
  116. //! package.json
  117. //! assets/
  118. //! logo.png
  119. //! ```
  120. //!
  121. //! ### Macos
  122. //!
  123. //! We simply use the macos format where binaries are in `Contents/MacOS` and assets are in `Contents/Resources`
  124. //! We put assets in an assets dir such that it generally matches every other platform and we can
  125. //! output `/assets/blah` from manganis.
  126. //! ```
  127. //! App.app/
  128. //! Contents/
  129. //! Info.plist
  130. //! MacOS/
  131. //! Frameworks/
  132. //! Resources/
  133. //! assets/
  134. //! blah.icns
  135. //! blah.png
  136. //! CodeResources
  137. //! _CodeSignature/
  138. //! ```
  139. //!
  140. //! ### iOS
  141. //!
  142. //! Not the same as mac! ios apps are a bit "flattened" in comparison. simpler format, presumably
  143. //! since most ios apps don't ship frameworks/plugins and such.
  144. //!
  145. //! todo(jon): include the signing and entitlements in this format diagram.
  146. //! ```
  147. //! App.app/
  148. //! main
  149. //! assets/
  150. //! ```
  151. //!
  152. //! ### Android:
  153. //!
  154. //! Currently we need to generate a `src` type structure, not a pre-packaged apk structure, since
  155. //! we need to compile kotlin and java. This pushes us into using gradle and following a structure
  156. //! similar to that of cargo mobile2. Eventually I'd like to slim this down (drop buildSrc) and
  157. //! drive the kotlin build ourselves. This would let us drop gradle (yay! no plugins!) but requires
  158. //! us to manage dependencies (like kotlinc) ourselves (yuck!).
  159. //!
  160. //! <https://github.com/WanghongLin/miscellaneous/blob/master/tools/build-apk-manually.sh>
  161. //!
  162. //! Unfortunately, it seems that while we can drop the `android` build plugin, we still will need
  163. //! gradle since kotlin is basically gradle-only.
  164. //!
  165. //! Pre-build:
  166. //! ```
  167. //! app.apk/
  168. //! .gradle
  169. //! app/
  170. //! src/
  171. //! main/
  172. //! assets/
  173. //! jniLibs/
  174. //! java/
  175. //! kotlin/
  176. //! res/
  177. //! AndroidManifest.xml
  178. //! build.gradle.kts
  179. //! proguard-rules.pro
  180. //! buildSrc/
  181. //! build.gradle.kts
  182. //! src/
  183. //! main/
  184. //! kotlin/
  185. //! BuildTask.kt
  186. //! build.gradle.kts
  187. //! gradle.properties
  188. //! gradlew
  189. //! gradlew.bat
  190. //! settings.gradle
  191. //! ```
  192. //!
  193. //! Final build:
  194. //! ```
  195. //! app.apk/
  196. //! AndroidManifest.xml
  197. //! classes.dex
  198. //! assets/
  199. //! logo.png
  200. //! lib/
  201. //! armeabi-v7a/
  202. //! libmyapp.so
  203. //! arm64-v8a/
  204. //! libmyapp.so
  205. //! ```
  206. //! Notice that we *could* feasibly build this ourselves :)
  207. //!
  208. //! ### Windows:
  209. //! <https://superuser.com/questions/749447/creating-a-single-file-executable-from-a-directory-in-windows>
  210. //! Windows does not provide an AppImage format, so instead we're going build the same folder
  211. //! structure as an AppImage, but when distributing, we'll create a .exe that embeds the resources
  212. //! as an embedded .zip file. When the app runs, it will implicitly unzip its resources into the
  213. //! Program Files folder. Any subsequent launches of the parent .exe will simply call the AppRun.exe
  214. //! entrypoint in the associated Program Files folder.
  215. //!
  216. //! This is, in essence, the same as an installer, so we might eventually just support something like msi/msix
  217. //! which functionally do the same thing but with a sleeker UI.
  218. //!
  219. //! This means no installers are required and we can bake an updater into the host exe.
  220. //!
  221. //! ## Handling asset lookups:
  222. //! current_exe.join("assets")
  223. //! ```
  224. //! app.appimage/
  225. //! main.exe
  226. //! main.desktop
  227. //! package.json
  228. //! assets/
  229. //! logo.png
  230. //! ```
  231. //!
  232. //! Since we support just a few locations, we could just search for the first that exists
  233. //! - usr
  234. //! - ../Resources
  235. //! - assets
  236. //! - Assets
  237. //! - $cwd/assets
  238. //!
  239. //! ```
  240. //! assets::root() ->
  241. //! mac -> ../Resources/
  242. //! ios -> ../Resources/
  243. //! android -> assets/
  244. //! server -> assets/
  245. //! liveview -> assets/
  246. //! web -> /assets/
  247. //! root().join(bundled)
  248. //! ```
  249. //!
  250. //! Every dioxus app can have an optional server executable which will influence the final bundle.
  251. //! This is built in parallel with the app executable during the `build` phase and the progres/status
  252. //! of the build is aggregated.
  253. //!
  254. //! The server will *always* be dropped into the `web` folder since it is considered "web" in nature,
  255. //! and will likely need to be combined with the public dir to be useful.
  256. //!
  257. //! We do our best to assemble read-to-go bundles here, such that the "bundle" step for each platform
  258. //! can just use the build dir
  259. //!
  260. //! When we write the AppBundle to a folder, it'll contain each bundle for each platform under the app's name:
  261. //! ```
  262. //! dog-app/
  263. //! build/
  264. //! web/
  265. //! server.exe
  266. //! assets/
  267. //! some-secret-asset.txt (a server-side asset)
  268. //! public/
  269. //! index.html
  270. //! assets/
  271. //! logo.png
  272. //! desktop/
  273. //! App.app
  274. //! App.appimage
  275. //! App.exe
  276. //! server/
  277. //! server
  278. //! assets/
  279. //! some-secret-asset.txt (a server-side asset)
  280. //! ios/
  281. //! App.app
  282. //! App.ipa
  283. //! android/
  284. //! App.apk
  285. //! bundle/
  286. //! build.json
  287. //! Desktop.app
  288. //! Mobile_x64.ipa
  289. //! Mobile_arm64.ipa
  290. //! Mobile_rosetta.ipa
  291. //! web.appimage
  292. //! web/
  293. //! server.exe
  294. //! assets/
  295. //! some-secret-asset.txt
  296. //! public/
  297. //! index.html
  298. //! assets/
  299. //! logo.png
  300. //! style.css
  301. //! ```
  302. //!
  303. //! When deploying, the build.json file will provide all the metadata that dx-deploy will use to
  304. //! push the app to stores, set up infra, manage versions, etc.
  305. //!
  306. //! The format of each build will follow the name plus some metadata such that when distributing you
  307. //! can easily trim off the metadata.
  308. //!
  309. //! The idea here is that we can run any of the programs in the same way that they're deployed.
  310. //!
  311. //! ## Bundle structure links
  312. //! - apple: <https://developer.apple.com/documentation/bundleresources/placing_content_in_a_bundle>
  313. //! - appimage: <https://docs.appimage.org/packaging-guide/manual.html#ref-manual>
  314. //!
  315. //! ## Extra links
  316. //! - xbuild: <https://github.com/rust-mobile/xbuild/blob/master/xbuild/src/command/build.rs>
  317. use crate::{
  318. AndroidTools, BuildContext, DioxusConfig, Error, LinkAction, LinkerFlavor, Platform, Result,
  319. RustcArgs, TargetArgs, TraceSrc, WasmBindgen, WasmOptConfig, Workspace,
  320. DX_RUSTC_WRAPPER_ENV_VAR,
  321. };
  322. use anyhow::Context;
  323. use cargo_metadata::diagnostic::Diagnostic;
  324. use dioxus_cli_config::format_base_path_meta_element;
  325. use dioxus_cli_config::{APP_TITLE_ENV, ASSET_ROOT_ENV};
  326. use dioxus_cli_opt::{process_file_to, AssetManifest};
  327. use itertools::Itertools;
  328. use krates::{cm::TargetKind, NodeId};
  329. use manganis::{AssetOptions, JsAssetOptions};
  330. use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
  331. use serde::{Deserialize, Serialize};
  332. use std::{
  333. collections::{BTreeMap, HashSet},
  334. io::Write,
  335. path::{Path, PathBuf},
  336. process::Stdio,
  337. sync::{
  338. atomic::{AtomicUsize, Ordering},
  339. Arc,
  340. },
  341. time::{SystemTime, UNIX_EPOCH},
  342. };
  343. use target_lexicon::{OperatingSystem, Triple};
  344. use tempfile::{NamedTempFile, TempDir};
  345. use tokio::{io::AsyncBufReadExt, process::Command};
  346. use toml_edit::Item;
  347. use uuid::Uuid;
  348. use super::HotpatchModuleCache;
  349. /// This struct is used to plan the build process.
  350. ///
  351. /// The point here is to be able to take in the user's config from the CLI without modifying the
  352. /// arguments in place. Creating a buildplan "resolves" their config into a build plan that can be
  353. /// introspected. For example, the users might not specify a "Triple" in the CLI but the triple will
  354. /// be guaranteed to be resolved here.
  355. ///
  356. /// Creating a buildplan also lets us introspect build requests and modularize our build process.
  357. /// This will, however, lead to duplicate fields between the CLI and the build engine. This is fine
  358. /// since we have the freedom to evolve the schema internally without breaking the API.
  359. ///
  360. /// All updates from the build will be sent on a global "BuildProgress" channel.
  361. #[derive(Clone)]
  362. pub(crate) struct BuildRequest {
  363. pub(crate) workspace: Arc<Workspace>,
  364. pub(crate) config: DioxusConfig,
  365. pub(crate) crate_package: NodeId,
  366. pub(crate) crate_target: krates::cm::Target,
  367. pub(crate) profile: String,
  368. pub(crate) release: bool,
  369. pub(crate) platform: Platform,
  370. pub(crate) enabled_platforms: Vec<Platform>,
  371. pub(crate) triple: Triple,
  372. pub(crate) device: bool,
  373. pub(crate) package: String,
  374. pub(crate) features: Vec<String>,
  375. pub(crate) extra_cargo_args: Vec<String>,
  376. pub(crate) extra_rustc_args: Vec<String>,
  377. pub(crate) no_default_features: bool,
  378. pub(crate) target_dir: PathBuf,
  379. pub(crate) skip_assets: bool,
  380. pub(crate) wasm_split: bool,
  381. pub(crate) debug_symbols: bool,
  382. pub(crate) inject_loading_scripts: bool,
  383. pub(crate) custom_linker: Option<PathBuf>,
  384. pub(crate) session_cache_dir: Arc<TempDir>,
  385. pub(crate) link_args_file: Arc<NamedTempFile>,
  386. pub(crate) link_err_file: Arc<NamedTempFile>,
  387. pub(crate) rustc_wrapper_args_file: Arc<NamedTempFile>,
  388. }
  389. /// dx can produce different "modes" of a build. A "regular" build is a "base" build. The Fat and Thin
  390. /// modes are used together to achieve binary patching and linking.
  391. ///
  392. /// Guide:
  393. /// ----------
  394. /// - Base: A normal build generated using `cargo rustc`, intended for production use cases
  395. ///
  396. /// - Fat: A "fat" build with -Wl,-all_load and no_dead_strip, keeping *every* symbol in the binary.
  397. /// Intended for development for larger up-front builds with faster link times and the ability
  398. /// to binary patch the final binary. On WASM, this also forces wasm-bindgen to generate all
  399. /// JS-WASM bindings, saving us the need to re-wasmbindgen the final binary.
  400. ///
  401. /// - Thin: A "thin" build that dynamically links against the dependencies produced by the "fat" build.
  402. /// This is generated by calling rustc *directly* and might be more fragile to construct, but
  403. /// generates *much* faster than a regular base or fat build.
  404. #[derive(Clone, Debug, PartialEq)]
  405. pub enum BuildMode {
  406. /// A normal build generated using `cargo rustc`
  407. Base,
  408. /// A "Fat" build generated with cargo rustc and dx as a custom linker without -Wl,-dead-strip
  409. Fat,
  410. /// A "thin" build generated with `rustc` directly and dx as a custom linker
  411. Thin {
  412. rustc_args: RustcArgs,
  413. changed_files: Vec<PathBuf>,
  414. aslr_reference: u64,
  415. cache: Arc<HotpatchModuleCache>,
  416. },
  417. }
  418. /// The end result of a build.
  419. ///
  420. /// Contains the final asset manifest, the executable, and metadata about the build.
  421. /// Note that the `exe` might be stale and/or overwritten by the time you read it!
  422. ///
  423. /// The patch cache is only populated on fat builds and then used for thin builds (see `BuildMode::Thin`).
  424. #[derive(Clone, Debug)]
  425. pub struct BuildArtifacts {
  426. pub(crate) platform: Platform,
  427. pub(crate) exe: PathBuf,
  428. pub(crate) direct_rustc: RustcArgs,
  429. pub(crate) time_start: SystemTime,
  430. pub(crate) time_end: SystemTime,
  431. pub(crate) assets: AssetManifest,
  432. pub(crate) mode: BuildMode,
  433. pub(crate) patch_cache: Option<Arc<HotpatchModuleCache>>,
  434. }
  435. pub(crate) static PROFILE_WASM: &str = "wasm-dev";
  436. pub(crate) static PROFILE_ANDROID: &str = "android-dev";
  437. pub(crate) static PROFILE_SERVER: &str = "server-dev";
  438. impl BuildRequest {
  439. /// Create a new build request.
  440. ///
  441. /// This method consolidates various inputs into a single source of truth. It combines:
  442. /// - Command-line arguments provided by the user.
  443. /// - The crate's `Cargo.toml`.
  444. /// - The `dioxus.toml` configuration file.
  445. /// - User-specific CLI settings.
  446. /// - The workspace metadata.
  447. /// - Host-specific details (e.g., Android tools, installed frameworks).
  448. /// - The intended target platform.
  449. ///
  450. /// Fields may be duplicated from the inputs to allow for autodetection and resolution.
  451. ///
  452. /// Autodetection is performed for unspecified fields where possible.
  453. ///
  454. /// Note: Build requests are typically created only when the CLI is invoked or when significant
  455. /// changes are detected in the `Cargo.toml` (e.g., features added or removed).
  456. pub(crate) async fn new(args: &TargetArgs, workspace: Arc<Workspace>) -> Result<Self> {
  457. let crate_package = workspace.find_main_package(args.package.clone())?;
  458. let config = workspace
  459. .load_dioxus_config(crate_package)?
  460. .unwrap_or_default();
  461. let target_kind = match args.example.is_some() {
  462. true => TargetKind::Example,
  463. false => TargetKind::Bin,
  464. };
  465. let main_package = &workspace.krates[crate_package];
  466. let target_name = args
  467. .example
  468. .clone()
  469. .or(args.bin.clone())
  470. .or_else(|| {
  471. if let Some(default_run) = &main_package.default_run {
  472. return Some(default_run.to_string());
  473. }
  474. let bin_count = main_package
  475. .targets
  476. .iter()
  477. .filter(|x| x.kind.contains(&target_kind))
  478. .count();
  479. if bin_count != 1 {
  480. return None;
  481. }
  482. main_package.targets.iter().find_map(|x| {
  483. if x.kind.contains(&target_kind) {
  484. Some(x.name.clone())
  485. } else {
  486. None
  487. }
  488. })
  489. })
  490. .unwrap_or(workspace.krates[crate_package].name.clone());
  491. let crate_target = main_package
  492. .targets
  493. .iter()
  494. .find(|target| {
  495. target_name == target.name.as_str() && target.kind.contains(&target_kind)
  496. })
  497. .with_context(|| {
  498. let target_of_kind = |kind|-> String {
  499. let filtered_packages = main_package
  500. .targets
  501. .iter()
  502. .filter_map(|target| {
  503. target.kind.contains(kind).then_some(target.name.as_str())
  504. }).collect::<Vec<_>>();
  505. filtered_packages.join(", ")};
  506. if let Some(example) = &args.example {
  507. let examples = target_of_kind(&TargetKind::Example);
  508. format!("Failed to find example {example}. \nAvailable examples are:\n{}", examples)
  509. } else if let Some(bin) = &args.bin {
  510. let binaries = target_of_kind(&TargetKind::Bin);
  511. format!("Failed to find binary {bin}. \nAvailable binaries are:\n{}", binaries)
  512. } else {
  513. format!("Failed to find target {target_name}. \nIt looks like you are trying to build dioxus in a library crate. \
  514. You either need to run dx from inside a binary crate or build a specific example with the `--example` flag. \
  515. Available examples are:\n{}", target_of_kind(&TargetKind::Example))
  516. }
  517. })?
  518. .clone();
  519. // The crate might enable multiple platforms or no platforms at
  520. // We collect all the platforms it enables first and then select based on the --platform arg
  521. let enabled_platforms =
  522. Self::enabled_cargo_toml_platforms(main_package, args.no_default_features);
  523. let using_dioxus_explicitly = main_package
  524. .dependencies
  525. .iter()
  526. .any(|dep| dep.name == "dioxus");
  527. let mut features = args.features.clone();
  528. let mut no_default_features = args.no_default_features;
  529. let platform: Platform = match args.platform {
  530. Some(platform) => match enabled_platforms.len() {
  531. 0 => platform,
  532. // The user passed --platform XYZ but already has `default = ["ABC"]` in their Cargo.toml or dioxus = { features = ["abc"] }
  533. // We want to strip out the default platform and use the one they passed, setting no-default-features
  534. _ => {
  535. features.extend(Self::platformless_features(main_package));
  536. no_default_features = true;
  537. platform
  538. }
  539. },
  540. None if !using_dioxus_explicitly => Platform::autodetect_from_cargo_feature("desktop").unwrap(),
  541. None => match enabled_platforms.len() {
  542. 0 => return Err(anyhow::anyhow!("No platform specified and no platform marked as default in Cargo.toml. Try specifying a platform with `--platform`").into()),
  543. 1 => enabled_platforms[0],
  544. _ => {
  545. return Err(anyhow::anyhow!(
  546. "Multiple platforms enabled in Cargo.toml. Please specify a platform with `--platform` or set a default platform in Cargo.toml"
  547. )
  548. .into())
  549. }
  550. },
  551. };
  552. // Add any features required to turn on the client
  553. if using_dioxus_explicitly {
  554. features.push(Self::feature_for_platform(main_package, platform));
  555. }
  556. // Set the profile of the build if it's not already set
  557. // This is mostly used for isolation of builds (preventing thrashing) but also useful to have multiple performance profiles
  558. // We might want to move some of these profiles into dioxus.toml and make them "virtual".
  559. let profile = match args.profile.clone() {
  560. Some(profile) => profile,
  561. None if args.release => "release".to_string(),
  562. None => match platform {
  563. Platform::Android => PROFILE_ANDROID.to_string(),
  564. Platform::Web => PROFILE_WASM.to_string(),
  565. Platform::Server => PROFILE_SERVER.to_string(),
  566. _ => "dev".to_string(),
  567. },
  568. };
  569. // Determining release mode is based on the profile, actually, so we need to check that
  570. let release = workspace.is_release_profile(&profile);
  571. // Determine the --package we'll pass to cargo.
  572. // todo: I think this might be wrong - we don't want to use main_package necessarily...
  573. let package = args
  574. .package
  575. .clone()
  576. .unwrap_or_else(|| main_package.name.clone());
  577. // We usually use the simulator unless --device is passed *or* a device is detected by probing.
  578. // For now, though, since we don't have probing, it just defaults to false
  579. // Tools like xcrun/adb can detect devices
  580. let device = args.device.unwrap_or(false);
  581. // We want a real triple to build with, so we'll autodetect it if it's not provided
  582. // The triple ends up being a source of truth for us later hence all this work to figure it out
  583. let triple = match args.target.clone() {
  584. Some(target) => target,
  585. None => match platform {
  586. // Generally just use the host's triple for native executables unless specified otherwise
  587. Platform::MacOS
  588. | Platform::Windows
  589. | Platform::Linux
  590. | Platform::Server
  591. | Platform::Liveview => target_lexicon::HOST,
  592. // We currently assume unknown-unknown for web, but we might want to eventually
  593. // support emscripten
  594. Platform::Web => "wasm32-unknown-unknown".parse().unwrap(),
  595. // For iOS we should prefer the actual architecture for the simulator, but in lieu of actually
  596. // figuring that out, we'll assume aarch64 on m-series and x86_64 otherwise
  597. Platform::Ios => {
  598. // use the host's architecture and sim if --device is passed
  599. use target_lexicon::{Architecture, HOST};
  600. match HOST.architecture {
  601. Architecture::Aarch64(_) if device => "aarch64-apple-ios".parse().unwrap(),
  602. Architecture::Aarch64(_) => "aarch64-apple-ios-sim".parse().unwrap(),
  603. _ if device => "x86_64-apple-ios".parse().unwrap(),
  604. _ => "x86_64-apple-ios-sim".parse().unwrap(),
  605. }
  606. }
  607. // Same idea with android but we figure out the connected device using adb
  608. Platform::Android => {
  609. workspace
  610. .android_tools()?
  611. .autodetect_android_device_triple()
  612. .await
  613. }
  614. },
  615. };
  616. // Somethings we override are also present in the user's config.
  617. // If we can't get them by introspecting cargo, then we need to get them from the config
  618. let cargo_config = cargo_config2::Config::load().unwrap();
  619. let mut custom_linker = if platform == Platform::Android {
  620. Some(workspace.android_tools()?.android_cc(&triple))
  621. } else {
  622. None
  623. };
  624. if let Ok(Some(linker)) = cargo_config.linker(triple.to_string()) {
  625. custom_linker = Some(linker);
  626. }
  627. let target_dir = std::env::var("CARGO_TARGET_DIR")
  628. .ok()
  629. .map(PathBuf::from)
  630. .or_else(|| cargo_config.build.target_dir.clone())
  631. .unwrap_or_else(|| workspace.workspace_root().join("target"));
  632. // Set up some tempfiles so we can do some IPC between us and the linker/rustc wrapper (which is occasionally us!)
  633. let link_args_file = Arc::new(
  634. NamedTempFile::with_suffix(".txt")
  635. .context("Failed to create temporary file for linker args")?,
  636. );
  637. let link_err_file = Arc::new(
  638. NamedTempFile::with_suffix(".txt")
  639. .context("Failed to create temporary file for linker args")?,
  640. );
  641. let rustc_wrapper_args_file = Arc::new(
  642. NamedTempFile::with_suffix(".json")
  643. .context("Failed to create temporary file for rustc wrapper args")?,
  644. );
  645. let session_cache_dir = Arc::new(
  646. TempDir::new().context("Failed to create temporary directory for session cache")?,
  647. );
  648. let extra_rustc_args = shell_words::split(&args.rustc_args.clone().unwrap_or_default())
  649. .context("Failed to parse rustc args")?;
  650. let extra_cargo_args = shell_words::split(&args.cargo_args.clone().unwrap_or_default())
  651. .context("Failed to parse cargo args")?;
  652. tracing::debug!(
  653. r#"Log Files:
  654. • link_args_file: {},
  655. • link_err_file: {},
  656. • rustc_wrapper_args_file: {},
  657. • session_cache_dir: {}
  658. • linker: {:?}
  659. • target_dir: {:?}
  660. "#,
  661. link_args_file.path().display(),
  662. link_err_file.path().display(),
  663. rustc_wrapper_args_file.path().display(),
  664. session_cache_dir.path().display(),
  665. custom_linker,
  666. target_dir,
  667. );
  668. Ok(Self {
  669. platform,
  670. features,
  671. no_default_features,
  672. crate_package,
  673. crate_target,
  674. profile,
  675. triple,
  676. device,
  677. workspace,
  678. config,
  679. enabled_platforms,
  680. target_dir,
  681. custom_linker,
  682. link_args_file,
  683. link_err_file,
  684. session_cache_dir,
  685. rustc_wrapper_args_file,
  686. extra_rustc_args,
  687. extra_cargo_args,
  688. release,
  689. package,
  690. skip_assets: args.skip_assets,
  691. wasm_split: args.wasm_split,
  692. debug_symbols: args.debug_symbols,
  693. inject_loading_scripts: args.inject_loading_scripts,
  694. })
  695. }
  696. pub(crate) async fn build(&self, ctx: &BuildContext) -> Result<BuildArtifacts> {
  697. // If we forget to do this, then we won't get the linker args since rust skips the full build
  698. // We need to make sure to not react to this though, so the filemap must cache it
  699. _ = self.bust_fingerprint(ctx);
  700. // Run the cargo build to produce our artifacts
  701. let mut artifacts = self.cargo_build(ctx).await?;
  702. // Write the build artifacts to the bundle on the disk
  703. match &ctx.mode {
  704. BuildMode::Thin {
  705. aslr_reference,
  706. cache,
  707. rustc_args,
  708. ..
  709. } => {
  710. self.write_patch(ctx, *aslr_reference, &mut artifacts, cache, rustc_args)
  711. .await?;
  712. }
  713. BuildMode::Base | BuildMode::Fat => {
  714. ctx.status_start_bundle();
  715. self.write_executable(ctx, &artifacts.exe, &mut artifacts.assets)
  716. .await
  717. .context("Failed to write main executable")?;
  718. self.write_frameworks(ctx, &artifacts.direct_rustc)
  719. .await
  720. .context("Failed to write frameworks")?;
  721. self.write_assets(ctx, &artifacts.assets)
  722. .await
  723. .context("Failed to write assets")?;
  724. self.write_metadata().await?;
  725. self.optimize(ctx).await?;
  726. self.assemble(ctx)
  727. .await
  728. .context("Failed to assemble app bundle")?;
  729. tracing::debug!("Bundle created at {}", self.root_dir().display());
  730. }
  731. }
  732. // Populate the patch cache if we're in fat mode
  733. if matches!(ctx.mode, BuildMode::Fat) {
  734. artifacts.patch_cache = Some(Arc::new(self.create_patch_cache(&artifacts.exe).await?));
  735. }
  736. Ok(artifacts)
  737. }
  738. /// Run the cargo build by assembling the build command and executing it.
  739. ///
  740. /// This method needs to be very careful with processing output since errors being swallowed will
  741. /// be very confusing to the user.
  742. async fn cargo_build(&self, ctx: &BuildContext) -> Result<BuildArtifacts> {
  743. let time_start = SystemTime::now();
  744. // Extract the unit count of the crate graph so build_cargo has more accurate data
  745. // "Thin" builds only build the final exe, so we only need to build one crate
  746. let crate_count = match ctx.mode {
  747. BuildMode::Thin { .. } => 1,
  748. _ => self.get_unit_count_estimate(ctx).await,
  749. };
  750. // Update the status to show that we're starting the build and how many crates we expect to build
  751. ctx.status_starting_build(crate_count);
  752. let mut cmd = self.build_command(ctx)?;
  753. tracing::debug!(dx_src = ?TraceSrc::Build, "Executing cargo for {} using {}", self.platform, self.triple);
  754. let mut child = cmd
  755. .stdout(Stdio::piped())
  756. .stderr(Stdio::piped())
  757. .spawn()
  758. .context("Failed to spawn cargo build")?;
  759. let stdout = tokio::io::BufReader::new(child.stdout.take().unwrap());
  760. let stderr = tokio::io::BufReader::new(child.stderr.take().unwrap());
  761. let mut output_location: Option<PathBuf> = None;
  762. let mut stdout = stdout.lines();
  763. let mut stderr = stderr.lines();
  764. let mut units_compiled = 0;
  765. let mut emitting_error = false;
  766. loop {
  767. use cargo_metadata::Message;
  768. let line = tokio::select! {
  769. Ok(Some(line)) = stdout.next_line() => line,
  770. Ok(Some(line)) = stderr.next_line() => line,
  771. else => break,
  772. };
  773. let Some(Ok(message)) = Message::parse_stream(std::io::Cursor::new(line)).next() else {
  774. continue;
  775. };
  776. match message {
  777. Message::BuildScriptExecuted(_) => units_compiled += 1,
  778. Message::CompilerMessage(msg) => ctx.status_build_diagnostic(msg.message),
  779. Message::TextLine(line) => {
  780. // Handle the case where we're getting lines directly from rustc.
  781. // These are in a different format than the normal cargo output, though I imagine
  782. // this parsing code is quite fragile/sensitive to changes in cargo, cargo_metadata, rustc, etc.
  783. #[derive(Deserialize)]
  784. struct RustcArtifact {
  785. artifact: PathBuf,
  786. emit: String,
  787. }
  788. // These outputs look something like:
  789. //
  790. // { "artifact":"target/debug/deps/libdioxus_core-4f2a0b3c1e5f8b7c.rlib", "emit":"link" }
  791. //
  792. // There are other outputs like depinfo that we might be interested in in the future.
  793. if let Ok(artifact) = serde_json::from_str::<RustcArtifact>(&line) {
  794. if artifact.emit == "link" {
  795. output_location = Some(artifact.artifact);
  796. }
  797. }
  798. // Handle direct rustc diagnostics
  799. if let Ok(diag) = serde_json::from_str::<Diagnostic>(&line) {
  800. ctx.status_build_diagnostic(diag);
  801. }
  802. // For whatever reason, if there's an error while building, we still receive the TextLine
  803. // instead of an "error" message. However, the following messages *also* tend to
  804. // be the error message, and don't start with "error:". So we'll check if we've already
  805. // emitted an error message and if so, we'll emit all following messages as errors too.
  806. //
  807. // todo: This can lead to some really ugly output though, so we might want to look
  808. // into a more reliable way to detect errors propagating out of the compiler. If
  809. // we always wrapped rustc, then we could store this data somewhere in a much more
  810. // reliable format.
  811. if line.trim_start().starts_with("error:") {
  812. emitting_error = true;
  813. }
  814. // Note that previous text lines might have set emitting_error to true
  815. match emitting_error {
  816. true => ctx.status_build_error(line),
  817. false => ctx.status_build_message(line),
  818. }
  819. }
  820. Message::CompilerArtifact(artifact) => {
  821. units_compiled += 1;
  822. ctx.status_build_progress(units_compiled, crate_count, artifact.target.name);
  823. output_location = artifact.executable.map(Into::into);
  824. }
  825. // todo: this can occasionally swallow errors, so we should figure out what exactly is going wrong
  826. // since that is a really bad user experience.
  827. Message::BuildFinished(finished) => {
  828. if !finished.success {
  829. return Err(anyhow::anyhow!(
  830. "Cargo build failed, signaled by the compiler. Toggle tracing mode (press `t`) for more information."
  831. )
  832. .into());
  833. }
  834. }
  835. _ => {}
  836. }
  837. }
  838. // Accumulate the rustc args from the wrapper, if they exist and can be parsed.
  839. let mut direct_rustc = RustcArgs::default();
  840. if let Ok(res) = std::fs::read_to_string(self.rustc_wrapper_args_file.path()) {
  841. if let Ok(res) = serde_json::from_str(&res) {
  842. direct_rustc = res;
  843. }
  844. }
  845. // If there's any warnings from the linker, we should print them out
  846. if let Ok(linker_warnings) = std::fs::read_to_string(self.link_err_file.path()) {
  847. if !linker_warnings.is_empty() {
  848. if output_location.is_none() {
  849. tracing::error!("Linker warnings: {}", linker_warnings);
  850. } else {
  851. tracing::debug!("Linker warnings: {}", linker_warnings);
  852. }
  853. }
  854. }
  855. // Collect the linker args from the and update the rustc args
  856. direct_rustc.link_args = std::fs::read_to_string(self.link_args_file.path())
  857. .context("Failed to read link args from file")?
  858. .lines()
  859. .map(|s| s.to_string())
  860. .collect::<Vec<_>>();
  861. let exe = output_location.context("Cargo build failed - no output location. Toggle tracing mode (press `t`) for more information.")?;
  862. // Fat builds need to be linked with the fat linker. Would also like to link here for thin builds
  863. if matches!(ctx.mode, BuildMode::Fat) {
  864. let link_start = SystemTime::now();
  865. self.run_fat_link(ctx, &exe, &direct_rustc).await?;
  866. tracing::debug!(
  867. "Fat linking completed in {}us",
  868. SystemTime::now()
  869. .duration_since(link_start)
  870. .unwrap()
  871. .as_micros()
  872. );
  873. }
  874. let assets = self.collect_assets(&exe, ctx)?;
  875. let time_end = SystemTime::now();
  876. let mode = ctx.mode.clone();
  877. let platform = self.platform;
  878. tracing::debug!(
  879. "Build completed successfully in {}us: {:?}",
  880. time_end.duration_since(time_start).unwrap().as_micros(),
  881. exe
  882. );
  883. Ok(BuildArtifacts {
  884. time_end,
  885. platform,
  886. exe,
  887. direct_rustc,
  888. time_start,
  889. assets,
  890. mode,
  891. patch_cache: None,
  892. })
  893. }
  894. /// Traverse the target directory and collect all assets from the incremental cache
  895. ///
  896. /// This uses "known paths" that have stayed relatively stable during cargo's lifetime.
  897. /// One day this system might break and we might need to go back to using the linker approach.
  898. fn collect_assets(&self, exe: &Path, ctx: &BuildContext) -> Result<AssetManifest> {
  899. // walk every file in the incremental cache dir, reading and inserting items into the manifest.
  900. let mut manifest = AssetManifest::default();
  901. // And then add from the exe directly, just in case it's LTO compiled and has no incremental cache
  902. if !self.skip_assets {
  903. ctx.status_extracting_assets();
  904. _ = manifest.add_from_object_path(exe);
  905. }
  906. Ok(manifest)
  907. }
  908. /// Take the output of rustc and make it into the main exe of the bundle
  909. ///
  910. /// For wasm, we'll want to run `wasm-bindgen` to make it a wasm binary along with some other optimizations
  911. /// Other platforms we might do some stripping or other optimizations
  912. /// Move the executable to the workdir
  913. async fn write_executable(
  914. &self,
  915. ctx: &BuildContext,
  916. exe: &Path,
  917. assets: &mut AssetManifest,
  918. ) -> Result<()> {
  919. match self.platform {
  920. // Run wasm-bindgen on the wasm binary and set its output to be in the bundle folder
  921. // Also run wasm-opt on the wasm binary, and sets the index.html since that's also the "executable".
  922. //
  923. // The wasm stuff will be in a folder called "wasm" in the workdir.
  924. //
  925. // Final output format:
  926. // ```
  927. // dx/
  928. // app/
  929. // web/
  930. // bundle/
  931. // build/
  932. // server.exe
  933. // public/
  934. // index.html
  935. // wasm/
  936. // app.wasm
  937. // glue.js
  938. // snippets/
  939. // ...
  940. // assets/
  941. // logo.png
  942. // ```
  943. Platform::Web => {
  944. self.bundle_web(ctx, exe, assets).await?;
  945. }
  946. // this will require some extra oomf to get the multi architecture builds...
  947. // for now, we just copy the exe into the current arch (which, sorry, is hardcoded for my m1)
  948. // we'll want to do multi-arch builds in the future, so there won't be *one* exe dir to worry about
  949. // eventually `exe_dir` and `main_exe` will need to take in an arch and return the right exe path
  950. //
  951. // todo(jon): maybe just symlink this rather than copy it?
  952. // we might want to eventually use the objcopy logic to handle this
  953. //
  954. // https://github.com/rust-mobile/xbuild/blob/master/xbuild/template/lib.rs
  955. // https://github.com/rust-mobile/xbuild/blob/master/apk/src/lib.rs#L19
  956. //
  957. // These are all super simple, just copy the exe into the folder
  958. // eventually, perhaps, maybe strip + encrypt the exe?
  959. Platform::Android
  960. | Platform::MacOS
  961. | Platform::Windows
  962. | Platform::Linux
  963. | Platform::Ios
  964. | Platform::Liveview
  965. | Platform::Server => {
  966. std::fs::create_dir_all(self.exe_dir())?;
  967. std::fs::copy(exe, self.main_exe())?;
  968. }
  969. }
  970. Ok(())
  971. }
  972. async fn write_frameworks(&self, _ctx: &BuildContext, direct_rustc: &RustcArgs) -> Result<()> {
  973. let framework_dir = self.frameworks_folder();
  974. for arg in &direct_rustc.link_args {
  975. // todo - how do we handle windows dlls? we don't want to bundle the system dlls
  976. // for now, we don't do anything with dlls, and only use .dylibs and .so files
  977. if arg.ends_with(".dylib") | arg.ends_with(".so") {
  978. let from = PathBuf::from(arg);
  979. let to = framework_dir.join(from.file_name().unwrap());
  980. _ = std::fs::remove_file(&to);
  981. tracing::debug!("Copying framework from {from:?} to {to:?}");
  982. _ = std::fs::create_dir_all(&framework_dir);
  983. // in dev and on normal oses, we want to symlink the file
  984. // otherwise, just copy it (since in release you want to distribute the framework)
  985. if cfg!(any(windows, unix)) && !self.release {
  986. #[cfg(windows)]
  987. std::os::windows::fs::symlink_file(from, to).with_context(|| {
  988. "Failed to symlink framework into bundle: {from:?} -> {to:?}"
  989. })?;
  990. #[cfg(unix)]
  991. std::os::unix::fs::symlink(from, to).with_context(|| {
  992. "Failed to symlink framework into bundle: {from:?} -> {to:?}"
  993. })?;
  994. } else {
  995. std::fs::copy(from, to)?;
  996. }
  997. }
  998. }
  999. Ok(())
  1000. }
  1001. fn frameworks_folder(&self) -> PathBuf {
  1002. match self.triple.operating_system {
  1003. OperatingSystem::Darwin(_) | OperatingSystem::MacOSX(_) => {
  1004. self.root_dir().join("Contents").join("Frameworks")
  1005. }
  1006. OperatingSystem::IOS(_) => self.root_dir().join("Frameworks"),
  1007. OperatingSystem::Linux | OperatingSystem::Windows => self.root_dir(),
  1008. _ => self.root_dir(),
  1009. }
  1010. }
  1011. /// Copy the assets out of the manifest and into the target location
  1012. ///
  1013. /// Should be the same on all platforms - just copy over the assets from the manifest into the output directory
  1014. async fn write_assets(&self, ctx: &BuildContext, assets: &AssetManifest) -> Result<()> {
  1015. // Server doesn't need assets - web will provide them
  1016. if self.platform == Platform::Server {
  1017. return Ok(());
  1018. }
  1019. let asset_dir = self.asset_dir();
  1020. // First, clear the asset dir of any files that don't exist in the new manifest
  1021. _ = std::fs::create_dir_all(&asset_dir);
  1022. // Create a set of all the paths that new files will be bundled to
  1023. let mut keep_bundled_output_paths: HashSet<_> = assets
  1024. .assets
  1025. .values()
  1026. .map(|a| asset_dir.join(a.bundled_path()))
  1027. .collect();
  1028. // The CLI creates a .version file in the asset dir to keep track of what version of the optimizer
  1029. // the asset was processed. If that version doesn't match the CLI version, we need to re-optimize
  1030. // all assets.
  1031. let version_file = self.asset_optimizer_version_file();
  1032. let clear_cache = std::fs::read_to_string(&version_file)
  1033. .ok()
  1034. .filter(|s| s == crate::VERSION.as_str())
  1035. .is_none();
  1036. if clear_cache {
  1037. keep_bundled_output_paths.clear();
  1038. }
  1039. tracing::trace!(
  1040. "Keeping bundled output paths: {:#?}",
  1041. keep_bundled_output_paths
  1042. );
  1043. // use walkdir::WalkDir;
  1044. // for item in WalkDir::new(&asset_dir).into_iter().flatten() {
  1045. // // If this asset is in the manifest, we don't need to remove it
  1046. // let canonicalized = dunce::canonicalize(item.path())?;
  1047. // if !keep_bundled_output_paths.contains(canonicalized.as_path()) {
  1048. // // Remove empty dirs, remove files not in the manifest
  1049. // if item.file_type().is_dir() && item.path().read_dir()?.next().is_none() {
  1050. // std::fs::remove_dir(item.path())?;
  1051. // } else {
  1052. // std::fs::remove_file(item.path())?;
  1053. // }
  1054. // }
  1055. // }
  1056. // todo(jon): we also want to eventually include options for each asset's optimization and compression, which we currently aren't
  1057. let mut assets_to_transfer = vec![];
  1058. // Queue the bundled assets
  1059. for (asset, bundled) in &assets.assets {
  1060. let from = asset.clone();
  1061. let to = asset_dir.join(bundled.bundled_path());
  1062. // prefer to log using a shorter path relative to the workspace dir by trimming the workspace dir
  1063. let from_ = from
  1064. .strip_prefix(self.workspace_dir())
  1065. .unwrap_or(from.as_path());
  1066. let to_ = from
  1067. .strip_prefix(self.workspace_dir())
  1068. .unwrap_or(to.as_path());
  1069. tracing::debug!("Copying asset {from_:?} to {to_:?}");
  1070. assets_to_transfer.push((from, to, *bundled.options()));
  1071. }
  1072. let asset_count = assets_to_transfer.len();
  1073. let started_processing = AtomicUsize::new(0);
  1074. let copied = AtomicUsize::new(0);
  1075. // Parallel Copy over the assets and keep track of progress with an atomic counter
  1076. let progress = ctx.tx.clone();
  1077. let ws_dir = self.workspace_dir();
  1078. // Optimizing assets is expensive and blocking, so we do it in a tokio spawn blocking task
  1079. tokio::task::spawn_blocking(move || {
  1080. assets_to_transfer
  1081. .par_iter()
  1082. .try_for_each(|(from, to, options)| {
  1083. let processing = started_processing.fetch_add(1, Ordering::SeqCst);
  1084. let from_ = from.strip_prefix(&ws_dir).unwrap_or(from);
  1085. tracing::trace!(
  1086. "Starting asset copy {processing}/{asset_count} from {from_:?}"
  1087. );
  1088. let res = process_file_to(options, from, to);
  1089. if let Err(err) = res.as_ref() {
  1090. tracing::error!("Failed to copy asset {from:?}: {err}");
  1091. }
  1092. let finished = copied.fetch_add(1, Ordering::SeqCst);
  1093. BuildContext::status_copied_asset(
  1094. &progress,
  1095. finished,
  1096. asset_count,
  1097. from.to_path_buf(),
  1098. );
  1099. res.map(|_| ())
  1100. })
  1101. })
  1102. .await
  1103. .map_err(|e| anyhow::anyhow!("A task failed while trying to copy assets: {e}"))??;
  1104. // // Remove the wasm bindgen output directory if it exists
  1105. // _ = std::fs::remove_dir_all(self.wasm_bindgen_out_dir());
  1106. // Write the version file so we know what version of the optimizer we used
  1107. std::fs::write(self.asset_optimizer_version_file(), crate::VERSION.as_str())?;
  1108. Ok(())
  1109. }
  1110. /// Run our custom linker setup to generate a patch file in the right location
  1111. ///
  1112. /// This should be the only case where the cargo output is a "dummy" file and requires us to
  1113. /// manually do any linking.
  1114. ///
  1115. /// We also run some post processing steps here, like extracting out any new assets.
  1116. async fn write_patch(
  1117. &self,
  1118. ctx: &BuildContext,
  1119. aslr_reference: u64,
  1120. artifacts: &mut BuildArtifacts,
  1121. cache: &Arc<HotpatchModuleCache>,
  1122. rustc_args: &RustcArgs,
  1123. ) -> Result<()> {
  1124. ctx.status_hotpatching();
  1125. tracing::debug!(
  1126. "Original builds for patch: {}",
  1127. self.link_args_file.path().display()
  1128. );
  1129. let raw_args = std::fs::read_to_string(self.link_args_file.path())
  1130. .context("Failed to read link args from file")?;
  1131. let args = raw_args.lines().collect::<Vec<_>>();
  1132. // Extract out the incremental object files.
  1133. //
  1134. // This is sadly somewhat of a hack, but it might be a moderately reliable hack.
  1135. //
  1136. // When rustc links your project, it passes the args as how a linker would expect, but with
  1137. // a somewhat reliable ordering. These are all internal details to cargo/rustc, so we can't
  1138. // rely on them *too* much, but the *are* fundamental to how rust compiles your projects, and
  1139. // linker interfaces probably won't change drastically for another 40 years.
  1140. //
  1141. // We need to tear apart this command and only pass the args that are relevant to our thin link.
  1142. // Mainly, we don't want any rlibs to be linked. Occasionally some libraries like objc_exception
  1143. // export a folder with their artifacts - unsure if we actually need to include them. Generally
  1144. // you can err on the side that most *libraries* don't need to be linked here since dlopen
  1145. // satisfies those symbols anyways when the binary is loaded.
  1146. //
  1147. // Many args are passed twice, too, which can be confusing, but generally don't have any real
  1148. // effect. Note that on macos/ios, there's a special macho header that needs to be set, otherwise
  1149. // dyld will complain.
  1150. //
  1151. // Also, some flags in darwin land might become deprecated, need to be super conservative:
  1152. // - https://developer.apple.com/forums/thread/773907
  1153. //
  1154. // The format of this command roughly follows:
  1155. // ```
  1156. // clang
  1157. // /dioxus/target/debug/subsecond-cli
  1158. // /var/folders/zs/gvrfkj8x33d39cvw2p06yc700000gn/T/rustcAqQ4p2/symbols.o
  1159. // /dioxus/target/subsecond-dev/deps/subsecond_harness-acfb69cb29ffb8fa.05stnb4bovskp7a00wyyf7l9s.rcgu.o
  1160. // /dioxus/target/subsecond-dev/deps/subsecond_harness-acfb69cb29ffb8fa.08rgcutgrtj2mxoogjg3ufs0g.rcgu.o
  1161. // /dioxus/target/subsecond-dev/deps/subsecond_harness-acfb69cb29ffb8fa.0941bd8fa2bydcv9hfmgzzne9.rcgu.o
  1162. // /dioxus/target/subsecond-dev/deps/libbincode-c215feeb7886f81b.rlib
  1163. // /dioxus/target/subsecond-dev/deps/libanyhow-e69ac15c094daba6.rlib
  1164. // /dioxus/target/subsecond-dev/deps/libratatui-c3364579b86a1dfc.rlib
  1165. // /.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-019f0f6ae6e6562b.rlib
  1166. // /.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-7387d38173a2eb37.rlib
  1167. // /.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-2b03cf6ece171d21.rlib
  1168. // -framework AppKit
  1169. // -lc
  1170. // -framework Foundation
  1171. // -framework Carbon
  1172. // -lSystem
  1173. // -framework CoreFoundation
  1174. // -lobjc
  1175. // -liconv
  1176. // -lm
  1177. // -arch arm64
  1178. // -mmacosx-version-min=11.0.0
  1179. // -L /dioxus/target/subsecond-dev/build/objc_exception-dc226cad0480ea65/out
  1180. // -o /dioxus/target/subsecond-dev/deps/subsecond_harness-acfb69cb29ffb8fa
  1181. // -nodefaultlibs
  1182. // -Wl,-all_load
  1183. // ```
  1184. let mut dylibs = vec![];
  1185. let mut object_files = args
  1186. .iter()
  1187. .filter(|arg| arg.ends_with(".rcgu.o"))
  1188. .sorted()
  1189. .map(PathBuf::from)
  1190. .collect::<Vec<_>>();
  1191. // On non-wasm platforms, we generate a special shim object file which converts symbols from
  1192. // fat binary into direct addresses from the running process.
  1193. //
  1194. // Our wasm approach is quite specific to wasm. We don't need to resolve any missing symbols
  1195. // there since wasm is relocatable, but there is considerable pre and post processing work to
  1196. // satisfy undefined symbols that we do by munging the binary directly.
  1197. //
  1198. // todo: can we adjust our wasm approach to also use a similar system?
  1199. // todo: don't require the aslr reference and just patch the got when loading.
  1200. //
  1201. // Requiring the ASLR offset here is necessary but unfortunately might be flakey in practice.
  1202. // Android apps can take a long time to open, and a hot patch might've been issued in the interim,
  1203. // making this hotpatch a failure.
  1204. if self.platform != Platform::Web {
  1205. let stub_bytes = crate::build::create_undefined_symbol_stub(
  1206. cache,
  1207. &object_files,
  1208. &self.triple,
  1209. aslr_reference,
  1210. )
  1211. .expect("failed to resolve patch symbols");
  1212. // Currently we're dropping stub.o in the exe dir, but should probably just move to a tempfile?
  1213. let patch_file = self.main_exe().with_file_name("stub.o");
  1214. std::fs::write(&patch_file, stub_bytes)?;
  1215. object_files.push(patch_file);
  1216. // Add the dylibs/sos to the linker args
  1217. // Make sure to use the one in the bundle, not the ones in the target dir or system.
  1218. for arg in &rustc_args.link_args {
  1219. if arg.ends_with(".dylib") || arg.ends_with(".so") {
  1220. let path = PathBuf::from(arg);
  1221. dylibs.push(self.frameworks_folder().join(path.file_name().unwrap()));
  1222. }
  1223. }
  1224. }
  1225. // And now we can run the linker with our new args
  1226. let linker = self.select_linker()?;
  1227. let out_exe = self.patch_exe(artifacts.time_start);
  1228. let out_arg = match self.triple.operating_system {
  1229. OperatingSystem::Windows => vec![format!("/OUT:{}", out_exe.display())],
  1230. _ => vec!["-o".to_string(), out_exe.display().to_string()],
  1231. };
  1232. tracing::trace!("Linking with {:?} using args: {:#?}", linker, object_files);
  1233. // Run the linker directly!
  1234. //
  1235. // We dump its output directly into the patch exe location which is different than how rustc
  1236. // does it since it uses llvm-objcopy into the `target/debug/` folder.
  1237. let res = Command::new(linker)
  1238. .args(object_files.iter())
  1239. .args(dylibs.iter())
  1240. .args(self.thin_link_args(&args)?)
  1241. .args(out_arg)
  1242. .env_clear()
  1243. .envs(rustc_args.envs.iter().map(|(k, v)| (k, v)))
  1244. .output()
  1245. .await?;
  1246. if !res.stderr.is_empty() {
  1247. let errs = String::from_utf8_lossy(&res.stderr);
  1248. if !self.patch_exe(artifacts.time_start).exists() || !res.status.success() {
  1249. tracing::error!("Failed to generate patch: {}", errs.trim());
  1250. } else {
  1251. tracing::trace!("Linker output during thin linking: {}", errs.trim());
  1252. }
  1253. }
  1254. // For some really weird reason that I think is because of dlopen caching, future loads of the
  1255. // jump library will fail if we don't remove the original fat file. I think this could be
  1256. // because of library versioning and namespaces, but really unsure.
  1257. //
  1258. // The errors if you forget to do this are *extremely* cryptic - missing symbols that never existed.
  1259. //
  1260. // Fortunately, this binary exists in two places - the deps dir and the target out dir. We
  1261. // can just remove the one in the deps dir and the problem goes away.
  1262. if let Some(idx) = args.iter().position(|arg| *arg == "-o") {
  1263. _ = std::fs::remove_file(PathBuf::from(args[idx + 1]));
  1264. }
  1265. // Now extract the assets from the fat binary
  1266. artifacts
  1267. .assets
  1268. .add_from_object_path(&self.patch_exe(artifacts.time_start))?;
  1269. // Clean up the temps manually
  1270. // todo: we might want to keep them around for debugging purposes
  1271. for file in object_files {
  1272. _ = std::fs::remove_file(file);
  1273. }
  1274. Ok(())
  1275. }
  1276. /// Take the original args passed to the "fat" build and then create the "thin" variant.
  1277. ///
  1278. /// This is basically just stripping away the rlibs and other libraries that will be satisfied
  1279. /// by our stub step.
  1280. fn thin_link_args(&self, original_args: &[&str]) -> Result<Vec<String>> {
  1281. let mut out_args = vec![];
  1282. match self.linker_flavor() {
  1283. // wasm32-unknown-unknown -> use wasm-ld (gnu-lld)
  1284. //
  1285. // We need to import a few things - namely the memory and ifunc table.
  1286. //
  1287. // We can safely export everything, I believe, though that led to issues with the "fat"
  1288. // binaries that also might lead to issues here too. wasm-bindgen chokes on some symbols
  1289. // and the resulting JS has issues.
  1290. //
  1291. // We turn on both --pie and --experimental-pic but I think we only need --pie.
  1292. //
  1293. // We don't use *any* of the original linker args since they do lots of custom exports
  1294. // and other things that we don't need.
  1295. //
  1296. // The trickiest one here is -Crelocation-model=pic, which forces data symbols
  1297. // into a GOT, making it possible to import them from the main module.
  1298. //
  1299. // I think we can make relocation-model=pic work for non-wasm platforms, enabling
  1300. // fully relocatable modules with no host coordination in lieu of sending out
  1301. // the aslr slide at runtime.
  1302. LinkerFlavor::WasmLld => {
  1303. out_args.extend([
  1304. "--fatal-warnings".to_string(),
  1305. "--verbose".to_string(),
  1306. "--import-memory".to_string(),
  1307. "--import-table".to_string(),
  1308. "--growable-table".to_string(),
  1309. "--export".to_string(),
  1310. "main".to_string(),
  1311. "--allow-undefined".to_string(),
  1312. "--no-demangle".to_string(),
  1313. "--no-entry".to_string(),
  1314. "--pie".to_string(),
  1315. "--experimental-pic".to_string(),
  1316. ]);
  1317. }
  1318. // This uses "cc" and these args need to be ld compatible
  1319. //
  1320. // Most importantly, we want to pass `-dylib` to both CC and the linker to indicate that
  1321. // we want to generate the shared library instead of an executable.
  1322. LinkerFlavor::Darwin => {
  1323. out_args.extend(["-Wl,-dylib".to_string()]);
  1324. // Preserve the original args. We only preserve:
  1325. // -framework
  1326. // -arch
  1327. // -lxyz
  1328. // There might be more, but some flags might break our setup.
  1329. for (idx, arg) in original_args.iter().enumerate() {
  1330. if *arg == "-framework" || *arg == "-arch" || *arg == "-L" {
  1331. out_args.push(arg.to_string());
  1332. out_args.push(original_args[idx + 1].to_string());
  1333. }
  1334. if arg.starts_with("-l") || arg.starts_with("-m") {
  1335. out_args.push(arg.to_string());
  1336. }
  1337. }
  1338. }
  1339. // android/linux need to be compatible with lld
  1340. //
  1341. // android currently drags along its own libraries and other zany flags
  1342. LinkerFlavor::Gnu => {
  1343. out_args.extend([
  1344. "-shared".to_string(),
  1345. "-Wl,--eh-frame-hdr".to_string(),
  1346. "-Wl,-z,noexecstack".to_string(),
  1347. "-Wl,-z,relro,-z,now".to_string(),
  1348. "-nodefaultlibs".to_string(),
  1349. "-Wl,-Bdynamic".to_string(),
  1350. ]);
  1351. // Preserve the original args. We only preserve:
  1352. // -L <path>
  1353. // -arch
  1354. // -lxyz
  1355. // There might be more, but some flags might break our setup.
  1356. for (idx, arg) in original_args.iter().enumerate() {
  1357. if *arg == "-L" {
  1358. out_args.push(arg.to_string());
  1359. out_args.push(original_args[idx + 1].to_string());
  1360. }
  1361. if arg.starts_with("-l")
  1362. || arg.starts_with("-m")
  1363. || arg.starts_with("-Wl,--target=")
  1364. || arg.starts_with("-Wl,-fuse-ld")
  1365. {
  1366. out_args.push(arg.to_string());
  1367. }
  1368. }
  1369. }
  1370. LinkerFlavor::Msvc => {
  1371. out_args.extend([
  1372. "shlwapi.lib".to_string(),
  1373. "kernel32.lib".to_string(),
  1374. "advapi32.lib".to_string(),
  1375. "ntdll.lib".to_string(),
  1376. "userenv.lib".to_string(),
  1377. "ws2_32.lib".to_string(),
  1378. "dbghelp.lib".to_string(),
  1379. "/defaultlib:msvcrt".to_string(),
  1380. "/DLL".to_string(),
  1381. "/DEBUG".to_string(),
  1382. "/PDBALTPATH:%_PDB%".to_string(),
  1383. "/EXPORT:main".to_string(),
  1384. "/HIGHENTROPYVA:NO".to_string(),
  1385. ]);
  1386. }
  1387. LinkerFlavor::Unsupported => {
  1388. return Err(anyhow::anyhow!("Unsupported platform for thin linking").into())
  1389. }
  1390. }
  1391. let extract_value = |arg: &str| -> Option<String> {
  1392. original_args
  1393. .iter()
  1394. .position(|a| *a == arg)
  1395. .map(|i| original_args[i + 1].to_string())
  1396. };
  1397. if let Some(vale) = extract_value("-target") {
  1398. out_args.push("-target".to_string());
  1399. out_args.push(vale);
  1400. }
  1401. if let Some(vale) = extract_value("-isysroot") {
  1402. out_args.push("-isysroot".to_string());
  1403. out_args.push(vale);
  1404. }
  1405. Ok(out_args)
  1406. }
  1407. /// Patches are stored in the same directory as the main executable, but with a name based on the
  1408. /// time the patch started compiling.
  1409. ///
  1410. /// - lib{name}-patch-{time}.(so/dll/dylib) (next to the main exe)
  1411. ///
  1412. /// Note that weirdly enough, the name of dylibs can actually matter. In some environments, libs
  1413. /// can override each other with symbol interposition.
  1414. ///
  1415. /// Also, on Android - and some Linux, we *need* to start the lib name with `lib` for the dynamic
  1416. /// loader to consider it a shared library.
  1417. ///
  1418. /// todo: the time format might actually be problematic if two platforms share the same build folder.
  1419. pub(crate) fn patch_exe(&self, time_start: SystemTime) -> PathBuf {
  1420. let path = self.main_exe().with_file_name(format!(
  1421. "lib{}-patch-{}",
  1422. self.executable_name(),
  1423. time_start
  1424. .duration_since(UNIX_EPOCH)
  1425. .map(|f| f.as_millis())
  1426. .unwrap_or(0),
  1427. ));
  1428. let extension = match self.linker_flavor() {
  1429. LinkerFlavor::Darwin => "dylib",
  1430. LinkerFlavor::Gnu => "so",
  1431. LinkerFlavor::WasmLld => "wasm",
  1432. LinkerFlavor::Msvc => "dll",
  1433. LinkerFlavor::Unsupported => "",
  1434. };
  1435. path.with_extension(extension)
  1436. }
  1437. /// When we link together the fat binary, we need to make sure every `.o` file in *every* rlib
  1438. /// is taken into account. This is the same work that the rust compiler does when assembling
  1439. /// staticlibs.
  1440. ///
  1441. /// <https://github.com/rust-lang/rust/blob/191df20fcad9331d3a948aa8e8556775ec3fe69d/compiler/rustc_codegen_ssa/src/back/link.rs#L448>
  1442. ///
  1443. /// Since we're going to be passing these to the linker, we need to make sure and not provide any
  1444. /// weird files (like the rmeta) file that rustc generates.
  1445. ///
  1446. /// We discovered the need for this after running into issues with wasm-ld not being able to
  1447. /// handle the rmeta file.
  1448. ///
  1449. /// <https://github.com/llvm/llvm-project/issues/55786>
  1450. ///
  1451. /// Also, crates might not drag in all their dependent code. The monorphizer won't lift trait-based generics:
  1452. ///
  1453. /// <https://github.com/rust-lang/rust/blob/191df20fcad9331d3a948aa8e8556775ec3fe69d/compiler/rustc_monomorphize/src/collector.rs>
  1454. ///
  1455. /// When Rust normally handles this, it uses the +whole-archive directive which adjusts how the rlib
  1456. /// is written to disk.
  1457. ///
  1458. /// Since creating this object file can be a lot of work, we cache it in the target dir by hashing
  1459. /// the names of the rlibs in the command and storing it in the target dir. That way, when we run
  1460. /// this command again, we can just used the cached object file.
  1461. ///
  1462. /// In theory, we only need to do this for every crate accessible by the current crate, but that's
  1463. /// hard acquire without knowing the exported symbols from each crate.
  1464. ///
  1465. /// todo: I think we can traverse our immediate dependencies and inspect their symbols, unless they `pub use` a crate
  1466. /// todo: we should try and make this faster with memmapping
  1467. pub(crate) async fn run_fat_link(
  1468. &self,
  1469. ctx: &BuildContext,
  1470. exe: &Path,
  1471. rustc_args: &RustcArgs,
  1472. ) -> Result<()> {
  1473. ctx.status_starting_link();
  1474. // Filter out the rlib files from the arguments
  1475. let rlibs = rustc_args
  1476. .link_args
  1477. .iter()
  1478. .filter(|arg| arg.ends_with(".rlib"))
  1479. .map(PathBuf::from)
  1480. .collect::<Vec<_>>();
  1481. // Acquire a hash from the rlib names, sizes, modified times, and dx's git commit hash
  1482. // This ensures that any changes in dx or the rlibs will cause a new hash to be generated
  1483. // The hash relies on both dx and rustc hashes, so it should be thoroughly unique. Keep it
  1484. // short to avoid long file names.
  1485. let hash_id = Uuid::new_v5(
  1486. &Uuid::NAMESPACE_OID,
  1487. rlibs
  1488. .iter()
  1489. .map(|p| {
  1490. format!(
  1491. "{}-{}-{}-{}",
  1492. p.file_name().unwrap().to_string_lossy(),
  1493. p.metadata().map(|m| m.len()).unwrap_or_default(),
  1494. p.metadata()
  1495. .ok()
  1496. .and_then(|m| m.modified().ok())
  1497. .and_then(|f| f.duration_since(UNIX_EPOCH).map(|f| f.as_secs()).ok())
  1498. .unwrap_or_default(),
  1499. crate::dx_build_info::GIT_COMMIT_HASH.unwrap_or_default()
  1500. )
  1501. })
  1502. .collect::<String>()
  1503. .as_bytes(),
  1504. )
  1505. .to_string()
  1506. .chars()
  1507. .take(8)
  1508. .collect::<String>();
  1509. // Check if we already have a cached object file
  1510. let out_ar_path = exe.with_file_name(format!("libdeps-{hash_id}.a",));
  1511. let out_rlibs_list = exe.with_file_name(format!("rlibs-{hash_id}.txt"));
  1512. let mut archive_has_contents = out_ar_path.exists();
  1513. // Use the rlibs list if it exists
  1514. let mut compiler_rlibs = std::fs::read_to_string(&out_rlibs_list)
  1515. .ok()
  1516. .map(|s| s.lines().map(PathBuf::from).collect::<Vec<_>>())
  1517. .unwrap_or_default();
  1518. // Create it by dumping all the rlibs into it
  1519. // This will include the std rlibs too, which can severely bloat the size of the archive
  1520. //
  1521. // The nature of this process involves making extremely fat archives, so we should try and
  1522. // speed up the future linking process by caching the archive.
  1523. //
  1524. // Since we're using the git hash for the CLI entropy, debug builds should always regenerate
  1525. // the archive since their hash might not change, but the logic might.
  1526. if !archive_has_contents || cfg!(debug_assertions) {
  1527. compiler_rlibs.clear();
  1528. let mut bytes = vec![];
  1529. let mut out_ar = ar::Builder::new(&mut bytes);
  1530. for rlib in &rlibs {
  1531. // Skip compiler rlibs since they're missing bitcode
  1532. //
  1533. // https://github.com/rust-lang/rust/issues/94232#issuecomment-1048342201
  1534. //
  1535. // if the rlib is not in the target directory, we skip it.
  1536. if !rlib.starts_with(self.workspace_dir()) {
  1537. compiler_rlibs.push(rlib.clone());
  1538. tracing::trace!("Skipping rlib: {:?}", rlib);
  1539. continue;
  1540. }
  1541. tracing::trace!("Adding rlib to staticlib: {:?}", rlib);
  1542. let rlib_contents = std::fs::read(rlib)?;
  1543. let mut reader = ar::Archive::new(std::io::Cursor::new(rlib_contents));
  1544. while let Some(Ok(object_file)) = reader.next_entry() {
  1545. let name = std::str::from_utf8(object_file.header().identifier()).unwrap();
  1546. if name.ends_with(".rmeta") {
  1547. continue;
  1548. }
  1549. if object_file.header().size() == 0 {
  1550. continue;
  1551. }
  1552. // rlibs might contain dlls/sos/lib files which we don't want to include
  1553. if name.ends_with(".dll")
  1554. || name.ends_with(".so")
  1555. || name.ends_with(".lib")
  1556. || name.ends_with(".dylib")
  1557. {
  1558. compiler_rlibs.push(rlib.to_owned());
  1559. continue;
  1560. }
  1561. if !(name.ends_with(".o") || name.ends_with(".obj")) {
  1562. tracing::debug!("Unknown object file in rlib: {:?}", name);
  1563. }
  1564. archive_has_contents = true;
  1565. out_ar
  1566. .append(&object_file.header().clone(), object_file)
  1567. .context("Failed to add object file to archive")?;
  1568. }
  1569. }
  1570. let bytes = out_ar.into_inner().context("Failed to finalize archive")?;
  1571. std::fs::write(&out_ar_path, bytes).context("Failed to write archive")?;
  1572. tracing::debug!("Wrote fat archive to {:?}", out_ar_path);
  1573. // Run the ranlib command to index the archive. This slows down this process a bit,
  1574. // but is necessary for some linkers to work properly.
  1575. // We ignore its error in case it doesn't recognize the architecture
  1576. if self.linker_flavor() == LinkerFlavor::Darwin {
  1577. if let Some(ranlib) = self.select_ranlib() {
  1578. _ = Command::new(ranlib).arg(&out_ar_path).output().await;
  1579. }
  1580. }
  1581. }
  1582. compiler_rlibs.dedup();
  1583. // We're going to replace the first rlib in the args with our fat archive
  1584. // And then remove the rest of the rlibs
  1585. //
  1586. // We also need to insert the -force_load flag to force the linker to load the archive
  1587. let mut args = rustc_args.link_args.clone();
  1588. if let Some(last_object) = args.iter().rposition(|arg| arg.ends_with(".o")) {
  1589. if archive_has_contents {
  1590. match self.linker_flavor() {
  1591. LinkerFlavor::WasmLld => {
  1592. args.insert(last_object, "--whole-archive".to_string());
  1593. args.insert(last_object + 1, out_ar_path.display().to_string());
  1594. args.insert(last_object + 2, "--no-whole-archive".to_string());
  1595. args.retain(|arg| !arg.ends_with(".rlib"));
  1596. for rlib in compiler_rlibs.iter().rev() {
  1597. args.insert(last_object + 3, rlib.display().to_string());
  1598. }
  1599. }
  1600. LinkerFlavor::Gnu => {
  1601. args.insert(last_object, "-Wl,--whole-archive".to_string());
  1602. args.insert(last_object + 1, out_ar_path.display().to_string());
  1603. args.insert(last_object + 2, "-Wl,--no-whole-archive".to_string());
  1604. args.retain(|arg| !arg.ends_with(".rlib"));
  1605. for rlib in compiler_rlibs.iter().rev() {
  1606. args.insert(last_object + 3, rlib.display().to_string());
  1607. }
  1608. }
  1609. LinkerFlavor::Darwin => {
  1610. args.insert(last_object, "-Wl,-force_load".to_string());
  1611. args.insert(last_object + 1, out_ar_path.display().to_string());
  1612. args.retain(|arg| !arg.ends_with(".rlib"));
  1613. for rlib in compiler_rlibs.iter().rev() {
  1614. args.insert(last_object + 2, rlib.display().to_string());
  1615. }
  1616. }
  1617. LinkerFlavor::Msvc => {
  1618. args.insert(
  1619. last_object,
  1620. format!("/WHOLEARCHIVE:{}", out_ar_path.display()),
  1621. );
  1622. args.retain(|arg| !arg.ends_with(".rlib"));
  1623. for rlib in compiler_rlibs.iter().rev() {
  1624. args.insert(last_object + 1, rlib.display().to_string());
  1625. }
  1626. }
  1627. LinkerFlavor::Unsupported => {
  1628. tracing::error!("Unsupported platform for fat linking");
  1629. }
  1630. };
  1631. }
  1632. }
  1633. // Add custom args to the linkers
  1634. match self.linker_flavor() {
  1635. LinkerFlavor::Gnu => {
  1636. // Export `main` so subsecond can use it for a reference point
  1637. args.push("-Wl,--export-dynamic-symbol,main".to_string());
  1638. }
  1639. LinkerFlavor::Darwin => {
  1640. // `-all_load` is an extra step to ensure that all symbols are loaded (different than force_load)
  1641. args.push("-Wl,-all_load".to_string());
  1642. }
  1643. LinkerFlavor::Msvc => {
  1644. // Prevent alsr from overflowing 32 bits
  1645. args.push("/HIGHENTROPYVA:NO".to_string());
  1646. // Export `main` so subsecond can use it for a reference point
  1647. args.push("/EXPORT:main".to_string());
  1648. }
  1649. LinkerFlavor::WasmLld | LinkerFlavor::Unsupported => {}
  1650. }
  1651. // We also need to remove the `-o` flag since we want the linker output to end up in the
  1652. // rust exe location, not in the deps dir as it normally would.
  1653. if let Some(idx) = args
  1654. .iter()
  1655. .position(|arg| *arg == "-o" || *arg == "--output")
  1656. {
  1657. args.remove(idx + 1);
  1658. args.remove(idx);
  1659. }
  1660. // same but windows support
  1661. if let Some(idx) = args.iter().position(|arg| arg.starts_with("/OUT")) {
  1662. args.remove(idx);
  1663. }
  1664. // We want to go through wasm-ld directly, so we need to remove the -flavor flag
  1665. if self.platform == Platform::Web {
  1666. let flavor_idx = args.iter().position(|arg| *arg == "-flavor").unwrap();
  1667. args.remove(flavor_idx + 1);
  1668. args.remove(flavor_idx);
  1669. }
  1670. // And now we can run the linker with our new args
  1671. let linker = self.select_linker()?;
  1672. tracing::trace!("Fat linking with args: {:?} {:#?}", linker, args);
  1673. tracing::trace!("Fat linking with env: {:#?}", rustc_args.envs);
  1674. // Run the linker directly!
  1675. let out_arg = match self.triple.operating_system {
  1676. OperatingSystem::Windows => vec![format!("/OUT:{}", exe.display())],
  1677. _ => vec!["-o".to_string(), exe.display().to_string()],
  1678. };
  1679. let res = Command::new(linker)
  1680. .args(args.iter().skip(1))
  1681. .args(out_arg)
  1682. .env_clear()
  1683. .envs(rustc_args.envs.iter().map(|(k, v)| (k, v)))
  1684. .output()
  1685. .await?;
  1686. if !res.stderr.is_empty() {
  1687. let errs = String::from_utf8_lossy(&res.stderr);
  1688. if !res.status.success() {
  1689. tracing::error!("Failed to generate fat binary: {}", errs.trim());
  1690. } else {
  1691. tracing::trace!("Warnings during fat linking: {}", errs.trim());
  1692. }
  1693. }
  1694. if !res.stdout.is_empty() {
  1695. let out = String::from_utf8_lossy(&res.stdout);
  1696. tracing::trace!("Output from fat linking: {}", out.trim());
  1697. }
  1698. // Clean up the temps manually
  1699. for f in args.iter().filter(|arg| arg.ends_with(".rcgu.o")) {
  1700. _ = std::fs::remove_file(f);
  1701. }
  1702. // Cache the rlibs list
  1703. _ = std::fs::write(
  1704. &out_rlibs_list,
  1705. compiler_rlibs
  1706. .into_iter()
  1707. .map(|s| s.display().to_string())
  1708. .join("\n"),
  1709. );
  1710. Ok(())
  1711. }
  1712. /// Automatically detect the linker flavor based on the target triple and any custom linkers.
  1713. ///
  1714. /// This tries to replicate what rustc does when selecting the linker flavor based on the linker
  1715. /// and triple.
  1716. fn linker_flavor(&self) -> LinkerFlavor {
  1717. if let Some(custom) = self.custom_linker.as_ref() {
  1718. let name = custom.file_name().unwrap().to_ascii_lowercase();
  1719. match name.to_str() {
  1720. Some("lld-link") => return LinkerFlavor::Msvc,
  1721. Some("lld-link.exe") => return LinkerFlavor::Msvc,
  1722. Some("wasm-ld") => return LinkerFlavor::WasmLld,
  1723. Some("ld64.lld") => return LinkerFlavor::Darwin,
  1724. Some("ld.lld") => return LinkerFlavor::Gnu,
  1725. Some("ld.gold") => return LinkerFlavor::Gnu,
  1726. Some("mold") => return LinkerFlavor::Gnu,
  1727. Some("sold") => return LinkerFlavor::Gnu,
  1728. Some("wild") => return LinkerFlavor::Gnu,
  1729. _ => {}
  1730. }
  1731. }
  1732. match self.triple.environment {
  1733. target_lexicon::Environment::Gnu
  1734. | target_lexicon::Environment::Gnuabi64
  1735. | target_lexicon::Environment::Gnueabi
  1736. | target_lexicon::Environment::Gnueabihf
  1737. | target_lexicon::Environment::GnuLlvm => LinkerFlavor::Gnu,
  1738. target_lexicon::Environment::Musl => LinkerFlavor::Gnu,
  1739. target_lexicon::Environment::Android => LinkerFlavor::Gnu,
  1740. target_lexicon::Environment::Msvc => LinkerFlavor::Msvc,
  1741. target_lexicon::Environment::Macabi => LinkerFlavor::Darwin,
  1742. _ => match self.triple.operating_system {
  1743. OperatingSystem::Darwin(_) => LinkerFlavor::Darwin,
  1744. OperatingSystem::IOS(_) => LinkerFlavor::Darwin,
  1745. OperatingSystem::MacOSX(_) => LinkerFlavor::Darwin,
  1746. OperatingSystem::Linux => LinkerFlavor::Gnu,
  1747. OperatingSystem::Windows => LinkerFlavor::Msvc,
  1748. _ => match self.triple.architecture {
  1749. target_lexicon::Architecture::Wasm32 => LinkerFlavor::WasmLld,
  1750. target_lexicon::Architecture::Wasm64 => LinkerFlavor::WasmLld,
  1751. _ => LinkerFlavor::Unsupported,
  1752. },
  1753. },
  1754. }
  1755. }
  1756. /// Select the linker to use for this platform.
  1757. ///
  1758. /// We prefer to use the rust-lld linker when we can since it's usually there.
  1759. /// On macos, we use the system linker since macho files can be a bit finicky.
  1760. ///
  1761. /// This means we basically ignore the linker flavor that the user configured, which could
  1762. /// cause issues with a custom linker setup. In theory, rust translates most flags to the right
  1763. /// linker format.
  1764. fn select_linker(&self) -> Result<PathBuf, Error> {
  1765. // Use a custom linker for non-crosscompile and crosscompile targets
  1766. if matches!(
  1767. self.triple.operating_system,
  1768. OperatingSystem::Darwin(_) | OperatingSystem::Linux | OperatingSystem::Windows
  1769. ) {
  1770. if let Ok(linker) = std::env::var("DX_HOST_LINKER") {
  1771. return Ok(PathBuf::from(linker));
  1772. }
  1773. }
  1774. if let Ok(linker) = std::env::var("DX_LINKER") {
  1775. return Ok(PathBuf::from(linker));
  1776. }
  1777. if let Some(linker) = self.custom_linker.clone() {
  1778. return Ok(linker);
  1779. }
  1780. let cc = match self.linker_flavor() {
  1781. LinkerFlavor::WasmLld => self.workspace.wasm_ld(),
  1782. // On macOS, we use the system linker since it's usually there.
  1783. // We could also use `lld` here, but it might not be installed by default.
  1784. //
  1785. // Note that this is *clang*, not `lld`.
  1786. LinkerFlavor::Darwin => self.workspace.cc(),
  1787. // On Linux, we use the system linker since it's usually there.
  1788. LinkerFlavor::Gnu => self.workspace.cc(),
  1789. // On windows, instead of trying to find the system linker, we just go with the lld.link
  1790. // that rustup provides. It's faster and more stable then reyling on link.exe in path.
  1791. LinkerFlavor::Msvc => self.workspace.lld_link(),
  1792. // The rest of the platforms use `cc` as the linker which should be available in your path,
  1793. // provided you have build-tools setup. On mac/linux this is the default, but on Windows
  1794. // it requires msvc or gnu downloaded, which is a requirement to use rust anyways.
  1795. //
  1796. // The default linker might actually be slow though, so we could consider using lld or rust-lld
  1797. // since those are shipping by default on linux as of 1.86. Window's linker is the really slow one.
  1798. //
  1799. // https://blog.rust-lang.org/2024/05/17/enabling-rust-lld-on-linux.html
  1800. //
  1801. // Note that "cc" is *not* a linker. It's a compiler! The arguments we pass need to be in
  1802. // the form of `-Wl,<args>` for them to make it to the linker. This matches how rust does it
  1803. // which is confusing.
  1804. LinkerFlavor::Unsupported => self.workspace.cc(),
  1805. };
  1806. Ok(cc)
  1807. }
  1808. /// Assemble the `cargo rustc` / `rustc` command
  1809. ///
  1810. /// When building fat/base binaries, we use `cargo rustc`.
  1811. /// When building thin binaries, we use `rustc` directly.
  1812. ///
  1813. /// When processing the output of this command, you need to make sure to handle both cases which
  1814. /// both have different formats (but with json output for both).
  1815. fn build_command(&self, ctx: &BuildContext) -> Result<Command> {
  1816. match &ctx.mode {
  1817. // We're assembling rustc directly, so we need to be *very* careful. Cargo sets rustc's
  1818. // env up very particularly, and we want to match it 1:1 but with some changes.
  1819. //
  1820. // To do this, we reset the env completely, and then pass every env var that the original
  1821. // rustc process had 1:1.
  1822. //
  1823. // We need to unset a few things, like the RUSTC wrappers and then our special env var
  1824. // indicating that dx itself is the compiler. If we forget to do this, then the compiler
  1825. // ends up doing some recursive nonsense and dx is trying to link instead of compiling.
  1826. //
  1827. // todo: maybe rustc needs to be found on the FS instead of using the one in the path?
  1828. BuildMode::Thin { rustc_args, .. } => {
  1829. let mut cmd = Command::new("rustc");
  1830. cmd.current_dir(self.workspace_dir());
  1831. cmd.env_clear();
  1832. cmd.args(rustc_args.args[1..].iter());
  1833. cmd.env_remove("RUSTC_WORKSPACE_WRAPPER");
  1834. cmd.env_remove("RUSTC_WRAPPER");
  1835. cmd.env_remove(DX_RUSTC_WRAPPER_ENV_VAR);
  1836. cmd.envs(self.cargo_build_env_vars(ctx)?);
  1837. cmd.arg(format!("-Clinker={}", Workspace::path_to_dx()?.display()));
  1838. if self.platform == Platform::Web {
  1839. cmd.arg("-Crelocation-model=pic");
  1840. }
  1841. tracing::debug!("Direct rustc: {:#?}", cmd);
  1842. cmd.envs(rustc_args.envs.iter().cloned());
  1843. // tracing::trace!("Setting env vars: {:#?}", rustc_args.envs);
  1844. Ok(cmd)
  1845. }
  1846. // For Base and Fat builds, we use a regular cargo setup, but we might need to intercept
  1847. // rustc itself in case we're hot-patching and need a reliable rustc environment to
  1848. // continuously recompile the top-level crate with.
  1849. //
  1850. // In the future, when we support hot-patching *all* workspace crates, we will need to
  1851. // make use of the RUSTC_WORKSPACE_WRAPPER environment variable instead of RUSTC_WRAPPER
  1852. // and then keep track of env and args on a per-crate basis.
  1853. //
  1854. // We've also had a number of issues with incorrect canonicalization when passing paths
  1855. // through envs on windows, hence the frequent use of dunce::canonicalize.
  1856. _ => {
  1857. let mut cmd = Command::new("cargo");
  1858. cmd.arg("rustc")
  1859. .current_dir(self.crate_dir())
  1860. .arg("--message-format")
  1861. .arg("json-diagnostic-rendered-ansi")
  1862. .args(self.cargo_build_arguments(ctx))
  1863. .envs(self.cargo_build_env_vars(ctx)?);
  1864. if ctx.mode == BuildMode::Fat {
  1865. cmd.env(
  1866. DX_RUSTC_WRAPPER_ENV_VAR,
  1867. dunce::canonicalize(self.rustc_wrapper_args_file.path())
  1868. .unwrap()
  1869. .display()
  1870. .to_string(),
  1871. );
  1872. cmd.env(
  1873. "RUSTC_WRAPPER",
  1874. Workspace::path_to_dx()?.display().to_string(),
  1875. );
  1876. }
  1877. tracing::debug!("Cargo: {:#?}", cmd);
  1878. Ok(cmd)
  1879. }
  1880. }
  1881. }
  1882. /// Create a list of arguments for cargo builds
  1883. ///
  1884. /// We always use `cargo rustc` *or* `rustc` directly. This means we can pass extra flags like
  1885. /// `-C` arguments directly to the compiler.
  1886. #[allow(clippy::vec_init_then_push)]
  1887. fn cargo_build_arguments(&self, ctx: &BuildContext) -> Vec<String> {
  1888. let mut cargo_args = Vec::with_capacity(4);
  1889. // Add required profile flags. --release overrides any custom profiles.
  1890. cargo_args.push("--profile".to_string());
  1891. cargo_args.push(self.profile.to_string());
  1892. // Pass the appropriate target to cargo. We *always* specify a target which is somewhat helpful for preventing thrashing
  1893. cargo_args.push("--target".to_string());
  1894. cargo_args.push(self.triple.to_string());
  1895. // We always run in verbose since the CLI itself is the one doing the presentation
  1896. cargo_args.push("--verbose".to_string());
  1897. if self.no_default_features {
  1898. cargo_args.push("--no-default-features".to_string());
  1899. }
  1900. if !self.features.is_empty() {
  1901. cargo_args.push("--features".to_string());
  1902. cargo_args.push(self.features.join(" "));
  1903. }
  1904. // We *always* set the package since that's discovered from cargo metadata
  1905. cargo_args.push(String::from("-p"));
  1906. cargo_args.push(self.package.clone());
  1907. // Set the executable
  1908. match self.executable_type() {
  1909. TargetKind::Bin => cargo_args.push("--bin".to_string()),
  1910. TargetKind::Lib => cargo_args.push("--lib".to_string()),
  1911. TargetKind::Example => cargo_args.push("--example".to_string()),
  1912. _ => {}
  1913. };
  1914. cargo_args.push(self.executable_name().to_string());
  1915. // Merge in extra args. Order shouldn't really matter.
  1916. cargo_args.extend(self.extra_cargo_args.clone());
  1917. cargo_args.push("--".to_string());
  1918. cargo_args.extend(self.extra_rustc_args.clone());
  1919. // The bundle splitter needs relocation data to create a call-graph.
  1920. // This will automatically be erased by wasm-opt during the optimization step.
  1921. if self.platform == Platform::Web && self.wasm_split {
  1922. cargo_args.push("-Clink-args=--emit-relocs".to_string());
  1923. }
  1924. // dx *always* links android and thin builds
  1925. if self.custom_linker.is_some()
  1926. || matches!(ctx.mode, BuildMode::Thin { .. } | BuildMode::Fat)
  1927. {
  1928. cargo_args.push(format!(
  1929. "-Clinker={}",
  1930. Workspace::path_to_dx().expect("can't find dx").display()
  1931. ));
  1932. }
  1933. // for debuggability, we need to make sure android studio can properly understand our build
  1934. // https://stackoverflow.com/questions/68481401/debugging-a-prebuilt-shared-library-in-android-studio
  1935. if self.platform == Platform::Android {
  1936. cargo_args.push("-Clink-arg=-Wl,--build-id=sha1".to_string());
  1937. }
  1938. // Handle frameworks/dylibs by setting the rpath
  1939. // This is dependent on the bundle structure - in this case, appimage and appbundle for mac/linux
  1940. // todo: we need to figure out what to do for windows
  1941. match self.triple.operating_system {
  1942. OperatingSystem::Darwin(_) | OperatingSystem::IOS(_) => {
  1943. cargo_args.push("-Clink-arg=-Wl,-rpath,@executable_path/../Frameworks".to_string());
  1944. cargo_args.push("-Clink-arg=-Wl,-rpath,@executable_path".to_string());
  1945. }
  1946. OperatingSystem::Linux => {
  1947. cargo_args.push("-Clink-arg=-Wl,-rpath,$ORIGIN/../lib".to_string());
  1948. cargo_args.push("-Clink-arg=-Wl,-rpath,$ORIGIN".to_string());
  1949. }
  1950. _ => {}
  1951. }
  1952. // Our fancy hot-patching engine needs a lot of customization to work properly.
  1953. //
  1954. // These args are mostly intended to be passed when *fat* linking but are generally fine to
  1955. // pass for both fat and thin linking.
  1956. //
  1957. // We need save-temps and no-dead-strip in both cases though. When we run `cargo rustc` with
  1958. // these args, they will be captured and re-ran for the fast compiles in the future, so whatever
  1959. // we set here will be set for all future hot patches too.
  1960. if matches!(ctx.mode, BuildMode::Thin { .. } | BuildMode::Fat) {
  1961. // rustc gives us some portable flags required:
  1962. // - link-dead-code: prevents rust from passing -dead_strip to the linker since that's the default.
  1963. // - save-temps=true: keeps the incremental object files around, which we need for manually linking.
  1964. cargo_args.extend_from_slice(&[
  1965. "-Csave-temps=true".to_string(),
  1966. "-Clink-dead-code".to_string(),
  1967. ]);
  1968. // We need to set some extra args that ensure all symbols make it into the final output
  1969. // and that the linker doesn't strip them out.
  1970. //
  1971. // This basically amounts of -all_load or --whole-archive, depending on the linker.
  1972. // We just assume an ld-like interface on macos and a gnu-ld interface elsewhere.
  1973. //
  1974. // macOS/iOS use ld64 but through the `cc` interface.
  1975. // cargo_args.push("-Clink-args=-Wl,-all_load".to_string());
  1976. //
  1977. // Linux and Android fit under this umbrella, both with the same clang-like entrypoint
  1978. // and the gnu-ld interface.
  1979. //
  1980. // cargo_args.push("-Clink-args=-Wl,--whole-archive".to_string());
  1981. //
  1982. // If windows -Wl,--whole-archive is required since it follows gnu-ld convention.
  1983. // There might be other flags on windows - we haven't tested windows thoroughly.
  1984. //
  1985. // cargo_args.push("-Clink-args=-Wl,--whole-archive".to_string());
  1986. // https://learn.microsoft.com/en-us/cpp/build/reference/wholearchive-include-all-library-object-files?view=msvc-170
  1987. //
  1988. // ------------------------------------------------------------
  1989. //
  1990. // if web, -Wl,--whole-archive is required since it follows gnu-ld convention.
  1991. //
  1992. // We also use --no-gc-sections and --export-table and --export-memory to push
  1993. // said symbols into the export table.
  1994. //
  1995. // We use --emit-relocs to build up a solid call graph.
  1996. //
  1997. // rust uses its own wasm-ld linker which can be found here (it's just gcc-ld with a `-target wasm` flag):
  1998. // - ~/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin/gcc-ld
  1999. // - ~/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin/gcc-ld/wasm-ld
  2000. //
  2001. // Note that we can't use --export-all, unfortunately, since some symbols are internal
  2002. // to wasm-bindgen and exporting them causes the JS generation to fail.
  2003. //
  2004. // We are basically replicating what emscripten does here with its dynamic linking
  2005. // approach where the MAIN_MODULE is very "fat" and exports the necessary arguments
  2006. // for the side modules to be linked in. This guide is really helpful:
  2007. //
  2008. // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
  2009. //
  2010. // The tricky one is -Ctarget-cpu=mvp, which prevents rustc from generating externref
  2011. // entries.
  2012. //
  2013. // https://blog.rust-lang.org/2024/09/24/webassembly-targets-change-in-default-target-features/#disabling-on-by-default-webassembly-proposals
  2014. //
  2015. // It's fine that these exist in the base module but not in the patch.
  2016. if self.platform == Platform::Web
  2017. || self.triple.operating_system == OperatingSystem::Wasi
  2018. {
  2019. cargo_args.push("-Ctarget-cpu=mvp".into());
  2020. cargo_args.push("-Clink-arg=--no-gc-sections".into());
  2021. cargo_args.push("-Clink-arg=--growable-table".into());
  2022. cargo_args.push("-Clink-arg=--export-table".into());
  2023. cargo_args.push("-Clink-arg=--export-memory".into());
  2024. cargo_args.push("-Clink-arg=--emit-relocs".into());
  2025. cargo_args.push("-Clink-arg=--export=__stack_pointer".into());
  2026. cargo_args.push("-Clink-arg=--export=__heap_base".into());
  2027. cargo_args.push("-Clink-arg=--export=__data_end".into());
  2028. }
  2029. }
  2030. cargo_args
  2031. }
  2032. fn cargo_build_env_vars(&self, ctx: &BuildContext) -> Result<Vec<(&'static str, String)>> {
  2033. let mut env_vars = vec![];
  2034. // Make sure to set all the crazy android flags. Cross-compiling is hard, man.
  2035. if self.platform == Platform::Android {
  2036. env_vars.extend(self.android_env_vars()?);
  2037. };
  2038. // If we're either zero-linking or using a custom linker, make `dx` itself do the linking.
  2039. if self.custom_linker.is_some()
  2040. || matches!(ctx.mode, BuildMode::Thin { .. } | BuildMode::Fat)
  2041. {
  2042. LinkAction {
  2043. triple: self.triple.clone(),
  2044. linker: self.custom_linker.clone(),
  2045. link_err_file: dunce::canonicalize(self.link_err_file.path())?,
  2046. link_args_file: dunce::canonicalize(self.link_args_file.path())?,
  2047. }
  2048. .write_env_vars(&mut env_vars)?;
  2049. }
  2050. // Disable reference types on wasm when using hotpatching
  2051. // https://blog.rust-lang.org/2024/09/24/webassembly-targets-change-in-default-target-features/#disabling-on-by-default-webassembly-proposals
  2052. if self.platform == Platform::Web
  2053. && matches!(ctx.mode, BuildMode::Thin { .. } | BuildMode::Fat)
  2054. {
  2055. env_vars.push(("RUSTFLAGS", {
  2056. let mut rust_flags = std::env::var("RUSTFLAGS").unwrap_or_default();
  2057. rust_flags.push_str(" -Ctarget-cpu=mvp");
  2058. rust_flags
  2059. }));
  2060. }
  2061. // If this is a release build, bake the base path and title into the binary with env vars.
  2062. // todo: should we even be doing this? might be better being a build.rs or something else.
  2063. if self.release {
  2064. if let Some(base_path) = self.base_path() {
  2065. env_vars.push((ASSET_ROOT_ENV, base_path.to_string()));
  2066. }
  2067. env_vars.push((APP_TITLE_ENV, self.config.web.app.title.clone()));
  2068. }
  2069. Ok(env_vars)
  2070. }
  2071. fn android_env_vars(&self) -> Result<Vec<(&'static str, String)>> {
  2072. let mut env_vars = vec![];
  2073. let tools = self.workspace.android_tools()?;
  2074. let linker = tools.android_cc(&self.triple);
  2075. let min_sdk_version = tools.min_sdk_version();
  2076. let ar_path = tools.ar_path();
  2077. let target_cc = tools.target_cc();
  2078. let target_cxx = tools.target_cxx();
  2079. let java_home = tools.java_home();
  2080. let ndk = tools.ndk.clone();
  2081. tracing::debug!(
  2082. r#"Using android:
  2083. min_sdk_version: {min_sdk_version}
  2084. linker: {linker:?}
  2085. ar_path: {ar_path:?}
  2086. target_cc: {target_cc:?}
  2087. target_cxx: {target_cxx:?}
  2088. java_home: {java_home:?}
  2089. "#
  2090. );
  2091. env_vars.push(("ANDROID_NATIVE_API_LEVEL", min_sdk_version.to_string()));
  2092. env_vars.push(("TARGET_AR", ar_path.display().to_string()));
  2093. env_vars.push(("TARGET_CC", target_cc.display().to_string()));
  2094. env_vars.push(("TARGET_CXX", target_cxx.display().to_string()));
  2095. env_vars.push(("ANDROID_NDK_ROOT", ndk.display().to_string()));
  2096. if let Some(java_home) = java_home {
  2097. tracing::debug!("Setting JAVA_HOME to {java_home:?}");
  2098. env_vars.push(("JAVA_HOME", java_home.display().to_string()));
  2099. }
  2100. // Set the wry env vars - this is where wry will dump its kotlin files.
  2101. // Their setup is really annyoing and requires us to hardcode `dx` to specific versions of tao/wry.
  2102. env_vars.push(("WRY_ANDROID_PACKAGE", "dev.dioxus.main".to_string()));
  2103. env_vars.push(("WRY_ANDROID_LIBRARY", "dioxusmain".to_string()));
  2104. env_vars.push((
  2105. "WRY_ANDROID_KOTLIN_FILES_OUT_DIR",
  2106. self.wry_android_kotlin_files_out_dir()
  2107. .display()
  2108. .to_string(),
  2109. ));
  2110. // Set the rust flags for android which get passed to *every* crate in the graph.
  2111. env_vars.push(("RUSTFLAGS", {
  2112. let mut rust_flags = std::env::var("RUSTFLAGS").unwrap_or_default();
  2113. rust_flags.push_str(" -Clink-arg=-landroid");
  2114. rust_flags.push_str(" -Clink-arg=-llog");
  2115. rust_flags.push_str(" -Clink-arg=-lOpenSLES");
  2116. rust_flags.push_str(" -Clink-arg=-Wl,--export-dynamic");
  2117. rust_flags
  2118. }));
  2119. // todo(jon): the guide for openssl recommends extending the path to include the tools dir
  2120. // in practice I couldn't get this to work, but this might eventually become useful.
  2121. //
  2122. // https://github.com/openssl/openssl/blob/master/NOTES-ANDROID.md#configuration
  2123. //
  2124. // They recommend a configuration like this:
  2125. //
  2126. // // export ANDROID_NDK_ROOT=/home/whoever/Android/android-sdk/ndk/20.0.5594570
  2127. // PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
  2128. // ./Configure android-arm64 -D__ANDROID_API__=29
  2129. // make
  2130. //
  2131. // let tools_dir = arch.android_tools_dir(&ndk);
  2132. // let extended_path = format!(
  2133. // "{}:{}",
  2134. // tools_dir.display(),
  2135. // std::env::var("PATH").unwrap_or_default()
  2136. // );
  2137. // env_vars.push(("PATH", extended_path));
  2138. Ok(env_vars)
  2139. }
  2140. /// Get an estimate of the number of units in the crate. If nightly rustc is not available, this
  2141. /// will return an estimate of the number of units in the crate based on cargo metadata.
  2142. ///
  2143. /// TODO: always use <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#unit-graph> once it is stable
  2144. async fn get_unit_count_estimate(&self, ctx: &BuildContext) -> usize {
  2145. // Try to get it from nightly
  2146. if let Ok(count) = self.get_unit_count(ctx).await {
  2147. return count;
  2148. }
  2149. // Otherwise, use cargo metadata
  2150. let units = self
  2151. .workspace
  2152. .krates
  2153. .krates_filtered(krates::DepKind::Dev)
  2154. .iter()
  2155. .map(|k| k.targets.len())
  2156. .sum::<usize>();
  2157. (units as f64 / 3.5) as usize
  2158. }
  2159. /// Try to get the unit graph for the crate. This is a nightly only feature which may not be
  2160. /// available with the current version of rustc the user has installed.
  2161. ///
  2162. /// It also might not be super reliable - I think in practice it occasionally returns 2x the units.
  2163. async fn get_unit_count(&self, ctx: &BuildContext) -> crate::Result<usize> {
  2164. #[derive(Debug, Deserialize)]
  2165. struct UnitGraph {
  2166. units: Vec<serde_json::Value>,
  2167. }
  2168. let output = tokio::process::Command::new("cargo")
  2169. .arg("+nightly")
  2170. .arg("build")
  2171. .arg("--unit-graph")
  2172. .arg("-Z")
  2173. .arg("unstable-options")
  2174. .args(self.cargo_build_arguments(ctx))
  2175. .envs(self.cargo_build_env_vars(ctx)?)
  2176. .output()
  2177. .await?;
  2178. if !output.status.success() {
  2179. return Err(anyhow::anyhow!("Failed to get unit count").into());
  2180. }
  2181. let output_text = String::from_utf8(output.stdout).context("Failed to get unit count")?;
  2182. let graph: UnitGraph =
  2183. serde_json::from_str(&output_text).context("Failed to get unit count")?;
  2184. Ok(graph.units.len())
  2185. }
  2186. pub(crate) fn all_target_features(&self) -> Vec<String> {
  2187. let mut features = self.features.clone();
  2188. if !self.no_default_features {
  2189. features.extend(
  2190. self.package()
  2191. .features
  2192. .get("default")
  2193. .cloned()
  2194. .unwrap_or_default(),
  2195. );
  2196. }
  2197. features.dedup();
  2198. features
  2199. }
  2200. /// returns the path to root build folder. This will be our working directory for the build.
  2201. ///
  2202. /// we only add an extension to the folders where it sorta matters that it's named with the extension.
  2203. /// for example, on mac, the `.app` indicates we can `open` it and it pulls in icons, dylibs, etc.
  2204. ///
  2205. /// for our simulator-based platforms, this is less important since they need to be zipped up anyways
  2206. /// to run in the simulator.
  2207. ///
  2208. /// For windows/linux, it's also not important since we're just running the exe directly out of the folder
  2209. ///
  2210. /// The idea of this folder is that we can run our top-level build command against it and we'll get
  2211. /// a final build output somewhere. Some platforms have basically no build command, and can simply
  2212. /// be ran by executing the exe directly.
  2213. pub(crate) fn root_dir(&self) -> PathBuf {
  2214. let platform_dir = self.platform_dir();
  2215. match self.platform {
  2216. Platform::Web => platform_dir.join("public"),
  2217. Platform::Server => platform_dir.clone(), // ends up *next* to the public folder
  2218. // These might not actually need to be called `.app` but it does let us run these with `open`
  2219. Platform::MacOS => platform_dir.join(format!("{}.app", self.bundled_app_name())),
  2220. Platform::Ios => platform_dir.join(format!("{}.app", self.bundled_app_name())),
  2221. // in theory, these all could end up directly in the root dir
  2222. Platform::Android => platform_dir.join("app"), // .apk (after bundling)
  2223. Platform::Linux => platform_dir.join("app"), // .appimage (after bundling)
  2224. Platform::Windows => platform_dir.join("app"), // .exe (after bundling)
  2225. Platform::Liveview => platform_dir.join("app"), // .exe (after bundling)
  2226. }
  2227. }
  2228. fn platform_dir(&self) -> PathBuf {
  2229. self.build_dir(self.platform, self.release)
  2230. }
  2231. fn platform_exe_name(&self) -> String {
  2232. match self.platform {
  2233. Platform::MacOS => self.executable_name().to_string(),
  2234. Platform::Ios => self.executable_name().to_string(),
  2235. Platform::Server => self.executable_name().to_string(),
  2236. Platform::Liveview => self.executable_name().to_string(),
  2237. Platform::Windows => format!("{}.exe", self.executable_name()),
  2238. // from the apk spec, the root exe is a shared library
  2239. // we include the user's rust code as a shared library with a fixed namespace
  2240. Platform::Android => "libdioxusmain.so".to_string(),
  2241. // this will be wrong, I think, but not important?
  2242. Platform::Web => format!("{}_bg.wasm", self.executable_name()),
  2243. // todo: maybe this should be called AppRun?
  2244. Platform::Linux => self.executable_name().to_string(),
  2245. }
  2246. }
  2247. /// Assemble the android app dir.
  2248. ///
  2249. /// This is a bit of a mess since we need to create a lot of directories and files. Other approaches
  2250. /// would be to unpack some zip folder or something stored via `include_dir!()`. However, we do
  2251. /// need to customize the whole setup a bit, so it's just simpler (though messier) to do it this way.
  2252. fn build_android_app_dir(&self) -> Result<()> {
  2253. use std::fs::{create_dir_all, write};
  2254. let root = self.root_dir();
  2255. // gradle
  2256. let wrapper = root.join("gradle").join("wrapper");
  2257. create_dir_all(&wrapper)?;
  2258. // app
  2259. let app = root.join("app");
  2260. let app_main = app.join("src").join("main");
  2261. let app_kotlin = app_main.join("kotlin");
  2262. let app_jnilibs = app_main.join("jniLibs");
  2263. let app_assets = app_main.join("assets");
  2264. let app_kotlin_out = self.wry_android_kotlin_files_out_dir();
  2265. create_dir_all(&app)?;
  2266. create_dir_all(&app_main)?;
  2267. create_dir_all(&app_kotlin)?;
  2268. create_dir_all(&app_jnilibs)?;
  2269. create_dir_all(&app_assets)?;
  2270. create_dir_all(&app_kotlin_out)?;
  2271. tracing::debug!(
  2272. r#"Initialized android dirs:
  2273. - gradle: {wrapper:?}
  2274. - app/ {app:?}
  2275. - app/src: {app_main:?}
  2276. - app/src/kotlin: {app_kotlin:?}
  2277. - app/src/jniLibs: {app_jnilibs:?}
  2278. - app/src/assets: {app_assets:?}
  2279. - app/src/kotlin/main: {app_kotlin_out:?}
  2280. "#
  2281. );
  2282. // handlebars
  2283. #[derive(Serialize)]
  2284. struct AndroidHandlebarsObjects {
  2285. application_id: String,
  2286. app_name: String,
  2287. android_bundle: Option<crate::AndroidSettings>,
  2288. }
  2289. let hbs_data = AndroidHandlebarsObjects {
  2290. application_id: self.full_mobile_app_name(),
  2291. app_name: self.bundled_app_name(),
  2292. android_bundle: self.config.bundle.android.clone(),
  2293. };
  2294. let hbs = handlebars::Handlebars::new();
  2295. // Top-level gradle config
  2296. write(
  2297. root.join("build.gradle.kts"),
  2298. include_bytes!("../../assets/android/gen/build.gradle.kts"),
  2299. )?;
  2300. write(
  2301. root.join("gradle.properties"),
  2302. include_bytes!("../../assets/android/gen/gradle.properties"),
  2303. )?;
  2304. write(
  2305. root.join("gradlew"),
  2306. include_bytes!("../../assets/android/gen/gradlew"),
  2307. )?;
  2308. write(
  2309. root.join("gradlew.bat"),
  2310. include_bytes!("../../assets/android/gen/gradlew.bat"),
  2311. )?;
  2312. write(
  2313. root.join("settings.gradle"),
  2314. include_bytes!("../../assets/android/gen/settings.gradle"),
  2315. )?;
  2316. // Then the wrapper and its properties
  2317. write(
  2318. wrapper.join("gradle-wrapper.properties"),
  2319. include_bytes!("../../assets/android/gen/gradle/wrapper/gradle-wrapper.properties"),
  2320. )?;
  2321. write(
  2322. wrapper.join("gradle-wrapper.jar"),
  2323. include_bytes!("../../assets/android/gen/gradle/wrapper/gradle-wrapper.jar"),
  2324. )?;
  2325. // Now the app directory
  2326. write(
  2327. app.join("build.gradle.kts"),
  2328. hbs.render_template(
  2329. include_str!("../../assets/android/gen/app/build.gradle.kts.hbs"),
  2330. &hbs_data,
  2331. )?,
  2332. )?;
  2333. write(
  2334. app.join("proguard-rules.pro"),
  2335. include_bytes!("../../assets/android/gen/app/proguard-rules.pro"),
  2336. )?;
  2337. let manifest_xml = match self.config.application.android_manifest.as_deref() {
  2338. Some(manifest) => std::fs::read_to_string(self.package_manifest_dir().join(manifest))
  2339. .context("Failed to locate custom AndroidManifest.xml")?,
  2340. _ => hbs.render_template(
  2341. include_str!("../../assets/android/gen/app/src/main/AndroidManifest.xml.hbs"),
  2342. &hbs_data,
  2343. )?,
  2344. };
  2345. write(
  2346. app.join("src").join("main").join("AndroidManifest.xml"),
  2347. manifest_xml,
  2348. )?;
  2349. // Write the main activity manually since tao dropped support for it
  2350. write(
  2351. self.wry_android_kotlin_files_out_dir()
  2352. .join("MainActivity.kt"),
  2353. hbs.render_template(
  2354. include_str!("../../assets/android/MainActivity.kt.hbs"),
  2355. &hbs_data,
  2356. )?,
  2357. )?;
  2358. // Write the res folder, containing stuff like default icons, colors, and menubars.
  2359. let res = app_main.join("res");
  2360. create_dir_all(&res)?;
  2361. create_dir_all(res.join("values"))?;
  2362. write(
  2363. res.join("values").join("strings.xml"),
  2364. hbs.render_template(
  2365. include_str!("../../assets/android/gen/app/src/main/res/values/strings.xml.hbs"),
  2366. &hbs_data,
  2367. )?,
  2368. )?;
  2369. write(
  2370. res.join("values").join("colors.xml"),
  2371. include_bytes!("../../assets/android/gen/app/src/main/res/values/colors.xml"),
  2372. )?;
  2373. write(
  2374. res.join("values").join("styles.xml"),
  2375. include_bytes!("../../assets/android/gen/app/src/main/res/values/styles.xml"),
  2376. )?;
  2377. create_dir_all(res.join("drawable"))?;
  2378. write(
  2379. res.join("drawable").join("ic_launcher_background.xml"),
  2380. include_bytes!(
  2381. "../../assets/android/gen/app/src/main/res/drawable/ic_launcher_background.xml"
  2382. ),
  2383. )?;
  2384. create_dir_all(res.join("drawable-v24"))?;
  2385. write(
  2386. res.join("drawable-v24").join("ic_launcher_foreground.xml"),
  2387. include_bytes!(
  2388. "../../assets/android/gen/app/src/main/res/drawable-v24/ic_launcher_foreground.xml"
  2389. ),
  2390. )?;
  2391. create_dir_all(res.join("mipmap-anydpi-v26"))?;
  2392. write(
  2393. res.join("mipmap-anydpi-v26").join("ic_launcher.xml"),
  2394. include_bytes!(
  2395. "../../assets/android/gen/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml"
  2396. ),
  2397. )?;
  2398. create_dir_all(res.join("mipmap-hdpi"))?;
  2399. write(
  2400. res.join("mipmap-hdpi").join("ic_launcher.webp"),
  2401. include_bytes!(
  2402. "../../assets/android/gen/app/src/main/res/mipmap-hdpi/ic_launcher.webp"
  2403. ),
  2404. )?;
  2405. create_dir_all(res.join("mipmap-mdpi"))?;
  2406. write(
  2407. res.join("mipmap-mdpi").join("ic_launcher.webp"),
  2408. include_bytes!(
  2409. "../../assets/android/gen/app/src/main/res/mipmap-mdpi/ic_launcher.webp"
  2410. ),
  2411. )?;
  2412. create_dir_all(res.join("mipmap-xhdpi"))?;
  2413. write(
  2414. res.join("mipmap-xhdpi").join("ic_launcher.webp"),
  2415. include_bytes!(
  2416. "../../assets/android/gen/app/src/main/res/mipmap-xhdpi/ic_launcher.webp"
  2417. ),
  2418. )?;
  2419. create_dir_all(res.join("mipmap-xxhdpi"))?;
  2420. write(
  2421. res.join("mipmap-xxhdpi").join("ic_launcher.webp"),
  2422. include_bytes!(
  2423. "../../assets/android/gen/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp"
  2424. ),
  2425. )?;
  2426. create_dir_all(res.join("mipmap-xxxhdpi"))?;
  2427. write(
  2428. res.join("mipmap-xxxhdpi").join("ic_launcher.webp"),
  2429. include_bytes!(
  2430. "../../assets/android/gen/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp"
  2431. ),
  2432. )?;
  2433. Ok(())
  2434. }
  2435. fn wry_android_kotlin_files_out_dir(&self) -> PathBuf {
  2436. let mut kotlin_dir = self
  2437. .root_dir()
  2438. .join("app")
  2439. .join("src")
  2440. .join("main")
  2441. .join("kotlin");
  2442. for segment in "dev.dioxus.main".split('.') {
  2443. kotlin_dir = kotlin_dir.join(segment);
  2444. }
  2445. kotlin_dir
  2446. }
  2447. /// Get the directory where this app can write to for this session that's guaranteed to be stable
  2448. /// for the same app. This is useful for emitting state like window position and size.
  2449. ///
  2450. /// The directory is specific for this app and might be
  2451. pub(crate) fn session_cache_dir(&self) -> PathBuf {
  2452. self.session_cache_dir.path().to_path_buf()
  2453. }
  2454. /// Get the outdir specified by the Dioxus.toml, relative to the crate directory.
  2455. /// We don't support workspaces yet since that would cause a collision of bundles per project.
  2456. pub(crate) fn crate_out_dir(&self) -> Option<PathBuf> {
  2457. self.config
  2458. .application
  2459. .out_dir
  2460. .as_ref()
  2461. .map(|out_dir| self.crate_dir().join(out_dir))
  2462. }
  2463. /// Compose an out directory. Represents the typical "dist" directory that
  2464. /// is "distributed" after building an application (configurable in the
  2465. /// `Dioxus.toml`).
  2466. fn internal_out_dir(&self) -> PathBuf {
  2467. let dir = self.target_dir.join("dx");
  2468. std::fs::create_dir_all(&dir).unwrap();
  2469. dir
  2470. }
  2471. /// Create a workdir for the given platform
  2472. /// This can be used as a temporary directory for the build, but in an observable way such that
  2473. /// you can see the files in the directory via `target`
  2474. ///
  2475. /// target/dx/build/app/web/
  2476. /// target/dx/build/app/web/public/
  2477. /// target/dx/build/app/web/server.exe
  2478. pub(crate) fn build_dir(&self, platform: Platform, release: bool) -> PathBuf {
  2479. self.internal_out_dir()
  2480. .join(self.executable_name())
  2481. .join(if release { "release" } else { "debug" })
  2482. .join(platform.build_folder_name())
  2483. }
  2484. /// target/dx/bundle/app/
  2485. /// target/dx/bundle/app/blah.app
  2486. /// target/dx/bundle/app/blah.exe
  2487. /// target/dx/bundle/app/public/
  2488. pub(crate) fn bundle_dir(&self, platform: Platform) -> PathBuf {
  2489. self.internal_out_dir()
  2490. .join(self.executable_name())
  2491. .join("bundle")
  2492. .join(platform.build_folder_name())
  2493. }
  2494. /// Get the workspace directory for the crate
  2495. pub(crate) fn workspace_dir(&self) -> PathBuf {
  2496. self.workspace
  2497. .krates
  2498. .workspace_root()
  2499. .as_std_path()
  2500. .to_path_buf()
  2501. }
  2502. /// Get the directory of the crate
  2503. pub(crate) fn crate_dir(&self) -> PathBuf {
  2504. self.package()
  2505. .manifest_path
  2506. .parent()
  2507. .unwrap()
  2508. .as_std_path()
  2509. .to_path_buf()
  2510. }
  2511. /// Get the package we are currently in
  2512. pub(crate) fn package(&self) -> &krates::cm::Package {
  2513. &self.workspace.krates[self.crate_package]
  2514. }
  2515. /// Get the name of the package we are compiling
  2516. pub(crate) fn executable_name(&self) -> &str {
  2517. &self.crate_target.name
  2518. }
  2519. /// Get the type of executable we are compiling
  2520. pub(crate) fn executable_type(&self) -> TargetKind {
  2521. self.crate_target.kind[0]
  2522. }
  2523. /// Get the features required to build for the given platform
  2524. fn feature_for_platform(package: &krates::cm::Package, platform: Platform) -> String {
  2525. // Try to find the feature that activates the dioxus feature for the given platform
  2526. let dioxus_feature = platform.feature_name();
  2527. let res = package.features.iter().find_map(|(key, features)| {
  2528. // if the feature is just the name of the platform, we use that
  2529. if key == dioxus_feature {
  2530. return Some(key.clone());
  2531. }
  2532. // Otherwise look for the feature that starts with dioxus/ or dioxus?/ and matches the platform
  2533. for feature in features {
  2534. if let Some((_, after_dioxus)) = feature.split_once("dioxus") {
  2535. if let Some(dioxus_feature_enabled) =
  2536. after_dioxus.trim_start_matches('?').strip_prefix('/')
  2537. {
  2538. // If that enables the feature we are looking for, return that feature
  2539. if dioxus_feature_enabled == dioxus_feature {
  2540. return Some(key.clone());
  2541. }
  2542. }
  2543. }
  2544. }
  2545. None
  2546. });
  2547. res.unwrap_or_else(|| {
  2548. let fallback = format!("dioxus/{}", platform.feature_name()) ;
  2549. tracing::debug!(
  2550. "Could not find explicit feature for platform {platform}, passing `fallback` instead"
  2551. );
  2552. fallback
  2553. })
  2554. }
  2555. // The `opt-level=1` increases build times, but can noticeably decrease time
  2556. // between saving changes and being able to interact with an app (for wasm/web). The "overall"
  2557. // time difference (between having and not having the optimization) can be
  2558. // almost imperceptible (~1 s) but also can be very noticeable (~6 s) — depends
  2559. // on setup (hardware, OS, browser, idle load).
  2560. //
  2561. // Find or create the client and server profiles in the top-level Cargo.toml file
  2562. // todo(jon): we should/could make these optional by placing some defaults somewhere
  2563. pub(crate) fn initialize_profiles(&self) -> crate::Result<()> {
  2564. let config_path = self.workspace_dir().join("Cargo.toml");
  2565. let mut config = match std::fs::read_to_string(&config_path) {
  2566. Ok(config) => config.parse::<toml_edit::DocumentMut>().map_err(|e| {
  2567. crate::Error::Other(anyhow::anyhow!("Failed to parse Cargo.toml: {}", e))
  2568. })?,
  2569. Err(_) => Default::default(),
  2570. };
  2571. if let Item::Table(table) = config
  2572. .as_table_mut()
  2573. .entry("profile")
  2574. .or_insert(Item::Table(Default::default()))
  2575. {
  2576. if let toml_edit::Entry::Vacant(entry) = table.entry(PROFILE_WASM) {
  2577. let mut client = toml_edit::Table::new();
  2578. client.insert("inherits", Item::Value("dev".into()));
  2579. client.insert("opt-level", Item::Value(1.into()));
  2580. entry.insert(Item::Table(client));
  2581. }
  2582. if let toml_edit::Entry::Vacant(entry) = table.entry(PROFILE_SERVER) {
  2583. let mut server = toml_edit::Table::new();
  2584. server.insert("inherits", Item::Value("dev".into()));
  2585. entry.insert(Item::Table(server));
  2586. }
  2587. if let toml_edit::Entry::Vacant(entry) = table.entry(PROFILE_ANDROID) {
  2588. let mut android = toml_edit::Table::new();
  2589. android.insert("inherits", Item::Value("dev".into()));
  2590. entry.insert(Item::Table(android));
  2591. }
  2592. }
  2593. std::fs::write(config_path, config.to_string())
  2594. .context("Failed to write profiles to Cargo.toml")?;
  2595. Ok(())
  2596. }
  2597. /// Return the version of the wasm-bindgen crate if it exists
  2598. fn wasm_bindgen_version(&self) -> Option<String> {
  2599. self.workspace
  2600. .krates
  2601. .krates_by_name("wasm-bindgen")
  2602. .next()
  2603. .map(|krate| krate.krate.version.to_string())
  2604. }
  2605. /// Return the platforms that are enabled for the package
  2606. ///
  2607. /// Ideally only one platform is enabled but we need to be able to
  2608. pub(crate) fn enabled_cargo_toml_platforms(
  2609. package: &krates::cm::Package,
  2610. no_default_features: bool,
  2611. ) -> Vec<Platform> {
  2612. let mut platforms = vec![];
  2613. // Attempt to discover the platform directly from the dioxus dependency
  2614. //
  2615. // [dependencies]
  2616. // dioxus = { features = ["web"] }
  2617. //
  2618. if let Some(dxs) = package.dependencies.iter().find(|dep| dep.name == "dioxus") {
  2619. for f in dxs.features.iter() {
  2620. if let Some(platform) = Platform::autodetect_from_cargo_feature(f) {
  2621. platforms.push(platform);
  2622. }
  2623. }
  2624. }
  2625. // Start searching through the default features
  2626. //
  2627. // [features]
  2628. // default = ["dioxus/web"]
  2629. //
  2630. // or
  2631. //
  2632. // [features]
  2633. // default = ["web"]
  2634. // web = ["dioxus/web"]
  2635. if no_default_features {
  2636. return platforms;
  2637. }
  2638. let Some(default) = package.features.get("default") else {
  2639. return platforms;
  2640. };
  2641. // we only trace features 1 level deep..
  2642. for feature in default.iter() {
  2643. // If the user directly specified a platform we can just use that.
  2644. if feature.starts_with("dioxus/") {
  2645. let dx_feature = feature.trim_start_matches("dioxus/");
  2646. let auto = Platform::autodetect_from_cargo_feature(dx_feature);
  2647. if let Some(auto) = auto {
  2648. platforms.push(auto);
  2649. }
  2650. }
  2651. // If the user is specifying an internal feature that points to a platform, we can use that
  2652. let internal_feature = package.features.get(feature);
  2653. if let Some(internal_feature) = internal_feature {
  2654. for feature in internal_feature {
  2655. if feature.starts_with("dioxus/") {
  2656. let dx_feature = feature.trim_start_matches("dioxus/");
  2657. let auto = Platform::autodetect_from_cargo_feature(dx_feature);
  2658. if let Some(auto) = auto {
  2659. platforms.push(auto);
  2660. }
  2661. }
  2662. }
  2663. }
  2664. }
  2665. platforms.sort();
  2666. platforms.dedup();
  2667. platforms
  2668. }
  2669. /// Gather the features that are enabled for the package
  2670. fn platformless_features(package: &krates::cm::Package) -> Vec<String> {
  2671. let Some(default) = package.features.get("default") else {
  2672. return Vec::new();
  2673. };
  2674. let mut kept_features = vec![];
  2675. // Only keep the top-level features in the default list that don't point to a platform directly
  2676. // IE we want to drop `web` if default = ["web"]
  2677. 'top: for feature in default {
  2678. // Don't keep features that point to a platform via dioxus/blah
  2679. if feature.starts_with("dioxus/") {
  2680. let dx_feature = feature.trim_start_matches("dioxus/");
  2681. if Platform::autodetect_from_cargo_feature(dx_feature).is_some() {
  2682. continue 'top;
  2683. }
  2684. }
  2685. // Don't keep features that point to a platform via an internal feature
  2686. if let Some(internal_feature) = package.features.get(feature) {
  2687. for feature in internal_feature {
  2688. if feature.starts_with("dioxus/") {
  2689. let dx_feature = feature.trim_start_matches("dioxus/");
  2690. if Platform::autodetect_from_cargo_feature(dx_feature).is_some() {
  2691. continue 'top;
  2692. }
  2693. }
  2694. }
  2695. }
  2696. // Otherwise we can keep it
  2697. kept_features.push(feature.to_string());
  2698. }
  2699. kept_features
  2700. }
  2701. pub(crate) fn mobile_org(&self) -> String {
  2702. let identifier = self.bundle_identifier();
  2703. let mut split = identifier.splitn(3, '.');
  2704. let sub = split
  2705. .next()
  2706. .expect("Identifier to have at least 3 periods like `com.example.app`");
  2707. let tld = split
  2708. .next()
  2709. .expect("Identifier to have at least 3 periods like `com.example.app`");
  2710. format!("{}.{}", sub, tld)
  2711. }
  2712. pub(crate) fn bundled_app_name(&self) -> String {
  2713. use convert_case::{Case, Casing};
  2714. self.executable_name().to_case(Case::Pascal)
  2715. }
  2716. pub(crate) fn full_mobile_app_name(&self) -> String {
  2717. format!("{}.{}", self.mobile_org(), self.bundled_app_name())
  2718. }
  2719. pub(crate) fn bundle_identifier(&self) -> String {
  2720. if let Some(identifier) = self.config.bundle.identifier.clone() {
  2721. return identifier.clone();
  2722. }
  2723. format!("com.example.{}", self.bundled_app_name())
  2724. }
  2725. /// The item that we'll try to run directly if we need to.
  2726. ///
  2727. /// todo(jon): we should name the app properly instead of making up the exe name. It's kinda okay for dev mode, but def not okay for prod
  2728. pub(crate) fn main_exe(&self) -> PathBuf {
  2729. self.exe_dir().join(self.platform_exe_name())
  2730. }
  2731. /// Does the app specify:
  2732. ///
  2733. /// - Dioxus with "fullstack" enabled? (access to serverfns, etc)
  2734. /// - An explicit "fullstack" feature that enables said feature?
  2735. ///
  2736. /// Note that we don't detect if dependencies enable it transitively since we want to be explicit about it.
  2737. ///
  2738. /// The intention here is to detect if "fullstack" is enabled in the target's features list:
  2739. /// ```toml
  2740. /// [dependencies]
  2741. /// dioxus = { version = "0.4", features = ["fullstack"] }
  2742. /// ```
  2743. ///
  2744. /// or as an explicit feature in default:
  2745. /// ```toml
  2746. /// [features]
  2747. /// default = ["dioxus/fullstack"]
  2748. /// ```
  2749. ///
  2750. /// or as a default feature that enables the dioxus feature:
  2751. /// ```toml
  2752. /// [features]
  2753. /// default = ["fullstack"]
  2754. /// fullstack = ["dioxus/fullstack"]
  2755. /// ```
  2756. ///
  2757. /// or as an explicit feature (that enables the dioxus feature):
  2758. /// ```
  2759. /// dx serve app --features "fullstack"
  2760. /// ```
  2761. pub(crate) fn fullstack_feature_enabled(&self) -> bool {
  2762. let dioxus_dep = self
  2763. .package()
  2764. .dependencies
  2765. .iter()
  2766. .find(|dep| dep.name == "dioxus");
  2767. // If we don't have a dioxus dependency, we can't be fullstack. This shouldn't impact non-dioxus projects
  2768. let Some(dioxus_dep) = dioxus_dep else {
  2769. return false;
  2770. };
  2771. // Check if the dioxus dependency has the "fullstack" feature enabled
  2772. if dioxus_dep.features.iter().any(|f| f == "fullstack") {
  2773. return true;
  2774. }
  2775. // Check if any of the features in our feature list enables a feature that enables "fullstack"
  2776. let transitive = self
  2777. .package()
  2778. .features
  2779. .iter()
  2780. .filter(|(_name, list)| list.iter().any(|f| f == "dioxus/fullstack"));
  2781. for (name, _list) in transitive {
  2782. if self.features.contains(name) {
  2783. return true;
  2784. }
  2785. }
  2786. false
  2787. }
  2788. /// todo(jon): use handlebars templates instead of these prebaked templates
  2789. async fn write_metadata(&self) -> Result<()> {
  2790. // write the Info.plist file
  2791. match self.platform {
  2792. Platform::MacOS => {
  2793. let dest = self.root_dir().join("Contents").join("Info.plist");
  2794. let plist = self.info_plist_contents(self.platform)?;
  2795. std::fs::write(dest, plist)?;
  2796. }
  2797. Platform::Ios => {
  2798. let dest = self.root_dir().join("Info.plist");
  2799. let plist = self.info_plist_contents(self.platform)?;
  2800. std::fs::write(dest, plist)?;
  2801. }
  2802. // AndroidManifest.xml
  2803. // er.... maybe even all the kotlin/java/gradle stuff?
  2804. Platform::Android => {}
  2805. // Probably some custom format or a plist file (haha)
  2806. // When we do the proper bundle, we'll need to do something with wix templates, I think?
  2807. Platform::Windows => {}
  2808. // eventually we'll create the .appimage file, I guess?
  2809. Platform::Linux => {}
  2810. // These are served as folders, not appimages, so we don't need to do anything special (I think?)
  2811. // Eventually maybe write some secrets/.env files for the server?
  2812. // We could also distribute them as a deb/rpm for linux and msi for windows
  2813. Platform::Web => {}
  2814. Platform::Server => {}
  2815. Platform::Liveview => {}
  2816. }
  2817. Ok(())
  2818. }
  2819. /// Run the optimizers, obfuscators, minimizers, signers, etc
  2820. async fn optimize(&self, ctx: &BuildContext) -> Result<()> {
  2821. match self.platform {
  2822. Platform::Web => {
  2823. // Compress the asset dir
  2824. // If pre-compressing is enabled, we can pre_compress the wasm-bindgen output
  2825. let pre_compress = self.should_pre_compress_web_assets(self.release);
  2826. if pre_compress {
  2827. ctx.status_compressing_assets();
  2828. let asset_dir = self.asset_dir();
  2829. tokio::task::spawn_blocking(move || {
  2830. crate::fastfs::pre_compress_folder(&asset_dir, pre_compress)
  2831. })
  2832. .await
  2833. .unwrap()?;
  2834. }
  2835. }
  2836. Platform::MacOS => {}
  2837. Platform::Windows => {}
  2838. Platform::Linux => {}
  2839. Platform::Ios => {}
  2840. Platform::Android => {}
  2841. Platform::Server => {}
  2842. Platform::Liveview => {}
  2843. }
  2844. Ok(())
  2845. }
  2846. /// Check if assets should be pre_compressed. This will only be true in release mode if the user
  2847. /// has enabled pre_compress in the web config.
  2848. fn should_pre_compress_web_assets(&self, release: bool) -> bool {
  2849. self.config.web.pre_compress & release
  2850. }
  2851. /// Bundle the web app
  2852. /// - Run wasm-bindgen
  2853. /// - Bundle split
  2854. /// - Run wasm-opt
  2855. /// - Register the .wasm and .js files with the asset system
  2856. async fn bundle_web(
  2857. &self,
  2858. ctx: &BuildContext,
  2859. exe: &Path,
  2860. assets: &mut AssetManifest,
  2861. ) -> Result<()> {
  2862. use crate::{wasm_bindgen::WasmBindgen, wasm_opt};
  2863. use std::fmt::Write;
  2864. // Locate the output of the build files and the bindgen output
  2865. // We'll fill these in a second if they don't already exist
  2866. let bindgen_outdir = self.wasm_bindgen_out_dir();
  2867. let post_bindgen_wasm = self.wasm_bindgen_wasm_output_file();
  2868. let should_bundle_split: bool = self.wasm_split;
  2869. let bindgen_version = self
  2870. .wasm_bindgen_version()
  2871. .expect("this should have been checked by tool verification");
  2872. // Prepare any work dirs
  2873. _ = std::fs::remove_dir_all(&bindgen_outdir);
  2874. std::fs::create_dir_all(&bindgen_outdir)?;
  2875. // Lift the internal functions to exports
  2876. if ctx.mode == BuildMode::Fat {
  2877. let unprocessed = std::fs::read(exe)?;
  2878. let all_exported_bytes = crate::build::prepare_wasm_base_module(&unprocessed)?;
  2879. std::fs::write(exe, all_exported_bytes)?;
  2880. }
  2881. // Prepare our configuration
  2882. //
  2883. // we turn off debug symbols in dev mode but leave them on in release mode (weird!) since
  2884. // wasm-opt and wasm-split need them to do better optimizations.
  2885. //
  2886. // We leave demangling to false since it's faster and these tools seem to prefer the raw symbols.
  2887. // todo(jon): investigate if the chrome extension needs them demangled or demangles them automatically.
  2888. let will_wasm_opt = (self.release || self.wasm_split)
  2889. && (self.workspace.wasm_opt.is_some() || cfg!(feature = "optimizations"));
  2890. let keep_debug = self.config.web.wasm_opt.debug
  2891. || self.debug_symbols
  2892. || self.wasm_split
  2893. || !self.release
  2894. || will_wasm_opt
  2895. || ctx.mode == BuildMode::Fat;
  2896. let keep_names = will_wasm_opt || ctx.mode == BuildMode::Fat;
  2897. let package_to_asset = self.release && !should_bundle_split;
  2898. let demangle = false;
  2899. let wasm_opt_options = WasmOptConfig {
  2900. memory_packing: self.wasm_split,
  2901. debug: self.debug_symbols,
  2902. ..self.config.web.wasm_opt.clone()
  2903. };
  2904. // Run wasm-bindgen. Some of the options are not "optimal" but will be fixed up by wasm-opt
  2905. //
  2906. // There's performance implications here. Running with --debug is slower than without
  2907. // We're keeping around lld sections and names but wasm-opt will fix them
  2908. // todo(jon): investigate a good balance of wiping debug symbols during dev (or doing a double build?)
  2909. ctx.status_wasm_bindgen_start();
  2910. tracing::debug!(dx_src = ?TraceSrc::Bundle, "Running wasm-bindgen");
  2911. let start = std::time::Instant::now();
  2912. WasmBindgen::new(&bindgen_version)
  2913. .input_path(exe)
  2914. .target("web")
  2915. .debug(keep_debug)
  2916. .demangle(demangle)
  2917. .keep_debug(keep_debug)
  2918. .keep_lld_sections(true)
  2919. .out_name(self.executable_name())
  2920. .out_dir(&bindgen_outdir)
  2921. .remove_name_section(!keep_names)
  2922. .remove_producers_section(!keep_names)
  2923. .run()
  2924. .await
  2925. .context("Failed to generate wasm-bindgen bindings")?;
  2926. tracing::debug!(dx_src = ?TraceSrc::Bundle, "wasm-bindgen complete in {:?}", start.elapsed());
  2927. // Run bundle splitting if the user has requested it
  2928. // It's pretty expensive but because of rayon should be running separate threads, hopefully
  2929. // not blocking this thread. Dunno if that's true
  2930. if should_bundle_split {
  2931. ctx.status_splitting_bundle();
  2932. if !will_wasm_opt {
  2933. return Err(anyhow::anyhow!(
  2934. "Bundle splitting requires wasm-opt to be installed or the CLI to be built with `--features optimizations`. Please install wasm-opt and try again."
  2935. )
  2936. .into());
  2937. }
  2938. // Load the contents of these binaries since we need both of them
  2939. // We're going to use the default makeLoad glue from wasm-split
  2940. let original = std::fs::read(exe)?;
  2941. let bindgened = std::fs::read(&post_bindgen_wasm)?;
  2942. let mut glue = wasm_split_cli::MAKE_LOAD_JS.to_string();
  2943. // Run the emitter
  2944. let splitter = wasm_split_cli::Splitter::new(&original, &bindgened);
  2945. let modules = splitter
  2946. .context("Failed to parse wasm for splitter")?
  2947. .emit()
  2948. .context("Failed to emit wasm split modules")?;
  2949. // Write the chunks that contain shared imports
  2950. // These will be in the format of chunk_0_modulename.wasm - this is hardcoded in wasm-split
  2951. tracing::debug!("Writing split chunks to disk");
  2952. for (idx, chunk) in modules.chunks.iter().enumerate() {
  2953. let path = bindgen_outdir.join(format!("chunk_{}_{}.wasm", idx, chunk.module_name));
  2954. wasm_opt::write_wasm(&chunk.bytes, &path, &wasm_opt_options).await?;
  2955. writeln!(
  2956. glue, "export const __wasm_split_load_chunk_{idx} = makeLoad(\"/assets/{url}\", [], fusedImports);",
  2957. url = assets
  2958. .register_asset(&path, AssetOptions::Unknown)?.bundled_path(),
  2959. )?;
  2960. }
  2961. // Write the modules that contain the entrypoints
  2962. tracing::debug!("Writing split modules to disk");
  2963. for (idx, module) in modules.modules.iter().enumerate() {
  2964. let comp_name = module
  2965. .component_name
  2966. .as_ref()
  2967. .context("generated bindgen module has no name?")?;
  2968. let path = bindgen_outdir.join(format!("module_{}_{}.wasm", idx, comp_name));
  2969. wasm_opt::write_wasm(&module.bytes, &path, &wasm_opt_options).await?;
  2970. let hash_id = module
  2971. .hash_id
  2972. .as_ref()
  2973. .context("generated wasm-split bindgen module has no hash id?")?;
  2974. writeln!(
  2975. glue,
  2976. "export const __wasm_split_load_{module}_{hash_id}_{comp_name} = makeLoad(\"/assets/{url}\", [{deps}], fusedImports);",
  2977. module = module.module_name,
  2978. // Again, register this wasm with the asset system
  2979. url = assets
  2980. .register_asset(&path, AssetOptions::Unknown)?.bundled_path(),
  2981. // This time, make sure to write the dependencies of this chunk
  2982. // The names here are again, hardcoded in wasm-split - fix this eventually.
  2983. deps = module
  2984. .relies_on_chunks
  2985. .iter()
  2986. .map(|idx| format!("__wasm_split_load_chunk_{idx}"))
  2987. .collect::<Vec<_>>()
  2988. .join(", ")
  2989. )?;
  2990. }
  2991. // Write the js binding
  2992. // It's not registered as an asset since it will get included in the main.js file
  2993. let js_output_path = bindgen_outdir.join("__wasm_split.js");
  2994. std::fs::write(&js_output_path, &glue)?;
  2995. // Make sure to write some entropy to the main.js file so it gets a new hash
  2996. // If we don't do this, the main.js file will be cached and never pick up the chunk names
  2997. let uuid = Uuid::new_v5(&Uuid::NAMESPACE_URL, glue.as_bytes());
  2998. std::fs::OpenOptions::new()
  2999. .append(true)
  3000. .open(self.wasm_bindgen_js_output_file())
  3001. .context("Failed to open main.js file")?
  3002. .write_all(format!("/*{uuid}*/").as_bytes())?;
  3003. // Write the main wasm_bindgen file and register it with the asset system
  3004. // This will overwrite the file in place
  3005. // We will wasm-opt it in just a second...
  3006. std::fs::write(&post_bindgen_wasm, modules.main.bytes).unwrap();
  3007. }
  3008. if matches!(ctx.mode, BuildMode::Fat) {
  3009. // add `export { __wbg_get_imports };` to the end of the wasmbindgen js file
  3010. let mut js = std::fs::read(self.wasm_bindgen_js_output_file())?;
  3011. writeln!(js, "\nexport {{ __wbg_get_imports }};")?;
  3012. std::fs::write(self.wasm_bindgen_js_output_file(), js)?;
  3013. }
  3014. // Make sure to optimize the main wasm file if requested or if bundle splitting
  3015. if should_bundle_split || self.release {
  3016. ctx.status_optimizing_wasm();
  3017. wasm_opt::optimize(&post_bindgen_wasm, &post_bindgen_wasm, &wasm_opt_options).await?;
  3018. }
  3019. // In release mode, we make the wasm and bindgen files into assets so they get bundled with max
  3020. // optimizations.
  3021. let wasm_path = if package_to_asset {
  3022. // Make sure to register the main wasm file with the asset system
  3023. let name = assets.register_asset(&post_bindgen_wasm, AssetOptions::Unknown)?;
  3024. format!("assets/{}", name.bundled_path())
  3025. } else {
  3026. let asset = self.wasm_bindgen_wasm_output_file();
  3027. format!("wasm/{}", asset.file_name().unwrap().to_str().unwrap())
  3028. };
  3029. let js_path = if package_to_asset {
  3030. // Register the main.js with the asset system so it bundles in the snippets and optimizes
  3031. let name = assets.register_asset(
  3032. &self.wasm_bindgen_js_output_file(),
  3033. AssetOptions::Js(JsAssetOptions::new().with_minify(true).with_preload(true)),
  3034. )?;
  3035. format!("assets/{}", name.bundled_path())
  3036. } else {
  3037. let asset = self.wasm_bindgen_js_output_file();
  3038. format!("wasm/{}", asset.file_name().unwrap().to_str().unwrap())
  3039. };
  3040. // Remove the wasm dir if we packaged it to an "asset"-type app
  3041. if package_to_asset {
  3042. std::fs::remove_dir_all(&bindgen_outdir).context("Failed to remove bindgen outdir")?;
  3043. }
  3044. // Write the index.html file with the pre-configured contents we got from pre-rendering
  3045. std::fs::write(
  3046. self.root_dir().join("index.html"),
  3047. self.prepare_html(assets, &wasm_path, &js_path).unwrap(),
  3048. )?;
  3049. Ok(())
  3050. }
  3051. fn info_plist_contents(&self, platform: Platform) -> Result<String> {
  3052. #[derive(Serialize)]
  3053. pub struct InfoPlistData {
  3054. pub display_name: String,
  3055. pub bundle_name: String,
  3056. pub bundle_identifier: String,
  3057. pub executable_name: String,
  3058. }
  3059. // Attempt to use the user's manually specified
  3060. let _app = &self.config.application;
  3061. match platform {
  3062. Platform::MacOS => {
  3063. if let Some(macos_info_plist) = _app.macos_info_plist.as_deref() {
  3064. return Ok(std::fs::read_to_string(macos_info_plist)?);
  3065. }
  3066. }
  3067. Platform::Ios => {
  3068. if let Some(macos_info_plist) = _app.ios_info_plist.as_deref() {
  3069. return Ok(std::fs::read_to_string(macos_info_plist)?);
  3070. }
  3071. }
  3072. _ => {}
  3073. }
  3074. match platform {
  3075. Platform::MacOS => handlebars::Handlebars::new()
  3076. .render_template(
  3077. include_str!("../../assets/macos/mac.plist.hbs"),
  3078. &InfoPlistData {
  3079. display_name: self.bundled_app_name(),
  3080. bundle_name: self.bundled_app_name(),
  3081. executable_name: self.platform_exe_name(),
  3082. bundle_identifier: self.bundle_identifier(),
  3083. },
  3084. )
  3085. .map_err(|e| e.into()),
  3086. Platform::Ios => handlebars::Handlebars::new()
  3087. .render_template(
  3088. include_str!("../../assets/ios/ios.plist.hbs"),
  3089. &InfoPlistData {
  3090. display_name: self.bundled_app_name(),
  3091. bundle_name: self.bundled_app_name(),
  3092. executable_name: self.platform_exe_name(),
  3093. bundle_identifier: self.bundle_identifier(),
  3094. },
  3095. )
  3096. .map_err(|e| e.into()),
  3097. _ => Err(anyhow::anyhow!("Unsupported platform for Info.plist").into()),
  3098. }
  3099. }
  3100. /// Run any final tools to produce apks or other artifacts we might need.
  3101. ///
  3102. /// This might include codesigning, zipping, creating an appimage, etc
  3103. async fn assemble(&self, ctx: &BuildContext) -> Result<()> {
  3104. if let Platform::Android = self.platform {
  3105. ctx.status_running_gradle();
  3106. // When the build mode is set to release and there is an Android signature configuration, use assembleRelease
  3107. let build_type = if self.release && self.config.bundle.android.is_some() {
  3108. "assembleRelease"
  3109. } else {
  3110. "assembleDebug"
  3111. };
  3112. let output = Command::new(self.gradle_exe()?)
  3113. .arg(build_type)
  3114. .current_dir(self.root_dir())
  3115. .output()
  3116. .await?;
  3117. if !output.status.success() {
  3118. return Err(anyhow::anyhow!("Failed to assemble apk: {output:?}").into());
  3119. }
  3120. }
  3121. Ok(())
  3122. }
  3123. /// Run bundleRelease and return the path to the `.aab` file
  3124. ///
  3125. /// <https://stackoverflow.com/questions/57072558/whats-the-difference-between-gradlewassemblerelease-gradlewinstallrelease-and>
  3126. pub(crate) async fn android_gradle_bundle(&self) -> Result<PathBuf> {
  3127. let output = Command::new(self.gradle_exe()?)
  3128. .arg("bundleRelease")
  3129. .current_dir(self.root_dir())
  3130. .output()
  3131. .await
  3132. .context("Failed to run gradle bundleRelease")?;
  3133. if !output.status.success() {
  3134. return Err(anyhow::anyhow!("Failed to bundleRelease: {output:?}").into());
  3135. }
  3136. let app_release = self
  3137. .root_dir()
  3138. .join("app")
  3139. .join("build")
  3140. .join("outputs")
  3141. .join("bundle")
  3142. .join("release");
  3143. // Rename it to Name-arch.aab
  3144. let from = app_release.join("app-release.aab");
  3145. let to = app_release.join(format!("{}-{}.aab", self.bundled_app_name(), self.triple));
  3146. std::fs::rename(from, &to).context("Failed to rename aab")?;
  3147. Ok(to)
  3148. }
  3149. fn gradle_exe(&self) -> Result<PathBuf> {
  3150. // make sure we can execute the gradlew script
  3151. #[cfg(unix)]
  3152. {
  3153. use std::os::unix::prelude::PermissionsExt;
  3154. std::fs::set_permissions(
  3155. self.root_dir().join("gradlew"),
  3156. std::fs::Permissions::from_mode(0o755),
  3157. )?;
  3158. }
  3159. let gradle_exec_name = match cfg!(windows) {
  3160. true => "gradlew.bat",
  3161. false => "gradlew",
  3162. };
  3163. Ok(self.root_dir().join(gradle_exec_name))
  3164. }
  3165. pub(crate) fn debug_apk_path(&self) -> PathBuf {
  3166. self.root_dir()
  3167. .join("app")
  3168. .join("build")
  3169. .join("outputs")
  3170. .join("apk")
  3171. .join("debug")
  3172. .join("app-debug.apk")
  3173. }
  3174. /// We only really currently care about:
  3175. ///
  3176. /// - app dir (.app, .exe, .apk, etc)
  3177. /// - assetas dir
  3178. /// - exe dir (.exe, .app, .apk, etc)
  3179. /// - extra scaffolding
  3180. ///
  3181. /// It's not guaranteed that they're different from any other folder
  3182. pub(crate) fn prepare_build_dir(&self) -> Result<()> {
  3183. use std::fs::{create_dir_all, remove_dir_all};
  3184. use std::sync::OnceLock;
  3185. static INITIALIZED: OnceLock<Result<()>> = OnceLock::new();
  3186. let success = INITIALIZED.get_or_init(|| {
  3187. if self.platform != Platform::Server {
  3188. _ = remove_dir_all(self.exe_dir());
  3189. }
  3190. self.flush_session_cache();
  3191. create_dir_all(self.root_dir())?;
  3192. create_dir_all(self.exe_dir())?;
  3193. create_dir_all(self.asset_dir())?;
  3194. tracing::debug!(
  3195. r#"Initialized build dirs:
  3196. • root dir: {:?}
  3197. • exe dir: {:?}
  3198. • asset dir: {:?}"#,
  3199. self.root_dir(),
  3200. self.exe_dir(),
  3201. self.asset_dir(),
  3202. );
  3203. // we could download the templates from somewhere (github?) but after having banged my head against
  3204. // cargo-mobile2 for ages, I give up with that. We're literally just going to hardcode the templates
  3205. // by writing them here.
  3206. if let Platform::Android = self.platform {
  3207. self.build_android_app_dir()?;
  3208. }
  3209. Ok(())
  3210. });
  3211. if let Err(e) = success.as_ref() {
  3212. return Err(format!("Failed to initialize build directory: {e}").into());
  3213. }
  3214. Ok(())
  3215. }
  3216. pub(crate) fn asset_dir(&self) -> PathBuf {
  3217. match self.platform {
  3218. Platform::MacOS => self
  3219. .root_dir()
  3220. .join("Contents")
  3221. .join("Resources")
  3222. .join("assets"),
  3223. Platform::Android => self
  3224. .root_dir()
  3225. .join("app")
  3226. .join("src")
  3227. .join("main")
  3228. .join("assets"),
  3229. // everyone else is soooo normal, just app/assets :)
  3230. Platform::Web
  3231. | Platform::Ios
  3232. | Platform::Windows
  3233. | Platform::Linux
  3234. | Platform::Server
  3235. | Platform::Liveview => self.root_dir().join("assets"),
  3236. }
  3237. }
  3238. /// The directory in which we'll put the main exe
  3239. ///
  3240. /// Mac, Android, Web are a little weird
  3241. /// - mac wants to be in Contents/MacOS
  3242. /// - android wants to be in jniLibs/arm64-v8a (or others, depending on the platform / architecture)
  3243. /// - web wants to be in wasm (which... we don't really need to, we could just drop the wasm into public and it would work)
  3244. ///
  3245. /// I think all others are just in the root folder
  3246. ///
  3247. /// todo(jon): investigate if we need to put .wasm in `wasm`. It kinda leaks implementation details, which ideally we don't want to do.
  3248. fn exe_dir(&self) -> PathBuf {
  3249. match self.platform {
  3250. Platform::MacOS => self.root_dir().join("Contents").join("MacOS"),
  3251. Platform::Web => self.root_dir().join("wasm"),
  3252. // Android has a whole build structure to it
  3253. Platform::Android => self
  3254. .root_dir()
  3255. .join("app")
  3256. .join("src")
  3257. .join("main")
  3258. .join("jniLibs")
  3259. .join(AndroidTools::android_jnilib(&self.triple)),
  3260. // these are all the same, I think?
  3261. Platform::Windows
  3262. | Platform::Linux
  3263. | Platform::Ios
  3264. | Platform::Server
  3265. | Platform::Liveview => self.root_dir(),
  3266. }
  3267. }
  3268. /// Get the path to the wasm bindgen temporary output folder
  3269. fn wasm_bindgen_out_dir(&self) -> PathBuf {
  3270. self.root_dir().join("wasm")
  3271. }
  3272. /// Get the path to the wasm bindgen javascript output file
  3273. pub(crate) fn wasm_bindgen_js_output_file(&self) -> PathBuf {
  3274. self.wasm_bindgen_out_dir()
  3275. .join(self.executable_name())
  3276. .with_extension("js")
  3277. }
  3278. /// Get the path to the wasm bindgen wasm output file
  3279. pub(crate) fn wasm_bindgen_wasm_output_file(&self) -> PathBuf {
  3280. self.wasm_bindgen_out_dir()
  3281. .join(format!("{}_bg", self.executable_name()))
  3282. .with_extension("wasm")
  3283. }
  3284. /// Get the path to the asset optimizer version file
  3285. pub(crate) fn asset_optimizer_version_file(&self) -> PathBuf {
  3286. self.platform_dir().join(".cli-version")
  3287. }
  3288. fn flush_session_cache(&self) {
  3289. let cache_dir = self.session_cache_dir();
  3290. _ = std::fs::remove_dir_all(&cache_dir);
  3291. _ = std::fs::create_dir_all(&cache_dir);
  3292. }
  3293. /// Check for tooling that might be required for this build.
  3294. ///
  3295. /// This should generally be only called on the first build since it takes time to verify the tooling
  3296. /// is in place, and we don't want to slow down subsequent builds.
  3297. pub(crate) async fn verify_tooling(&self, ctx: &BuildContext) -> Result<()> {
  3298. ctx.status_installing_tooling();
  3299. self
  3300. .initialize_profiles()
  3301. .context("Failed to initialize profiles - dioxus can't build without them. You might need to initialize them yourself.")?;
  3302. match self.platform {
  3303. Platform::Web => self.verify_web_tooling().await?,
  3304. Platform::Ios => self.verify_ios_tooling().await?,
  3305. Platform::Android => self.verify_android_tooling().await?,
  3306. Platform::Linux => self.verify_linux_tooling().await?,
  3307. Platform::MacOS | Platform::Windows | Platform::Server | Platform::Liveview => {}
  3308. }
  3309. Ok(())
  3310. }
  3311. async fn verify_web_tooling(&self) -> Result<()> {
  3312. // Install target using rustup.
  3313. #[cfg(not(feature = "no-downloads"))]
  3314. if !self.workspace.has_wasm32_unknown_unknown() {
  3315. tracing::info!(
  3316. "Web platform requires wasm32-unknown-unknown to be installed. Installing..."
  3317. );
  3318. let _ = tokio::process::Command::new("rustup")
  3319. .args(["target", "add", "wasm32-unknown-unknown"])
  3320. .output()
  3321. .await?;
  3322. }
  3323. // Ensure target is installed.
  3324. if !self.workspace.has_wasm32_unknown_unknown() {
  3325. return Err(Error::Other(anyhow::anyhow!(
  3326. "Missing target wasm32-unknown-unknown."
  3327. )));
  3328. }
  3329. // Wasm bindgen
  3330. let krate_bindgen_version = self.wasm_bindgen_version().ok_or(anyhow::anyhow!(
  3331. "failed to detect wasm-bindgen version, unable to proceed"
  3332. ))?;
  3333. WasmBindgen::verify_install(&krate_bindgen_version).await?;
  3334. Ok(())
  3335. }
  3336. /// Currently does nothing, but eventually we need to check that the mobile tooling is installed.
  3337. ///
  3338. /// For ios, this would be just aarch64-apple-ios + aarch64-apple-ios-sim, as well as xcrun and xcode-select
  3339. ///
  3340. /// We don't auto-install these yet since we're not doing an architecture check. We assume most users
  3341. /// are running on an Apple Silicon Mac, but it would be confusing if we installed these when we actually
  3342. /// should be installing the x86 versions.
  3343. async fn verify_ios_tooling(&self) -> Result<()> {
  3344. // open the simulator
  3345. // _ = tokio::process::Command::new("open")
  3346. // .arg("/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app")
  3347. // .output()
  3348. // .await;
  3349. // Now xcrun to open the device
  3350. // todo: we should try and query the device list and/or parse it rather than hardcode this simulator
  3351. // _ = tokio::process::Command::new("xcrun")
  3352. // .args(["simctl", "boot", "83AE3067-987F-4F85-AE3D-7079EF48C967"])
  3353. // .output()
  3354. // .await;
  3355. // if !rustup
  3356. // .installed_toolchains
  3357. // .contains(&"aarch64-apple-ios".to_string())
  3358. // {
  3359. // tracing::error!("You need to install aarch64-apple-ios to build for ios. Run `rustup target add aarch64-apple-ios` to install it.");
  3360. // }
  3361. // if !rustup
  3362. // .installed_toolchains
  3363. // .contains(&"aarch64-apple-ios-sim".to_string())
  3364. // {
  3365. // tracing::error!("You need to install aarch64-apple-ios to build for ios. Run `rustup target add aarch64-apple-ios` to install it.");
  3366. // }
  3367. Ok(())
  3368. }
  3369. /// Check if the android tooling is installed
  3370. ///
  3371. /// looks for the android sdk + ndk
  3372. ///
  3373. /// will do its best to fill in the missing bits by exploring the sdk structure
  3374. /// IE will attempt to use the Java installed from android studio if possible.
  3375. async fn verify_android_tooling(&self) -> Result<()> {
  3376. let linker = self.workspace.android_tools()?.android_cc(&self.triple);
  3377. tracing::debug!("Verifying android linker: {linker:?}");
  3378. if linker.exists() {
  3379. return Ok(());
  3380. }
  3381. Err(anyhow::anyhow!(
  3382. "Android linker not found. Please set the `ANDROID_NDK_HOME` environment variable to the root of your NDK installation."
  3383. ).into())
  3384. }
  3385. /// Ensure the right dependencies are installed for linux apps.
  3386. /// This varies by distro, so we just do nothing for now.
  3387. ///
  3388. /// Eventually, we want to check for the prereqs for wry/tao as outlined by tauri:
  3389. /// <https://tauri.app/start/prerequisites/>
  3390. async fn verify_linux_tooling(&self) -> Result<()> {
  3391. Ok(())
  3392. }
  3393. /// update the mtime of the "main" file to bust the fingerprint, forcing rustc to recompile it.
  3394. ///
  3395. /// This prevents rustc from using the cached version of the binary, which can cause issues
  3396. /// with our hotpatching setup since it uses linker interception.
  3397. ///
  3398. /// This is sadly a hack. I think there might be other ways of busting the fingerprint (rustc wrapper?)
  3399. /// but that would require relying on cargo internals.
  3400. ///
  3401. /// This might stop working if/when cargo stabilizes contents-based fingerprinting.
  3402. fn bust_fingerprint(&self, ctx: &BuildContext) -> Result<()> {
  3403. if !matches!(ctx.mode, BuildMode::Thin { .. }) {
  3404. std::fs::File::open(&self.crate_target.src_path)?.set_modified(SystemTime::now())?;
  3405. // read and write the file to update the mtime
  3406. if cfg!(target_os = "windows") {
  3407. let contents = std::fs::read_to_string(&self.crate_target.src_path)?;
  3408. _ = std::fs::write(&self.crate_target.src_path, contents);
  3409. }
  3410. }
  3411. Ok(())
  3412. }
  3413. async fn create_patch_cache(&self, exe: &Path) -> Result<HotpatchModuleCache> {
  3414. let exe = match self.platform {
  3415. Platform::Web => self.wasm_bindgen_wasm_output_file(),
  3416. _ => exe.to_path_buf(),
  3417. };
  3418. Ok(HotpatchModuleCache::new(&exe, &self.triple)?)
  3419. }
  3420. /// Users create an index.html for their SPA if they want it
  3421. ///
  3422. /// We always write our wasm as main.js and main_bg.wasm
  3423. ///
  3424. /// In prod we run the optimizer which bundles everything together properly
  3425. ///
  3426. /// So their index.html needs to include main.js in the scripts otherwise nothing happens?
  3427. ///
  3428. /// Seems like every platform has a weird file that declares a bunch of stuff
  3429. /// - web: index.html
  3430. /// - ios: info.plist
  3431. /// - macos: info.plist
  3432. /// - linux: appimage root thing?
  3433. /// - android: androidmanifest.xml
  3434. ///
  3435. /// You also might different variants of these files (staging / prod) and different flavors (eu/us)
  3436. ///
  3437. /// web's index.html is weird since it's not just a bundle format but also a *content* format
  3438. pub(crate) fn prepare_html(
  3439. &self,
  3440. assets: &AssetManifest,
  3441. wasm_path: &str,
  3442. js_path: &str,
  3443. ) -> Result<String> {
  3444. let mut html = {
  3445. const DEV_DEFAULT_HTML: &str = include_str!("../../assets/web/dev.index.html");
  3446. const PROD_DEFAULT_HTML: &str = include_str!("../../assets/web/prod.index.html");
  3447. let crate_root: &Path = &self.crate_dir();
  3448. let custom_html_file = crate_root.join("index.html");
  3449. let default_html = match self.release {
  3450. true => PROD_DEFAULT_HTML,
  3451. false => DEV_DEFAULT_HTML,
  3452. };
  3453. std::fs::read_to_string(custom_html_file).unwrap_or_else(|_| String::from(default_html))
  3454. };
  3455. // Inject any resources from the config into the html
  3456. self.inject_resources(assets, &mut html)?;
  3457. // Inject loading scripts if they are not already present
  3458. self.inject_loading_scripts(&mut html);
  3459. // Replace any special placeholders in the HTML with resolved values
  3460. self.replace_template_placeholders(&mut html, wasm_path, js_path);
  3461. let title = self.config.web.app.title.clone();
  3462. Self::replace_or_insert_before("{app_title}", "</title", &title, &mut html);
  3463. Ok(html)
  3464. }
  3465. fn is_dev_build(&self) -> bool {
  3466. !self.release
  3467. }
  3468. // Inject any resources from the config into the html
  3469. fn inject_resources(&self, assets: &AssetManifest, html: &mut String) -> Result<()> {
  3470. use std::fmt::Write;
  3471. // Collect all resources into a list of styles and scripts
  3472. let resources = &self.config.web.resource;
  3473. let mut style_list = resources.style.clone().unwrap_or_default();
  3474. let mut script_list = resources.script.clone().unwrap_or_default();
  3475. if self.is_dev_build() {
  3476. style_list.extend(resources.dev.style.iter().cloned());
  3477. script_list.extend(resources.dev.script.iter().cloned());
  3478. }
  3479. let mut head_resources = String::new();
  3480. // Add all styles to the head
  3481. for style in &style_list {
  3482. writeln!(
  3483. &mut head_resources,
  3484. "<link rel=\"stylesheet\" href=\"{}\">",
  3485. &style.to_str().unwrap(),
  3486. )?;
  3487. }
  3488. // Add all scripts to the head
  3489. for script in &script_list {
  3490. writeln!(
  3491. &mut head_resources,
  3492. "<script src=\"{}\"></script>",
  3493. &script.to_str().unwrap(),
  3494. )?;
  3495. }
  3496. // Add the base path to the head if this is a debug build
  3497. if self.is_dev_build() {
  3498. if let Some(base_path) = &self.base_path() {
  3499. head_resources.push_str(&format_base_path_meta_element(base_path));
  3500. }
  3501. }
  3502. // Inject any resources from manganis into the head
  3503. for asset in assets.assets.values() {
  3504. let asset_path = asset.bundled_path();
  3505. match asset.options() {
  3506. AssetOptions::Css(css_options) => {
  3507. if css_options.preloaded() {
  3508. head_resources.push_str(&format!(
  3509. "<link rel=\"preload\" as=\"style\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
  3510. ))
  3511. }
  3512. }
  3513. AssetOptions::Image(image_options) => {
  3514. if image_options.preloaded() {
  3515. head_resources.push_str(&format!(
  3516. "<link rel=\"preload\" as=\"image\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
  3517. ))
  3518. }
  3519. }
  3520. AssetOptions::Js(js_options) => {
  3521. if js_options.preloaded() {
  3522. head_resources.push_str(&format!(
  3523. "<link rel=\"preload\" as=\"script\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
  3524. ))
  3525. }
  3526. }
  3527. _ => {}
  3528. }
  3529. }
  3530. // Manually inject the wasm file for preloading. WASM currently doesn't support preloading in the manganis asset system
  3531. let wasm_source_path = self.wasm_bindgen_wasm_output_file();
  3532. if let Some(wasm_path) = assets.assets.get(&wasm_source_path) {
  3533. let wasm_path = wasm_path.bundled_path();
  3534. head_resources.push_str(&format!(
  3535. "<link rel=\"preload\" as=\"fetch\" type=\"application/wasm\" href=\"/{{base_path}}/assets/{wasm_path}\" crossorigin>"
  3536. ));
  3537. Self::replace_or_insert_before("{style_include}", "</head", &head_resources, html);
  3538. }
  3539. Ok(())
  3540. }
  3541. /// Inject loading scripts if they are not already present
  3542. fn inject_loading_scripts(&self, html: &mut String) {
  3543. // If it looks like we are already loading wasm or the current build opted out of injecting loading scripts, don't inject anything
  3544. if !self.inject_loading_scripts || html.contains("__wbindgen_start") {
  3545. return;
  3546. }
  3547. // If not, insert the script
  3548. *html = html.replace(
  3549. "</body",
  3550. r#" <script>
  3551. // We can't use a module script here because we need to start the script immediately when streaming
  3552. import("/{base_path}/{js_path}").then(
  3553. ({ default: init, initSync, __wbg_get_imports }) => {
  3554. // export initSync in case a split module needs to initialize
  3555. window.__wasm_split_main_initSync = initSync;
  3556. // Actually perform the load
  3557. init({module_or_path: "/{base_path}/{wasm_path}"}).then((wasm) => {
  3558. // assign this module to be accessible globally
  3559. window.__dx_mainWasm = wasm;
  3560. window.__dx_mainInit = init;
  3561. window.__dx_mainInitSync = initSync;
  3562. window.__dx___wbg_get_imports = __wbg_get_imports;
  3563. if (wasm.__wbindgen_start == undefined) {
  3564. wasm.main();
  3565. }
  3566. });
  3567. }
  3568. );
  3569. </script>
  3570. </body"#,
  3571. );
  3572. }
  3573. /// Replace any special placeholders in the HTML with resolved values
  3574. fn replace_template_placeholders(&self, html: &mut String, wasm_path: &str, js_path: &str) {
  3575. let base_path = self.base_path_or_default();
  3576. *html = html.replace("{base_path}", base_path);
  3577. let app_name = &self.executable_name();
  3578. // If the html contains the old `{app_name}` placeholder, replace {app_name}_bg.wasm and {app_name}.js
  3579. // with the new paths
  3580. *html = html.replace("wasm/{app_name}_bg.wasm", wasm_path);
  3581. *html = html.replace("wasm/{app_name}.js", js_path);
  3582. // Otherwise replace the new placeholders
  3583. *html = html.replace("{wasm_path}", wasm_path);
  3584. *html = html.replace("{js_path}", js_path);
  3585. // Replace the app_name if we find it anywhere standalone
  3586. *html = html.replace("{app_name}", app_name);
  3587. }
  3588. /// Replace a string or insert the new contents before a marker
  3589. fn replace_or_insert_before(
  3590. replace: &str,
  3591. or_insert_before: &str,
  3592. with: &str,
  3593. content: &mut String,
  3594. ) {
  3595. if content.contains(replace) {
  3596. *content = content.replace(replace, with);
  3597. } else if let Some(pos) = content.find(or_insert_before) {
  3598. content.insert_str(pos, with);
  3599. }
  3600. }
  3601. /// Get the base path from the config or None if this is not a web or server build
  3602. pub(crate) fn base_path(&self) -> Option<&str> {
  3603. self.config
  3604. .web
  3605. .app
  3606. .base_path
  3607. .as_deref()
  3608. .filter(|_| matches!(self.platform, Platform::Web | Platform::Server))
  3609. }
  3610. /// Get the normalized base path for the application with `/` trimmed from both ends. If the base path is not set, this will return `.`.
  3611. pub(crate) fn base_path_or_default(&self) -> &str {
  3612. let trimmed_path = self.base_path().unwrap_or_default().trim_matches('/');
  3613. if trimmed_path.is_empty() {
  3614. "."
  3615. } else {
  3616. trimmed_path
  3617. }
  3618. }
  3619. /// Get the path to the package manifest directory
  3620. pub(crate) fn package_manifest_dir(&self) -> PathBuf {
  3621. self.workspace.krates[self.crate_package]
  3622. .manifest_path
  3623. .parent()
  3624. .unwrap()
  3625. .to_path_buf()
  3626. .into()
  3627. }
  3628. pub(crate) async fn start_simulators(&self) -> Result<()> {
  3629. if self.device {
  3630. return Ok(());
  3631. }
  3632. match self.platform {
  3633. // Boot an iOS simulator if one is not already running.
  3634. //
  3635. // We always choose the most recently opened simulator based on the xcrun list.
  3636. // Note that simulators can be running but the simulator app itself is not open.
  3637. // Calling `open::that` is always fine, even on running apps, since apps are singletons.
  3638. Platform::Ios => {
  3639. #[derive(Deserialize, Debug)]
  3640. struct XcrunListJson {
  3641. // "com.apple.CoreSimulator.SimRuntime.iOS-18-4": [{}, {}, {}]
  3642. devices: BTreeMap<String, Vec<XcrunDevice>>,
  3643. }
  3644. #[derive(Deserialize, Debug)]
  3645. struct XcrunDevice {
  3646. #[serde(rename = "lastBootedAt")]
  3647. last_booted_at: Option<String>,
  3648. udid: String,
  3649. name: String,
  3650. state: String,
  3651. }
  3652. let xcrun_list = Command::new("xcrun")
  3653. .arg("simctl")
  3654. .arg("list")
  3655. .arg("-j")
  3656. .output()
  3657. .await?;
  3658. let as_str = String::from_utf8_lossy(&xcrun_list.stdout);
  3659. let xcrun_list_json = serde_json::from_str::<XcrunListJson>(as_str.trim());
  3660. if let Ok(xcrun_list_json) = xcrun_list_json {
  3661. if xcrun_list_json.devices.is_empty() {
  3662. tracing::warn!(
  3663. "No iOS sdks installed found. Please install the iOS SDK in Xcode."
  3664. );
  3665. }
  3666. if let Some((_rt, devices)) = xcrun_list_json.devices.iter().next() {
  3667. if devices.iter().all(|device| device.state != "Booted") {
  3668. let last_booted =
  3669. devices
  3670. .iter()
  3671. .max_by_key(|device| match device.last_booted_at {
  3672. Some(ref last_booted) => last_booted,
  3673. None => "2000-01-01T01:01:01Z",
  3674. });
  3675. if let Some(device) = last_booted {
  3676. tracing::info!("Booting iOS simulator: \"{}\"", device.name);
  3677. Command::new("xcrun")
  3678. .arg("simctl")
  3679. .arg("boot")
  3680. .arg(&device.udid)
  3681. .output()
  3682. .await?;
  3683. }
  3684. }
  3685. }
  3686. }
  3687. let path_to_xcode = Command::new("xcode-select")
  3688. .arg("--print-path")
  3689. .output()
  3690. .await?;
  3691. let path_to_xcode: PathBuf = String::from_utf8_lossy(&path_to_xcode.stdout)
  3692. .as_ref()
  3693. .trim()
  3694. .into();
  3695. let path_to_sim = path_to_xcode.join("Applications").join("Simulator.app");
  3696. open::that(path_to_sim)?;
  3697. }
  3698. Platform::Android => {
  3699. let tools = self.workspace.android_tools()?;
  3700. tokio::spawn(async move {
  3701. let emulator = tools.emulator();
  3702. let avds = Command::new(&emulator)
  3703. .arg("-list-avds")
  3704. .output()
  3705. .await
  3706. .unwrap();
  3707. let avds = String::from_utf8_lossy(&avds.stdout);
  3708. let avd = avds.trim().lines().next().map(|s| s.trim().to_string());
  3709. if let Some(avd) = avd {
  3710. tracing::info!("Booting Android emulator: \"{avd}\"");
  3711. Command::new(&emulator)
  3712. .arg("-avd")
  3713. .arg(avd)
  3714. .args(["-netdelay", "none", "-netspeed", "full"])
  3715. .stdout(std::process::Stdio::null()) // prevent accumulating huge amounts of mem usage
  3716. .stderr(std::process::Stdio::null()) // prevent accumulating huge amounts of mem usage
  3717. .output()
  3718. .await
  3719. .unwrap();
  3720. } else {
  3721. tracing::warn!("No Android emulators found. Please create one using `emulator -avd <name>`");
  3722. }
  3723. });
  3724. }
  3725. _ => {
  3726. // nothing - maybe on the web we should open the browser?
  3727. }
  3728. };
  3729. Ok(())
  3730. }
  3731. fn select_ranlib(&self) -> Option<PathBuf> {
  3732. // prefer the modern llvm-ranlib if they have it
  3733. which::which("llvm-ranlib")
  3734. .or_else(|_| which::which("ranlib"))
  3735. .ok()
  3736. }
  3737. }