request.rs 176 KB

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