makedep.c 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369
  1. /*
  2. * Generate include file dependencies
  3. *
  4. * Copyright 1996, 2013, 2020 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include <signal.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include "tools.h"
  33. #include "wine/list.h"
  34. enum incl_type
  35. {
  36. INCL_NORMAL, /* #include "foo.h" */
  37. INCL_SYSTEM, /* #include <foo.h> */
  38. INCL_IMPORT, /* idl import "foo.idl" */
  39. INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
  40. INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
  41. INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
  42. };
  43. struct dependency
  44. {
  45. int line; /* source line where this header is included */
  46. enum incl_type type; /* type of include */
  47. char *name; /* header name */
  48. };
  49. struct file
  50. {
  51. struct list entry;
  52. char *name; /* full file name relative to cwd */
  53. void *args; /* custom arguments for makefile rule */
  54. unsigned int flags; /* flags (see below) */
  55. unsigned int deps_count; /* files in use */
  56. unsigned int deps_size; /* total allocated size */
  57. struct dependency *deps; /* all header dependencies */
  58. };
  59. struct incl_file
  60. {
  61. struct list entry;
  62. struct file *file;
  63. char *name;
  64. char *filename;
  65. char *basename; /* base target name for generated files */
  66. char *sourcename; /* source file name for generated headers */
  67. struct incl_file *included_by; /* file that included this one */
  68. int included_line; /* line where this file was included */
  69. enum incl_type type; /* type of include */
  70. int use_msvcrt:1; /* put msvcrt headers in the search path? */
  71. int is_external:1; /* file from external library? */
  72. struct incl_file *owner;
  73. unsigned int files_count; /* files in use */
  74. unsigned int files_size; /* total allocated size */
  75. struct incl_file **files;
  76. struct strarray dependencies; /* file dependencies */
  77. struct strarray importlibdeps; /* importlib dependencies */
  78. };
  79. #define FLAG_GENERATED 0x000001 /* generated file */
  80. #define FLAG_INSTALL 0x000002 /* file to install */
  81. #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
  82. #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
  83. #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
  84. #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
  85. #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
  86. #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
  87. #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
  88. #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
  89. #define FLAG_RC_PO 0x010000 /* rc file contains translations */
  90. #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
  91. #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
  92. #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
  93. static const struct
  94. {
  95. unsigned int flag;
  96. const char *ext;
  97. } idl_outputs[] =
  98. {
  99. { FLAG_IDL_TYPELIB, "_l.res" },
  100. { FLAG_IDL_REGTYPELIB, "_t.res" },
  101. { FLAG_IDL_CLIENT, "_c.c" },
  102. { FLAG_IDL_IDENT, "_i.c" },
  103. { FLAG_IDL_PROXY, "_p.c" },
  104. { FLAG_IDL_SERVER, "_s.c" },
  105. { FLAG_IDL_REGISTER, "_r.res" },
  106. { FLAG_IDL_HEADER, ".h" }
  107. };
  108. #define HASH_SIZE 997
  109. static struct list files[HASH_SIZE];
  110. enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
  111. /* variables common to all makefiles */
  112. static struct strarray linguas;
  113. static struct strarray dll_flags;
  114. static struct strarray unix_dllflags;
  115. static struct strarray target_flags;
  116. static struct strarray msvcrt_flags;
  117. static struct strarray extra_cflags;
  118. static struct strarray extra_cross_cflags;
  119. static struct strarray extra_cflags_extlib;
  120. static struct strarray extra_cross_cflags_extlib;
  121. static struct strarray cpp_flags;
  122. static struct strarray lddll_flags;
  123. static struct strarray libs;
  124. static struct strarray enable_tests;
  125. static struct strarray cmdline_vars;
  126. static struct strarray subdirs;
  127. static struct strarray disabled_dirs;
  128. static struct strarray delay_import_libs;
  129. static struct strarray top_install_lib;
  130. static struct strarray top_install_dev;
  131. static const char *root_src_dir;
  132. static const char *tools_dir;
  133. static const char *tools_ext;
  134. static const char *exe_ext;
  135. static const char *dll_ext;
  136. static const char *man_ext;
  137. static const char *host_cpu;
  138. static const char *pe_dir;
  139. static const char *so_dir;
  140. static const char *crosstarget;
  141. static const char *crossdebug;
  142. static const char *fontforge;
  143. static const char *convert;
  144. static const char *flex;
  145. static const char *bison;
  146. static const char *ar;
  147. static const char *ranlib;
  148. static const char *rsvg;
  149. static const char *icotool;
  150. static const char *dlltool;
  151. static const char *msgfmt;
  152. static const char *ln_s;
  153. static const char *sed_cmd;
  154. static const char *delay_load_flag;
  155. struct makefile
  156. {
  157. /* values determined from input makefile */
  158. struct strarray vars;
  159. struct strarray include_paths;
  160. struct strarray include_args;
  161. struct strarray define_args;
  162. struct strarray programs;
  163. struct strarray scripts;
  164. struct strarray imports;
  165. struct strarray delayimports;
  166. struct strarray extradllflags;
  167. struct strarray install_lib;
  168. struct strarray install_dev;
  169. struct strarray extra_targets;
  170. struct strarray extra_imports;
  171. struct list sources;
  172. struct list includes;
  173. const char *src_dir;
  174. const char *obj_dir;
  175. const char *parent_dir;
  176. const char *module;
  177. const char *testdll;
  178. const char *extlib;
  179. const char *sharedlib;
  180. const char *staticlib;
  181. const char *staticimplib;
  182. const char *importlib;
  183. const char *unixlib;
  184. int native_unix_lib;
  185. int disabled;
  186. int use_msvcrt;
  187. int is_cross;
  188. int is_win16;
  189. int is_exe;
  190. /* values generated at output time */
  191. struct strarray in_files;
  192. struct strarray ok_files;
  193. struct strarray pot_files;
  194. struct strarray test_files;
  195. struct strarray clean_files;
  196. struct strarray distclean_files;
  197. struct strarray uninstall_files;
  198. struct strarray object_files;
  199. struct strarray crossobj_files;
  200. struct strarray unixobj_files;
  201. struct strarray res_files;
  202. struct strarray font_files;
  203. struct strarray c2man_files;
  204. struct strarray debug_files;
  205. struct strarray dlldata_files;
  206. struct strarray implib_files;
  207. struct strarray crossimplib_files;
  208. struct strarray all_targets;
  209. struct strarray phony_targets;
  210. struct strarray dependencies;
  211. struct strarray install_rules[NB_INSTALL_RULES];
  212. };
  213. static struct makefile *top_makefile;
  214. static struct makefile **submakes;
  215. static const char separator[] = "### Dependencies";
  216. static const char *output_makefile_name = "Makefile";
  217. static const char *input_file_name;
  218. static const char *output_file_name;
  219. static const char *temp_file_name;
  220. static int relative_dir_mode;
  221. static int silent_rules;
  222. static int input_line;
  223. static int output_column;
  224. static FILE *output_file;
  225. static const char Usage[] =
  226. "Usage: makedep [options] [directories]\n"
  227. "Options:\n"
  228. " -R from to Compute the relative path between two directories\n"
  229. " -S Generate Automake-style silent rules\n"
  230. " -fxxx Store output in file 'xxx' (default: Makefile)\n";
  231. static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  232. static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  233. static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  234. static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  235. /*******************************************************************
  236. * fatal_error
  237. */
  238. static void fatal_error( const char *msg, ... )
  239. {
  240. va_list valist;
  241. va_start( valist, msg );
  242. if (input_file_name)
  243. {
  244. fprintf( stderr, "%s:", input_file_name );
  245. if (input_line) fprintf( stderr, "%d:", input_line );
  246. fprintf( stderr, " error: " );
  247. }
  248. else fprintf( stderr, "makedep: error: " );
  249. vfprintf( stderr, msg, valist );
  250. va_end( valist );
  251. exit(1);
  252. }
  253. /*******************************************************************
  254. * fatal_perror
  255. */
  256. static void fatal_perror( const char *msg, ... )
  257. {
  258. va_list valist;
  259. va_start( valist, msg );
  260. if (input_file_name)
  261. {
  262. fprintf( stderr, "%s:", input_file_name );
  263. if (input_line) fprintf( stderr, "%d:", input_line );
  264. fprintf( stderr, " error: " );
  265. }
  266. else fprintf( stderr, "makedep: error: " );
  267. vfprintf( stderr, msg, valist );
  268. perror( " " );
  269. va_end( valist );
  270. exit(1);
  271. }
  272. /*******************************************************************
  273. * cleanup_files
  274. */
  275. static void cleanup_files(void)
  276. {
  277. if (temp_file_name) unlink( temp_file_name );
  278. if (output_file_name) unlink( output_file_name );
  279. }
  280. /*******************************************************************
  281. * exit_on_signal
  282. */
  283. static void exit_on_signal( int sig )
  284. {
  285. exit( 1 ); /* this will call the atexit functions */
  286. }
  287. /*******************************************************************
  288. * output
  289. */
  290. static void output( const char *format, ... )
  291. {
  292. int ret;
  293. va_list valist;
  294. va_start( valist, format );
  295. ret = vfprintf( output_file, format, valist );
  296. va_end( valist );
  297. if (ret < 0) fatal_perror( "output" );
  298. if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
  299. else output_column += ret;
  300. }
  301. /*******************************************************************
  302. * strarray_get_value
  303. *
  304. * Find a value in a name/value pair string array.
  305. */
  306. static const char *strarray_get_value( const struct strarray *array, const char *name )
  307. {
  308. int pos, res, min = 0, max = array->count / 2 - 1;
  309. while (min <= max)
  310. {
  311. pos = (min + max) / 2;
  312. if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
  313. if (res < 0) min = pos + 1;
  314. else max = pos - 1;
  315. }
  316. return NULL;
  317. }
  318. /*******************************************************************
  319. * strarray_set_value
  320. *
  321. * Define a value in a name/value pair string array.
  322. */
  323. static void strarray_set_value( struct strarray *array, const char *name, const char *value )
  324. {
  325. int i, pos, res, min = 0, max = array->count / 2 - 1;
  326. while (min <= max)
  327. {
  328. pos = (min + max) / 2;
  329. if (!(res = strcmp( array->str[pos * 2], name )))
  330. {
  331. /* redefining a variable replaces the previous value */
  332. array->str[pos * 2 + 1] = value;
  333. return;
  334. }
  335. if (res < 0) min = pos + 1;
  336. else max = pos - 1;
  337. }
  338. strarray_add( array, NULL );
  339. strarray_add( array, NULL );
  340. for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
  341. array->str[min * 2] = name;
  342. array->str[min * 2 + 1] = value;
  343. }
  344. /*******************************************************************
  345. * normalize_arch
  346. */
  347. static const char *normalize_arch( const char *arch )
  348. {
  349. unsigned int i, j;
  350. static const char *map[][8] =
  351. {
  352. /* normalized aliases */
  353. { "i386", "i486", "i586", "i686", "ia32" },
  354. { "x86_64", "amd64", "x86-64", "x86_amd64", "x64" },
  355. { "aarch64", "arm64" },
  356. { "arm" },
  357. };
  358. for (i = 0; i < sizeof(map) / sizeof(map[0]); i++)
  359. for (j = 0; map[i][j]; j++)
  360. if (!strncmp( arch, map[i][j], strlen(map[i][j]) ))
  361. return map[i][0];
  362. return NULL;
  363. }
  364. /*******************************************************************
  365. * output_filename
  366. */
  367. static void output_filename( const char *name )
  368. {
  369. if (output_column + strlen(name) + 1 > 100)
  370. {
  371. output( " \\\n" );
  372. output( " " );
  373. }
  374. else if (output_column) output( " " );
  375. output( "%s", name );
  376. }
  377. /*******************************************************************
  378. * output_filenames
  379. */
  380. static void output_filenames( struct strarray array )
  381. {
  382. unsigned int i;
  383. for (i = 0; i < array.count; i++) output_filename( array.str[i] );
  384. }
  385. /*******************************************************************
  386. * output_rm_filenames
  387. */
  388. static void output_rm_filenames( struct strarray array )
  389. {
  390. static const unsigned int max_cmdline = 30000; /* to be on the safe side */
  391. unsigned int i, len;
  392. if (!array.count) return;
  393. output( "\trm -f" );
  394. for (i = len = 0; i < array.count; i++)
  395. {
  396. if (len > max_cmdline)
  397. {
  398. output( "\n" );
  399. output( "\trm -f" );
  400. len = 0;
  401. }
  402. output_filename( array.str[i] );
  403. len += strlen( array.str[i] ) + 1;
  404. }
  405. output( "\n" );
  406. }
  407. /*******************************************************************
  408. * get_extension
  409. */
  410. static char *get_extension( char *filename )
  411. {
  412. char *ext = strrchr( filename, '.' );
  413. if (ext && strchr( ext, '/' )) ext = NULL;
  414. return ext;
  415. }
  416. /*******************************************************************
  417. * get_base_name
  418. */
  419. static const char *get_base_name( const char *name )
  420. {
  421. char *base;
  422. if (!strchr( name, '.' )) return name;
  423. base = xstrdup( name );
  424. *strrchr( base, '.' ) = 0;
  425. return base;
  426. }
  427. /*******************************************************************
  428. * replace_filename
  429. */
  430. static char *replace_filename( const char *path, const char *name )
  431. {
  432. const char *p;
  433. char *ret;
  434. size_t len;
  435. if (!path) return xstrdup( name );
  436. if (!(p = strrchr( path, '/' ))) return xstrdup( name );
  437. len = p - path + 1;
  438. ret = xmalloc( len + strlen( name ) + 1 );
  439. memcpy( ret, path, len );
  440. strcpy( ret + len, name );
  441. return ret;
  442. }
  443. /*******************************************************************
  444. * replace_substr
  445. */
  446. static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
  447. {
  448. size_t pos = start - str;
  449. char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
  450. memcpy( ret, str, pos );
  451. strcpy( ret + pos, replace );
  452. strcat( ret + pos, start + len );
  453. return ret;
  454. }
  455. /*******************************************************************
  456. * get_relative_path
  457. *
  458. * Determine where the destination path is located relative to the 'from' path.
  459. */
  460. static char *get_relative_path( const char *from, const char *dest )
  461. {
  462. const char *start;
  463. char *ret, *p;
  464. unsigned int dotdots = 0;
  465. /* a path of "." is equivalent to an empty path */
  466. if (!strcmp( from, "." )) from = "";
  467. for (;;)
  468. {
  469. while (*from == '/') from++;
  470. while (*dest == '/') dest++;
  471. start = dest; /* save start of next path element */
  472. if (!*from) break;
  473. while (*from && *from != '/' && *from == *dest) { from++; dest++; }
  474. if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
  475. /* count remaining elements in 'from' */
  476. do
  477. {
  478. dotdots++;
  479. while (*from && *from != '/') from++;
  480. while (*from == '/') from++;
  481. }
  482. while (*from);
  483. break;
  484. }
  485. if (!start[0] && !dotdots) return NULL; /* empty path */
  486. ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
  487. for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
  488. if (start[0]) strcpy( p, start );
  489. else p[-1] = 0; /* remove trailing slash */
  490. return ret;
  491. }
  492. /*******************************************************************
  493. * concat_paths
  494. */
  495. static char *concat_paths( const char *base, const char *path )
  496. {
  497. int i, len;
  498. char *ret;
  499. if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
  500. if (!path || !path[0]) return xstrdup( base );
  501. if (path[0] == '/') return xstrdup( path );
  502. len = strlen( base );
  503. while (len && base[len - 1] == '/') len--;
  504. while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
  505. {
  506. for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
  507. if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
  508. if (i != len - 1 || base[i] != '.')
  509. {
  510. path += 2;
  511. while (*path == '/') path++;
  512. }
  513. /* else ignore "." element */
  514. while (i > 0 && base[i - 1] == '/') i--;
  515. len = i;
  516. }
  517. if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
  518. ret = xmalloc( len + strlen( path ) + 2 );
  519. memcpy( ret, base, len );
  520. ret[len++] = '/';
  521. strcpy( ret + len, path );
  522. return ret;
  523. }
  524. /*******************************************************************
  525. * obj_dir_path
  526. */
  527. static char *obj_dir_path( const struct makefile *make, const char *path )
  528. {
  529. return concat_paths( make->obj_dir, path );
  530. }
  531. /*******************************************************************
  532. * src_dir_path
  533. */
  534. static char *src_dir_path( const struct makefile *make, const char *path )
  535. {
  536. if (make->src_dir) return concat_paths( make->src_dir, path );
  537. return obj_dir_path( make, path );
  538. }
  539. /*******************************************************************
  540. * root_src_dir_path
  541. */
  542. static char *root_src_dir_path( const char *path )
  543. {
  544. return concat_paths( root_src_dir, path );
  545. }
  546. /*******************************************************************
  547. * tools_dir_path
  548. */
  549. static char *tools_dir_path( const struct makefile *make, const char *path )
  550. {
  551. if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
  552. return strmake( "tools/%s", path );
  553. }
  554. /*******************************************************************
  555. * tools_path
  556. */
  557. static char *tools_path( const struct makefile *make, const char *name )
  558. {
  559. return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
  560. }
  561. /*******************************************************************
  562. * strarray_addall_path
  563. */
  564. static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
  565. {
  566. unsigned int i;
  567. for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
  568. }
  569. /*******************************************************************
  570. * get_line
  571. */
  572. static char *get_line( FILE *file )
  573. {
  574. static char *buffer;
  575. static size_t size;
  576. if (!size)
  577. {
  578. size = 1024;
  579. buffer = xmalloc( size );
  580. }
  581. if (!fgets( buffer, size, file )) return NULL;
  582. input_line++;
  583. for (;;)
  584. {
  585. char *p = buffer + strlen(buffer);
  586. /* if line is larger than buffer, resize buffer */
  587. while (p == buffer + size - 1 && p[-1] != '\n')
  588. {
  589. buffer = xrealloc( buffer, size * 2 );
  590. if (!fgets( buffer + size - 1, size + 1, file )) break;
  591. p = buffer + strlen(buffer);
  592. size *= 2;
  593. }
  594. if (p > buffer && p[-1] == '\n')
  595. {
  596. *(--p) = 0;
  597. if (p > buffer && p[-1] == '\r') *(--p) = 0;
  598. if (p > buffer && p[-1] == '\\')
  599. {
  600. *(--p) = 0;
  601. /* line ends in backslash, read continuation line */
  602. if (!fgets( p, size - (p - buffer), file )) return buffer;
  603. input_line++;
  604. continue;
  605. }
  606. }
  607. return buffer;
  608. }
  609. }
  610. /*******************************************************************
  611. * hash_filename
  612. */
  613. static unsigned int hash_filename( const char *name )
  614. {
  615. /* FNV-1 hash */
  616. unsigned int ret = 2166136261u;
  617. while (*name) ret = (ret * 16777619) ^ *name++;
  618. return ret % HASH_SIZE;
  619. }
  620. /*******************************************************************
  621. * add_file
  622. */
  623. static struct file *add_file( const char *name )
  624. {
  625. struct file *file = xmalloc( sizeof(*file) );
  626. memset( file, 0, sizeof(*file) );
  627. file->name = xstrdup( name );
  628. return file;
  629. }
  630. /*******************************************************************
  631. * add_dependency
  632. */
  633. static void add_dependency( struct file *file, const char *name, enum incl_type type )
  634. {
  635. if (file->deps_count >= file->deps_size)
  636. {
  637. file->deps_size *= 2;
  638. if (file->deps_size < 16) file->deps_size = 16;
  639. file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
  640. }
  641. file->deps[file->deps_count].line = input_line;
  642. file->deps[file->deps_count].type = type;
  643. file->deps[file->deps_count].name = xstrdup( name );
  644. file->deps_count++;
  645. }
  646. /*******************************************************************
  647. * find_src_file
  648. */
  649. static struct incl_file *find_src_file( const struct makefile *make, const char *name )
  650. {
  651. struct incl_file *file;
  652. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
  653. if (!strcmp( name, file->name )) return file;
  654. return NULL;
  655. }
  656. /*******************************************************************
  657. * find_include_file
  658. */
  659. static struct incl_file *find_include_file( const struct makefile *make, const char *name )
  660. {
  661. struct incl_file *file;
  662. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
  663. if (!strcmp( name, file->name )) return file;
  664. return NULL;
  665. }
  666. /*******************************************************************
  667. * add_include
  668. *
  669. * Add an include file if it doesn't already exists.
  670. */
  671. static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
  672. const char *name, int line, enum incl_type type )
  673. {
  674. struct incl_file *include;
  675. if (parent->files_count >= parent->files_size)
  676. {
  677. parent->files_size *= 2;
  678. if (parent->files_size < 16) parent->files_size = 16;
  679. parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
  680. }
  681. LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
  682. if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
  683. goto found;
  684. include = xmalloc( sizeof(*include) );
  685. memset( include, 0, sizeof(*include) );
  686. include->name = xstrdup(name);
  687. include->included_by = parent;
  688. include->included_line = line;
  689. include->type = type;
  690. include->use_msvcrt = parent->use_msvcrt;
  691. list_add_tail( &make->includes, &include->entry );
  692. found:
  693. parent->files[parent->files_count++] = include;
  694. return include;
  695. }
  696. /*******************************************************************
  697. * add_generated_source
  698. *
  699. * Add a generated source file to the list.
  700. */
  701. static struct incl_file *add_generated_source( struct makefile *make,
  702. const char *name, const char *filename )
  703. {
  704. struct incl_file *file;
  705. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  706. file = xmalloc( sizeof(*file) );
  707. memset( file, 0, sizeof(*file) );
  708. file->file = add_file( name );
  709. file->name = xstrdup( name );
  710. file->basename = xstrdup( filename ? filename : name );
  711. file->filename = obj_dir_path( make, file->basename );
  712. file->file->flags = FLAG_GENERATED;
  713. file->use_msvcrt = make->use_msvcrt;
  714. list_add_tail( &make->sources, &file->entry );
  715. return file;
  716. }
  717. /*******************************************************************
  718. * parse_include_directive
  719. */
  720. static void parse_include_directive( struct file *source, char *str )
  721. {
  722. char quote, *include, *p = str;
  723. while (*p && isspace(*p)) p++;
  724. if (*p != '\"' && *p != '<' ) return;
  725. quote = *p++;
  726. if (quote == '<') quote = '>';
  727. include = p;
  728. while (*p && (*p != quote)) p++;
  729. if (!*p) fatal_error( "malformed include directive '%s'\n", str );
  730. *p = 0;
  731. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  732. }
  733. /*******************************************************************
  734. * parse_pragma_directive
  735. */
  736. static void parse_pragma_directive( struct file *source, char *str )
  737. {
  738. char *flag, *p = str;
  739. if (!isspace( *p )) return;
  740. while (*p && isspace(*p)) p++;
  741. p = strtok( p, " \t" );
  742. if (strcmp( p, "makedep" )) return;
  743. while ((flag = strtok( NULL, " \t" )))
  744. {
  745. if (!strcmp( flag, "depend" ))
  746. {
  747. while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
  748. return;
  749. }
  750. else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
  751. if (strendswith( source->name, ".idl" ))
  752. {
  753. if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
  754. else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
  755. else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
  756. else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
  757. else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
  758. else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
  759. else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
  760. else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
  761. }
  762. else if (strendswith( source->name, ".rc" ))
  763. {
  764. if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
  765. }
  766. else if (strendswith( source->name, ".sfd" ))
  767. {
  768. if (!strcmp( flag, "font" ))
  769. {
  770. struct strarray *array = source->args;
  771. if (!array)
  772. {
  773. source->args = array = xmalloc( sizeof(*array) );
  774. *array = empty_strarray;
  775. source->flags |= FLAG_SFD_FONTS;
  776. }
  777. strarray_add( array, xstrdup( strtok( NULL, "" )));
  778. return;
  779. }
  780. }
  781. else
  782. {
  783. if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
  784. if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
  785. }
  786. }
  787. }
  788. /*******************************************************************
  789. * parse_cpp_directive
  790. */
  791. static void parse_cpp_directive( struct file *source, char *str )
  792. {
  793. while (*str && isspace(*str)) str++;
  794. if (*str++ != '#') return;
  795. while (*str && isspace(*str)) str++;
  796. if (!strncmp( str, "include", 7 ))
  797. parse_include_directive( source, str + 7 );
  798. else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
  799. parse_include_directive( source, str + 6 );
  800. else if (!strncmp( str, "pragma", 6 ))
  801. parse_pragma_directive( source, str + 6 );
  802. }
  803. /*******************************************************************
  804. * parse_idl_file
  805. */
  806. static void parse_idl_file( struct file *source, FILE *file )
  807. {
  808. char *buffer, *include;
  809. input_line = 0;
  810. while ((buffer = get_line( file )))
  811. {
  812. char quote;
  813. char *p = buffer;
  814. while (*p && isspace(*p)) p++;
  815. if (!strncmp( p, "importlib", 9 ))
  816. {
  817. p += 9;
  818. while (*p && isspace(*p)) p++;
  819. if (*p++ != '(') continue;
  820. while (*p && isspace(*p)) p++;
  821. if (*p++ != '"') continue;
  822. include = p;
  823. while (*p && (*p != '"')) p++;
  824. if (!*p) fatal_error( "malformed importlib directive\n" );
  825. *p = 0;
  826. add_dependency( source, include, INCL_IMPORTLIB );
  827. continue;
  828. }
  829. if (!strncmp( p, "import", 6 ))
  830. {
  831. p += 6;
  832. while (*p && isspace(*p)) p++;
  833. if (*p != '"') continue;
  834. include = ++p;
  835. while (*p && (*p != '"')) p++;
  836. if (!*p) fatal_error( "malformed import directive\n" );
  837. *p = 0;
  838. add_dependency( source, include, INCL_IMPORT );
  839. continue;
  840. }
  841. /* check for #include inside cpp_quote */
  842. if (!strncmp( p, "cpp_quote", 9 ))
  843. {
  844. p += 9;
  845. while (*p && isspace(*p)) p++;
  846. if (*p++ != '(') continue;
  847. while (*p && isspace(*p)) p++;
  848. if (*p++ != '"') continue;
  849. if (*p++ != '#') continue;
  850. while (*p && isspace(*p)) p++;
  851. if (strncmp( p, "include", 7 )) continue;
  852. p += 7;
  853. while (*p && isspace(*p)) p++;
  854. if (*p == '\\' && p[1] == '"')
  855. {
  856. p += 2;
  857. quote = '"';
  858. }
  859. else
  860. {
  861. if (*p++ != '<' ) continue;
  862. quote = '>';
  863. }
  864. include = p;
  865. while (*p && (*p != quote)) p++;
  866. if (!*p || (quote == '"' && p[-1] != '\\'))
  867. fatal_error( "malformed #include directive inside cpp_quote\n" );
  868. if (quote == '"') p--; /* remove backslash */
  869. *p = 0;
  870. add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
  871. continue;
  872. }
  873. parse_cpp_directive( source, p );
  874. }
  875. }
  876. /*******************************************************************
  877. * parse_c_file
  878. */
  879. static void parse_c_file( struct file *source, FILE *file )
  880. {
  881. char *buffer;
  882. input_line = 0;
  883. while ((buffer = get_line( file )))
  884. {
  885. parse_cpp_directive( source, buffer );
  886. }
  887. }
  888. /*******************************************************************
  889. * parse_rc_file
  890. */
  891. static void parse_rc_file( struct file *source, FILE *file )
  892. {
  893. char *buffer, *include;
  894. input_line = 0;
  895. while ((buffer = get_line( file )))
  896. {
  897. char quote;
  898. char *p = buffer;
  899. while (*p && isspace(*p)) p++;
  900. if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
  901. {
  902. p += 2;
  903. while (*p && isspace(*p)) p++;
  904. if (strncmp( p, "@makedep:", 9 )) continue;
  905. p += 9;
  906. while (*p && isspace(*p)) p++;
  907. quote = '"';
  908. if (*p == quote)
  909. {
  910. include = ++p;
  911. while (*p && *p != quote) p++;
  912. }
  913. else
  914. {
  915. include = p;
  916. while (*p && !isspace(*p) && *p != '*') p++;
  917. }
  918. if (!*p)
  919. fatal_error( "malformed makedep comment\n" );
  920. *p = 0;
  921. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  922. continue;
  923. }
  924. parse_cpp_directive( source, buffer );
  925. }
  926. }
  927. /*******************************************************************
  928. * parse_in_file
  929. */
  930. static void parse_in_file( struct file *source, FILE *file )
  931. {
  932. char *p, *buffer;
  933. /* make sure it gets rebuilt when the version changes */
  934. add_dependency( source, "config.h", INCL_SYSTEM );
  935. if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
  936. input_line = 0;
  937. while ((buffer = get_line( file )))
  938. {
  939. if (strncmp( buffer, ".TH", 3 )) continue;
  940. if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
  941. if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
  942. if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
  943. source->args = xstrdup( p );
  944. return;
  945. }
  946. }
  947. /*******************************************************************
  948. * parse_sfd_file
  949. */
  950. static void parse_sfd_file( struct file *source, FILE *file )
  951. {
  952. char *p, *eol, *buffer;
  953. input_line = 0;
  954. while ((buffer = get_line( file )))
  955. {
  956. if (strncmp( buffer, "UComments:", 10 )) continue;
  957. p = buffer + 10;
  958. while (*p == ' ') p++;
  959. if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
  960. {
  961. p++;
  962. buffer[strlen(buffer) - 1] = 0;
  963. }
  964. while ((eol = strstr( p, "+AAoA" )))
  965. {
  966. *eol = 0;
  967. while (*p && isspace(*p)) p++;
  968. if (*p++ == '#')
  969. {
  970. while (*p && isspace(*p)) p++;
  971. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  972. }
  973. p = eol + 5;
  974. }
  975. while (*p && isspace(*p)) p++;
  976. if (*p++ != '#') return;
  977. while (*p && isspace(*p)) p++;
  978. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  979. return;
  980. }
  981. }
  982. static const struct
  983. {
  984. const char *ext;
  985. void (*parse)( struct file *file, FILE *f );
  986. } parse_functions[] =
  987. {
  988. { ".c", parse_c_file },
  989. { ".h", parse_c_file },
  990. { ".inl", parse_c_file },
  991. { ".l", parse_c_file },
  992. { ".m", parse_c_file },
  993. { ".rh", parse_c_file },
  994. { ".x", parse_c_file },
  995. { ".y", parse_c_file },
  996. { ".idl", parse_idl_file },
  997. { ".rc", parse_rc_file },
  998. { ".in", parse_in_file },
  999. { ".sfd", parse_sfd_file }
  1000. };
  1001. /*******************************************************************
  1002. * load_file
  1003. */
  1004. static struct file *load_file( const char *name )
  1005. {
  1006. struct file *file;
  1007. FILE *f;
  1008. unsigned int i, hash = hash_filename( name );
  1009. LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
  1010. if (!strcmp( name, file->name )) return file;
  1011. if (!(f = fopen( name, "r" ))) return NULL;
  1012. file = add_file( name );
  1013. list_add_tail( &files[hash], &file->entry );
  1014. input_file_name = file->name;
  1015. input_line = 0;
  1016. for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
  1017. {
  1018. if (!strendswith( name, parse_functions[i].ext )) continue;
  1019. parse_functions[i].parse( file, f );
  1020. break;
  1021. }
  1022. fclose( f );
  1023. input_file_name = NULL;
  1024. return file;
  1025. }
  1026. /*******************************************************************
  1027. * open_include_path_file
  1028. *
  1029. * Open a file from a directory on the include path.
  1030. */
  1031. static struct file *open_include_path_file( const struct makefile *make, const char *dir,
  1032. const char *name, char **filename )
  1033. {
  1034. char *src_path = concat_paths( dir, name );
  1035. struct file *ret = load_file( src_path );
  1036. if (ret) *filename = src_path;
  1037. return ret;
  1038. }
  1039. /*******************************************************************
  1040. * open_file_same_dir
  1041. *
  1042. * Open a file in the same directory as the parent.
  1043. */
  1044. static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
  1045. {
  1046. char *src_path = replace_filename( parent->file->name, name );
  1047. struct file *ret = load_file( src_path );
  1048. if (ret) *filename = replace_filename( parent->filename, name );
  1049. return ret;
  1050. }
  1051. /*******************************************************************
  1052. * open_local_file
  1053. *
  1054. * Open a file in the source directory of the makefile.
  1055. */
  1056. static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
  1057. {
  1058. char *src_path = src_dir_path( make, path );
  1059. struct file *ret = load_file( src_path );
  1060. /* if not found, try parent dir */
  1061. if (!ret && make->parent_dir)
  1062. {
  1063. free( src_path );
  1064. path = strmake( "%s/%s", make->parent_dir, path );
  1065. src_path = src_dir_path( make, path );
  1066. ret = load_file( src_path );
  1067. }
  1068. if (ret) *filename = src_path;
  1069. return ret;
  1070. }
  1071. /*******************************************************************
  1072. * open_global_file
  1073. *
  1074. * Open a file in the top-level source directory.
  1075. */
  1076. static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
  1077. {
  1078. char *src_path = root_src_dir_path( path );
  1079. struct file *ret = load_file( src_path );
  1080. if (ret) *filename = src_path;
  1081. return ret;
  1082. }
  1083. /*******************************************************************
  1084. * open_global_header
  1085. *
  1086. * Open a file in the global include source directory.
  1087. */
  1088. static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
  1089. {
  1090. if (!strncmp( path, "../", 3 )) return NULL;
  1091. return open_global_file( make, strmake( "include/%s", path ), filename );
  1092. }
  1093. /*******************************************************************
  1094. * open_src_file
  1095. */
  1096. static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
  1097. {
  1098. struct file *file = open_local_file( make, pFile->name, &pFile->filename );
  1099. if (!file) fatal_perror( "open %s", pFile->name );
  1100. return file;
  1101. }
  1102. /*******************************************************************
  1103. * find_importlib_module
  1104. */
  1105. static struct makefile *find_importlib_module( const char *name )
  1106. {
  1107. unsigned int i, len;
  1108. for (i = 0; i < subdirs.count; i++)
  1109. {
  1110. if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
  1111. len = strlen(submakes[i]->obj_dir);
  1112. if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
  1113. if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
  1114. }
  1115. return NULL;
  1116. }
  1117. /*******************************************************************
  1118. * has_external_import
  1119. */
  1120. static int has_external_import( const struct makefile *make )
  1121. {
  1122. unsigned int i;
  1123. for (i = 0; i < make->imports.count; i++)
  1124. {
  1125. if (!strncmp( make->imports.str[i], "-l", 2 ))
  1126. return 1;
  1127. }
  1128. return 0;
  1129. }
  1130. /*******************************************************************
  1131. * open_include_file
  1132. */
  1133. static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
  1134. {
  1135. struct file *file = NULL;
  1136. char *filename;
  1137. unsigned int i, len;
  1138. errno = ENOENT;
  1139. /* check for generated bison header */
  1140. if (strendswith( pFile->name, ".tab.h" ) &&
  1141. (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
  1142. {
  1143. pFile->sourcename = filename;
  1144. pFile->filename = obj_dir_path( make, pFile->name );
  1145. return file;
  1146. }
  1147. /* check for corresponding idl file in source dir */
  1148. if (strendswith( pFile->name, ".h" ) &&
  1149. (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1150. {
  1151. pFile->sourcename = filename;
  1152. pFile->filename = obj_dir_path( make, pFile->name );
  1153. return file;
  1154. }
  1155. /* check for extra targets */
  1156. if (strarray_exists( &make->extra_targets, pFile->name ))
  1157. {
  1158. pFile->sourcename = src_dir_path( make, pFile->name );
  1159. pFile->filename = obj_dir_path( make, pFile->name );
  1160. return NULL;
  1161. }
  1162. /* now try in source dir */
  1163. if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
  1164. /* check for global importlib (module dependency) */
  1165. if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
  1166. {
  1167. pFile->filename = pFile->name;
  1168. return NULL;
  1169. }
  1170. /* check for corresponding idl file in global includes */
  1171. if (strendswith( pFile->name, ".h" ) &&
  1172. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1173. {
  1174. pFile->sourcename = filename;
  1175. pFile->filename = strmake( "include/%s", pFile->name );
  1176. return file;
  1177. }
  1178. /* check for corresponding .in file in global includes (for config.h.in) */
  1179. if (strendswith( pFile->name, ".h" ) &&
  1180. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
  1181. {
  1182. pFile->sourcename = filename;
  1183. pFile->filename = strmake( "include/%s", pFile->name );
  1184. return file;
  1185. }
  1186. /* check for corresponding .x file in global includes */
  1187. if (strendswith( pFile->name, "tmpl.h" ) &&
  1188. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
  1189. {
  1190. pFile->sourcename = filename;
  1191. pFile->filename = strmake( "include/%s", pFile->name );
  1192. return file;
  1193. }
  1194. /* check in global includes source dir */
  1195. if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
  1196. /* check in global msvcrt includes */
  1197. if (pFile->use_msvcrt &&
  1198. (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
  1199. return file;
  1200. /* now search in include paths */
  1201. for (i = 0; i < make->include_paths.count; i++)
  1202. {
  1203. const char *dir = make->include_paths.str[i];
  1204. if (root_src_dir)
  1205. {
  1206. len = strlen( root_src_dir );
  1207. if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
  1208. {
  1209. while (dir[len] == '/') len++;
  1210. file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
  1211. }
  1212. }
  1213. else
  1214. {
  1215. if (*dir == '/') continue;
  1216. file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
  1217. }
  1218. if (!file) continue;
  1219. pFile->is_external = 1;
  1220. return file;
  1221. }
  1222. if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt &&
  1223. !make->extlib && !pFile->included_by->is_external)
  1224. {
  1225. if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
  1226. if (!strcmp( pFile->name, "x86intrin.h" )) return NULL;
  1227. if (has_external_import( make )) return NULL;
  1228. fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
  1229. pFile->included_by->file->name, pFile->included_line, pFile->name );
  1230. exit(1);
  1231. }
  1232. if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
  1233. /* try in src file directory */
  1234. if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
  1235. {
  1236. pFile->is_external = pFile->included_by->is_external;
  1237. return file;
  1238. }
  1239. if (make->extlib) return NULL; /* ignore missing files in external libs */
  1240. fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
  1241. perror( pFile->name );
  1242. pFile = pFile->included_by;
  1243. while (pFile && pFile->included_by)
  1244. {
  1245. const char *parent = pFile->included_by->sourcename;
  1246. if (!parent) parent = pFile->included_by->file->name;
  1247. fprintf( stderr, "%s:%d: note: %s was first included here\n",
  1248. parent, pFile->included_line, pFile->name );
  1249. pFile = pFile->included_by;
  1250. }
  1251. exit(1);
  1252. }
  1253. /*******************************************************************
  1254. * add_all_includes
  1255. */
  1256. static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
  1257. {
  1258. unsigned int i;
  1259. for (i = 0; i < file->deps_count; i++)
  1260. {
  1261. switch (file->deps[i].type)
  1262. {
  1263. case INCL_NORMAL:
  1264. case INCL_IMPORT:
  1265. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1266. break;
  1267. case INCL_IMPORTLIB:
  1268. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
  1269. break;
  1270. case INCL_SYSTEM:
  1271. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1272. break;
  1273. case INCL_CPP_QUOTE:
  1274. case INCL_CPP_QUOTE_SYSTEM:
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. /*******************************************************************
  1280. * parse_file
  1281. */
  1282. static void parse_file( struct makefile *make, struct incl_file *source, int src )
  1283. {
  1284. struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
  1285. if (!file) return;
  1286. source->file = file;
  1287. source->files_count = 0;
  1288. source->files_size = file->deps_count;
  1289. source->files = xmalloc( source->files_size * sizeof(*source->files) );
  1290. if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
  1291. else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
  1292. if (source->sourcename)
  1293. {
  1294. if (strendswith( source->sourcename, ".idl" ))
  1295. {
  1296. unsigned int i;
  1297. /* generated .h file always includes these */
  1298. add_include( make, source, "rpc.h", 0, INCL_NORMAL );
  1299. add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
  1300. for (i = 0; i < file->deps_count; i++)
  1301. {
  1302. switch (file->deps[i].type)
  1303. {
  1304. case INCL_IMPORT:
  1305. if (strendswith( file->deps[i].name, ".idl" ))
  1306. add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
  1307. file->deps[i].line, INCL_NORMAL );
  1308. else
  1309. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1310. break;
  1311. case INCL_CPP_QUOTE:
  1312. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1313. break;
  1314. case INCL_CPP_QUOTE_SYSTEM:
  1315. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1316. break;
  1317. case INCL_NORMAL:
  1318. case INCL_SYSTEM:
  1319. case INCL_IMPORTLIB:
  1320. break;
  1321. }
  1322. }
  1323. return;
  1324. }
  1325. if (strendswith( source->sourcename, ".y" ))
  1326. return; /* generated .tab.h doesn't include anything */
  1327. }
  1328. add_all_includes( make, source, file );
  1329. }
  1330. /*******************************************************************
  1331. * add_src_file
  1332. *
  1333. * Add a source file to the list.
  1334. */
  1335. static struct incl_file *add_src_file( struct makefile *make, const char *name )
  1336. {
  1337. struct incl_file *file;
  1338. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  1339. file = xmalloc( sizeof(*file) );
  1340. memset( file, 0, sizeof(*file) );
  1341. file->name = xstrdup(name);
  1342. file->use_msvcrt = make->use_msvcrt;
  1343. file->is_external = !!make->extlib;
  1344. list_add_tail( &make->sources, &file->entry );
  1345. parse_file( make, file, 1 );
  1346. return file;
  1347. }
  1348. /*******************************************************************
  1349. * open_input_makefile
  1350. */
  1351. static FILE *open_input_makefile( const struct makefile *make )
  1352. {
  1353. FILE *ret;
  1354. if (make->obj_dir)
  1355. input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
  1356. else
  1357. input_file_name = output_makefile_name; /* always use output name for main Makefile */
  1358. input_line = 0;
  1359. if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
  1360. return ret;
  1361. }
  1362. /*******************************************************************
  1363. * get_make_variable
  1364. */
  1365. static const char *get_make_variable( const struct makefile *make, const char *name )
  1366. {
  1367. const char *ret;
  1368. if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
  1369. if ((ret = strarray_get_value( &make->vars, name ))) return ret;
  1370. if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
  1371. return NULL;
  1372. }
  1373. /*******************************************************************
  1374. * get_expanded_make_variable
  1375. */
  1376. static char *get_expanded_make_variable( const struct makefile *make, const char *name )
  1377. {
  1378. const char *var;
  1379. char *p, *end, *expand, *tmp;
  1380. var = get_make_variable( make, name );
  1381. if (!var) return NULL;
  1382. p = expand = xstrdup( var );
  1383. while ((p = strchr( p, '$' )))
  1384. {
  1385. if (p[1] == '(')
  1386. {
  1387. if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
  1388. *end++ = 0;
  1389. if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
  1390. var = get_make_variable( make, p + 2 );
  1391. tmp = replace_substr( expand, p, end - p, var ? var : "" );
  1392. /* switch to the new string */
  1393. p = tmp + (p - expand);
  1394. free( expand );
  1395. expand = tmp;
  1396. }
  1397. else if (p[1] == '{') /* don't expand ${} variables */
  1398. {
  1399. if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
  1400. p = end + 1;
  1401. }
  1402. else if (p[1] == '$')
  1403. {
  1404. p += 2;
  1405. }
  1406. else fatal_error( "syntax error in '%s'\n", expand );
  1407. }
  1408. /* consider empty variables undefined */
  1409. p = expand;
  1410. while (*p && isspace(*p)) p++;
  1411. if (*p) return expand;
  1412. free( expand );
  1413. return NULL;
  1414. }
  1415. /*******************************************************************
  1416. * get_expanded_make_var_array
  1417. */
  1418. static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
  1419. {
  1420. struct strarray ret = empty_strarray;
  1421. char *value, *token;
  1422. if ((value = get_expanded_make_variable( make, name )))
  1423. for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
  1424. strarray_add( &ret, token );
  1425. return ret;
  1426. }
  1427. /*******************************************************************
  1428. * get_expanded_file_local_var
  1429. */
  1430. static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
  1431. const char *name )
  1432. {
  1433. char *p, *var = strmake( "%s_%s", file, name );
  1434. for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
  1435. return get_expanded_make_var_array( make, var );
  1436. }
  1437. /*******************************************************************
  1438. * set_make_variable
  1439. */
  1440. static int set_make_variable( struct strarray *array, const char *assignment )
  1441. {
  1442. char *p, *name;
  1443. p = name = xstrdup( assignment );
  1444. while (isalnum(*p) || *p == '_') p++;
  1445. if (name == p) return 0; /* not a variable */
  1446. if (isspace(*p))
  1447. {
  1448. *p++ = 0;
  1449. while (isspace(*p)) p++;
  1450. }
  1451. if (*p != '=') return 0; /* not an assignment */
  1452. *p++ = 0;
  1453. while (isspace(*p)) p++;
  1454. strarray_set_value( array, name, p );
  1455. return 1;
  1456. }
  1457. /*******************************************************************
  1458. * parse_makefile
  1459. */
  1460. static struct makefile *parse_makefile( const char *path )
  1461. {
  1462. char *buffer;
  1463. FILE *file;
  1464. struct makefile *make = xmalloc( sizeof(*make) );
  1465. memset( make, 0, sizeof(*make) );
  1466. make->obj_dir = path;
  1467. if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
  1468. file = open_input_makefile( make );
  1469. while ((buffer = get_line( file )))
  1470. {
  1471. if (!strncmp( buffer, separator, strlen(separator) )) break;
  1472. if (*buffer == '\t') continue; /* command */
  1473. while (isspace( *buffer )) buffer++;
  1474. if (*buffer == '#') continue; /* comment */
  1475. set_make_variable( &make->vars, buffer );
  1476. }
  1477. fclose( file );
  1478. input_file_name = NULL;
  1479. return make;
  1480. }
  1481. /*******************************************************************
  1482. * add_generated_sources
  1483. */
  1484. static void add_generated_sources( struct makefile *make )
  1485. {
  1486. unsigned int i;
  1487. struct incl_file *source, *next, *file;
  1488. struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
  1489. LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
  1490. {
  1491. if (source->file->flags & FLAG_IDL_CLIENT)
  1492. {
  1493. file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
  1494. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1495. add_all_includes( make, file, file->file );
  1496. }
  1497. if (source->file->flags & FLAG_IDL_SERVER)
  1498. {
  1499. file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
  1500. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1501. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1502. add_all_includes( make, file, file->file );
  1503. }
  1504. if (source->file->flags & FLAG_IDL_IDENT)
  1505. {
  1506. file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
  1507. add_dependency( file->file, "rpc.h", INCL_NORMAL );
  1508. add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
  1509. add_dependency( file->file, "guiddef.h", INCL_NORMAL );
  1510. add_all_includes( make, file, file->file );
  1511. }
  1512. if (source->file->flags & FLAG_IDL_PROXY)
  1513. {
  1514. file = add_generated_source( make, "dlldata.o", "dlldata.c" );
  1515. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1516. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1517. add_all_includes( make, file, file->file );
  1518. file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
  1519. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1520. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1521. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1522. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1523. add_all_includes( make, file, file->file );
  1524. }
  1525. if (source->file->flags & FLAG_IDL_TYPELIB)
  1526. {
  1527. add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
  1528. }
  1529. if (source->file->flags & FLAG_IDL_REGTYPELIB)
  1530. {
  1531. add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
  1532. }
  1533. if (source->file->flags & FLAG_IDL_REGISTER)
  1534. {
  1535. add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
  1536. }
  1537. if (source->file->flags & FLAG_IDL_HEADER)
  1538. {
  1539. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1540. }
  1541. if (!source->file->flags && strendswith( source->name, ".idl" ))
  1542. {
  1543. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1544. }
  1545. if (strendswith( source->name, ".x" ))
  1546. {
  1547. add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
  1548. }
  1549. if (strendswith( source->name, ".y" ))
  1550. {
  1551. file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
  1552. /* steal the includes list from the source file */
  1553. file->files_count = source->files_count;
  1554. file->files_size = source->files_size;
  1555. file->files = source->files;
  1556. source->files_count = source->files_size = 0;
  1557. source->files = NULL;
  1558. }
  1559. if (strendswith( source->name, ".l" ))
  1560. {
  1561. file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
  1562. /* steal the includes list from the source file */
  1563. file->files_count = source->files_count;
  1564. file->files_size = source->files_size;
  1565. file->files = source->files;
  1566. source->files_count = source->files_size = 0;
  1567. source->files = NULL;
  1568. }
  1569. if (source->file->flags & FLAG_C_IMPLIB)
  1570. {
  1571. if (!make->staticimplib && make->importlib && *dll_ext)
  1572. make->staticimplib = strmake( "lib%s.a", make->importlib );
  1573. }
  1574. if (strendswith( source->name, ".po" ))
  1575. {
  1576. if (!make->disabled)
  1577. strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
  1578. }
  1579. if (strendswith( source->name, ".spec" ))
  1580. {
  1581. char *obj = replace_extension( source->name, ".spec", "" );
  1582. strarray_addall_uniq( &make->extra_imports,
  1583. get_expanded_file_local_var( make, obj, "IMPORTS" ));
  1584. }
  1585. }
  1586. if (make->testdll)
  1587. {
  1588. file = add_generated_source( make, "testlist.o", "testlist.c" );
  1589. add_dependency( file->file, "wine/test.h", INCL_NORMAL );
  1590. add_all_includes( make, file, file->file );
  1591. }
  1592. for (i = 0; i < objs.count; i++)
  1593. {
  1594. /* default to .c for unknown extra object files */
  1595. if (strendswith( objs.str[i], ".o" ))
  1596. {
  1597. file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
  1598. file->file->flags |= FLAG_C_UNIX;
  1599. file->use_msvcrt = 0;
  1600. }
  1601. else if (strendswith( objs.str[i], ".res" ))
  1602. add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
  1603. else
  1604. add_generated_source( make, objs.str[i], NULL );
  1605. }
  1606. }
  1607. /*******************************************************************
  1608. * create_dir
  1609. */
  1610. static void create_dir( const char *dir )
  1611. {
  1612. char *p, *path;
  1613. p = path = xstrdup( dir );
  1614. while ((p = strchr( p, '/' )))
  1615. {
  1616. *p = 0;
  1617. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1618. *p++ = '/';
  1619. while (*p == '/') p++;
  1620. }
  1621. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1622. free( path );
  1623. }
  1624. /*******************************************************************
  1625. * create_file_directories
  1626. *
  1627. * Create the base directories of all the files.
  1628. */
  1629. static void create_file_directories( const struct makefile *make, struct strarray files )
  1630. {
  1631. struct strarray subdirs = empty_strarray;
  1632. unsigned int i;
  1633. char *dir;
  1634. for (i = 0; i < files.count; i++)
  1635. {
  1636. if (!strchr( files.str[i], '/' )) continue;
  1637. dir = obj_dir_path( make, files.str[i] );
  1638. *strrchr( dir, '/' ) = 0;
  1639. strarray_add_uniq( &subdirs, dir );
  1640. }
  1641. for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
  1642. }
  1643. /*******************************************************************
  1644. * output_filenames_obj_dir
  1645. */
  1646. static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
  1647. {
  1648. unsigned int i;
  1649. for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
  1650. }
  1651. /*******************************************************************
  1652. * get_dependencies
  1653. */
  1654. static void get_dependencies( struct incl_file *file, struct incl_file *source )
  1655. {
  1656. unsigned int i;
  1657. if (!file->filename) return;
  1658. if (file != source)
  1659. {
  1660. if (file->owner == source) return; /* already processed */
  1661. if (file->type == INCL_IMPORTLIB)
  1662. {
  1663. if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
  1664. return; /* library is imported only when building a typelib */
  1665. strarray_add( &source->importlibdeps, file->filename );
  1666. }
  1667. else strarray_add( &source->dependencies, file->filename );
  1668. file->owner = source;
  1669. /* sanity checks */
  1670. if (!strcmp( file->filename, "include/config.h" ) &&
  1671. file != source->files[0] && !source->is_external)
  1672. {
  1673. input_file_name = source->filename;
  1674. input_line = 0;
  1675. for (i = 0; i < source->file->deps_count; i++)
  1676. {
  1677. if (!strcmp( source->file->deps[i].name, file->name ))
  1678. input_line = source->file->deps[i].line;
  1679. }
  1680. fatal_error( "%s must be included before other headers\n", file->name );
  1681. }
  1682. }
  1683. for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
  1684. }
  1685. /*******************************************************************
  1686. * get_local_dependencies
  1687. *
  1688. * Get the local dependencies of a given target.
  1689. */
  1690. static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
  1691. struct strarray targets )
  1692. {
  1693. unsigned int i;
  1694. struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
  1695. for (i = 0; i < deps.count; i++)
  1696. {
  1697. if (strarray_exists( &targets, deps.str[i] ))
  1698. deps.str[i] = obj_dir_path( make, deps.str[i] );
  1699. else
  1700. deps.str[i] = src_dir_path( make, deps.str[i] );
  1701. }
  1702. return deps;
  1703. }
  1704. /*******************************************************************
  1705. * get_static_lib
  1706. *
  1707. * Check if makefile builds the named static library and return the full lib path.
  1708. */
  1709. static const char *get_static_lib( const struct makefile *make, const char *name )
  1710. {
  1711. if (!make->staticlib) return NULL;
  1712. if (make->disabled) return NULL;
  1713. if (strncmp( make->staticlib, "lib", 3 )) return NULL;
  1714. if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
  1715. if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
  1716. return obj_dir_path( make, make->staticlib );
  1717. }
  1718. /*******************************************************************
  1719. * get_native_unix_lib
  1720. */
  1721. static const char *get_native_unix_lib( const struct makefile *make, const char *name )
  1722. {
  1723. if (!make->native_unix_lib) return NULL;
  1724. if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
  1725. if (make->unixlib[strlen(name)] != '.') return NULL;
  1726. return obj_dir_path( make, make->unixlib );
  1727. }
  1728. /*******************************************************************
  1729. * get_parent_makefile
  1730. */
  1731. static struct makefile *get_parent_makefile( struct makefile *make )
  1732. {
  1733. char *dir, *p;
  1734. int i;
  1735. if (!make->obj_dir) return NULL;
  1736. dir = xstrdup( make->obj_dir );
  1737. if (!(p = strrchr( dir, '/' ))) return NULL;
  1738. *p = 0;
  1739. for (i = 0; i < subdirs.count; i++)
  1740. if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
  1741. return NULL;
  1742. }
  1743. /*******************************************************************
  1744. * needs_delay_lib
  1745. */
  1746. static int needs_delay_lib( const struct makefile *make )
  1747. {
  1748. if (delay_load_flag) return 0;
  1749. if (*dll_ext && !crosstarget) return 0;
  1750. if (!make->importlib) return 0;
  1751. return strarray_exists( &delay_import_libs, make->importlib );
  1752. }
  1753. /*******************************************************************
  1754. * add_unix_libraries
  1755. */
  1756. static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
  1757. {
  1758. struct strarray ret = empty_strarray;
  1759. struct strarray all_libs = empty_strarray;
  1760. unsigned int i, j;
  1761. if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
  1762. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  1763. for (i = 0; i < all_libs.count; i++)
  1764. {
  1765. const char *lib = NULL;
  1766. if (!strncmp( all_libs.str[i], "-l", 2 ))
  1767. {
  1768. for (j = 0; j < subdirs.count; j++)
  1769. {
  1770. if (make == submakes[j]) continue;
  1771. if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
  1772. }
  1773. }
  1774. if (lib)
  1775. {
  1776. strarray_add( deps, lib );
  1777. strarray_add( &ret, lib );
  1778. }
  1779. else strarray_add( &ret, all_libs.str[i] );
  1780. }
  1781. strarray_addall( &ret, libs );
  1782. return ret;
  1783. }
  1784. /*******************************************************************
  1785. * is_crt_module
  1786. */
  1787. static int is_crt_module( const char *file )
  1788. {
  1789. return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
  1790. }
  1791. /*******************************************************************
  1792. * get_default_crt
  1793. */
  1794. static const char *get_default_crt( const struct makefile *make )
  1795. {
  1796. if (!make->use_msvcrt) return NULL;
  1797. if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
  1798. return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
  1799. }
  1800. /*******************************************************************
  1801. * get_crt_define
  1802. */
  1803. static const char *get_crt_define( const struct makefile *make )
  1804. {
  1805. const char *crt_dll = NULL;
  1806. unsigned int i, version = 0;
  1807. for (i = 0; i < make->imports.count; i++)
  1808. {
  1809. if (!is_crt_module( make->imports.str[i] )) continue;
  1810. if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
  1811. crt_dll, make->imports.str[i] );
  1812. crt_dll = make->imports.str[i];
  1813. }
  1814. if (!crt_dll)
  1815. {
  1816. if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
  1817. if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
  1818. }
  1819. if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
  1820. sscanf( crt_dll, "msvcr%u", &version );
  1821. return strmake( "-D_MSVCR_VER=%u", version );
  1822. }
  1823. /*******************************************************************
  1824. * add_default_imports
  1825. */
  1826. static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
  1827. {
  1828. struct strarray ret = empty_strarray;
  1829. const char *crt_dll = get_default_crt( make );
  1830. unsigned int i;
  1831. for (i = 0; i < imports.count; i++)
  1832. {
  1833. if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
  1834. else strarray_add( &ret, imports.str[i] );
  1835. }
  1836. strarray_add( &ret, "winecrt0" );
  1837. if (crt_dll) strarray_add( &ret, crt_dll );
  1838. if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
  1839. strarray_add( &ret, "kernel" );
  1840. strarray_add( &ret, "kernel32" );
  1841. strarray_add( &ret, "ntdll" );
  1842. return ret;
  1843. }
  1844. /*******************************************************************
  1845. * add_import_libs
  1846. */
  1847. static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
  1848. struct strarray imports, int delay, int is_cross )
  1849. {
  1850. struct strarray ret = empty_strarray;
  1851. unsigned int i, j;
  1852. for (i = 0; i < imports.count; i++)
  1853. {
  1854. const char *name = imports.str[i];
  1855. const char *lib = NULL;
  1856. if (name[0] == '-')
  1857. {
  1858. switch (name[1])
  1859. {
  1860. case 'L': strarray_add( &ret, name ); continue;
  1861. case 'l': name += 2; break;
  1862. default: continue;
  1863. }
  1864. }
  1865. else name = get_base_name( name );
  1866. for (j = 0; j < subdirs.count; j++)
  1867. {
  1868. if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
  1869. lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
  1870. else
  1871. lib = get_static_lib( submakes[j], name );
  1872. if (lib) break;
  1873. }
  1874. if (lib)
  1875. {
  1876. const char *ext = NULL;
  1877. if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
  1878. else if (is_cross) ext = ".cross.a";
  1879. if (ext) lib = replace_extension( lib, ".a", ext );
  1880. strarray_add_uniq( deps, lib );
  1881. strarray_add( &ret, lib );
  1882. }
  1883. else strarray_add( &ret, strmake( "-l%s", name ));
  1884. }
  1885. return ret;
  1886. }
  1887. /*******************************************************************
  1888. * add_install_rule
  1889. */
  1890. static void add_install_rule( struct makefile *make, const char *target,
  1891. const char *file, const char *dest )
  1892. {
  1893. if (strarray_exists( &make->install_lib, target ) ||
  1894. strarray_exists( &top_install_lib, make->obj_dir ) ||
  1895. strarray_exists( &top_install_lib, obj_dir_path( make, target )))
  1896. {
  1897. strarray_add( &make->install_rules[INSTALL_LIB], file );
  1898. strarray_add( &make->install_rules[INSTALL_LIB], dest );
  1899. }
  1900. else if (strarray_exists( &make->install_dev, target ) ||
  1901. strarray_exists( &top_install_dev, make->obj_dir ) ||
  1902. strarray_exists( &top_install_dev, obj_dir_path( make, target )))
  1903. {
  1904. strarray_add( &make->install_rules[INSTALL_DEV], file );
  1905. strarray_add( &make->install_rules[INSTALL_DEV], dest );
  1906. }
  1907. }
  1908. /*******************************************************************
  1909. * get_include_install_path
  1910. *
  1911. * Determine the installation path for a given include file.
  1912. */
  1913. static const char *get_include_install_path( const char *name )
  1914. {
  1915. if (!strncmp( name, "wine/", 5 )) return name + 5;
  1916. if (!strncmp( name, "msvcrt/", 7 )) return name;
  1917. return strmake( "windows/%s", name );
  1918. }
  1919. /*******************************************************************
  1920. * get_shared_library_name
  1921. *
  1922. * Determine possible names for a shared library with a version number.
  1923. */
  1924. static struct strarray get_shared_lib_names( const char *libname )
  1925. {
  1926. struct strarray ret = empty_strarray;
  1927. const char *ext, *p;
  1928. char *name, *first, *second;
  1929. size_t len = 0;
  1930. strarray_add( &ret, libname );
  1931. for (p = libname; (p = strchr( p, '.' )); p++)
  1932. if ((len = strspn( p + 1, "0123456789." ))) break;
  1933. if (!len) return ret;
  1934. ext = p + 1 + len;
  1935. if (*ext && ext[-1] == '.') ext--;
  1936. /* keep only the first group of digits */
  1937. name = xstrdup( libname );
  1938. first = name + (p - libname);
  1939. if ((second = strchr( first + 1, '.' )))
  1940. {
  1941. strcpy( second, ext );
  1942. strarray_add( &ret, xstrdup( name ));
  1943. }
  1944. return ret;
  1945. }
  1946. /*******************************************************************
  1947. * get_source_defines
  1948. */
  1949. static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
  1950. const char *obj )
  1951. {
  1952. unsigned int i;
  1953. struct strarray ret = empty_strarray;
  1954. strarray_addall( &ret, make->include_args );
  1955. if (source->use_msvcrt)
  1956. strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
  1957. for (i = 0; i < make->include_paths.count; i++)
  1958. strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
  1959. strarray_addall( &ret, make->define_args );
  1960. strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
  1961. if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
  1962. return ret;
  1963. }
  1964. /*******************************************************************
  1965. * remove_warning_flags
  1966. */
  1967. static struct strarray remove_warning_flags( struct strarray flags )
  1968. {
  1969. unsigned int i;
  1970. struct strarray ret = empty_strarray;
  1971. for (i = 0; i < flags.count; i++)
  1972. if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
  1973. strarray_add( &ret, flags.str[i] );
  1974. return ret;
  1975. }
  1976. /*******************************************************************
  1977. * get_debug_file
  1978. */
  1979. static const char *get_debug_file( struct makefile *make, const char *name )
  1980. {
  1981. const char *debug_file = NULL;
  1982. if (!make->is_cross || !crossdebug) return NULL;
  1983. if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
  1984. else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
  1985. if (debug_file) strarray_add( &make->debug_files, debug_file );
  1986. return debug_file;
  1987. }
  1988. /*******************************************************************
  1989. * cmd_prefix
  1990. */
  1991. static const char *cmd_prefix( const char *cmd )
  1992. {
  1993. if (!silent_rules) return "";
  1994. return strmake( "$(quiet_%s)", cmd );
  1995. }
  1996. /*******************************************************************
  1997. * output_winegcc_command
  1998. */
  1999. static void output_winegcc_command( struct makefile *make, int is_cross )
  2000. {
  2001. output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
  2002. output_filename( "--wine-objdir ." );
  2003. if (tools_dir)
  2004. {
  2005. output_filename( "--winebuild" );
  2006. output_filename( tools_path( make, "winebuild" ));
  2007. }
  2008. if (is_cross)
  2009. {
  2010. output_filename( "-b" );
  2011. output_filename( crosstarget );
  2012. output_filename( "--lib-suffix=.cross.a" );
  2013. }
  2014. else
  2015. {
  2016. output_filenames( target_flags );
  2017. output_filenames( lddll_flags );
  2018. }
  2019. }
  2020. /*******************************************************************
  2021. * output_symlink_rule
  2022. *
  2023. * Output a rule to create a symlink.
  2024. */
  2025. static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
  2026. {
  2027. const char *name = strrchr( link_name, '/' );
  2028. char *dir = NULL;
  2029. if (name)
  2030. {
  2031. dir = xstrdup( link_name );
  2032. dir[name - link_name] = 0;
  2033. }
  2034. output( "\t%s", cmd_prefix( "LN" ));
  2035. if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
  2036. output( "rm -f %s && ", link_name );
  2037. /* dest path with a directory needs special handling if ln -s isn't supported */
  2038. if (dir && strcmp( ln_s, "ln -s" ))
  2039. output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
  2040. else
  2041. output( "%s %s %s\n", ln_s, src_name, link_name );
  2042. free( dir );
  2043. }
  2044. /*******************************************************************
  2045. * output_srcdir_symlink
  2046. *
  2047. * Output rule to create a symlink back to the source directory, for source files
  2048. * that are needed at run-time.
  2049. */
  2050. static void output_srcdir_symlink( struct makefile *make, const char *obj )
  2051. {
  2052. char *src_file, *dst_file, *src_name;
  2053. if (!make->src_dir) return;
  2054. src_file = src_dir_path( make, obj );
  2055. dst_file = obj_dir_path( make, obj );
  2056. output( "%s: %s\n", dst_file, src_file );
  2057. src_name = src_file;
  2058. if (src_name[0] != '/' && make->obj_dir)
  2059. src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
  2060. output_symlink_rule( src_name, dst_file, 0 );
  2061. strarray_add( &make->all_targets, obj );
  2062. }
  2063. /*******************************************************************
  2064. * output_install_commands
  2065. */
  2066. static void output_install_commands( struct makefile *make, struct strarray files )
  2067. {
  2068. unsigned int i;
  2069. char *install_sh = root_src_dir_path( "tools/install-sh" );
  2070. for (i = 0; i < files.count; i += 2)
  2071. {
  2072. const char *file = files.str[i];
  2073. const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
  2074. switch (*files.str[i + 1])
  2075. {
  2076. case 'c': /* cross-compiled program */
  2077. output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2078. crosstarget, install_sh, obj_dir_path( make, file ), dest );
  2079. output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
  2080. break;
  2081. case 'd': /* data file */
  2082. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2083. install_sh, obj_dir_path( make, file ), dest );
  2084. break;
  2085. case 'D': /* data file in source dir */
  2086. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2087. install_sh, src_dir_path( make, file ), dest );
  2088. break;
  2089. case 'p': /* program file */
  2090. output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2091. install_sh, obj_dir_path( make, file ), dest );
  2092. break;
  2093. case 's': /* script */
  2094. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2095. install_sh, obj_dir_path( make, file ), dest );
  2096. break;
  2097. case 'S': /* script in source dir */
  2098. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2099. install_sh, src_dir_path( make, file ), dest );
  2100. break;
  2101. case 't': /* script in tools dir */
  2102. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2103. install_sh, tools_dir_path( make, files.str[i] ), dest );
  2104. break;
  2105. case 'y': /* symlink */
  2106. output_symlink_rule( files.str[i], dest, 1 );
  2107. break;
  2108. default:
  2109. assert(0);
  2110. }
  2111. strarray_add( &make->uninstall_files, dest );
  2112. }
  2113. }
  2114. /*******************************************************************
  2115. * output_install_rules
  2116. *
  2117. * Rules are stored as a (file,dest) pair of values.
  2118. * The first char of dest indicates the type of install.
  2119. */
  2120. static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
  2121. {
  2122. unsigned int i;
  2123. struct strarray files = make->install_rules[rules];
  2124. struct strarray targets = empty_strarray;
  2125. if (!files.count) return;
  2126. for (i = 0; i < files.count; i += 2)
  2127. {
  2128. const char *file = files.str[i];
  2129. switch (*files.str[i + 1])
  2130. {
  2131. case 'c': /* cross-compiled program */
  2132. case 'd': /* data file */
  2133. case 'p': /* program file */
  2134. case 's': /* script */
  2135. strarray_add_uniq( &targets, obj_dir_path( make, file ));
  2136. break;
  2137. case 't': /* script in tools dir */
  2138. strarray_add_uniq( &targets, tools_dir_path( make, file ));
  2139. break;
  2140. }
  2141. }
  2142. output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
  2143. output_filenames( targets );
  2144. output( "\n" );
  2145. output_install_commands( make, files );
  2146. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
  2147. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
  2148. }
  2149. static int cmp_string_length( const char **a, const char **b )
  2150. {
  2151. int paths_a = 0, paths_b = 0;
  2152. const char *p;
  2153. for (p = *a; *p; p++) if (*p == '/') paths_a++;
  2154. for (p = *b; *p; p++) if (*p == '/') paths_b++;
  2155. if (paths_b != paths_a) return paths_b - paths_a;
  2156. return strcmp( *a, *b );
  2157. }
  2158. /*******************************************************************
  2159. * output_uninstall_rules
  2160. */
  2161. static void output_uninstall_rules( struct makefile *make )
  2162. {
  2163. static const char *dirs_order[] =
  2164. { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
  2165. struct strarray uninstall_dirs = empty_strarray;
  2166. unsigned int i, j;
  2167. if (!make->uninstall_files.count) return;
  2168. output( "uninstall::\n" );
  2169. output_rm_filenames( make->uninstall_files );
  2170. strarray_add_uniq( &make->phony_targets, "uninstall" );
  2171. if (!subdirs.count) return;
  2172. for (i = 0; i < make->uninstall_files.count; i++)
  2173. {
  2174. char *dir = xstrdup( make->uninstall_files.str[i] );
  2175. while (strchr( dir, '/' ))
  2176. {
  2177. *strrchr( dir, '/' ) = 0;
  2178. strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
  2179. }
  2180. }
  2181. strarray_qsort( &uninstall_dirs, cmp_string_length );
  2182. output( "\t-rmdir" );
  2183. for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
  2184. {
  2185. for (j = 0; j < uninstall_dirs.count; j++)
  2186. {
  2187. if (!uninstall_dirs.str[j]) continue;
  2188. if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
  2189. continue;
  2190. output_filename( uninstall_dirs.str[j] );
  2191. uninstall_dirs.str[j] = NULL;
  2192. }
  2193. }
  2194. for (j = 0; j < uninstall_dirs.count; j++)
  2195. if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
  2196. output( "\n" );
  2197. }
  2198. /*******************************************************************
  2199. * output_po_files
  2200. */
  2201. static void output_po_files( const struct makefile *make )
  2202. {
  2203. const char *po_dir = src_dir_path( make, "po" );
  2204. unsigned int i;
  2205. if (linguas.count)
  2206. {
  2207. for (i = 0; i < linguas.count; i++)
  2208. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2209. output( ": %s/wine.pot\n", po_dir );
  2210. output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
  2211. cmd_prefix( "MSG" ), po_dir );
  2212. output( "po:" );
  2213. for (i = 0; i < linguas.count; i++)
  2214. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2215. output( "\n" );
  2216. }
  2217. output( "%s/wine.pot:", po_dir );
  2218. output_filenames( make->pot_files );
  2219. output( "\n" );
  2220. output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
  2221. output_filenames( make->pot_files );
  2222. output( "\n" );
  2223. }
  2224. /*******************************************************************
  2225. * output_source_y
  2226. */
  2227. static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
  2228. {
  2229. /* add source file dependency for parallel makes */
  2230. char *header = strmake( "%s.tab.h", obj );
  2231. if (find_include_file( make, header ))
  2232. {
  2233. output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
  2234. output( "\t%s%s -p %s_ -o %s.tab.c -d %s\n",
  2235. cmd_prefix( "BISON" ), bison, obj, obj_dir_path( make, obj ), source->filename );
  2236. output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
  2237. source->filename, obj_dir_path( make, header ));
  2238. strarray_add( &make->clean_files, header );
  2239. }
  2240. else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
  2241. output( "\t%s%s -p %s_ -o $@ %s\n", cmd_prefix( "BISON" ), bison, obj, source->filename );
  2242. }
  2243. /*******************************************************************
  2244. * output_source_l
  2245. */
  2246. static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
  2247. {
  2248. output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
  2249. output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
  2250. }
  2251. /*******************************************************************
  2252. * output_source_h
  2253. */
  2254. static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
  2255. {
  2256. if (source->file->flags & FLAG_GENERATED)
  2257. strarray_add( &make->all_targets, source->name );
  2258. else
  2259. add_install_rule( make, source->name, source->name,
  2260. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2261. }
  2262. /*******************************************************************
  2263. * output_source_rc
  2264. */
  2265. static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
  2266. {
  2267. struct strarray defines = get_source_defines( make, source, obj );
  2268. const char *po_dir = NULL;
  2269. unsigned int i;
  2270. if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
  2271. if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
  2272. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2273. if (source->file->flags & FLAG_RC_PO)
  2274. {
  2275. strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
  2276. output( "%s.pot ", obj_dir_path( make, obj ) );
  2277. }
  2278. output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
  2279. output_filename( tools_path( make, "wrc" ));
  2280. output_filenames( source->dependencies );
  2281. output( "\n" );
  2282. output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
  2283. if (make->is_win16) output_filename( "-m16" );
  2284. output_filename( "--nostdinc" );
  2285. if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
  2286. output_filenames( defines );
  2287. output_filename( source->filename );
  2288. output( "\n" );
  2289. if (po_dir)
  2290. {
  2291. output( "%s.res:", obj_dir_path( make, obj ));
  2292. for (i = 0; i < linguas.count; i++)
  2293. output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
  2294. output( "\n" );
  2295. }
  2296. }
  2297. /*******************************************************************
  2298. * output_source_mc
  2299. */
  2300. static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
  2301. {
  2302. unsigned int i;
  2303. char *obj_path = obj_dir_path( make, obj );
  2304. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2305. strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
  2306. output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
  2307. output_filename( tools_path( make, "wmc" ));
  2308. output_filenames( source->dependencies );
  2309. output( "\n" );
  2310. output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
  2311. if (linguas.count)
  2312. {
  2313. output_filename( "--po-dir=po" );
  2314. output( "\n" );
  2315. output( "%s.res:", obj_dir_path( make, obj ));
  2316. for (i = 0; i < linguas.count; i++)
  2317. output_filename( strmake( "po/%s.mo", linguas.str[i] ));
  2318. }
  2319. output( "\n" );
  2320. }
  2321. /*******************************************************************
  2322. * output_source_res
  2323. */
  2324. static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
  2325. {
  2326. strarray_add( &make->res_files, source->name );
  2327. }
  2328. /*******************************************************************
  2329. * output_source_idl
  2330. */
  2331. static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
  2332. {
  2333. struct strarray defines = get_source_defines( make, source, obj );
  2334. struct strarray targets = empty_strarray;
  2335. char *dest;
  2336. unsigned int i;
  2337. if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
  2338. if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
  2339. for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
  2340. {
  2341. if (!(source->file->flags & idl_outputs[i].flag)) continue;
  2342. dest = strmake( "%s%s", obj, idl_outputs[i].ext );
  2343. if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
  2344. strarray_add( &targets, dest );
  2345. }
  2346. if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
  2347. if (source->file->flags & FLAG_INSTALL)
  2348. {
  2349. add_install_rule( make, source->name, xstrdup( source->name ),
  2350. strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
  2351. if (source->file->flags & FLAG_IDL_HEADER)
  2352. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2353. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2354. }
  2355. if (!targets.count) return;
  2356. output_filenames_obj_dir( make, targets );
  2357. output( ": %s\n", tools_path( make, "widl" ));
  2358. output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
  2359. output_filenames( target_flags );
  2360. output_filename( "--nostdinc" );
  2361. output_filename( "-Ldlls/\\*" );
  2362. output_filenames( defines );
  2363. output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
  2364. output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
  2365. output_filename( source->filename );
  2366. output( "\n" );
  2367. output_filenames_obj_dir( make, targets );
  2368. output( ": %s", source->filename );
  2369. output_filenames( source->dependencies );
  2370. for (i = 0; i < source->importlibdeps.count; i++)
  2371. {
  2372. struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
  2373. output_filename( obj_dir_path( submake, submake->module ));
  2374. }
  2375. output( "\n" );
  2376. }
  2377. /*******************************************************************
  2378. * output_source_x
  2379. */
  2380. static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
  2381. {
  2382. output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
  2383. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2384. output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
  2385. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2386. if (source->file->flags & FLAG_INSTALL)
  2387. {
  2388. add_install_rule( make, source->name, source->name,
  2389. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2390. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2391. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2392. }
  2393. }
  2394. /*******************************************************************
  2395. * output_source_sfd
  2396. */
  2397. static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
  2398. {
  2399. unsigned int i;
  2400. char *ttf_obj = strmake( "%s.ttf", obj );
  2401. char *ttf_file = src_dir_path( make, ttf_obj );
  2402. if (fontforge && !make->src_dir)
  2403. {
  2404. output( "%s: %s\n", ttf_file, source->filename );
  2405. output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
  2406. fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
  2407. if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
  2408. }
  2409. if (source->file->flags & FLAG_INSTALL)
  2410. {
  2411. add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
  2412. output_srcdir_symlink( make, ttf_obj );
  2413. }
  2414. if (source->file->flags & FLAG_SFD_FONTS)
  2415. {
  2416. struct strarray *array = source->file->args;
  2417. for (i = 0; i < array->count; i++)
  2418. {
  2419. char *font = strtok( xstrdup(array->str[i]), " \t" );
  2420. char *args = strtok( NULL, "" );
  2421. strarray_add( &make->all_targets, xstrdup( font ));
  2422. output( "%s: %s %s\n", obj_dir_path( make, font ),
  2423. tools_path( make, "sfnt2fon" ), ttf_file );
  2424. output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
  2425. tools_path( make, "sfnt2fon" ), ttf_file, args );
  2426. add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
  2427. }
  2428. }
  2429. }
  2430. /*******************************************************************
  2431. * output_source_svg
  2432. */
  2433. static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
  2434. {
  2435. static const char * const images[] = { "bmp", "cur", "ico", NULL };
  2436. unsigned int i;
  2437. if (convert && rsvg && icotool && !make->src_dir)
  2438. {
  2439. for (i = 0; images[i]; i++)
  2440. if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
  2441. if (images[i])
  2442. {
  2443. output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
  2444. output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
  2445. cmd_prefix( "GEN" ), convert, icotool, rsvg,
  2446. root_src_dir_path( "tools/buildimage" ), source->filename );
  2447. }
  2448. }
  2449. }
  2450. /*******************************************************************
  2451. * output_source_nls
  2452. */
  2453. static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
  2454. {
  2455. add_install_rule( make, source->name, source->name,
  2456. strmake( "D$(nlsdir)/%s", source->name ));
  2457. output_srcdir_symlink( make, strmake( "%s.nls", obj ));
  2458. }
  2459. /*******************************************************************
  2460. * output_source_desktop
  2461. */
  2462. static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
  2463. {
  2464. add_install_rule( make, source->name, source->name,
  2465. strmake( "D$(datadir)/applications/%s", source->name ));
  2466. }
  2467. /*******************************************************************
  2468. * output_source_po
  2469. */
  2470. static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
  2471. {
  2472. output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
  2473. output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
  2474. strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
  2475. }
  2476. /*******************************************************************
  2477. * output_source_in
  2478. */
  2479. static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
  2480. {
  2481. unsigned int i;
  2482. if (strendswith( obj, ".man" ) && source->file->args)
  2483. {
  2484. struct strarray symlinks;
  2485. char *dir, *dest = replace_extension( obj, ".man", "" );
  2486. char *lang = strchr( dest, '.' );
  2487. char *section = source->file->args;
  2488. if (lang)
  2489. {
  2490. *lang++ = 0;
  2491. dir = strmake( "$(mandir)/%s/man%s", lang, section );
  2492. }
  2493. else dir = strmake( "$(mandir)/man%s", section );
  2494. add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
  2495. symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
  2496. for (i = 0; i < symlinks.count; i++)
  2497. add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
  2498. strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
  2499. free( dest );
  2500. free( dir );
  2501. }
  2502. strarray_add( &make->in_files, xstrdup(obj) );
  2503. strarray_add( &make->all_targets, xstrdup(obj) );
  2504. output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
  2505. output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
  2506. output( "%s:", obj_dir_path( make, obj ));
  2507. output_filenames( source->dependencies );
  2508. output( "\n" );
  2509. add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
  2510. }
  2511. /*******************************************************************
  2512. * output_source_spec
  2513. */
  2514. static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
  2515. {
  2516. struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
  2517. struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
  2518. struct strarray all_libs, dep_libs = empty_strarray;
  2519. char *dll_name, *obj_name, *output_file;
  2520. const char *debug_file;
  2521. if (!imports.count) imports = make->imports;
  2522. if (!dll_flags.count) dll_flags = make->extradllflags;
  2523. if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
  2524. all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
  2525. dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
  2526. obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
  2527. output_file = obj_dir_path( make, dll_name );
  2528. strarray_add( &make->clean_files, dll_name );
  2529. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2530. output( "%s.res:", obj_dir_path( make, obj ));
  2531. output_filename( obj_dir_path( make, dll_name ));
  2532. output_filename( tools_path( make, "wrc" ));
  2533. output( "\n" );
  2534. output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
  2535. tools_path( make, "wrc" ));
  2536. output( "%s:", output_file);
  2537. output_filename( source->filename );
  2538. output_filename( obj_name );
  2539. output_filenames( dep_libs );
  2540. output_filename( tools_path( make, "winebuild" ));
  2541. output_filename( tools_path( make, "winegcc" ));
  2542. output( "\n" );
  2543. output_winegcc_command( make, make->is_cross );
  2544. output_filename( "-s" );
  2545. output_filenames( dll_flags );
  2546. output_filename( "-shared" );
  2547. output_filename( source->filename );
  2548. output_filename( obj_name );
  2549. if ((debug_file = get_debug_file( make, dll_name )))
  2550. output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
  2551. output_filenames( all_libs );
  2552. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2553. output( "\n" );
  2554. }
  2555. /*******************************************************************
  2556. * output_source_default
  2557. */
  2558. static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
  2559. {
  2560. struct strarray defines = get_source_defines( make, source, obj );
  2561. int is_dll_src = (make->testdll &&
  2562. strendswith( source->name, ".c" ) &&
  2563. find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
  2564. int need_cross = (crosstarget &&
  2565. !(source->file->flags & FLAG_C_UNIX) &&
  2566. (make->is_cross || make->staticlib ||
  2567. (source->file->flags & FLAG_C_IMPLIB)));
  2568. int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
  2569. (!need_cross ||
  2570. (source->file->flags & FLAG_C_IMPLIB) ||
  2571. (make->staticlib && !make->extlib)));
  2572. if ((source->file->flags & FLAG_GENERATED) &&
  2573. (!make->testdll || !strendswith( source->filename, "testlist.c" )))
  2574. strarray_add( &make->clean_files, source->basename );
  2575. if (need_obj)
  2576. {
  2577. if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
  2578. strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
  2579. else if (source->file->flags & FLAG_C_IMPLIB)
  2580. strarray_add( &make->implib_files, strmake( "%s.o", obj ));
  2581. else if (!is_dll_src)
  2582. strarray_add( &make->object_files, strmake( "%s.o", obj ));
  2583. else
  2584. strarray_add( &make->clean_files, strmake( "%s.o", obj ));
  2585. output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2586. output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
  2587. output_filenames( defines );
  2588. if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
  2589. {
  2590. output_filenames( unix_dllflags );
  2591. }
  2592. else if (make->module || make->testdll)
  2593. {
  2594. output_filenames( dll_flags );
  2595. if (source->use_msvcrt) output_filenames( msvcrt_flags );
  2596. if (!*dll_ext && make->module && is_crt_module( make->module ))
  2597. output_filename( "-fno-builtin" );
  2598. }
  2599. output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
  2600. output_filenames( cpp_flags );
  2601. output_filename( "$(CFLAGS)" );
  2602. output( "\n" );
  2603. }
  2604. if (need_cross)
  2605. {
  2606. if (source->file->flags & FLAG_C_IMPLIB)
  2607. strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
  2608. else if (!is_dll_src)
  2609. strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
  2610. else
  2611. strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
  2612. output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2613. output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
  2614. output_filenames( defines );
  2615. output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
  2616. if (make->module && is_crt_module( make->module ))
  2617. output_filename( "-fno-builtin" );
  2618. output_filenames( cpp_flags );
  2619. output_filename( "$(CROSSCFLAGS)" );
  2620. output( "\n" );
  2621. }
  2622. if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
  2623. {
  2624. strarray_add( &make->c2man_files, source->filename );
  2625. if (make->testdll && !is_dll_src)
  2626. {
  2627. strarray_add( &make->test_files, obj );
  2628. strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
  2629. output( "%s.ok:\n", obj_dir_path( make, obj ));
  2630. output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
  2631. cmd_prefix( "TEST" ),
  2632. root_src_dir_path( "tools/runtest" ), make->testdll,
  2633. obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
  2634. make->is_cross ? "" : dll_ext, obj );
  2635. }
  2636. }
  2637. if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
  2638. if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
  2639. output( ":" );
  2640. output_filenames( source->dependencies );
  2641. output( "\n" );
  2642. }
  2643. /* dispatch table to output rules for a single source file */
  2644. static const struct
  2645. {
  2646. const char *ext;
  2647. void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
  2648. } output_source_funcs[] =
  2649. {
  2650. { "y", output_source_y },
  2651. { "l", output_source_l },
  2652. { "h", output_source_h },
  2653. { "rh", output_source_h },
  2654. { "inl", output_source_h },
  2655. { "rc", output_source_rc },
  2656. { "mc", output_source_mc },
  2657. { "res", output_source_res },
  2658. { "idl", output_source_idl },
  2659. { "sfd", output_source_sfd },
  2660. { "svg", output_source_svg },
  2661. { "nls", output_source_nls },
  2662. { "desktop", output_source_desktop },
  2663. { "po", output_source_po },
  2664. { "in", output_source_in },
  2665. { "x", output_source_x },
  2666. { "spec", output_source_spec },
  2667. { NULL, output_source_default }
  2668. };
  2669. /*******************************************************************
  2670. * get_unix_lib_name
  2671. */
  2672. static char *get_unix_lib_name( struct makefile *make )
  2673. {
  2674. struct incl_file *source;
  2675. if (!*dll_ext) return NULL;
  2676. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  2677. {
  2678. if (!(source->file->flags & FLAG_C_UNIX)) continue;
  2679. return strmake( "%s%s", get_base_name( make->module ), dll_ext );
  2680. }
  2681. return NULL;
  2682. }
  2683. /*******************************************************************
  2684. * output_man_pages
  2685. */
  2686. static void output_man_pages( struct makefile *make )
  2687. {
  2688. if (make->c2man_files.count)
  2689. {
  2690. char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2691. output( "manpages::\n" );
  2692. output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2693. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2694. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2695. output_filename( strmake( "-o documentation/man%s", man_ext ));
  2696. output_filenames( make->c2man_files );
  2697. output( "\n" );
  2698. output( "htmlpages::\n" );
  2699. output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2700. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2701. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2702. output_filename( "-o documentation/html" );
  2703. output_filenames( make->c2man_files );
  2704. output( "\n" );
  2705. output( "sgmlpages::\n" );
  2706. output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2707. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2708. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2709. output_filename( "-o documentation/api-guide" );
  2710. output_filenames( make->c2man_files );
  2711. output( "\n" );
  2712. output( "xmlpages::\n" );
  2713. output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2714. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2715. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2716. output_filename( "-o documentation/api-guide-xml" );
  2717. output_filenames( make->c2man_files );
  2718. output( "\n" );
  2719. strarray_add( &make->phony_targets, "manpages" );
  2720. strarray_add( &make->phony_targets, "htmlpages" );
  2721. strarray_add( &make->phony_targets, "sgmlpages" );
  2722. strarray_add( &make->phony_targets, "xmlpages" );
  2723. }
  2724. }
  2725. /*******************************************************************
  2726. * output_module
  2727. */
  2728. static void output_module( struct makefile *make )
  2729. {
  2730. struct strarray all_libs = empty_strarray;
  2731. struct strarray dep_libs = empty_strarray;
  2732. struct strarray imports = make->imports;
  2733. char *module_name = strmake( "%s%s", make->module, make->is_cross ? "" : dll_ext );
  2734. const char *debug_file;
  2735. const char *delay_load = delay_load_flag;
  2736. char *spec_file = NULL;
  2737. unsigned int i;
  2738. if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2739. if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
  2740. imports = add_default_imports( make, imports );
  2741. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
  2742. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
  2743. if (!make->use_msvcrt)
  2744. {
  2745. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  2746. strarray_addall( &all_libs, libs );
  2747. }
  2748. if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
  2749. if (delay_load)
  2750. {
  2751. for (i = 0; i < make->delayimports.count; i++)
  2752. strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
  2753. strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
  2754. }
  2755. strarray_add( &make->all_targets, module_name );
  2756. if (make->is_cross)
  2757. add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
  2758. else if (*dll_ext)
  2759. add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
  2760. else
  2761. add_install_rule( make, make->module, module_name,
  2762. strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
  2763. output( "%s:", obj_dir_path( make, module_name ));
  2764. if (spec_file) output_filename( spec_file );
  2765. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2766. output_filenames_obj_dir( make, make->res_files );
  2767. output_filenames( dep_libs );
  2768. output_filename( tools_path( make, "winebuild" ));
  2769. output_filename( tools_path( make, "winegcc" ));
  2770. output( "\n" );
  2771. output_winegcc_command( make, make->is_cross );
  2772. if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
  2773. if (spec_file)
  2774. {
  2775. output_filename( "-shared" );
  2776. output_filename( spec_file );
  2777. }
  2778. output_filenames( make->extradllflags );
  2779. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2780. output_filenames_obj_dir( make, make->res_files );
  2781. debug_file = get_debug_file( make, make->module );
  2782. if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
  2783. output_filenames( all_libs );
  2784. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2785. output( "\n" );
  2786. if (spec_file)
  2787. output_man_pages( make );
  2788. else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
  2789. {
  2790. char *binary = replace_extension( make->module, ".exe", "" );
  2791. add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
  2792. }
  2793. }
  2794. /*******************************************************************
  2795. * output_fake_module
  2796. */
  2797. static void output_fake_module( struct makefile *make )
  2798. {
  2799. char *spec_file = NULL;
  2800. if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2801. strarray_add( &make->all_targets, make->module );
  2802. add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
  2803. output( "%s:", obj_dir_path( make, make->module ));
  2804. if (spec_file) output_filename( spec_file );
  2805. output_filenames_obj_dir( make, make->res_files );
  2806. output_filename( tools_path( make, "winebuild" ));
  2807. output_filename( tools_path( make, "winegcc" ));
  2808. output( "\n" );
  2809. output_winegcc_command( make, make->is_cross );
  2810. output_filename( "-Wb,--fake-module" );
  2811. if (spec_file)
  2812. {
  2813. output_filename( "-shared" );
  2814. output_filename( spec_file );
  2815. }
  2816. output_filenames( make->extradllflags );
  2817. output_filenames_obj_dir( make, make->res_files );
  2818. output( "\n" );
  2819. }
  2820. /*******************************************************************
  2821. * output_import_lib
  2822. */
  2823. static void output_import_lib( struct makefile *make )
  2824. {
  2825. char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2826. char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
  2827. strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
  2828. if (!*dll_ext && needs_delay_lib( make ))
  2829. {
  2830. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2831. output( "%s.delay.a ", importlib_path );
  2832. }
  2833. output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
  2834. output_filenames_obj_dir( make, make->implib_files );
  2835. output( "\n" );
  2836. output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
  2837. output_filenames( target_flags );
  2838. if (make->is_win16) output_filename( "-m16" );
  2839. output_filename( "--export" );
  2840. output_filename( spec_file );
  2841. output_filenames_obj_dir( make, make->implib_files );
  2842. output( "\n" );
  2843. add_install_rule( make, make->importlib,
  2844. strmake( "lib%s.a", make->importlib ),
  2845. strmake( "d%s/lib%s.a", so_dir, make->importlib ));
  2846. if (crosstarget)
  2847. {
  2848. strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
  2849. output_filename( strmake( "%s.cross.a", importlib_path ));
  2850. if (needs_delay_lib( make ))
  2851. {
  2852. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2853. output_filename( strmake( "%s.delay.a", importlib_path ));
  2854. }
  2855. output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
  2856. output_filenames_obj_dir( make, make->crossimplib_files );
  2857. output( "\n" );
  2858. output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
  2859. tools_path( make, "winebuild" ), crosstarget );
  2860. if (make->is_win16) output_filename( "-m16" );
  2861. output_filename( "--export" );
  2862. output_filename( spec_file );
  2863. output_filenames_obj_dir( make, make->crossimplib_files );
  2864. output( "\n" );
  2865. add_install_rule( make, make->importlib,
  2866. strmake( "lib%s.cross.a", make->importlib ),
  2867. strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
  2868. }
  2869. }
  2870. /*******************************************************************
  2871. * output_unix_lib
  2872. */
  2873. static void output_unix_lib( struct makefile *make )
  2874. {
  2875. struct strarray unix_libs = empty_strarray;
  2876. struct strarray unix_deps = empty_strarray;
  2877. char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2878. if (!make->native_unix_lib)
  2879. {
  2880. struct strarray unix_imports = empty_strarray;
  2881. strarray_add( &unix_imports, "ntdll" );
  2882. strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
  2883. strarray_add( &unix_imports, "winecrt0" );
  2884. if (spec_file) strarray_add( &unix_deps, spec_file );
  2885. strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
  2886. strarray_addall( &unix_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  2887. strarray_addall( &unix_libs, libs );
  2888. }
  2889. else unix_libs = add_unix_libraries( make, &unix_deps );
  2890. strarray_add( &make->all_targets, make->unixlib );
  2891. add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
  2892. output( "%s:", obj_dir_path( make, make->unixlib ));
  2893. output_filenames_obj_dir( make, make->unixobj_files );
  2894. output_filenames( unix_deps );
  2895. if (make->native_unix_lib)
  2896. {
  2897. output( "\n" );
  2898. output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
  2899. output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
  2900. }
  2901. else
  2902. {
  2903. output_filename( tools_path( make, "winebuild" ));
  2904. output_filename( tools_path( make, "winegcc" ));
  2905. output( "\n" );
  2906. output_winegcc_command( make, 0 );
  2907. output_filename( "-munix" );
  2908. output_filename( "-shared" );
  2909. if (spec_file) output_filename( spec_file );
  2910. }
  2911. output_filenames_obj_dir( make, make->unixobj_files );
  2912. output_filenames( unix_libs );
  2913. output_filename( "$(LDFLAGS)" );
  2914. output( "\n" );
  2915. }
  2916. /*******************************************************************
  2917. * output_static_lib
  2918. */
  2919. static void output_static_lib( struct makefile *make )
  2920. {
  2921. if (!crosstarget || !make->extlib)
  2922. {
  2923. strarray_add( &make->clean_files, make->staticlib );
  2924. output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
  2925. output_filenames_obj_dir( make, make->object_files );
  2926. output_filenames_obj_dir( make, make->unixobj_files );
  2927. output( "\n" );
  2928. output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
  2929. output_filenames( target_flags );
  2930. output_filenames_obj_dir( make, make->object_files );
  2931. output_filenames_obj_dir( make, make->unixobj_files );
  2932. output( "\n" );
  2933. add_install_rule( make, make->staticlib, make->staticlib,
  2934. strmake( "d%s/%s", so_dir, make->staticlib ));
  2935. }
  2936. if (crosstarget)
  2937. {
  2938. char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
  2939. strarray_add( &make->clean_files, name );
  2940. output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
  2941. output_filenames_obj_dir( make, make->crossobj_files );
  2942. output( "\n" );
  2943. output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
  2944. tools_path( make, "winebuild" ), crosstarget );
  2945. output_filenames_obj_dir( make, make->crossobj_files );
  2946. output( "\n" );
  2947. if (!make->extlib)
  2948. add_install_rule( make, make->staticlib, name,
  2949. strmake( "d%s/%s", pe_dir, make->staticlib ));
  2950. }
  2951. }
  2952. /*******************************************************************
  2953. * output_shared_lib
  2954. */
  2955. static void output_shared_lib( struct makefile *make )
  2956. {
  2957. unsigned int i;
  2958. char *basename, *p;
  2959. struct strarray names = get_shared_lib_names( make->sharedlib );
  2960. struct strarray all_libs = empty_strarray;
  2961. struct strarray dep_libs = empty_strarray;
  2962. basename = xstrdup( make->sharedlib );
  2963. if ((p = strchr( basename, '.' ))) *p = 0;
  2964. strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
  2965. strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
  2966. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  2967. strarray_addall( &all_libs, libs );
  2968. output( "%s:", obj_dir_path( make, make->sharedlib ));
  2969. output_filenames_obj_dir( make, make->object_files );
  2970. output_filenames( dep_libs );
  2971. output( "\n" );
  2972. output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
  2973. output_filenames_obj_dir( make, make->object_files );
  2974. output_filenames( all_libs );
  2975. output_filename( "$(LDFLAGS)" );
  2976. output( "\n" );
  2977. add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
  2978. for (i = 1; i < names.count; i++)
  2979. {
  2980. output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
  2981. output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
  2982. add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
  2983. }
  2984. strarray_addall( &make->all_targets, names );
  2985. }
  2986. /*******************************************************************
  2987. * output_test_module
  2988. */
  2989. static void output_test_module( struct makefile *make )
  2990. {
  2991. char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
  2992. char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
  2993. char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
  2994. struct strarray dep_libs = empty_strarray;
  2995. struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
  2996. 0, make->is_cross );
  2997. struct makefile *parent = get_parent_makefile( make );
  2998. const char *ext = make->is_cross ? "" : dll_ext;
  2999. const char *debug_file;
  3000. char *output_file;
  3001. strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
  3002. strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
  3003. output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
  3004. output( "%s:\n", output_file );
  3005. output_winegcc_command( make, make->is_cross );
  3006. output_filenames( make->extradllflags );
  3007. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3008. output_filenames_obj_dir( make, make->res_files );
  3009. if ((debug_file = get_debug_file( make, testmodule )))
  3010. output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
  3011. output_filenames( all_libs );
  3012. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3013. output( "\n" );
  3014. output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
  3015. output_winegcc_command( make, make->is_cross );
  3016. output_filename( "-s" );
  3017. output_filename( strmake( "-Wb,-F,%s", testmodule ));
  3018. output_filenames( make->extradllflags );
  3019. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3020. output_filenames_obj_dir( make, make->res_files );
  3021. output_filenames( all_libs );
  3022. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3023. output( "\n" );
  3024. output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
  3025. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3026. output_filenames_obj_dir( make, make->res_files );
  3027. output_filenames( dep_libs );
  3028. output_filename( tools_path( make, "winebuild" ));
  3029. output_filename( tools_path( make, "winegcc" ));
  3030. output( "\n" );
  3031. output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
  3032. output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
  3033. testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
  3034. output_filenames_obj_dir( make, make->ok_files );
  3035. output( ": %s%s", obj_dir_path( make, testmodule ), ext );
  3036. if (parent)
  3037. {
  3038. output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
  3039. : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
  3040. if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
  3041. }
  3042. output( "\n" );
  3043. output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
  3044. if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
  3045. output( "\n" );
  3046. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
  3047. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
  3048. output( "%s::\n", obj_dir_path( make, "testclean" ));
  3049. output( "\trm -f" );
  3050. output_filenames_obj_dir( make, make->ok_files );
  3051. output( "\n" );
  3052. strarray_addall( &make->clean_files, make->ok_files );
  3053. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
  3054. }
  3055. /*******************************************************************
  3056. * output_programs
  3057. */
  3058. static void output_programs( struct makefile *make )
  3059. {
  3060. unsigned int i, j;
  3061. for (i = 0; i < make->programs.count; i++)
  3062. {
  3063. char *program_installed = NULL;
  3064. char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
  3065. struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
  3066. struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
  3067. struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
  3068. struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
  3069. if (!objs.count) objs = make->object_files;
  3070. if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
  3071. {
  3072. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  3073. strarray_addall( &all_libs, libs );
  3074. }
  3075. output( "%s:", obj_dir_path( make, program ) );
  3076. output_filenames_obj_dir( make, objs );
  3077. output_filenames( deps );
  3078. output( "\n" );
  3079. output( "\t%s$(CC) -o $@", cmd_prefix( "CC" ));
  3080. output_filenames_obj_dir( make, objs );
  3081. output_filenames( all_libs );
  3082. output_filename( "$(LDFLAGS)" );
  3083. output( "\n" );
  3084. strarray_add( &make->all_targets, program );
  3085. for (j = 0; j < symlinks.count; j++)
  3086. {
  3087. output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
  3088. output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
  3089. }
  3090. strarray_addall( &make->all_targets, symlinks );
  3091. add_install_rule( make, program, program_installed ? program_installed : program,
  3092. strmake( "p$(bindir)/%s", program ));
  3093. for (j = 0; j < symlinks.count; j++)
  3094. add_install_rule( make, symlinks.str[j], program,
  3095. strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
  3096. }
  3097. }
  3098. /*******************************************************************
  3099. * output_subdirs
  3100. */
  3101. static void output_subdirs( struct makefile *make )
  3102. {
  3103. struct strarray all_targets = empty_strarray;
  3104. struct strarray makefile_deps = empty_strarray;
  3105. struct strarray clean_files = empty_strarray;
  3106. struct strarray testclean_files = empty_strarray;
  3107. struct strarray distclean_files = empty_strarray;
  3108. struct strarray dependencies = empty_strarray;
  3109. struct strarray install_lib_deps = empty_strarray;
  3110. struct strarray install_dev_deps = empty_strarray;
  3111. struct strarray tooldeps_deps = empty_strarray;
  3112. struct strarray buildtest_deps = empty_strarray;
  3113. unsigned int i;
  3114. strarray_addall( &clean_files, make->clean_files );
  3115. strarray_addall( &distclean_files, make->distclean_files );
  3116. strarray_addall( &all_targets, make->all_targets );
  3117. for (i = 0; i < subdirs.count; i++)
  3118. {
  3119. strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
  3120. strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
  3121. strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
  3122. strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
  3123. strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
  3124. strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
  3125. strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
  3126. strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
  3127. if (submakes[i]->disabled) continue;
  3128. strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
  3129. if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
  3130. strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
  3131. if (submakes[i]->testdll)
  3132. strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
  3133. if (submakes[i]->install_rules[INSTALL_LIB].count)
  3134. strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
  3135. if (submakes[i]->install_rules[INSTALL_DEV].count)
  3136. strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
  3137. }
  3138. strarray_addall( &dependencies, makefile_deps );
  3139. output( "all:" );
  3140. output_filenames( all_targets );
  3141. output( "\n" );
  3142. output( "Makefile:" );
  3143. output_filenames( makefile_deps );
  3144. output( "\n" );
  3145. output_filenames( dependencies );
  3146. output( ":\n" );
  3147. if (install_lib_deps.count)
  3148. {
  3149. output( "install install-lib::" );
  3150. output_filenames( install_lib_deps );
  3151. output( "\n" );
  3152. strarray_add_uniq( &make->phony_targets, "install" );
  3153. strarray_add_uniq( &make->phony_targets, "install-lib" );
  3154. }
  3155. if (install_dev_deps.count)
  3156. {
  3157. output( "install install-dev::" );
  3158. output_filenames( install_dev_deps );
  3159. output( "\n" );
  3160. strarray_add_uniq( &make->phony_targets, "install" );
  3161. strarray_add_uniq( &make->phony_targets, "install-dev" );
  3162. }
  3163. output_uninstall_rules( make );
  3164. if (buildtest_deps.count)
  3165. {
  3166. output( "buildtests:" );
  3167. output_filenames( buildtest_deps );
  3168. output( "\n" );
  3169. strarray_add_uniq( &make->phony_targets, "buildtests" );
  3170. }
  3171. output( "check test:" );
  3172. output_filenames( testclean_files );
  3173. output( "\n" );
  3174. strarray_add_uniq( &make->phony_targets, "check" );
  3175. strarray_add_uniq( &make->phony_targets, "test" );
  3176. output( "clean::\n");
  3177. output_rm_filenames( clean_files );
  3178. output( "testclean::\n");
  3179. output_rm_filenames( testclean_files );
  3180. output( "distclean::\n");
  3181. output_rm_filenames( distclean_files );
  3182. strarray_add_uniq( &make->phony_targets, "distclean" );
  3183. strarray_add_uniq( &make->phony_targets, "testclean" );
  3184. if (tooldeps_deps.count)
  3185. {
  3186. output( "__tooldeps__:" );
  3187. output_filenames( tooldeps_deps );
  3188. output( "\n" );
  3189. strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
  3190. }
  3191. if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
  3192. if (make->phony_targets.count)
  3193. {
  3194. output( ".PHONY:" );
  3195. output_filenames( make->phony_targets );
  3196. output( "\n" );
  3197. }
  3198. }
  3199. /*******************************************************************
  3200. * output_sources
  3201. */
  3202. static void output_sources( struct makefile *make )
  3203. {
  3204. struct strarray all_targets = empty_strarray;
  3205. struct incl_file *source;
  3206. unsigned int i, j;
  3207. strarray_add_uniq( &make->phony_targets, "all" );
  3208. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3209. {
  3210. char *obj = xstrdup( source->name );
  3211. char *ext = get_extension( obj );
  3212. if (!ext) fatal_error( "unsupported file type %s\n", source->name );
  3213. *ext++ = 0;
  3214. for (j = 0; output_source_funcs[j].ext; j++)
  3215. if (!strcmp( ext, output_source_funcs[j].ext )) break;
  3216. output_source_funcs[j].fn( make, source, obj );
  3217. strarray_addall_uniq( &make->dependencies, source->dependencies );
  3218. }
  3219. /* special case for winetest: add resource files from other test dirs */
  3220. if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
  3221. {
  3222. struct strarray tests = enable_tests;
  3223. if (!tests.count)
  3224. for (i = 0; i < subdirs.count; i++)
  3225. if (submakes[i]->testdll && !submakes[i]->disabled)
  3226. strarray_add( &tests, submakes[i]->testdll );
  3227. for (i = 0; i < tests.count; i++)
  3228. strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
  3229. }
  3230. if (make->dlldata_files.count)
  3231. {
  3232. output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
  3233. tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
  3234. output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
  3235. output_filenames( make->dlldata_files );
  3236. output( "\n" );
  3237. }
  3238. if (make->staticlib) output_static_lib( make );
  3239. else if (make->module)
  3240. {
  3241. output_module( make );
  3242. if (*dll_ext && !make->is_cross) output_fake_module( make );
  3243. if (make->unixlib) output_unix_lib( make );
  3244. if (make->importlib) output_import_lib( make );
  3245. }
  3246. else if (make->testdll) output_test_module( make );
  3247. else if (make->sharedlib) output_shared_lib( make );
  3248. else if (make->programs.count) output_programs( make );
  3249. for (i = 0; i < make->scripts.count; i++)
  3250. add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
  3251. strmake( "S$(bindir)/%s", make->scripts.str[i] ));
  3252. for (i = 0; i < make->extra_targets.count; i++)
  3253. if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
  3254. strarray_add( &make->clean_files, make->extra_targets.str[i] );
  3255. else
  3256. strarray_add( &make->all_targets, make->extra_targets.str[i] );
  3257. if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
  3258. strarray_add( &make->distclean_files, "Makefile" );
  3259. if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
  3260. if (!make->obj_dir)
  3261. strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
  3262. else if (!strcmp( make->obj_dir, "po" ))
  3263. strarray_add( &make->distclean_files, "LINGUAS" );
  3264. strarray_addall( &make->clean_files, make->object_files );
  3265. strarray_addall( &make->clean_files, make->crossobj_files );
  3266. strarray_addall( &make->clean_files, make->implib_files );
  3267. strarray_addall( &make->clean_files, make->crossimplib_files );
  3268. strarray_addall( &make->clean_files, make->unixobj_files );
  3269. strarray_addall( &make->clean_files, make->res_files );
  3270. strarray_addall( &make->clean_files, make->pot_files );
  3271. strarray_addall( &make->clean_files, make->debug_files );
  3272. strarray_addall( &make->clean_files, make->all_targets );
  3273. if (make == top_makefile)
  3274. {
  3275. output_subdirs( make );
  3276. return;
  3277. }
  3278. strarray_addall( &all_targets, make->all_targets );
  3279. strarray_addall( &all_targets, make->font_files );
  3280. if (all_targets.count)
  3281. {
  3282. output( "%s:", obj_dir_path( make, "all" ));
  3283. output_filenames_obj_dir( make, all_targets );
  3284. output( "\n" );
  3285. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
  3286. }
  3287. output_install_rules( make, INSTALL_LIB, "install-lib" );
  3288. output_install_rules( make, INSTALL_DEV, "install-dev" );
  3289. if (make->clean_files.count)
  3290. {
  3291. output( "%s::\n", obj_dir_path( make, "clean" ));
  3292. output( "\trm -f" );
  3293. output_filenames_obj_dir( make, make->clean_files );
  3294. output( "\n" );
  3295. strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
  3296. }
  3297. }
  3298. /*******************************************************************
  3299. * create_temp_file
  3300. */
  3301. static FILE *create_temp_file( const char *orig )
  3302. {
  3303. char *name = xmalloc( strlen(orig) + 13 );
  3304. unsigned int i, id = getpid();
  3305. int fd;
  3306. FILE *ret = NULL;
  3307. for (i = 0; i < 100; i++)
  3308. {
  3309. sprintf( name, "%s.tmp%08x", orig, id );
  3310. if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
  3311. {
  3312. ret = fdopen( fd, "w" );
  3313. break;
  3314. }
  3315. if (errno != EEXIST) break;
  3316. id += 7777;
  3317. }
  3318. if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
  3319. temp_file_name = name;
  3320. return ret;
  3321. }
  3322. /*******************************************************************
  3323. * rename_temp_file
  3324. */
  3325. static void rename_temp_file( const char *dest )
  3326. {
  3327. int ret = rename( temp_file_name, dest );
  3328. if (ret == -1 && errno == EEXIST)
  3329. {
  3330. /* rename doesn't overwrite on windows */
  3331. unlink( dest );
  3332. ret = rename( temp_file_name, dest );
  3333. }
  3334. if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
  3335. temp_file_name = NULL;
  3336. }
  3337. /*******************************************************************
  3338. * are_files_identical
  3339. */
  3340. static int are_files_identical( FILE *file1, FILE *file2 )
  3341. {
  3342. for (;;)
  3343. {
  3344. char buffer1[8192], buffer2[8192];
  3345. int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
  3346. int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
  3347. if (size1 != size2) return 0;
  3348. if (!size1) return feof( file1 ) && feof( file2 );
  3349. if (memcmp( buffer1, buffer2, size1 )) return 0;
  3350. }
  3351. }
  3352. /*******************************************************************
  3353. * rename_temp_file_if_changed
  3354. */
  3355. static void rename_temp_file_if_changed( const char *dest )
  3356. {
  3357. FILE *file1, *file2;
  3358. int do_rename = 1;
  3359. if ((file1 = fopen( dest, "r" )))
  3360. {
  3361. if ((file2 = fopen( temp_file_name, "r" )))
  3362. {
  3363. do_rename = !are_files_identical( file1, file2 );
  3364. fclose( file2 );
  3365. }
  3366. fclose( file1 );
  3367. }
  3368. if (!do_rename)
  3369. {
  3370. unlink( temp_file_name );
  3371. temp_file_name = NULL;
  3372. }
  3373. else rename_temp_file( dest );
  3374. }
  3375. /*******************************************************************
  3376. * output_linguas
  3377. */
  3378. static void output_linguas( const struct makefile *make )
  3379. {
  3380. const char *dest = obj_dir_path( make, "LINGUAS" );
  3381. struct incl_file *source;
  3382. output_file = create_temp_file( dest );
  3383. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3384. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3385. if (strendswith( source->name, ".po" ))
  3386. output( "%s\n", replace_extension( source->name, ".po", "" ));
  3387. if (fclose( output_file )) fatal_perror( "write" );
  3388. output_file = NULL;
  3389. rename_temp_file_if_changed( dest );
  3390. }
  3391. /*******************************************************************
  3392. * output_testlist
  3393. */
  3394. static void output_testlist( const struct makefile *make )
  3395. {
  3396. const char *dest = obj_dir_path( make, "testlist.c" );
  3397. unsigned int i;
  3398. output_file = create_temp_file( dest );
  3399. output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
  3400. output( "#define WIN32_LEAN_AND_MEAN\n" );
  3401. output( "#include <windows.h>\n\n" );
  3402. output( "#define STANDALONE\n" );
  3403. output( "#include \"wine/test.h\"\n\n" );
  3404. for (i = 0; i < make->test_files.count; i++)
  3405. output( "extern void func_%s(void);\n", make->test_files.str[i] );
  3406. output( "\n" );
  3407. output( "const struct test winetest_testlist[] =\n" );
  3408. output( "{\n" );
  3409. for (i = 0; i < make->test_files.count; i++)
  3410. output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
  3411. output( " { 0, 0 }\n" );
  3412. output( "};\n" );
  3413. if (fclose( output_file )) fatal_perror( "write" );
  3414. output_file = NULL;
  3415. rename_temp_file_if_changed( dest );
  3416. }
  3417. /*******************************************************************
  3418. * output_gitignore
  3419. */
  3420. static void output_gitignore( const char *dest, struct strarray files )
  3421. {
  3422. int i;
  3423. output_file = create_temp_file( dest );
  3424. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3425. for (i = 0; i < files.count; i++)
  3426. {
  3427. if (!strchr( files.str[i], '/' )) output( "/" );
  3428. output( "%s\n", files.str[i] );
  3429. }
  3430. if (fclose( output_file )) fatal_perror( "write" );
  3431. output_file = NULL;
  3432. rename_temp_file( dest );
  3433. }
  3434. /*******************************************************************
  3435. * output_stub_makefile
  3436. */
  3437. static void output_stub_makefile( struct makefile *make )
  3438. {
  3439. struct strarray targets = empty_strarray;
  3440. const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
  3441. if (make->obj_dir) create_dir( make->obj_dir );
  3442. output_file_name = obj_dir_path( make, "Makefile" );
  3443. output_file = create_temp_file( output_file_name );
  3444. output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
  3445. if (make_var) output( "MAKE = %s\n\n", make_var );
  3446. output( "all:\n" );
  3447. if (make->all_targets.count) strarray_add( &targets, "all" );
  3448. if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
  3449. strarray_add( &targets, "install" );
  3450. if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
  3451. if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
  3452. if (make->clean_files.count) strarray_add( &targets, "clean" );
  3453. if (make->test_files.count)
  3454. {
  3455. strarray_add( &targets, "check" );
  3456. strarray_add( &targets, "test" );
  3457. strarray_add( &targets, "testclean" );
  3458. }
  3459. output_filenames( targets );
  3460. output_filenames( make->clean_files );
  3461. output( ":\n" );
  3462. output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
  3463. output( ".PHONY:" );
  3464. output_filenames( targets );
  3465. output( "\n" );
  3466. fclose( output_file );
  3467. output_file = NULL;
  3468. rename_temp_file( output_file_name );
  3469. }
  3470. /*******************************************************************
  3471. * output_silent_rules
  3472. */
  3473. static void output_silent_rules(void)
  3474. {
  3475. static const char *cmds[] =
  3476. {
  3477. "BISON",
  3478. "BUILD",
  3479. "CC",
  3480. "CCLD",
  3481. "FLEX",
  3482. "GEN",
  3483. "LN",
  3484. "MSG",
  3485. "SED",
  3486. "TEST",
  3487. "WIDL",
  3488. "WMC",
  3489. "WRC"
  3490. };
  3491. unsigned int i;
  3492. output( "V = 0\n" );
  3493. for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
  3494. {
  3495. output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
  3496. output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
  3497. output( "quiet_%s_1 =\n", cmds[i] );
  3498. }
  3499. }
  3500. /*******************************************************************
  3501. * output_top_makefile
  3502. */
  3503. static void output_top_makefile( struct makefile *make )
  3504. {
  3505. char buffer[1024];
  3506. FILE *src_file;
  3507. int i, found = 0;
  3508. output_file_name = obj_dir_path( make, output_makefile_name );
  3509. output_file = create_temp_file( output_file_name );
  3510. /* copy the contents of the source makefile */
  3511. src_file = open_input_makefile( make );
  3512. while (fgets( buffer, sizeof(buffer), src_file ) && !found)
  3513. {
  3514. if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
  3515. found = !strncmp( buffer, separator, strlen(separator) );
  3516. }
  3517. if (fclose( src_file )) fatal_perror( "close" );
  3518. input_file_name = NULL;
  3519. if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
  3520. if (silent_rules) output_silent_rules();
  3521. for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
  3522. output_sources( make );
  3523. /* disable implicit rules */
  3524. output( ".SUFFIXES:\n" );
  3525. fclose( output_file );
  3526. output_file = NULL;
  3527. rename_temp_file( output_file_name );
  3528. }
  3529. /*******************************************************************
  3530. * output_dependencies
  3531. */
  3532. static void output_dependencies( struct makefile *make )
  3533. {
  3534. struct strarray ignore_files = empty_strarray;
  3535. if (make->obj_dir) create_dir( make->obj_dir );
  3536. if (make == top_makefile) output_top_makefile( make );
  3537. else output_stub_makefile( make );
  3538. strarray_addall( &ignore_files, make->distclean_files );
  3539. strarray_addall( &ignore_files, make->clean_files );
  3540. if (make->testdll) output_testlist( make );
  3541. if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
  3542. if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
  3543. create_file_directories( make, ignore_files );
  3544. output_file_name = NULL;
  3545. }
  3546. /*******************************************************************
  3547. * load_sources
  3548. */
  3549. static void load_sources( struct makefile *make )
  3550. {
  3551. static const char *source_vars[] =
  3552. {
  3553. "SOURCES",
  3554. "C_SRCS",
  3555. "OBJC_SRCS",
  3556. "RC_SRCS",
  3557. "MC_SRCS",
  3558. "IDL_SRCS",
  3559. "BISON_SRCS",
  3560. "LEX_SRCS",
  3561. "HEADER_SRCS",
  3562. "XTEMPLATE_SRCS",
  3563. "SVG_SRCS",
  3564. "FONT_SRCS",
  3565. "IN_SRCS",
  3566. "PO_SRCS",
  3567. "MANPAGES",
  3568. NULL
  3569. };
  3570. const char **var;
  3571. unsigned int i;
  3572. struct strarray value;
  3573. struct incl_file *file;
  3574. strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
  3575. strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
  3576. make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
  3577. make->module = get_expanded_make_variable( make, "MODULE" );
  3578. make->testdll = get_expanded_make_variable( make, "TESTDLL" );
  3579. make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
  3580. make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
  3581. make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
  3582. make->extlib = get_expanded_make_variable( make, "EXTLIB" );
  3583. if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
  3584. make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
  3585. make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
  3586. make->imports = get_expanded_make_var_array( make, "IMPORTS" );
  3587. make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
  3588. make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
  3589. make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
  3590. make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
  3591. make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
  3592. if (make->extlib) make->staticlib = make->extlib;
  3593. if (make->staticlib) make->module = make->staticlib;
  3594. if (host_cpu)
  3595. {
  3596. value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
  3597. if (value.count) make->extradllflags = value;
  3598. }
  3599. make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
  3600. make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
  3601. make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
  3602. !strarray_exists( &make->extradllflags, "-mcygwin" );
  3603. make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
  3604. strarray_exists( &make->extradllflags, "-mwindows" );
  3605. make->native_unix_lib = !!make->unixlib;
  3606. if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
  3607. if (make->module && !make->install_lib.count && !make->install_dev.count)
  3608. {
  3609. if (make->importlib) strarray_add( &make->install_dev, make->importlib );
  3610. if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
  3611. else strarray_add( &make->install_lib, make->module );
  3612. }
  3613. make->include_paths = empty_strarray;
  3614. make->include_args = empty_strarray;
  3615. make->define_args = empty_strarray;
  3616. if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
  3617. value = get_expanded_make_var_array( make, "EXTRAINCL" );
  3618. for (i = 0; i < value.count; i++)
  3619. if (!strncmp( value.str[i], "-I", 2 ))
  3620. strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
  3621. else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
  3622. strarray_add_uniq( &make->define_args, value.str[i] );
  3623. strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
  3624. strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
  3625. if (make->src_dir)
  3626. strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
  3627. if (make->parent_dir)
  3628. strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
  3629. strarray_add( &make->include_args, "-Iinclude" );
  3630. if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
  3631. list_init( &make->sources );
  3632. list_init( &make->includes );
  3633. for (var = source_vars; *var; var++)
  3634. {
  3635. value = get_expanded_make_var_array( make, *var );
  3636. for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
  3637. }
  3638. add_generated_sources( make );
  3639. if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
  3640. if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
  3641. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
  3642. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
  3643. make->is_cross = crosstarget && make->use_msvcrt;
  3644. if (!*dll_ext || make->is_cross)
  3645. for (i = 0; i < make->delayimports.count; i++)
  3646. strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
  3647. }
  3648. /*******************************************************************
  3649. * parse_makeflags
  3650. */
  3651. static void parse_makeflags( const char *flags )
  3652. {
  3653. const char *p = flags;
  3654. char *var, *buffer = xmalloc( strlen(flags) + 1 );
  3655. while (*p)
  3656. {
  3657. while (isspace(*p)) p++;
  3658. var = buffer;
  3659. while (*p && !isspace(*p))
  3660. {
  3661. if (*p == '\\' && p[1]) p++;
  3662. *var++ = *p++;
  3663. }
  3664. *var = 0;
  3665. if (var > buffer) set_make_variable( &cmdline_vars, buffer );
  3666. }
  3667. }
  3668. /*******************************************************************
  3669. * parse_option
  3670. */
  3671. static int parse_option( const char *opt )
  3672. {
  3673. if (opt[0] != '-')
  3674. {
  3675. if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
  3676. return 0;
  3677. }
  3678. switch(opt[1])
  3679. {
  3680. case 'f':
  3681. if (opt[2]) output_makefile_name = opt + 2;
  3682. break;
  3683. case 'R':
  3684. relative_dir_mode = 1;
  3685. break;
  3686. case 'S':
  3687. silent_rules = 1;
  3688. break;
  3689. default:
  3690. fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
  3691. exit(1);
  3692. }
  3693. return 1;
  3694. }
  3695. /*******************************************************************
  3696. * main
  3697. */
  3698. int main( int argc, char *argv[] )
  3699. {
  3700. const char *makeflags = getenv( "MAKEFLAGS" );
  3701. int i, j;
  3702. if (makeflags) parse_makeflags( makeflags );
  3703. i = 1;
  3704. while (i < argc)
  3705. {
  3706. if (parse_option( argv[i] ))
  3707. {
  3708. for (j = i; j < argc; j++) argv[j] = argv[j+1];
  3709. argc--;
  3710. }
  3711. else i++;
  3712. }
  3713. if (relative_dir_mode)
  3714. {
  3715. char *relpath;
  3716. if (argc != 3)
  3717. {
  3718. fprintf( stderr, "Option -R needs two directories\n%s", Usage );
  3719. exit( 1 );
  3720. }
  3721. relpath = get_relative_path( argv[1], argv[2] );
  3722. printf( "%s\n", relpath ? relpath : "." );
  3723. exit( 0 );
  3724. }
  3725. if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
  3726. atexit( cleanup_files );
  3727. signal( SIGTERM, exit_on_signal );
  3728. signal( SIGINT, exit_on_signal );
  3729. #ifdef SIGHUP
  3730. signal( SIGHUP, exit_on_signal );
  3731. #endif
  3732. for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
  3733. top_makefile = parse_makefile( NULL );
  3734. target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
  3735. msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
  3736. dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
  3737. extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
  3738. extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
  3739. unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
  3740. cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
  3741. lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
  3742. libs = get_expanded_make_var_array( top_makefile, "LIBS" );
  3743. enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
  3744. top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
  3745. top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
  3746. delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
  3747. root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
  3748. tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
  3749. tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
  3750. exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
  3751. man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
  3752. dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
  3753. host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
  3754. crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
  3755. crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
  3756. fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
  3757. convert = get_expanded_make_variable( top_makefile, "CONVERT" );
  3758. flex = get_expanded_make_variable( top_makefile, "FLEX" );
  3759. bison = get_expanded_make_variable( top_makefile, "BISON" );
  3760. ar = get_expanded_make_variable( top_makefile, "AR" );
  3761. ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
  3762. rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
  3763. icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
  3764. dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
  3765. msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
  3766. sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
  3767. ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
  3768. if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
  3769. if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
  3770. if (!exe_ext) exe_ext = "";
  3771. if (!tools_ext) tools_ext = "";
  3772. if (!man_ext) man_ext = "3w";
  3773. if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
  3774. {
  3775. so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
  3776. pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
  3777. }
  3778. else
  3779. so_dir = pe_dir = "$(dlldir)";
  3780. extra_cflags_extlib = remove_warning_flags( extra_cflags );
  3781. extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
  3782. top_makefile->src_dir = root_src_dir;
  3783. subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
  3784. disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
  3785. submakes = xmalloc( subdirs.count * sizeof(*submakes) );
  3786. for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
  3787. load_sources( top_makefile );
  3788. for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
  3789. output_dependencies( top_makefile );
  3790. for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
  3791. return 0;
  3792. }