makedep.c 153 KB

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