1
0

request.rs 153 KB

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